Skip to content

Commit

Permalink
Start hooking up ui to code
Browse files Browse the repository at this point in the history
  • Loading branch information
eliben committed Jul 11, 2020
1 parent 8bc1fc5 commit 173dc82
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: lint test bundle
.PHONY: lint test build

lint:
# jshint uses .jshintignore and .jshintrc
Expand All @@ -7,5 +7,5 @@ lint:
test:
npm test

bundle:
build:
browserify src/bundleMain.js --s js8080sim -o js8080simBundle.js
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
working to avoid distraction.

* Create a UI similar to https://schweigi.github.io/assembler-simulator/
Use some sort of bundler to create a single-file JS for web?

Links:

Expand Down
2 changes: 1 addition & 1 deletion sample/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ CPU8080.set('PC', 0);

let N = 10000;

// TODO: note, 0x00 is NOPs, so it will just keep executing.
// note, 0x00 is NOPs, so it will just keep executing.
for (let i = 0; i < N; i++) {
CPU8080.steps(1);
console.log(`T=${CPU8080.T()}; status=${JSON.stringify(CPU8080.status())}`);
Expand Down
34 changes: 31 additions & 3 deletions ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ loadState();

document.querySelector("#run").addEventListener("mousedown", runCode);

// Expects the global js8080sim to be available.
let asm = new js8080sim.Assembler();

let st = document.querySelector("#status");
st.textContent = "SUCCESS";
st.style.color = "green";
Expand All @@ -26,6 +23,37 @@ function saveState() {
localStorage.setItem(STORAGE_ID, JSON.stringify(state));
}

// TODO: editing -- same whitespace offset as last line
function runCode() {
saveState();

let prog = document.querySelector('#codetext').value;
let [state, mem] = runProg(prog, 100);
console.log(state);
}

function runProg(progText, maxSteps) {
let p = new js8080sim.Parser();
let asm = new js8080sim.Assembler();
let sourceLines = p.parse(progText);
let mem = asm.assemble(sourceLines);

const memoryTo = (addr, value) => {mem[addr] = value;};
const memoryAt = (addr) => {return mem[addr];};
js8080sim.CPU8080.init(memoryTo, memoryAt);
js8080sim.CPU8080.set('PC', 0);

if (maxSteps === undefined) {
maxSteps = 50000;
}

for (let i = 0; i < maxSteps; i++) {
js8080sim.CPU8080.steps(1);

if (js8080sim.CPU8080.status().halted) {
break;
}
}

return [js8080sim.CPU8080.status(), mem];
}

0 comments on commit 173dc82

Please sign in to comment.