-
Notifications
You must be signed in to change notification settings - Fork 2
/
screen-decode.asm
558 lines (455 loc) · 8.14 KB
/
screen-decode.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
; asmsyntax=ca65
; Load screen data to the PPU
; TODO: add offset opcode ("start at PPU address")
; TODO: add clear option to load screen? (ie, write $FF unitl the address given in offset opcode)
; TODO: figure out including attribute data in scene data
ScreenTest_Palette:
.byte $0F, $0F, $00, $10
.byte $0F, $00, $20, $10
StaticPalettes:
.byte $0F, $10, $00, $2D
.byte $0F, $2D, $10, $00
.byte $0F, $00, $2D, $10
.byte $0F, $0F, $00, $2D
; Quadrants of the attribute data for better readability
ST_BOTL = %0011_0000
ST_BOTR = %1100_0000
ST_TOPL = %0000_0011
ST_TOPR = %0000_1100
SCREEN_ATTR_01 = %0101_0101
SCREEN_ATTR_02 = %1010_1010
SCREEN_ATTR_03 = %1111_1111
STATIC_START_OFFSET = 1
STATIC_ANIM_RATE = 3
STATIC_TRAVEL_RATE = 4
STATIC_ROW_COUNT = 4
;Init_ScreenTest_new:
lda #$55
jsr FillAttrTable0
lda #SceneIDs::Intro
jmp RunScene
;Init_ScreenTest:
.NMI_Disable
.Disable_Drawing
jsr ClearSprites
jsr WriteSprites
jsr ClearAttrTable0
jsr ClearAttrTable1
;lda #ScreenIDs::News
lda #ScreenIDs::Hood
ldx #$20
jsr LoadScreen
lda #ScreenIDs::TvStatic
ldx #$24
jsr LoadScreen
lda #ScreenIDs::TextBox
ldx #$20
jsr LoadScreen
lda #6
jsr LoadChrData
lda #7
jsr LoadChrData
ldx #0
:
lda ScreenTest_Palette, x
sta PaletteBuffer, x
sta PaletteBufferSprites, x
inx
cpx #8
bne :-
jsr WriteTvAttr
jsr WriteStaticAttributes
InitStatic:
lda #STATIC_START_OFFSET
sta IdxB
lda #0
sta IdxC
sta TmpW ; Static palette animation frame
lda #6
sta IdxD
; Static palette animation interval
lda #STATIC_ANIM_RATE
sta TmpX
; Speed static bars travel down the screen
lda #STATIC_TRAVEL_RATE
sta TmpY
rts
; cycles per scanline: 113 1/3
Frame_ScreenTest:
; ; This controller code moved the start point up and down
; jsr ReadControllers
; lda #BUTTON_UP
; jsr ButtonPressedP1
; beq :+
; dec IdxB
;:
;
; lda #BUTTON_DOWN
; jsr ButtonPressedP1
; beq :+
; inc IdxB
;:
; Turn static on and off on an interval of 6 frames
dec IdxD
bne :+
lda #30
sta IdxD
lda FlipFlop
eor #$FF
sta FlipFlop
; reset the static start line
lda #STATIC_START_OFFSET
sta IdxB
:
lda FlipFlop
beq @NoStatic
; store the number of static blocks
lda #STATIC_ROW_COUNT
sta IdxA
; increment the static start line
dec TmpY
bne :+
lda #STATIC_TRAVEL_RATE
sta TmpY
inc IdxB
:
lda #PPU_CTRL_NMI | PPU_CTRL_SP_PATTERN | 1
jsr WaitForSpriteZero
;sta $2005
;stx $2005
ldy #PPU_CTRL_NMI | PPU_CTRL_SP_PATTERN | 0
; Wait an offset number of scanlines before starting static
ldx IdxB
:
jsr WaitScanline
dex
bne :-
sta $2000
; Turn static on and off every ~4 lines
@static:
ldx #12
; Turn on
:
jsr WaitScanline
dex
bne :-
sty $2000
dec IdxA
bne :+
jmp @staticDone
:
ldx #12
; Turn off
:
jsr WaitScanline
dex
bne :-
sta $2000
jmp @static
@staticDone:
sty $2000
@NoStatic:
rts
WaitScanline:
ora IdxC
.repeat 46
nop
.endrepeat
rts
NMI_ScreenTest:
dec TmpX
lda TmpX
bne :+
lda #STATIC_ANIM_RATE
sta TmpX
inc TmpW
lda TmpW
cmp #4
bcc :+
lda #0
sta TmpW
:
; Write the appropriate static palette to the PPU
lda TmpW
asl a
asl a
tax
lda #$3F
sta $2006
lda #$0C
sta $2006
ldy #4
:
lda StaticPalettes, x
sta $2007
inx
dey
bne :-
rts
WriteTvAttr:
; 23CA
lda #$23
sta $2006
lda #$CA
sta $2006
lda #(ST_BOTL | ST_BOTR) & SCREEN_ATTR_01
sta $2007
sta $2007
sta $2007
sta $2007
lda #$23
sta $2006
lda #$D2
sta $2006
lda #SCREEN_ATTR_01
sta $2007
sta $2007
sta $2007
sta $2007
lda #$23
sta $2006
lda #$DA
sta $2006
lda #(ST_BOTL | ST_BOTR | ST_TOPL | ST_TOPR) & SCREEN_ATTR_01
sta $2007
sta $2007
sta $2007
sta $2007
lda #$23
sta $2006
lda #$E2
sta $2006
lda #(ST_TOPL | ST_TOPR) & SCREEN_ATTR_01
sta $2007
sta $2007
sta $2007
sta $2007
rts
WriteNewsAttr:
; 23CA
lda #$23
sta $2006
lda #$CA
sta $2006
lda #(ST_BOTL | ST_BOTR) & SCREEN_ATTR_01
sta $2007
sta $2007
lda #(ST_BOTL | ST_BOTR) & SCREEN_ATTR_02
sta $2007
sta $2007
lda #$23
sta $2006
lda #$D2
sta $2006
lda #((ST_TOPL | ST_TOPR) & SCREEN_ATTR_01) | ((ST_BOTL | ST_BOTR) & SCREEN_ATTR_02)
sta $2007
sta $2007
; lda #something_else_lol
lda #SCREEN_ATTR_02
sta $2007
sta $2007
lda #$23
sta $2006
lda #$DA
sta $2006
lda #(ST_BOTL | ST_BOTR | ST_TOPL | ST_TOPR) & SCREEN_ATTR_02
sta $2007
sta $2007
sta $2007
sta $2007
lda #$23
sta $2006
lda #$E2
sta $2006
lda #(ST_TOPL | ST_TOPR) & SCREEN_ATTR_02
sta $2007
sta $2007
sta $2007
sta $2007
rts
WriteStaticAttributes:
lda #$27
sta $2006
lda #$CA
sta $2006
lda #ST_BOTL | ST_BOTR
sta $2007
sta $2007
sta $2007
sta $2007
lda #$27
sta $2006
lda #$D2
sta $2006
lda #ST_BOTL | ST_BOTR | ST_TOPL | ST_TOPR
sta $2007
sta $2007
sta $2007
sta $2007
lda #$27
sta $2006
lda #$DA
sta $2006
lda #ST_BOTL | ST_BOTR | ST_TOPL | ST_TOPR
sta $2007
sta $2007
sta $2007
sta $2007
lda #$27
sta $2006
lda #$E2
sta $2006
lda #ST_TOPL | ST_TOPR
sta $2007
sta $2007
sta $2007
sta $2007
rts
; TODO: This needs to be modified or rewritten.
; Expects pointer to screen data in AddressPointer0 and
; the high byte of the Nametable address in X
; The bank number that contains the data should be in IdxA
LoadScreen:
asl a
tay
lda screen_Index, y
sta AddressPointer0
lda screen_Index+1, y
sta AddressPointer0+1
stx TmpY ; High byte of nametable address
bit $2002
;lda #$20
stx $2006
lda #$00
sta $2006
@loop:
; AddressPointer0 should always point to the
; start of a chunk
ldy #0
lda (AddressPointer0), y
tax ; save a copy
; get the length of data
and #$1F
clc
adc #1
sta TmpZ
; get the command from the copy
txa
and #$E0
bne @notDone
lda #0
sta TmpY ; clear out MSB for nametable address
rts
@notDone:
cmp #CHUNK_RLE
bne :+
jsr screen_DecodeRLE
jmp @next
:
cmp #CHUNK_RAW
bne :+
jsr screen_DecodeRAW
jmp @next
:
cmp #CHUNK_ADDR
bne :+
jsr screen_DecodeADDR
jmp @next
:
cmp #CHUNK_SPR
bne :+
jsr screen_DecodeSPR
jmp @next
:
brk ; Invalid command
@next:
iny
; Get address of next chunk
tya
clc
adc AddressPointer0
sta AddressPointer0
lda AddressPointer0+1
adc #0
sta AddressPointer0+1
jmp @loop
screen_DecodeRLE:
lda TmpZ
tax
iny
and #$01
beq @even
; odd
lda (AddressPointer0), y
sta $2007
dex
bne @loop
rts ; there was only one byte of data. just return
@even:
lda (AddressPointer0), y
@loop:
sta $2007
sta $2007
dex
dex
bne @loop
rts
screen_DecodeRAW:
ldx TmpZ
@loop:
iny
lda (AddressPointer0), y
sta $2007
dex
bne @loop
rts
screen_DecodeADDR:
bit $2002
lda TmpY
beq :+
iny
lda (AddressPointer0), y
and #$03
ora TmpY
sta $2006
jmp :++
:
iny
lda (AddressPointer0), y
sta $2006
:
iny
lda (AddressPointer0), y
sta $2006
lda #3
sta TmpZ
rts
screen_DecodeSPR:
iny
lda (AddressPointer0), y
sta AddressPointer2
iny
lda (AddressPointer0), y
sta AddressPointer2+1
tya
pha
ldy #0
ldx #0 ; TODO: load sprites at an offset?
; Load number of sprites
lda (AddressPointer2), y
sta TmpX
iny
:
.repeat 4
lda (AddressPointer2), y
sta Sprites, x
iny
inx
.endrepeat
dec TmpX
bne :-
dey
sty LastSpriteOffset
pla
tay
rts