1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-06-09 15:29:29 +00:00

refactored into BasicZ80ScanlinePlatform

This commit is contained in:
Steven Hugg 2018-12-02 10:13:59 -05:00
parent 6f9d9ecac1
commit 4e77088ca1
3 changed files with 165 additions and 106 deletions

6
package-lock.json generated
View File

@ -830,6 +830,12 @@
"integrity": "sha512-2qHaIQr2VLRFoxe2nASzsV6ef4yOOH+Fi9FBOVH6cqeSgUnoyySPZkxzLuzd+RYOQTRpROA0ztTMqxROKSb/nA==", "integrity": "sha512-2qHaIQr2VLRFoxe2nASzsV6ef4yOOH+Fi9FBOVH6cqeSgUnoyySPZkxzLuzd+RYOQTRpROA0ztTMqxROKSb/nA==",
"dev": true "dev": true
}, },
"pngjs": {
"version": "3.3.3",
"resolved": "https://registry.npmjs.org/pngjs/-/pngjs-3.3.3.tgz",
"integrity": "sha512-1n3Z4p3IOxArEs1VRXnZ/RXdfEniAUS9jb68g58FIXMNkPJeZd+Qh4Uq7/e0LVxAQGos1eIUrqrt4FpjdnEd+Q==",
"dev": true
},
"prelude-ls": { "prelude-ls": {
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz",

View File

@ -1,5 +1,5 @@
import { RAM, RasterVideo, dumpRAM } from "./emu"; import { RAM, RasterVideo, dumpRAM, AnimationTimer, setKeyboardFromMap, padBytes } from "./emu";
import { hex, printFlags, invertMap } from "./util"; import { hex, printFlags, invertMap } from "./util";
import { CodeAnalyzer } from "./analysis"; import { CodeAnalyzer } from "./analysis";
import { disassemble6502 } from "./cpu/disasm6502"; import { disassemble6502 } from "./cpu/disasm6502";
@ -196,6 +196,8 @@ export abstract class BaseDebugPlatform extends BasePlatform {
} }
} }
//////
////// 6502 ////// 6502
export function getToolForFilename_6502(fn:string) : string { export function getToolForFilename_6502(fn:string) : string {
@ -436,7 +438,7 @@ export abstract class BaseZ80Platform extends BaseDebugPlatform {
getSP() { return this._cpu.getSP(); } getSP() { return this._cpu.getSP(); }
// TODO: refactor other parts into here // TODO: refactor other parts into here
runCPU(cpu, cycles) { runCPU(cpu, cycles:number) {
this._cpu = cpu; // TODO? this._cpu = cpu; // TODO?
if (this.wasBreakpointHit()) if (this.wasBreakpointHit())
return 0; return 0;
@ -959,3 +961,116 @@ export function lookupSymbol(platform:Platform, addr:number) {
return foundsym || ""; return foundsym || "";
} }
///// Basic Platforms
export abstract class BasicZ80ScanlinePlatform extends BaseZ80Platform {
cpuFrequency : number;
canvasWidth : number;
numTotalScanlines : number;
numVisibleScanlines : number;
defaultROMSize : number;
cpuCyclesPerLine : number;
currentScanline : number;
startLineTstates : number;
cpu;
membus : MemoryBus;
iobus : MemoryBus;
ram = new Uint8Array(0);
rom = new Uint8Array(0);
video;
timer;
audio;
psg;
inputs = new Uint8Array(16);
mainElement : HTMLElement;
abstract newRAM() : Uint8Array;
abstract newMembus() : MemoryBus;
abstract newIOBus() : MemoryBus;
abstract getVideoOptions();
abstract getKeyboardMap();
abstract startScanline(sl : number);
abstract drawScanline(sl : number);
constructor(mainElement : HTMLElement) {
super();
this.mainElement = mainElement;
}
start() {
this.cpuCyclesPerLine = Math.round(this.cpuFrequency / 60 / this.numTotalScanlines);
this.ram = this.newRAM();
this.membus = this.newMembus();
this.iobus = this.newIOBus();
this.cpu = this.newCPU(this.membus, this.iobus);
this.video = new RasterVideo(this.mainElement, this.canvasWidth, this.numVisibleScanlines, this.getVideoOptions());
this.video.create();
setKeyboardFromMap(this.video, this.inputs, this.getKeyboardMap())
this.timer = new AnimationTimer(60, this.nextFrame.bind(this));
}
readAddress(addr) {
return this.membus.read(addr);
}
advance(novideo : boolean) {
var extraCycles = 0;
for (var sl=0; sl<this.numTotalScanlines; sl++) {
this.startLineTstates = this.cpu.getTstates();
this.currentScanline = sl;
this.startScanline(sl);
extraCycles = this.runCPU(this.cpu, this.cpuCyclesPerLine - extraCycles); // TODO: HALT opcode?
this.drawScanline(sl);
}
this.video.updateFrame();
}
loadROM(title, data) {
this.rom = padBytes(data, this.defaultROMSize);
this.reset();
}
loadState(state) {
this.cpu.loadState(state.c);
this.ram.set(state.b);
this.inputs.set(state.in);
}
saveState() {
return {
c:this.getCPUState(),
b:this.ram.slice(0),
in:this.inputs.slice(0),
};
}
loadControlsState(state) {
this.inputs.set(state.in);
}
saveControlsState() {
return {
in:this.inputs.slice(0)
};
}
getCPUState() {
return this.cpu.saveState();
}
isRunning() {
return this.timer && this.timer.isRunning();
}
pause() {
this.timer.stop();
if (this.audio) this.audio.stop();
}
resume() {
this.timer.start();
if (this.audio) this.audio.start();
}
reset() {
this.cpu.reset();
this.cpu.setTstates(0);
}
}

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
import { Platform, BaseMAMEPlatform, BaseZ80Platform, getToolForFilename_z80 } from "../baseplatform"; import { Platform, BasicZ80ScanlinePlatform } from "../baseplatform";
import { PLATFORMS, RAM, newAddressDecoder, padBytes, noise, setKeyboardFromMap, AnimationTimer, RasterVideo, Keys, makeKeycodeMap } from "../emu"; import { PLATFORMS, RAM, newAddressDecoder, padBytes, noise, setKeyboardFromMap, AnimationTimer, RasterVideo, Keys, makeKeycodeMap } from "../emu";
import { hex, lzgmini, stringToByteArray } from "../util"; import { hex, lzgmini, stringToByteArray } from "../util";
import { MasterAudio, SN76489_Audio } from "../audio"; import { MasterAudio, SN76489_Audio } from "../audio";
@ -48,50 +48,34 @@ var SG1000_KEYCODE_MAP = makeKeycodeMap([
[Keys.VK_1, 1, 0x10], [Keys.VK_1, 1, 0x10],
]); ]);
class SG1000Platform extends BaseZ80Platform { class SG1000Platform extends BasicZ80ScanlinePlatform implements Platform {
cpuFrequency = 3579545; // MHz cpuFrequency = 3579545; // MHz
canvasWidth = 304; canvasWidth = 304;
numTotalScanlines = 262; numTotalScanlines = 262;
numVisibleScanlines = 240; numVisibleScanlines = 240;
cpuCyclesPerLine; defaultROMSize = 0xc000;
cpu; vdp : TMS9918A;
ram : RAM;
membus;
iobus;
rom = new Uint8Array(0);
video;
vdp;
timer;
audio;
psg;
inputs = new Uint8Array(4);
mainElement : HTMLElement;
currentScanline : number;
startLineTstates : number;
constructor(mainElement : HTMLElement) {
super();
this.mainElement = mainElement;
}
getPresets() { return SG1000_PRESETS; } getPresets() { return SG1000_PRESETS; }
getKeyboardMap() { return SG1000_KEYCODE_MAP; }
getVideoOptions() { return {overscan:true}; }
newRAM() { newRAM() {
return new RAM(0x400); return new Uint8Array(0x400);
} }
newMembus() { newMembus() {
var ramSize = this.ram.mem.length;
return { return {
read: newAddressDecoder([ read: newAddressDecoder([
[0xc000, 0xffff, ramSize-1, (a) => { return this.ram.mem[a]; }], [0xc000, 0xffff, 0x3ff, (a) => { return this.ram[a]; }],
[0x0000, 0xbfff, 0xffff, (a) => { return this.rom[a]; }], [0x0000, 0xbfff, 0xffff, (a) => { return this.rom[a]; }],
]), ]),
write: newAddressDecoder([ write: newAddressDecoder([
[0xc000, 0xffff, ramSize-1, (a,v) => { this.ram.mem[a] = v; }], [0xc000, 0xffff, 0x3ff, (a,v) => { this.ram[a] = v; }],
]), ]),
isContended: () => { return false; }, isContended: () => { return false; },
}; };
@ -138,13 +122,7 @@ class SG1000Platform extends BaseZ80Platform {
} }
start() { start() {
this.cpuCyclesPerLine = Math.round(this.cpuFrequency / 60 / this.numTotalScanlines); super.start();
this.ram = this.newRAM();
this.membus = this.newMembus();
this.iobus = this.newIOBus();
this.cpu = this.newCPU(this.membus, this.iobus);
this.video = new RasterVideo(this.mainElement, this.canvasWidth, this.numVisibleScanlines, {overscan:true});
this.video.create();
this.audio = new MasterAudio(); this.audio = new MasterAudio();
this.psg = new SN76489_Audio(this.audio); this.psg = new SN76489_Audio(this.audio);
var cru = { var cru = {
@ -157,71 +135,26 @@ class SG1000Platform extends BaseZ80Platform {
} }
}; };
this.vdp = this.newVDP(this.video.getFrameData(), cru, true); // true = 4 sprites/line this.vdp = this.newVDP(this.video.getFrameData(), cru, true); // true = 4 sprites/line
setKeyboardFromMap(this.video, this.inputs, SG1000_KEYCODE_MAP); // TODO
this.timer = new AnimationTimer(60, this.nextFrame.bind(this));
} }
readAddress(addr) { startScanline(sl : number) {
return this.membus.read(addr);
} }
advance(novideo : boolean) { drawScanline(sl : number) {
var extraCycles = 0; this.vdp.drawScanline(sl);
for (var sl=0; sl<this.numTotalScanlines; sl++) {
this.currentScanline = sl;
this.startLineTstates = this.cpu.getTstates() + extraCycles;
extraCycles = this.runCPU(this.cpu, this.cpuCyclesPerLine - extraCycles); // TODO: HALT opcode?
//debug//this.video.getFrameData()[sl] = -1>>>extraCycles;
this.vdp.drawScanline(sl);
}
this.video.updateFrame();
}
loadROM(title, data) {
this.rom = padBytes(data, 0xc000);
this.reset();
} }
loadState(state) { loadState(state) {
this.cpu.loadState(state.c); super.loadState(state);
this.ram.mem.set(state.b); this.vdp.restoreState(state['vdp']);
this.vdp.restoreState(state.vdp);
this.inputs.set(state.in);
} }
saveState() { saveState() {
return { var state = super.saveState();
c:this.getCPUState(), state['vdp'] = this.vdp.getState();
b:this.ram.mem.slice(0), return state;
vdp:this.vdp.getState(),
in:this.inputs.slice(0),
};
}
loadControlsState(state) {
this.inputs.set(state.in);
}
saveControlsState() {
return {
in:this.inputs.slice(0)
};
}
getCPUState() {
return this.cpu.saveState();
}
isRunning() {
return this.timer && this.timer.isRunning();
}
pause() {
this.timer.stop();
this.audio.stop();
}
resume() {
this.timer.start();
this.audio.start();
} }
reset() { reset() {
this.cpu.reset(); super.reset();
this.cpu.setTstates(0);
this.vdp.reset(); this.vdp.reset();
this.psg.reset(); this.psg.reset();
} }
@ -244,10 +177,9 @@ class SG1000Platform extends BaseZ80Platform {
class SMSPlatform extends SG1000Platform { class SMSPlatform extends SG1000Platform {
cartram : RAM = new RAM(0); cartram = new Uint8Array(0);
pagingRegisters = new Uint8Array(4); pagingRegisters = new Uint8Array(4);
romPageMask : number; romPageMask : number;
// TODO: add to state
latchedHCounter = 0; latchedHCounter = 0;
ioControlFlags = 0; ioControlFlags = 0;
// TODO: hide bottom scanlines // TODO: hide bottom scanlines
@ -281,7 +213,9 @@ class SMSPlatform extends SG1000Platform {
this.ioControlFlags = v; this.ioControlFlags = v;
} }
newRAM() { return new RAM(0x2000); } newRAM() {
return new Uint8Array(0x2000);
}
getPagedROM(a:number, reg:number) { getPagedROM(a:number, reg:number) {
//if (!(a&0xff)) console.log(hex(a), reg, this.pagingRegisters[reg], this.romPageMask); //if (!(a&0xff)) console.log(hex(a), reg, this.pagingRegisters[reg], this.romPageMask);
@ -291,14 +225,14 @@ class SMSPlatform extends SG1000Platform {
newMembus() { newMembus() {
return { return {
read: newAddressDecoder([ read: newAddressDecoder([
[0xc000, 0xffff, 0x1fff, (a) => { return this.ram.mem[a]; }], [0xc000, 0xffff, 0x1fff, (a) => { return this.ram[a]; }],
[0x0000, 0x03ff, 0x3ff, (a) => { return this.rom[a]; }], [0x0000, 0x03ff, 0x3ff, (a) => { return this.rom[a]; }],
[0x0400, 0x3fff, 0x3fff, (a) => { return this.getPagedROM(a,1); }], [0x0400, 0x3fff, 0x3fff, (a) => { return this.getPagedROM(a,1); }],
[0x4000, 0x7fff, 0x3fff, (a) => { return this.getPagedROM(a,2); }], [0x4000, 0x7fff, 0x3fff, (a) => { return this.getPagedROM(a,2); }],
[0x8000, 0xbfff, 0x3fff, (a) => { [0x8000, 0xbfff, 0x3fff, (a) => {
var reg0 = this.pagingRegisters[0]; // RAM select? var reg0 = this.pagingRegisters[0]; // RAM select?
if (reg0 & 0x8) { if (reg0 & 0x8) {
return this.cartram.mem[(reg0 & 0x4) ? a+0x4000 : a]; return this.cartram[(reg0 & 0x4) ? a+0x4000 : a];
} else { } else {
return this.getPagedROM(a,3); return this.getPagedROM(a,3);
} }
@ -306,18 +240,18 @@ class SMSPlatform extends SG1000Platform {
]), ]),
write: newAddressDecoder([ write: newAddressDecoder([
[0xc000, 0xfffb, 0x1fff, (a,v) => { [0xc000, 0xfffb, 0x1fff, (a,v) => {
this.ram.mem[a] = v; this.ram[a] = v;
}], }],
[0xfffc, 0xffff, 0x3, (a,v) => { [0xfffc, 0xffff, 0x3, (a,v) => {
this.pagingRegisters[a] = v; this.pagingRegisters[a] = v;
this.ram.mem[a+0x1ffc] = v; this.ram[a+0x1ffc] = v;
}], }],
[0x8000, 0xbfff, 0x3fff, (a,v) => { [0x8000, 0xbfff, 0x3fff, (a,v) => {
var reg0 = this.pagingRegisters[0]; // RAM select? var reg0 = this.pagingRegisters[0]; // RAM select?
if (reg0 & 0x8) { if (reg0 & 0x8) {
if (this.cartram.mem.length == 0) if (this.cartram.length == 0)
this.cartram = new RAM(0x8000); // create cartridge RAM lazily this.cartram = new Uint8Array(0x8000); // create cartridge RAM lazily
this.cartram.mem[(reg0 & 0x4) ? a+0x4000 : a] = v; this.cartram[(reg0 & 0x4) ? a+0x4000 : a] = v;
} }
}], }],
]), ]),
@ -349,12 +283,16 @@ class SMSPlatform extends SG1000Platform {
loadState(state) { loadState(state) {
super.loadState(state); super.loadState(state);
this.pagingRegisters.set(state.pr); this.pagingRegisters.set(state.pr);
this.cartram.mem.set(state.cr); this.cartram.set(state.cr);
this.latchedHCounter = state.lhc;
this.ioControlFlags = state.iocf;
} }
saveState() { saveState() {
var state = super.saveState(); var state = super.saveState();
state['pr'] = this.pagingRegisters.slice(0); state['pr'] = this.pagingRegisters.slice(0);
state['cr'] = this.cartram.mem.slice(0); state['cr'] = this.cartram.slice(0);
state['lhc'] = this.latchedHCounter;
state['iocf'] = this.ioControlFlags;
return state; return state;
} }
getDebugInfo(category, state) { getDebugInfo(category, state) {