honeylisp/link/mame.fnl

141 lines
5.5 KiB
Plaintext
Raw Normal View History

2020-11-08 20:36:38 +00:00
(local util (require :lib.util))
(local {: spawn : kill} (util.require :lib.spawn))
(local nrepl (require :link.nrepl))
(local Session (require :link.nrepl-session))
(local bencode (require :lib.bencode))
(local lume (require :lib.lume))
(fn current-pluginspath []
(local f (io.popen "mame -showconfig" :r))
(var pluginspath "")
(each [line (f:lines)]
(local path (line:match "pluginspath%s+(.*)"))
(when path (set pluginspath path)))
pluginspath)
(local pluginspath (.. (current-pluginspath) ";" (love.filesystem.getWorkingDirectory) "/support/mame"))
2020-11-08 20:36:38 +00:00
(fn start-mame [platform]
(spawn [:mame :-debug :-plugins :-pluginspath pluginspath :-plugin :jeejah platform]))
2020-11-08 20:36:38 +00:00
(local Machine (Session:extend))
(fn Machine.new [self]
2020-11-10 03:58:57 +00:00
(Machine.super.new self {:out (fn []) :value (fn [])})
(set self.breakpoints {}))
2020-11-08 20:36:38 +00:00
(fn Machine.boot [self]
(when (not self.pid)
(set self.pid (start-mame :apple2p))))
(fn Machine.run [self]
(self:boot)
(self:connect))
(fn Machine.die [self]
(self:disconnect)
(when self.pid
(kill (- self.pid) 1)
(set self.pid nil)))
(fn Machine.running? [self] self.pid)
(fn Machine.connected? [self] self.session)
(fn Machine.connect [self]
(util.in-coro (fn []
(when (not (nrepl:connected?))
(util.waitfor
#(if (nrepl:connected?) true
(do (pcall #(nrepl:connect)) false))))
(self:init-session))))
2020-11-10 03:58:57 +00:00
(fn Machine.init-session [self]
(Machine.super.init-session self)
(set self.monitor (Session {:out (fn []) :value (fn [])}))
(self.monitor:init-session)
(self.monitor:eval
"(global periodic-jobs {})
(emu.register_periodic (fn []
(local dead-ids [])
(each [id coro (pairs periodic-jobs)]
(coroutine.resume coro)
(when (= (coroutine.status coro) :dead)
(table.insert dead-ids id)))
(each [_ id (ipairs dead-ids)]
(tset periodic-jobs id nil))))
(global run-periodic-job (fn [id f]
(tset periodic-jobs id (coroutine.running))
(f)
(tset periodic-jobs id nil)))")
(self:start-cpu-monitor)
2020-11-16 16:09:14 +00:00
(self:continue) ; debug mame starts paused
2020-11-10 03:58:57 +00:00
(self:done-msg))
(fn Machine.start-cpu-monitor [self ?last-addr]
(self.monitor:eval-input
"(let [?last-addr (tonumber (io.read))]
(run-periodic-job :cpu-monitor (fn []
(var last-addr ?last-addr)
(while (let [state (-> (manager:machine) (: :debugger) (. :execution_state))
addr (-> (manager:machine) (. :devices ::maincpu :state :PC :value))]
(not (and (= state :stop) (not= addr ?last-addr))))
(when (= :run (-> (manager:machine) (: :debugger) (. :execution_state)))
(set last-addr nil))
(coroutine.yield))))
(-> (manager:machine) (. :devices ::maincpu :state :PC :value)))"
(tostring ?last-addr)
{:value (fn [nrepl output response]
(self:on-cpu-paused (tonumber output))
(self:start-cpu-monitor (tonumber output)))}))
(fn Machine.on-cpu-paused [self addr]
(local action (-?> self.breakpoints (. addr) (. :action)))
(when action (action)))
2020-11-08 20:36:38 +00:00
(fn Machine.disconnect [self]
2020-11-10 03:58:57 +00:00
(self:shutdown-session)
(when self.monitor (self.monitor:shutdown-session))
2020-11-08 20:36:38 +00:00
(when (nrepl:connected?) (nrepl:disconnect))
2020-11-10 03:58:57 +00:00
(set self.breakpoints {}))
2020-11-08 20:36:38 +00:00
(fn Machine.write [self addr bytes]
2020-11-10 03:58:57 +00:00
(self:eval-input
2020-11-08 20:36:38 +00:00
"(let [bencode (require :bencode)
{: addr : bytes} (bencode.decode (io.read))
mem (-> (manager:machine) (. :devices ::maincpu :spaces :program))]
(print :writing (bytes:len) :to addr)
(for [i 1 (bytes:len)]
2020-11-10 03:58:57 +00:00
(mem:write_u8 (+ addr i -1) (string.byte (bytes:sub i i)))))"
(bencode.encode {: addr : bytes})))
(fn Machine.launch [self prg]
(self:eval (string.format "(emu.keypost \"CALL-151\\n %xG\\n\")" (prg:lookup-addr prg.start-symbol))))
2020-11-10 03:58:57 +00:00
(fn Machine.reboot [self] (self:eval "(: (manager:machine) :soft_reset)"))
2020-11-08 20:36:38 +00:00
(fn Machine.coro-eval [self code]
(var result nil)
2020-11-10 03:58:57 +00:00
(local append-to-result #(set result (.. (or result "") $2)))
2020-11-08 20:36:38 +00:00
(self:eval code
2020-11-10 03:58:57 +00:00
(self:coro-handlers (coroutine.running) {:value append-to-result :out append-to-result}))
2020-11-08 20:36:38 +00:00
(coroutine.yield)
2020-11-10 03:58:57 +00:00
(or result "<no result>"))
(fn Machine.dbgcmd [self cmd ?handlers]
(self:eval (.. "(-> (manager:machine) (: :debugger) (: :command \"" cmd "\"))")) ?handlers)
2020-11-08 20:36:38 +00:00
(fn Machine.continue [self] (self:dbgcmd :go))
(fn Machine.step [self] (self:dbgcmd :s))
2020-11-10 03:58:57 +00:00
(fn Machine.set-bp [self addr ?action]
; todo: handle setting the same breakpoint more than once?
(tset self.breakpoints addr {:action ?action})
(self:eval (.. "(-> (manager:machine) (. :devices ::maincpu) (: :debug) (: :bpset " (tostring addr) "))")
{:value #(tset (. self.breakpoints addr) :id (tonumber $2))}))
(fn Machine.clear-bp [self addr]
(local bpid (-?> self.breakpoints (. addr) (. :id)))
(when bpid (self:dbgcmd (.. "bpclear " (string.format "%x" bpid))))
2020-11-10 03:58:57 +00:00
(tset self.breakpoints addr nil))
(fn Machine.jump [self addr]
(self:eval (.. "(tset (-> (manager:machine) (. :devices ::maincpu :state :PC)) :value " (tostring addr) ")")))
(fn Machine.stub [self org post-check-jump ...]
(org:append :debug-stub [:jmp post-check-jump] :on-hotswap ...))
(fn Machine.hotswap [self prg-old prg-new]
(local addr (prg-old:lookup-addr :debug-stub))
(self:set-bp addr
(fn [] (self:clear-bp addr)
(prg-new:upload self)
(self:jump (prg-new:lookup-addr :on-hotswap))
(self:continue))))
2020-11-17 20:35:41 +00:00
(fn Machine.overlay [self prg-overlay]
(self:step)
(prg-overlay:upload self)
(self:continue))
2020-11-10 03:58:57 +00:00
2020-11-08 20:36:38 +00:00
(Machine:new)
Machine