From 173dc8201382a2aae06e26cade6a2e49282d9798 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sat, 11 Jul 2020 07:31:37 -0700 Subject: [PATCH] Start hooking up ui to code --- Makefile | 4 ++-- TODO | 1 - sample/sample.js | 2 +- ui.js | 34 +++++++++++++++++++++++++++++++--- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 3e189c9..916c703 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: lint test bundle +.PHONY: lint test build lint: # jshint uses .jshintignore and .jshintrc @@ -7,5 +7,5 @@ lint: test: npm test -bundle: +build: browserify src/bundleMain.js --s js8080sim -o js8080simBundle.js diff --git a/TODO b/TODO index dc8bdb2..091a92e 100644 --- a/TODO +++ b/TODO @@ -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: diff --git a/sample/sample.js b/sample/sample.js index e20d5d0..8d9f60c 100644 --- a/sample/sample.js +++ b/sample/sample.js @@ -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())}`); diff --git a/ui.js b/ui.js index 09216f7..b89ae00 100644 --- a/ui.js +++ b/ui.js @@ -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"; @@ -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]; }