// initialization
void cpu_65c02_init(
struct cpu_65c02 *self,
- int (*read_byte)(int addr),
- void (*write_byte)(int addr, int data)
+ int (*read_byte)(void *context, int addr),
+ void *read_byte_context,
+ void (*write_byte)(void *context, int addr, int data),
+ void *write_byte_context
) {
memset(self, 0, sizeof(struct cpu_65c02));
self->regs.byte.p = 0x30; // unused bits are hard coded to 1
self->regs.word.sp = 0x1ff; // unused byte is hard coded to 01
self->read_byte = read_byte;
+ self->read_byte_context = read_byte_context;
self->write_byte = write_byte;
+ self->write_byte_context = write_byte_context;
}
// instruction decode
struct cpu_65c02 {
int cycles;
- int (*read_byte)(int addr);
- void (*write_byte)(int addr, int data);
+ int (*read_byte)(void *context, int addr);
+ void *read_byte_context;
+ void (*write_byte)(void *context, int addr, int data);
+ void *write_byte_context;
union cpu_65c02_regs regs;
};
return self->regs.mem_le[addr - CPU_65C02_EA_A];
#endif
self->cycles += 1;
- return self->read_byte(addr & 0xffff);
+ return self->read_byte(self->read_byte_context, addr & 0xffff);
}
static ALWAYS_INLINE int cpu_65c02_read_word(struct cpu_65c02 *self, int addr) {
self->regs.mem_le[addr - CPU_65C02_EA_A] = data;
#endif
else
- self->write_byte(addr, data);
+ self->write_byte(self->write_byte_context, addr, data);
}
static ALWAYS_INLINE void cpu_65c02_write_word(struct cpu_65c02 *self, int addr, int data) {
// prototypes
void cpu_65c02_init(
struct cpu_65c02 *self,
- int (*read_byte)(int addr),
- void (*write_byte)(int addr, int data)
+ int (*read_byte)(void *context, int addr),
+ void *read_byte_context,
+ void (*write_byte)(void *context, int addr, int data),
+ void *write_byte_context
);
void cpu_65c02_execute(struct cpu_65c02 *self);
// initialization
void cpu_z80_init(
struct cpu_z80 *self,
- int (*read_byte)(int addr),
- void (*write_byte)(int addr, int data),
- int (*in_byte)(int addr),
- void (*out_byte)(int addr, int data)
+ int (*read_byte)(void *context, int addr),
+ void *read_byte_context,
+ void (*write_byte)(void *context, int addr, int data),
+ void *write_byte_context,
+ int (*in_byte)(void *context, int addr),
+ void *in_byte_context,
+ void (*out_byte)(void *context, int addr, int data),
+ void *out_byte_context
) {
memset(self, 0, sizeof(struct cpu_z80));
self->read_byte = read_byte;
+ self->read_byte_context = read_byte_context;
self->write_byte = write_byte;
+ self->write_byte_context = write_byte_context;
self->in_byte = in_byte;
+ self->in_byte_context = in_byte_context;
self->out_byte = out_byte;
+ self->out_byte_context = out_byte_context;
}
// instruction decode
struct cpu_z80 {
int cycles;
- int (*read_byte)(int addr);
- void (*write_byte)(int addr, int data);
- int (*in_byte)(int addr);
- void (*out_byte)(int addr, int data);
+ int (*read_byte)(void *context, int addr);
+ void *read_byte_context;
+ void (*write_byte)(void *context, int addr, int data);
+ void *write_byte_context;
+ int (*in_byte)(void *context, int addr);
+ void *in_byte_context;
+ void (*out_byte)(void *context, int addr, int data);
+ void *out_byte_context;
union cpu_z80_regs regs;
};
return self->regs.mem_le[addr - CPU_Z80_EA_F];
#endif
self->cycles += 1;
- return self->read_byte(addr & 0xffff);
+ return self->read_byte(self->read_byte_context, addr & 0xffff);
}
static ALWAYS_INLINE int cpu_z80_read_word(struct cpu_z80 *self, int addr) {
self->regs.mem_le[addr - CPU_Z80_EA_F] = data;
#endif
else
- self->write_byte(addr, data);
+ self->write_byte(self->write_byte_context, addr, data);
}
static ALWAYS_INLINE void cpu_z80_write_word(struct cpu_z80 *self, int addr, int data) {
static ALWAYS_INLINE int cpu_z80_in_byte(struct cpu_z80 *self, int addr) {
self->cycles += 1;
- return self->in_byte(addr);
+ return self->in_byte(self->in_byte_context, addr);
}
static ALWAYS_INLINE void cpu_z80_out_byte(struct cpu_z80 *self, int addr, int data) {
self->cycles += 1;
- self->out_byte(addr, data);
+ self->out_byte(self->out_byte_context, addr, data);
}
// effective address calculation
// prototypes
void cpu_z80_init(
struct cpu_z80 *self,
- int (*read_byte)(int addr),
- void (*write_byte)(int addr, int data),
- int (*in_byte)(int addr),
- void (*out_byte)(int addr, int data)
+ int (*read_byte)(void *context, int addr),
+ void *read_byte_context,
+ void (*write_byte)(void *context, int addr, int data),
+ void *write_byte_context,
+ int (*in_byte)(void *context, int addr),
+ void *in_byte_context,
+ void (*out_byte)(void *context, int addr, int data),
+ void *out_byte_context
);
void cpu_z80_execute(struct cpu_z80 *self);
void cpu_z80_execute_cb(struct cpu_z80 *self);
return entry_point;
}
-int read_byte(int addr) {
+int read_byte(void *context, int addr) {
#if MEM_TRACE
int data = mem[addr];
fprintf(stderr, "addr=%04x rd=%02x\n", addr, data);
#endif
}
-void write_byte(int addr, int data) {
+void write_byte(void *context, int addr, int data) {
#if MEM_TRACE
fprintf(stderr, "addr=%04x wr=%02x\n", addr, data);
#endif
#if ALT_BACKEND
uint8_t mem_read(uint16_t addr, bool isDbg) {
- return read_byte(addr);
+ return read_byte(NULL, addr);
}
void mem_write(uint16_t addr, uint8_t val) {
- write_byte(addr, val);
+ write_byte(NULL, addr, val);
}
#endif
#if REG_TRACE
fprintf(
stderr,
- "pc=%04x a=%02x x=%02x y=%02x p=%02x s=%02x cf=%d zf=%d if=%d df=%d vf=%d nf=%d\n",
+ "pc=%04x a=%02x x=%02x y=%02x s=%02x p=%02x cf=%d zf=%d if=%d df=%d vf=%d nf=%d\n",
vrEmu6502GetPC(cpu),
vrEmu6502GetAcc(cpu),
vrEmu6502GetX(cpu),
vrEmu6502GetY(cpu),
- vrEmu6502GetStatus(cpu) | 0x30,
vrEmu6502GetStackPointer(cpu),
+ vrEmu6502GetStatus(cpu) | 0x30,
vrEmu6502GetStatus(cpu) & 1,
(vrEmu6502GetStatus(cpu) >> 1) & 1,
(vrEmu6502GetStatus(cpu) >> 2) & 1,
}
#else
struct cpu_65c02 cpu;
- cpu_65c02_init(&cpu, read_byte, write_byte);
+ cpu_65c02_init(&cpu, read_byte, NULL, write_byte, NULL);
cpu.regs.word.pc = entry_point;
while (true) {
#if REG_TRACE
fprintf(
stderr,
- "pc=%04x a=%02x x=%02x y=%02x p=%02x s=%02x cf=%d zf=%d if=%d df=%d vf=%d nf=%d\n",
+ "pc=%04x a=%02x x=%02x y=%02x s=%02x p=%02x cf=%d zf=%d if=%d df=%d vf=%d nf=%d\n",
cpu.regs.word.pc,
cpu.regs.byte.a,
cpu.regs.byte.x,
cpu.regs.byte.y,
- cpu.regs.byte.p,
cpu.regs.byte.s,
+ cpu.regs.byte.p,
cpu.regs.bit.cf,
cpu.regs.bit.zf,
cpu.regs.bit._if,
return de;
}
-int read_byte(int addr) {
+int read_byte(void *context, int addr) {
#if MEM_TRACE
int data = mem[addr];
fprintf(stderr, "addr=%04x rd=%02x\n", addr, data);
#endif
}
-void write_byte(int addr, int data) {
+void write_byte(void *context, int addr, int data) {
#if MEM_TRACE
fprintf(stderr, "addr=%04x wr=%02x\n", addr, data);
#endif
mem[addr] = data;
}
-int in_byte(int addr) {
+int in_byte(void *context, int addr) {
addr &= 0xff;
#if IO_TRACE
int data = io_read[addr];
#endif
}
-void out_byte(int addr, int data) {
+void out_byte(void *context, int addr, int data) {
addr &= 0xff;
#if IO_TRACE
printf("io=%02x wr=%02x\n", addr, data);
#if ALT_BACKEND
uint8_t rb(void *userdata, uint16_t addr) {
- return read_byte(addr);
+ return read_byte(NULL, addr);
}
void wb(void *userdata, uint16_t addr, uint8_t val) {
- write_byte(addr, val);
+ write_byte(NULL, addr, val);
}
uint8_t in(z80 *const z, uint8_t port) {
- return in_byte(port);
+ return in_byte(NULL, port);
}
void out(z80 *const z, uint8_t port, uint8_t val) {
- out_byte(port, val);
+ out_byte(NULL, port, val);
}
#endif
}
#else
struct cpu_z80 cpu;
- cpu_z80_init(&cpu, read_byte, write_byte, in_byte, out_byte);
+ cpu_z80_init(
+ &cpu,
+ read_byte,
+ NULL,
+ write_byte,
+ NULL,
+ in_byte,
+ NULL,
+ out_byte,
+ NULL
+ );
cpu.regs.word.af = 0xffff;
cpu.regs.word.sp = 0xffff;
cpu.regs.word.pc = entry_point;