connected to the 8080 cpu simulator. also added the code to dump cpu status.

This commit is contained in:
Yonggang Wang 2020-03-06 13:06:47 +08:00
parent dd9c51125c
commit 41384d9842
2 changed files with 65 additions and 12 deletions

View File

@ -5,24 +5,19 @@
<body onload="test();"> <body onload="test();">
<h1>test</h1> <h1>test</h1>
<div id="dump"></div>
<div id="cpu"></div>
<div id="mem"></div>
<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 test() { function test() {
var sim = new Sim8800(256, 100000); var sim = new Sim8800(256, 100000);
var dumpElem = document.getElementById('dump'); var dumpCpuElem = document.getElementById('cpu');
sim.dumpMem(dumpElem); var dumpMemElem = document.getElementById('mem');
sim.dumpCpu(dumpCpuElem);
var writeFunc = sim.getWriteByteCallback(); sim.dumpMem(dumpMemElem);
var readFunc = sim.getReadByteCallback();
var i = readFunc(256);
writeFunc(256, 16);
i = readFunc(256);
sim.dumpMem(dumpElem);
} }
</script> </script>
</body> </body>

View File

@ -31,6 +31,11 @@ class Sim8800 {
/** @type {Array<number>} */ /** @type {Array<number>} */
this.mem = new Array(memSize); this.mem = new Array(memSize);
this.initMem(); this.initMem();
CPU8080.init(this.getWriteByteCallback(),
this.getReadByteCallback(),
null, /* not used. */
this.getWritePortCallback(),
this.getReadPortCallback());
} }
/** /**
@ -62,6 +67,38 @@ class Sim8800 {
}; };
} }
/**
* Returns the porto (write port) callback.
* @return {function(number, number)}
*/
getWritePortCallback() {
var self = this;
return function(address, value) {
window.console.log('writing port @' + Sim8800.toHex(address, 8)
+ ' : ' + Sim8800.toHex(value, 2));
if (address == 0xff) {
// We only care about port 0xff.
}
};
}
/**
* Returns the byteAt (read memory) callback.
* @return {function(number): number}
*/
getReadPortCallback() {
var self = this;
return function(address) {
var value = 0;
if (address == 0xff) {
// We only care about port 0xff.
}
window.console.log('reading port @' + Sim8800.toHex(address, 8)
+ ' : ' + Sim8800.toHex(value, 2));
return value;
};
}
/** /**
* Formats a number to fixed length hex string. * Formats a number to fixed length hex string.
* @param {number} n The number to be formatted. * @param {number} n The number to be formatted.
@ -101,4 +138,25 @@ class Sim8800 {
sb.push('</pre>\n'); sb.push('</pre>\n');
containerElem.innerHTML = sb.join(''); containerElem.innerHTML = sb.join('');
} }
/**
* Dumps the internal CPU status to HTML, for debugging or mornitoring.
* @param {Element} containerElem The DOM element to hold the generated HTML.
*/
dumpCpu(containerElem) {
var cpu = CPU8080.status();
var sb = ['<pre>\n'];
sb.push('PC = ' + Sim8800.toHex(cpu.pc, 4) + ' ');
sb.push('SP = ' + Sim8800.toHex(cpu.sp, 4) + '\n');
sb.push('A = ' + Sim8800.toHex(cpu.a, 2) + ' ');
sb.push('B = ' + Sim8800.toHex(cpu.b, 2) + ' ');
sb.push('C = ' + Sim8800.toHex(cpu.c, 2) + ' ');
sb.push('D = ' + Sim8800.toHex(cpu.d, 2) + '\n');
sb.push('E = ' + Sim8800.toHex(cpu.e, 2) + ' ');
sb.push('F = ' + Sim8800.toHex(cpu.f, 2) + ' ');
sb.push('H = ' + Sim8800.toHex(cpu.h, 2) + ' ');
sb.push('L = ' + Sim8800.toHex(cpu.l, 2) + '\n');
sb.push('</pre>\n');
containerElem.innerHTML = sb.join('');
}
}; };