Skip to content

Commit 17c35a2

Browse files
squash commit:
94c3c3df68e3fa89774a86002d2e94c43b834ef7 WIP 6a49a45f81f88561aa83cfe7242a3e18cf374d08 more investigations f3f8200cb66cdbf4732c5399a5849e938fd1d485 more investigations
1 parent 3353890 commit 17c35a2

File tree

1 file changed

+232
-0
lines changed

1 file changed

+232
-0
lines changed

tests/infrastructure/debugger.lua

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
--[[ BEGINCONFIG ========================================
2+
3+
MODEL="apple2ee"
4+
MODELARGS="-sl2 mouse -sl7 cffa2 -aux ext80 -debug -debugger none"
5+
DISKARGS="-hard1 $HARDIMG"
6+
7+
======================================== ENDCONFIG ]]--
8+
9+
--[[
10+
Notes:
11+
12+
'-debug' enables the debugger
13+
'-debugger none' doesn't show an actual debugger window, really runs
14+
15+
Useful links:
16+
17+
- https://www.mamecheat.co.uk/forums/viewtopic.php?t=13285
18+
-- covers issues with bpset/wpset
19+
20+
- https://www.reddit.com/r/MAME/comments/9n93sw/mame_lua_help_noticing_a_specific_pc_in_the_code/
21+
-- discussion about this very issue
22+
23+
- https://github.com/mamedev/mame/blob/master/plugins/cheat/init.lua#L267
24+
-- pointed to by above - real code that works
25+
26+
27+
- https://github.com/mamedev/mame/issues/2451#issuecomment-317299876
28+
-- "The none debugger can't be used with the lua console for breakpoints because it will always immediately resume when stopped. Non-interactive scripts should be okay because emu.register_periodic is always called."
29+
-- More precisely, the breakpoint doesn't stop; you can still tell that it was hit, which is enough for applications such as determining that control has returned to an idle loop, etc.
30+
31+
]]--
32+
33+
--[[
34+
local debugger = manager.machine.debugger
35+
local cpu = manager.machine.devices[":maincpu"]
36+
37+
local mem = cpu.spaces["program"] -- only needed for watchpoints
38+
-- TODO: aux memory on IIe?
39+
40+
-- print("setting watchpoint")
41+
-- cpu.debug:wpset(mem, "rw", 0x4000, 1, '1', 'printf "Read @ %08X\n",wpaddr ; g')
42+
43+
-- TODO: Build abstraction allowing registering a BP/WP callback
44+
-- TODO: ... and ensure it handles banking configuration
45+
46+
local MAIN_LOOP1 = 0x4054 -- main event input loop
47+
local MAIN_LOOP2 = 0x405A -- tight loop if no event
48+
49+
local id = cpu.debug:bpset(MAIN_LOOP1, '1', '') -- all arguments are required or we crash!
50+
--bp = cpu.debug:bplist()[id] -- example
51+
52+
-- TODO: Add WP for tick_counter (4105...4107)
53+
--id = cpu.debug:wpset(mem, "rw", 0x4106, 1, '1', 'g') -- makes everything stall?
54+
--id = cpu.debug:wpset(mem, "rw", 0x4106, 1, '1', '') -- crashes
55+
--id = cpu.debug:wpset(mem, "rw", 0x4106, 1) -- crashes w/o args
56+
--wp = cpu.debug:wplist()[id] -- example
57+
58+
function describe_bp(bp)
59+
print("bp index: " .. bp.index)
60+
print(" enabled: " .. (bp.enabled and "yes" or "no"))
61+
print(" addr: " .. string.format("%04X", bp.address))
62+
print(" cond: " .. bp.condition)
63+
print(" action: " .. bp.action)
64+
end
65+
66+
function describe_wp(wp)
67+
print("wp index: " .. wp.index)
68+
print(" enabled: " .. (wp.enabled and "yes" or "no"))
69+
print(" type: " .. wp.type)
70+
print(" addr: " .. string.format("%04X", bp.address))
71+
print(" len: " .. wp.length)
72+
print(" cond: " .. wp.condition)
73+
print(" action: " .. wp.action)
74+
end
75+
76+
function describe_cpu()
77+
print("CPU")
78+
print(" PC: " .. string.format("%04X", cpu.state.PC.value))
79+
print(" A: " .. string.format("%02X", cpu.state.A.value))
80+
print(" X: " .. string.format("%02X", cpu.state.X.value))
81+
print(" Y: " .. string.format("%02X", cpu.state.Y.value))
82+
-- TODO: Cycle count? Use emulation time as a proxy
83+
print(" time: " .. manager.machine.time.usec .. "usec")
84+
85+
print("MMU")
86+
print(" RDRAMRD: " .. ((apple2.ReadSSW("RDRAMRD")>127) and "on" or "off"))
87+
end
88+
89+
90+
91+
-- Register callback, called when a frame is drawn or there's a debug break
92+
93+
local count = 0
94+
95+
local last = 0
96+
local consolelog = debugger.consolelog -- alias
97+
emu.register_periodic(function()
98+
if #consolelog == last then
99+
return
100+
end
101+
102+
last = #consolelog
103+
local msg = consolelog[#consolelog]
104+
if msg:find("Stopped at", 1, true) then
105+
last = #consolelog
106+
107+
-- Ignore aux for now
108+
if apple2.ReadSSW("RDRAMRD") > 127 then
109+
return
110+
end
111+
112+
print("")
113+
print("------------------------------")
114+
115+
116+
describe_cpu()
117+
118+
local index
119+
index = tonumber(msg:match("Stopped at breakpoint ([0-9]+)"))
120+
if index then
121+
print("stopped at bp")
122+
local bp = cpu.debug:bplist()[index]
123+
describe_bp(bp)
124+
125+
126+
local ticks = apple2.ReadRAMDevice(0x4105) +
127+
(apple2.ReadRAMDevice(0x4106)<<8) +
128+
(apple2.ReadRAMDevice(0x4107)<<16)
129+
print(" >>> ticks " .. ticks)
130+
count = count+1
131+
print(" >>> count " .. count)
132+
if count > 4 then
133+
print "disabling bp after 10"
134+
cpu.debug:bpdisable(index)
135+
end
136+
137+
-- If "-debugger none" is used, this is unnecessary.
138+
debugger.execution_state = "run"
139+
return
140+
end
141+
142+
index = tonumber(msg:match("Stopped at watchpoint ([0-9]+)"))
143+
if index then
144+
print("stopped at wp")
145+
local wp = cpu.debug:wplist()[index]
146+
describe_wp(wp)
147+
148+
-- If "-debugger none" is used, this is unnecessary.
149+
debugger.execution_state = "run"
150+
return
151+
end
152+
153+
print("unknown message: " .. msg)
154+
else
155+
print("ignoring '"..msg)
156+
end
157+
end)
158+
159+
160+
-- TODO: What to do here?
161+
emu.wait(60)
162+
163+
]]--
164+
165+
--[[
166+
167+
This is from a non-mainline branch, apparently:
168+
169+
https://github.com/mamedev/mame/issues/2451
170+
171+
172+
function callback1()
173+
print("callback1 ran")
174+
end
175+
176+
function callback2()
177+
print("callback2 ran")
178+
end
179+
180+
cpu = manager.machine.devices[":maincpu"]
181+
cpu:break_set(0x4000, callback1)
182+
cpu:break_set(0x2000, callback2)
183+
184+
]]--
185+
186+
-- Still requires '-debug -debugger none` so not sure if we want this
187+
188+
-- bank is "main" or "aux"
189+
local pc_waiter_bank = nil
190+
local pc_waiter_id = nil
191+
local pc_waiter_signal = nil
192+
function WaitForPC(address, bank)
193+
local cpu = manager.machine.devices[":maincpu"]
194+
pc_waiter_id = cpu.debug:bpset(address, '1', '')
195+
pc_waiter_bank = bank
196+
pc_waiter_signal = nil
197+
repeat
198+
emu.wait(1/60)
199+
until pc_waiter_signal
200+
end
201+
202+
function CurrentBank()
203+
end
204+
do
205+
local cpu = manager.machine.devices[":maincpu"]
206+
local consolelog = manager.machine.debugger.consolelog -- alias
207+
local last = 0
208+
emu.register_periodic(function()
209+
if #consolelog == last then
210+
return
211+
end
212+
last = #consolelog
213+
local index = tonumber(consolelog[#consolelog]:match("Stopped at breakpoint ([0-9]+)"))
214+
if index then
215+
local bank = (apple2.ReadSSW("RDRAMRD") > 127) and "aux" or "main"
216+
if index == pc_waiter_id and bank == pc_waiter_bank then
217+
cpu.debug:bpclear(pc_waiter_id)
218+
pc_waiter_id = nil
219+
pc_waiter_bank = nil
220+
pc_waiter_signal = true
221+
end
222+
end
223+
end)
224+
end
225+
226+
print("waiting...")
227+
WaitForPC(0x405A, "main")
228+
print("done!")
229+
230+
print("waiting...")
231+
WaitForPC(0x51B2, "main")
232+
print("done!")

0 commit comments

Comments
 (0)