OXYGENHUB
# 9120e87480a6 enhanced-x16-emulator / tests / test_debug_core.c
65.9 KiB · C · last changed chore(): initial commit 25 days ago source blame history raw
1
// Checks for the debugger breakpoint core (src/debug_core.c).
2
//
3
// Upstream tracked one breakpoint in one global, so there was very little to
4
// get wrong. A table with bank matching, conditions and ignore counts has real
5
// decision logic in it, and that logic decides whether a debugging session
6
// stops where you asked -- which is exactly the sort of thing that should not
7
// be verified by clicking around a UI.
8
//
9
// debug_core.c reaches for only the register file, the two bank registers and a
10
// debug memory read, so this supplies its own and links no emulator core.
11
12
#include "debug_core.h"
13
#include "cpu/registers.h"
14
15
#include <stdbool.h>
16
#include <stdint.h>
17
#include <stdio.h>
18
#include <string.h>
19
20
// ---- Stand-ins for the emulator core --------------------------------------
21
22
struct regs regs;
23
bool        is_gen2;
24
25
static uint8_t g_mem[0x10000];
26
static uint8_t g_ram_bank = 0;
27
static uint8_t g_rom_bank = 0;
28
29
// The four CPU configurations the emulator can be in, since three of them
30
// change how wide a register comparison is.
31
typedef enum { CPU_65C02, CPU_816_EMU, CPU_816_NATIVE_8, CPU_816_NATIVE_16 } cpu_mode_t;
32
33
static const char *
34
cpu_mode_name(cpu_mode_t m)
35
{
36
	switch (m) {
37
		case CPU_65C02:          return "65C02";
38
		case CPU_816_EMU:        return "65C816 emulation";
39
		case CPU_816_NATIVE_8:   return "65C816 native, 8-bit";
40
		case CPU_816_NATIVE_16:  return "65C816 native, 16-bit";
41
	}
42
	return "?";
43
}
44
45
static void
46
set_cpu_mode(cpu_mode_t m)
47
{
48
	switch (m) {
49
		case CPU_65C02:
50
			regs.is65c816 = false;
51
			regs.e        = 1;
52
			regs.status   = FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH;
53
			break;
54
		case CPU_816_EMU:
55
			regs.is65c816 = true;
56
			regs.e        = 1;
57
			// Emulation mode forces 8-bit whatever these bits say.
58
			regs.status   = 0;
59
			break;
60
		case CPU_816_NATIVE_8:
61
			regs.is65c816 = true;
62
			regs.e        = 0;
63
			regs.status   = FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH;
64
			break;
65
		case CPU_816_NATIVE_16:
66
			regs.is65c816 = true;
67
			regs.e        = 0;
68
			regs.status   = 0;
69
			break;
70
	}
71
}
72
73
// True where the CPU is treating A/X/Y as 16-bit.
74
static bool
75
mode_is_16bit(cpu_mode_t m)
76
{
77
	return m == CPU_816_NATIVE_16;
78
}
79
80
uint8_t
81
real_read6502(uint16_t address, uint8_t bank, bool debugOn, int16_t x16Bank)
82
{
83
	(void)bank;
84
	(void)debugOn;
85
	(void)x16Bank;
86
	return g_mem[address];
87
}
88
89
uint8_t
90
memory_get_ram_bank(void)
91
{
92
	return g_ram_bank;
93
}
94
95
uint8_t
96
memory_get_rom_bank(void)
97
{
98
	return g_rom_bank;
99
}
100
101
// ---- Harness ---------------------------------------------------------------
102
103
static int failures = 0;
104
105
static void
106
check(bool cond, const char *what)
107
{
108
	if (!cond) {
109
		failures++;
110
		printf("FAIL: %s\n", what);
111
	} else {
112
		printf("ok  : %s\n", what);
113
	}
114
}
115
116
static void
117
reset_all(void)
118
{
119
	debug_core_free();
120
	memset(g_mem, 0, sizeof(g_mem));
121
	memset(&regs, 0, sizeof(regs));
122
	g_ram_bank = 0;
123
	g_rom_bank = 0;
124
}
125
126
static struct breakpoint
127
bp_at(int pc, uint8_t bank, int x16Bank)
128
{
129
	struct breakpoint bp = { pc, bank, x16Bank, 0, false };
130
	return bp;
131
}
132
133
// ---- Owner-defaulting shim -------------------------------------------------
134
//
135
// Ownership is exercised on its own further down. Everything above it is about
136
// bank matching, conditions and counts, which are the same whoever asked, so
137
// those cases name a single owner through these rather than restating it on
138
// every line. Deliberately thin: each is the ownership call the emulator itself
139
// makes, with DEBUG_OWNER_CLI standing in for "somebody".
140
141
static int
142
debug_bp_add(struct breakpoint bp)
143
{
144
	// The old entry point answered with the new entry's index, and several
145
	// cases below check that a second breakpoint got its own slot rather than
146
	// displacing the first. A created entry always lands at the end.
147
	return debug_bp_add_for(bp, DEBUG_OWNER_CLI) == DEBUG_ADD_CREATED ? numBreakpoints - 1 : -1;
148
}
149
150
static bool
151
debug_bp_remove(int pc, uint8_t bank, int x16Bank)
152
{
153
	return debug_bp_delete(pc, bank, x16Bank);
154
}
155
156
static void
157
debug_bp_toggle(int pc, uint8_t bank, int x16Bank)
158
{
159
	debug_bp_toggle_for(pc, bank, x16Bank, DEBUG_OWNER_CLI);
160
}
161
162
static int
163
debug_wp_add(uint16_t addr, uint16_t len, int x16Bank)
164
{
165
	return debug_wp_add_for(addr, len, x16Bank, DEBUG_OWNER_CLI) == DEBUG_ADD_CREATED
166
	           ? debug_wp_count() - 1
167
	           : -1;
168
}
169
170
static bool
171
debug_wp_remove(uint16_t addr, int x16Bank)
172
{
173
	return debug_wp_delete(addr, x16Bank);
174
}
175
176
int
177
main(void)
178
{
179
	// ── The table ───────────────────────────────────────────────────────────
180
	// Upstream had exactly one breakpoint, so setting a second silently threw
181
	// the first away. Holding several at once is the whole point of the table.
182
	{
183
		reset_all();
184
		check(numBreakpoints == 0, "starts with no breakpoints");
185
		check(!debug_bp_is_set(0x8000, 0), "reports nothing set before anything is added");
186
187
		check(debug_bp_add(bp_at(0x8000, 0, -1)) == 0, "adds a breakpoint");
188
		check(debug_bp_add(bp_at(0x9000, 0, -1)) == 1, "adds a second without losing the first");
189
		check(numBreakpoints == 2, "keeps both");
190
		check(debug_bp_is_set(0x8000, 0) && debug_bp_is_set(0x9000, 0),
191
		      "both breakpoints are live at once");
192
193
		check(debug_bp_add(bp_at(0x8000, 0, -1)) == -1, "refuses a duplicate address");
194
		check(numBreakpoints == 2, "a refused duplicate does not grow the table");
195
196
		check(debug_bp_remove(0x8000, 0, -1), "removes a breakpoint");
197
		check(!debug_bp_is_set(0x8000, 0), "the removed one stops matching");
198
		check(debug_bp_is_set(0x9000, 0), "removing one leaves the other alone");
199
		check(!debug_bp_remove(0x8000, 0, -1), "removing a missing breakpoint reports nothing done");
200
201
		// Removal compacts the array; the survivors must still be intact.
202
		reset_all();
203
		for (int i = 0; i < 5; i++) {
204
			debug_bp_add(bp_at(0x8000 + i * 0x10, 0, -1));
205
		}
206
		debug_bp_remove(0x8010, 0, -1);
207
		debug_bp_remove(0x8030, 0, -1);
208
		check(numBreakpoints == 3, "removes from the middle");
209
		check(debug_bp_is_set(0x8000, 0) && debug_bp_is_set(0x8020, 0) && debug_bp_is_set(0x8040, 0),
210
		      "the survivors are still the right ones");
211
		check(!debug_bp_is_set(0x8010, 0) && !debug_bp_is_set(0x8030, 0),
212
		      "the removed ones are gone");
213
	}
214
215
	// ── Growing past the initial capacity ───────────────────────────────────
216
	// The table starts small and doubles; entries have to survive the
217
	// reallocations that implies.
218
	{
219
		reset_all();
220
		bool all_added = true;
221
		for (int i = 0; i < 200; i++) {
222
			if (debug_bp_add(bp_at(0x1000 + i, 0, -1)) != i) {
223
				all_added = false;
224
			}
225
		}
226
		check(all_added, "adds 200 breakpoints, each at the expected index");
227
		check(numBreakpoints == 200, "grows past the initial capacity");
228
		check(debug_bp_is_set(0x1000, 0) && debug_bp_is_set(0x1063, 0) && debug_bp_is_set(0x10C7, 0),
229
		      "entries survive the reallocations");
230
	}
231
232
	// ── Toggling ────────────────────────────────────────────────────────────
233
	{
234
		reset_all();
235
		debug_bp_toggle(0x8000, 0, -1);
236
		check(debug_bp_is_set(0x8000, 0), "toggle adds when absent");
237
		debug_bp_toggle(0x8000, 0, -1);
238
		check(!debug_bp_is_set(0x8000, 0), "toggle removes when present");
239
240
		// Toggling a banked address must act on the bank being displayed, not
241
		// on whatever entry happens to share the address. Keying the table on
242
		// (pc, bank) alone made F9 in bank 6 delete the bank-5 breakpoint and
243
		// arm nothing -- so the user lost a breakpoint and gained none.
244
		reset_all();
245
		g_ram_bank = 5;
246
		debug_bp_toggle(0xA100, 0, 5);
247
		check(debug_bp_is_set(0xA100, 0), "arms a breakpoint in the displayed bank");
248
249
		g_ram_bank = 6;
250
		debug_bp_toggle(0xA100, 0, 6);
251
		check(debug_bp_is_set(0xA100, 0), "toggling the same address in another bank arms it");
252
		g_ram_bank = 5;
253
		check(debug_bp_is_set(0xA100, 0), "and leaves the first bank's breakpoint alone");
254
		check(numBreakpoints == 2, "both banks' breakpoints coexist");
255
256
		// And toggling again removes only the one for that bank.
257
		g_ram_bank = 6;
258
		debug_bp_toggle(0xA100, 0, 6);
259
		check(!debug_bp_is_set(0xA100, 0), "toggling off removes the right one");
260
		g_ram_bank = 5;
261
		check(debug_bp_is_set(0xA100, 0), "the other bank's breakpoint survives");
262
	}
263
264
	// ── Same address in two banks ───────────────────────────────────────────
265
	// Banked RAM means $A000 holds a different program in each bank, so wanting
266
	// a breakpoint at the same address in two of them is ordinary, not exotic.
267
	{
268
		reset_all();
269
		check(debug_bp_add(bp_at(0xA000, 0, 5)) == 0, "adds a breakpoint in bank 5");
270
		check(debug_bp_add(bp_at(0xA000, 0, 6)) == 1,
271
		      "adds one at the same address in bank 6");
272
		check(numBreakpoints == 2, "keeps both");
273
274
		// Each keeps its own hit count, rather than sharing one.
275
		g_ram_bank = 5;
276
		check(debug_bp_on_arrival(0xA000, 0), "stops in bank 5");
277
		check(debug_bp_on_arrival(0xA000, 0), "stops in bank 5 again");
278
		g_ram_bank = 6;
279
		check(debug_bp_on_arrival(0xA000, 0), "stops in bank 6");
280
		check(debug_bp_get_hits(0xA000, 0, 5) == 2 && debug_bp_get_hits(0xA000, 0, 6) == 1,
281
		      "each bank's breakpoint counts its own hits");
282
283
		// And its own condition.
284
		debug_bp_set_condition(0xA000, 0, 5, BPOPERAND_A, 0, BPCMP_EQ, 0x11);
285
		regs.c = 0x22;
286
		g_ram_bank = 5;
287
		check(!debug_bp_on_arrival(0xA000, 0), "bank 5's condition applies in bank 5");
288
		g_ram_bank = 6;
289
		check(debug_bp_on_arrival(0xA000, 0), "and does not leak into bank 6");
290
291
		check(debug_bp_add(bp_at(0xA000, 0, 5)) == -1,
292
		      "still refuses a true duplicate (same address AND bank)");
293
	}
294
295
	// ── Bank matching ───────────────────────────────────────────────────────
296
	// Banked RAM means the same address holds unrelated code depending on which
297
	// bank is mapped. A breakpoint set while looking at bank 5 must not fire on
298
	// bank 6's code, which upstream's single unbanked breakpoint could not
299
	// express at all.
300
	{
301
		reset_all();
302
		debug_bp_add(bp_at(0xA000, 0, 5));
303
304
		g_ram_bank = 5;
305
		check(debug_bp_is_set(0xA000, 0), "fires in the bank it was set for");
306
		g_ram_bank = 6;
307
		check(!debug_bp_is_set(0xA000, 0), "does not fire in a different RAM bank");
308
309
		// Below $A000 nothing is banked, so the bank must be ignored entirely
310
		// rather than accidentally filtering the breakpoint out.
311
		reset_all();
312
		debug_bp_add(bp_at(0x0801, 0, -1));
313
		g_ram_bank = 3;
314
		check(debug_bp_is_set(0x0801, 0), "unbanked addresses ignore the RAM bank");
315
316
		// The ROM window follows the ROM bank, not the RAM bank.
317
		reset_all();
318
		debug_bp_add(bp_at(0xC000, 0, 2));
319
		g_rom_bank = 2;
320
		g_ram_bank = 9;
321
		check(debug_bp_is_set(0xC000, 0), "the ROM window follows the ROM bank");
322
		g_rom_bank = 3;
323
		check(!debug_bp_is_set(0xC000, 0), "does not fire in a different ROM bank");
324
	}
325
326
	// ── Stopping, and counting ──────────────────────────────────────────────
327
	{
328
		reset_all();
329
		debug_bp_add(bp_at(0x8000, 0, -1));
330
331
		check(!debug_bp_on_arrival(0x7FFF, 0), "does not stop where no breakpoint is");
332
		check(debug_bp_get_hits(0x8000, 0, -1) == 0, "a breakpoint starts with no hits");
333
334
		check(debug_bp_on_arrival(0x8000, 0), "stops on a plain breakpoint");
335
		check(debug_bp_get_hits(0x8000, 0, -1) == 1, "counts the hit");
336
		check(debug_bp_on_arrival(0x8000, 0), "stops again");
337
		check(debug_bp_get_hits(0x8000, 0, -1) == 2, "keeps counting");
338
339
		debug_bp_reset_hits(0x8000, 0, -1);
340
		check(debug_bp_get_hits(0x8000, 0, -1) == 0, "the count can be reset");
341
342
		// A count is kept for an address even without a condition, and survives
343
		// the remove/re-add a UI performs when a breakpoint is disabled and
344
		// re-enabled -- silently resetting it there would be surprising.
345
		debug_bp_on_arrival(0x8000, 0);
346
		debug_bp_remove(0x8000, 0, -1);
347
		debug_bp_add(bp_at(0x8000, 0, -1));
348
		check(debug_bp_get_hits(0x8000, 0, -1) == 1, "the count survives a disable/enable cycle");
349
350
		// An explicit delete does discard it.
351
		debug_bp_remove(0x8000, 0, -1);
352
		debug_bp_forget(0x8000, 0, -1);
353
		debug_bp_add(bp_at(0x8000, 0, -1));
354
		check(debug_bp_get_hits(0x8000, 0, -1) == 0, "an explicit delete discards the count");
355
	}
356
357
	// ── Ignore counts ───────────────────────────────────────────────────────
358
	// "Stop the 100th time round this loop" is the reason to have these: the
359
	// hit is counted every time, but execution only stops once the budget is
360
	// spent.
361
	{
362
		reset_all();
363
		debug_bp_add(bp_at(0x8000, 0, -1));
364
		debug_bp_set_ignore(0x8000, 0, -1, 3);
365
366
		check(!debug_bp_on_arrival(0x8000, 0), "does not stop on the 1st of 3 ignored hits");
367
		check(!debug_bp_on_arrival(0x8000, 0), "does not stop on the 2nd");
368
		check(!debug_bp_on_arrival(0x8000, 0), "does not stop on the 3rd");
369
		check(debug_bp_on_arrival(0x8000, 0), "stops on the 4th, once the budget is spent");
370
		check(debug_bp_get_hits(0x8000, 0, -1) == 4, "ignored arrivals are still counted");
371
		check(debug_bp_on_arrival(0x8000, 0), "keeps stopping thereafter");
372
	}
373
374
	// ── Conditions ──────────────────────────────────────────────────────────
375
	{
376
		reset_all();
377
		debug_bp_add(bp_at(0x8000, 0, -1));
378
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_A, 0, BPCMP_EQ, 0x42);
379
380
		regs.c = 0x41;
381
		check(!debug_bp_on_arrival(0x8000, 0), "does not stop while the condition is false");
382
		check(debug_bp_get_hits(0x8000, 0, -1) == 0,
383
		      "an unsatisfied condition does not spend a hit");
384
385
		regs.c = 0x42;
386
		check(debug_bp_on_arrival(0x8000, 0), "stops once the condition holds");
387
388
		// Every comparison, so none of them is quietly inverted.
389
		regs.c = 0x10;
390
		struct { int op; uint32_t val; bool want; const char *what; } cases[] = {
391
			{ BPCMP_EQ, 0x10, true,  "== when equal" },
392
			{ BPCMP_EQ, 0x11, false, "== when not equal" },
393
			{ BPCMP_NE, 0x11, true,  "!= when different" },
394
			{ BPCMP_NE, 0x10, false, "!= when same" },
395
			{ BPCMP_LT, 0x11, true,  "< when below" },
396
			{ BPCMP_LT, 0x10, false, "< when equal" },
397
			{ BPCMP_LE, 0x10, true,  "<= when equal" },
398
			{ BPCMP_LE, 0x0F, false, "<= when above" },
399
			{ BPCMP_GT, 0x0F, true,  "> when above" },
400
			{ BPCMP_GT, 0x10, false, "> when equal" },
401
			{ BPCMP_GE, 0x10, true,  ">= when equal" },
402
			{ BPCMP_GE, 0x11, false, ">= when below" },
403
		};
404
		bool all_ok = true;
405
		for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
406
			debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_A, 0, cases[i].op, cases[i].val);
