1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-17 11:05:58 +00:00
8bitworkshop/src/platform/williams.ts

450 lines
14 KiB
TypeScript
Raw Normal View History

2017-01-21 13:13:36 +00:00
import { Platform, BaseZ80Platform, Base6809Platform } from "../common/baseplatform";
import { PLATFORMS, RAM, newAddressDecoder, padBytes, noise, setKeyboardFromMap, AnimationTimer, RasterVideo, Keys, makeKeycodeMap } from "../common/emu";
import { hex } from "../common/util";
import { MasterAudio, WorkerSoundChannel } from "../common/audio";
2017-01-20 03:42:58 +00:00
var WILLIAMS_PRESETS = [
{ id: 'gfxtest.c', name: 'Graphics Test' },
{ id: 'sprites.c', name: 'Sprite Test' },
{ id: 'game1.c', name: 'Raster Paranoia Game' },
{ id: 'bitmap_rle.c', name: 'RLE Bitmap' },
2017-01-20 03:42:58 +00:00
];
2017-01-29 21:06:05 +00:00
var WilliamsPlatform = function(mainElement, proto) {
2017-01-20 03:42:58 +00:00
var self = this;
this.__proto__ = new (proto ? proto : Base6809Platform)();
2017-01-20 03:42:58 +00:00
2017-01-20 22:54:02 +00:00
var SCREEN_HEIGHT = 304;
var SCREEN_WIDTH = 256;
var cpu, ram, rom, nvram;
var portsel = 0;
2017-01-20 03:42:58 +00:00
var banksel = 0;
var watchdog_counter;
var watchdog_enabled = false;
2017-01-20 22:54:02 +00:00
var pia6821 = new RAM(8).mem;
var blitregs = new RAM(8).mem;
var video, timer, pixels, displayPCs;
2017-01-20 03:42:58 +00:00
var screenNeedsRefresh = false;
var membus;
2017-01-20 22:54:02 +00:00
var video_counter;
2017-01-20 03:42:58 +00:00
2017-04-02 18:54:51 +00:00
var audio, worker, workerchannel;
2017-01-20 03:42:58 +00:00
var xtal = 12000000;
var cpuFrequency = xtal / 3 / 4;
var cpuCyclesPerFrame = cpuFrequency / 60; // TODO
var cpuScale = 1;
2017-04-13 19:48:37 +00:00
var INITIAL_WATCHDOG = 8;
2017-01-20 03:42:58 +00:00
var PIXEL_ON = 0xffeeeeee;
var PIXEL_OFF = 0xff000000;
2017-01-20 22:54:02 +00:00
var DEFENDER_KEYCODE_MAP = makeKeycodeMap([
[Keys.A, 4, 0x1],
[Keys.RIGHT, 4, 0x2],
[Keys.B, 4, 0x4],
2017-01-20 03:42:58 +00:00
[Keys.VK_X, 4, 0x8],
[Keys.P2_START, 4, 0x10],
[Keys.START, 4, 0x20],
[Keys.LEFT, 4, 0x40],
[Keys.DOWN, 4, 0x80],
[Keys.UP, 6, 0x1],
[Keys.SELECT, 0, 0x4],
2017-01-20 03:42:58 +00:00
[Keys.VK_7, 0, 0x1],
[Keys.VK_8, 0, 0x2],
[Keys.VK_9, 0, 0x8],
]);
2017-01-20 22:54:02 +00:00
var ROBOTRON_KEYCODE_MAP = makeKeycodeMap([
[Keys.P2_UP, 0, 0x1],
[Keys.P2_DOWN, 0, 0x2],
[Keys.P2_LEFT, 0, 0x4],
[Keys.P2_RIGHT, 0, 0x8],
[Keys.START, 0, 0x10],
[Keys.P2_START, 0, 0x20],
[Keys.UP, 0, 0x40],
[Keys.DOWN, 0, 0x80],
[Keys.LEFT, 2, 0x1],
[Keys.RIGHT, 2, 0x2],
2017-01-20 22:54:02 +00:00
[Keys.VK_7, 4, 0x1],
[Keys.VK_8, 4, 0x2],
2017-04-06 19:09:30 +00:00
[Keys.VK_6, 4, 0x4],
2017-01-20 22:54:02 +00:00
[Keys.VK_9, 4, 0x8],
[Keys.SELECT, 4, 0x10],
2017-01-20 22:54:02 +00:00
]);
2017-04-06 19:09:30 +00:00
// TODO: sound board handshake
2017-01-20 22:54:02 +00:00
2017-01-20 03:42:58 +00:00
var palette = [];
for (var ii = 0; ii < 16; ii++)
2017-01-20 03:42:58 +00:00
palette[ii] = 0xff000000;
this.getPresets = function() {
return WILLIAMS_PRESETS;
}
2017-01-20 22:54:02 +00:00
// Defender
var ioread_defender = newAddressDecoder([
2017-01-20 03:42:58 +00:00
[0x400, 0x5ff, 0x1ff, function(a) { return nvram.mem[a]; }],
[0x800, 0x800, 0, function(a) { return video_counter; }],
[0xc00, 0xc07, 0x7, function(a) { return pia6821[a]; }],
[0x0, 0xfff, 0, function(a) { /*console.log('ioread',hex(a));*/ }],
2017-01-20 03:42:58 +00:00
]);
var iowrite_defender = newAddressDecoder([
[0x0, 0xf, 0xf, setPalette],
[0x3fc, 0x3ff, 0, function(a, v) { if (v == 0x38) watchdog_counter = INITIAL_WATCHDOG; }],
[0x400, 0x5ff, 0x1ff, function(a, v) { nvram.mem[a] = v; }],
[0xc00, 0xc07, 0x7, function(a, v) { pia6821[a] = v; }],
[0x0, 0xfff, 0, function(a, v) { console.log('iowrite', hex(a), hex(v)); }],
2017-01-20 03:42:58 +00:00
]);
var memread_defender = newAddressDecoder([
2017-01-20 22:54:02 +00:00
[0x0000, 0xbfff, 0xffff, function(a) { return ram.mem[a]; }],
[0xc000, 0xcfff, 0x0fff, function(a) {
switch (banksel) {
case 0: return ioread_defender(a);
case 1: return rom[a + 0x3000];
case 2: return rom[a + 0x4000];
case 3: return rom[a + 0x5000];
case 7: return rom[a + 0x6000];
2017-01-20 22:54:02 +00:00
default: return 0; // TODO: error light
}
}],
[0xd000, 0xffff, 0xffff, function(a) { return rom ? rom[a - 0xd000] : 0; }],
2017-01-20 22:54:02 +00:00
]);
var memwrite_defender = newAddressDecoder([
[0x0000, 0x97ff, 0, write_display_byte],
[0x9800, 0xbfff, 0, function(a, v) { ram.mem[a] = v; }],
2017-01-20 22:54:02 +00:00
[0xc000, 0xcfff, 0x0fff, iowrite_defender],
[0xd000, 0xdfff, 0, function(a, v) { banksel = v & 0x7; }],
[0, 0xffff, 0, function(a, v) { console.log(hex(a), hex(v)); }],
2017-01-20 22:54:02 +00:00
]);
// Robotron, Joust, Bubbles, Stargate
var ioread_williams = newAddressDecoder([
[0x804, 0x807, 0x3, function(a) { return pia6821[a]; }],
[0x80c, 0x80f, 0x3, function(a) { return pia6821[a + 4]; }],
[0xb00, 0xbff, 0, function(a) { return video_counter; }],
2017-01-20 22:54:02 +00:00
[0xc00, 0xfff, 0x3ff, function(a) { return nvram.mem[a]; }],
[0x0, 0xfff, 0, function(a) { /* console.log('ioread',hex(a)); */ }],
2017-01-20 22:54:02 +00:00
]);
var iowrite_williams = newAddressDecoder([
[0x0, 0xf, 0xf, setPalette],
[0x80c, 0x80c, 0xf, function(a, v) { if (worker) worker.postMessage({ command: v }); }],
2017-04-02 18:54:51 +00:00
//[0x804, 0x807, 0x3, function(a,v) { console.log('iowrite',a); }], // TODO: sound
//[0x80c, 0x80f, 0x3, function(a,v) { console.log('iowrite',a+4); }], // TODO: sound
[0x900, 0x9ff, 0, function(a, v) { banksel = v & 0x1; }],
[0xa00, 0xa07, 0x7, setBlitter],
[0xbff, 0xbff, 0, function(a, v) { if (v == 0x39) watchdog_counter = INITIAL_WATCHDOG; }],
[0xc00, 0xfff, 0x3ff, function(a, v) { nvram.mem[a] = v; }],
2017-03-15 14:11:28 +00:00
//[0x0, 0xfff, 0, function(a,v) { console.log('iowrite',hex(a),hex(v)); }],
2017-01-20 22:54:02 +00:00
]);
var memread_williams = newAddressDecoder([
2017-04-06 14:28:51 +00:00
[0x0000, 0x8fff, 0xffff, function(a) { return banksel ? rom[a] : ram.mem[a]; }],
[0x9000, 0xbfff, 0xffff, function(a) { return ram.mem[a]; }],
2017-01-20 22:54:02 +00:00
[0xc000, 0xcfff, 0x0fff, ioread_williams],
[0xd000, 0xffff, 0xffff, function(a) { return rom ? rom[a - 0x4000] : 0; }],
2017-01-20 22:54:02 +00:00
]);
var memwrite_williams = newAddressDecoder([
[0x0000, 0x97ff, 0, write_display_byte],
[0x9800, 0xbfff, 0, function(a, v) { ram.mem[a] = v; }],
2017-01-20 22:54:02 +00:00
[0xc000, 0xcfff, 0x0fff, iowrite_williams],
2017-03-15 14:11:28 +00:00
//[0x0000, 0xffff, 0, function(a,v) { console.log(hex(a), hex(v)); }],
2017-01-20 22:54:02 +00:00
]);
// d1d6 ldu $11 / beq $d1ed
function setPalette(a, v) {
2017-01-20 22:54:02 +00:00
// RRRGGGBB
var color = 0xff000000 | ((v & 7) << 5) | (((v >> 3) & 7) << 13) | (((v >> 6) << 22));
2017-01-20 22:54:02 +00:00
if (color != palette[a]) {
palette[a] = color;
screenNeedsRefresh = true;
}
}
function write_display_byte(a: number, v: number) {
2017-01-20 22:54:02 +00:00
ram.mem[a] = v;
drawDisplayByte(a, v);
if (displayPCs) displayPCs[a] = cpu.getPC(); // save program counter
2017-01-20 22:54:02 +00:00
}
function drawDisplayByte(a, v) {
var ofs = ((a & 0xff00) << 1) | ((a & 0xff) ^ 0xff);
pixels[ofs] = palette[v >> 4];
pixels[ofs + 256] = palette[v & 0xf];
2017-01-20 03:42:58 +00:00
}
function setBlitter(a, v) {
2017-01-20 22:54:02 +00:00
if (a) {
blitregs[a] = v;
} else {
2017-01-29 21:06:05 +00:00
var cycles = doBlit(v);
this.waitCycles -= cycles * cpuScale; // wait CPU cycles
2017-01-20 22:54:02 +00:00
}
}
function doBlit(flags) {
//console.log(hex(flags), blitregs);
flags &= 0xff;
var offs = SCREEN_HEIGHT - blitregs[7];
var sstart = (blitregs[2] << 8) + blitregs[3];
var dstart = (blitregs[4] << 8) + blitregs[5];
var w = blitregs[6] ^ 4; // blitter bug fix
var h = blitregs[7] ^ 4;
if (w == 0) w++;
if (h == 0) h++;
if (h == 255) h++;
2017-01-20 22:54:02 +00:00
var sxinc = (flags & 0x1) ? 256 : 1;
var syinc = (flags & 0x1) ? 1 : w;
var dxinc = (flags & 0x2) ? 256 : 1;
var dyinc = (flags & 0x2) ? 1 : w;
var pixdata = 0;
for (var y = 0; y < h; y++) {
var source = sstart & 0xffff;
var dest = dstart & 0xffff;
for (var x = 0; x < w; x++) {
var data = memread_williams(source);
if (flags & 0x20) {
pixdata = (pixdata << 8) | data;
blit_pixel(dest, (pixdata >> 4) & 0xff, flags);
} else {
blit_pixel(dest, data, flags);
}
source += sxinc;
source &= 0xffff;
dest += dxinc;
dest &= 0xffff;
}
if (flags & 0x2)
dstart = (dstart & 0xff00) | ((dstart + dyinc) & 0xff);
else
dstart += dyinc;
if (flags & 0x1)
sstart = (sstart & 0xff00) | ((sstart + syinc) & 0xff);
else
sstart += syinc;
}
return w * h * (2 + ((flags & 0x4) >> 2)); // # of memory accesses
2017-01-20 22:54:02 +00:00
}
function blit_pixel(dstaddr, srcdata, flags) {
var curpix = dstaddr < 0xc000 ? ram.mem[dstaddr] : memread_williams(dstaddr);
var solid = blitregs[1];
var keepmask = 0xff; //what part of original dst byte should be kept, based on NO_EVEN and NO_ODD flags
//even pixel (D7-D4)
if ((flags & 0x8) && !(srcdata & 0xf0)) { //FG only and src even pixel=0
if (flags & 0x80) keepmask &= 0x0f; // no even
2017-01-20 22:54:02 +00:00
} else {
if (!(flags & 0x80)) keepmask &= 0x0f; // not no even
2017-01-20 22:54:02 +00:00
}
//odd pixel (D3-D0)
if ((flags & 0x8) && !(srcdata & 0x0f)) { //FG only and src odd pixel=0
if (flags & 0x40) keepmask &= 0xf0; // no odd
2017-01-20 22:54:02 +00:00
} else {
if (!(flags & 0x40)) keepmask &= 0xf0; // not no odd
2017-01-20 22:54:02 +00:00
}
curpix &= keepmask;
if (flags & 0x10) // solid bit
2017-01-20 22:54:02 +00:00
curpix |= (solid & ~keepmask);
else
curpix |= (srcdata & ~keepmask);
if (dstaddr < 0x9800) // can cause recursion otherwise
2017-01-29 21:06:05 +00:00
memwrite_williams(dstaddr, curpix);
2017-01-20 22:54:02 +00:00
}
// TODO
/*
var trace = false;
var _traceinsns = {};
function _trace() {
var pc = cpu.getPC();
if (!_traceinsns[pc]) {
_traceinsns[pc] = 1;
console.log(hex(pc), cpu.getTstates());
}
2017-01-20 22:54:02 +00:00
}
*/
2017-01-20 03:42:58 +00:00
this.start = function() {
ram = new RAM(0xc000);
2017-01-20 22:54:02 +00:00
nvram = new RAM(0x400);
rom = new Uint8Array(0xc000);
2017-01-20 03:42:58 +00:00
// TODO: save in browser storage?
//displayPCs = new Uint16Array(new ArrayBuffer(0x9800*2));
//rom = padBytes(new lzgmini().decode(ROBOTRON_ROM).slice(0), 0xc001);
2017-01-20 03:42:58 +00:00
membus = {
2017-01-20 22:54:02 +00:00
read: memread_williams,
write: memwrite_williams,
2017-01-20 03:42:58 +00:00
};
this.readAddress = membus.read;
var iobus = {
read: function(a) { return 0; },
write: function(a, v) { console.log(hex(a), hex(v)); }
}
cpu = self.newCPU(membus, iobus);
2017-04-02 18:54:51 +00:00
audio = new MasterAudio();
worker = new Worker("./src/common/audio/z80worker.js");
workerchannel = new WorkerSoundChannel(worker);
2017-04-02 18:54:51 +00:00
audio.master.addChannel(workerchannel);
video = new RasterVideo(mainElement, SCREEN_WIDTH, SCREEN_HEIGHT, { rotate: -90 });
2017-01-20 03:42:58 +00:00
video.create();
$(video.canvas).click(function(e) {
var x = Math.floor(e.offsetX * video.canvas.width / $(video.canvas).width());
var y = Math.floor(e.offsetY * video.canvas.height / $(video.canvas).height());
var addr = (x >> 3) + (y * 32) + 0x400;
if (displayPCs) console.log(x, y, hex(addr, 4), "PC", hex(displayPCs[addr], 4));
});
2017-01-20 03:42:58 +00:00
var idata = video.getFrameData();
2017-01-21 13:13:36 +00:00
setKeyboardFromMap(video, pia6821, ROBOTRON_KEYCODE_MAP);
2017-01-20 03:42:58 +00:00
pixels = video.getFrameData();
timer = new AnimationTimer(60, this.nextFrame.bind(this));
}
2019-05-19 22:18:41 +00:00
this.getRasterScanline = function() { return video_counter; }
this.advance = function(novideo: boolean) {
var cpuCyclesPerSection = Math.round(cpuCyclesPerFrame / 65);
for (var sl = 0; sl < 256; sl += 4) {
video_counter = sl;
// interrupts happen every 1/4 of the screen
if (sl == 0 || sl == 0x3c || sl == 0xbc || sl == 0xfc) {
if (membus.read != memread_defender || pia6821[7] == 0x3c) { // TODO?
if (cpu.interrupt)
cpu.interrupt();
if (cpu.requestInterrupt)
cpu.requestInterrupt();
2017-01-20 03:42:58 +00:00
}
}
this.runCPU(cpu, cpuCyclesPerSection);
if (sl < 256) video.updateFrame(0, 0, 256 - 4 - sl, 0, 4, 304);
}
// last 6 lines
this.runCPU(cpu, cpuCyclesPerSection * 2);
if (screenNeedsRefresh && !novideo) {
for (var i = 0; i < 0x9800; i++)
drawDisplayByte(i, ram.mem[i]);
screenNeedsRefresh = false;
}
if (watchdog_enabled && watchdog_counter-- <= 0) {
console.log("WATCHDOG FIRED, PC =", cpu.getPC().toString(16)); // TODO: alert on video
// TODO: this.breakpointHit(cpu.T());
this.reset();
}
2017-01-20 03:42:58 +00:00
}
2017-04-06 14:28:51 +00:00
this.loadSoundROM = function(data) {
2017-04-14 22:43:07 +00:00
console.log("loading sound ROM " + data.length + " bytes");
2017-04-06 14:28:51 +00:00
var soundrom = padBytes(data, 0x4000);
worker.postMessage({ rom: soundrom });
2017-04-06 14:28:51 +00:00
}
2017-01-20 03:42:58 +00:00
this.loadROM = function(title, data) {
2017-01-29 21:06:05 +00:00
if (data.length > 2) {
2017-04-06 14:28:51 +00:00
if (data.length > 0xc000) {
self.loadSoundROM(data.slice(0xc000));
2017-04-14 22:43:07 +00:00
rom = rom.slice(0, 0xc000);
2017-04-06 14:28:51 +00:00
}
2017-04-14 22:43:07 +00:00
else if (data.length > 0x9000 && data[0x9000]) {
self.loadSoundROM(data.slice(0x9000));
}
rom = padBytes(data, 0xc000);
2017-01-29 21:06:05 +00:00
}
2017-01-20 03:42:58 +00:00
self.reset();
}
this.loadState = function(state) {
cpu.loadState(state.c);
ram.mem.set(state.b);
nvram.mem.set(state.nvram);
2018-08-23 12:49:14 +00:00
pia6821.set(state.pia);
2017-01-20 22:54:02 +00:00
blitregs.set(state.blt);
2017-01-20 03:42:58 +00:00
watchdog_counter = state.wdc;
banksel = state.bs;
2017-01-20 22:54:02 +00:00
portsel = state.ps;
2017-01-20 03:42:58 +00:00
}
this.saveState = function() {
return {
c: self.getCPUState(),
b: ram.mem.slice(0),
nvram: nvram.mem.slice(0),
pia: pia6821.slice(0),
blt: blitregs.slice(0),
wdc: watchdog_counter,
bs: banksel,
ps: portsel,
2017-01-20 03:42:58 +00:00
};
}
2018-08-23 12:49:14 +00:00
this.loadControlsState = function(state) {
pia6821.set(state.pia);
}
this.saveControlsState = function() {
return {
pia: pia6821.slice(0),
2018-08-23 12:49:14 +00:00
};
}
2017-01-20 03:42:58 +00:00
this.getCPUState = function() {
return cpu.saveState();
}
this.isRunning = function() {
2017-04-19 01:18:53 +00:00
return timer && timer.isRunning();
2017-01-20 03:42:58 +00:00
}
this.pause = function() {
timer.stop();
2017-04-02 18:54:51 +00:00
audio.stop();
2017-01-20 03:42:58 +00:00
}
this.resume = function() {
timer.start();
2017-04-02 18:54:51 +00:00
audio.start();
2017-01-20 03:42:58 +00:00
}
this.reset = function() {
cpu.reset();
watchdog_counter = INITIAL_WATCHDOG;
2017-01-29 21:06:05 +00:00
banksel = 1;
2017-01-20 03:42:58 +00:00
}
this.scaleCPUFrequency = function(scale) {
cpuScale = scale;
cpuFrequency *= scale;
cpuCyclesPerFrame *= scale;
}
2019-08-27 16:12:56 +00:00
this.getMemoryMap = function() { return { main:[
{name:'Video RAM',start:0x0000,size:0xc000,type:'ram'},
{name:'I/O Registers',start:0xc000,size:0x1000,type:'io'},
] } };
2017-01-20 03:42:58 +00:00
}
2017-01-29 21:06:05 +00:00
var WilliamsZ80Platform = function(mainElement) {
this.__proto__ = new WilliamsPlatform(mainElement, BaseZ80Platform);
2017-02-01 18:21:17 +00:00
// Z80 @ 4 MHz
// also scale bitblt clocks
this.scaleCPUFrequency(4);
2017-02-01 18:21:17 +00:00
this.ramStateToLongString = function(state) {
var blt = state.blt;
var sstart = (blt[2] << 8) + blt[3];
var dstart = (blt[4] << 8) + blt[5];
var w = blt[6] ^ 4; // blitter bug fix
var h = blt[7] ^ 4;
return "\nBLIT"
+ " " + hex(sstart, 4) + " " + hex(dstart, 4)
2017-02-01 18:21:17 +00:00
+ " w:" + hex(w) + " h:" + hex(h)
+ " f:" + hex(blt[0]) + " s:" + hex(blt[1]);
}
2017-01-29 21:06:05 +00:00
}
2017-01-20 03:42:58 +00:00
PLATFORMS['williams'] = WilliamsPlatform;
2017-01-29 21:06:05 +00:00
PLATFORMS['williams-z80'] = WilliamsZ80Platform;
// http://seanriddle.com/willhard.html