Switch to common, I guess
[ScratchNES.git] / CPU.tosh
1 define mapper read (location)
2 set M to 255
3
4 define mapper write (location) (value)
5
6 define immediate
7 mapper read PC+1
8
9 define zeropage
10 mapper read PC+1
11 mapper read M
12
13 define zeropageX
14 mapper read PC+1
15 mapper read M + X mod 256
16
17 define zeropageY
18 mapper read PC+1
19 mapper read M + Y mod 256
20
21 define absolute
22 mapper read PC+1
23 set tmp to M
24 mapper read PC+2
25 mapper read tmp * 256 + M
26
27 define absoluteX
28 mapper read PC+1
29 set tmp to M
30 mapper read PC+2
31 mapper read tmp * 256 + M + X
32
33 define absoluteY
34 mapper read PC+1
35 set tmp to M
36 mapper read PC+2
37 mapper read tmp * 256 + M + Y
38
39 define implied
40
41 define indirect
42 mapper read PC+1
43 set tmp to M
44 mapper read PC+2
45 mapper read tmp * 256 + M
46 set tmp2 to M
47 mapper read tmp * 256 + M + 1
48 mapper read tmp2 * 256 + M
49
50 define indirectX
51 mapper read PC+1
52 mapper read (M + X) mod 256
53
54 define indirectY
55 mapper read PC+1
56 mapper read M
57 mapper read M + Y + flagC
58
59 define flagZ
60 set flagZ to <A = 0>
61
62 define flagN
63 set flagN to <join "0x" (letter 1 of A) > 7 >
64
65 define two2native
66 set tmp to A - 256 + (256 * <A < 128>)
67
68 define push (value)
69 mapper write 256 + S value
70 set S to (S - 1) mod 256
71
72 define pull
73 mapper read (257 + S)
74 set S to (S + 1) mod 256
75
76 define interrupt
77
78 define ADC
79 R,N,Z
80 set tmp to (join "0x" (A)) + (join "0x" (OP)) + flagC
81 set A to item (tmp mod 256) of hex
82 set flagC to <tmp > 255>
83 set flagV to <<(join "0x" (A)) < 128 and (join "0x" (OP)) < 128 and (tmp mod 256) > 128> or <(join "0x" (A)) > 127 and (join "0x" (OP)) > 127 and (tmp mod 256) < 128>>
84 set flagV to <tmp > 127 or tmp < -128>
85
86 define SBC
87 R,N,Z,A
88 set tmp to (join "0x" (A)) - (join "0x" (OP)) - flagC
89 set A to item (tmp mod 256) of hex
90 set flagC to <tmp > 255>
91 set flagV to <<(join "0x" (A)) < 128 and (join "0x" (OP)) < 128 and (tmp mod 256) > 128> or <(join "0x" (A)) > 127 and (join "0x" (OP)) > 127 and (tmp mod 256) < 128>>
92
93 define AND
94 R,N,Z,A
95 set A to join (item (join (letter 1 of A) (letter 1 of OP)) of AND) (item (join (letter 2 of A) (letter 2 of OP)) of AND)
96
97 define ASL
98 RW,N,Z,OP
99 set flagC to <join "0x" (letter 1 of OP) > 7 >
100 set OP to (item ( (join "0x" (OP)) * 2) mod 256 of hex)
101
102 define BCS
103 BRANCH
104 mapper read PC+1
105 change PC by flagC * (M + 256 * (<M < 128> - 1))
106
107 define BRK
108 IMPLIED
109 set flagI to 1
110 mapper write (256 + S) (PC + 2)
111 set S to (S - 1) mod 256
112 compute SR
113 mapper write (256 + S) SR
114 set S to (S - 1) mod 256
115 interrupt
116
117 define CLC
118 IMPLIED
119 set flagC to 0
120
121 define CMP
122 R, tmp, N, Z, C
123 set tmp to (join "0x" (A)) - (join "0x" (OP))
124
125 define CPX
126 R, tmp, N, Z, C
127 set tmp to X - OP
128
129 define CPY
130 R, tmp, N, Z, C
131 set tmp to Y - OP
132
133 define INC
134 RW, N, Z
135 set OP to OP + 1
136
137 define INX
138 IMPLIED
139 change X by 1
140
141 define JMP
142 RAW
143 set PC to OP
144
145 define JSR
146 RAW
147 set tmp to (join "0x" (PC)) + 2
148 mapper write (256 + S) ( (tmp - (tmp mod 256)) / 256)
149 set S to (S - 1) mod 256
150 mapper write (256 + S) (tmp mod 256)
151 set S to (S - 1) mod 256
152 set PC to OP
153
154 define LDA
155 R, N, Z, A
156 set A to OP
157
158 define LSR
159 RW, Z
160 set flagC to OP mod 2
161 set OP to floor of (join "0x" (OP)) / 2
162
163 define PHA
164 mapper write 256 + S A
165 set S to (S - 1) mod 256
166
167 define PLA
168 IMPLIED, N, Z, A
169 mapper read (257 + S)
170 set S to (S + 1) mod 256
171 set A to M
172
173 define PLP
174 IMPLIED
175 mapper read (257 + S)
176 set S to (S + 1) mod 256
177 set flagN to < (item (join (letter 1 of S) "8") of AND) = 8>
178 set flagV to < (item (join (letter 1 of S) "4") of AND) = 4>
179 set flagB to < (item (join (letter 1 of S) "1") of AND) = 1>
180 set flagD to < (item (join (letter 2 of S) "8") of AND) = 8>
181 set flagI to < (item (join (letter 2 of S) "4") of AND) = 4>
182 set flagZ to < (item (join (letter 2 of S) "2") of AND) = 2>
183 set flagC to < (item (join (letter 2 of S) "1") of AND) = 1>
184
185 define ROL
186 RW, N, Z
187 set tmp to <(join "0x" (letter 1 of OP)) > 7>
188 set OP to OP * 2 + flagC
189 set flagC to tmp
190
191 define ROR
192 RW, N, Z
193 set tmp to <(join "0x" (OP)) mod 2 = 1>
194 set OP to (floor of OP / 2) + (128 * flagC)
195 set flagC to tmp
196
197 def RTS
198 IMPLIED
199 mapper read (257 + S)
200 set S to (S + 1) mod 256
201 set tmp to M
202 mapper read (257 + S)
203 set S to (S + 1) mod 256
204 set PC to join tmp M
205
206 def STA
207 W
208 set OP to A
This page took 0.081345 seconds and 4 git commands to generate.