407
			if (debug_bp_on_arrival(0x8000, 0) != cases[i].want) {
408
				printf("      (comparison wrong: %s)\n", cases[i].what);
409
				all_ok = false;
410
			}
411
		}
412
		check(all_ok, "every comparison operator behaves as named");
413
414
		// Clearing the condition leaves an unconditional breakpoint behind
415
		// rather than deleting it.
416
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_A, 0, BPCMP_EQ, 0xFF);
417
		regs.c = 0x00;
418
		check(!debug_bp_on_arrival(0x8000, 0), "condition still applies before clearing");
419
		debug_bp_clear_condition(0x8000, 0, -1);
420
		check(debug_bp_on_arrival(0x8000, 0), "clearing the condition makes it fire again");
421
	}
422
423
	// ── Condition operands ──────────────────────────────────────────────────
424
	{
425
		reset_all();
426
		debug_bp_add(bp_at(0x8000, 0, -1));
427
428
		regs.x  = 0x33;
429
		regs.y  = 0x44;
430
		regs.sp = 0x01F0;
431
		regs.status = 0x24;
432
433
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_X, 0, BPCMP_EQ, 0x33);
434
		check(debug_bp_on_arrival(0x8000, 0), "tests X");
435
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_Y, 0, BPCMP_EQ, 0x44);
436
		check(debug_bp_on_arrival(0x8000, 0), "tests Y");
437
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_P, 0, BPCMP_EQ, 0x24);
438
		check(debug_bp_on_arrival(0x8000, 0), "tests the status register");
439
440
		// SP is compared 16-bit wide, so a stack pointer above $FF is not
441
		// truncated into matching the wrong value.
442
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_SP, 0, BPCMP_EQ, 0x01F0);
443
		check(debug_bp_on_arrival(0x8000, 0), "tests the full 16-bit stack pointer");
444
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_SP, 0, BPCMP_EQ, 0x00F0);
445
		check(!debug_bp_on_arrival(0x8000, 0), "does not truncate the stack pointer to 8 bits");
446
447
		// Memory operands: "stop when this variable becomes N" is the most
448
		// useful conditional breakpoint there is.
449
		g_mem[0x1234] = 0x99;
450
		g_mem[0x1235] = 0xAB;
451
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_BYTE, 0x1234, BPCMP_EQ, 0x99);
452
		check(debug_bp_on_arrival(0x8000, 0), "tests a byte in memory");
453
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_WORD, 0x1234, BPCMP_EQ, 0xAB99);
454
		check(debug_bp_on_arrival(0x8000, 0), "tests a little-endian word in memory");
