refactor the logic of examine

This commit is contained in:
wixette 2020-03-07 01:37:05 +08:00
parent ae735688e6
commit c4b4e5d06d
2 changed files with 25 additions and 12 deletions

View File

@ -211,7 +211,7 @@ db ff d3 ff c3 00 00 // Switch echo between A2 and A1.
dumpMemElem.innerHTML = dumpHtml;
}
function getInputWord() {
function getInputAddressCallback() {
var word = 0;
for (let i = 0; i < 16; i++) {
var switchElem = document.getElementById('s' + i);
@ -225,6 +225,7 @@ db ff d3 ff c3 00 00 // Switch echo between A2 and A1.
var sim = new Sim8800(256, 1000000,
setAddressLedsCallback, setDataLedsCallback,
setWaitLedCallback, setStatusLedsCallback,
getInputAddressCallback,
dumpCpuCallback, dumpMemCallback);
function powerOn() {
@ -257,8 +258,7 @@ db ff d3 ff c3 00 00 // Switch echo between A2 and A1.
}
function examine() {
var address = getInputWord();
sim.examine(address);
sim.examine();
}
function examineNext() {

View File

@ -32,6 +32,8 @@ class Sim8800 {
* set the WAIT LED.
* @param {function(boolean)?} setStatusLedsCallback The callback to
* set the STATUS LEDs.
* @param {function():number?} getInputAddressCallback The
* callback to get the input word from address/data switches.
* @param {function(string)?} dumpCpuCallback The callback to receive
* CPU status dump, in HTML string.
* @param {function(string)?} dumpMemCallback The callback to receive
@ -40,6 +42,7 @@ class Sim8800 {
constructor(memSize, clockRate,
setAddressLedsCallback, setDataLedsCallback,
setWaitLedCallback, setStatusLedsCallback,
getInputAddressCallback,
dumpCpuCallback, dumpMemCallback) {
this.clockRate = clockRate;
this.mem = new Array(memSize);
@ -47,6 +50,7 @@ class Sim8800 {
this.setDataLedsCallback = setDataLedsCallback;
this.setWaitLedCallback = setWaitLedCallback;
this.setStatusLedsCallback = setStatusLedsCallback;
this.getInputAddressCallback = getInputAddressCallback;
this.dumpCpuCallback = dumpCpuCallback;
this.dumpMemCallback = dumpMemCallback;
this.isPoweredOn = false;
@ -377,23 +381,32 @@ class Sim8800 {
}
/**
* Reads a byte from the given address.
* @param {number} address The given address.
* Shows the address and the byte at the address via LEDs.
*/
examine(address) {
if (!this.isPoweredOn)
return;
this.lastAddress = address;
showAddressAndData() {
if (this.setAddressLedsCallback) {
let bits = Sim8800.parseBits(address, 16);
let bits = Sim8800.parseBits(this.lastAddress, 16);
this.setAddressLedsCallback(bits);
}
if (this.setDataLedsCallback) {
let bits = Sim8800.parseBits(this.mem[address], 8);
let bits = Sim8800.parseBits(this.mem[this.lastAddress], 8);
this.setDataLedsCallback(bits);
}
}
/**
* Reads a byte from the given address.
*/
examine() {
if (!this.isPoweredOn)
return;
if (this.getInputAddressCallback) {
var address = this.getInputAddressCallback();
this.lastAddress = address;
this.showAddressAndData();
}
}
/**
* Reads a byte from the next address.
*/
@ -401,6 +414,6 @@ class Sim8800 {
if (!this.isPoweredOn)
return;
this.lastAddress++;
this.examine(this.lastAddress);
this.showAddressAndData();
}
};