honeylisp/link/mame.fnl

145 lines
5.6 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)
2021-04-25 16:12:57 +00:00
(set self.pid (start-mame :apple2e))))
2020-11-08 20:36:38 +00:00
(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))
2020-11-10 03:58:57 +00:00
(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)]
2020-11-10 03:58:57 +00:00
(not (and (= state :stop) (not= addr ?last-addr))))
(when (= :run manager.machine.debugger.execution_state)
2020-11-10 03:58:57 +00:00
(set last-addr nil))
(coroutine.yield))))
(. manager.machine.devices ::maincpu :state :PC :value))"
2020-11-10 03:58:57 +00:00
(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]
(if (> (bytes:len) 0x1000)
(do (self:write addr (bytes:sub 1 0x1000))
(self:write (+ addr 0x1000) (bytes:sub 0x1001)))
(self:eval-input
"(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)]
(mem:write_u8 (+ addr i -1) (string.byte (bytes:sub i i)))))"
(bencode.encode {: addr : bytes}))))
2020-11-10 03:58:57 +00:00
(fn Machine.launch [self prg]
(self:eval "(manager.machine:soft_reset)")
(self:eval (string.format "(emu.keypost \"CALL-151\\n %xG\\n\")" (prg:lookup-addr prg.start-symbol))))
(fn Machine.reboot [self] (self:eval "(manager.machine:hard_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})
2021-01-14 01:56:51 +00:00
(self:eval (.. "(-> (. manager.machine.devices ::maincpu :debug) (: :bpset " (tostring addr) "))")
2020-11-10 03:58:57 +00:00
{: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 0x" (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) ")")))
2020-11-10 03:58:57 +00:00
(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