455
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_WORD, 0x1234, BPCMP_EQ, 0x99AB);
456
		check(!debug_bp_on_arrival(0x8000, 0), "reads the word in the right byte order");
457
	}
458
459
	// ── Conditions and ignore counts together ───────────────────────────────
460
	// The ignore count counts *satisfying* arrivals, so "the 3rd time we pass
461
	// here with A == 7" means what it says, rather than counting arrivals that
462
	// failed the test.
463
	{
464
		reset_all();
465
		debug_bp_add(bp_at(0x8000, 0, -1));
466
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_A, 0, BPCMP_EQ, 0x07);
467
		debug_bp_set_ignore(0x8000, 0, -1, 2);
468
469
		regs.c = 0x00;
470
		for (int i = 0; i < 10; i++) {
471
			debug_bp_on_arrival(0x8000, 0);
472
		}
473
		check(debug_bp_get_hits(0x8000, 0, -1) == 0,
474
		      "arrivals that fail the condition do not spend the ignore budget");
475
476
		regs.c = 0x07;
477
		check(!debug_bp_on_arrival(0x8000, 0), "1st satisfying arrival is ignored");
478
		check(!debug_bp_on_arrival(0x8000, 0), "2nd satisfying arrival is ignored");
479
		check(debug_bp_on_arrival(0x8000, 0), "3rd satisfying arrival stops");
480
	}
481
482
	// ── Reading a condition back ────────────────────────────────────────────
483
	{
484
		reset_all();
485
		debug_bp_add(bp_at(0x8000, 0, -1));
486
487
		int      has_cond = 9, operand = 9, op = 9;
488
		uint16_t operand_addr = 9;
489
		uint32_t value = 9, ignore = 9;
490
		check(!debug_bp_get_condition(0x8000, 0, -1, &has_cond, &operand, &operand_addr, &op, &value, &ignore),
491
		      "reports nothing for a breakpoint never given a condition");
492
493
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_BYTE, 0x1234, BPCMP_GE, 0x55);
494
		debug_bp_set_ignore(0x8000, 0, -1, 7);
495
		check(debug_bp_get_condition(0x8000, 0, -1, &has_cond, &operand, &operand_addr, &op, &value, &ignore),
496
		      "reads a condition back");
497
		check(has_cond == 1 && operand == BPOPERAND_BYTE && operand_addr == 0x1234
498
		          && op == BPCMP_GE && value == 0x55 && ignore == 7,
499
		      "reads back exactly what was set");
500
501
		// Every out-param is optional.
502
		check(debug_bp_get_condition(0x8000, 0, -1, NULL, NULL, NULL, NULL, NULL, NULL),
503
		      "tolerates NULL out-parameters");
504
	}
505
506
	// ── Register widths follow the CPU ──────────────────────────────────────
507
	// On a 65C816 in native mode A/X/Y can be 16-bit. Comparing only the low
508
	// byte would make "A == $1234" silently mean "A == $34" and stop in the
509
	// wrong place, so the comparison uses the width the CPU is using.
510
	{
511
		reset_all();
512
		debug_bp_add(bp_at(0x8000, 0, -1));
513
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_A, 0, BPCMP_EQ, 0x1234);
514
515
		// 65C02: A is 8 bits, so a 16-bit value can never match.
516
		regs.is65c816 = false;
517
		regs.c        = 0x1234;
518
		check(!debug_bp_on_arrival(0x8000, 0), "an 8-bit A never matches a 16-bit value");
519
520
		// 65C816 native with M clear: A is 16 bits and matches in full.
521
		regs.is65c816 = true;
522
		regs.e        = 0;
523
		regs.status   = FLAG_INDEX_WIDTH;             // M clear => 16-bit accumulator
524
		check(debug_bp_on_arrival(0x8000, 0), "a 16-bit A matches the full value");
525
526
		// The low byte alone must not be enough.
527
		regs.c = 0x9934;
528
		check(!debug_bp_on_arrival(0x8000, 0),
529
		      "a 16-bit A does not match on the low byte alone");
530
531
		// M set => back to 8-bit, where the low byte is all there is.
532
		regs.status = FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH;
533
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_A, 0, BPCMP_EQ, 0x34);
534
		check(debug_bp_on_arrival(0x8000, 0), "an 8-bit A compares its low byte");
535
536
		// Emulation mode forces 8-bit regardless of the width bits.
537
		regs.e      = 1;
538
		regs.status = FLAG_INDEX_WIDTH;
539
		regs.c      = 0x9934;
540
		check(debug_bp_on_arrival(0x8000, 0), "emulation mode forces the 8-bit comparison");
541
542
		// X and Y follow the index-width bit, not the memory-width bit.
543
		regs.e      = 0;
544
		regs.status = FLAG_MEMORY_WIDTH;              // X clear => 16-bit index
545
		regs.x      = 0x0555;
546
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_X, 0, BPCMP_EQ, 0x0555);
547
		check(debug_bp_on_arrival(0x8000, 0), "a 16-bit X matches the full value");
548
549
		regs.y = 0x0666;
550
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_Y, 0, BPCMP_EQ, 0x0666);
551
		check(debug_bp_on_arrival(0x8000, 0), "a 16-bit Y matches the full value");
552
553
		regs.status = FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH;   // both 8-bit again
554
		check(!debug_bp_on_arrival(0x8000, 0),
555
		      "an 8-bit Y does not match a 16-bit value");
556
557
		regs.is65c816 = false;
558
		regs.e        = 0;
559
	}
560
561
	// ── Clearing everything really clears everything ────────────────────────
562
	// A full clear is a delete, not a disable. A front end that replaces its
563
	// whole breakpoint set -- which is how the Debug Adapter Protocol works --
564
	// must not silently inherit an old condition or ignore count on what the
565
	// user thinks is a brand new breakpoint.
566
	{
567
		reset_all();
568
		debug_bp_add(bp_at(0x8000, 0, -1));
569
		debug_bp_set_condition(0x8000, 0, -1, BPOPERAND_A, 0, BPCMP_EQ, 0xAA);
570
		debug_bp_set_ignore(0x8000, 0, -1, 5);
571
		regs.c = 0xAA;
572
		debug_bp_on_arrival(0x8000, 0);
573
574
		debug_bp_clear_all();
575
		debug_bp_add(bp_at(0x8000, 0, -1));
576
577
		check(debug_bp_get_hits(0x8000, 0, -1) == 0, "a full clear discards hit counts");
578
		int has_cond = 9;
579
		check(!debug_bp_get_condition(0x8000, 0, -1, &has_cond, NULL, NULL, NULL, NULL, NULL),
580
		      "a full clear discards conditions");
581
		regs.c = 0x00;
582
		check(debug_bp_on_arrival(0x8000, 0),
583
		      "the recreated breakpoint is unconditional, not the old one");
584
	}
585
586
	// ── Clearing everything ─────────────────────────────────────────────────
587
	{
588
		reset_all();
589
		debug_bp_add(bp_at(0x8000, 0, -1));
590
		debug_bp_add(bp_at(0x9000, 0, -1));
591
		debug_bp_clear_all();
592
		check(numBreakpoints == 0, "clears the whole table");
593
		check(!debug_bp_is_set(0x8000, 0) && !debug_bp_is_set(0x9000, 0),
594
		      "nothing matches after clearing");
595
		check(!debug_bp_on_arrival(0x8000, 0), "and nothing stops");
596
	}
597
598
	// ── Memory write watchpoints ────────────────────────────────────────────
599
	// The counterpart to a breakpoint, for data rather than code: stop when
600
	// something writes here. The tool for finding what is corrupting a variable
601
	// when you have no idea which code is responsible.
602
	{
603
		reset_all();
604
		check(debug_wp_count() == 0, "starts with no watchpoints");
605
		check(!debug_wp_check_write(0x1234, 0x00), "nothing fires before anything is watched");
606
607
		check(debug_wp_add(0x1234, 1, -1) == 0, "adds a watchpoint");
608
		check(debug_wp_count() == 1, "counts it");
609
		check(debug_wp_check_write(0x1234, 0x55), "fires on a write to the watched byte");
610
		check(!debug_wp_check_write(0x1235, 0x55), "does not fire on the next byte");
611
		check(!debug_wp_check_write(0x1233, 0x55), "does not fire on the previous byte");
612
613
		// A second watchpoint on the same address in the same bank is a
614
		// duplicate and is refused, rather than quietly reporting success while
615
		// discarding whatever the caller asked for.
616
		check(debug_wp_add(0x1234, 8, DEBUG_BANK_ANY) == -1 && debug_wp_count() == 1,
617
		      "refuses a duplicate address in the same bank");
618
619
		check(debug_wp_remove(0x1234, DEBUG_BANK_ANY), "removes a watchpoint");
620
		check(debug_wp_count() == 0, "the table is empty again");
621
		check(!debug_wp_check_write(0x1234, 0x55), "the removed one no longer fires");
622
		check(!debug_wp_remove(0x1234, DEBUG_BANK_ANY), "removing a missing one reports nothing done");
623
	}
624
625
	// ── Watching a range ────────────────────────────────────────────────────
626
	{
627
		reset_all();
628
		debug_wp_add(0x2000, 4, -1);
629
		check(debug_wp_check_write(0x2000, 0) && debug_wp_check_write(0x2003, 0),
630
		      "fires anywhere inside the range");
631
		check(!debug_wp_check_write(0x1FFF, 0) && !debug_wp_check_write(0x2004, 0),
632
		      "does not fire either side of it");
633
634
		// A zero length is a single byte, not an empty range that never fires.
635
		reset_all();
636
		debug_wp_add(0x3000, 0, -1);
637
		check(debug_wp_check_write(0x3000, 0), "a zero length watches one byte");
638
639
		// A range running off the top of memory catches nothing beyond it: the
640
		// coverage test is a half-open comparison in 32 bits, so the end cannot
641
		// wrap round to the bottom of memory.
642
		reset_all();
643
		debug_wp_add(0xFFFE, 8, DEBUG_BANK_ANY);
644
		check(debug_wp_check_write(0xFFFF, 0), "watches up to the last byte");
645
		check(!debug_wp_check_write(0x0000, 0), "does not wrap past the end of memory");
646
		check(!debug_wp_check_write(0x0005, 0), "really does not wrap");
647
	}
648
649
	// ── Value filters ───────────────────────────────────────────────────────
650
	// "Stop when this becomes zero" is far more useful than stopping on every
651
	// write to a variable that is written constantly.
