-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduet.z80
589 lines (528 loc) · 8.1 KB
/
duet.z80
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
; VDP manual: http://bifi.msxnet.org/msxnet/tech/tms9918a.txt
; More on VDP and speed limits: http://map.grauw.nl/articles/vdp_tut.php
;include "msx.h"
include "svi.h"
; Header values for svitool to package it in a tape container
; dw start, end-1, start
org 0xA000
start:
call INIGRP
call set_single_pattern_table
call set_pattern_name_table
call set_color_palette
call clearbuffer
call main_bg_loop
;call test_static
call CHGET ; clear keyboard buffer
call INITXT
ret
test_static:
ld b,0
ld c,3
ld d,0
ld e,0
call drawfigurexy
ld b,0
ld c,3
ld d,32
ld e,5
;call drawfigurexy
ld b,5
ld c,1
ld d,0
ld e,48
;call drawfigurexy
ld b,7
ld c,2
ld d,32
ld e,32
;call drawfigurexy
call flushbuffer
ret
main_bg_loop:
halt
; Change backdrop for performance markers
;ld a,2
;out (VDP_CW),a
;ld a,128+7
;out (VDP_CW),a
call .update_background
;ld a,8
;out (VDP_CW),a
;ld a,128+7
;out (VDP_CW),a
call CHSNS
jr z,main_bg_loop
ret
framestate:
db 0, 14 ; rotation frames
dw rotation_wave
dw (rotation_wave_end-rotation_wave)/2+rotation_wave
db 0 ; angle
.update_background:
call clearbuffer
;halt
;halt
ld a,(framestate+6)
;and 127
ld l,a
ld h,0
add hl,hl
ld de,sintable
add hl,de
ld l,(hl)
push hl
ld a,(framestate+6)
add a,64
;and 127
ld l,a
ld h,0
add hl,hl
ld de,sintable
add hl,de
ld a,(hl)
pop hl
ld h,a
ld a,(framestate)
ld b,a
ld c,3
ld de,0x0000
ld a,l
add d
ld d,a
ld a,h
add e
ld e,a
call drawfigurexy
ld a,(framestate+1)
ld b,a
ld c,1
ld de,0x2004
ld a,l
add d
ld d,a
ld a,h
add e
ld e,a
call drawfigurexy
ld a,(framestate+1)
ld b,a
ld c,1
ld de,0x0424
ld a,l
add d
ld d,a
ld a,h
add e
ld e,a
call drawfigurexy
ld a,(framestate)
ld b,a
ld c,0
ld de,0x2424
ld a,l
add d
ld d,a
ld a,h
add e
ld e,a
call drawfigurexy
ld de,(framestate+2)
ld hl,framestate
ld a,(de)
add a,(hl)
ld (hl),a
inc de
ld hl,rotation_wave_end
call compare_de_hl
jr nz,.ub_nowrap1
ld de,rotation_wave
.ub_nowrap1:
ld (framestate+2),de
ld de,(framestate+4)
ld hl,framestate+1
ld a,(de)
neg
add a,(hl)
ld (hl),a
inc de
ld hl,rotation_wave_end
call compare_de_hl
jr nz,.ub_nowrap2
ld de,rotation_wave
.ub_nowrap2:
ld (framestate+4),de
ld a,(framestate+6)
inc a
ld (framestate+6),a
call flushbuffer
ret
drawfigurexy:
push bc
push de
push hl
; B = rotation frame (lower 4 bits will be used)
; C = size (0-3)
; D,E = x,y coordinates in drawing buffer
; Ensure coordinates are 0 <= x,y < 64
ld a,d
and 63
ld d,a
ld a,e
and 63
ld e,a
call get_figure_ptr
; now HL -> figure to draw
ld a,d
ex de,hl
ld h,0 ; HL is now y coordinate in buffer to draw to
add hl,hl
add hl,hl
add hl,hl
add hl,hl
ld bc,gfxbuffer
add hl,bc
ld c,a
srl c
srl c
srl c
ld b,0
add hl,bc
ex de,hl
; HL -> figure to draw
; DE -> correct position in the first row in buffer to draw to
; A = x coordinate
ld b,32
ld c,a
.processrow:
; B rows left
; HL -> current position in figure
; DE -> correct position in row in drawing buffer
; C = x offset
push bc
push hl
push de
ld b,c ; B = number of times to rotate
ld c,(hl)
inc hl
ld d,(hl)
inc hl
ld e,(hl)
inc hl
ld h,(hl)
ld l,0
ld a,c
or d
or e
or h
jr z,.rowdone ; empty row in figure, skip it
ld a,b
and 7
jr z,.rotationdone
ld b,a
; If we need to rotate 5,6 or 7 times, prefer rotate 3, 2, 1 times the other way instead
cp 5
jr nc,.setuprotateleft
.rotateright:
srl c
rr d
rr e
rr h
rr l
djnz .rotateright
jr .rotationdone
.setuprotateleft:
ld a,8
sub b
ld b,a
; Shift the data
ld l,h
ld h,e
ld e,d
ld d,c
ld c,0
.rotateleft:
sla l
rl h
rl e
rl d
rl c
djnz .rotateleft
.rotationdone:
; Data is now correctly rotated in BCDHL
ld a,c
ld c,l
ld b,h
pop hl
push hl
; ADEBC should now be OR:ed with HL..HL+4
; Because of the way the drawing buffer is aligned, we can do some
; tricks to make the wraparound to work fast
or (hl)
ld (hl),a
inc l
res 3,l
ld a,(hl)
or d
ld (hl),a
inc l
res 3,l
ld a,(hl)
or e
ld (hl),a
inc l
res 3,l
ld a,(hl)
or b
ld (hl),a
inc l
res 3,l
ld a,(hl)
or c
ld (hl),a
.rowdone:
pop de
ld hl,16
add hl,de
ex de,hl
; DE -> next line in drawing buffer
ld hl,gfxbufferend
call compare_de_hl
jr c,.cont
ld hl,-1024
add hl,de
ex de,hl
.cont:
pop hl
inc hl
inc hl
inc hl
inc hl
; HL -> next line in figure
pop bc
; B = rows left
djnz .processrow
pop hl
pop de
pop bc
ret
; Z will be set if DE=HL, C set if DE<HL
compare_de_hl:
ld a,d
cp h
ret nz
ld a,e
cp l
ret
drawbuffer:
; B = rotation frame (lower 4 bits will be used)
; C = size (0-3)
; DE = position in drawing buffer
call get_figure_ptr
ld b,32
.db2loop:
push bc
ldi
ldi
ldi
ldi
inc de
inc de
inc de
inc de
pop bc
djnz .db2loop
ret
clearbuffer:
push bc
push de
push hl
ld hl,gfxbuffer
ld de,9
; Clear 64 rows
ld b,64
xor a
.cb_loop:
; Clear 8 bytes, then skip 8 bytes
ld (hl),a
inc l
ld (hl),a
inc l
ld (hl),a
inc l
ld (hl),a
inc l
ld (hl),a
inc l
ld (hl),a
inc l
ld (hl),a
inc l
ld (hl),a
add hl,de
djnz .cb_loop
pop hl
pop de
pop bc
ret
flushbuffer:
push bc
push de
push hl
ld hl,0x0000
call set_vdp_write_addr
ld hl,gfxbuffer
ld c,VDP_DW
ld a,8 ; 8 rows
.fb2_nextrow:
ld b,64 ; 64 bytes to write per row
.fb2_nexttile:
ld de,15 ; 1 is added automatically after every outi
push hl
outi
add hl,de
outi
add hl,de
outi
add hl,de
outi
add hl,de
outi
add hl,de
outi
add hl,de
outi
add hl,de
outi
pop hl
inc hl
jr nz, .fb2_nexttile ; flag was set from last outi
ld de,7*16+8
add hl,de
dec a
jr nz,.fb2_nextrow
pop hl
pop de
pop bc
ret
get_figure_ptr:
; B = rotation frame (lower 5 bits will be used)
; C = size (0-3)
; gfx start at gfx+(c*32+b)*128
ld l,c
ld h,0
add hl,hl
add hl,hl
add hl,hl
add hl,hl
add hl,hl ; HL = C*32
ld a,b
and 31
ld c,a
ld b,0
add hl,bc ; HL = C*16+B
add hl,hl
add hl,hl
add hl,hl
add hl,hl
add hl,hl
add hl,hl
add hl,hl ; HL = (C*16+B)*128
ld bc,gfx
add hl,bc
ret
draw_tile:
ld b,8
ld c,VDP_DW
.dt_loop:
outi
jr nz,.dt_loop
ret
set_single_pattern_table:
; Sets all three parts of the screen to use the same pattern table
di
ld a,0
out (VDP_CW),a
ld a,128+4
out (VDP_CW),a
ld a,128+31
out (VDP_CW),a
ld a,128+3
out (VDP_CW),a
ei
ret
set_color_palette:
; Set backdrop to blue
ld a,4
out (VDP_CW),a
ld a,128+7
out (VDP_CW),a
; Sets foreground to white, background to black for the 256 chars used
ld hl,0x2000
call set_vdp_write_addr
ld d,8
ld b,0
ld a,0x54 ; FG=gray BG=black
.scp_loop:
out (VDP_DW),a
djnz .scp_loop
dec d
jr nz,.scp_loop
ret
set_pattern_name_table:
; Creates an 8x8 matrix of characters 0-63 and repeats it 4x3 over the screen
ld hl,0x1800
call set_vdp_write_addr
; 01234567012345670123456701234567
; 89ABCDEF89ABCDEF89ABCDEF89ABCDEF
; etc
ld d,0 ; row offset
ld e,24 ; rows left
.spnt_loop2:
ld c,0 ; col offset
ld b,32 ; cols left
.spnt_loop1:
ld a,c
add a,d
out (VDP_DW),a
inc c
res 3,c
djnz .spnt_loop1
ld a,8
add a,d
and 63
ld d,a
dec e
jr nz,.spnt_loop2
ret
; Sets the VDP address registers to HL and prepares for writing
; C will be equal to VDP_DW
set_vdp_write_addr:
ld c,VDP_CW
set 6,h
di
ld a,l
out (VDP_CW),a
ld a,h
out (VDP_CW),a
ei
ret
gfx:
incbin "squares.bin"
rotation_wave:
db 1,3,4,5,5,5,4,3,1
db 0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0
rotation_wave_end:
db -1
sintable:
incbin "sin.bin"
; The drawing buffer is 64x64 pixel in size
; Every row is 16 bytes:
; * The first 8 bytes correspond to the 64 pixels
; * Then their are 8 empty bytes, to make wraparound easier
gfxbuffer: equ start-0x1000 ;8*8*8
gfxbufferend: equ gfxbuffer+2*8*8*8
end:
end