-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathvideo.asm
649 lines (565 loc) · 12.4 KB
/
video.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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
INCLUDE "gbhw.asm"
MBC5_bank_low EQU $2000
MBC5_bank_high EQU $3000
audio_buffer EQU $c100
line_buffer EQU ((audio_buffer >> 8) * $101)
current_line EQU $94 ; For backup, current_line is stored in SP
current_bank EQU $96 ; For backup, current_bank is stored in b (Except for the 9th bit)
frame_repeat EQU $a0
; Address to jump to on repeat
repeat_bank EQU $a1
repeat_line EQU $a3
audio_bank EQU $a5 ; Since audio isn't that big, we use only one byte for bank number
audio_address EQU $a6
first_sample_backup EQU $a8
compression_jr EQU $fd
SECTION "Player", ROM0[0]
Bank0:
; Write changed values (Up to 9) compared to the previous line
rept 9
pop de
ld l, d
ld [hl], e
endr
; Decrease all values by 1
ld l, h
dec [hl]
rept 19
inc l
dec [hl]
endr
WaitForInterrupt::
; Wait for interrupt
halt
xor a
ldh [rIF], a
; Update PCM sample
ld h, audio_buffer >> 8
ldh a, [rLY]
ld l, a
cp 143
ld a, [hl]
ldh [rNR50], a
; Render the line by modifying SCY
ld l, h ; hl now points to line_buffer
rept 20
ld a, [hli]
ld [c], a ; c = rSCY
endr
jr z, _VBlank
ld l, h ; Due to how line_buffer is defined, hl now points to line_buffer
; The main loop. HL will point to either a frame start, or a row start
Main::
; When entering Main, the following is assumed:
; - c is rSCY
; - b is the lowest 8 bits of bank
; - sp points to current video position in ROM1
; - hl = line_buffer
; Read the first byte at sp
pop de
dec sp
; Valid values for e:
; $00 - Uncompressed, no new bank
; $01 - Uncompressed, data starts at $4000 on new bank
; $02 - Compressed, 9 changes
; $08 - Compressed, 8 changes
; $0e - Compressed, 7 changes
; $14 - Compressed, 6 changes
; $1a - Compressed, 5 changes
; $20 - Compressed, 4 changes
; $26 - Compressed, 3 changes
; $2c - Compressed, 2 changes
; $32 - Compressed, 1 changes
; $38 - Compressed, no changes
sra e
jr nz, Compressed ; If neither 0 or 1, means line is compressed
jr nc, NoNewBank; ; If zero, means no bank switch needed
; If 1, means bank switch required
; b is the current bank
inc b
ld h, MBC5_bank_low >> 8
ld [hl], b ; Write to $20xx to bank switch
ld sp, $4000 ; Reset SP
ld h, l ; Due to how line_buffer is defined, hl now points to line_buffer
NoNewBank::
; Copy the current line (20 bytes) to line_buffer
rept 10
pop de
ld a, e
ld [hli], a
ld a, d
ld [hli], a
endr
jr WaitForInterrupt
_VBlank::
jp VBlank
Compressed::
ld a, e
ldh [compression_jr + 1], a
jp $ff00 + compression_jr
; Header
ds $100 - (@ - Bank0)
_Start::
di
jp Start
; Start
ds $150 - (@ - Bank0)
Start::
; Increase the CPU speed from 4MHz to 8MHz
ld a, 1
ldh [rKEY1], a
stop
; Init the stack for the initialization routines
ld sp, $fffe
; Other inits
call InitAPU
call LCDOff
call LoadGraphics
call CreateMap
call CreateAttributeMap
; Start Playing
jp StartPlayback
WaitVBlank::
ldh a, [rLY]
cp 144
jr nz, WaitVBlank
ret
InitAPU::
; Reset the APU
xor a
ldh [rNR52], a
ld a, $80
ldh [rNR52], a
; Turn all DACs on
ldh [rNR12], a
ldh [rNR22], a
ldh [rNR30], a
ldh [rNR42], a
; Put all channels on both left and right
ld a, $FF
ldh [rNR51], a
ret
LCDOff::
call WaitVBlank
ldh a, [rLCDC]
and $7F
ldh [rLCDC], a
ret
LoadGraphics::
ld de, $8000
ld b, PixelStructureEnd - PixelStructure
ld hl, PixelStructure
.loop
ld a, [hli]
ld [de], a
inc de
dec b
jr nz, .loop
ret
CreateMap::
ld hl, $9800
ld a, 0
ld c, 32
xor a
.loopY
ld b, 32
.loopX
ld [hli], a
dec b
jr nz, .loopX
inc a
and 3
dec c
jr nz, .loopY
ret
CreateAttributeMap::
ld a, 1
ldh [rVBK], a
ld hl, $9800
ld a, 7
ld c, 32
.loopY
ld b, 32
.loopX
ld [hli], a
dec b
jr nz, .loopX
dec c
ld a, c
dec a
rra
rra
and 7
jr nz, .loopY
ret
PixelStructure::
db %00000000
db %00000000
db %00011111
db %00000000
db %00000000
db %00011111
db %00011111
db %00011111
db %11100000
db %00000000
db %11111111
db %00000000
db %11100000
db %00011111
db %11111111
db %00011111
db %00000000
db %11100000
db %00011111
db %11100000
db %00000000
db %11111111
db %00011111
db %11111111
db %11100000
db %11100000
db %11111111
db %11100000
db %11100000
db %11111111
db %11111111
db %11111111
db %00110011
db %00001111
db %00000111
db %00000000
db %00000000
db %00000111
db %00000111
db %00000111
db %11111000
db %00000000
db %11001100
db %00001111
db %11111000
db %00000111
db %11111111
db %00000111
db %00000000
db %11111000
db %00000111
db %11111000
db %00110011
db %11110000
db %00000111
db %11111111
db %11111000
db %11111000
db %11111111
db %11111000
db %11111000
db %11111111
db %11001100
db %11110000
PixelStructureEnd::
StartPlayback:
; Enable timer interrupt
ld a, 4
ldh [rIE], a
; Set SCX to 4. When HandleFrameRepeatAndUnshiftFramebuffer runs,
; it will set it back to 0 and consider this (HW) frame as the start
; of a (video) frame.
ldh [rSCX], a
; Set up the compression JR instruction
ld a, $18
ldh [compression_jr], a
; Init variables
xor a
ldh [current_bank + 1], a
ld hl, audio_buffer
ld c, 154
.loop
ld [hli], a
dec c
jr nz, .loop
ld [$3000], a
; Audio starts at 1:4001
inc a
ldh [audio_bank], a
ldh [audio_address], a
ldh [frame_repeat], a ; Set frame repeat to 1 so the VBlank functions load
; a new frame
ld [$2000], a
ld a, $40
ldh [audio_address + 1], a
; Fill line_buffer with $101 - 144. When HandleFrameRepeatAndUnshiftFramebuffer
; runs, it will add 144 to all bytes, setting them to the expected initial
; value of $01. (Note: the encoder currently does not make use of this property)
ld a, $101 - 144
ld c, 20
ld l, h ; hl = line_buffer
.loop2
ld [hli], a
dec c
jr nz, .loop2
ld c, rSCY & $FF ; C is assumed to be SCY when entring Main
; Audio comes first, first byte at ROM1 is the first video bank
ld a, [$4000]
ld b, a ; B is assumed to be the current bank
ld [$2000], a
ld sp, $4000 ; Point SP to video start
ld hl, line_buffer
; Enable LCD
ldh a, [rLCDC]
or $80
ldh [rLCDC], a
.lyloop
ldh a, [rLY]
cp 143
jr nz, .lyloop
; Exactly 2 NOPs are needed to sync properly on both CGB-D and newer and CGB-C and older
nop
nop
; Configure timer
ld [rDIV], a ; Synchronize DIV
ld a, $100-(912 / 16) ; Configure modulo so we tick every 912 clocks (The length of a scanline in double speed mode)
ldh [rTMA], a
ld a, $c8 ; Configure the initial value of TIMA so we overflow at the right timing
ldh [rTIMA], a
ld a, 5 ; Enable, tick every 16 CPU clocks
ldh [rTAC], a
; Clear interrupts
xor a
ldh [rIF], a
jp VBlank
; Align to $100
ds $100 - ((@ - Bank0) & $FF)
VBlankJumpTable::
dw HandleFrameRepeatAndUnshiftFramebuffer ; 144
dw HandleSCXAndLoadPalette ;145
dw LoadPalette ; 146
dw CopyPCM24 ; 147
dw CopyPCM26 ; 148
dw CopyPCM26 ; 149
dw CopyPCM26 ; 150
dw CopyPCM26 ; 151
dw CopyPCMLY152 ; 152
; dw ReturnToMain ; During line 153 LY almost always reads 0, so it's handled outside of this table
VBlank::
; Wait for interrupt
halt
xor a
ldh [rIF], a
; Update PCM sample
ld h, audio_buffer >> 8
ldh a, [rLY]
ld l, a
cp 143 ; Result not used, only here to match the cycle count in MainLoop
ld a, [hl]
ldh [rNR50], a
ld a, l
sub 144
jr c, ReturnToMain
add a
ld l, a
ld h, VBlankJumpTable >> 8
ld a, [hli]
ld h, [hl]
ld l, a
jp hl
ReturnToMain::
ld a, b
ld [$2000], a
ldh a, [current_bank + 1]
ld [$3000], a
ld de, 153
ld hl, sp+0
add hl, de
ld a, $80
cp h
jr nz, NoAudioBankSwitch
ldh a, [audio_bank]
inc a
ldh [audio_bank], a
ld sp, $4000
NoAudioBankSwitch::
ld [$ff00 + audio_address], sp
ld hl, $ff00 + current_line
ld a, [hli]
ld h, [hl]
ld l, a
ld sp, hl
ldh a, [first_sample_backup]
ld [audio_buffer], a
ld hl, line_buffer
ld c, rSCY & $ff
jp Main
; We probably have enough CPU cycles to implement compression
CopyPCMLY152::
ld a, [audio_buffer]
ldh [first_sample_backup], a
CopyPCM26::
ld h, audio_buffer >> 8
ld l, c
rept 26 / 2
pop de
ld [hl], e
inc l
ld [hl], d
inc l
endr
ld c, l
jp VBlank ; Not implemented
CopyPCM24::
ld [$ff00 + current_line], sp
ld a, b
ldh [current_bank], a
ld hl, $ff00 + audio_bank
ld a, [hli]
ld [$2000], a
xor a
ld [$3000], a
ld a, [hli]
ld h, [hl]
ld l, a
ld sp, hl
ld hl, audio_buffer
rept 24 / 2
pop de
ld [hl], e
inc l
ld [hl], d
inc l
endr
ld c, l
jp VBlank ; Not implemented
HandleFrameRepeatAndUnshiftFramebuffer::
ld hl, line_buffer
ld d, 144
rept 20
ld a, [hl]
add a, d
ld [hli], a
endr
ldh a, [rSCX]
and a
jp z, VBlank ; A video frame is 2 GB frames
ld a, b
inc a
jr nz, BankNotFF
; A frame must not start at bank $ff
ld [MBC5_bank_low], a
ld b, a
inc a
ld [MBC5_bank_high], a
ldh [current_bank + 1], a
ld sp, $4000
BankNotFF::
ldh a, [frame_repeat]
dec a
jr nz, RepeatFrame
pop de
dec sp
ld a, e
and a
jp z, RestartPlayback
ldh [frame_repeat], a
ld [$ff00 + repeat_line], sp
ld a, b
ldh [repeat_bank], a
ldh a, [current_bank + 1]
ldh [repeat_bank + 1], a
jp VBlank
RepeatFrame::
ldh [frame_repeat], a
ld hl, $ff00 + repeat_bank
ld a, [hli]
ld [MBC5_bank_low], a
ld b, a
ld a, [hli]
ld [MBC5_bank_high], a
ld a, [hli]
ld h, [hl]
ld l, a
ld sp, hl
jp VBlank
HandleSCXAndLoadPalette:
ldh a, [rSCX]
xor 4
ldh [rSCX], a
LoadPalette:
ldh a, [rSCX]
and a
jp nz, VBlank ; A video frame is 2 HW frames
; When here, SP points to a color. Since color is stored in Big Endian
; mode, that byte is not allowed to be $ff. If we do point to $ff,
; it means we need to bank switch.
pop de
dec sp
dec sp
ld a, e
inc a
jp nz, NoBankSwitch
inc b
ld h, MBC5_bank_low >> 8
ld [hl], b ; Write to $20xx to bank switch
ld sp, $4000 ; Reset SP
NoBankSwitch:
ld hl, rBGPD
rept 16
pop de
ld [hl], d
ld [hl], e
endr
jp VBlank
RestartPlayback::
; Clear palette
ld hl, rBGPD
xor a
rept 64
ld [hl], a
endr
; Set SCX to 4. When HandleFrameRepeatAndUnshiftFramebuffer runs,
; it will set it back to 0 and consider this (HW) frame as the start
; of a (video) frame.
ld a, 4
ldh [rSCX], a
; Init variables
xor a
ldh [current_bank + 1], a
ld hl, audio_buffer
ld c, 154
.loop
ld [hli], a
dec c
jr nz, .loop
ld [$3000], a
; Audio starts at 1:4001
inc a
ldh [audio_bank], a
ldh [audio_address], a
ldh [frame_repeat], a ; Set frame repeat to 1 so the VBlank functions load
; a new frame
ld [$2000], a
ld a, $40
ldh [audio_address + 1], a
; Fill line_buffer with $101 - 144. When HandleFrameRepeatAndUnshiftFramebuffer
; runs, it will add 144 to all bytes, setting them to the expected initial
; value of $01. (Note: the encoder currently does not make use of this property)
ld a, $101 - 144
ld c, 20
ld l, h ; hl = line_buffer
.loop2
ld [hli], a
dec c
jr nz, .loop2
ld c, rSCY & $FF ; C is assumed to be SCY when entring Main
; Audio comes first, first byte at ROM1 is the first video bank
ld a, [$4000]
ld b, a ; B is assumed to be the current bank
ld [$2000], a
ld sp, $4000 ; Point SP to video start
ld hl, line_buffer
.lyloop
ldh a, [rLY]
cp 144
jr nz, .lyloop
xor a
ldh [rIF], a
jp VBlank