callbacks to support update LEDs

This commit is contained in:
wixette 2020-03-06 23:20:38 +08:00
parent c76a8142b7
commit 55f1cebc6e
2 changed files with 43 additions and 13 deletions

View File

@ -75,13 +75,28 @@
<script src="js/8080.js"></script> <script src="js/8080.js"></script>
<script src="js/sim8800.js"></script> <script src="js/sim8800.js"></script>
<script> <script>
function setAddressLedsCallback(bits) {
for (let i = 0; i < bits.length; i++) {
var ledElem = document.getElementById('a' + i);
ledElem.innerHTML = bits[i] ? '&#x25cf' : '&#x25cb';
}
}
function setDataLedsCallback(bits) {
for (let i = 0; i < bits.length; i++) {
var ledElem = document.getElementById('d' + i);
ledElem.innerHTML = bits[i] ? '&#x25cf' : '&#x25cb';
}
}
var dumpCpuElem = document.getElementById('cpu'); var dumpCpuElem = document.getElementById('cpu');
var dumpMemElem = document.getElementById('mem'); var dumpMemElem = document.getElementById('mem');
var sim = new Sim8800(256, 1000000, var sim = new Sim8800(256, 1000000,
setAddressLedsCallback, setDataLedsCallback,
dumpCpuElem, dumpMemElem); dumpCpuElem, dumpMemElem);
function init() { function init() {
sim.loadDataAsHexString(0, 'db ff d3 ff c3 00 00'); sim.loadDataAsHexString(0, '00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f');
sim.loadData(16, [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]); sim.loadData(16, [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]);
sim.dumpCpu(); sim.dumpCpu();
sim.dumpMem() sim.dumpMem()
@ -94,7 +109,10 @@
} }
function run() { function run() {
sim.loadDataAsHexString(0, 'db ff d3 ff c3 00 00'); // Switch echo between A2 and A1.
// sim.loadDataAsHexString(0, 'db ff d3 ff c3 00 00');
// Pattern shift.
sim.loadDataAsHexString(0, '3e 8c d3 ff 0f c3 02 00');
sim.start(); sim.start();
sim.dumpCpu(); sim.dumpCpu();
sim.dumpMem() sim.dumpMem()

View File

@ -24,15 +24,22 @@ class Sim8800 {
/** /**
* @param {number} memSize The memory size, in bytes. * @param {number} memSize The memory size, in bytes.
* @param {number} clockRate The clock rate. * @param {number} clockRate The clock rate.
* @param {function(Array<number>)?} setAddressLedsCallback The
* callback to set address LEDs.
* @param {function(Array<number>)?} setDataLedsCallback The
* callback to set data LEDs.
* @param {Element?} dumpCpuElem The DOM element used to render * @param {Element?} dumpCpuElem The DOM element used to render
* dumped CPU status. null to disable the feature. * dumped CPU status. null to disable the feature.
* @param {Element?} dumpMemElem The DOM element used to render * @param {Element?} dumpMemElem The DOM element used to render
* dumped memory contents. null to disable the feature. * dumped memory contents. null to disable the feature.
*/ */
constructor(memSize, clockRate, constructor(memSize, clockRate,
setAddressLedsCallback, setDataLedsCallback,
dumpCpuElem, dumpMemElem) { dumpCpuElem, dumpMemElem) {
this.clockRate = clockRate; this.clockRate = clockRate;
this.mem = new Array(memSize); this.mem = new Array(memSize);
this.setAddressLedsCallback = setAddressLedsCallback;
this.setDataLedsCallback = setDataLedsCallback;
this.dumpCpuElem = dumpCpuElem; this.dumpCpuElem = dumpCpuElem;
this.dumpMemElem = dumpMemElem; this.dumpMemElem = dumpMemElem;
this.running = false; this.running = false;
@ -57,13 +64,13 @@ class Sim8800 {
/** /**
* Parses a number into an array of binary bits. * Parses a number into an array of binary bits.
* @param {number} data The data to be parsed. * @param {number} data The data to be parsed.
* @param {number} numBytes Number of bytes to be parsed. * @param {number} numBits Number of bits to be parsed.
* @return {Array<number>} Sequence of 0 or 1, from the lowest bit to * @return {Array<number>} Sequence of 0 or 1, from the lowest bit to
* the highest bit. * the highest bit.
*/ */
static parseBits(data, numBytes) { static parseBits(data, numBits) {
var bits = []; var bits = [];
for (let i = 0; i < numBytes * 8; i++) { for (let i = 0; i < numBits; i++) {
bits.push(data & 1 != 0 ? 1 : 0); bits.push(data & 1 != 0 ? 1 : 0);
data >>>= 1; data >>>= 1;
} }
@ -205,8 +212,9 @@ class Sim8800 {
getWritePortCallback() { getWritePortCallback() {
var self = this; var self = this;
return function(address, value) { return function(address, value) {
if (address == 0xff) { if (address == 0xff && self.setDataLedsCallback) {
// We only care about port 0xff. var bits = Sim8800.parseBits(value, 8);
self.setDataLedsCallback(bits);
} }
}; };
} }
@ -235,21 +243,25 @@ class Sim8800 {
return function(timestamp) { return function(timestamp) {
if (self.running) { if (self.running) {
var cycles = self.clockRate / 1000; var cycles = self.clockRate / 1000;
CPU8080.steps(cycles); self.step(cycles);
self.dumpCpu();
self.dumpMem();
window.setTimeout(self.getClockTickerCallback(), 1); window.setTimeout(self.getClockTickerCallback(), 1);
} }
}; };
} }
/** /**
* Runs a single CPU step. * Runs a given number of CPU cycles.
* @param {number} cycles The number of CPU cycles to step on.
*/ */
singleStep() { step(cycles) {
CPU8080.steps(1); CPU8080.steps(cycles);
this.dumpCpu(); this.dumpCpu();
this.dumpMem(); this.dumpMem();
if (this.setAddressLedsCallback) {
var cpu = CPU8080.status();
var pcBits = Sim8800.parseBits(cpu.pc, 16);
this.setAddressLedsCallback(pcBits);
}
} }
/** /**