Speed up hot reload

This commit is contained in:
Jeremy Penner 2021-05-15 20:52:06 -04:00
parent 1db882c150
commit 9e2f44166c
5 changed files with 56 additions and 21 deletions

Binary file not shown.

View file

@ -350,15 +350,19 @@
self) self)
:read-hotswap :read-hotswap
(fn [self machine] (fn [self machine]
(let [hotswap {}] (let [addr-to-label {}
addr-to-size {}]
(each [_ block (pairs self.org-to-block)] (each [_ block (pairs self.org-to-block)]
(each [label pdat (pairs block.preserved)] (each [label pdat (pairs block.preserved)]
(tset hotswap label (machine:read pdat.addr pdat.size)))) (tset addr-to-label pdat.addr label)
hotswap)) (tset addr-to-size pdat.addr pdat.size)))
(collect [addr bytes (pairs (machine:read-batch addr-to-size))]
(values (. addr-to-label addr) bytes))))
:write-hotswap :write-hotswap
(fn [self machine hotswap] (fn [self machine hotswap]
(each [label bytes (pairs hotswap)] (machine:write-batch
(machine:write (self:lookup-addr label) bytes))) (collect [label bytes (pairs hotswap)]
(values (self:lookup-addr label) bytes))))
:upload :upload
(fn [self machine] (fn [self machine]
(if machine.upload (machine:upload self) (if machine.upload (machine:upload self)

File diff suppressed because one or more lines are too long

View file

@ -88,19 +88,36 @@
(when (nrepl:connected?) (nrepl:disconnect)) (when (nrepl:connected?) (nrepl:disconnect))
(set self.breakpoints {})) (set self.breakpoints {}))
(fn Machine.read [self addr len] (fn Machine.read [self addr len]
(-> (self:coro-eval (var bytes nil)
(.. "(let [bencode (require :bencode) (self:coro-eval
addr " addr " "(let [bencode (require :bencode)
len " len " {: addr : len} (bencode.decode (io.read))
mem (. manager.machine.devices ::maincpu :spaces :program)] mem (. manager.machine.devices ::maincpu :spaces :program)]
(var bytes \"\") (var bytes \"\")
(print :reading len :from addr)
(for [i 1 len] (for [i 1 len]
(set bytes (.. bytes (string.char (mem:read_u8 (+ addr i -1)))))) (set bytes (.. bytes (string.char (mem:read_u8 (+ addr i -1))))))
bytes)")) (io.write bytes))"
; result is piped through fennelview; have to eval it to turn it back into bytes (lume.merge
; would be nice if io.write worked (self:input-handler (bencode.encode {: addr : len}))
(fennel.eval))) {:out #(set bytes $2)}))
bytes)
(fn Machine.read-batch [self addr-to-len]
(var addr-to-bytes nil)
(self:coro-eval
"(let [bencode (require :bencode)
addr-to-len (bencode.decode (io.read))
mem (. manager.machine.devices ::maincpu :spaces :program)
addr-to-bytes {}]
(each [addr len (pairs addr-to-len)]
(var bytes \"\")
(for [i 1 len]
(set bytes (.. bytes (string.char (mem:read_u8 (+ addr i -1))))))
(tset addr-to-bytes addr bytes))
(io.write (bencode.encode addr-to-bytes)))"
(lume.merge
(self:input-handler (bencode.encode addr-to-len))
{:out #(set addr-to-bytes (bencode.decode $2))}))
addr-to-bytes)
(fn Machine.write [self addr bytes] (fn Machine.write [self addr bytes]
(if (> (bytes:len) 0x1000) (if (> (bytes:len) 0x1000)
(do (self:write addr (bytes:sub 1 0x1000)) (do (self:write addr (bytes:sub 1 0x1000))
@ -113,6 +130,15 @@
(for [i 1 (bytes:len)] (for [i 1 (bytes:len)]
(mem:write_u8 (+ addr i -1) (string.byte (bytes:sub i i)))))" (mem:write_u8 (+ addr i -1) (string.byte (bytes:sub i i)))))"
(bencode.encode {: addr : bytes})))) (bencode.encode {: addr : bytes}))))
(fn Machine.write-batch [self addr-to-bytes]
(self:eval-input
"(let [bencode (require :bencode)
addr-to-bytes (bencode.decode (io.read))
mem (. manager.machine.devices ::maincpu :spaces :program)]
(each [addr bytes (pairs addr-to-bytes)]
(for [i 1 (bytes:len)]
(mem:write_u8 (+ addr i -1) (string.byte (bytes:sub i i))))))"
(bencode.encode addr-to-bytes)))
(fn Machine.launch [self prg] (fn Machine.launch [self prg]
(self:eval "(manager.machine:soft_reset)") (self:eval "(manager.machine:soft_reset)")
(self:eval (string.format "(emu.keypost \"CALL-151\\n %xG\\n\")" (prg:lookup-addr prg.start-symbol)))) (self:eval (string.format "(emu.keypost \"CALL-151\\n %xG\\n\")" (prg:lookup-addr prg.start-symbol))))
@ -121,7 +147,9 @@
(var result nil) (var result nil)
(local append-to-result #(set result (.. (or result "") $2))) (local append-to-result #(set result (.. (or result "") $2)))
(self:eval code (self:eval code
(self:coro-handlers (coroutine.running) {:value append-to-result :out append-to-result} ?handlers)) (self:coro-handlers (coroutine.running)
(lume.merge {:value append-to-result :out append-to-result}
(or ?handlers {}))))
(coroutine.yield) (coroutine.yield)
(or result "<no result>")) (or result "<no result>"))
(fn Machine.dbgcmd [self cmd ?handlers] (fn Machine.dbgcmd [self cmd ?handlers]

View file

@ -36,7 +36,7 @@ local make_repl = function(session, repls)
env.io = {} env.io = {}
end end
env.print = print_for(session.write) env.print = print_for(session.write)
env.io.write = session.write env.io.write = function(...) return session.io_write(...) end
env.io.read = function() env.io.read = function()
session.needinput() session.needinput()
local input, done = coroutine.yield() local input, done = coroutine.yield()
@ -63,6 +63,9 @@ return function(conn, msg, session, send, response_for)
session.values = function(xs) session.values = function(xs)
send(conn, response_for(msg, {value=table.concat(xs, "\n") .. "\n"})) send(conn, response_for(msg, {value=table.concat(xs, "\n") .. "\n"}))
end end
session.io_write = function(...)
send(conn, response_for(msg, {out=table.concat({...})}))
end
session.done = function() session.done = function()
send(conn, response_for(msg, {status={"done"}})) send(conn, response_for(msg, {status={"done"}}))
end end