-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.mrc
132 lines (86 loc) · 3.3 KB
/
mem.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
;; okay, let's write a stack handler now!! :3
;; complete rewrite
alias nes.mem.stack {
;; stack starts at $01FF and decreases down to $0100
var %startAddress $base(01FF, 16, 10)
;; calculate the current stack address.
;; start - pointer
var %stackAddress $calc(%startAddress - $hget(nes.mem, stackPointer))
var %mode $1
if (%mode == push) {
;; check if we haven't reached stack overflow yet
if ($hget(nes.mem, stackPointer) < 255) {
;; if pushing, there's a value.
var %value $2
;; write value to the stack
nes.mem.write %stackAddress %value
;; increment the stack pointer
hinc nes.mem stackPointer
}
else {
echo @nes.debug /!\66,28 $+ $+($chr(160),stack overflow,$chr(160),) $+(96,$calc($ticksqpc - $hget(nes.cpu, ticks.start)),94ms)
nes.cpu.stop
}
}
elseif (%mode == pop) {
;; read value from the current stack address + 1
;; this is what was wrong the whole time. we were reading from
;; the *next* stack address, rather than the last one we wrote to.
;; big thanks to zowie for talking me through debugging this on discord <3
var %value $nes.mem.read($calc(%stackAddress + 1))
;; decrease stack pointer
hdec nes.mem stackPointer
}
echo -s . $time : stack $1 -> $base(%value, 10, 16)
updateStackDisplay
return %value
}
;; since stupid. fuckin. starting at 1.
;; yeah. so. here's an alternative that does the extra math for us.
;; assumes decimal input.
alias nes.mem.write {
bset &RAM $calc($1 + 1) $2
;hadd nes.mem $1 $2
}
alias nes.mem.read {
return $bvar(&RAM, $calc($1 + 1))
;return $hget(nes.mem, $1)
}
alias nes.mem.init {
;; set up RAM. just fill it with zeroes first.
echo @nes.debug setting up RAM
if ($hget(nes.mem) != $null) {
hfree nes.mem
}
;; 64k RAM space, plus a lil extra for other stuff.
hmake nes.mem $calc(64 * 128)
bset &RAM $calc(64 * 1024) 0
;; stack init
;; stack is 256 bytes from $0100 - $01FF.
;; it starts at $01FF and is filled from there, backwards.
hadd nes.mem stackPointer 0
;; update stack in listbox
initStackDisplay
updateStackDisplay
}
alias -l initStackDisplay {
clear -l @nes.debug
var %i 0
while (%i < 256) {
aline -l @nes.debug .
inc %i
}
}
alias -l updateStackDisplay {
var %i 0
while (%i < 256) {
var %address $calc($base(0100, 16, 10) + %i)
var %value $base($nes.mem.read(%address), 10, 16, 2)
rline -l @nes.debug $calc(%i + 1) $+($,$base(%address, 10, 16, 4)) : %value
inc %i
}
;; select last item on the list to force scroll
sline -l @nes.debug 256
;; select current stack address
sline -l @nes.debug $calc(256 - $hget(nes.mem, stackPointer))
}