apple2js/js/apple2.js

190 lines
4.7 KiB
JavaScript
Raw Normal View History

import Apple2IO from './apple2io';
import { HiresPage, LoresPage, VideoModes } from './canvas';
import CPU6502 from './cpu6502';
import MMU from './mmu';
import RAM from './ram';
import { debug } from './util';
import SYMBOLS from './symbols';
export function Apple2(options) {
var stats = {
renderedFrames: 0
};
var kHz = 1023;
var paused = false;
var DEBUG = false;
var TRACE = false;
var MAX_TRACE = 256;
var trace = [];
var runTimer = null;
2019-12-28 02:14:33 +00:00
var runAnimationFrame = null;
var cpu = new CPU6502({ '65C02': options.enhanced });
var gr = new LoresPage(1, options.characterRom, options.e, options.screen[0]);
var gr2 = new LoresPage(2, options.characterRom, options.e, options.screen[1]);
var hgr = new HiresPage(1, options.screen[2]);
var hgr2 = new HiresPage(2, options.screen[3]);
var vm = new VideoModes(gr, hgr, gr2, hgr2, options.e);
vm.multiScreen(options.multiScreen);
2019-12-27 23:50:40 +00:00
vm.enhanced(options.enhanced);
var io = new Apple2IO(cpu, vm);
if (options.e) {
var mmu = new MMU(cpu, vm, gr, gr2, hgr, hgr2, io, options.rom);
cpu.addPageHandler(mmu);
} else {
var ram1 = new RAM(0x00, 0x03),
ram2 = new RAM(0x0C, 0x1F),
ram3 = new RAM(0x60, 0xBF);
cpu.addPageHandler(ram1);
cpu.addPageHandler(gr);
cpu.addPageHandler(gr2);
cpu.addPageHandler(ram2);
cpu.addPageHandler(hgr);
cpu.addPageHandler(hgr2);
cpu.addPageHandler(ram3);
cpu.addPageHandler(io);
cpu.addPageHandler(options.rom);
}
var _requestAnimationFrame =
window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame;
2019-12-28 02:14:33 +00:00
var _cancelAnimationFrame =
window.cancelAnimationFrame ||
window.mozCancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.msCancelAnimationFrame;
function run() {
2019-12-28 02:14:33 +00:00
if (runTimer || runAnimationFrame) {
return; // already running
}
var interval = 30;
var now, last = Date.now();
var runFn = function() {
now = Date.now();
var step = (now - last) * kHz, stepMax = kHz * interval;
last = now;
if (step > stepMax) {
step = stepMax;
}
if (DEBUG) {
cpu.stepCyclesDebug(TRACE ? 1 : step, function() {
var line = cpu.dumpRegisters() + ' ' +
cpu.dumpPC(undefined, SYMBOLS);
if (TRACE) {
debug(line);
} else {
trace.push(line);
if (trace.length > MAX_TRACE) {
trace.shift();
}
}
});
} else {
cpu.stepCycles(step);
}
if (io.annunciator(0)) {
if (options.multiScreen) {
vm.blit();
}
if (io.blit()) {
stats.renderedFrames++;
}
} else {
if (vm.blit()) {
stats.renderedFrames++;
}
}
io.tick();
options.tick();
if (!paused && _requestAnimationFrame) {
2019-12-28 02:14:33 +00:00
runAnimationFrame = _requestAnimationFrame(runFn);
}
};
if (_requestAnimationFrame) {
_requestAnimationFrame(runFn);
} else {
runTimer = setInterval(runFn, interval);
}
}
function stop() {
if (runTimer) {
clearInterval(runTimer);
}
2019-12-28 02:14:33 +00:00
if (runAnimationFrame) {
_cancelAnimationFrame(runAnimationFrame);
}
runTimer = null;
2019-12-28 02:14:33 +00:00
runAnimationFrame = null;
}
function saveState() {
var state = {
cpu: cpu.getState(),
};
return state;
}
function restoreState(state) {
cpu.setState(state.cpu);
}
return {
reset: function () {
cpu.reset();
},
run: function () {
run();
},
stop: function () {
stop();
},
saveState: function () {
saveState();
},
restoreState: function () {
restoreState();
},
getStats: function () {
return stats;
},
getCPU: function () {
return cpu;
},
getIO: function () {
return io;
},
getVideoModes: function () {
return vm;
}
};
}