-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.mrc
141 lines (101 loc) · 6.14 KB
/
debug.mrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
alias nes.debug.cpu {
var %cycles $+(72,$padstring(6, $hget(nes.cpu, cycles)).pre)
var %pc $+(41$,65,$base($1, 10, 16, 4))
var %opcode $+(4468,$2)
var %length $3
;; instructions are at least 1 byte long.
;; if not, well, it's just not implemented yet!
if (%length > 0) {
if ($hget(nes.cpu, debug) == full) {
var %mode $4
var %mnemonic $5
;; special handling for how to display the operand/result depending on length/mode
if (%mode == implicit) {
;; since implicit instructions have no operand or "result",
var %result %operand
}
elseif (%mode == immediate) {
;; immediate mode means the operand is a single byte direct value,
;; to be prefixed with #$, not to be confused with zeropage, which
;; is also a single byte operand but is displayed as an 8-bit address.
var %operand $+(57,$base($6, 10, 16, 2))
var %result $+(50#$,74,$base($6, 10, 16, 2))
}
elseif (%mode == absolute) {
;; this is an address. for display purposes, swap them bytes again.
var %operand $+(57,$base($6, 10, 16, 2)) $+(69,$base($7, 10, 16, 2))
var %result $+(50$,74,$+($hex($7),$hex($6)))
}
elseif (%mode == relative) {
;; if mode is relative, we'd rather display the result of the
;; instruction, so we can see where a branch ends up,
;; rather than the offset we may or not be adding/subtracting.
;; we're still keeping the original operand as well though,
;; just to keep things clear
var %operand $+(57,$base($6, 10, 16, 2))
var %result $+(50$,74,$base($7, 10, 16, 4))
}
elseif (%mode == zeropage) {
;; operands on single page operations are only 1 byte long.
var %operand $+(57,$base($6, 10, 16, 2))
var %result $+(50$,74,$base($6, 10, 16, 2))
}
elseif (%mode == indirect,y) {
;; this is an address. for display purposes, swap them bytes again.
var %operand $+(57,$base($6, 10, 16, 2))
var %result $+(50$,74,$base($7, 10, 16, 4),50,$chr(44),74,$hex($hget(nes.cpu, y)))
}
;; calculate n prettify execution time
var %ticks $+(96,$calc($ticksqpc - $hget(nes.cpu, ticks.instruction)),94ms) 91/96 $calc($hget(nes.cpu, ips) / 1000000)
;; prettify the status flag display
var %flags $replace($nes.cpu.statusFlags, 0, $+(30,0), 1, $+(66,1))
var %regs 85 $padString(2, $hex($hget(nes.cpu, accumulator))) $padString(2, $hex($hget(nes.cpu, x))) $padString(2, $hex($hget(nes.cpu, y)))
;; the big line that put da stuff on screen~
;; this is getting a bit unwieldy, lol
iline @nes.debug $line(@nes.debug, -1) %cycles %pc 93: %opcode $padString(5, %operand) 93-> $+(71,%mnemonic) $padString(8, %result) $padString(10, %regs) $padString(11, $+(94,%mode)) $padString(10, %flags) %ticks
;echo @nes.debug %cycles %pc 93: %opcode $padString(5, %operand) 93-> $+(71,%mnemonic) $padString(6, %result) $padString(10, %regs) $padString(11, $+(94,%mode)) $padString(10, %flags) %ticks
}
}
else {
;; print a warning if we encounter an unimplemented opcode
echo @nes.debug %cycles %pc 93: %opcode $padString(5, %operand) 93-> 54,52 $+ /!\66,28 $+ $+($chr(160),unimplemented instruction,$chr(160),) $+(96,$calc($ticksqpc - $hget(nes.cpu, ticks.start)),94ms)
;echo -a stack dump:
;echo -a . $bvar(&RAM, $dec(0101), $dec(ff))
}
}
alias nes.debug.init {
;; debug output selector.
;; full shows everything, error only shows errors, none shows... none!
;; this is not completely implemented but it's a start
hadd nes.cpu debug $iif($1, $1, error)
if ($hget(nes.cpu, debug) == full) {
;; print debug header, twice. this is because our cpu output
;; gets printed right in between, so that it's easier to read
;; the output even if we're all the way at the end.
debugHeader
debugHeader
}
}
alias nes.debug.stackDump {
;noop $hget(nes.mem, ram, &RAM)
echo -a . $bvar(&RAM, $dec(0101), 256)
}
alias -l debugHeader {
echo @nes.debug 91---95cyl91-95pc91------95op91-95oprnd91----95mnm91-95result91----95A91--95X91--95Y91----95mode91--------95NVssDIZC91---95exec91--95mips91--
}
;; pad $2- up to $1 characters, using $chr(160) ((unicode space))
alias -l padString {
var %stringLength $len($strip($2-))
var %newLength $1
var %padLength $calc(%newLength - %stringLength)
var %padding $str($chr(160),%padLength)
return $iif($prop == pre, $+(%padding,$2-), $+($2-,%padding))
}
;; explicit hex -> decimal conversion
alias -l dec {
return $base($1, 16, 10)
}
;; explicit decimal -> hex conversion
alias -l hex {
return $base($1, 10, 16, 2)
}