652
	{
653
		reset_all();
654
		debug_wp_add(0x1234, 1, -1);
655
		check(debug_wp_set_value(0x1234, DEBUG_BANK_ANY, BPCMP_EQ, 0x00), "attaches a value filter");
656
		check(debug_wp_check_write(0x1234, 0x00), "fires when the written value matches");
657
		check(!debug_wp_check_write(0x1234, 0x01), "stays quiet when it does not");
658
659
		check(!debug_wp_set_value(0x9999, DEBUG_BANK_ANY, BPCMP_EQ, 0),
660
		      "declines a filter for an unwatched address");
661
662
		// Every comparison, so none is quietly inverted.
663
		struct { int op; uint8_t val; uint8_t written; bool want; const char *what; } wcases[] = {
664
			{ BPCMP_EQ, 0x10, 0x10, true,  "== when equal" },
665
			{ BPCMP_EQ, 0x10, 0x11, false, "== when not equal" },
666
			{ BPCMP_NE, 0x10, 0x11, true,  "!= when different" },
667
			{ BPCMP_NE, 0x10, 0x10, false, "!= when same" },
668
			{ BPCMP_LT, 0x10, 0x0F, true,  "< when below" },
669
			{ BPCMP_LT, 0x10, 0x10, false, "< when equal" },
670
			{ BPCMP_LE, 0x10, 0x10, true,  "<= when equal" },
671
			{ BPCMP_LE, 0x10, 0x11, false, "<= when above" },
672
			{ BPCMP_GT, 0x10, 0x11, true,  "> when above" },
673
			{ BPCMP_GT, 0x10, 0x10, false, "> when equal" },
674
			{ BPCMP_GE, 0x10, 0x10, true,  ">= when equal" },
675
			{ BPCMP_GE, 0x10, 0x0F, false, ">= when below" },
676
		};
677
		bool wp_ops_ok = true;
678
		for (unsigned i = 0; i < sizeof(wcases) / sizeof(wcases[0]); i++) {
679
			debug_wp_set_value(0x1234, DEBUG_BANK_ANY, wcases[i].op, wcases[i].val);
680
			if (debug_wp_check_write(0x1234, wcases[i].written) != wcases[i].want) {
681
				printf("      (watchpoint comparison wrong: %s)\n", wcases[i].what);
682
				wp_ops_ok = false;
683
			}
684
		}
685
		check(wp_ops_ok, "every watchpoint comparison behaves as named");
686
	}
687
688
	// ── Watchpoints follow the RAM bank ─────────────────────────────────────
689
	// $A100 holds a different variable in every bank. A watchpoint that fired
690
	// on all of them would bury the write it was set to find.
691
	{
692
		reset_all();
693
		debug_wp_add(0xA100, 1, 5);
694
695
		g_ram_bank = 5;
696
		check(debug_wp_check_write(0xA100, 0x99), "fires in the bank it was set for");
697
		g_ram_bank = 6;
698
		check(!debug_wp_check_write(0xA100, 0x99), "stays quiet in a different bank");
699
700
		// Below the banked window the bank is irrelevant and must be ignored.
701
		reset_all();
702
		debug_wp_add(0x0400, 1, -1);
703
		g_ram_bank = 7;
704
		check(debug_wp_check_write(0x0400, 0x01), "unbanked addresses ignore the RAM bank");
705
706
		// -1 on a banked address means "any bank", for watching an address
707
		// wherever it happens to live.
708
		reset_all();
709
		debug_wp_add(0xA100, 1, -1);
710
		g_ram_bank = 2;
711
		bool any_first = debug_wp_check_write(0xA100, 0x01);
712
		g_ram_bank = 9;
713
		check(any_first && debug_wp_check_write(0xA100, 0x01),
714
		      "a bankless banked watchpoint fires in every bank");
715
	}
716
717
	// ── Enable and disable ──────────────────────────────────────────────────
718
	{
719
		reset_all();
720
		debug_wp_add(0x1234, 1, -1);
721
		debug_wp_set_value(0x1234, DEBUG_BANK_ANY, BPCMP_EQ, 0x42);
722
723
		check(debug_wp_set_active(0x1234, DEBUG_BANK_ANY, false), "disables a watchpoint");
724
		check(!debug_wp_check_write(0x1234, 0x42), "a disabled watchpoint does not fire");
725
		check(debug_wp_count() == 1, "but it is still in the table");
726
727
		check(debug_wp_set_active(0x1234, DEBUG_BANK_ANY, true), "re-enables it");
728
		check(debug_wp_check_write(0x1234, 0x42), "and its value filter survived");
729
		check(!debug_wp_set_active(0x9999, DEBUG_BANK_ANY, true), "declines to enable one that is not there");
730
	}
731
732
	// ── Coverage query, and a full table ────────────────────────────────────
733
	{
734
		reset_all();
735
		debug_wp_add(0x2000, 4, -1);
736
		check(debug_wp_covers(0x2002), "reports a watched address as covered");
737
		check(!debug_wp_covers(0x2004), "reports an unwatched address as not covered");
738
		debug_wp_set_active(0x2000, DEBUG_BANK_ANY, false);
739
		check(!debug_wp_covers(0x2002), "a disabled watchpoint covers nothing");
740
741
		reset_all();
742
		int added = 0;
743
		for (int i = 0; i < MAX_WATCHPOINTS + 10; i++) {
744
			if (debug_wp_add((uint16_t)(0x4000 + i), 1, -1) >= 0) {
745
				added++;
746
			}
747
		}
748
		check(added == MAX_WATCHPOINTS, "fills the table and then refuses more");
749
		check(debug_wp_count() == MAX_WATCHPOINTS, "does not overflow the table");
750
		check(debug_wp_check_write(0x4000, 0) && debug_wp_check_write(0x403F, 0),
751
		      "every entry that fit still works");
752
753
		debug_wp_clear_all();
754
		check(debug_wp_count() == 0, "clears them all");
755
		check(!debug_wp_check_write(0x4000, 0), "and none fire afterwards");
756
	}
757
758
	// ── Removal keeps the survivors intact ──────────────────────────────────
759
	{
760
		reset_all();
761
		for (int i = 0; i < 5; i++) {
762
			debug_wp_add((uint16_t)(0x5000 + i * 0x10), 1, -1);
763
		}
764
		debug_wp_remove(0x5010, DEBUG_BANK_ANY);
765
		debug_wp_remove(0x5030, DEBUG_BANK_ANY);
766
		check(debug_wp_count() == 3, "removes from the middle");
767
		check(debug_wp_check_write(0x5000, 0) && debug_wp_check_write(0x5020, 0)
768
		          && debug_wp_check_write(0x5040, 0),
769
		      "the survivors still fire");
770
		check(!debug_wp_check_write(0x5010, 0) && !debug_wp_check_write(0x5030, 0),
771
		      "the removed ones do not");
772
	}
773
774
	// ── One bank-selector rule, for breakpoints and watchpoints alike ───────
775
	// A bank selector says one of two things: "whichever bank is mapped"
776
	// (DEBUG_BANK_ANY) or "this specific bank". For an address outside a banked
777
	// window it says nothing at all, so it is normalised away on the way in --
778
	// otherwise it would be a hidden field distinguishing two entries that are
779
	// really the same.
780
	{
781
		reset_all();
782
783
		// Breakpoints: ANY fires in every bank.
784
		debug_bp_add(bp_at(0xA000, 0, DEBUG_BANK_ANY));
785
		g_ram_bank = 0;
786
		bool any0 = debug_bp_is_set(0xA000, 0);
787
		g_ram_bank = 5;
788
		bool any5 = debug_bp_is_set(0xA000, 0);
789
		g_ram_bank = 200;
790
		check(any0 && any5 && debug_bp_is_set(0xA000, 0),
791
		      "a bankless breakpoint fires in every RAM bank");
792
793
		// The ROM window follows the ROM bank the same way.
794
		reset_all();
795
		debug_bp_add(bp_at(0xC000, 0, DEBUG_BANK_ANY));
796
		g_rom_bank = 0;
797
		bool rany0 = debug_bp_is_set(0xC000, 0);
798
		g_rom_bank = 31;
799
		check(rany0 && debug_bp_is_set(0xC000, 0),
800
		      "a bankless breakpoint fires in every ROM bank");
801
802
		// A specific bank still means only that bank.
803
		reset_all();
804
		debug_bp_add(bp_at(0xA000, 0, 5));
805
		g_ram_bank = 5;
806
		check(debug_bp_is_set(0xA000, 0), "a specific bank fires in that bank");
807
		g_ram_bank = 6;
808
		check(!debug_bp_is_set(0xA000, 0), "and not in another");
809
810
		// Below the banked window the selector is meaningless, so a stored bank
811
		// must not stop the breakpoint firing, and must not create a second
812
		// distinct breakpoint either.
813
		reset_all();
814
		debug_bp_add(bp_at(0x0801, 0, 7));
815
		g_ram_bank = 3;
816
		check(debug_bp_is_set(0x0801, 0),
817
		      "an unbanked address ignores a bank it was given");
818
		check(debug_bp_add(bp_at(0x0801, 0, 9)) == -1,
819
		      "the same unbanked address is one breakpoint whatever bank is named");
820
		check(debug_bp_remove(0x0801, 0, DEBUG_BANK_ANY),
821
		      "and it can be removed without naming that bank");
822
823
		// Watchpoints follow exactly the same rule.
824
		reset_all();
825
		debug_wp_add(0xA100, 1, DEBUG_BANK_ANY);
826
		g_ram_bank = 1;
827
		bool wany1 = debug_wp_check_write(0xA100, 0);
828
		g_ram_bank = 99;
829
		check(wany1 && debug_wp_check_write(0xA100, 0),
830
		      "a bankless watchpoint fires in every bank");
831
832
		reset_all();
833
		debug_wp_add(0x0400, 1, 7);
834
		g_ram_bank = 2;
835
		check(debug_wp_check_write(0x0400, 0),
836
		      "an unbanked watch address ignores a bank it was given");
837
838
		// And the ROM window is watched by ROM bank, not RAM bank.
839
		reset_all();
840
		debug_wp_add(0xC100, 1, 3);
841
		g_rom_bank = 3;
842
		g_ram_bank = 9;
843
		check(debug_wp_check_write(0xC100, 0), "the ROM window follows the ROM bank");
844
		g_rom_bank = 4;
845
		check(!debug_wp_check_write(0xC100, 0), "and not a different ROM bank");
846
847
		// The same address can be watched in two banks at once -- the whole
848
		// reason the selector is part of a watchpoint's identity. Keying on the
849
		// address alone silently kept only the first.
850
		reset_all();
851
		check(debug_wp_add(0xA100, 1, 5) == 0, "watches an address in bank 5");
852
		check(debug_wp_add(0xA100, 1, 6) == 1, "watches the same address in bank 6");
853
		check(debug_wp_count() == 2, "keeps both");
854
		g_ram_bank = 5;
855
		bool w5 = debug_wp_check_write(0xA100, 0);
856
		g_ram_bank = 6;
857
		check(w5 && debug_wp_check_write(0xA100, 0), "each fires in its own bank");
858
		g_ram_bank = 7;
859
		check(!debug_wp_check_write(0xA100, 0), "and neither fires in a third");
860
861
		// Removing one leaves the other, and names which one.
862
		check(debug_wp_remove(0xA100, 5) && debug_wp_count() == 1,
863
		      "removes only the bank it names");
864
		g_ram_bank = 6;
865
		check(debug_wp_check_write(0xA100, 0), "the other bank's watchpoint survives");
866
867
		// A value filter belongs to one of them, not to the address.
868
		reset_all();
869
		debug_wp_add(0xA100, 1, 5);
870
		debug_wp_add(0xA100, 1, 6);
871
		check(debug_wp_set_value(0xA100, 5, BPCMP_EQ, 0x11),
872
		      "attaches a filter to one bank's watchpoint");
873
		g_ram_bank = 5;
874
		check(!debug_wp_check_write(0xA100, 0x22), "that bank honours its filter");
875
		g_ram_bank = 6;
876
		check(debug_wp_check_write(0xA100, 0x22), "the other bank is unaffected");
877
	}
