From 8898c3e59da94048c3e513cb0630d8a173683fbb Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Wed, 10 Jan 2018 20:12:48 -0600 Subject: [PATCH] Use macros to define segment read/write map functions --- include/apple2.mem.h | 6 +++--- include/vm_segment.h | 6 ++++++ src/apple2.mem.c | 21 +++++++++------------ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/include/apple2.mem.h b/include/apple2.mem.h index 7803c0d..8165be8 100644 --- a/include/apple2.mem.h +++ b/include/apple2.mem.h @@ -45,10 +45,10 @@ #define APPLE2_BANK_OFFSET 0xD000 -extern vm_8bit apple2_mem_read_bank(vm_segment *, size_t, void *); -extern void apple2_mem_write_bank(vm_segment *, size_t, vm_8bit, void *); -extern void apple2_mem_map(apple2 *); +extern SEGMENT_READER(apple2_mem_read_bank); +extern SEGMENT_WRITER(apple2_mem_write_bank); extern int apple2_mem_init_peripheral_rom(apple2 *); extern int apple2_mem_init_sys_rom(apple2 *); +extern void apple2_mem_map(apple2 *); #endif diff --git a/include/vm_segment.h b/include/vm_segment.h index 640a67e..481ff77 100644 --- a/include/vm_segment.h +++ b/include/vm_segment.h @@ -22,6 +22,12 @@ typedef struct vm_segment vm_segment; typedef vm_8bit (*vm_segment_read_fn)(vm_segment *, size_t, void *); typedef void (*vm_segment_write_fn)(vm_segment *, size_t, vm_8bit, void *); +#define SEGMENT_READER(x) \ + vm_8bit x (vm_segment *segment, size_t addr, void *_mach) + +#define SEGMENT_WRITER(x) \ + void x (vm_segment *segment, size_t addr, vm_8bit value, void *_mach) + /* * The bounds check is just some inline code to try and cut down on the * cost of it. diff --git a/src/apple2.mem.c b/src/apple2.mem.c index 270c3e6..a6df9f7 100644 --- a/src/apple2.mem.c +++ b/src/apple2.mem.c @@ -10,8 +10,7 @@ * Return a byte of memory from a bank-switchable address. This may be * from ROM, from main memory, or from the "extra" 4k bank of RAM. */ -vm_8bit -apple2_mem_read_bank(vm_segment *segment, size_t address, void *_mach) +SEGMENT_READER(apple2_mem_read_bank) { apple2 *mach; @@ -20,30 +19,28 @@ apple2_mem_read_bank(vm_segment *segment, size_t address, void *_mach) if (mach->bank_switch & MEMORY_ROM) { // We need to account for the difference in address location // before we can successfully get any data from ROM. - return vm_segment_get(mach->rom, address - APPLE2_BANK_OFFSET); + return vm_segment_get(mach->rom, addr - APPLE2_BANK_OFFSET); } // If the address is $D000..$DFFF, then we may need to get the byte // from the ram2 bank. - if (address < 0xE000 && mach->bank_switch & MEMORY_RAM2) { + if (addr < 0xE000 && mach->bank_switch & MEMORY_RAM2) { // The same caution holds for getting data from the // second RAM bank. return vm_segment_get(mach->ram2, - address - APPLE2_BANK_OFFSET); + addr - APPLE2_BANK_OFFSET); } // Otherwise, the byte is returned from bank 1 RAM, which is the // literal memory available in the segment. - return segment->memory[address]; + return segment->memory[addr]; } /* * Write a byte into bank-switchable memory. Many of the same cautions, * notes, etc. written for the read function apply here as well. */ -void -apple2_mem_write_bank(vm_segment *segment, - size_t address, vm_8bit value, void *_mach) +SEGMENT_WRITER(apple2_mem_write_bank) { apple2 *mach; @@ -63,16 +60,16 @@ apple2_mem_write_bank(vm_segment *segment, // If bank 2 RAM is turned on, and the address is in the $D000 // hexapage, then we write to our ram2 segment. - if (address < 0xE000 && mach->bank_switch & MEMORY_RAM2) { + if (addr < 0xE000 && mach->bank_switch & MEMORY_RAM2) { vm_segment_set(mach->ram2, - address - APPLE2_BANK_OFFSET, value); + addr - APPLE2_BANK_OFFSET, value); return; } // But if bank 2 RAM is not turned on, or the address is between // $E000 - $FFFF, then writes go to bank 1 RAM, which is our main // memory. - segment->memory[address] = value; + segment->memory[addr] = value; } /*