-
Notifications
You must be signed in to change notification settings - Fork 3
/
taus.s
569 lines (507 loc) · 12.8 KB
/
taus.s
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
;
; Tetris AUS mod: Actually Useful Stats
;
.include "build/tetris.inc"
.include "ips.inc"
.include "chart.inc"
;TESTING_POST_GAME_STATS = 1
.segment "HUNK1"
ips_segment "HUNK1",incrementPieceStat+1 ; $996A
; at incrementPieceStat, replaces lda
jmp statsPerBlock
afterJmpResetStatMod:
.segment "BSS"
copyToPpuDuringRenderAddr:
.res 2
.segment "GAMEBSS"
DHT_index = $00
DHT := statsByType + DHT_index * 2
BRN_index = $01
BRN := statsByType + BRN_index * 2
EFF_index = $02
EFF := statsByType + EFF_index * 2
TRT_index = $03
TRT := statsByType + TRT_index * 2
; stored as little endian bcd
TRNS:
.res 3
tetrisLines:
.res 1
; stored as little endian binary, divided by 2
lvl0Score:
.res 2
; stored as little endian binary; 9 bits
binaryLines:
.res 2
; Maxes out at $0A, at which point gets set back to 0
chartLevelLines:
.res 1
chartLevelPoints:
.res 2
levelEffIdx:
.res 1
chartDrawn:
.res 1
.segment "CODE"
ips_segment "CODE",unreferenced_data4,$0515
initGameState_mod:
.import __GAMEBSS_SIZE__, __GAMEBSS_RUN__
lda #$00
ldx #<__GAMEBSS_SIZE__
@clearByte:
sta __GAMEBSS_RUN__-1,x
dex
bne @clearByte
.ifdef TESTING_POST_GAME_STATS
lda #$0A
sta player1_playState
lda #300/2
jsr chartEffConvert
.if 1
; Various test cases
; separate; left larger, right larger
sta levelEffs
sta levelEffs+3
lda #14
sta levelEffs+1
sta levelEffs+2
; together; left larger, right larger
lda #16
sta levelEffs+4
sta levelEffs+7
lda #14
sta levelEffs+5
sta levelEffs+6
; exactly a difference of 8; left larger, right larger
lda #16
sta levelEffs+8
sta levelEffs+11
lda #8
sta levelEffs+9
sta levelEffs+10
; short
; together, left larger
lda #9
sta levelEffs+12
lda #8
sta levelEffs+13
; separate, right is zero
sta levelEffs+14
.elseif 0
; All maxed out
ldx #$00
@initTestEffs:
sta levelEffs,x
inx
cpx #chartBarCount
bne @initTestEffs
.else
; Descending
ldx #$00
lda #$30
@initTestEffs:
sta levelEffs,x
inx
sta levelEffs,x
inx
sec
sbc #$01
cpx #chartBarCount
bne @initTestEffs
.endif
.endif
ldx #$0F
lda #$00
rts
statsPerBlock:
lda tetriminoTypeFromOrientation,x
cmp #$06 ; i piece
beq @clearDrought
lda #$00
jmp afterJmpResetStatMod
@clearDrought:
lda #$00
sta DHT
sta DHT+1
rts
statsPerLineClear:
; Manage the burn
lda completedLines
jsr switchOnA
.addr statsPerLineClearDone
.addr @worstenBurn1
.addr @worstenBurn2
.addr @worstenBurn3
.addr @healBurn
@healBurn:
lda #$00
sta BRN
sta BRN+1
jmp @afterBurnUpdated
@worstenBurn3:
lda #BRN_index
jsr afterJmpResetStatMod
@worstenBurn2:
lda #BRN_index
jsr afterJmpResetStatMod
@worstenBurn1:
lda #BRN_index
jsr afterJmpResetStatMod
@afterBurnUpdated:
; update lines
lda completedLines
clc
adc binaryLines
sta binaryLines
bcc @updateScore
inc binaryLines+1
@updateScore:
lda completedLines
asl a
tax
lda binaryPointsTable,x
clc
adc lvl0Score
sta lvl0Score
lda binaryPointsTable+1,x
adc lvl0Score+1
sta lvl0Score+1
;updateLevelEff:
ldx completedLines
ldy scorePerLineTable,x
@addToLevelScore:
tya
clc
adc chartLevelPoints
sta chartLevelPoints
lda #$00
adc chartLevelPoints+1
sta chartLevelPoints+1
inc chartLevelLines
lda chartLevelLines
cmp #$0A
bne @addToLevelScore_iter
; compute level EFF
txa
pha
tya
pha
lda chartLevelPoints
sta tmp1
lda chartLevelPoints+1
sta tmp2
lda chartLevelLines
jsr divmod
lda tmp1
jsr chartEffConvert
ldx levelEffIdx
cpx #chartBarCount
beq @dontSave
sta levelEffs,x
inc levelEffIdx
@dontSave:
lda #$00
sta chartLevelLines
sta chartLevelPoints
sta chartLevelPoints+1
pla
tay
pla
tax
@addToLevelScore_iter:
dex
bne @addToLevelScore
@updateEff:
lda lvl0Score
sta tmp1
lda lvl0Score+1
sta tmp2
lda binaryLines+1
beq @loadLines
lsr tmp2
ror tmp1
lda binaryLines
sec
ror a
jmp doDiv
@loadLines:
lda binaryLines
doDiv:
pha
jsr divmod
; calculate one more bit of result
pla
sta tmp3
lda tmp2
asl a
sec
sbc tmp3
rol tmp1
lda #$00
rol a
jsr binaryToBcd
sta EFF
lda tmp2
sta EFF+1
@updateTrt:
lda completedLines
cmp #$04
bne @calcTrt
lda tetrisLines
clc
adc #$04
sta tetrisLines
@calcTrt:
lda tetrisLines
sta tmp1
lda #$00
sta tmp2
jsr multiplyBy100
lda binaryLines
jsr divmod
lda #$00
jsr binaryToBcd
sta TRT
lda tmp2
sta TRT+1
;@checkForTrns:
lda TRNS
bne @doneCheckingTrns
lda TRNS+1
bne @doneCheckingTrns
lda TRNS+2
bne @doneCheckingTrns
lda startLevel
cmp levelNumber
beq @doneCheckingTrns
lda score
sta TRNS
lda score+1
sta TRNS+1
lda score+2
sta TRNS+2
@doneCheckingTrns:
statsPerLineClearDone:
lda #$00
sta completedLines
rts
binaryPointsTable: ; in binary, not bcd. All values pre-divided by 2
.word 0, 40/2, 100/2, 300/2, 1200/2
scorePerLineTable: ; All values pre-divided by 2
.byte 0, 40/1/2, 100/2/2, 300/3/2, 1200/4/2
renderStats:
lda TRNS
bne @hasTrns
lda TRNS+1
bne @hasTrns
lda TRNS+2
bne @hasTrns
beq @checkForAddrToCopy
@hasTrns:
lda #$22
sta PPUADDR
lda #$C4
sta PPUADDR
lda TRNS+2
jsr twoDigsToPPU
lda TRNS+1
jsr twoDigsToPPU
lda TRNS
jsr twoDigsToPPU
@checkForAddrToCopy:
lda copyToPpuDuringRenderAddr+1
beq @ret
sta tmp2
lda copyToPpuDuringRenderAddr
sta tmp1
jsr bulkCopyToPpu+3
lda #$00
sta copyToPpuDuringRenderAddr
sta copyToPpuDuringRenderAddr+1
@ret:
lda #$00
sta $B0
rts
postGameStats:
lda chartDrawn
bne @chartOnPlayfield
inc chartDrawn
jsr drawChartBackground
@chartOnPlayfield:
jsr drawChartSprites
lda frameCounter
and #$03
bne @checkInput
lda #20
sec
sbc chartDrawn
beq @checkInput
sta vramRow
inc chartDrawn
.import chart_attributetable_patch
cmp #20-6-2
bne @checkInput
; Handled by renderStats
lda #<chart_attributetable_patch
sta copyToPpuDuringRenderAddr
lda #>chart_attributetable_patch
sta copyToPpuDuringRenderAddr+1
lda outOfDateRenderFlags
ora #$40
sta outOfDateRenderFlags
@checkInput:
; require pressing start independent of score
lda newlyPressedButtons_player1
cmp #$10
bne @ret
lda player1_score+2
cmp playState_updateGameOverCurtain+$63 ; $9A50: #$03, but can be changed by Game Genie
bcc @exitGame
jsr endingAnimation_maybe
@exitGame:
jmp playState_updateGameOverCurtain+$53 ; $9A64
@ret: rts
; Convert 10 bit binary number (max 999) to bcd. Double dabble algorithm.
; a: (input) 2 high bits of binary number
; (output) low byte
; tmp1: (input) 8 low bits of binary number
; tmp2: (output) high byte
binaryToBcd:
ldy #00
sty tmp2
.if 1
ldy #08
.else
; Uses 5 bytes to save 16 cycles
asl tmp1
rol a
rol tmp2
ldy #07
.endif
@while:
tax
and #$0F
cmp #$05
txa ; Does not change carry
bcc @tensDigit
; carry is set, so it will add +1
adc #$02
tax
@tensDigit:
cmp #$50
bcc @shift
clc
adc #$30
@shift:
asl tmp1
rol a
rol tmp2
dey
bne @while
rts
; Divide 16 bit number by 8 bit number; result must fit in 8 bits
; tmp1: (input) binary dividend LO
; (output) quotient
; tmp2: (input) binary dividend HI
; (output) remainder
; reg a: divisor
divmod:
sta tmp3
ldx #$08
@while:
asl tmp1
rol tmp2
lda tmp2
bcs @withCarry
sec
sbc tmp3
bcc @checkDone
sta tmp2
inc tmp1
jmp @checkDone
@withCarry:
sec
sbc tmp3
bcs @checkDone
sta tmp2
inc tmp1
@checkDone:
dex
bne @while
lda tmp1
rts
; Multiply 16 bit number by 100
; tmp1: (input) LO
; (output) LO
; tmp2: (input) HI
; (output) HI
multiplyBy100:
asl tmp1 ; input =<< 2
rol tmp2
asl tmp1
rol tmp2
lda tmp1 ; output = input
ldx tmp2
asl tmp1 ; input =<< 3
rol tmp2
asl tmp1
rol tmp2
asl tmp1
rol tmp2
clc ; output += input
adc tmp1
tay
txa
adc tmp2
tax
tya
asl tmp1 ; input =<< 1
rol tmp2
clc ; output += input
adc tmp1
tay
txa
adc tmp2
sty tmp1
sta tmp2
rts
.segment "GAME_BG"
ips_segment "GAME_BG",game_nametable,$8000
; game_nametable
.incbin "build/taus_game.nam.stripe"
.byte $FF
.segment "STATS_NUMBER"
; Address happens to be same for PAL
ips_segment "STATS_NUMBER",$9669
; Only show 4 stats
cmp #$04
.segment "JMP_STATS_PER_LINE_CLEAR"
; Address happens to be same for PAL
ips_segment "JMP_STATS_PER_LINE_CLEAR",$9C9E
; at end of addLineClearPoints, replaces "lda #0; sta completedLines"
jsr statsPerLineClear
nop
.segment "JMP_INIT_GAME_STATE"
ips_segment "JMP_INIT_GAME_STATE",gameModeState_initGameState+9 ; $86E5
; at beginning of initGameState, replaces "ldx #$0F; lda #$00"
jsr initGameState_mod
nop
.segment "JMP_POST_GAME_STATS"
; Address happens to be same for PAL
ips_segment "JMP_POST_GAME_STATS",$9A4D
; within @curtainFinished of playState_updateGameOverCurtain, replacing
; "lda player1_score+2; cmp #$03"
jmp postGameStats
; This leaves the cmp cut in half, but we don't jump back to it so this
; is okay. We want to leave the #$03 intact to support Game Genie codes
; that skip the ending animation.
.segment "JMP_RENDER_STATS"
; Address happens to be same for PAL
ips_segment "JMP_RENDER_STATS",$9645
; within render_play_digits, after L9639, replaces "lda #$00; sta $B0"
jsr renderStats
nop
.segment "IPSCHR"
ips_tile_segment "IPSCHR",CHR01+CHR_RIGHT,$54
; percent
.incbin "build/taus.chrs/00"