878
879
	// ── Every condition operand, in every CPU mode ──────────────────────────
880
	// A/X/Y are 8 or 16 bits depending on the CPU and the M/X status bits;
881
	// SP and a memory word are always 16; P and a memory byte always 8. The
882
	// comparison has to agree with whatever the CPU is actually doing, in all
883
	// four configurations the emulator can be in.
884
	{
885
		const cpu_mode_t modes[] = { CPU_65C02, CPU_816_EMU, CPU_816_NATIVE_8,
886
			                         CPU_816_NATIVE_16 };
887
		bool low_ok = true, wide_ok = true, narrow_ok = true;
888
889
		for (unsigned mi = 0; mi < sizeof(modes) / sizeof(modes[0]); mi++) {
890
			const cpu_mode_t m    = modes[mi];
891
			const bool       wide = mode_is_16bit(m);
892
893
			// -- A, X and Y, compared on their low byte. Always works.
894
			const struct { int operand; const char *name; } regs8[] = {
895
				{ BPOPERAND_A, "A" }, { BPOPERAND_X, "X" }, { BPOPERAND_Y, "Y" },
896
			};
897
			for (unsigned ri = 0; ri < 3; ri++) {
898
				reset_all();
899
				set_cpu_mode(m);
900
				debug_bp_add(bp_at(0x8000, 0, DEBUG_BANK_ANY));
901
				regs.c = regs.x = regs.y = 0x0042;
902
				debug_bp_set_condition(0x8000, 0, DEBUG_BANK_ANY, regs8[ri].operand, 0,
903
				                       BPCMP_EQ, 0x42);
904
				if (!debug_bp_on_arrival(0x8000, 0)) {
905
					printf("      (%s == $42 failed on %s)\n", regs8[ri].name, cpu_mode_name(m));
906
					low_ok = false;
907
				}
908
				debug_bp_set_condition(0x8000, 0, DEBUG_BANK_ANY, regs8[ri].operand, 0,
909
				                       BPCMP_NE, 0x42);
910
				if (debug_bp_on_arrival(0x8000, 0)) {
911
					printf("      (%s != $42 wrongly fired on %s)\n", regs8[ri].name,
912
					       cpu_mode_name(m));
913
					low_ok = false;
914
				}
915
916
				// -- The high byte. Only visible where the register is 16-bit,
917
				// and a 16-bit value must never match an 8-bit register.
918
				reset_all();
919
				set_cpu_mode(m);
920
				debug_bp_add(bp_at(0x8000, 0, DEBUG_BANK_ANY));
921
				regs.c = regs.x = regs.y = 0x1234;
922
				debug_bp_set_condition(0x8000, 0, DEBUG_BANK_ANY, regs8[ri].operand, 0,
923
				                       BPCMP_EQ, 0x1234);
924
				bool matched16 = debug_bp_on_arrival(0x8000, 0);
925
				if (matched16 != wide) {
926
					printf("      (%s == $1234 %s on %s)\n", regs8[ri].name,
927
					       matched16 ? "wrongly matched" : "did not match", cpu_mode_name(m));
928
					wide_ok = false;
929
				}
930
931
				// The low byte alone must satisfy an 8-bit register and must
932
				// NOT satisfy a 16-bit one.
933
				debug_bp_set_condition(0x8000, 0, DEBUG_BANK_ANY, regs8[ri].operand, 0,
934
				                       BPCMP_EQ, 0x34);
935
				bool matched8 = debug_bp_on_arrival(0x8000, 0);
936
				if (matched8 == wide) {
937
					printf("      (%s == $34 with A=$1234 %s on %s)\n", regs8[ri].name,
938
					       matched8 ? "wrongly matched" : "did not match", cpu_mode_name(m));
939
					narrow_ok = false;
940
				}
941
			}
942
		}
943
		check(low_ok, "A, X and Y compare their low byte in every CPU mode");
944
		check(wide_ok, "A, X and Y compare 16 bits only where the CPU is 16-bit");
945
		check(narrow_ok, "an 8-bit register is not matched by a 16-bit value, and vice versa");
946
947
		// -- SP, P, and memory operands are the same width in every mode.
948
		bool fixed_ok = true;
949
		for (unsigned mi = 0; mi < sizeof(modes) / sizeof(modes[0]); mi++) {
950
			const cpu_mode_t m = modes[mi];
951
			reset_all();
952
			set_cpu_mode(m);
953
			debug_bp_add(bp_at(0x8000, 0, DEBUG_BANK_ANY));
954
955
			regs.sp = 0x01F0;
956
			debug_bp_set_condition(0x8000, 0, DEBUG_BANK_ANY, BPOPERAND_SP, 0, BPCMP_EQ, 0x01F0);
957
			if (!debug_bp_on_arrival(0x8000, 0)) { printf("      (SP full on %s)\n", cpu_mode_name(m)); fixed_ok = false; }
958
			debug_bp_set_condition(0x8000, 0, DEBUG_BANK_ANY, BPOPERAND_SP, 0, BPCMP_EQ, 0x00F0);
959
			if (debug_bp_on_arrival(0x8000, 0)) { printf("      (SP truncated on %s)\n", cpu_mode_name(m)); fixed_ok = false; }
960
961
			// P is 8 bits wide on every CPU. Set it last: it is the mode.
962
			uint8_t saved = regs.status;
963
			debug_bp_set_condition(0x8000, 0, DEBUG_BANK_ANY, BPOPERAND_P, 0, BPCMP_EQ, saved);
964
			if (!debug_bp_on_arrival(0x8000, 0)) { printf("      (P on %s)\n", cpu_mode_name(m)); fixed_ok = false; }
965
966
			g_mem[0x1234] = 0x99;
967
			g_mem[0x1235] = 0xAB;
968
			debug_bp_set_condition(0x8000, 0, DEBUG_BANK_ANY, BPOPERAND_BYTE, 0x1234, BPCMP_EQ, 0x99);
969
			if (!debug_bp_on_arrival(0x8000, 0)) { printf("      (BYTE on %s)\n", cpu_mode_name(m)); fixed_ok = false; }
970
			debug_bp_set_condition(0x8000, 0, DEBUG_BANK_ANY, BPOPERAND_WORD, 0x1234, BPCMP_EQ, 0xAB99);
971
			if (!debug_bp_on_arrival(0x8000, 0)) { printf("      (WORD on %s)\n", cpu_mode_name(m)); fixed_ok = false; }
972
			debug_bp_set_condition(0x8000, 0, DEBUG_BANK_ANY, BPOPERAND_WORD, 0x1234, BPCMP_EQ, 0x99AB);
973
			if (debug_bp_on_arrival(0x8000, 0)) { printf("      (WORD byte order on %s)\n", cpu_mode_name(m)); fixed_ok = false; }
974
		}
975
		check(fixed_ok, "SP, P, and memory operands keep their width in every CPU mode");
976
977
		reset_all();
978
	}
979
980
981
	// ── PC matching across banks and machine types ──────────────────────────
982
	// A breakpoint's address is (pc, program bank, bank selector). Which of
983
	// those actually select memory depends on the machine, so the same
984
	// breakpoint has to behave correctly on a stock X16, on one with a 65C816,
985
	// and on a Gen2 where a non-zero program bank maps its own flat 64K.
