1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-25 18:33:11 +00:00

added SMS

This commit is contained in:
Steven Hugg 2019-08-25 13:01:07 -04:00
parent e87cb2bef1
commit ecd7f364a6
4 changed files with 251 additions and 412 deletions

View File

@ -1079,127 +1079,7 @@ export function lookupSymbol(platform:Platform, addr:number, extra:boolean) {
return "";
}
///// 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;
pixels : Uint32Array;
inputs : Uint8Array = new Uint8Array(32);
mainElement : HTMLElement;
poller : ControllerPoller;
abstract newRAM() : Uint8Array;
abstract newMembus() : MemoryBus;
abstract newIOBus() : MemoryBus;
abstract getVideoOptions() : {};
abstract getKeyboardMap();
abstract startScanline(sl : number) : void;
abstract drawScanline(sl : number) : void;
getRasterScanline() : number { return this.currentScanline; }
getKeyboardFunction() { return null; }
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();
this.pixels = this.video.getFrameData();
this.poller = setKeyboardFromMap(this.video, this.inputs, this.getKeyboardMap(), this.getKeyboardFunction());
this.timer = new AnimationTimer(60, this.nextFrame.bind(this));
}
readAddress(addr) {
return this.membus.read(addr);
}
pollControls() { this.poller.poll(); }
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);
}
}
/// new style
/// new Machine platform adapters
import { Bus, Resettable, FrameBased, VideoSource, SampledAudioSource, AcceptsROM, AcceptsKeyInput, SavesState, SavesInputState, HasCPU } from "./devices";
import { Probeable, RasterFrameBased } from "./devices";
@ -1409,7 +1289,10 @@ export abstract class BaseZ80MachinePlatform<T extends Machine> extends BaseMach
getToolForFilename = getToolForFilename_z80;
getDebugCategories() {
return ['CPU','Stack'];
if (isDebuggable(this.machine))
return this.machine.getDebugCategories();
else
return ['CPU','Stack'];
}
getDebugInfo(category:string, state:EmuState) : string {
switch (category) {

226
src/machine/sms.ts Normal file
View File

@ -0,0 +1,226 @@
import { Z80, Z80State } from "../cpu/ZilogZ80";
import { BasicScanlineMachine } from "../devices";
import { BaseZ80VDPBasedMachine } from "./vdp_z80";
import { KeyFlags, newAddressDecoder, padBytes, Keys, makeKeycodeMap, newKeyboardHandler } from "../emu";
import { hex, lzgmini, stringToByteArray } from "../util";
import { TssChannelAdapter, MasterAudio, SN76489_Audio } from "../audio";
import { TMS9918A, SMSVDP } from "../video/tms9918a";
// http://www.smspower.org/Development/Index
// http://www.smspower.org/uploads/Development/sg1000.txt
// http://www.smspower.org/uploads/Development/richard.txt
// http://www.smspower.org/uploads/Development/msvdp-20021112.txt
// http://www.smspower.org/uploads/Development/SN76489-20030421.txt
var SG1000_KEYCODE_MAP = makeKeycodeMap([
[Keys.UP, 0, 0x1],
[Keys.DOWN, 0, 0x2],
[Keys.LEFT, 0, 0x4],
[Keys.RIGHT, 0, 0x8],
[Keys.A, 0, 0x10],
[Keys.B, 0, 0x20],
[Keys.P2_UP, 0, 0x40],
[Keys.P2_DOWN, 0, 0x80],
[Keys.P2_LEFT, 1, 0x1],
[Keys.P2_RIGHT, 1, 0x2],
[Keys.P2_A, 1, 0x4],
[Keys.P2_B, 1, 0x8],
[Keys.VK_BACK_SLASH, 1, 0x10], // reset
]);
export class SG1000 extends BaseZ80VDPBasedMachine {
numVisibleScanlines = 240;
defaultROMSize = 0xc000;
overscan = true;
ram = new Uint8Array(0x400);
constructor() {
super();
this.init(this, this.newIOBus(), new SN76489_Audio(new MasterAudio()));
}
getKeyboardMap() { return SG1000_KEYCODE_MAP; }
vdpInterrupt() { return this.cpu.NMI(); }
read = newAddressDecoder([
[0xc000, 0xffff, 0x3ff, (a) => { return this.ram[a]; }],
[0x0000, 0xbfff, 0xffff, (a) => { return this.rom[a]; }],
]);
write = newAddressDecoder([
[0xc000, 0xffff, 0x3ff, (a,v) => { this.ram[a] = v; }],
]);
getVCounter() : number { return 0; }
getHCounter() : number { return 0; }
setMemoryControl(v:number) { }
setIOPortControl(v:number) { }
newIOBus() {
return {
read: (addr:number) => {
addr &= 0xff;
//console.log('IO read', hex(addr,4));
switch (addr & 0xc1) {
case 0x40: return this.getVCounter();
case 0x41: return this.getHCounter();
case 0x80: return this.vdp.readData();
case 0x81: return this.vdp.readStatus();
case 0xc0: return this.inputs[0] ^ 0xff;
case 0xc1: return this.inputs[1] ^ 0xff;
}
return 0;
},
write: (addr:number, val:number) => {
addr &= 0xff;
val &= 0xff;
//console.log('IO write', hex(addr,4), hex(val,2));
switch (addr & 0xc1) {
case 0x00: return this.setMemoryControl(val);
case 0x01: return this.setIOPortControl(val);
case 0x40:
case 0x41: return this.psg.setData(val);
case 0x80: return this.vdp.writeData(val);
case 0x81: return this.vdp.writeAddress(val);
}
}
};
}
}
///
export class SMS extends SG1000 {
cartram = new Uint8Array(0);
pagingRegisters = new Uint8Array(4);
romPageMask : number;
latchedHCounter = 0;
ioControlFlags = 0;
// TODO: hide bottom scanlines
ram = new Uint8Array(0x2000);
newVDP(frameData, cru, flicker) {
return new SMSVDP(frameData, cru, flicker);
}
reset() {
super.reset();
this.pagingRegisters.set([0,0,1,2]);
}
getVCounter() {
var y = this.scanline;
return (y <= 0xda) ? (y) : (y - 6);
}
getHCounter() {
return this.latchedHCounter;
}
computeHCounter() {
return 0;
/*
var t0 = this.startLineTstates;
var t1 = this.cpu.getTstates();
return (t1-t0) & 0xff; // TODO
*/
}
setIOPortControl(v:number) {
if ((v ^ this.ioControlFlags) & 0xa0) { // either joystick TH pin
this.latchedHCounter = this.computeHCounter();
//console.log("H:"+hex(this.latchedHCounter)+" V:"+hex(this.getVCounter()));
}
this.ioControlFlags = v;
}
getPagedROM(a:number, reg:number) {
//if (!(a&0xff)) console.log(hex(a), reg, this.pagingRegisters[reg], this.romPageMask);
return this.rom[a + ((this.pagingRegisters[reg] & this.romPageMask) << 14)]; // * $4000
}
newMembus() {
return {
read: newAddressDecoder([
[0xc000, 0xffff, 0x1fff, (a) => { return this.ram[a]; }],
[0x0000, 0x03ff, 0x3ff, (a) => { return this.rom[a]; }],
[0x0400, 0x3fff, 0x3fff, (a) => { return this.getPagedROM(a,1); }],
[0x4000, 0x7fff, 0x3fff, (a) => { return this.getPagedROM(a,2); }],
[0x8000, 0xbfff, 0x3fff, (a) => {
var reg0 = this.pagingRegisters[0]; // RAM select?
if (reg0 & 0x8) {
return this.cartram[(reg0 & 0x4) ? a+0x4000 : a];
} else {
return this.getPagedROM(a,3);
}
}],
]),
write: newAddressDecoder([
[0xc000, 0xfffb, 0x1fff, (a,v) => {
this.ram[a] = v;
}],
[0xfffc, 0xffff, 0x3, (a,v) => {
this.pagingRegisters[a] = v;
this.ram[a+0x1ffc] = v;
}],
[0x8000, 0xbfff, 0x3fff, (a,v) => {
var reg0 = this.pagingRegisters[0]; // RAM select?
if (reg0 & 0x8) {
if (this.cartram.length == 0)
this.cartram = new Uint8Array(0x8000); // create cartridge RAM lazily
this.cartram[(reg0 & 0x4) ? a+0x4000 : a] = v;
}
}],
]),
isContended: () => { return false; },
};
}
loadROM(data:Uint8Array) {
if (data.length <= 0xc000) {
this.rom = padBytes(data, 0xc000);
this.romPageMask = 3; // only pages 0, 1, 2
} else {
switch (data.length) {
case 0x10000:
case 0x20000:
case 0x40000:
case 0x80000:
this.rom = data;
this.romPageMask = (data.length >> 14) - 1; // div $4000
break;
default:
throw "Unknown rom size: $" + hex(data.length);
}
}
//console.log("romPageMask: " + hex(this.romPageMask));
this.reset();
}
loadState(state) {
super.loadState(state);
this.pagingRegisters.set(state.pr);
this.cartram.set(state.cr);
this.latchedHCounter = state.lhc;
this.ioControlFlags = state.iocf;
}
saveState() {
var state = super.saveState();
state['pr'] = this.pagingRegisters.slice(0);
state['cr'] = this.cartram.slice(0);
state['lhc'] = this.latchedHCounter;
state['iocf'] = this.ioControlFlags;
return state;
}
getDebugInfo(category, state) {
switch (category) {
case 'CPU':
return super.getDebugInfo(category, state) +
"\nBank Regs: " + this.pagingRegisters + "\n";
default: return super.getDebugInfo(category, state);
}
}
}

View File

@ -41,10 +41,6 @@ export abstract class BaseZ80VDPBasedMachine extends BasicScanlineMachine {
connectVideo(pixels) {
super.connectVideo(pixels);
this.vdp = this.newVDP(this.pixels);
}
newVDP(frameData) {
var cru = {
setVDPInterrupt: (b) => {
if (b) {
@ -54,7 +50,11 @@ export abstract class BaseZ80VDPBasedMachine extends BasicScanlineMachine {
}
}
};
return new TMS9918A(frameData, cru, true);
this.vdp = this.newVDP(this.pixels, cru, true);
}
newVDP(frameData, cru, flicker) {
return new TMS9918A(frameData, cru, flicker);
}
startScanline() {
@ -81,7 +81,7 @@ export abstract class BaseZ80VDPBasedMachine extends BasicScanlineMachine {
}
getDebugCategories() {
return ['VDP'];
return ['CPU','Stack','VDP'];
}
getDebugInfo(category, state) {
switch (category) {

View File

@ -1,17 +1,7 @@
"use strict";
import { Platform, BasicZ80ScanlinePlatform } from "../baseplatform";
import { PLATFORMS, RAM, newAddressDecoder, padBytes, noise, setKeyboardFromMap, AnimationTimer, RasterVideo, Keys, makeKeycodeMap } from "../emu";
import { hex, lzgmini, stringToByteArray } from "../util";
import { MasterAudio, SN76489_Audio } from "../audio";
import { TMS9918A, SMSVDP } from "../video/tms9918a";
import { ColecoVision_PRESETS } from "./coleco";
// http://www.smspower.org/Development/Index
// http://www.smspower.org/uploads/Development/sg1000.txt
// http://www.smspower.org/uploads/Development/richard.txt
// http://www.smspower.org/uploads/Development/msvdp-20021112.txt
// http://www.smspower.org/uploads/Development/SN76489-20030421.txt
import { SG1000, SMS } from "../machine/sms";
import { Platform, BaseZ80MachinePlatform, BaseMAMEPlatform, getToolForFilename_z80 } from "../baseplatform";
import { PLATFORMS } from "../emu";
// TODO: merge w/ coleco
export var SG1000_PRESETS = [
@ -36,282 +26,22 @@ export var SMS_PRESETS = [
{id:'climber.c', name:'Climber Game'},
];
var SG1000_KEYCODE_MAP = makeKeycodeMap([
[Keys.UP, 0, 0x1],
[Keys.DOWN, 0, 0x2],
[Keys.LEFT, 0, 0x4],
[Keys.RIGHT, 0, 0x8],
[Keys.A, 0, 0x10],
[Keys.B, 0, 0x20],
[Keys.P2_UP, 0, 0x40],
[Keys.P2_DOWN, 0, 0x80],
[Keys.P2_LEFT, 1, 0x1],
[Keys.P2_RIGHT, 1, 0x2],
[Keys.P2_A, 1, 0x4],
[Keys.P2_B, 1, 0x8],
[Keys.VK_BACK_SLASH, 1, 0x10], // reset
]);
class SG1000Platform extends BasicZ80ScanlinePlatform implements Platform {
cpuFrequency = 3579545; // MHz
canvasWidth = 304;
numTotalScanlines = 262;
numVisibleScanlines = 240;
defaultROMSize = 0xc000;
vdp : TMS9918A;
getPresets() { return SG1000_PRESETS; }
getKeyboardMap() { return SG1000_KEYCODE_MAP; }
getVideoOptions() { return {overscan:true}; }
newRAM() {
return new Uint8Array(0x400);
}
newMembus() {
return {
read: newAddressDecoder([
[0xc000, 0xffff, 0x3ff, (a) => { return this.ram[a]; }],
[0x0000, 0xbfff, 0xffff, (a) => { return this.rom[a]; }],
]),
write: newAddressDecoder([
[0xc000, 0xffff, 0x3ff, (a,v) => { this.ram[a] = v; }],
]),
};
}
getVCounter() : number { return 0; }
getHCounter() : number { return 0; }
setMemoryControl(v:number) { }
setIOPortControl(v:number) { }
newIOBus() {
return {
read: (addr:number) => {
addr &= 0xff;
//console.log('IO read', hex(addr,4));
switch (addr & 0xc1) {
case 0x40: return this.getVCounter();
case 0x41: return this.getHCounter();
case 0x80: return this.vdp.readData();
case 0x81: return this.vdp.readStatus();
case 0xc0: return this.inputs[0] ^ 0xff;
case 0xc1: return this.inputs[1] ^ 0xff;
}
return 0;
},
write: (addr:number, val:number) => {
addr &= 0xff;
val &= 0xff;
//console.log('IO write', hex(addr,4), hex(val,2));
switch (addr & 0xc1) {
case 0x00: return this.setMemoryControl(val);
case 0x01: return this.setIOPortControl(val);
case 0x40:
case 0x41: return this.psg.setData(val);
case 0x80: return this.vdp.writeData(val);
case 0x81: return this.vdp.writeAddress(val);
}
}
};
}
newVDP(frameData, cru, flicker) {
return new TMS9918A(frameData, cru, flicker);
}
start() {
super.start();
this.audio = new MasterAudio();
this.psg = new SN76489_Audio(this.audio);
var cru = {
setVDPInterrupt: (b) => {
if (b) {
this.cpu.nonMaskableInterrupt();
} else {
// TODO: reset interrupt?
}
}
};
this.vdp = this.newVDP(this.video.getFrameData(), cru, true); // true = 4 sprites/line
}
startScanline(sl : number) {
}
drawScanline(sl : number) {
this.vdp.drawScanline(sl);
}
loadState(state) {
super.loadState(state);
this.vdp.restoreState(state['vdp']);
}
saveState() {
var state = super.saveState();
state['vdp'] = this.vdp.getState();
return state;
}
reset() {
super.reset();
this.vdp.reset();
this.psg.reset();
}
getDebugCategories() {
return super.getDebugCategories().concat(['VDP']);
}
getDebugInfo(category, state) {
switch (category) {
case 'VDP': return this.vdpStateToLongString(state.vdp);
default: return super.getDebugInfo(category, state);
}
}
vdpStateToLongString(ppu) {
return this.vdp.getRegsString();
}
readVRAMAddress(a : number) : number {
return this.vdp.ram[a & 0x3fff];
}
}
///
class SMSPlatform extends SG1000Platform {
class SG1000Platform extends BaseZ80MachinePlatform<SG1000> implements Platform {
cartram = new Uint8Array(0);
pagingRegisters = new Uint8Array(4);
romPageMask : number;
latchedHCounter = 0;
ioControlFlags = 0;
// TODO: hide bottom scanlines
getPresets() { return SMS_PRESETS; }
reset() {
super.reset();
this.pagingRegisters.set([0,0,1,2]);
}
newMachine() { return new SG1000(); }
getPresets() { return SG1000_PRESETS; }
getDefaultExtension() { return ".c"; };
readAddress(a) { return this.machine.read(a); }
}
newVDP(frameData, cru, flicker) {
return new SMSVDP(frameData, cru, flicker);
}
getVCounter() {
var y = this.currentScanline;
return (y <= 0xda) ? (y) : (y - 6);
}
getHCounter() {
return this.latchedHCounter;
}
computeHCounter() {
var t0 = this.startLineTstates;
var t1 = this.cpu.getTstates();
return (t1-t0) & 0xff; // TODO
}
setIOPortControl(v:number) {
if ((v ^ this.ioControlFlags) & 0xa0) { // either joystick TH pin
this.latchedHCounter = this.computeHCounter();
//console.log("H:"+hex(this.latchedHCounter)+" V:"+hex(this.getVCounter()));
}
this.ioControlFlags = v;
}
newRAM() {
return new Uint8Array(0x2000);
}
getPagedROM(a:number, reg:number) {
//if (!(a&0xff)) console.log(hex(a), reg, this.pagingRegisters[reg], this.romPageMask);
return this.rom[a + ((this.pagingRegisters[reg] & this.romPageMask) << 14)]; // * $4000
}
class SMSPlatform extends BaseZ80MachinePlatform<SMS> implements Platform {
newMembus() {
return {
read: newAddressDecoder([
[0xc000, 0xffff, 0x1fff, (a) => { return this.ram[a]; }],
[0x0000, 0x03ff, 0x3ff, (a) => { return this.rom[a]; }],
[0x0400, 0x3fff, 0x3fff, (a) => { return this.getPagedROM(a,1); }],
[0x4000, 0x7fff, 0x3fff, (a) => { return this.getPagedROM(a,2); }],
[0x8000, 0xbfff, 0x3fff, (a) => {
var reg0 = this.pagingRegisters[0]; // RAM select?
if (reg0 & 0x8) {
return this.cartram[(reg0 & 0x4) ? a+0x4000 : a];
} else {
return this.getPagedROM(a,3);
}
}],
]),
write: newAddressDecoder([
[0xc000, 0xfffb, 0x1fff, (a,v) => {
this.ram[a] = v;
}],
[0xfffc, 0xffff, 0x3, (a,v) => {
this.pagingRegisters[a] = v;
this.ram[a+0x1ffc] = v;
}],
[0x8000, 0xbfff, 0x3fff, (a,v) => {
var reg0 = this.pagingRegisters[0]; // RAM select?
if (reg0 & 0x8) {
if (this.cartram.length == 0)
this.cartram = new Uint8Array(0x8000); // create cartridge RAM lazily
this.cartram[(reg0 & 0x4) ? a+0x4000 : a] = v;
}
}],
]),
isContended: () => { return false; },
};
}
loadROM(title, data) {
if (data.length <= 0xc000) {
this.rom = padBytes(data, 0xc000);
this.romPageMask = 3; // only pages 0, 1, 2
} else {
switch (data.length) {
case 0x10000:
case 0x20000:
case 0x40000:
case 0x80000:
this.rom = data;
this.romPageMask = (data.length >> 14) - 1; // div $4000
break;
default:
throw "Unknown rom size: $" + hex(data.length);
}
}
//console.log("romPageMask: " + hex(this.romPageMask));
this.reset();
}
loadState(state) {
super.loadState(state);
this.pagingRegisters.set(state.pr);
this.cartram.set(state.cr);
this.latchedHCounter = state.lhc;
this.ioControlFlags = state.iocf;
}
saveState() {
var state = super.saveState();
state['pr'] = this.pagingRegisters.slice(0);
state['cr'] = this.cartram.slice(0);
state['lhc'] = this.latchedHCounter;
state['iocf'] = this.ioControlFlags;
return state;
}
getDebugInfo(category, state) {
switch (category) {
case 'CPU':
return super.getDebugInfo(category, state) +
"\nBank Regs: " + this.pagingRegisters + "\n";
default: return super.getDebugInfo(category, state);
}
}
newMachine() { return new SMS(); }
getPresets() { return SMS_PRESETS; }
getDefaultExtension() { return ".c"; };
readAddress(a) { return this.machine.read(a); }
}
///