-
Notifications
You must be signed in to change notification settings - Fork 7
/
4-flags.8o
324 lines (285 loc) · 6.25 KB
/
4-flags.8o
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# Flags test
# This is a visual adaptation of the math tests I wrote for Silicon8
# (https://github.com/Timendus/silicon8/tree/main/tests)
:include "../utils/helpers.8o"
:include "../utils/text-rendering.8o"
:alias 15_in_a_register v1
:alias 100_in_a_register v1
:macro opcode-digit OPC {
vD := OPC
flags-draw-opcode-digit
}
: flags-draw-opcode-digit
i := im0
vE := vD
vE <<= vE
vE <<= vE
i += vE
sprite x y 4
x += 5
return
:macro expect-v0 V0VAL {
v2 := v0
vC := V0VAL
flags-draw-result
}
:macro expect-v2-vf-v3 V2VAL VFVAL V3VAL {
vE := vF
vC := V2VAL
flags-draw-result
v2 := vE
vC := VFVAL
flags-draw-result
v2 := v3
vC := V3VAL
flags-draw-result
}
:macro expect-v2-vf-v3-v4 V2VAL VFVAL V3VAL V4VAL {
vE := vF
vC := V2VAL
flags-draw-result
v2 := vE
vC := VFVAL
flags-draw-result
v2 := v3
vC := V3VAL
flags-draw-result
v2 := v4
vC := V4VAL
flags-draw-result
}
: flags-draw-result
i := flag-err
if v2 == vC then i := flag-ok
y += 1
sprite x y 3
x += 4
y -= 1
return
: main
clear
x := 50
y := 27
i := version-0-0
sprite x y 4
x := 58
i := version-1-0
sprite x y 4
## Without complications
text 0 0 flags-no-carry
x := 22
y := 0
15_in_a_register := 15
# OR
opcode-digit 1 # 0x81
v3 := 15
vF := 20
v3 |= vF # 31 (0x1F)
vF := 0 # vF will not be reset if that quirk is not active
v2 := 50
v2 |= 15_in_a_register # 63 (0x3F)
expect-v2-vf-v3 63 0 31
x += 5
# AND
opcode-digit 2 # 0x82
v3 := 15
vF := 20
v3 &= vF # 4 (0x04)
vF := 0 # vF will not be reset if that quirk is not active
v2 := 50
v2 &= 15_in_a_register # 2 (0x02)
expect-v2-vf-v3 2 0 4
y += 5
x := 0
# XOR
opcode-digit 0x3 # 0x83
v3 := 15
vF := 20
v3 ^= vF # 27 (0x1B)
vF := 0 # vF will not be reset if that quirk is not active
v2 := 50
v2 ^= 15_in_a_register # 61 (0x3D)
expect-v2-vf-v3 61 0 27
x += 5
# Addition (no overflow)
opcode-digit 0x4 # 0x84
vF := 20
vF += 15_in_a_register # 35 (0x23), but should be overwritten by flag, so 0
v4 := vF
v3 := 15
vF := 20
v3 += vF # 35 (0x23)
vF := 0xAA
v2 := 50
v2 += 15_in_a_register # 65 (0x41)
expect-v2-vf-v3-v4 65 0 35 0
x += 1
# Subtraction in one direction (no carry)
opcode-digit 0x5 # 0x85
# Edge cases:
# Check that vF -= vX results in no carry
vF := 20
vF -= 15_in_a_register # 5 (0x05), but should be overwritten by flag, so 1
v4 := vF
# Check that vX -= vF results in the correct result and no carry
v3 := 20
vF := 15
v3 -= vF # 5 (0x05)
# Check that N - N (for the same N) does not result in carry
v5 := 10
vF := 10
v5 -= vF
v5 := vF
# Base case: check that subtracting two regular registers results in the
# correct value and no carry set, and that the carry register actually gets
# overwritten.
vF := 0xAA
v2 := 50
v2 -= 15_in_a_register # 35 (0x23)
# Check all our assertions
if v5 != 1 then vF := 2
expect-v2-vf-v3-v4 35 1 5 1
y += 5
x := 0
# Shift right (no LSB)
opcode-digit 0x6 # 0x86
vF := 60
vF >>= vF # 30 (0x1E), but should be overwritten by flag, so 0
v3 := vF
vF := 0xAA
v2 := 60
v2 >>= v2 # 30 (0x1E)
expect-v2-vf-v3 30 0 0
x += 5
# Subtraction in the other direction (no carry)
opcode-digit 0x7 # 0x87
# Edge cases:
# Check that vF =- vX results in no carry
vF := 10
vF =- 15_in_a_register # 5 (0x5), but should be overwritten by flag, so 1
v4 := vF
# Check that vX =- vF results in the correct result and no carry
v3 := 15
vF := 20
v3 =- vF # 5 (0x05)
# Check that N - N (for the same N) does not result in carry
v5 := 10
vF := 10
v5 =- vF
v5 := vF
# Base case: check that subtracting two regular registers results in the
# correct value and no carry set, and taht the carry register actually gets
# overwritten.
vF := 0xAA
v2 := 15
v1 := 50
v2 =- v1 # 35 (0x23)
# Check all our assertions
if v5 != 1 then vF := 2
expect-v2-vf-v3-v4 35 1 5 1
x += 1
# Shift left (no MSB)
opcode-digit 0xE # 0x8E
vF := 50
vF <<= vF # 100 (0x64), but should be overwritten by flag, so 0
v3 := vF
vF := 0xAA
v2 := 50
v2 <<= v2 # 100 (0x64)
expect-v2-vf-v3 100 0 0
# With complications
text 0 16 flags-carry
x := 22
y := 16
100_in_a_register := 100
# Addition (with overflow)
opcode-digit 0x4 # 0x84
vF := 200
vF += 100_in_a_register # 300 (0x2C), but should be overwritten by flag, so 1
v4 := vF
v3 := 100
vF := 200
v3 += vF # 300 (0x2C)
vF := 0xAA
v2 := 200
v2 += 100_in_a_register # 300, but overflows so 44 (0x2C)
expect-v2-vf-v3-v4 44 1 44 1
x += 1
# Subtraction in one direction (with carry)
opcode-digit 0x5 # 0x85
vF := 95
vF -= 100_in_a_register # -5 = 251 (0xFB), but should be overwritten by flag, so 0
v4 := vF
v3 := 95
vF := 100
v3 -= vF # -5 = 251 (0xFB)
vF := 0xAA
v2 := 95
v2 -= 100_in_a_register # -5 = 251 (0xFB)
expect-v2-vf-v3-v4 -5 0 -5 0
y += 5
x := 0
# Shift right (with LSB)
opcode-digit 0x6 # 0x86
vF := 61
vF >>= vF # 30 (0x1E), but should be overwritten by flag, so 1
v3 := vF
vF := 0xAA
v2 := 61
v2 >>= v2 # 30 (0x1E)
expect-v2-vf-v3 30 1 1
x += 5
# Subtraction in the other direction (with carry)
opcode-digit 0x7 # 0x87
vF := 105
vF =- 100_in_a_register # -5 = 251 (0xFB), but should be overwritten by flag, so 0
v4 := vF
v3 := 105
vF := 100
v3 =- vF # -5 = 251 (0xFB)
vF := 0xAA
v2 := 105
v2 =- 100_in_a_register # -5 = 251 (0xFB)
expect-v2-vf-v3-v4 -5 0 -5 0
x += 1
# Shift left (with MSB)
opcode-digit 0xE # 0x8E
vF := 188
vF <<= vF # 376 (0x178), but should be overwritten by flag, so 1
v3 := vF
vF := 0xAA
v2 := 188
v2 <<= v2 # 376 (0x178), but overflows so 120 (0x78)
expect-v2-vf-v3 120 1 1
# Addition to i
text 0 27 flags-other
x := 22
y := 27
opcode-digit 0xF # This block
x -= 1 # draws
opcode-digit 0xE # opcode 0xFE
i := scratchpad
v1 := 16
i += v1
v0 := 0xAA
save v0
i := scratchpad-plus-16
load v0
expect-v0 0xAA
i := scratchpad
vF := 16
i += vF
v0 := 0x55
save v0
i := scratchpad-plus-16
load v0
expect-v0 0x55
loop again
:segment data
: flags-no-carry
str "HAPPY" 0
: flags-carry
str "CARRY" 0
: flags-other
str "OTHER" 0
:include "../../pictures/version.png"