-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscores.asm
622 lines (518 loc) · 12.1 KB
/
scores.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
; asmsyntax=ca65
; Individual scores - 16 bytes/score = 511 scores (-1 for current seed; other metadata)
; Seed 2 bytes
; Score 4 bytes - base100
; Name 10
; page of 8 per seed - 114 bytes/page of 8 = 71 pages of scores; 568 scores total
; seed 2 bytes
; scores 32 bytes (4 * 8)
; names 80 bytes (10 * 8)
; maybe don't store the seed in ram? only store scores. That way we've got
; room to store all the seeds for the pages in the first page as an index.
; for the seed, increment each byte every alternating frame on title. increment by three.
; eg,
; frame 0 inc rng_seed (x3)
; frame 1 inc rng_seed+1 (x3)
; frame 2 inc rng_seed (x3)
; frame 3 inc rng_seed+1 (x3)
; actually, there's only room for 64-1 tables of scores (minus one for the index)
; = 126 bytes for the index. leaves two bytes for the saved seed.
; lookup table of score tables?
; First two bytes = saved seed
; the rest is the index
Score_IndexPage = $6000 ; ends at $607F. list of 2-byte seeds
;Score_IndexPage:
; .word sc_demo_index
; each page is $80 in size (128 bytes); first table has rng_seed and the last 20
; or so runs, with only seeds and scores, no names.
Score_Tables:
;.word sc_demo_data
.word $6080 ; last 8 runs
.word $6100
.word $6180
.word $6200
; [...]
; each page in the score table:
; entry page
; $xx00-$xx7F
; $xx80-$xxFF
; each entry:
; $00-$03 score
; $04-$0F name
; entry offsets
; $00 entry 1
; $10 entry 2
; $20 entry 3
; $30 entry 4
; $40 entry 5
; $50 entry 6
; $60 entry 7
; $70 entry 8
; move RNG seed to the end?
; rng_seed = $7FFE
; or have the first page the most recent runs?
; just seed and score, no name?
; could have 20 scores listed with only seeds and scores at 8 bytes per entry
Scores_Init:
lda #PPU_MASK_OFF
sta $2001
lda #<Scores_Palette
sta PaletteAddr
lda #>Scores_Palette
sta PaletteAddr+1
jsr LoadPalettes
lda #<Scores_Frame
sta DoFramePointer
lda #>Scores_Frame
sta DoFramePointer+1
lda #<Scores_NMI
sta DoNMIPointer
lda #>Scores_NMI
sta DoNMIPointer+1
jsr ClearSprites
jsr WriteSprites
jsr ClearAttrTable0
; Debug stuff. Overwrites save RAM
; ldx #0
;@dbgloop:
; lda sc_demo_data, x
; sta $6090, x
; inx
; cpx #112
; bne @dbgloop
lda #$20
sta $2006
lda #$00
sta $2006
jsr sc_DrawTwoBlankRows
jsr sc_DrawScrollThing
jsr sc_DrawBGRow
lda #<Scores_BG_RowH1
sta TmpAddr
lda #>Scores_BG_RowH1
sta TmpAddr+1
jsr sc_DrawBGDataRow
jsr sc_DrawBGRow
lda #<Scores_BG_RowH2
sta TmpAddr
lda #>Scores_BG_RowH2
sta TmpAddr+1
jsr sc_DrawBGDataRow
; Inner bits of the scroll graphic (actual background for text)
ldy #18
@blankRowLoop:
jsr sc_DrawBGRow
dey
bne @blankRowLoop
jsr sc_DrawScrollThing
jsr sc_DrawTwoBlankRows
lda #0
jsr Scores_Draw_Page
lda #$00
sta $2005
sta $2005
lda #PPU_CTRL_TITLE
sta $2000
rts
sc_DrawTwoBlankRows:
ldx #64
lda #SC_TILE_BACKGROUND
@loop:
sta $2007
dex
bne @loop
rts
; draw the top and bottom roll of the scroll
sc_DrawScrollThing:
ldx #3
lda #SC_TILE_BACKGROUND
@lp0:
sta $2007
dex
bne @lp0
; 1st header row
lda #SC_TILE_KNOB_R1L0
sta $2007
lda #SC_TILE_KNOB_R1L1
sta $2007
lda #SC_TILE_KNOB_R1L2
sta $2007
lda #SC_TILE_ROLL_TOP
ldx #20
@row1loop:
sta $2007
dex
bne @row1loop
lda #SC_TILE_KNOB_R1R0
sta $2007
lda #SC_TILE_KNOB_R1R1
sta $2007
lda #SC_TILE_KNOB_R1R2
sta $2007
ldx #6
lda #SC_TILE_BACKGROUND
@lp2:
sta $2007
dex
bne @lp2
; 2nd header row
lda #SC_TILE_KNOB_R2L0
sta $2007
lda #SC_TILE_KNOB_R2L1
sta $2007
lda #SC_TILE_KNOB_R2L2
sta $2007
lda #SC_TILE_ROLL_BOTTOM
ldx #20
@row2loop:
sta $2007
dex
bne @row2loop
lda #SC_TILE_KNOB_R2R0
sta $2007
lda #SC_TILE_KNOB_R2R1
sta $2007
lda #SC_TILE_KNOB_R2R2
sta $2007
ldx #3
lda #SC_TILE_BACKGROUND
@lp4:
sta $2007
dex
bne @lp4
rts
sc_DrawBGRow:
ldx #31
@loop:
lda Scores_BG_Row, x
sta $2007
dex
bpl @loop
rts
sc_DrawBGDataRow:
ldy #0
@loop:
lda (TmpAddr), y
sta $2007
iny
cpy #32
bne @loop
rts
; Draw a page of scores. Page index taken from A
Scores_Draw_Page:
; Grab the pointer to the current score table and store it in TmpAddr.
; The ASL A above multiples X by 2, so we can use it as the index to
; this lookup table.
tax
lda Score_Tables, x
sta TmpAddr
inx
lda Score_Tables, x
sta TmpAddr+1
ldx #0
@outer:
; start PPU address for name/seed
lda sc_ppu_high_byte, x
sta $2006
lda sc_ppu_low_byte, x
sta $2006
; Transfer X (the index of the current name) to A and multiply it by 16
; to get the correct byte offsets in the page for the current entry.
; (entries are 16 bytes wide)
txa
; multiply by 16
asl a
asl a
asl a
asl a
tay
; Store the end condition for the score loop in TmpY
clc
adc #2
sta TmpY
; Check for empty name
lda (TmpAddr), y
bne @inner
rts
; name loop
@inner:
lda (TmpAddr), y
lsr a
lsr a
lsr a
lsr a
ora #$F0
sta $2007
lda (TmpAddr), y
ora #$F0
sta $2007
iny
cpy TmpY
bne @inner
tya
clc
adc #10
tay
lda (TmpAddr), y
sta PlayerScore3
iny
lda (TmpAddr), y
sta PlayerScore2
iny
lda (TmpAddr), y
sta PlayerScore1
iny
lda (TmpAddr), y
sta PlayerScore0
; X and Y are clobbered in BufferScoreDisplay. Back them up in the
; stack so our loop isn't borked.
txa
pha
tya
pha
; PlayerScoreBase100 -> PlayerScoreText
jsr BufferScoreDisplay
; Restore the backups of Y and X from the stack.
pla
tay
pla
tax
lda sc_ppu_high_byte_odd, x
sta $2006
lda sc_ppu_low_byte_odd, x
sta $2006
; Draw the score to screen
lda PlayerScoreText+0
sta $2007
lda PlayerScoreText+1
sta $2007
lda #$2D ; altertate ','
sta $2007
lda PlayerScoreText+2
sta $2007
lda PlayerScoreText+3
sta $2007
lda PlayerScoreText+4
sta $2007
lda #$2D
sta $2007
lda PlayerScoreText+5
sta $2007
lda PlayerScoreText+6
sta $2007
lda PlayerScoreText+7
sta $2007
inx
cpx #8
bne @outer_jmp
rts
; branch here because @outer is out of range for BNE
@outer_jmp:
jmp @outer
Scores_Frame:
lda #BUTTON_START
jsr ButtonPressedP1
beq @nobutton
lda #STATES::GS_TITLE
sta current_gamestate
inc gamestate_changed
@nobutton:
jmp WaitFrame
Scores_NMI:
jsr WritePalettes
; scroll to the first nametable
lda #$00
sta $2005
sta $2005
lda #PPU_CTRL_TITLE
sta $2000
jmp NMI_Finished
; Check to see if the current score belongs on the board
Scores_CheckIfNew:
lda #$80
sta SavedScore
lda #$60
sta SavedScore+1
lda #7
jmp scores_NewGreaterThanOld
; new score in an address and the old score as an index
scores_NewGreaterThanOld:
asl a
asl a
asl a
asl a
clc
adc #12
tay
ldx #0
@loop:
lda (SavedScore), y
sta TmpX
lda PlayerScoreBase100, x
cmp TmpX
beq @next ; equal, check next digit
bcc @nope ; less than, we're done
bcs @yup ; greater than, we're done
@next:
iny
inx
cpx #4
bne @loop
cmp TmpX
beq @nope
bcc @nope
@yup:
lda #1
rts
@nope:
lda #0
rts
; Inserts a new score. Entry point for inserting a score into a page.
Scores_InsertNewScore:
jsr Scores_CheckIfNew
bne :+
rts ; score doesn't make the cut
: lda #7
sta TmpY ; index of current saved score
; Starting at the bottom, find the spot for the new score, copying scores down
; a row when needed.
@loop:
lda TmpY
bmi @insert
jsr scores_MoveEntryDown
lda TmpY
jsr scores_NewGreaterThanOld
beq @insert
dec TmpY
jmp @loop
; Spot found, overwrite entry (it's been copied a slot down)
@insert:
; multiply by 16 to get offset from index. The start of the page is used
; with this to get the correct position in Save RAM.
inc TmpY
lda TmpY
asl a
asl a
asl a
asl a
tay
; load up the address of the page to start at
lda Score_Tables
sta TmpAddr
lda Score_Tables+1
sta TmpAddr+1
; Get the ASCII values for the seed, and store them in the correct entry.
lda rng_seed
sta (TmpAddr), y
iny
lda rng_seed+1
sta (TmpAddr), y
; Move offset to the start of the base100 score
tya
clc
adc #11
tay
lda #4
sta TmpX
lda Score_Tables
sta TmpAddr
lda Score_Tables+1
sta TmpAddr+1
; Write the new score to the entry
ldx #0
@b100loop:
lda PlayerScoreBase100, x
sta (TmpAddr), y
inx
iny
cpx #4
bne @b100loop
rts
; Current entry to move down is in A
; TODO: make this work with other tables
scores_MoveEntryDown:
cmp #7 ; if it's the last entry, don't move it anywhere. It'll just get over written.
bne :+
rts
:
; Multiply entry index by 16 to get offset
asl a
asl a
asl a
asl a
tay
lda Score_Tables
sta TmpAddr
lda Score_Tables+1
sta TmpAddr+1
ldx #0
@loopRead:
lda (TmpAddr), y
sta TmpScoreEntry, x
inx
iny
cpx #16
bne @loopRead
ldx #0
@loopWrite:
lda TmpScoreEntry, x
sta (TmpAddr), y
inx
iny
cpx #16
bne @loopWrite
rts
; Tile constants
SC_TILE_BACKGROUND = $03
SC_TILE_ROLL_TOP = $11
SC_TILE_ROLL_BOTTOM = $16
; Scroll roll header left and right (the knobs)
SC_TILE_KNOB_R1L0 = $13
SC_TILE_KNOB_R1L1 = $14
SC_TILE_KNOB_R1L2 = $10
SC_TILE_KNOB_R1R0 = $12
SC_TILE_KNOB_R1R1 = $13
SC_TILE_KNOB_R1R2 = $14
SC_TILE_KNOB_R2L0 = $18
SC_TILE_KNOB_R2L1 = $19
SC_TILE_KNOB_R2L2 = $15
SC_TILE_KNOB_R2R0 = $17
SC_TILE_KNOB_R2R1 = $18
SC_TILE_KNOB_R2R2 = $19
Scores_BG_RowH1:
.byte SC_TILE_BACKGROUND,SC_TILE_BACKGROUND,SC_TILE_BACKGROUND
.byte SC_TILE_BACKGROUND,SC_TILE_BACKGROUND,SC_TILE_BACKGROUND
.byte $00,$00,$00,$00,$00,$00 ;,$00,$00,$00,$00,$00
.byte $30, $31, $32, $33, $34, $35, $36, $37, $38, $00; for seed"
.byte $00,$00,$00,$00 ;,$00,$00,$00 ;,$00 ;,$00
;.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.byte SC_TILE_BACKGROUND,SC_TILE_BACKGROUND,SC_TILE_BACKGROUND
.byte SC_TILE_BACKGROUND,SC_TILE_BACKGROUND,SC_TILE_BACKGROUND
Scores_BG_RowH2:
.byte SC_TILE_BACKGROUND,SC_TILE_BACKGROUND,SC_TILE_BACKGROUND
.byte SC_TILE_BACKGROUND,SC_TILE_BACKGROUND,SC_TILE_BACKGROUND
.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00
;.byte " for seed FFFF"
;.byte $00 ;,$00,$00,$00,$00 ;,$00,$00 ;,$00,$00 ;,$00
;.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.byte SC_TILE_BACKGROUND,SC_TILE_BACKGROUND,SC_TILE_BACKGROUND
.byte SC_TILE_BACKGROUND,SC_TILE_BACKGROUND,SC_TILE_BACKGROUND
Scores_BG_Row:
.byte SC_TILE_BACKGROUND,SC_TILE_BACKGROUND,SC_TILE_BACKGROUND
.byte SC_TILE_BACKGROUND,SC_TILE_BACKGROUND,SC_TILE_BACKGROUND
.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.byte SC_TILE_BACKGROUND,SC_TILE_BACKGROUND,SC_TILE_BACKGROUND
.byte SC_TILE_BACKGROUND,SC_TILE_BACKGROUND,SC_TILE_BACKGROUND
Scores_Palette:
.byte $37,$0F,$17,$07, $37,$05,$15,$13, $37,$0A,$1A,$13, $37,$11,$21,$13
.byte $37,$30,$13,$13, $37,$05,$15,$13, $37,$0A,$1A,$13, $37,$11,$21,$13
; lookup for name rows
.define sc_NamePPUAddresses $2127, $2167, $21A7, $21E7, $2227, $2267, $22A7, $22E7
sc_ppu_high_byte: .hibytes sc_NamePPUAddresses
sc_ppu_low_byte: .lobytes sc_NamePPUAddresses
; lookup for score rows
.define sc_ScorePPUAddresses $214F, $218F, $21CF, $220F, $224F, $228F, $22CF, $230F
sc_ppu_high_byte_odd: .hibytes sc_ScorePPUAddresses
sc_ppu_low_byte_odd: .lobytes sc_ScorePPUAddresses