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