mirror of
https://github.com/sehugg/8bitworkshop.git
synced 2024-06-19 17:29:32 +00:00
refactored into BaseWASMMachine/C64
This commit is contained in:
parent
4560a1b227
commit
e1772f2565
|
@ -954,7 +954,7 @@ import { Probeable, RasterFrameBased, AcceptsPaddleInput, SampledAudioSink } fro
|
||||||
import { SampledAudio } from "./audio";
|
import { SampledAudio } from "./audio";
|
||||||
import { ProbeRecorder } from "./recorder";
|
import { ProbeRecorder } from "./recorder";
|
||||||
|
|
||||||
interface Machine extends Bus, Resettable, FrameBased, AcceptsROM, HasCPU, SavesState<EmuState>, SavesInputState<any> {
|
export interface Machine extends Bus, Resettable, FrameBased, AcceptsROM, HasCPU, SavesState<EmuState>, SavesInputState<any> {
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasVideo(arg:any): arg is VideoSource {
|
function hasVideo(arg:any): arg is VideoSource {
|
||||||
|
@ -1154,28 +1154,25 @@ export abstract class BaseZ80MachinePlatform<T extends Machine> extends BaseMach
|
||||||
// WASM Support
|
// WASM Support
|
||||||
// TODO: detangle from c64
|
// TODO: detangle from c64
|
||||||
|
|
||||||
export class WASMMachine implements Machine {
|
export abstract class BaseWASMMachine {
|
||||||
|
|
||||||
prefix : string;
|
prefix : string;
|
||||||
instance : WebAssembly.Instance;
|
instance : WebAssembly.Instance;
|
||||||
exports : any;
|
exports : any;
|
||||||
sys : number;
|
sys : number;
|
||||||
pixel_dest : Uint32Array;
|
pixel_dest : Uint32Array;
|
||||||
pixel_src : Uint32Array;
|
pixel_src : Uint32Array;
|
||||||
romptr : number;
|
|
||||||
romlen : number;
|
|
||||||
romarr : Uint8Array;
|
|
||||||
stateptr : number;
|
stateptr : number;
|
||||||
statearr : Uint8Array;
|
statearr : Uint8Array;
|
||||||
cpustateptr : number;
|
cpustateptr : number;
|
||||||
cpustatearr : Uint8Array;
|
cpustatearr : Uint8Array;
|
||||||
cpu : CPU;
|
cpu : CPU;
|
||||||
|
romptr : number;
|
||||||
|
romlen : number;
|
||||||
|
romarr : Uint8Array;
|
||||||
audio : SampledAudioSink;
|
audio : SampledAudioSink;
|
||||||
audioarr : Float32Array;
|
audioarr : Float32Array;
|
||||||
prgstart : number;
|
|
||||||
initstring : string;
|
abstract getCPUState() : CpuState;
|
||||||
initindex : number;
|
|
||||||
joymask0 = 0;
|
|
||||||
|
|
||||||
constructor(prefix: string) {
|
constructor(prefix: string) {
|
||||||
this.prefix = prefix;
|
this.prefix = prefix;
|
||||||
|
@ -1228,21 +1225,6 @@ export class WASMMachine implements Machine {
|
||||||
// enable c64 joystick map to arrow keys (TODO)
|
// enable c64 joystick map to arrow keys (TODO)
|
||||||
//this.exports.c64_set_joystick_type(this.sys, 1);
|
//this.exports.c64_set_joystick_type(this.sys, 1);
|
||||||
}
|
}
|
||||||
reset() {
|
|
||||||
this.exports.machine_reset(this.sys);
|
|
||||||
// load rom
|
|
||||||
if (this.romptr && this.romlen) {
|
|
||||||
this.exports.machine_load_rom(this.sys, this.romptr, this.romlen);
|
|
||||||
this.prgstart = this.romarr[0] + (this.romarr[1]<<8); // TODO: get starting address
|
|
||||||
if (this.prgstart == 0x801) this.prgstart = 0x80d;
|
|
||||||
}
|
|
||||||
// set init string
|
|
||||||
// TODO: sometimes gets hung up
|
|
||||||
if (this.prgstart) {
|
|
||||||
this.initstring = "\r\r\r\r\r\r\r\r\r\r\rSYS " + this.prgstart + "\r";
|
|
||||||
this.initindex = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
getPC() : number {
|
getPC() : number {
|
||||||
return this.exports.machine_cpu_get_pc(this.sys);
|
return this.exports.machine_cpu_get_pc(this.sys);
|
||||||
}
|
}
|
||||||
|
@ -1261,31 +1243,8 @@ export class WASMMachine implements Machine {
|
||||||
this.romlen = rom.length;
|
this.romlen = rom.length;
|
||||||
this.reset();
|
this.reset();
|
||||||
}
|
}
|
||||||
advanceFrame(trap: TrapCondition) : number {
|
reset() {
|
||||||
var i : number;
|
this.exports.machine_reset(this.sys);
|
||||||
var cpf = 19656; // TODO: pal, const
|
|
||||||
if (trap) {
|
|
||||||
for (i=0; i<cpf; i++) {
|
|
||||||
if (trap()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
this.exports.machine_tick(this.sys);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.exports.machine_exec(this.sys, cpf);
|
|
||||||
i = cpf;
|
|
||||||
this.typeInitString(); // TODO: type init string into console (doesnt work on reset)
|
|
||||||
}
|
|
||||||
this.syncVideo();
|
|
||||||
this.syncAudio();
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
typeInitString() {
|
|
||||||
if (this.initstring) {
|
|
||||||
var ch = this.initstring.charCodeAt(this.initindex >> 1);
|
|
||||||
this.setKeyInput(ch, 0, (this.initindex&1) ? KeyFlags.KeyUp : KeyFlags.KeyDown);
|
|
||||||
if (++this.initindex >= this.initstring.length*2) this.initstring = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
read(address: number) : number {
|
read(address: number) : number {
|
||||||
return this.exports.machine_mem_read(this.sys, address & 0xffff);
|
return this.exports.machine_mem_read(this.sys, address & 0xffff);
|
||||||
|
@ -1296,53 +1255,6 @@ export class WASMMachine implements Machine {
|
||||||
write(address: number, value: number) : void {
|
write(address: number, value: number) : void {
|
||||||
this.exports.machine_mem_write(this.sys, address & 0xffff, value & 0xff);
|
this.exports.machine_mem_write(this.sys, address & 0xffff, value & 0xff);
|
||||||
}
|
}
|
||||||
getCPUState() {
|
|
||||||
this.exports.machine_save_cpu_state(this.sys, this.cpustateptr);
|
|
||||||
var s = this.cpustatearr;
|
|
||||||
var pc = s[2] + (s[3]<<8);
|
|
||||||
return {
|
|
||||||
PC:pc,
|
|
||||||
SP:s[9],
|
|
||||||
A:s[6],
|
|
||||||
X:s[7],
|
|
||||||
Y:s[8],
|
|
||||||
C:s[10] & 1,
|
|
||||||
Z:s[10] & 2,
|
|
||||||
I:s[10] & 4,
|
|
||||||
D:s[10] & 8,
|
|
||||||
V:s[10] & 64,
|
|
||||||
N:s[10] & 128,
|
|
||||||
o:this.readConst(pc),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
saveState() {
|
|
||||||
this.exports.machine_save_state(this.sys, this.stateptr);
|
|
||||||
/*
|
|
||||||
for (var i=0; i<this.statearr.length; i++)
|
|
||||||
if (this.statearr[i] == 0xa0 && this.statearr[i+1] == 0x4d && this.statearr[i+2] == 0xe2) console.log(hex(i));
|
|
||||||
*/
|
|
||||||
return {
|
|
||||||
c:this.getCPUState(),
|
|
||||||
state:this.statearr.slice(0),
|
|
||||||
ram:this.statearr.slice(18640, 18640+0x200), // ZP and stack
|
|
||||||
};
|
|
||||||
}
|
|
||||||
loadState(state) : void {
|
|
||||||
this.statearr.set(state.state);
|
|
||||||
this.exports.machine_load_state(this.sys, this.stateptr);
|
|
||||||
}
|
|
||||||
// assume controls buffer is smaller than cpu buffer
|
|
||||||
saveControlsState() : any {
|
|
||||||
this.exports.machine_save_controls_state(this.sys, this.cpustateptr);
|
|
||||||
return { controls:this.cpustatearr.slice(0, this.exports.machine_get_controls_state_size()) }
|
|
||||||
}
|
|
||||||
loadControlsState(state) : void {
|
|
||||||
this.cpustatearr.set(state.controls);
|
|
||||||
this.exports.machine_load_controls_state(this.sys, this.cpustateptr);
|
|
||||||
}
|
|
||||||
getVideoParams() {
|
|
||||||
return {width:392, height:272, overscan:true, videoFrequency:50}; // TODO: const
|
|
||||||
}
|
|
||||||
getAudioParams() {
|
getAudioParams() {
|
||||||
return {sampleRate:44100, stereo:false};
|
return {sampleRate:44100, stereo:false};
|
||||||
}
|
}
|
||||||
|
@ -1358,25 +1270,14 @@ export class WASMMachine implements Machine {
|
||||||
this.pixel_dest.set(this.pixel_src);
|
this.pixel_dest.set(this.pixel_src);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setKeyInput(key: number, code: number, flags: number): void {
|
// assume controls buffer is smaller than cpu buffer
|
||||||
// TODO: handle shifted keys
|
saveControlsState() : any {
|
||||||
if (key == 16 || key == 17 || key == 18 || key == 224) return; // meta keys
|
this.exports.machine_save_controls_state(this.sys, this.cpustateptr);
|
||||||
//console.log(key, code, flags);
|
return { controls:this.cpustatearr.slice(0, this.exports.machine_get_controls_state_size()) }
|
||||||
//if (flags & KeyFlags.Shift) { key += 64; }
|
}
|
||||||
// convert to c64
|
loadControlsState(state) : void {
|
||||||
var mask = 0;
|
this.cpustatearr.set(state.controls);
|
||||||
if (key == 37) { key = 0x8; mask = 0x4; } // LEFT
|
this.exports.machine_load_controls_state(this.sys, this.cpustateptr);
|
||||||
if (key == 38) { key = 0xb; mask = 0x1; } // UP
|
|
||||||
if (key == 39) { key = 0x9; mask = 0x8; } // RIGHT
|
|
||||||
if (key == 40) { key = 0xa; mask = 0x2; } // DOWN
|
|
||||||
if (flags & KeyFlags.KeyDown) {
|
|
||||||
this.exports.machine_key_down(this.sys, key);
|
|
||||||
this.joymask0 |= mask;
|
|
||||||
} else if (flags & KeyFlags.KeyUp) {
|
|
||||||
this.exports.machine_key_up(this.sys, key);
|
|
||||||
this.joymask0 &= ~mask;
|
|
||||||
}
|
|
||||||
this.exports.c64_joystick(this.sys, this.joymask0, 0); // TODO: c64
|
|
||||||
}
|
}
|
||||||
connectAudio(audio : SampledAudioSink) : void {
|
connectAudio(audio : SampledAudioSink) : void {
|
||||||
this.audio = audio;
|
this.audio = audio;
|
||||||
|
@ -1389,4 +1290,22 @@ export class WASMMachine implements Machine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
advanceFrameClock(trap, cpf:number) : number {
|
||||||
|
var i : number;
|
||||||
|
if (trap) {
|
||||||
|
for (i=0; i<cpf; i++) {
|
||||||
|
if (trap()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
this.exports.machine_tick(this.sys);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.exports.machine_exec(this.sys, cpf);
|
||||||
|
i = cpf;
|
||||||
|
}
|
||||||
|
this.syncVideo();
|
||||||
|
this.syncAudio();
|
||||||
|
return i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
import { C64 } from "../machine/c64";
|
import { C64, C64_WASMMachine } from "../machine/c64";
|
||||||
import { Platform, WASMMachine, Base6502MachinePlatform, getToolForFilename_6502, getOpcodeMetadata_6502 } from "../common/baseplatform";
|
import { Platform, Base6502MachinePlatform, getToolForFilename_6502, getOpcodeMetadata_6502 } from "../common/baseplatform";
|
||||||
import { PLATFORMS } from "../common/emu";
|
import { PLATFORMS } from "../common/emu";
|
||||||
|
|
||||||
const C64_PRESETS = [
|
const C64_PRESETS = [
|
||||||
|
@ -35,9 +35,9 @@ class C64Platform extends Base6502MachinePlatform<C64> implements Platform {
|
||||||
getMemoryMap() { return C64_MEMORY_MAP; }
|
getMemoryMap() { return C64_MEMORY_MAP; }
|
||||||
}
|
}
|
||||||
|
|
||||||
class C64WASMPlatform extends Base6502MachinePlatform<WASMMachine> implements Platform {
|
class C64WASMPlatform extends Base6502MachinePlatform<C64_WASMMachine> implements Platform {
|
||||||
|
|
||||||
newMachine() { return new WASMMachine('c64'); }
|
newMachine() { return new C64_WASMMachine('c64'); }
|
||||||
|
|
||||||
async start() {
|
async start() {
|
||||||
// TODO: start() needs to block
|
// TODO: start() needs to block
|
||||||
|
|
Loading…
Reference in New Issue
Block a user