Add context argument to (read|write|in|out)_byte() functions
authorNick Downing <nick@ndcode.org>
Sat, 23 Jul 2022 01:35:09 +0000 (11:35 +1000)
committerNick Downing <nick@ndcode.org>
Sat, 23 Jul 2022 01:35:09 +0000 (11:35 +1000)
cpu_65c02.c
cpu_65c02.h
cpu_z80.c
cpu_z80.h
emu_65c02.c
emu_z80.c

index 9e7cbb4..26fc1b3 100644 (file)
@@ -6,14 +6,18 @@
 // 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
index 4c63053..35a188c 100644 (file)
@@ -106,8 +106,10 @@ union cpu_65c02_regs {
 
 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;
 };
 
@@ -120,7 +122,7 @@ static ALWAYS_INLINE int cpu_65c02_read_byte(struct cpu_65c02 *self, int addr) {
     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) {
@@ -142,7 +144,7 @@ static ALWAYS_INLINE void cpu_65c02_write_byte(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) {
@@ -506,8 +508,10 @@ static ALWAYS_INLINE void cpu_65c02_wai(struct cpu_65c02 *self) {
 // 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);
  
index 6f755ca..6e251f4 100644 (file)
--- a/cpu_z80.c
+++ b/cpu_z80.c
@@ -6,16 +6,24 @@
 // 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
index 18fea86..be7d978 100644 (file)
--- a/cpu_z80.h
+++ b/cpu_z80.h
@@ -206,10 +206,14 @@ union cpu_z80_regs {
 
 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;
 };
 
@@ -222,7 +226,7 @@ static ALWAYS_INLINE int cpu_z80_read_byte(struct cpu_z80 *self, int addr) {
     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) {
@@ -239,7 +243,7 @@ static ALWAYS_INLINE void cpu_z80_write_byte(struct cpu_z80 *self, int addr, int
     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) {
@@ -270,12 +274,12 @@ static ALWAYS_INLINE int cpu_z80_pop_word(struct cpu_z80 *self) {
 
 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
@@ -1154,10 +1158,14 @@ static ALWAYS_INLINE void cpu_z80_xor(struct cpu_z80 *self, int rvalue) {
 // 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);
index 876a3a3..6bd5470 100644 (file)
@@ -131,7 +131,7 @@ int load_ihx(char *name) {
   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);
@@ -141,7 +141,7 @@ int read_byte(int addr) {
 #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
@@ -150,11 +150,11 @@ void write_byte(int addr, int data) {
 
 #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
 
@@ -179,13 +179,13 @@ int main(int argc, char **argv) {
 #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,
@@ -205,20 +205,20 @@ int main(int argc, char **argv) {
   }
 #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,
index 40409d7..2913ae1 100644 (file)
--- a/emu_z80.c
+++ b/emu_z80.c
@@ -158,7 +158,7 @@ int bdos(int c, int de) {
   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);
@@ -168,14 +168,14 @@ int read_byte(int addr) {
 #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];
@@ -186,7 +186,7 @@ int in_byte(int 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);
@@ -196,19 +196,19 @@ void out_byte(int addr, int 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
 
@@ -276,7 +276,17 @@ int main(int argc, char **argv) {
   }
 #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;