nesemu/cpu/cpu.h

117 lines
2.4 KiB
C

//
// Created by william on 10/15/23.
//
#ifndef CPU_CPU_H
#define CPU_CPU_H
#include <stdbool.h>
#include "memory.h"
// 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
// 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;
unsigned int busy_cycle_count;
} CPU;
CPU *cpu_get_state();
/**
* Gets a flag from the CPU registers.
*
* @param system The system
* @param mask The flag mask
* @return The value of the flag.
*/
bool cpu_get_flag(byte mask);
/**
* 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
*/
void cpu_set_flag(byte mask, bool set);
/**
* Gets the next byte in the program.
* Increases the system program counter.
*
* @param system The system
* @return The value of the next byte.
*/
byte cpu_get_next_byte();
/**
* 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.
*/
word cpu_get_next_word();
/**
* Pushes a byte in to the stack.
*
* @param system The system
* @param value The value to push to the stack
*/
void cpu_stack_push(byte value);
/**
* Pushes the execution context to the stack.
* This includes the program counter and the CPU status.
*
* @param system The system
*/
void cpu_stack_push_context();
/**
* Pops a byte from the stack.
*
* @param system The system
* @return The value of the byte
*/
byte cpu_stack_pop();
/**
* 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
*/
void cpu_stack_pop_context();
/**
* Adds wait cycles to the CPU.
*
* @param cycle_count The number of cycle to wait
*/
void cpu_add_cycles(unsigned int cycle_count);
void cpu_trigger_oam_dma();
void cpu_trigger_nmi();
#endif //CPU_CPU_H