nesemu/cpu/cpu.h

117 lines
2.4 KiB
C
Raw Normal View History

2023-11-26 12:11:49 -05:00
//
// Created by william on 10/15/23.
//
#ifndef CPU_CPU_H
#define CPU_CPU_H
#include <stdbool.h>
2023-12-03 00:27:07 -05:00
#include "memory.h"
2023-11-26 12:11:49 -05:00
// Reference: https://www.nesdev.org/wiki/Status_flags
#define CPU_STATUS_CARRY_MASK 0x01
#define CPU_STATUS_ZERO_MASK 0x02
#define CPU_STATUS_INTERRUPT_DISABLE_MASK 0x04
#define CPU_STATUS_DECIMAL_MASK 0x08
#define CPU_STATUS_B_MASK 0x10
#define CPU_STATUS_OVERFLOW_MASK 0x40
#define CPU_STATUS_NEGATIVE_MASK 0x80
#define CPU_STACK_ADDR 0x0100
2024-05-06 20:23:44 -04:00
// Reference: https://www.nesdev.org/obelisk-6502-guide/registers.html
typedef struct cpu {
address program_counter;
byte stack_pointer;
byte accumulator;
byte x;
byte y;
byte status;
bool oam_dma_triggered;
bool nmi_requested;
2024-05-17 00:33:37 -04:00
unsigned int busy_cycle_count;
2024-05-06 20:23:44 -04:00
} CPU;
CPU *cpu_get_state();
2024-01-06 14:27:09 -05:00
/**
* Gets a flag from the CPU registers.
*
* @param system The system
* @param mask The flag mask
* @return The value of the flag.
*/
2024-05-06 20:23:44 -04:00
bool cpu_get_flag(byte mask);
2024-01-06 14:27:09 -05:00
/**
* Sets a flag in the CPU registers.
*
* @param system The system
* @param mask The flag mask
* @param set If the flag is set or not
*/
2024-05-06 20:23:44 -04:00
void cpu_set_flag(byte mask, bool set);
2024-01-06 14:27:09 -05:00
/**
* Gets the next byte in the program.
* Increases the system program counter.
*
* @param system The system
* @return The value of the next byte.
*/
2024-05-06 20:23:44 -04:00
byte cpu_get_next_byte();
2024-01-06 14:27:09 -05:00
/**
* Gets the next word in the program.
* Increases the system program counter by 2.
*
* @param system The system
* @return The value of the next word.
*/
2024-05-06 20:23:44 -04:00
word cpu_get_next_word();
2023-12-23 17:10:31 -05:00
2024-01-06 14:27:09 -05:00
/**
* Pushes a byte in to the stack.
*
* @param system The system
* @param value The value to push to the stack
*/
2024-05-06 20:23:44 -04:00
void cpu_stack_push(byte value);
2023-11-26 12:11:49 -05:00
2024-01-06 14:27:09 -05:00
/**
* Pushes the execution context to the stack.
* This includes the program counter and the CPU status.
*
* @param system The system
*/
2024-05-06 20:23:44 -04:00
void cpu_stack_push_context();
2023-11-26 12:11:49 -05:00
2024-01-06 14:27:09 -05:00
/**
* Pops a byte from the stack.
*
* @param system The system
* @return The value of the byte
*/
2024-05-06 20:23:44 -04:00
byte cpu_stack_pop();
2023-11-26 12:11:49 -05:00
2024-01-06 14:27:09 -05:00
/**
* Pops an execution context from the stack and overwrite the current context.
* This includes the program counter and the CPU status.
*
* @param system The system
*/
2024-05-06 20:23:44 -04:00
void cpu_stack_pop_context();
2023-11-26 12:11:49 -05:00
2024-01-06 14:27:09 -05:00
/**
* Adds wait cycles to the CPU.
*
* @param cycle_count The number of cycle to wait
*/
2024-05-06 20:23:44 -04:00
void cpu_add_cycles(unsigned int cycle_count);
void cpu_trigger_oam_dma();
void cpu_trigger_nmi();
2023-12-23 16:35:23 -05:00
2023-11-26 12:11:49 -05:00
#endif //CPU_CPU_H