986
	{
987
		// -- Unbanked low memory: neither bank means anything.
988
		reset_all();
989
		// Named with a specific bank, which is the only way this proves
990
		// anything: an ANY selector matches before the bank rule is consulted,
991
		// so it would pass even under a rule that wrongly called low memory
992
		// banked. A bank named here has to be normalised away on insert.
993
		debug_bp_add(bp_at(0x0801, 0, 7));
994
		bool unbanked_ok = true;
995
		for (int b = 0; b < 3; b++) {
996
			g_ram_bank = (uint8_t)(b * 7);
997
			g_rom_bank = (uint8_t)(b * 3);
998
			if (!debug_bp_is_set(0x0801, 0))
999
				unbanked_ok = false;
1000
		}
1001
		check(unbanked_ok, "an unbanked address matches whatever banks are mapped");
1002
1003
		// And the rule itself agrees, asked directly.
1004
		check(debug_bank_selector_matches(7, 0x0801, 0),
1005
		      "a bank named for low memory does not filter anything out");
1006
1007
		// -- RAM window, every bank a stock X16 can have.
1008
		reset_all();
1009
		debug_bp_add(bp_at(0xA000, 0, 5));
1010
		bool ram_exact_ok = true;
1011
		for (int b = 0; b < 256; b++) {
1012
			g_ram_bank = (uint8_t)b;
1013
			if (debug_bp_is_set(0xA000, 0) != (b == 5))
1014
				ram_exact_ok = false;
1015
		}
1016
		check(ram_exact_ok, "a RAM-bank breakpoint matches exactly one of all 256 banks");
1017
1018
		reset_all();
1019
		debug_bp_add(bp_at(0xA000, 0, DEBUG_BANK_ANY));
1020
		bool ram_any_ok = true;
1021
		for (int b = 0; b < 256; b++) {
1022
			g_ram_bank = (uint8_t)b;
1023
			if (!debug_bp_is_set(0xA000, 0))
1024
				ram_any_ok = false;
1025
		}
1026
		check(ram_any_ok, "a bankless RAM breakpoint matches all 256 banks");
1027
1028
		// -- ROM window, every bank the machine has.
1029
		reset_all();
1030
		debug_bp_add(bp_at(0xE000, 0, 7));
1031
		bool rom_exact_ok = true;
1032
		for (int b = 0; b < 32; b++) {
1033
			g_rom_bank = (uint8_t)b;
1034
			if (debug_bp_is_set(0xE000, 0) != (b == 7))
1035
				rom_exact_ok = false;
1036
		}
1037
		check(rom_exact_ok, "a ROM-bank breakpoint matches exactly one of all 32 banks");
1038
1039
		reset_all();
1040
		debug_bp_add(bp_at(0xE000, 0, DEBUG_BANK_ANY));
1041
		bool rom_any_ok = true;
1042
		for (int b = 0; b < 32; b++) {
1043
			g_rom_bank = (uint8_t)b;
1044
			if (!debug_bp_is_set(0xE000, 0))
1045
				rom_any_ok = false;
1046
		}
1047
		check(rom_any_ok, "a bankless ROM breakpoint matches all 32 banks");
1048
1049
		// -- The RAM bank must not be consulted for a ROM address, or vice
1050
		// versa: the two windows have independent bank registers.
1051
		reset_all();
1052
		debug_bp_add(bp_at(0xA000, 0, 4));
1053
		debug_bp_add(bp_at(0xE000, 0, 4));
1054
		g_ram_bank = 4;
1055
		g_rom_bank = 9;
1056
		bool ram_hit = debug_bp_is_set(0xA000, 0), rom_miss = debug_bp_is_set(0xE000, 0);
1057
		g_ram_bank = 9;
1058
		g_rom_bank = 4;
1059
		check(ram_hit && !rom_miss && !debug_bp_is_set(0xA000, 0) && debug_bp_is_set(0xE000, 0),
1060
		      "the two windows use their own bank registers");
1061
1062
		// -- Gen1 with a 65C816: the program bank selects nothing, so the RAM
1063
		// window still applies at $A000 even from program bank 1.
1064
		reset_all();
1065
		is_gen2 = false;
1066
		debug_bp_add(bp_at(0xA000, 1, 5));
1067
		g_ram_bank = 5;
1068
		bool g1_hit = debug_bp_is_set(0xA000, 1);
1069
		g_ram_bank = 6;
1070
		check(g1_hit && !debug_bp_is_set(0xA000, 1),
1071
		      "on a gen1 65C816 the RAM window still applies inside a program bank");
1072
1073
		// -- Gen2 (GS): a non-zero program bank is flat RAM, so the window
1074
		// registers select nothing and must not filter the breakpoint out.
1075
		reset_all();
1076
		is_gen2 = true;
1077
		// Added with a SPECIFIC bank, not ANY. An ANY selector short-circuits
1078
		// before the bank rule is ever consulted, so it passes whatever that
1079
		// rule does -- which is how the only direct check of this case used to
1080
		// pass with the very bug it was written for.
1081
		debug_bp_add(bp_at(0xA000, 1, 5));
1082
		bool gs_ok = true;
1083
		for (int b = 0; b < 8; b++) {
1084
			g_ram_bank = (uint8_t)b;
1085
			if (!debug_bp_is_set(0xA000, 1))
1086
				gs_ok = false;
1087
		}
1088
		check(gs_ok, "in a Gen2 program bank the RAM bank does not apply");
1089
1090
		// The shared entry point, called directly. hitBreakpoint() in the
1091
		// debugger reaches the rule through this and nothing else did.
1092
		check(debug_bank_selector_matches(5, 0xA000, 1),
1093
		      "a named bank does not filter inside a Gen2 program bank");
1094
		g_ram_bank = 5;
1095
		is_gen2 = false;
1096
		check(debug_bank_selector_matches(5, 0xA000, 1),
1097
		      "but on gen1 the window still applies inside a program bank");
1098
		g_ram_bank = 6;
1099
		check(!debug_bank_selector_matches(5, 0xA000, 1),
1100
		      "and there it filters on the mapped bank");
1101
		is_gen2 = true;
1102
1103
		// And a bank named for such an address is normalised away rather than
1104
		// silently making a second, unreachable breakpoint.
1105
		check(debug_bp_add(bp_at(0xA000, 1, 3)) == -1,
1106
		      "naming a bank inside a Gen2 program bank does not make a new breakpoint");
1107
1108
		// -- Program bank 0 on a Gen2 is still the ordinary windowed map.
1109
		reset_all();
1110
		is_gen2 = true;
1111
		debug_bp_add(bp_at(0xA000, 0, 5));
1112
		g_ram_bank = 5;
1113
		bool gs0_hit = debug_bp_is_set(0xA000, 0);
1114
		g_ram_bank = 6;
1115
		check(gs0_hit && !debug_bp_is_set(0xA000, 0),
1116
		      "program bank 0 on a Gen2 is banked as usual");
1117
1118
		// -- The program bank is part of the address: the same pc in two
1119
		// program banks is two different breakpoints.
1120
		reset_all();
1121
		is_gen2 = true;
1122
		check(debug_bp_add(bp_at(0x8000, 0, DEBUG_BANK_ANY)) == 0
1123
		          && debug_bp_add(bp_at(0x8000, 1, DEBUG_BANK_ANY)) == 1,
1124
		      "the same address in two program banks is two breakpoints");
1125
		check(debug_bp_is_set(0x8000, 0) && debug_bp_is_set(0x8000, 1)
1126
		          && !debug_bp_is_set(0x8000, 2),
1127
		      "each program bank matches only itself");
1128
1129
		reset_all();
1130
	}
1131
1132
1133
	// ── Watchpoints, the same matrix ────────────────────────────────────────
1134
	{
1135
		reset_all();
1136
		debug_wp_add(0xA100, 1, 5);
1137
		bool wp_ram_ok = true;
1138
		for (int b = 0; b < 256; b++) {
1139
			g_ram_bank = (uint8_t)b;
1140
			if (debug_wp_check_write(0xA100, 0) != (b == 5))
1141
				wp_ram_ok = false;
1142
		}
1143
		check(wp_ram_ok, "a RAM-bank watchpoint fires in exactly one of all 256 banks");
1144
1145
		reset_all();
1146
		debug_wp_add(0xA100, 1, DEBUG_BANK_ANY);
1147
		bool wp_any_ok = true;
1148
		for (int b = 0; b < 256; b++) {
1149
			g_ram_bank = (uint8_t)b;
1150
			if (!debug_wp_check_write(0xA100, 0))
1151
				wp_any_ok = false;
1152
		}
1153
		check(wp_any_ok, "a bankless watchpoint fires in all 256 banks");
1154
1155
		reset_all();
1156
		debug_wp_add(0xE000, 1, 7);
1157
		bool wp_rom_ok = true;
1158
		for (int b = 0; b < 32; b++) {
1159
			g_rom_bank = (uint8_t)b;
1160
			if (debug_wp_check_write(0xE000, 0) != (b == 7))
1161
				wp_rom_ok = false;
1162
		}
1163
		check(wp_rom_ok, "a ROM-window watchpoint fires in exactly one of all 32 banks");
1164
1165
		// A watchpoint address is only ever bank 0, so a Gen2 program bank
1166
		// cannot change what it means -- but the lookup still has to normalise
1167
		// consistently, or removal by a named bank would miss.
1168
		reset_all();
1169
		debug_wp_add(0x0400, 1, 7);       // bank named for an unbanked address
1170
		check(debug_wp_remove(0x0400, DEBUG_BANK_ANY),
1171
		      "an unbanked watchpoint can be removed without naming a bank");
1172
		reset_all();
1173
		debug_wp_add(0x0400, 1, DEBUG_BANK_ANY);
1174
		check(debug_wp_remove(0x0400, 7),
1175
		      "and can be removed while naming one, since it means nothing there");
1176
1177
		// A watchpoint's bank means one thing for the whole range, judged on
1178
		// where the range starts. The selector's meaning changes at $C000 (RAM
1179
		// bank below, ROM bank above), so a range spanning that line would
1180
		// otherwise have its halves tested against unrelated registers.
1181
		reset_all();
1182
		debug_wp_add(0xBFFF, 2, 5);
1183
		g_ram_bank = 5;
1184
		g_rom_bank = 9;                   // deliberately not 5
1185
		bool lo_hit = debug_wp_check_write(0xBFFF, 0);
1186
		check(lo_hit && debug_wp_check_write(0xC000, 0),
1187
		      "a range crossing $C000 keeps its bank meaning throughout");
1188
		g_ram_bank = 6;
1189
		check(!debug_wp_check_write(0xBFFF, 0) && !debug_wp_check_write(0xC000, 0),
1190
		      "and the whole range stops matching when that bank is unmapped");
1191
1192
		reset_all();
1193
	}
1194
1195
	// ── Ownership ───────────────────────────────────────────────────────────
1196
	// The table used to record only that an address was wanted, never by whom,
1197
	// so the first remove disarmed it for everyone. Five independent things can
1198
	// want one address -- -bp, the debugger's F9, and the three kinds of DAP
1199
	// breakpoint -- and the DAP server spent eight review rounds trying to
1200
	// reconstruct from outside what only the table can know. Every case here is
1201
	// a defect that was actually found during those rounds.
1202
	{
1203
		// -- Two owners, one address. The first to let go must not disarm the
1204
		// other. (Round 4: teardown deleted -bp and F9 breakpoints outright.)
1205
		reset_all();
1206
		check(debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_CLI) == DEBUG_ADD_CREATED,
1207
		      "the first owner creates the entry");
1208
		check(debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_SOURCE) == DEBUG_ADD_EXISTED,
1209
		      "the second owner joins the entry that is already there");
1210
		check(numBreakpoints == 1, "two owners at one address are still one place to stop");
1211
1212
		check(debug_bp_remove_for(0x2000, 0, -1, DEBUG_OWNER_CLI),
1213
		      "the first owner can let go");
1214
		check(numBreakpoints == 1 && debug_bp_is_set(0x2000, 0),
1215
		      "and the breakpoint stays armed for the owner that still wants it");
1216
		check(debug_bp_on_arrival(0x2000, 0), "and still actually fires");
1217
1218
		// -- ...and when the last owner goes, so does the entry. (Round 5: the
