1
0
mirror of https://github.com/pevans/erc-c.git synced 2025-02-25 14:29:13 +00:00

Use macros to define segment read/write map functions

This commit is contained in:
Peter Evans 2018-01-10 20:12:48 -06:00
parent 3db536a83d
commit 8898c3e59d
3 changed files with 18 additions and 15 deletions

View File

@ -45,10 +45,10 @@
#define APPLE2_BANK_OFFSET 0xD000 #define APPLE2_BANK_OFFSET 0xD000
extern vm_8bit apple2_mem_read_bank(vm_segment *, size_t, void *); extern SEGMENT_READER(apple2_mem_read_bank);
extern void apple2_mem_write_bank(vm_segment *, size_t, vm_8bit, void *); extern SEGMENT_WRITER(apple2_mem_write_bank);
extern void apple2_mem_map(apple2 *);
extern int apple2_mem_init_peripheral_rom(apple2 *); extern int apple2_mem_init_peripheral_rom(apple2 *);
extern int apple2_mem_init_sys_rom(apple2 *); extern int apple2_mem_init_sys_rom(apple2 *);
extern void apple2_mem_map(apple2 *);
#endif #endif

View File

@ -22,6 +22,12 @@ typedef struct vm_segment vm_segment;
typedef vm_8bit (*vm_segment_read_fn)(vm_segment *, size_t, void *); 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 *); 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 * The bounds check is just some inline code to try and cut down on the
* cost of it. * cost of it.

View File

@ -10,8 +10,7 @@
* Return a byte of memory from a bank-switchable address. This may be * 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. * from ROM, from main memory, or from the "extra" 4k bank of RAM.
*/ */
vm_8bit SEGMENT_READER(apple2_mem_read_bank)
apple2_mem_read_bank(vm_segment *segment, size_t address, void *_mach)
{ {
apple2 *mach; apple2 *mach;
@ -20,30 +19,28 @@ apple2_mem_read_bank(vm_segment *segment, size_t address, void *_mach)
if (mach->bank_switch & MEMORY_ROM) { if (mach->bank_switch & MEMORY_ROM) {
// We need to account for the difference in address location // We need to account for the difference in address location
// before we can successfully get any data from ROM. // 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 // If the address is $D000..$DFFF, then we may need to get the byte
// from the ram2 bank. // 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 // The same caution holds for getting data from the
// second RAM bank. // second RAM bank.
return vm_segment_get(mach->ram2, 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 // Otherwise, the byte is returned from bank 1 RAM, which is the
// literal memory available in the segment. // 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, * Write a byte into bank-switchable memory. Many of the same cautions,
* notes, etc. written for the read function apply here as well. * notes, etc. written for the read function apply here as well.
*/ */
void SEGMENT_WRITER(apple2_mem_write_bank)
apple2_mem_write_bank(vm_segment *segment,
size_t address, vm_8bit value, void *_mach)
{ {
apple2 *mach; 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 // If bank 2 RAM is turned on, and the address is in the $D000
// hexapage, then we write to our ram2 segment. // 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, vm_segment_set(mach->ram2,
address - APPLE2_BANK_OFFSET, value); addr - APPLE2_BANK_OFFSET, value);
return; return;
} }
// But if bank 2 RAM is not turned on, or the address is between // 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 // $E000 - $FFFF, then writes go to bank 1 RAM, which is our main
// memory. // memory.
segment->memory[address] = value; segment->memory[addr] = value;
} }
/* /*