New example code, highlighting peek / poke, run btn detects code changes

This commit is contained in:
2024-05-03 23:47:57 -05:00
parent 1253653203
commit 3f1b8fd316
3 changed files with 18 additions and 5 deletions
+12 -2
View File
@@ -9,10 +9,20 @@
<body>
<div class="simulator">
<pre>
// Welcome! Press [run] to run this, or write your own!
// User manual coming soon
// -- ross.andrews@gmail.com
const str = "Hello, world!";
fn main() {
repeat(1200) n {
poke(0x10000 + 1200 + n, 0x6f);
poke(0x10000 + n, 0);
}
var n = 0;
while (n < 40 * 30) {
asm(72, n + 0x10000) { store }
while(peek(str + n)) {
poke(0x10000 + 41 + n, peek(str + n));
n = n + 1;
}
}
+1 -1
View File
@@ -16,7 +16,7 @@ export default {
if (word = stream.match(/^[a-zA-Z_$][a-zA-Z0-9_$]*/)) {
// This is either a keyword or an identifier.
// All the keywords are valid names, so, check that:
if (word[0].match(/^fn|var|new|static|asm|return|if|while|repeat|global|const$/)) {
if (word[0].match(/^fn|var|new|static|asm|return|if|while|repeat|global|const|peek|poke$/)) {
if (word[0] === 'asm') {
state.asm = true
} // This is the start of an asm control structure
+5 -2
View File
@@ -11,6 +11,7 @@ export default function({ src: defaultSrc }) {
const [cpu, setCpu] = useState(() => new WasmCPU()) // The actual CPU emulator
const [errors, setErrors] = useState(null) // What's displayed on the compile errors tab
const [status, setStatus] = useState('') // The contents of the status bar
const [stale, setStale] = useState(false) // Whether the editor has been changed since the last build
// Whether the emulator should be running. Has to be a ref because the CPU setTimeout loop won't ever see changes in it otherwise
const running = useRef(false)
@@ -59,6 +60,7 @@ export default function({ src: defaultSrc }) {
// When we update the src, also update the key in localStorage
const updateSrc = useCallback((newSrc) => {
setSrc(newSrc)
setStale(true)
window.localStorage.setItem(currentFile, newSrc)
}, [currentFile])
@@ -71,6 +73,7 @@ export default function({ src: defaultSrc }) {
setBinary(bin) // Store that
setStatus(`Compiled ${bin.length} bytes`) // Success!
setErrors(null) // Clear the old error messages off the tab
setStale(false)
return { bin, errors: null }
} catch (err) {
if (err.message) { err = err.message } // How we get this differs between forge and asm
@@ -84,7 +87,7 @@ export default function({ src: defaultSrc }) {
// Callback for the run button
const run = useCallback(() => {
let bin = binary
if (!bin) {
if (!bin || stale) {
const result = build()
if (result.errors) { return }
else { bin = result.bin }
@@ -105,7 +108,7 @@ export default function({ src: defaultSrc }) {
}
}
setTimeout(time_slice, 0)
}, [cpu, binary, running, build])
}, [cpu, binary, running, build, stale])
const reset = useCallback(() => {
running.current = false