| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include "support/fake_devices.h" |
| 32 |
#include "support/harness.h" |
| 33 |
|
| 34 |
#include "memory.h" |
| 35 |
|
| 36 |
#include <stdint.h> |
| 37 |
|
| 38 |
#define RAM_BANK_REG 0x0000 |
| 39 |
#define ROM_BANK_REG 0x0001 |
| 40 |
|
| 41 |
#define BANKED_RAM_START 0xA000 |
| 42 |
#define BANKED_RAM_END 0xBFFF |
| 43 |
#define BANKED_ROM_START 0xC000 |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#define INSTALLED_RAM_BANKS 64 |
| 49 |
|
| 50 |
static void |
| 51 |
reset(void) |
| 52 |
{ |
| 53 |
fake_devices_reset(); |
| 54 |
memory_reset(); |
| 55 |
} |
| 56 |
|
| 57 |
static uint8_t |
| 58 |
read(uint16_t address) |
| 59 |
{ |
| 60 |
return read6502(address, 0); |
| 61 |
} |
| 62 |
|
| 63 |
static void |
| 64 |
write(uint16_t address, uint8_t value) |
| 65 |
{ |
| 66 |
write6502(address, 0, value); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
static void |
| 79 |
test_the_bank_registers_hold_and_report_a_bank(void) |
| 80 |
{ |
| 81 |
reset(); |
| 82 |
check_eq(read(RAM_BANK_REG), 0, "the RAM bank defaults to 0 on reset"); |
| 83 |
check_eq(read(ROM_BANK_REG), 0, "and so does the ROM bank"); |
| 84 |
|
| 85 |
write(RAM_BANK_REG, 0x2A); |
| 86 |
check_eq(read(RAM_BANK_REG), 0x2A, "the RAM bank reads back what was written"); |
| 87 |
check_eq(memory_get_ram_bank(), 0x2A, "and the machine is switched to it"); |
| 88 |
|
| 89 |
write(ROM_BANK_REG, 0x1F); |
| 90 |
check_eq(read(ROM_BANK_REG), 0x1F, "the ROM bank reads back what was written"); |
| 91 |
check_eq(memory_get_rom_bank(), 0x1F, "and the machine is switched to it"); |
| 92 |
|
| 93 |
check_eq(read(RAM_BANK_REG), 0x2A, "with the two registers independent"); |
| 94 |
|
| 95 |
|
| 96 |
write(RAM_BANK_REG, 0xFF); |
| 97 |
check_eq(read(RAM_BANK_REG), 0xFF, "the RAM bank spans the whole byte"); |
| 98 |
check_eq(memory_get_ram_bank(), 0xFF, "and reaches the latch unmasked"); |
| 99 |
write(ROM_BANK_REG, 0xFF); |
| 100 |
check_eq(read(ROM_BANK_REG), 0xFF, "and so does the ROM bank"); |
| 101 |
check_eq(memory_get_rom_bank(), 0xFF, "also unmasked"); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
static void |
| 111 |
test_a_reset_returns_both_banks_to_zero(void) |
| 112 |
{ |
| 113 |
reset(); |
| 114 |
write(RAM_BANK_REG, 0xFF); |
| 115 |
write(ROM_BANK_REG, 0xFF); |
| 116 |
|
| 117 |
reset(); |
| 118 |
|
| 119 |
check_eq(memory_get_ram_bank(), 0, "a reset switches the machine to RAM bank 0"); |
| 120 |
check_eq(memory_get_rom_bank(), 0, "and to ROM bank 0"); |
| 121 |
|
| 122 |
check_divergent(read(RAM_BANK_REG) == 0, |
| 123 |
"$0000 reports bank 0 after a reset", |
| 124 |
"memory.c:110 clears the latch but the address reads RAM[0] " |
| 125 |
"(memory.c:220), which the reset leaves alone, so the byte reports the " |
| 126 |
"pre-reset bank while the machine is on bank 0"); |
| 127 |
check_divergent(read(ROM_BANK_REG) == 0, |
| 128 |
"$0001 reports bank 0 after a reset", |
| 129 |
"same cause as $0000: the register's RAM shadow survives the reset"); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static void |
| 138 |
test_the_ram_window_shows_one_bank_at_a_time(void) |
| 139 |
{ |
| 140 |
reset(); |
| 141 |
|
| 142 |
write(RAM_BANK_REG, 0); |
| 143 |
write(BANKED_RAM_START, 0x11); |
| 144 |
write(BANKED_RAM_END, 0x22); |
| 145 |
|
| 146 |
write(RAM_BANK_REG, 1); |
| 147 |
write(BANKED_RAM_START, 0x33); |
| 148 |
write(BANKED_RAM_END, 0x44); |
| 149 |
|
| 150 |
check_eq(read(BANKED_RAM_START), 0x33, "bank 1 holds its own byte at the window start"); |
| 151 |
check_eq(read(BANKED_RAM_END), 0x44, "and at the window end"); |
| 152 |
|
| 153 |
write(RAM_BANK_REG, 0); |
| 154 |
check_eq(read(BANKED_RAM_START), 0x11, "and bank 0 still holds what it was given"); |
| 155 |
check_eq(read(BANKED_RAM_END), 0x22, "at both ends of the window"); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
write(0x9EFF, 0x55); |
| 161 |
write(RAM_BANK_REG, 1); |
| 162 |
check_eq(read(0x9EFF), 0x55, "the last byte of fixed RAM is not banked"); |
| 163 |
check_eq(BANKED_RAM_END - BANKED_RAM_START + 1, 8192, "and the window is 8 KB"); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
static void |
| 170 |
test_the_rom_window_is_sixteen_kilobytes(void) |
| 171 |
{ |
| 172 |
reset(); |
| 173 |
check_eq(0xFFFF - BANKED_ROM_START + 1, 16384, "the ROM window is 16 KB"); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
write(ROM_BANK_REG, 31); |
| 178 |
check_eq(memory_get_rom_bank(), 31, "the last system ROM bank can be selected"); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
static void |
| 194 |
test_an_uninstalled_ram_bank_reads_as_the_address_high_byte(void) |
| 195 |
{ |
| 196 |
reset(); |
| 197 |
check_eq(INSTALLED_RAM_BANKS, 64, "the fixture is a 512 KB machine"); |
| 198 |
|
| 199 |
write(RAM_BANK_REG, INSTALLED_RAM_BANKS - 1); |
| 200 |
write(BANKED_RAM_START, 0x77); |
| 201 |
check_eq(read(BANKED_RAM_START), 0x77, "the last installed bank is real memory"); |
| 202 |
|
| 203 |
write(RAM_BANK_REG, INSTALLED_RAM_BANKS); |
| 204 |
check_eq(read(BANKED_RAM_START), 0xA0, "the first uninstalled bank reads the address high byte"); |
| 205 |
check_eq(read(BANKED_RAM_END), 0xBF, "which tracks the address across the window"); |
| 206 |
|
| 207 |
write(RAM_BANK_REG, 0xFF); |
| 208 |
check_eq(read(BANKED_RAM_START), 0xA0, "and the highest bank reads the same"); |
| 209 |
} |
| 210 |
|
| 211 |
static void |
| 212 |
test_a_write_to_an_uninstalled_ram_bank_is_dropped(void) |
| 213 |
{ |
| 214 |
reset(); |
| 215 |
|
| 216 |
write(RAM_BANK_REG, 0); |
| 217 |
write(BANKED_RAM_START, 0x5A); |
| 218 |
|
| 219 |
write(RAM_BANK_REG, INSTALLED_RAM_BANKS); |
| 220 |
write(BANKED_RAM_START, 0xA5); |
| 221 |
|
| 222 |
write(RAM_BANK_REG, 0); |
| 223 |
check_eq(read(BANKED_RAM_START), 0x5A, "a write to an uninstalled bank reaches no memory"); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
static void |
| 231 |
test_writes_to_system_rom_are_ignored(void) |
| 232 |
{ |
| 233 |
reset(); |
| 234 |
write(ROM_BANK_REG, 0); |
| 235 |
|
| 236 |
const uint8_t before = read(BANKED_ROM_START); |
| 237 |
write(BANKED_ROM_START, (uint8_t)(before ^ 0xFF)); |
| 238 |
check_eq(read(BANKED_ROM_START), before, "a write to system ROM changes nothing"); |
| 239 |
} |
| 240 |
|
| 241 |
static void |
| 242 |
test_a_rom_bank_above_the_system_rom_reads_as_open_bus(void) |
| 243 |
{ |
| 244 |
reset(); |
| 245 |
|
| 246 |
|
| 247 |
write(ROM_BANK_REG, 32); |
| 248 |
check_eq(read(BANKED_ROM_START), 0xC0, "the first cartridge bank reads the address high byte"); |
| 249 |
check_eq(read(0xFFFF), 0xFF, "which tracks the address across the window"); |
| 250 |
} |
| 251 |
|
| 252 |
int |
| 253 |
main(void) |
| 254 |
{ |
| 255 |
test_the_bank_registers_hold_and_report_a_bank(); |
| 256 |
test_a_reset_returns_both_banks_to_zero(); |
| 257 |
test_the_ram_window_shows_one_bank_at_a_time(); |
| 258 |
test_the_rom_window_is_sixteen_kilobytes(); |
| 259 |
|
| 260 |
test_an_uninstalled_ram_bank_reads_as_the_address_high_byte(); |
| 261 |
test_a_write_to_an_uninstalled_ram_bank_is_dropped(); |
| 262 |
test_writes_to_system_rom_are_ignored(); |
| 263 |
test_a_rom_bank_above_the_system_rom_reads_as_open_bus(); |
| 264 |
|
| 265 |
return x16_test_summary("memory_banking"); |
| 266 |
} |