| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include "code_map.h" |
| 14 |
#include "cpu/registers.h" |
| 15 |
|
| 16 |
#include <stdbool.h> |
| 17 |
#include <stdint.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
struct regs regs; |
| 26 |
bool is_gen2; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#define TEST_RAM_BANKS 256 |
| 40 |
#define TEST_ROM_BANKS 32 |
| 41 |
#define TEST_GEN2_BANKS 4 |
| 42 |
|
| 43 |
static uint8_t g_mem[0x10000]; |
| 44 |
static uint8_t g_bram[TEST_RAM_BANKS][0x2000]; |
| 45 |
static uint8_t g_brom[TEST_ROM_BANKS][0x4000]; |
| 46 |
static uint8_t g_gen2[TEST_GEN2_BANKS][0x10000]; |
| 47 |
static uint8_t g_ram_bank = 0; |
| 48 |
static uint8_t g_rom_bank = 0; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static int g_low_mem_windowed = 0; |
| 54 |
|
| 55 |
uint8_t |
| 56 |
real_read6502(uint16_t address, uint8_t bank, bool debugOn, int16_t x16Bank) |
| 57 |
{ |
| 58 |
(void)debugOn; |
| 59 |
|
| 60 |
if (is_gen2 && bank != 0) { |
| 61 |
if (bank < TEST_GEN2_BANKS) { |
| 62 |
return g_gen2[bank][address]; |
| 63 |
} |
| 64 |
return (uint8_t)((address >> 8) & 0xFF); |
| 65 |
} |
| 66 |
if (address >= 0xC000) { |
| 67 |
int rb = x16Bank >= 0 ? (uint8_t)x16Bank : g_rom_bank; |
| 68 |
if (rb >= TEST_ROM_BANKS) { |
| 69 |
return (uint8_t)((address >> 8) & 0xFF); |
| 70 |
} |
| 71 |
return g_brom[rb][address - 0xC000]; |
| 72 |
} |
| 73 |
if (address >= 0xA000) { |
| 74 |
int rb = x16Bank >= 0 ? (uint8_t)x16Bank : g_ram_bank; |
| 75 |
return g_bram[rb % TEST_RAM_BANKS][address - 0xA000]; |
| 76 |
} |
| 77 |
if (x16Bank >= 0) { |
| 78 |
g_low_mem_windowed++; |
| 79 |
} |
| 80 |
return g_mem[address]; |
| 81 |
} |
| 82 |
|
| 83 |
uint8_t |
| 84 |
memory_get_ram_bank(void) |
| 85 |
{ |
| 86 |
return g_ram_bank; |
| 87 |
} |
| 88 |
|
| 89 |
uint8_t |
| 90 |
memory_get_rom_bank(void) |
| 91 |
{ |
| 92 |
return g_rom_bank; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
static int failures = 0; |
| 98 |
|
| 99 |
static void |
| 100 |
check(bool cond, const char *what) |
| 101 |
{ |
| 102 |
if (!cond) { |
| 103 |
failures++; |
| 104 |
printf("FAIL: %s\n", what); |
| 105 |
} else { |
| 106 |
printf("ok : %s\n", what); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
static void |
| 116 |
check_line_flags(const code_map_line_t *lines, int n, const char *what) |
| 117 |
{ |
| 118 |
for (int i = 0; i < n; i++) { |
| 119 |
bool expect = (lines[i].kind == CM_LINE_INSTRUCTION) && lines[i].start_recorded; |
| 120 |
if (lines[i].recorded != expect) { |
| 121 |
printf(" line %d at $%04X: recorded=%d kind=%u start_recorded=%d\n", |
| 122 |
i, (unsigned)lines[i].addr, (int)lines[i].recorded, |
| 123 |
(unsigned)lines[i].kind, (int)lines[i].start_recorded); |
| 124 |
check(false, what); |
| 125 |
return; |
| 126 |
} |
| 127 |
if (lines[i].kind == CM_LINE_DATA && lines[i].eff_addr != -1) { |
| 128 |
printf(" line %d at $%04X: data row with eff_addr $%04X\n", |
| 129 |
i, (unsigned)lines[i].addr, (unsigned)lines[i].eff_addr); |
| 130 |
check(false, what); |
| 131 |
return; |
| 132 |
} |
| 133 |
} |
| 134 |
check(n >= 1, what); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
static void |
| 145 |
check_tiles(const code_map_line_t *lines, int n, const char *what) |
| 146 |
{ |
| 147 |
for (int i = 0; i < n; i++) { |
| 148 |
if (lines[i].size < 1) { |
| 149 |
printf(" line %d at $%04X has size %u\n", |
| 150 |
i, (unsigned)lines[i].addr, (unsigned)lines[i].size); |
| 151 |
check(false, what); |
| 152 |
return; |
| 153 |
} |
| 154 |
if (i == 0) { |
| 155 |
continue; |
| 156 |
} |
| 157 |
uint16_t end = (uint16_t)((lines[i - 1].addr + lines[i - 1].size) & 0xFFFF); |
| 158 |
if (lines[i].addr != end) { |
| 159 |
printf(" %s: line %d $%04X+%u ends at $%04X, line %d starts at $%04X\n", |
| 160 |
(uint16_t)(lines[i].addr - end) < 0x8000 ? "gap" : "overlap", |
| 161 |
i - 1, (unsigned)lines[i - 1].addr, (unsigned)lines[i - 1].size, |
| 162 |
(unsigned)end, i, (unsigned)lines[i].addr); |
| 163 |
check(false, what); |
| 164 |
return; |
| 165 |
} |
| 166 |
} |
| 167 |
check(n >= 1, what); |
| 168 |
} |
| 169 |
|
| 170 |
static void |
| 171 |
reset_all(void) |
| 172 |
{ |
| 173 |
code_map_reset(); |
| 174 |
memset(g_mem, 0xEA, sizeof(g_mem)); |
| 175 |
memset(g_bram, 0xEA, sizeof(g_bram)); |
| 176 |
memset(g_brom, 0xEA, sizeof(g_brom)); |
| 177 |
memset(g_gen2, 0xEA, sizeof(g_gen2)); |
| 178 |
g_ram_bank = 0; |
| 179 |
g_rom_bank = 0; |
| 180 |
g_low_mem_windowed = 0; |
| 181 |
memset(®s, 0, sizeof(regs)); |
| 182 |
regs.status = FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
static void |
| 189 |
poke_banked(uint16_t addr, uint8_t window_bank, const uint8_t *bytes, size_t n) |
| 190 |
{ |
| 191 |
for (size_t i = 0; i < n; i++) { |
| 192 |
uint16_t a = (uint16_t)((addr + i) & 0xFFFF); |
| 193 |
if (a >= 0xC000) { |
| 194 |
g_brom[window_bank % TEST_ROM_BANKS][a - 0xC000] = bytes[i]; |
| 195 |
} else if (a >= 0xA000) { |
| 196 |
g_bram[window_bank % TEST_RAM_BANKS][a - 0xA000] = bytes[i]; |
| 197 |
} else { |
| 198 |
g_mem[a] = bytes[i]; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
static void |
| 206 |
poke(uint16_t addr, const uint8_t *bytes, size_t n) |
| 207 |
{ |
| 208 |
for (size_t i = 0; i < n; i++) { |
| 209 |
uint16_t a = (uint16_t)((addr + i) & 0xFFFF); |
| 210 |
poke_banked(a, a >= 0xC000 ? g_rom_bank : g_ram_bank, &bytes[i], 1); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
static void |
| 215 |
poke_gen2(uint8_t cpu_bank, uint16_t addr, const uint8_t *bytes, size_t n) |
| 216 |
{ |
| 217 |
for (size_t i = 0; i < n; i++) { |
| 218 |
g_gen2[cpu_bank % TEST_GEN2_BANKS][(addr + i) & 0xFFFF] = bytes[i]; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
int |
| 223 |
main(void) |
| 224 |
{ |
| 225 |
|
| 226 |
{ |
| 227 |
reset_all(); |
| 228 |
check(!code_map_is_recorded_start(0x8000, 0, 0, 0), |
| 229 |
"reports nothing recorded before anything runs"); |
| 230 |
|
| 231 |
code_map_record(0x8000, 0, 0, 0, 0x34); |
| 232 |
check(code_map_is_recorded_start(0x8000, 0, 0, 0), |
| 233 |
"remembers a recorded instruction start"); |
| 234 |
check(code_map_recorded_status(0x8000, 0, 0, 0, 0xFF) == 0x34, |
| 235 |
"returns the status captured at that instruction"); |
| 236 |
check(code_map_recorded_status(0x8001, 0, 0, 0, 0xFF) == 0xFF, |
| 237 |
"falls back for an address that never executed"); |
| 238 |
check(!code_map_is_recorded_start(0x8001, 0, 0, 0), |
| 239 |
"does not claim neighbouring addresses"); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
{ |
| 247 |
reset_all(); |
| 248 |
code_map_record(0xA000, 0, 5, 0, 0x20); |
| 249 |
|
| 250 |
check(code_map_is_recorded_start(0xA000, 0, 5, 0), |
| 251 |
"finds the record in its own RAM bank"); |
| 252 |
check(!code_map_is_recorded_start(0xA000, 0, 6, 0), |
| 253 |
"keeps RAM banks apart"); |
| 254 |
|
| 255 |
code_map_record(0xC000, 0, 0, 1, 0x20); |
| 256 |
check(code_map_is_recorded_start(0xC000, 0, 0, 1), |
| 257 |
"finds the record in its own ROM bank"); |
| 258 |
check(!code_map_is_recorded_start(0xC000, 0, 0, 2), |
| 259 |
"keeps ROM banks apart"); |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
reset_all(); |
| 266 |
is_gen2 = true; |
| 267 |
code_map_record(0x8000, 0, 0, 0, 0x30); |
| 268 |
check(code_map_is_recorded_start(0x8000, 0, 0, 0), |
| 269 |
"finds the record in program bank 0"); |
| 270 |
check(!code_map_is_recorded_start(0x8000, 1, 0, 0), |
| 271 |
"keeps Gen2 program banks apart"); |
| 272 |
|
| 273 |
code_map_record(0x8000, 1, 0, 0, 0x00); |
| 274 |
check(code_map_recorded_status(0x8000, 0, 0, 0, 0xFF) == 0x30 && |
| 275 |
code_map_recorded_status(0x8000, 1, 0, 0, 0xFF) == 0x00, |
| 276 |
"keeps a separate status per Gen2 program bank"); |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
check(code_map_is_recorded_start(0x8000, 1, 7, 3), |
| 281 |
"ignores the window banks inside a Gen2 program bank"); |
| 282 |
code_map_record(0xC000, 1, 0, 0, 0x30); |
| 283 |
check(code_map_is_recorded_start(0xC000, 1, 0, 9), |
| 284 |
"ignores the ROM bank inside a Gen2 program bank"); |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
reset_all(); |
| 290 |
is_gen2 = false; |
| 291 |
code_map_record(0x8000, 0, 0, 0, 0x30); |
| 292 |
check(code_map_is_recorded_start(0x8000, 1, 0, 0), |
| 293 |
"shares coverage across program banks when they select nothing"); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
{ |
| 303 |
reset_all(); |
| 304 |
code_map_record(0x0801, 0, 0, 0, 0x30); |
| 305 |
|
| 306 |
check(code_map_is_recorded_start(0x0801, 0, 7, 3), |
| 307 |
"finds low-memory coverage under different banks"); |
| 308 |
check(code_map_recorded_status(0x0801, 0, 7, 3, 0xFF) == 0x30, |
| 309 |
"keeps the recorded status for low memory across banks"); |
| 310 |
|
| 311 |
|
| 312 |
for (int b = 0; b < 64; b++) { |
| 313 |
code_map_record(0x0900, 0, (uint8_t)b, (uint8_t)(b & 31), 0x30); |
| 314 |
} |
| 315 |
check(code_map_is_recorded_start(0x0801, 0, 0, 0), |
| 316 |
"does not evict low-memory coverage while banks churn"); |
| 317 |
|
| 318 |
|
| 319 |
reset_all(); |
| 320 |
code_map_record(0xA000, 0, 5, 0, 0x30); |
| 321 |
check(code_map_is_recorded_start(0xA000, 0, 5, 9), |
| 322 |
"ignores the ROM bank for a RAM-window address"); |
| 323 |
check(!code_map_is_recorded_start(0xA000, 0, 6, 0), |
| 324 |
"still separates RAM banks in the RAM window"); |
| 325 |
|
| 326 |
code_map_record(0xC000, 0, 0, 2, 0x30); |
| 327 |
check(code_map_is_recorded_start(0xC000, 0, 9, 2), |
| 328 |
"ignores the RAM bank for a ROM-window address"); |
| 329 |
check(!code_map_is_recorded_start(0xC000, 0, 0, 3), |
| 330 |
"still separates ROM banks in the ROM window"); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
{ |
| 337 |
reset_all(); |
| 338 |
code_map_record(0x8000, 0, 0, 0, 0x24); |
| 339 |
code_map_record(0x9000, 0, 3, 0, 0x24); |
| 340 |
code_map_reset(); |
| 341 |
check(!code_map_is_recorded_start(0x8000, 0, 0, 0) && |
| 342 |
!code_map_is_recorded_start(0x9000, 0, 3, 0), |
| 343 |
"forgets all coverage on reset"); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
{ |
| 351 |
reset_all(); |
| 352 |
for (int b = 0; b < 40; b++) { |
| 353 |
code_map_record((uint16_t)(0xA000 + b), 0, (uint8_t)b, 0, 0x20); |
| 354 |
|
| 355 |
|
| 356 |
code_map_record(0xA0FF, 0, (uint8_t)b, 0, 0x20); |
| 357 |
} |
| 358 |
check(code_map_is_recorded_start(0xA027, 0, 39, 0), |
| 359 |
"keeps the most recent context after recycling"); |
| 360 |
|
| 361 |
|
| 362 |
int stale = 0; |
| 363 |
for (int b = 0; b < 40; b++) { |
| 364 |
if (code_map_is_recorded_start((uint16_t)(0xA000 + b), 0, (uint8_t)b, 0)) { |
| 365 |
continue; |
| 366 |
} |
| 367 |
stale++; |
| 368 |
} |
| 369 |
check(stale > 0, "recycles contexts once the cap is reached"); |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
code_map_record(0xB000, 0, 0, 0, 0x20); |
| 374 |
check(code_map_is_recorded_start(0xB000, 0, 0, 0), |
| 375 |
"records into a recycled context"); |
| 376 |
check(!code_map_is_recorded_start(0xA0FF, 0, 0, 0), |
| 377 |
"a recycled context does not resurrect old coverage"); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
{ |
| 385 |
reset_all(); |
| 386 |
|
| 387 |
|
| 388 |
const uint8_t code[] = { 0xA9, 0x00, 0xEA }; |
| 389 |
poke(0x8000, code, sizeof(code)); |
| 390 |
|
| 391 |
code_map_record(0x8000, 0, 0, 0, regs.status); |
| 392 |
code_map_record(0x8002, 0, 0, 0, regs.status); |
| 393 |
|
| 394 |
check(code_map_prev_instruction(0x8002, 0, 0, 0) == 0x8000, |
| 395 |
"walks back onto a recorded instruction start"); |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
code_map_reset(); |
| 400 |
check(code_map_prev_instruction(0x8002, 0, 0, 0) == 0x8000, |
| 401 |
"self-syncs backwards without any coverage"); |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
reset_all(); |
| 410 |
const uint8_t amb[] = { 0xA9, 0xEA, 0xEA }; |
| 411 |
poke(0x8001, amb, sizeof(amb)); |
| 412 |
check(code_map_prev_instruction(0x8003, 0, 0, 0) == 0x8002, |
| 413 |
"self-sync alone picks the nearer decoy"); |
| 414 |
code_map_record(0x8001, 0, 0, 0, regs.status); |
| 415 |
check(code_map_prev_instruction(0x8003, 0, 0, 0) == 0x8001, |
| 416 |
"recorded coverage outranks the self-sync guess"); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
{ |
| 421 |
reset_all(); |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
const uint8_t code[] = { 0xA9, 0x12, 0x8D, 0x34, 0x12, 0xEA }; |
| 426 |
poke(0x8000, code, sizeof(code)); |
| 427 |
|
| 428 |
code_map_line_t lines[8]; |
| 429 |
uint16_t next = 0; |
| 430 |
int n = code_map_disasm_forward(0x8000, 0, 0, 0, 3, lines, 8, &next); |
| 431 |
|
| 432 |
check(n == 3, "disassembles the requested number of instructions"); |
| 433 |
check(n == 3 && lines[0].addr == 0x8000 && lines[0].size == 2, |
| 434 |
"sizes an immediate load"); |
| 435 |
check(n == 3 && lines[1].addr == 0x8002 && lines[1].size == 3, |
| 436 |
"sizes an absolute store"); |
| 437 |
check(n == 3 && lines[2].addr == 0x8005 && lines[2].size == 1, |
| 438 |
"sizes an implied instruction"); |
| 439 |
check(next == 0x8006, "reports the address just past the last instruction"); |
| 440 |
check(n == 3 && lines[0].bytes[0] == 0xA9 && lines[0].bytes[1] == 0x12, |
| 441 |
"captures the raw instruction bytes"); |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
check(n == 3 && strstr(lines[1].text, "sta") != NULL && |
| 446 |
strstr(lines[1].text, "$1234") != NULL, |
| 447 |
"renders the mnemonic and operand for the opcode it decoded"); |
| 448 |
|
| 449 |
|
| 450 |
check(n == 3 && !lines[0].recorded, "marks unexecuted lines as unrecorded"); |
| 451 |
code_map_record(0x8000, 0, 0, 0, regs.status); |
| 452 |
n = code_map_disasm_forward(0x8000, 0, 0, 0, 1, lines, 8, &next); |
| 453 |
check(n == 1 && lines[0].recorded, "marks executed lines as recorded"); |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
{ |
| 458 |
reset_all(); |
| 459 |
const uint8_t code[] = { 0xEA, 0xEA, 0xEA, 0xEA, 0xEA, 0xEA, 0xEA, 0xEA }; |
| 460 |
poke(0x8000, code, sizeof(code)); |
| 461 |
for (uint16_t a = 0x8000; a < 0x8008; a++) { |
| 462 |
code_map_record(a, 0, 0, 0, regs.status); |
| 463 |
} |
| 464 |
|
| 465 |
code_map_line_t lines[16]; |
| 466 |
int center_index = -99; |
| 467 |
int n = code_map_disasm_window(0x8004, 0, 0, 0, 3, 3, lines, 16, ¢er_index); |
| 468 |
|
| 469 |
check(n == 6, "returns the requested window size"); |
| 470 |
check(center_index >= 0 && center_index < n, "reports where the center landed"); |
| 471 |
check(center_index >= 0 && center_index < n && lines[center_index].addr == 0x8004, |
| 472 |
"centers the window on the requested address"); |
| 473 |
check(n >= 1 && lines[0].addr == 0x8001, "starts the window before the center"); |
| 474 |
|
| 475 |
|
| 476 |
center_index = -99; |
| 477 |
n = code_map_disasm_window(0x8004, 0, 0, 0, 3, 3, lines, 2, ¢er_index); |
| 478 |
check(n == 2, "honours the output capacity"); |
| 479 |
check(center_index == 1 && lines[center_index].addr == 0x8004, |
| 480 |
"still locates the center in a window that cannot fit"); |
| 481 |
check(n == 2 && lines[0].addr == 0x8003, |
| 482 |
"spends a too-small window on the lines nearest the center"); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
{ |
| 499 |
reset_all(); |
| 500 |
regs.is65c816 = true; |
| 501 |
regs.e = 0; |
| 502 |
|
| 503 |
const uint8_t code[] = { 0x28, 0xA9, 0xEA, 0xA9, 0xEA }; |
| 504 |
poke(0x8000, code, sizeof(code)); |
| 505 |
|
| 506 |
const uint8_t st_8bit = FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH; |
| 507 |
const uint8_t st_16bit = FLAG_INDEX_WIDTH; |
| 508 |
|
| 509 |
regs.status = st_8bit; |
| 510 |
code_map_record(0x8000, 0, 0, 0, st_8bit); |
| 511 |
code_map_record(0x8004, 0, 0, 0, st_16bit); |
| 512 |
|
| 513 |
code_map_line_t lines[16]; |
| 514 |
int center_index = -99; |
| 515 |
int n = code_map_disasm_window(0x8004, 0, 0, 0, 2, 2, lines, 16, ¢er_index); |
| 516 |
|
| 517 |
check(n > 0 && center_index >= 0, "does not lose the center line to width drift"); |
| 518 |
check(center_index >= 0 && center_index < n && lines[center_index].addr == 0x8004, |
| 519 |
"re-aligns onto the center address after drifting"); |
| 520 |
check(n == 4, "returns exactly the requested line count despite the drift"); |
| 521 |
check(center_index == 2, "puts exactly the requested number of lines before center"); |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
check(n == 4 && lines[1].addr == 0x8001 && lines[1].size == 3, |
| 527 |
"sizes the drifting instruction from the status that reaches center"); |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
check_tiles(lines, n, "never emits overlapping instructions"); |
| 533 |
|
| 534 |
regs.is65c816 = false; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
{ |
| 551 |
reset_all(); |
| 552 |
g_mem[0x7FFF] = 0xA9; |
| 553 |
|
| 554 |
code_map_line_t lines[8]; |
| 555 |
int center_index = -99; |
| 556 |
int n = code_map_disasm_window(0x8000, 0, 0, 0, 1, 2, lines, 8, ¢er_index); |
| 557 |
|
| 558 |
check(n >= 2 && lines[0].addr == 0x7FFF, |
| 559 |
"backs off a single byte when nothing decodes onto the anchor"); |
| 560 |
check(center_index >= 0 && center_index < n && lines[center_index].addr == 0x8000, |
| 561 |
"keeps the center where the caller asked despite the fallback"); |
| 562 |
check_tiles(lines, n, "the one-byte fallback tiles the range instead of overlapping"); |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
{ |
| 580 |
reset_all(); |
| 581 |
const uint8_t skip_idiom[] = { 0x2C, 0x8D, 0x12 }; |
| 582 |
poke(0x8002, skip_idiom, sizeof(skip_idiom)); |
| 583 |
code_map_record(0x8003, 0, 0, 0, regs.status); |
| 584 |
|
| 585 |
code_map_line_t lines[8]; |
| 586 |
int center_index = -99; |
| 587 |
int n = code_map_disasm_window(0x8005, 0, 0, 0, 1, 2, lines, 8, ¢er_index); |
| 588 |
|
| 589 |
check(n >= 2 && lines[0].addr == 0x8002, |
| 590 |
"walks back onto the wider of two overlapping starts"); |
| 591 |
check(center_index >= 0 && center_index < n && lines[center_index].addr == 0x8005, |
| 592 |
"still centers on the requested address after truncating"); |
| 593 |
check_tiles(lines, n, "truncating onto an interior anchor leaves no gap"); |
| 594 |
check_line_flags(lines, n, "keeps recorded/kind/start_recorded consistent"); |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
bool starts_at_8003 = false; |
| 601 |
for (int i = 0; i < n; i++) { |
| 602 |
if (lines[i].addr == 0x8003) { |
| 603 |
starts_at_8003 = true; |
| 604 |
} |
| 605 |
} |
| 606 |
check(starts_at_8003, "begins a line at the recorded start inside the BIT"); |
| 607 |
check(n >= 1 && lines[0].size == 1 && !lines[0].recorded && lines[0].eff_addr == -1, |
| 608 |
"a line cut short is not reported as a whole verified instruction"); |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
check(n >= 1 && lines[0].bytes[0] == 0x2C && |
| 613 |
strcmp(lines[0].text, ".byte $2c") == 0, |
| 614 |
"renders a cut-short line as the bytes it owns"); |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
check(n >= 1 && lines[0].kind == CM_LINE_DATA, |
| 619 |
"marks a cut-short line as data rather than an instruction"); |
| 620 |
check(center_index >= 0 && center_index < n && |
| 621 |
lines[center_index].kind == CM_LINE_INSTRUCTION, |
| 622 |
"marks a whole line as an instruction"); |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
center_index = -99; |
| 628 |
int tight = code_map_disasm_window(0x8005, 0, 0, 0, 1, 2, lines, 2, ¢er_index); |
| 629 |
check(tight >= 1 && center_index >= 0 && center_index < tight && |
| 630 |
lines[center_index].addr == 0x8005, |
| 631 |
"keeps the center line when phase B cannot fit as well"); |
| 632 |
check_tiles(lines, tight, "tiles what fits when capacity runs out"); |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
check(tight == 2 && center_index == 1 && lines[0].addr == 0x8003, |
| 639 |
"keeps the part of a too-large group closest to the center"); |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
check(tight == 2 && lines[0].kind == CM_LINE_DATA && lines[0].start_recorded && |
| 645 |
!lines[0].recorded, |
| 646 |
"a row cut by a boundary still reports its start as executed"); |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
{ |
| 661 |
reset_all(); |
| 662 |
const uint8_t skip_idiom[] = { 0x2C, 0x8D, 0x12 }; |
| 663 |
poke(0x8002, skip_idiom, sizeof(skip_idiom)); |
| 664 |
code_map_record(0x8002, 0, 0, 0, regs.status); |
| 665 |
code_map_record(0x8003, 0, 0, 0, regs.status); |
| 666 |
|
| 667 |
code_map_line_t lines[8]; |
| 668 |
uint16_t next = 0; |
| 669 |
|
| 670 |
int n = code_map_disasm_forward(0x8002, 0, 0, 0, 2, lines, 8, &next); |
| 671 |
check(n == 2 && lines[0].addr == 0x8002 && lines[0].size == 3 && |
| 672 |
lines[0].recorded, |
| 673 |
"emits a recorded instruction whole over a recorded start inside it"); |
| 674 |
check(n == 2 && strstr(lines[0].text, "bit") != NULL, |
| 675 |
"keeps the mnemonic of a recorded instruction spanning another start"); |
| 676 |
check(n == 2 && lines[1].addr == 0x8005, |
| 677 |
"continues past a swallowed start on the path the caller asked for"); |
| 678 |
check_tiles(lines, n, "tiles the path entered at the outer start"); |
| 679 |
|
| 680 |
|
| 681 |
n = code_map_disasm_forward(0x8003, 0, 0, 0, 2, lines, 8, &next); |
| 682 |
check(n == 2 && lines[0].addr == 0x8003 && lines[0].size == 3 && |
| 683 |
lines[0].recorded && strstr(lines[0].text, "sta") != NULL, |
| 684 |
"emits the hidden entry point whole when entered there"); |
| 685 |
check_tiles(lines, n, "tiles the path entered at the hidden start"); |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
code_map_reset(); |
| 690 |
code_map_record(0x8003, 0, 0, 0, regs.status); |
| 691 |
n = code_map_disasm_forward(0x8002, 0, 0, 0, 2, lines, 8, &next); |
| 692 |
check(n == 2 && lines[0].size == 1 && !lines[0].recorded, |
| 693 |
"an unrecorded decode still gives way to a recorded start inside it"); |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
reset_all(); |
| 700 |
poke(0x8002, skip_idiom, sizeof(skip_idiom)); |
| 701 |
code_map_record(0x8002, 0, 0, 0, regs.status); |
| 702 |
code_map_record(0x8003, 0, 0, 0, regs.status); |
| 703 |
|
| 704 |
int center_index = -99; |
| 705 |
code_map_line_t win[8]; |
| 706 |
int wn = code_map_disasm_window(0x8005, 0, 0, 0, 1, 1, win, 8, ¢er_index); |
| 707 |
check(wn >= 2 && win[0].addr == 0x8002 && win[0].size == 3 && |
| 708 |
win[0].kind == CM_LINE_INSTRUCTION && win[0].start_recorded, |
| 709 |
"the window shows the outer instruction whole when both are recorded"); |
| 710 |
check_tiles(win, wn, "tiles when both overlapping starts are recorded"); |
| 711 |
|
| 712 |
|
| 713 |
center_index = -99; |
| 714 |
wn = code_map_disasm_window(0x8002, 0, 0, 0, 0, 2, win, 8, ¢er_index); |
| 715 |
check(wn >= 1 && center_index == 0 && win[0].addr == 0x8002 && |
| 716 |
win[0].size == 3 && win[0].kind == CM_LINE_INSTRUCTION, |
| 717 |
"centering on the outer start shows the outer instruction whole"); |
| 718 |
center_index = -99; |
| 719 |
wn = code_map_disasm_window(0x8003, 0, 0, 0, 0, 2, win, 8, ¢er_index); |
| 720 |
check(wn >= 1 && center_index == 0 && win[0].addr == 0x8003 && |
| 721 |
win[0].size == 3 && win[0].kind == CM_LINE_INSTRUCTION, |
| 722 |
"centering on the inner start shows the inner instruction whole"); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
{ |
| 757 |
reset_all(); |
| 758 |
regs.is65c816 = true; |
| 759 |
regs.e = 0; |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
const uint8_t old_code[] = { 0xA9, 0x11, 0x22, 0x33 }; |
| 764 |
poke(0x8000, old_code, sizeof(old_code)); |
| 765 |
code_map_record(0x8000, 0, 0, 0, FLAG_INDEX_WIDTH); |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
const uint8_t new_code[] = { 0xA9, 0xEA, 0xEA, 0xEA }; |
| 771 |
poke(0x8000, new_code, sizeof(new_code)); |
| 772 |
code_map_record(0x8002, 0, 0, 0, FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH); |
| 773 |
regs.status = FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH; |
| 774 |
|
| 775 |
code_map_line_t lines[4]; |
| 776 |
uint16_t next = 0; |
| 777 |
int n = code_map_disasm_forward(0x8000, 0, 0, 0, 2, lines, 4, &next); |
| 778 |
|
| 779 |
check(n == 2 && lines[0].size == 3, |
| 780 |
"KNOWN LIMITATION: a stale same-opcode anchor still swallows a fresh one"); |
| 781 |
check(n == 2 && lines[1].addr == 0x8003, |
| 782 |
"KNOWN LIMITATION: the line after it starts past the fresh anchor"); |
| 783 |
|
| 784 |
regs.is65c816 = false; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
{ |
| 793 |
reset_all(); |
| 794 |
|
| 795 |
const uint8_t old_bit[] = { 0x2C, 0x8D, 0x12 }; |
| 796 |
poke(0x8000, old_bit, sizeof(old_bit)); |
| 797 |
code_map_record(0x8000, 0, 0, 0, regs.status); |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
const uint8_t new_bit[] = { 0x2C, 0xA9, 0xEA }; |
| 802 |
poke(0x8000, new_bit, sizeof(new_bit)); |
| 803 |
code_map_record(0x8001, 0, 0, 0, regs.status); |
| 804 |
|
| 805 |
code_map_line_t lines[4]; |
| 806 |
uint16_t next = 0; |
| 807 |
int n = code_map_disasm_forward(0x8000, 0, 0, 0, 2, lines, 4, &next); |
| 808 |
|
| 809 |
check(n == 2 && lines[0].size == 3 && lines[1].addr == 0x8003, |
| 810 |
"KNOWN LIMITATION: stale-anchor swallowing is not 65C816-specific"); |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
{ |
| 818 |
reset_all(); |
| 819 |
regs.is65c816 = true; |
| 820 |
regs.e = 0; |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
const uint8_t code[] = { 0xA9, 0xEA, 0xEA, 0xEA }; |
| 825 |
poke(0x8000, code, sizeof(code)); |
| 826 |
regs.status = FLAG_INDEX_WIDTH; |
| 827 |
code_map_record(0x8002, 0, 0, 0, FLAG_INDEX_WIDTH); |
| 828 |
|
| 829 |
code_map_line_t lines[4]; |
| 830 |
uint16_t next = 0; |
| 831 |
int n = code_map_disasm_forward(0x8000, 0, 0, 0, 2, lines, 4, &next); |
| 832 |
check(n == 2 && lines[0].size == 2, |
| 833 |
"truncates an instruction that would swallow a recorded start"); |
| 834 |
check(n == 2 && lines[1].addr == 0x8002, |
| 835 |
"lands on the recorded start instead of stepping over it"); |
| 836 |
|
| 837 |
regs.is65c816 = false; |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
{ |
| 848 |
reset_all(); |
| 849 |
|
| 850 |
const uint8_t prog1[] = { 0xA9, 0x00, 0xEA }; |
| 851 |
poke(0x8000, prog1, sizeof(prog1)); |
| 852 |
code_map_record(0x8000, 0, 0, 0, regs.status); |
| 853 |
code_map_record(0x8002, 0, 0, 0, regs.status); |
| 854 |
check(code_map_is_recorded_start(0x8002, 0, 0, 0), |
| 855 |
"trusts an anchor while its code is still there"); |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
const uint8_t prog2[] = { 0xEA, 0x8D, 0x34, 0x12 }; |
| 860 |
poke(0x8000, prog2, sizeof(prog2)); |
| 861 |
check(!code_map_is_recorded_start(0x8002, 0, 0, 0), |
| 862 |
"drops an anchor once the code under it changed"); |
| 863 |
check(code_map_recorded_status(0x8002, 0, 0, 0, 0xC3) == 0xC3, |
| 864 |
"does not report a stale anchor's status"); |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
check(!code_map_is_recorded_start(0x8000, 0, 0, 0), |
| 869 |
"drops an anchor whose opcode was replaced"); |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
code_map_line_t lines[4]; |
| 874 |
uint16_t next = 0; |
| 875 |
int n = code_map_disasm_forward(0x8001, 0, 0, 0, 1, lines, 4, &next); |
| 876 |
check(n == 1 && lines[0].size == 3, |
| 877 |
"a stale anchor does not cut a real instruction short"); |
| 878 |
|
| 879 |
|
| 880 |
code_map_record(0x8001, 0, 0, 0, regs.status); |
| 881 |
check(code_map_is_recorded_start(0x8001, 0, 0, 0), |
| 882 |
"trusts the new code once it has actually run"); |
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
{ |
| 892 |
reset_all(); |
| 893 |
regs.is65c816 = true; |
| 894 |
regs.e = 0; |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
const uint8_t code[] = { 0x28, 0xA9, 0xEA, 0xA9, 0xEA }; |
| 900 |
poke(0x8000, code, sizeof(code)); |
| 901 |
const uint8_t st_8bit = FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH; |
| 902 |
const uint8_t st_16bit = FLAG_INDEX_WIDTH; |
| 903 |
regs.status = st_8bit; |
| 904 |
code_map_record(0x8000, 0, 0, 0, st_8bit); |
| 905 |
code_map_record(0x8004, 0, 0, 0, st_16bit); |
| 906 |
|
| 907 |
code_map_line_t lines[8]; |
| 908 |
uint16_t next = 0; |
| 909 |
int n = code_map_disasm_forward(0x8000, 0, 0, 0, 4, lines, 8, &next); |
| 910 |
|
| 911 |
|
| 912 |
check(n >= 2 && lines[0].recorded, "reports the anchored line as recorded"); |
| 913 |
check(n >= 2 && !lines[1].recorded, |
| 914 |
"reports a line decoded from a guessed width as unrecorded"); |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
check(n == 4 && lines[3].addr == 0x8004, |
| 919 |
"re-syncs onto the next anchor after a bad width guess"); |
| 920 |
check(n == 4 && lines[3].recorded && lines[3].status == st_16bit, |
| 921 |
"recovers the real width from the anchor it landed on"); |
| 922 |
|
| 923 |
regs.is65c816 = false; |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
{ |
| 941 |
reset_all(); |
| 942 |
regs.is65c816 = true; |
| 943 |
regs.e = 0; |
| 944 |
|
| 945 |
const uint8_t code[] = { 0x40, 0xA9, 0xEA, 0xA9, 0xEA }; |
| 946 |
poke(0x8000, code, sizeof(code)); |
| 947 |
const uint8_t st_8bit = FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH; |
| 948 |
const uint8_t st_16bit = FLAG_INDEX_WIDTH; |
| 949 |
regs.status = st_8bit; |
| 950 |
code_map_record(0x8000, 0, 0, 0, st_8bit); |
| 951 |
code_map_record(0x8004, 0, 0, 0, st_16bit); |
| 952 |
|
| 953 |
code_map_line_t lines[8]; |
| 954 |
uint16_t next = 0; |
| 955 |
int n = code_map_disasm_forward(0x8000, 0, 0, 0, 4, lines, 8, &next); |
| 956 |
|
| 957 |
check(n >= 2 && lines[0].recorded && lines[0].size == 1, |
| 958 |
"decodes RTI itself from its own anchor"); |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
check(n >= 2 && lines[1].addr == 0x8001 && lines[1].size == 2 && |
| 965 |
lines[1].status == st_8bit, |
| 966 |
"leaves the width estimate alone across RTI instead of guessing"); |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
check(n >= 2 && !lines[1].recorded, |
| 972 |
"reports a line decoded across RTI as a guess, not as known"); |
| 973 |
bool resynced = false; |
| 974 |
for (int i = 0; i < n; i++) { |
| 975 |
if (lines[i].addr == 0x8004 && lines[i].recorded && lines[i].status == st_16bit) { |
| 976 |
resynced = true; |
| 977 |
} |
| 978 |
} |
| 979 |
check(resynced, "reaches the anchor after RTI and recovers the real width"); |
| 980 |
check_tiles(lines, n, "tiles the range either side of an RTI"); |
| 981 |
|
| 982 |
regs.is65c816 = false; |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
{ |
| 994 |
reset_all(); |
| 995 |
g_ram_bank = 0x11; |
| 996 |
g_rom_bank = 0x17; |
| 997 |
|
| 998 |
|
| 999 |
const uint8_t ram5[] = { 0xA9, 0x12 }; |
| 1000 |
const uint8_t rom2[] = { 0x8D, 0x34, 0x12 }; |
| 1001 |
|
| 1002 |
const uint8_t decoy[] = { 0xEA, 0xEA, 0xEA }; |
| 1003 |
poke_banked(0xA000, 5, ram5, sizeof(ram5)); |
| 1004 |
poke_banked(0xC000, 2, rom2, sizeof(rom2)); |
| 1005 |
poke_banked(0xA000, 0x11, decoy, sizeof(decoy)); |
| 1006 |
poke_banked(0xC000, 0x17, decoy, sizeof(decoy)); |
| 1007 |
|
| 1008 |
code_map_line_t lines[4]; |
| 1009 |
uint16_t next = 0; |
| 1010 |
|
| 1011 |
int n = code_map_disasm_forward(0xA000, 0, 5, 2, 1, lines, 4, &next); |
| 1012 |
check(n == 1 && lines[0].size == 2 && lines[0].bytes[0] == 0xA9 && |
| 1013 |
lines[0].bytes[1] == 0x12, |
| 1014 |
"reads a $A000 address through the requested RAM bank"); |
| 1015 |
|
| 1016 |
n = code_map_disasm_forward(0xC000, 0, 5, 2, 1, lines, 4, &next); |
| 1017 |
check(n == 1 && lines[0].size == 3 && lines[0].bytes[0] == 0x8D && |
| 1018 |
lines[0].bytes[2] == 0x12, |
| 1019 |
"reads a $C000 address through the requested ROM bank"); |
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
g_low_mem_windowed = 0; |
| 1024 |
n = code_map_disasm_forward(0x0801, 0, 5, 2, 4, lines, 4, &next); |
| 1025 |
check(n == 4 && g_low_mem_windowed == 0, |
| 1026 |
"does not put a window bank on an unbanked low-memory read"); |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
reset_all(); |
| 1032 |
g_ram_bank = 0x11; |
| 1033 |
g_rom_bank = 0x17; |
| 1034 |
const uint8_t head[] = { 0x8D }; |
| 1035 |
const uint8_t tail[] = { 0x34, 0x12 }; |
| 1036 |
poke_banked(0xBFFF, 5, head, sizeof(head)); |
| 1037 |
poke_banked(0xC000, 2, tail, sizeof(tail)); |
| 1038 |
const uint8_t straddle_decoy[] = { 0x00, 0x00 }; |
| 1039 |
poke_banked(0xC000, 0x17, straddle_decoy, sizeof(straddle_decoy)); |
| 1040 |
|
| 1041 |
n = code_map_disasm_forward(0xBFFF, 0, 5, 2, 1, lines, 4, &next); |
| 1042 |
check(n == 1 && lines[0].size == 3 && lines[0].bytes[0] == 0x8D && |
| 1043 |
lines[0].bytes[1] == 0x34 && lines[0].bytes[2] == 0x12, |
| 1044 |
"shows each byte of a window-straddling instruction from its own bank"); |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
reset_all(); |
| 1067 |
regs.is65c816 = true; |
| 1068 |
regs.e = 0; |
| 1069 |
regs.status = FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH; |
| 1070 |
g_ram_bank = 0x11; |
| 1071 |
g_rom_bank = 0x17; |
| 1072 |
|
| 1073 |
const uint8_t rep_op[] = { 0xC2 }; |
| 1074 |
const uint8_t rep_arg[] = { 0x20 }; |
| 1075 |
const uint8_t rep_decoy[] = { 0x00 }; |
| 1076 |
const uint8_t lda_imm[] = { 0xA9, 0xEA, 0xEA }; |
| 1077 |
poke_banked(0xBFFF, 5, rep_op, sizeof(rep_op)); |
| 1078 |
poke_banked(0xC000, 2, rep_arg, sizeof(rep_arg)); |
| 1079 |
poke_banked(0xC000, 5, rep_decoy, sizeof(rep_decoy)); |
| 1080 |
poke_banked(0xC001, 2, lda_imm, sizeof(lda_imm)); |
| 1081 |
|
| 1082 |
n = code_map_disasm_forward(0xBFFF, 0, 5, 2, 2, lines, 4, &next); |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
check(n == 2 && lines[0].addr == 0xBFFF && lines[0].size == 2, |
| 1087 |
"sizes a REP that straddles the window boundary"); |
| 1088 |
check(n == 2 && lines[1].addr == 0xC001 && lines[1].size == 3, |
| 1089 |
"reads a REP operand through the window backing the operand"); |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
reset_all(); |
| 1094 |
regs.is65c816 = true; |
| 1095 |
regs.e = 0; |
| 1096 |
regs.status = FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH; |
| 1097 |
g_ram_bank = 0x11; |
| 1098 |
const uint8_t sep_op[] = { 0xC2 }; |
| 1099 |
const uint8_t sep_arg[] = { 0x20 }; |
| 1100 |
const uint8_t sep_bad[] = { 0x00 }; |
| 1101 |
poke_banked(0x9FFF, 0, sep_op, sizeof(sep_op)); |
| 1102 |
poke_banked(0xA000, 4, sep_arg, sizeof(sep_arg)); |
| 1103 |
poke_banked(0xA000, 0x11, sep_bad, sizeof(sep_bad)); |
| 1104 |
poke_banked(0xA001, 4, lda_imm, sizeof(lda_imm)); |
| 1105 |
|
| 1106 |
n = code_map_disasm_forward(0x9FFF, 0, 4, 0, 2, lines, 4, &next); |
| 1107 |
check(n == 2 && lines[1].addr == 0xA001 && lines[1].size == 3, |
| 1108 |
"reads a REP operand across the $9FFF/$A000 boundary correctly"); |
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
reset_all(); |
| 1116 |
regs.is65c816 = true; |
| 1117 |
regs.e = 0; |
| 1118 |
regs.status = FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH; |
| 1119 |
poke_banked(0xFFFF, 3, sep_op, sizeof(sep_op)); |
| 1120 |
poke(0x0000, sep_arg, sizeof(sep_arg)); |
| 1121 |
poke(0x0001, lda_imm, sizeof(lda_imm)); |
| 1122 |
|
| 1123 |
n = code_map_disasm_forward(0xFFFF, 0, 0, 3, 2, lines, 4, &next); |
| 1124 |
check(n == 2 && lines[1].addr == 0x0001 && lines[1].size == 3, |
| 1125 |
"wraps the REP operand address past $FFFF instead of reading past the end"); |
| 1126 |
|
| 1127 |
regs.is65c816 = false; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
{ |
| 1137 |
reset_all(); |
| 1138 |
const uint8_t lda[] = { 0xA9, 0x12 }; |
| 1139 |
const uint8_t nop[] = { 0xEA, 0xEA }; |
| 1140 |
poke_banked(0xA000, 5, lda, sizeof(lda)); |
| 1141 |
poke_banked(0xA000, 6, nop, sizeof(nop)); |
| 1142 |
|
| 1143 |
g_ram_bank = 6; |
| 1144 |
code_map_record(0xA000, 0, 5, 0, regs.status); |
| 1145 |
check(code_map_is_recorded_start(0xA000, 0, 5, 0), |
| 1146 |
"validates an anchor against the bank it was recorded in"); |
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
poke_banked(0xA000, 5, nop, sizeof(nop)); |
| 1151 |
check(!code_map_is_recorded_start(0xA000, 0, 5, 0), |
| 1152 |
"drops the anchor when its own bank's code changes"); |
| 1153 |
} |
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
{ |
| 1160 |
reset_all(); |
| 1161 |
is_gen2 = true; |
| 1162 |
g_rom_bank = 0x17; |
| 1163 |
const uint8_t code[] = { 0xA9, 0x12 }; |
| 1164 |
const uint8_t decoy[] = { 0x8D, 0x34, 0x12 }; |
| 1165 |
poke_gen2(1, 0xC000, code, sizeof(code)); |
| 1166 |
poke_banked(0xC000, 0x17, decoy, sizeof(decoy)); |
| 1167 |
poke_banked(0xC000, 3, decoy, sizeof(decoy)); |
| 1168 |
|
| 1169 |
code_map_line_t lines[4]; |
| 1170 |
uint16_t next = 0; |
| 1171 |
int n = code_map_disasm_forward(0xC000, 1, 0, 3, 1, lines, 4, &next); |
| 1172 |
check(n == 1 && lines[0].size == 2 && lines[0].bytes[0] == 0xA9 && |
| 1173 |
lines[0].bytes[1] == 0x12, |
| 1174 |
"reads a Gen2 program bank as flat RAM, ignoring the ROM window"); |
| 1175 |
|
| 1176 |
is_gen2 = false; |
| 1177 |
} |
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
{ |
| 1197 |
reset_all(); |
| 1198 |
regs.is65c816 = true; |
| 1199 |
regs.e = 0; |
| 1200 |
|
| 1201 |
regs.status = FLAG_INDEX_WIDTH | FLAG_CARRY; |
| 1202 |
|
| 1203 |
const uint8_t code[] = { 0x4A, 0xFB, 0xA9, 0xEA, 0xEA, 0xA9, 0xEA, 0xEA }; |
| 1204 |
poke(0x8000, code, sizeof(code)); |
| 1205 |
|
| 1206 |
code_map_line_t lines[8]; |
| 1207 |
uint16_t next = 0; |
| 1208 |
int n = code_map_disasm_forward(0x8000, 0, 0, 0, 3, lines, 8, &next); |
| 1209 |
|
| 1210 |
check(n == 3 && lines[2].addr == 0x8002 && lines[2].size == 2, |
| 1211 |
"KNOWN LIMITATION: a stale carry into XCE mispredicts the widths"); |
| 1212 |
check(n == 3 && !lines[2].recorded, |
| 1213 |
"the mispredicted line is still reported as a guess"); |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
const uint8_t st_16 = FLAG_INDEX_WIDTH; |
| 1223 |
code_map_record(0x8002, 0, 0, 0, st_16); |
| 1224 |
n = code_map_disasm_forward(0x8000, 0, 0, 0, 4, lines, 8, &next); |
| 1225 |
check(n == 4 && lines[2].addr == 0x8002 && lines[2].size == 3 && |
| 1226 |
lines[2].recorded, |
| 1227 |
"an anchor recovers the width of its own line after a bad XCE"); |
| 1228 |
check(n == 4 && lines[3].addr == 0x8005 && lines[3].size == 2 && |
| 1229 |
!lines[3].recorded, |
| 1230 |
"KNOWN LIMITATION: past that anchor a stale E mis-sizes again"); |
| 1231 |
|
| 1232 |
regs.is65c816 = false; |
| 1233 |
} |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
{ |
| 1260 |
reset_all(); |
| 1261 |
regs.is65c816 = true; |
| 1262 |
regs.e = 1; |
| 1263 |
regs.a = 0x80; |
| 1264 |
regs.status = FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH; |
| 1265 |
|
| 1266 |
const uint8_t code[] = { |
| 1267 |
0x0A, |
| 1268 |
0xFB, |
| 1269 |
0xC2, 0x20, |
| 1270 |
0xA9, 0xEA, 0xEA, |
| 1271 |
}; |
| 1272 |
poke(0x8000, code, sizeof(code)); |
| 1273 |
|
| 1274 |
code_map_line_t lines[8]; |
| 1275 |
uint16_t next = 0; |
| 1276 |
int n = code_map_disasm_forward(0x8000, 0, 0, 0, 4, lines, 8, &next); |
| 1277 |
|
| 1278 |
check(n == 4 && lines[3].addr == 0x8004 && lines[3].size == 3, |
| 1279 |
"KNOWN LIMITATION: a stale carry mis-sizes even in emulation mode"); |
| 1280 |
check(n == 4 && !lines[3].recorded, |
| 1281 |
"that mis-sized line is still reported as a guess"); |
| 1282 |
|
| 1283 |
regs.is65c816 = false; |
| 1284 |
regs.e = 0; |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
{ |
| 1296 |
reset_all(); |
| 1297 |
regs.is65c816 = true; |
| 1298 |
regs.e = 0; |
| 1299 |
|
| 1300 |
|
| 1301 |
const uint8_t old_code[] = { 0xA9, 0x11 }; |
| 1302 |
poke(0x8000, old_code, sizeof(old_code)); |
| 1303 |
code_map_record(0x8000, 0, 0, 0, FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH); |
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
const uint8_t new_code[] = { 0xA9, 0xEA, 0xEA, 0xEA }; |
| 1310 |
poke(0x8000, new_code, sizeof(new_code)); |
| 1311 |
regs.status = FLAG_INDEX_WIDTH; |
| 1312 |
|
| 1313 |
code_map_line_t lines[4]; |
| 1314 |
uint16_t next = 0; |
| 1315 |
int n = code_map_disasm_forward(0x8000, 0, 0, 0, 2, lines, 4, &next); |
| 1316 |
|
| 1317 |
check(n == 2 && lines[0].size == 2, |
| 1318 |
"KNOWN LIMITATION: a stale anchor can size an instruction too short"); |
| 1319 |
check(n == 2 && lines[1].addr == 0x8002, |
| 1320 |
"KNOWN LIMITATION: the next row then starts inside a real operand"); |
| 1321 |
check_tiles(lines, n, "the too-short reading still tiles, so it looks correct"); |
| 1322 |
|
| 1323 |
regs.is65c816 = false; |
| 1324 |
} |
| 1325 |
{ |
| 1326 |
reset_all(); |
| 1327 |
code_map_line_t lines[4]; |
| 1328 |
int center_index = -99; |
| 1329 |
|
| 1330 |
check(code_map_disasm_window(0x8000, 0, 0, 0, 2, 2, NULL, 4, ¢er_index) == 0, |
| 1331 |
"declines a NULL output buffer"); |
| 1332 |
check(center_index == -1, "clears the center index when it declines"); |
| 1333 |
check(code_map_disasm_window(0x8000, 0, 0, 0, 2, 2, lines, 0, NULL) == 0, |
| 1334 |
"declines a zero-capacity buffer"); |
| 1335 |
|
| 1336 |
uint16_t next = 0x1234; |
| 1337 |
check(code_map_disasm_forward(0x8000, 0, 0, 0, 4, NULL, 4, &next) == 0, |
| 1338 |
"forward disassembly declines a NULL buffer"); |
| 1339 |
check(next == 0x8000, "leaves next_addr at the start when it declines"); |
| 1340 |
check(code_map_disasm_forward(0x8000, 0, 0, 0, -1, lines, 4, &next) == 0, |
| 1341 |
"treats a negative count as nothing to do"); |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
center_index = -99; |
| 1347 |
int n = code_map_disasm_window(0x8000, 0, 0, 0, -5, 1, lines, 4, ¢er_index); |
| 1348 |
check(n == 1 && center_index == 0 && lines[0].addr == 0x8000, |
| 1349 |
"treats a negative lines_before as none at all"); |
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
static code_map_line_t wide[300]; |
| 1357 |
center_index = -99; |
| 1358 |
n = code_map_disasm_window(0x8000, 0, 0, 0, 100000, 1, wide, 300, ¢er_index); |
| 1359 |
check(n == 256 && center_index == 255, |
| 1360 |
"caps an absurd lines_before at the backward-walk ceiling"); |
| 1361 |
check_tiles(wide, n, "tiles a window capped by the backward-walk ceiling"); |
| 1362 |
} |
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
{ |
| 1370 |
reset_all(); |
| 1371 |
|
| 1372 |
|
| 1373 |
const uint8_t tail[] = { 0xA9, 0x12 }; |
| 1374 |
const uint8_t head[] = { 0x8D, 0x34, 0x12 }; |
| 1375 |
poke(0xFFFE, tail, sizeof(tail)); |
| 1376 |
poke(0x0000, head, sizeof(head)); |
| 1377 |
|
| 1378 |
code_map_line_t lines[4]; |
| 1379 |
uint16_t next = 0; |
| 1380 |
int n = code_map_disasm_forward(0xFFFE, 0, 0, 0, 2, lines, 4, &next); |
| 1381 |
|
| 1382 |
check(n == 2 && lines[0].addr == 0xFFFE && lines[0].size == 2, |
| 1383 |
"decodes the instruction at the very top of memory"); |
| 1384 |
check(n == 2 && lines[1].addr == 0x0000 && lines[1].size == 3 && |
| 1385 |
lines[1].bytes[0] == 0x8D && lines[1].bytes[2] == 0x12, |
| 1386 |
"wraps to $0000 and decodes the bytes actually there"); |
| 1387 |
check(next == 0x0003, "reports the wrapped next address"); |
| 1388 |
check_tiles(lines, n, "tiles across the $FFFF wrap"); |
| 1389 |
} |
| 1390 |
|
| 1391 |
if (failures) { |
| 1392 |
printf("\nFAILED (%d failures)\n", failures); |
| 1393 |
return 1; |
| 1394 |
} |
| 1395 |
printf("\nPASSED (0 failures)\n"); |
| 1396 |
return 0; |
| 1397 |
} |