Skip to content
Browse files

Move config into CPU.reset().

  • Loading branch information...
1 parent 733e23d commit 0120bd4da259eb4c1a44626825a48a313744c846 @nelhage committed May 30, 2011
Showing with 34 additions and 35 deletions.
  1. +15 −15 CPU.js
  2. +6 −7 beta.html
  3. +13 −13 main.js
View
30 CPU.js
@@ -169,9 +169,9 @@ var CPU = {
PC: 0,
halt: false,
pending_interrupts: 0,
- callback: null,
config: null,
clock: null,
+ run_timer: null,
key_buffer: [],
decode: function(op) {
@@ -184,16 +184,17 @@ var CPU = {
};
},
- reset: function() {
+ reset: function(config) {
var i;
+ CPU.config = config;
CPU.PC = ISR_RESET;
for (i = 0; i < 32; i ++)
CPU.regs[i] = 0;
CPU.halt = false;
CPU.pending_interrupts = 0;
CPU.callback = null;
- CPU.config = {};
CPU.clock = null;
+ CPU.run_timer = null;
CPU.key_buffer = [];
},
@@ -234,24 +235,23 @@ var CPU = {
}
},
+ pause: function() {
+ clearTimeout(CPU.run_timer);
+ if (CPU.clock)
+ clearInterval(CPU.clock);
+ CPU.run_timer = null;
+ CPU.clock = null;
+ },
+
run: function() {
- var cb, options = {};
- if (arguments.length == 2) {
- cb = arguments[1];
- options = arguments[0];
- } else {
- cb = arguments[0];
- }
- CPU.config = options;
- CPU.callback = cb;
- if (options.timer) {
+ if (CPU.config.timer) {
CPU.clock = setInterval(
function() {
debug("clock!");
CPU.pending_interrupts |= INT_CLK;
}, 10);
}
- setTimeout(CPU._run, 0);
+ CPU.run_timer = setTimeout(CPU._run, 0);
},
_run: function() {
@@ -261,7 +261,7 @@ var CPU = {
if (CPU.halt) {
if (CPU.clock)
clearInterval(CPU.clock);
- CPU.callback();
+ if (CPU.config.halt) CPU.config.halt();
} else {
setTimeout(CPU._run, 0);
}
View
13 beta.html
@@ -17,19 +17,18 @@
term.charMode = true;
term.open();
- MMU.load(litmus_rom);
- CPU.reset();
- CPU.run({timer: true,
+ MMU.load(lab8_rom);
+ CPU.reset({timer: true,
write: function(ch){
if (ch == 10)
term.newLine();
else
term.type(String.fromCharCode(ch));
- }
- },
- function() {
+ },
+ halt: function() {
term.type("--- Program terminated ----");
- });
+ }});
+ CPU.run();
}
function initTerm() {
View
26 main.js
@@ -46,19 +46,19 @@ if (process.argv.length < 3){
function (e, rom) {
if (e) throw e;
MMU.load(rom);
- CPU.reset();
- CPU.run({timer: true,
- write: writeChar},
- function () {
- console.log(sprintf.sprintf("[%08x] Done", norm(CPU.PC)));
- for (var i = 0; i < 32; i++) {
- process.stdout.write(sprintf.sprintf("[%02d] %08x ", i, norm(CPU.regs[i])));
- if (i % 4 == 3)
- process.stdout.write("\n");
- }
- tty.setRawMode(false);
- process.exit(0);
- });
+ CPU.reset({timer: true,
+ write: writeChar,
+ halt: function () {
+ console.log(sprintf.sprintf("[%08x] Done", norm(CPU.PC)));
+ for (var i = 0; i < 32; i++) {
+ process.stdout.write(sprintf.sprintf("[%02d] %08x ", i, norm(CPU.regs[i])));
+ if (i % 4 == 3)
+ process.stdout.write("\n");
+ }
+ tty.setRawMode(false);
+ process.exit(0);
+ }});
+ CPU.run();
});
tty.setRawMode(true);
process.stdin.resume();

0 comments on commit 0120bd4

Please sign in to comment.
Something went wrong with that request. Please try again.