🍯 Glaze

#ifndef __CPU_H__
#define __CPU_H__

#include 
#include 
#include 

#include "mapper.h"

typedef struct cpu6502_t cpu6502_t;

typedef struct opcode_t
{
	int inst;
	int addr_mode;
	int size;
	int cycles;
	
	/* DEBUG */
	char* inst_name;
} opcode_t;

/* Addressing modes */
#define ADDRMODE_ZP			0
#define ADDRMODE_REL		1
#define ADDRMODE_IMP		2
#define ADDRMODE_ABS		3
#define ADDRMODE_ACC		4
#define ADDRMODE_IMM		5
#define ADDRMODE_ZPX		6
#define ADDRMODE_ZPY		7
#define ADDRMODE_ABSX		8
#define ADDRMODE_ABSY		9
#define ADDRMODE_PREIDXIND	10
#define ADDRMODE_POSTIDXIND	11
#define ADDRMODE_INDABS		12

/* Processor status flags */
#define FLAG_CARRY		1
#define FLAG_ZERO		2
#define FLAG_INTERRUPT	4
#define FLAG_DECIMAL	8
#define FLAG_BREAK		16
#define FLAG_UNUSED		32
#define FLAG_OVERFLOW	64
#define FLAG_NEGATIVE	128

/* IRQs */
#define IRQ_NORMAL 0
#define IRQ_NMI 1
#define IRQ_RESET 2

/* cpu_init
 * Initializes the cpu6502_t struct.
 * [in/out] cpu6502_t** cpu - The return pointer. Expects NULL.
 */
void cpu_init(cpu6502_t** cpu);

/* cpu_kill
 * Kills the cpu6502_t struct.
 * [in] cpu6502_t* cpu - The emulated cpu.
 */
void cpu_kill(cpu6502_t* cpu);

/* cpu_reset
 * Resets the cpu.
 * [in] cpu6502_t* cpu - The emulated cpu.
 */
void cpu_reset(cpu6502_t* cpu);

/* cpu_set_mapper
 * Gives a memory mapper to the cpu.
 * [in] cpu6502_t* cpu - The emulated cpu.
 * [in] mapper_t* mapper - The mapper to use.
 */
void cpu_set_mapper(cpu6502_t* cpu, mapper_t* mapper);

/* cpu_request_irq
 * Requests an IRQ.
 * [in] cpu6502_t* cpu - The emulated cpu.
 * [in] int type - The IRQ type.
 */
void cpu_request_irq(cpu6502_t* cpu, int type);

/* cpu_emulate
 * Emulates a single instruction.
 * [in] cpu6502_t* cpu - The emulated cpu.
 * Returns: Number of cycles.
 */
int cpu_emulate(cpu6502_t* cpu);

/* cpu_dump_instructionlist
 * Dumps the instructionlist to stdout.
 * [in] cpu6502_t* cpu - The emulated cpu.
 */
void cpu_dump_instructiontable(cpu6502_t* cpu);

/* cpu_get_pc
 * Gets the program counter.
 * [in] cpu6502_t* cpu - The emulated cpu.
 * Returns: The program counter.
 */
int cpu_get_pc(cpu6502_t* cpu);

/* cpu_get_a
 * Gets the accumulator.
 * [in] cpu6502_t* cpu - The emulated cpu.
 * Returns: The accumulator.
 */
int cpu_get_a(cpu6502_t* cpu);

/* cpu_get_x
 * Gets the x register.
 * [in] cpu6502_t* cpu - The emulated cpu.
 * Returns: The x register.
 */
int cpu_get_x(cpu6502_t* cpu);

/* cpu_get_y
 * Gets the y register.
 * [in] cpu6502_t* cpu - The emulated cpu.
 * Returns: The y register.
 */
int cpu_get_y(cpu6502_t* cpu);

/* cpu_get_op
 * Translates a hex into an opcode_t struct.
 * [in] cpu6502_t* cpu - The emulated cpu.
 * [in] int hex - The hex to translate.
 * Returns: The correct opcode.
 */
opcode_t cpu_get_op(cpu6502_t* cpu, int hex);

#endif