1219
		// orphan -- armed, with no table naming it, unreachable by any request.
1220
		// On a headless -debugport run that is a machine that cannot resume.)
1221
		check(debug_bp_remove_for(0x2000, 0, -1, DEBUG_OWNER_DAP_SOURCE),
1222
		      "the last owner can let go too");
1223
		check(numBreakpoints == 0 && !debug_bp_is_set(0x2000, 0),
1224
		      "and then nothing is left armed");
1225
1226
		// -- Letting go of something you never asked for changes nothing.
1227
		reset_all();
1228
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_CLI);
1229
		check(!debug_bp_remove_for(0x2000, 0, -1, DEBUG_OWNER_DAP_SOURCE),
1230
		      "an owner that never asked cannot remove");
1231
		check(numBreakpoints == 1, "and the entry is untouched");
1232
1233
		// -- Asking twice as the same owner is idempotent, and one clear
1234
		// retires it. Two source paths with the same basename resolved to one
1235
		// address and the two records vetoed each other's removal, leaving it
1236
		// armed. (Round 8: the mutual veto.)
1237
		reset_all();
1238
		debug_bp_add_for(bp_at(0x3000, 0, -1), DEBUG_OWNER_DAP_SOURCE);
1239
		check(debug_bp_add_for(bp_at(0x3000, 0, -1), DEBUG_OWNER_DAP_SOURCE) == DEBUG_ADD_EXISTED,
1240
		      "one owner asking twice is idempotent");
1241
		check(debug_bp_clear_owner(DEBUG_OWNER_DAP_SOURCE) == 1,
1242
		      "and one clear retires it exactly once");
1243
		check(numBreakpoints == 0, "leaving nothing armed");
1244
1245
		// -- Clearing an owner that holds nothing is a no-op.
1246
		reset_all();
1247
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_CLI);
1248
		check(debug_bp_clear_owner(DEBUG_OWNER_DAP_FUNCTION) == 0,
1249
		      "clearing an owner that holds nothing disarms nothing");
1250
		check(numBreakpoints == 1, "and leaves the table alone");
1251
1252
		// -- A -bp breakpoint survives a DAP session, with its condition and
1253
		// hit count intact. (Rounds 5 and 6: the flag that was meant to protect
1254
		// it either orphaned it or handed it to the wrong table.)
1255
		reset_all();
1256
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_CLI);
1257
		debug_bp_set_condition(0x2000, 0, -1, BPOPERAND_A, 0, BPCMP_EQ, 5);
1258
		regs.c = 5;
1259
		debug_bp_on_arrival(0x2000, 0);
1260
		debug_bp_on_arrival(0x2000, 0);
1261
		check(debug_bp_get_hits(0x2000, 0, -1) == 2, "the -bp breakpoint has been hit twice");
1262
1263
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_SOURCE);
1264
		check(debug_bp_clear_owner(DEBUG_OWNER_DAP_SOURCE) == 0,
1265
		      "a DAP session ending disarms nothing another owner still wants");
1266
		check(debug_bp_is_set(0x2000, 0), "the -bp breakpoint is still armed");
1267
1268
		int      has_cond = 0, operand = 0, op = 0;
1269
		uint32_t value = 0, ignore = 0;
1270
		uint16_t operand_addr = 0;
1271
		check(debug_bp_get_condition(0x2000, 0, -1, &has_cond, &operand, &operand_addr, &op,
1272
		                             &value, &ignore)
1273
		          && has_cond && operand == BPOPERAND_A && op == BPCMP_EQ && value == 5,
1274
		      "with its condition intact");
1275
		check(debug_bp_get_hits(0x2000, 0, -1) == 2, "and its hit count intact");
1276
1277
		// -- A client re-sending its list keeps the count. VS Code re-sends a
1278
		// whole file's breakpoints on every edit, so this path runs constantly;
1279
		// a count that reset each time could not drive a hit condition.
1280
		reset_all();
1281
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_SOURCE);
1282
		debug_bp_on_arrival(0x2000, 0);
1283
		debug_bp_on_arrival(0x2000, 0);
1284
		debug_bp_on_arrival(0x2000, 0);
1285
		debug_bp_clear_owner(DEBUG_OWNER_DAP_SOURCE);
1286
		check(numBreakpoints == 0, "the client's breakpoint goes when the client drops it");
1287
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_SOURCE);
1288
		check(debug_bp_get_hits(0x2000, 0, -1) == 3,
1289
		      "but re-sending the same breakpoint keeps its hit count");
1290
1291
		// -- The stale-registry pair. The server cached a per-address verdict
1292
		// and nothing invalidated it, so it went wrong in both directions.
1293
		//
1294
		// Direction 1: a client set and cleared an address, the user then set
1295
		// their own there, and teardown deleted the user's. (Round 7.)
1296
		reset_all();
1297
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_SOURCE);
1298
		debug_bp_clear_owner(DEBUG_OWNER_DAP_SOURCE);
1299
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_UI);   // the user's F9
1300
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_SOURCE);
1301
		debug_bp_clear_owner(DEBUG_OWNER_DAP_SOURCE);             // session teardown
1302
		check(numBreakpoints == 1 && debug_bp_is_set(0x2000, 0),
1303
		      "a client session ending leaves the user's F9 breakpoint armed");
1304
		check(debug_bp_has_owner(0x2000, 0, -1, DEBUG_OWNER_UI)
1305
		          && !debug_bp_has_owner(0x2000, 0, -1, DEBUG_OWNER_DAP_SOURCE),
1306
		      "and owned by the user alone");
1307
1308
		// Direction 2: a cached "someone else owns this" survived the user
1309
		// deleting their own, leaving a server-created entry armed and orphaned.
1310
		reset_all();
1311
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_CLI);
1312
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_SOURCE);
1313
		debug_bp_remove_for(0x2000, 0, -1, DEBUG_OWNER_DAP_SOURCE);
1314
		debug_bp_remove_for(0x2000, 0, -1, DEBUG_OWNER_CLI);      // the user deletes theirs
1315
		check(numBreakpoints == 0, "both owners letting go clears the address");
1316
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_SOURCE);
1317
		debug_bp_clear_owner(DEBUG_OWNER_DAP_SOURCE);
1318
		check(numBreakpoints == 0,
1319
		      "and a later client breakpoint there is not mistaken for the user's");
1320
1321
		// -- The human's delete is authoritative. Under a strict refcount F9 on
1322
		// a client's breakpoint would only drop the UI's own reference and
1323
		// leave it armed, so the user would press the key and watch nothing
1324
		// happen. Whoever asked for it, the keyboard wins.
1325
		reset_all();
1326
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_SOURCE);
1327
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_FUNCTION);
1328
		check(debug_bp_delete(0x2000, 0, -1), "the user can delete a client's breakpoint");
1329
		check(numBreakpoints == 0 && !debug_bp_is_set(0x2000, 0),
1330
		      "and it is really gone, not merely down one owner");
1331
1332
		// F9 over an address nobody has claimed creates one owned by the UI;
1333
		// pressing it again takes it away, exactly as it did before ownership.
1334
		reset_all();
1335
		debug_bp_toggle_for(0x2000, 0, -1, DEBUG_OWNER_UI);
1336
		check(debug_bp_is_set(0x2000, 0) && debug_bp_has_owner(0x2000, 0, -1, DEBUG_OWNER_UI),
1337
		      "F9 on a clear address arms one for the user");
1338
		debug_bp_toggle_for(0x2000, 0, -1, DEBUG_OWNER_UI);
1339
		check(!debug_bp_is_set(0x2000, 0), "and F9 again takes it away");
1340
1341
		// -- Ownership keys are normalised like every other key. A bank named
1342
		// for an address that is not banked must not produce an entry that the
1343
		// owner can never name again. (This was its own defect, on gen2 with a
1344
		// non-zero program bank.)
1345
		reset_all();
1346
		debug_bp_add_for(bp_at(0x0801, 0, 7), DEBUG_OWNER_DAP_SOURCE);
1347
		check(debug_bp_has_owner(0x0801, 0, DEBUG_BANK_ANY, DEBUG_OWNER_DAP_SOURCE),
1348
		      "an owner recorded with a meaningless bank is found without one");
1349
		check(debug_bp_remove_for(0x0801, 0, DEBUG_BANK_ANY, DEBUG_OWNER_DAP_SOURCE),
1350
		      "and can let go without naming it");
1351
		check(numBreakpoints == 0, "leaving nothing behind");
1352
1353
		// -- The "restate what I still want" pattern the DAP source handler
1354
		// uses. Source files are matched to addresses by basename, so two paths
1355
		// can resolve to one entry and one owner's two claims are a single bit;
1356
		// removing per entry would disarm an address the owner still wants.
1357
		// Clearing the owner and re-asserting the survivors is safe instead.
1358
		reset_all();
1359
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_CLI);        // the user's
1360
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_SOURCE); // two paths,
1361
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_SOURCE); // one address
1362
		debug_bp_add_for(bp_at(0x2100, 0, -1), DEBUG_OWNER_DAP_SOURCE); // a third, elsewhere
1363
		debug_bp_set_condition(0x2000, 0, -1, BPOPERAND_A, 0, BPCMP_EQ, 5);
1364
		regs.c = 5;
1365
		debug_bp_on_arrival(0x2000, 0);
1366
1367
		debug_bp_clear_owner(DEBUG_OWNER_DAP_SOURCE);
1368
		check(debug_bp_is_set(0x2000, 0),
1369
		      "clearing the owner leaves an address another owner still wants");
1370
		check(!debug_bp_is_set(0x2100, 0), "and releases the one only it wanted");
1371
1372
		// Re-assert only the surviving claim.
1373
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_SOURCE);
1374
		check(debug_bp_is_set(0x2000, 0) && numBreakpoints == 1,
1375
		      "re-asserting a claim restores it without duplicating the entry");
1376
		check(debug_bp_get_hits(0x2000, 0, -1) == 1,
1377
		      "and the hit count survives the round trip");
1378
		check(debug_bp_get_condition(0x2000, 0, -1, &has_cond, NULL, NULL, NULL, NULL, NULL)
1379
		          && has_cond,
1380
		      "as does the condition, which belongs to the address");
1381
1382
		reset_all();
1383
	}
1384
1385
	// ── Enable and disable ──────────────────────────────────────────────────
1386
	// Front ends used to implement this by deleting the entry and remembering
1387
	// it themselves, which loses the condition, the count, and every other
1388
	// view's marker. A disabled breakpoint keeps its place.
1389
	{
1390
		reset_all();
1391
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_UI);
1392
		debug_bp_on_arrival(0x2000, 0);
