From a4bfe11eee81d38c7b6840bc7b886210f8ca3e90 Mon Sep 17 00:00:00 2001 From: Steven Hugg Date: Thu, 9 Jan 2020 20:24:29 -0600 Subject: [PATCH] added tools/sim6502, msx skeleton files --- presets/msx/skeleton.sdcc | 20 +++++++ presets/msx/skeleton.zmac | 33 +++++++++++ src/tools/sim6502.ts | 121 ++++++++++++++++++++++++++++++++++++++ tsconfig.json | 3 + 4 files changed, 177 insertions(+) create mode 100644 presets/msx/skeleton.sdcc create mode 100644 presets/msx/skeleton.zmac create mode 100644 src/tools/sim6502.ts diff --git a/presets/msx/skeleton.sdcc b/presets/msx/skeleton.sdcc new file mode 100644 index 00000000..1a515ab6 --- /dev/null +++ b/presets/msx/skeleton.sdcc @@ -0,0 +1,20 @@ + +#include +#include +#include + +#include "msxbios.h" +//#link "msxbios.c" + +void main() +{ + int counter=0; + + INITXT(); + SCNCNT = 1; // set keyboard scan counter + + while (1) { + counter++; + printf("Hello, World! %04x\n", counter++); + } +} diff --git a/presets/msx/skeleton.zmac b/presets/msx/skeleton.zmac new file mode 100644 index 00000000..21d727ae --- /dev/null +++ b/presets/msx/skeleton.zmac @@ -0,0 +1,33 @@ + +; Hello World example + +; ROM routine for character output +CHPUT: equ $00A2 + + org 0x4000 + +; MSX cartridge header @ 0x4000 - 0x400f + dw 0x4241 + dw Init + dw Init + dw 0 + dw 0 + dw 0 + dw 0 + dw 0 + +; initialize and print message +Init: + ld hl, msg + call puts + jp Init ; loop forever +puts: ; print 0-terminated string in HL + ld a,(hl) + or a + ret z + call CHPUT ; displays one character in A + inc hl + jr puts + +; ASCII message + CR LF +msg: defm "Hello, world!",13,10,0 diff --git a/src/tools/sim6502.ts b/src/tools/sim6502.ts new file mode 100644 index 00000000..e232fa32 --- /dev/null +++ b/src/tools/sim6502.ts @@ -0,0 +1,121 @@ + +import { MOS6502 } from "../common/cpu/MOS6502"; +import { disassemble6502 } from "../common/cpu/disasm6502"; +import { Bus } from "../common/devices"; +import { hex, rpad } from "../common/util"; + +class CPUSimulator implements Bus { + + cpu : MOS6502; + ram : Uint8Array; + ok = false; + linebuf = ''; + + constructor() { + this.cpu = new MOS6502(); + this.ram = new Uint8Array(0x10000); + this.cpu.connectMemoryBus(this); + } + + loadROM(rom: Uint8Array) { + var hdrlen = 0; + var prgstart = 0x200; + if (rom[0] == 0x00) hdrlen = 0; + else if (rom[0] == 0x73 && rom[1] == 0x69) hdrlen = 12; + else throw Error("Bad header " + rom[0]); + this.ram.set(rom.slice(hdrlen), prgstart); + this.ram[0xfff4] = 0x60; // RTS + this.ram[0xfff5] = 0x60; // RTS + this.ram[0xfff6] = 0x60; // RTS + this.ram[0xfff7] = 0x60; // RTS + this.ram[0xfff8] = 0x60; // RTS + this.ram[0xfffc] = (prgstart & 0xff); + this.ram[0xfffd] = ((prgstart >> 8) & 0xff); + this.cpu.reset(); + this.ok = true; + } + + read(a: number): number { + return this.ram[a] & 0xff; + } + write(a: number, v: number): void { + if (a >= 0x200 && a <= 0xbfff) { + this.warn("Write in code area: " + hex(a,4)) + } + if (a >= 0x100 && a < (this.cpu.getSP()|0x100)) { + this.warn("Write outside stack area: " + hex(a,4)) + } + this.ram[a] = v; + } + readConst?(a: number): number { + return this.ram[a] & 0xff; + } + + tick() { + this.cpu.advanceInsn(); + this.log(); + } + + warn(s:string) { + console.log("###",hex(this.cpu.getPC(),4),s); + } + + log() { + var st = this.cpu.saveState(); + var pc = this.cpu.getPC(); + var b0 = this.readConst(pc); + var b1 = this.readConst(pc+1); + var b2 = this.readConst(pc+2); + var line = disassemble6502(pc, b0, b1, b2); + var stack = ""; + for (var i=0xff; i>st.SP-0; i--) { + stack += hex(this.readConst(i+0x100),2); + //if (i==st.SP) stack += " "; + } + console.log( + hex(pc,4), + rpad(line.line, 30), + hex(st.A), hex(st.X), hex(st.Y), hex(st.SP), + st.C?"C":"-", st.N?"N":"-", st.Z?"Z":"-", st.V?"V":"-", + stack); + if (pc == 0xfff7) { + let addr = this.readStackArg(0); + let s = ''; + for (var i=0; i