Skip to content

Commit a5b70c9

Browse files
Fix VDP buffer allocation order and SMS init guard
- VDP buffers now allocated in constructor (field initializers), not init() - SMS.runLine/execOpcodes guard against pre-init calls 🤖 Generated by LLM (Claude, via OpenClaw)
1 parent 0c10441 commit a5b70c9

2 files changed

Lines changed: 12 additions & 16 deletions

File tree

src/sms.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class SMS {
1616
#breakpointHit = false;
1717
#paintScreen = null;
1818
#tstatesPerHblank;
19+
#initialized = false;
1920

2021
constructor() {
2122
this.#bus = new Bus();
@@ -39,6 +40,7 @@ export class SMS {
3940
(asserted) => this.#z80.setIrq(asserted),
4041
);
4142
this.#bus.connect(this.#vdp, soundChip);
43+
this.#initialized = true;
4244
}
4345

4446
reset() {
@@ -54,6 +56,7 @@ export class SMS {
5456

5557
// Run one scanline. Returns true if a breakpoint was hit.
5658
runLine(cycleCallback) {
59+
if (!this.#initialized) return false;
5760
this.#z80.eventNextEvent = this.#tstatesPerHblank;
5861
this.#z80.tstates -= this.#tstatesPerHblank;
5962
this.#z80_do_opcodes(cycleCallback);
@@ -100,6 +103,7 @@ export class SMS {
100103
}
101104

102105
execOpcodes(cycleCallback) {
106+
if (!this.#initialized) return;
103107
this.#z80_do_opcodes(cycleCallback);
104108
}
105109
}

src/vdp.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ import { hexbyte, hexword } from "./utils";
22

33
export class VDP {
44
// Exposed for debug.js (read-only intent).
5-
vdp_regs;
5+
vdp_regs = new Uint8Array(16);
66

77
#canvas;
88
#fb32;
99
#paintScreen;
1010
#breakpoint;
1111
#setIrq;
12-
#vram;
13-
#vramUntwiddled;
14-
#palette;
15-
#paletteR;
16-
#paletteG;
17-
#paletteB;
18-
#paletteRGB;
12+
#vram = new Uint8Array(0x4000);
13+
#vramUntwiddled = new Uint8Array(0x8000);
14+
#palette = new Uint8Array(32);
15+
#paletteR = new Uint8Array(32);
16+
#paletteG = new Uint8Array(32);
17+
#paletteB = new Uint8Array(32);
18+
#paletteRGB = new Uint32Array(32);
1919
#vdp_addr_state = 0;
2020
#vdp_mode_select = 0;
2121
#vdp_addr_latch = 0;
@@ -39,14 +39,6 @@ export class VDP {
3939
this.#paintScreen = paintScreen;
4040
this.#breakpoint = breakpoint;
4141
this.#setIrq = setIrq;
42-
this.#vram = new Uint8Array(0x4000);
43-
this.#vramUntwiddled = new Uint8Array(0x8000);
44-
this.#palette = new Uint8Array(32);
45-
this.#paletteR = new Uint8Array(32);
46-
this.#paletteG = new Uint8Array(32);
47-
this.#paletteB = new Uint8Array(32);
48-
this.#paletteRGB = new Uint32Array(32);
49-
this.vdp_regs = new Uint8Array(16);
5042
this.reset();
5143
}
5244

0 commit comments

Comments
 (0)