Skip to content
Merged
22 changes: 1 addition & 21 deletions src/bus.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hexbyte, hexword } from "./utils";

class Bus {
export class Bus {
#ram = new Uint8Array(0x2000);
#cartridgeRam = new Uint8Array(0x8000);
#romBanks = [];
Expand Down Expand Up @@ -248,23 +248,3 @@ class Bus {
}
}
}

export const bus = new Bus();

// Convenience re-exports so callers that import bare functions (z80.js,
// z80_ops.js, z80_dis.js, debug.js) only need to change their import path.
export function readbyte(a) {
return bus.readbyte(a);
}
export function writebyte(a, v) {
bus.writebyte(a, v);
}
export function readport(a) {
return bus.readport(a);
}
export function writeport(a, v) {
bus.writeport(a, v);
}
export function virtualAddress(a) {
return bus.virtualAddress(a);
}
56 changes: 30 additions & 26 deletions src/debug.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { hexbyte, hexword } from "./utils";
import { bus, readbyte, virtualAddress } from "./bus";
import {
clearBreakpoint,
audio_enable,
cycleCallback,
start,
z80_do_opcodes,
} from "./miracle";
import { z80 } from "./z80/z80.js";
import { disassemble } from "./z80/z80_dis";
import { vdp } from "./vdp";
import { makeDisassembler } from "./z80/z80_dis";

// Module-level refs — set during debug_init from the SMS instance + callbacks.
let z80, bus, vdp;
let sms;
let disassemble;
let _audioEnable, _cycleCallback, _start;

let debugSerial = 0;
let annotations = null;

export function debug_init(romName) {
export function debug_init(romName, smsInstance, callbacks) {
sms = smsInstance;
z80 = sms.getZ80();
bus = sms.getBus();
vdp = sms.getVdp();
_audioEnable = callbacks.audioEnable;
_cycleCallback = callbacks.cycleCallback;
_start = callbacks.start;

const { disassemble: dis } = makeDisassembler(
(a) => bus.readbyte(a),
addressHtml,
);
disassemble = dis;

debugSerial = (bus.romBanks[1][0x3ffc] << 8) | bus.romBanks[1][0x3ffd];

if (!localStorage[debugSerial]) {
Expand Down Expand Up @@ -44,7 +54,7 @@ export function debug_init(romName) {
// }

function addressName(addr) {
const virtual = virtualAddress(addr);
const virtual = bus.virtualAddress(addr);
if (annotations.labels[virtual]) {
return (
'<span class="addr label" title="' +
Expand Down Expand Up @@ -78,7 +88,7 @@ function labelHtml(addr) {
// TODO(#18) reinstate
// function endLabelEdit(content) {
// var addr = this.getAttribute("title") || content.previous;
// var virtual = virtualAddress(parseInt(addr, 16));
// var virtual = bus.virtualAddress(parseInt(addr, 16));
// setLabel(virtual, content.current);
// }

Expand All @@ -92,18 +102,12 @@ function updateDisassembly(address) {
let hex = "";
for (let i = address; i < result[1]; ++i) {
if (hex !== "") hex += " ";
hex += hexbyte(readbyte(i));
hex += hexbyte(bus.readbyte(i));
}
child.querySelector(".dis_addr").innerHTML = labelHtml(address);
child.classList.toggle("current", address === z80.pc);
child.querySelector(".instr_bytes").textContent = hex;
child.querySelector(".disassembly").innerHTML = result[0];
// TODO(#18) re-enable once there's a vanilla-DOM solution:
// child.querySelector('.addr')
// .editable({editBy: 'dblclick', editClass: 'editable', onSubmit: endLabelEdit})
// .keypress(function (e) {
// if (e.which === 13) child.querySelector('.addr').blur();
// });
address = result[1];
}
}
Expand Down Expand Up @@ -190,16 +194,16 @@ function updateDebug(pcOrNone) {
}

export function stepUntil(f) {
audio_enable(true);
clearBreakpoint();
_audioEnable(true);
sms.clearBreakpoint();
for (let i = 0; i < 65536; i++) {
z80.tstates = 0;
z80.eventNextEvent = 1;
z80_do_opcodes(cycleCallback);
sms.execOpcodes(_cycleCallback);
if (f()) break;
}
showDebug(z80.pc);
audio_enable(false);
_audioEnable(false);
}

export function step() {
Expand Down Expand Up @@ -267,7 +271,7 @@ export function debugKeyPress(key) {
stepOut();
break;
case "g":
start();
_start();
break;
}
return true;
Expand Down
25 changes: 20 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { RomList } from "./roms";
import { z80_init } from "./z80/z80.js";
import { miracle_init, miracle_reset, start, stop } from "./miracle";
import { bus } from "./bus";
import {
sms,
miracle_init,
miracle_reset,
start,
stop,
audio_enable,
cycleCallback,
} from "./miracle";
import { step, stepOver, stepOut, debug_init } from "./debug";

function loadRomData(name) {
Expand All @@ -16,9 +23,17 @@ function loadRomData(name) {
return request.response;
}

function onRomLoaded(name) {
debug_init(name, sms, {
audioEnable: audio_enable,
cycleCallback: cycleCallback,
start: start,
});
}

function resetLoadAndStart(filename, romdata) {
miracle_reset();
bus.loadRom(filename, romdata, debug_init);
sms.loadRom(filename, romdata, onRomLoaded);
hideRomChooser();
start();
}
Expand Down Expand Up @@ -129,10 +144,10 @@ function go() {

const parsedQuery = parseQuery();
if (parsedQuery["b64sms"]) {
bus.loadRom("b64.sms", atob(parsedQuery["b64sms"]), debug_init);
sms.loadRom("b64.sms", atob(parsedQuery["b64sms"]), onRomLoaded);
} else {
const defaultRom = getDefaultRom();
bus.loadRom(defaultRom, loadRomData(defaultRom), debug_init);
sms.loadRom(defaultRom, loadRomData(defaultRom), onRomLoaded);
}

start();
Expand Down
Loading