-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathZ80 XCF Flavor.asm
340 lines (297 loc) · 8.94 KB
/
Z80 XCF Flavor.asm
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
; Z80 XCF Flavor v1.6
; Copyright (C) 2022-2024 Manuel Sainz de Baranda y Goñi.
;
; This program is free software: you can redistribute it and/or modify it under
; the terms of the GNU General Public License as published by the Free Software
; Foundation, either version 3 of the License, or (at your option) any later
; version.
;
; This program is distributed in the hope that it will be useful, but WITHOUT
; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License along with
; this program. If not, see <http://www.gnu.org/licenses/>.
device zxspectrum48, $7FFF
CL_ALL = $0DAF
CHAN_OPEN = $1601
UDG = $5C7B
BRIGHT = 19
FLASH = 18
INK = 16
PAPER = 17
TAB = 23
macro Q0_F0_A0
xor a ; A = 0; YF, XF, YQ, XQ = 0
endm
macro Q0_F1_A0
xor a ;
dec a ; YF, XF = 1
ld a, 0 ; A = 0; Q = 0
endm
macro Q1_F1_A0
xor a ; A = 0
ld e, a ;
dec e ; YF, XF, YQ, XQ = 1
endm
macro Q0_F0_A1
xor a ; YF, XF = 0
ld a, $FF ; A = FFh; Q = 0
endm
macro Q0_F1_A1
xor a ;
dec a ; A = FFh; YF, XF = 1
nop ; Q = 0
endm
macro Q1_F1_A1
xor a ;
dec a ; A = FFh; YF, XF, YQ, XQ = 1
endm
org $8000
start: di ; Disable interrupts.
call CL_ALL ; Clear the screen.
ld a, 2 ; Open channel #2 for text output.
call CHAN_OPEN ;
ld bc, (UDG) ; Save the current UDG pointer.
ld de, nn ; Set custom UDG for "ñ".
ld (UDG), de ;
ld hl, header_text ; Print the header.
call print ;
ld (UDG), bc ; Restore the original UDG pointer.
ld bc, results ; Set BC to the address of the results array.
; Test all factor combinations with `ccf` and
; keep the resulting values of YF and XF.
Q0_F0_A0 : ccf : call keep_yxf
Q0_F1_A0 : ccf : call keep_yxf
Q1_F1_A0 : ccf : call keep_yxf
Q0_F0_A1 : ccf : call keep_yxf
Q0_F1_A1 : ccf : call keep_yxf
Q1_F1_A1 : ccf : call keep_yxf
; Test all factor combinations with `scf` and
; keep the resulting values of YF and XF.
Q0_F0_A0 : scf : call keep_yxf
Q0_F1_A0 : scf : call keep_yxf
Q1_F1_A0 : scf : call keep_yxf
Q0_F0_A1 : scf : call keep_yxf
Q0_F1_A1 : scf : call keep_yxf
Q1_F1_A1 : scf : call keep_yxf
ld c, 6 ; C = number of rows to print.
ld hl, rows_text ; (HL) = Static text of the row.
ld de, results ; (DE) = `ccf` results.
ld ix, results + 6 ; (IX) = `scf` results.
.print_table_row:
call print ; Print the static text of the row.
ld a, (de) ; Print the results for `ccf` and point DE
call print_yxf ; to the next element in the results
inc de ; array.
dec hl ; Point HL to the last two spaces in the
dec hl ; static text of the row, and print those
call print ; spaces (column gap). Next, point HL to
inc hl ; the static text of the next row.
ld a, (ix) ; Print the results for `scf` and point IX
call print_yxf ; to the next element in the results
inc ix ; array.
dec c ;
jr nz, .print_table_row ; Repeat until all rows have been printed.
call print ; Now HL points to the footer; print it.
ld de, results ; Compare the values obtained with `ccf`,
ld hl, results + 6 ; against those obtained with `scf`.
call compare_results ; They should be the same; otherwise,
cp 0 ; the behavior is unknown (or unstable)
jr nz, .unknown_flavor ; and we report it.
ld de, results ; Compare the values obtained with `ccf`
ld hl, results_on_zilog ; against the reference values for Zilog
call compare_results ; CPU models.
ld hl, zilog_text ;
cp 0 ; If the values match, report "Zilog
jr z, .print_result ; flavor" and exit.
ld de, results ; Compare the values obtained with `ccf`
ld hl, results_on_nec_nmos ; against the reference values for NEC
call compare_results ; NMOS CPU models.
ld hl, nec_nmos_text ;
cp 0 ; If the values match, report "NEC NMOS
jr z, .print_result ; flavor" and exit.
ld de, results ; Compare the values obtained with `ccf`
ld hl, results_on_st_cmos ; against the reference values for ST
call compare_results ; CMOS CPU models.
ld hl, st_cmos_text ;
cp 0 ; If the values match, report "ST CMOS
jr z, .print_result ; flavor" and exit.
.unknown_flavor:
ld hl, unknown_text ; Report "Unknown flavor".
.print_result:
call print
ld hl, flavor_text
call print
ei ; Re-enable interrupts.
ret ; Exit to BASIC.
; Keeps YF and XF into the results array.
;
; On entry:
; BC - Address of the element in the results array.
; On exit:
; BC - Address of the next element in the results array.
; Destroys:
; A and DE.
keep_yxf:
push af ; Transfer F to A.
pop de ;
ld a, e ;
and 00101000b ; Clear all flags except YF and XF.
ld (bc), a ; Keep YF and XF into the results array.
inc bc ; Point BC to the next element of the array.
ret
; Prints YF and XF.
;
; On entry:
; A - Flags.
; Destroys:
; A and B.
print_yxf:
ld b, a ; Copy the flags to B.
ld a, INK ; Set blue ink.
rst $10 ;
ld a, 1 ;
rst $10 ;
srl b ; Shift the flags to the right until XF is at bit 0.
srl b ;
srl b ;
ld a, b ; Copy the shifted flags to A, and shift this register to
srl a ; the right until YF is at bit 0.
srl a ;
and 1 ; Clear all bits except bit 0.
add $30 ; Translate the value of YF to ASCII.
rst $10 ; Print the value of YF.
ld a, b ; Copy B to A. Now bit 0 of A contains XF.
and 1 ; Clear all bits except bit 0.
add $30 ; Translate the value of XF to ASCII.
rst $10 ; Print the value of XF.
ld a, INK ; Restore the default ink.
rst $10 ;
ld a, 8 ;
rst $10 ;
ret
; Prints a 1Fh-terminated string.
;
; On entry:
; HL - String address.
; On exit:
; HL - Address of the termination byte.
; Destroys:
; A.
print: ld a, (hl)
cp $1F
ret z
rst $10
inc hl
jr print
; Compares 2 arrays of results.
;
; On entry:
; HL - Array 1 address.
; DE - Array 2 address.
; On exit:
; A - 0 if the arrays are equal; otherwise, a non-zero value.
; Destroys:
; C, DE and HL.
compare_results:
ld c, 6
.compare:
ld a, (de)
sub (hl)
ret nz
inc de
inc hl
dec c
jr nz, .compare
ret
results:
block 12
results_on_zilog:
db 00000000b, 00101000b, 00000000b, 00101000b, 00101000b, 00101000b
results_on_nec_nmos:
db 00000000b, 00000000b, 00000000b, 00101000b, 00101000b, 00101000b
results_on_st_cmos:
db 00000000b, 00100000b, 00000000b, 00001000b, 00101000b, 00101000b
header_text:
db 'Z80 XCF '
db BRIGHT, 1, PAPER, 2, INK, 7, 'F', PAPER, 3, 'L', PAPER, 1, 'A'
db PAPER, 5, INK, 8, 'V'
db PAPER, 4, 'O', PAPER, 6, 'R', INK, 8, PAPER, 8, BRIGHT, 8, ' v1.6 ('
db __DATE__, ")\r"
db 127, ' Manuel Sainz de Baranda y Go', $90, "i\r"
db INK, 1, 'https://zxe.io', INK, 8, "\r"
db "\r"
db "This program checks the behavior\r"
db "of the undocumented flags during\r"
db "the CCF and SCF instructions and\r"
db "detects the Z80 CPU type of your\r"
db "ZX Spectrum.\r"
db "\r"
db BRIGHT, 1, PAPER, 0, INK, 7," Case Any NEC ST HOST \r"
db " Tested Zilog NMOS CMOS CPU \r"
db '(Q<>F)|A YX YX YX YX YX', INK, 8, PAPER, 8, BRIGHT, 8, $1F
rows_text:
db "\r(0<>0)|0 00 00 00 ", $1F
db "\r(0<>1)|0 11 00 10 ", $1F
db "\r(1<>1)|0 00 00 00 ", $1F
db "\r(0<>0)|1 11 11 01 ", $1F
db "\r(0<>1)|1 11 11 11 ", $1F
db "\r(1<>1)|1 11 11 11 ", $1F
footer_text:
db "\r", BRIGHT, 1, PAPER, 0, INK, 7, TAB, 25, 0, 'ccf scf'
db INK, 8, PAPER, 8, BRIGHT, 8, "\r\r"
db 'Result: ', BRIGHT, 1, $1F
zilog_text:
db PAPER, 4, 'Zilog', $1F
nec_nmos_text:
db PAPER, 4, 'NEC NMOS', $1F
st_cmos_text:
db PAPER, 4, 'ST CMOS', $1F
unknown_text:
db PAPER, 2, FLASH, 1, 'Unknown', FLASH, 8, $1F
flavor_text:
db PAPER, 8, BRIGHT, 8, ' flavor', $1F
nn:
db 00000000b ; ñ
db 00111000b
db 00000000b
db 01011000b
db 01100100b
db 01000100b
db 01000100b
db 00000000b
PROGRAM_SIZE = $ - start
savesna 'Z80 XCF Flavor.sna', start
CLEAR = $FD
CODE = $AF
LOAD = $EF
RANDOMIZE = $F9
USR = $C0
org $5C00
basic: db 0, 1
dw LINE_1_SIZE
line_1: db CLEAR, '8', $0E, 0, 0
dw start - 1
db 0, ':'
db LOAD, '"'
name: ds 10, 32
org name
db 'XCF Flavor'
org name + 10
db '"', CODE, $0D
LINE_1_SIZE = $ - line_1
db 0, 2
dw LINE_2_SIZE
line_2: db RANDOMIZE, USR, '8', $0E, 0, 0
dw start
db 0, $0D
LINE_2_SIZE = $ - line_2
BASIC_SIZE = $ - basic
macro MAKE_TAPE tap_name, file_name
emptytap tap_name
savetap tap_name, BASIC, file_name, basic, BASIC_SIZE, 1
savetap tap_name, CODE, file_name, start, PROGRAM_SIZE, start
endm
MAKE_TAPE 'Z80 XCF Flavor.tap', 'XCF Flavor'
; EOF