1393
		debug_bp_on_arrival(0x2000, 0);
1394
1395
		check(debug_bp_set_enabled(0x2000, 0, -1, false), "a breakpoint can be disabled");
1396
		check(numBreakpoints == 1, "and keeps its place in the table");
1397
		check(!debug_bp_is_set(0x2000, 0), "but no longer counts as set");
1398
		check(!debug_bp_on_arrival(0x2000, 0), "and does not stop the machine");
1399
		check(debug_bp_get_hits(0x2000, 0, -1) == 2,
1400
		      "arriving while disabled does not spend the count");
1401
		check(debug_bp_has_owner(0x2000, 0, -1, DEBUG_OWNER_UI),
1402
		      "and it still belongs to whoever asked for it");
1403
1404
		check(debug_bp_set_enabled(0x2000, 0, -1, true), "and can be enabled again");
1405
		check(debug_bp_on_arrival(0x2000, 0), "whereupon it fires again");
1406
		check(debug_bp_get_hits(0x2000, 0, -1) == 3, "carrying its count with it");
1407
1408
		// Asking for a breakpoint is asking for it to be armed, so a client
1409
		// re-sending one that is disabled gets an armed breakpoint back.
1410
		debug_bp_set_enabled(0x2000, 0, -1, false);
1411
		debug_bp_add_for(bp_at(0x2000, 0, -1), DEBUG_OWNER_DAP_SOURCE);
1412
		check(debug_bp_is_enabled(0x2000, 0, -1), "re-adding a disabled breakpoint re-arms it");
1413
1414
		check(!debug_bp_set_enabled(0x9999, 0, -1, false),
1415
		      "enabling something that is not there reports so");
1416
1417
		reset_all();
1418
	}
1419
1420
	// ── Watchpoint ownership ────────────────────────────────────────────────
1421
	// The same shape, and it had the same defect: -wp, the debugger and a DAP
1422
	// client can all want one address.
1423
	{
1424
		reset_all();
1425
		check(debug_wp_add_for(0xA100, 1, DEBUG_BANK_ANY, DEBUG_OWNER_CLI) == DEBUG_ADD_CREATED,
1426
		      "-wp creates the watchpoint");
1427
		check(debug_wp_add_for(0xA100, 1, DEBUG_BANK_ANY, DEBUG_OWNER_DAP_SOURCE)
1428
		          == DEBUG_ADD_EXISTED,
1429
		      "a data breakpoint joins it");
1430
		check(debug_wp_count() == 1, "two owners share one watch");
1431
1432
		debug_wp_set_value(0xA100, DEBUG_BANK_ANY, BPCMP_EQ, 0x42);
1433
		check(debug_wp_clear_owner(DEBUG_OWNER_DAP_SOURCE) == 0,
1434
		      "the client's session ending disarms nothing -wp still wants");
1435
		check(debug_wp_count() == 1, "the -wp watchpoint survives");
1436
		check(debug_wp_check_write(0xA100, 0x42) && !debug_wp_check_write(0xA100, 0x01),
1437
		      "with its value filter intact");
1438
1439
		check(debug_wp_remove_for(0xA100, DEBUG_BANK_ANY, DEBUG_OWNER_CLI),
1440
		      "and goes when its last owner does");
1441
		check(debug_wp_count() == 0, "leaving nothing watched");
1442
1443
		// The console's watch_clear is the human speaking, so it takes
1444
		// everything, exactly like the breakpoint delete above.
1445
		reset_all();
1446
		debug_wp_add_for(0xA100, 1, DEBUG_BANK_ANY, DEBUG_OWNER_CLI);
1447
		debug_wp_add_for(0xA100, 1, DEBUG_BANK_ANY, DEBUG_OWNER_DAP_CONSOLE);
1448
		check(debug_wp_delete(0xA100, DEBUG_BANK_ANY), "a delete takes the whole watch");
1449
		check(debug_wp_count() == 0, "whoever asked for it");
1450
1451
		// A full table has to be distinguishable from a duplicate. The server
1452
		// read one -1 as "already there" and would have armed nothing while
1453
		// believing it had.
1454
		reset_all();
1455
		bool filled = true;
1456
		for (int i = 0; i < MAX_WATCHPOINTS; i++) {
1457
			if (debug_wp_add_for((uint16_t)(0x1000 + i), 1, DEBUG_BANK_ANY, DEBUG_OWNER_CLI)
1458
			    != DEBUG_ADD_CREATED)
1459
				filled = false;
1460
		}
1461
		check(filled, "the watchpoint table takes its full complement");
1462
		check(debug_wp_add_for(0x2000, 1, DEBUG_BANK_ANY, DEBUG_OWNER_CLI) == DEBUG_ADD_FULL,
1463
		      "one more reports a full table");
1464
		check(debug_wp_add_for(0x1000, 1, DEBUG_BANK_ANY, DEBUG_OWNER_DAP_SOURCE)
1465
		          == DEBUG_ADD_EXISTED,
1466
		      "which is not the same answer as one that is already there");
1467
1468
		reset_all();
1469
	}
1470
1471
	// ── Address references: which field a bank byte lands in ────────────────
1472
	//
1473
	// The DAP instruction-breakpoint path had no X16 bank dimension. It read a
1474
	// reference as `pc | bank << 16` into the program-bank field and left the
1475
	// window selector at ANY, so a breakpoint in banked RAM was recorded against
1476
	// a program bank a gen1 never runs in: accepted, reported verified, and
1477
	// silently dead. These pin the interpretation, which is the part a caller
1478
	// cannot be trusted to re-derive.
1479
	{
1480
		reset_all();
1481
		is_gen2 = false;
1482
		struct breakpoint bp;
1483
1484
		check(debug_bp_from_ref(0xA555, DEBUG_BANK_ANY, 0, &bp)
1485
		          && bp.pc == 0xA555 && bp.bank == 0 && bp.x16Bank == DEBUG_BANK_ANY,
1486
		      "a bare address names no bank and matches whichever is mapped");
1487
1488
		// The regression: off a Gen2 the program bank is always zero, so the
1489
		// only bank a top byte can be talking about is the window's.
1490
		check(debug_bp_from_ref(0x33A555, DEBUG_BANK_ANY, 0, &bp)
1491
		          && bp.pc == 0xA555 && bp.bank == 0 && bp.x16Bank == 0x33,
1492
		      "a packed bank byte is the window bank on a gen1, not the program bank");
1493
1494
		// "bb:aaaa", the form -bp takes, has to arrive at the same entry.
1495
		struct breakpoint colon;
1496
		check(debug_bp_from_ref(0xA555, 0x33, 0, &colon)
1497
		          && colon.pc == bp.pc && colon.bank == bp.bank
1498
		          && colon.x16Bank == bp.x16Bank,
1499
		      "the bb:aaaa form and the packed form describe one breakpoint");
1500
1501
		// ...and it has to actually fire, which is what the old entry never did.
1502
		// Added through the real table and matched through the real rule.
1503
		check(debug_bp_add(bp) == 0, "the resolved breakpoint goes into the table");
1504
		g_ram_bank = 0x33;
1505
		bool armed = debug_bp_is_set(0xA555, 0);
1506
		g_ram_bank = 0x34;
1507
		check(armed && !debug_bp_is_set(0xA555, 0),
1508
		      "and fires in the bank it named, and only there");
1509
1510
		// What the old code built, stated directly: the window bank left in the
1511
		// program-bank field. A gen1 runs in program bank 0, so nothing matches.
1512
		reset_all();
1513
		is_gen2 = false;
1514
		debug_bp_add(bp_at(0xA555, 0x33, DEBUG_BANK_ANY));
1515
		bool dead = true;
1516
		for (int b = 0; b < 0x40; b++) {
1517
			g_ram_bank = (uint8_t)b;
1518
			if (debug_bp_is_set(0xA555, 0))
1519
				dead = false;
1520
		}
1521
		check(dead, "a window bank left in the program-bank field can never match");
1522
1523
		reset_all();
1524
		is_gen2 = false;
1525
1526
		// A bank selects nothing below $A000, so naming one there is a request
1527
		// that cannot be honoured rather than one to quietly widen to ANY.
1528
		check(!debug_bp_from_ref(0x330555, DEBUG_BANK_ANY, 0, &bp),
1529
		      "a bank packed onto an unbanked address is refused");
1530
		check(!debug_bp_from_ref(0x0555, 0x33, 0, &bp),
1531
		      "and so is one named alongside it");
1532
1533
		check(!debug_bp_from_ref(0x33A555, 0x34, 0, &bp),
1534
		      "a bank given twice, two different ways, is refused");
1535
1536
		check(!debug_bp_from_ref(-1, DEBUG_BANK_ANY, 0, &bp)
1537
		          && !debug_bp_from_ref(0x1000000, DEBUG_BANK_ANY, 0, &bp),
1538
		      "a reference outside 24 bits is refused");
1539
1540
		// The offset is a displacement within the bank; letting it carry would
1541
		// move the breakpoint to an address nobody named.
1542
		check(debug_bp_from_ref(0x33A555, DEBUG_BANK_ANY, 0x10, &bp)
1543
		          && bp.pc == 0xA565 && bp.x16Bank == 0x33,
1544
		      "an offset moves the address and leaves the bank alone");
1545
		check(debug_bp_from_ref(0xFFFF, DEBUG_BANK_ANY, 1, &bp) && bp.pc == 0x0000,
1546
		      "and wraps inside 16 bits rather than into the bank byte");
1547
1548
		// -- Gen2: there a top byte really is the program bank, which is what
1549
		// the disassembly and the stack frames hand out.
1550
		is_gen2 = true;
1551
		check(debug_bp_from_ref(0x01A555, DEBUG_BANK_ANY, 0, &bp)
1552
		          && bp.pc == 0xA555 && bp.bank == 1 && bp.x16Bank == DEBUG_BANK_ANY,
1553
		      "on a Gen2 a packed bank byte is the program bank");
1554
		check(debug_bp_from_ref(0xA555, 5, 0, &bp)
1555
		          && bp.pc == 0xA555 && bp.bank == 0 && bp.x16Bank == 5,
1556
		      "and program bank 0 there is still the windowed map");
1557
1558
		reset_all();
1559
		is_gen2 = true;
1560
	}
1561
1562
	debug_core_free();
1563
1564
	if (failures) {
1565
		printf("\nFAILED (%d failures)\n", failures);
1566
		return 1;
1567
	}
1568
	printf("\nPASSED (0 failures)\n");
1569
	return 0;
1570
}