-
Notifications
You must be signed in to change notification settings - Fork 53
/
failure.asm
574 lines (411 loc) · 9.19 KB
/
failure.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
CheckZSNES:
SEP #$28
LDA.b #$FF
CLC
ADC.b #$FF
CMP.b #$64
REP #$28
BEQ .zsnes
LDA.w #$01FF : TCS ; thing we wrote over - initialize stack
JML ReturnCheckZSNES
.zsnes
JML DontUseZSNES
;===================================================================================================
pushtable
table "data/bsodencode.txt"
; Uncomment this to force a crash to test message
; pushpc : org $008132 : db 0 : pullpc
;===================================================================================================
DontUseZSNES:
SEP #$35 ; sets carry and I flag too
LDA.b #$00
STA.l NMITIMEN ; disable NMI and IRQ
STA.l HDMAEN ; disable HDMA
ROR ; A = 0x80 from carry
STA.l INIDISP
STA.l VMAIN
; Empty VRAM
LDA.b #AllZeros>>16 : STA.l A1B0
REP #$20
LDA.w #AllZeros
STA.l A1T0L
LDA.w #$1809
STA.l DMAP0
LDA.w #$0000
STA.l DAS0L
LDA.w #$0001
STA.l MDMAEN
JSR ConfigurePPUForFailureReport
JSR ConfigureBSODVWF
STZ.b VWFR
LDA.w #ZSNESMessage
JSR DrawVWFMessage
LDA.w #$0F0F
STA.w INIDISP
-- BRA --
ZSNESMessage:
db "It has been detected that you are attempting to load", $80
db "this ROM on a low-quality emulator.", $80
db "The randomizer is designed to work on real hardware,", $80
db "which this application cannot emulate properly;", $80
db "as such, this ROM file refuses to boot.", $80
db "Please upgrade to a more accurate emulator such as", $80
db "SNES9X or BSNES/higan.", $80
db $FF
;===================================================================================================
AllZeros:
db $00
UselessStackTooFar:
dw $137
Crashed:
;===================================================================================================
;===================================================================================================
;===================================================================================================
; !!!DO NOT TRY TO OPTIMIZE THIS CODE!!!
; IT IS INTENTIONALLY AVOIDING CHANGING CERTAIN REGISTERS, ESPECIALLY THE STACK.
;===================================================================================================
;===================================================================================================
;===================================================================================================
SEP #$35 ; sets carry and I flag too
LDA.b #$00
STA.l NMITIMEN ; disable NMI and IRQ
STA.l HDMAEN ; disable HDMA
ROR ; A = 0x80 from carry
STA.l INIDISP
STA.l VMAIN
; Empty VRAM
LDA.b #AllZeros>>16 : STA.l A1B0
REP #$38
LDA.w #AllZeros
STA.l A1T0L
LDA.w #$1809
STA.l DMAP0
LDA.w #$0000
STA.l DAS0L
LDA.w #$0001
STA.l MDMAEN
;===================================================================================================
; Create report
LDA.w #$2100
TCD
;---------------------------------------------------------------------------------------------------
; Report status
; stack pointer
LDA.w #$0C38>>1
STA.b VMADDL
TSC
XBA
AND.w #$00FF
ORA.w #$0100
STA.b VMDATAL
TSC
AND.w #$00FF
ORA.w #$0100
STA.l VMDATAL
; game module
LDA.w #$0C78>>1
STA.b VMADDL
LDA.l GameMode
AND.w #$00FF
ORA.w #$0100
STA.b VMDATAL
LDA.l GameSubMode
AND.w #$00FF
ORA.w #$0100
STA.b VMDATAL
;---------------------------------------------------------------------------------------------------
; Report stack
TSC
INC
STA.l $7F0100
LDA.l UselessStackTooFar
; For now, we can report as much of the stack as possible
; If desired later on, uncomment the code below to keep the stack limited to
; where it was last doing stuff
; TSC
;
; ; keep stack in a useful range
; CMP.w #$01FF ; this means we pulled too much and the stack is useless
; BCC .stack_fine
;
; JMP .skip_stack
;
;.stack_fine
; CMP.l UselessStackTooFar
; BCS .stack_adjusted
;
; ; this means we went too far in, and the stack contains little useful info
; LDA.l UselessStackTooFar
;
;.stack_adjusted
STA.l $7F0000
LDX.w #$01FF
LDA.w #$0C04>>1
.next_row
STA.l $7F0004
STA.b VMADDL
LDY.w #20
TXA
.next_char
; set carry to see if we should color this value as being in the stack
CMP.l $7F0100
LDA.l $000000,X
AND.w #$00FF
ORA.w #$0500
BCS .in_stack
AND.w #$01FF
.in_stack
STA.b VMDATAL
DEX
TXA
CMP.l $7F0000
BEQ .skip_stack
DEY
BNE .next_char
.done_row
CLC
LDA.l $7F0004
ADC.w #32
BRA .next_row
;===================================================================================================
;===================================================================================================
;===================================================================================================
; once the stack is reported, we can start doing stuff that mucks with it
.skip_stack
LDA.w #$0000
TCD
; TSC
; STA.b $80 ; remember stack for later messages
LDA.w #$01FF
TCS
JSR ConfigurePPUForFailureReport
JSR ConfigureBSODVWF
JSR LoadBSODHexFont
STZ.b VWFR
LDA.w #BSODMessage
JSR DrawVWFMessage
LDA.w #$0F0F
STA.w INIDISP
-- BRA --
BSODMessage:
db "A fatal error has occurred and resulted in an", $80
db "unrecoverable crash. ?", $80
db "If you believe this was the result of a bug caused by", $80
db "the randomizer itself, please screenshot this message", $80
db "and share it in the #bug-reports channel of the official", $80
db "ALTTPR discord along with a detailed description of", $80
db "what you were doing, including video if available.", $80
db "Please also make a savestate now and include that in", $80
db "your report.", $80
db $FF
;===================================================================================================
DrawVWFMessage:
STA.b Scrap06
.next
LDA.b (Scrap06)
INC.b Scrap06
AND.w #$00FF
CMP.w #$0080
BEQ .done_row
CMP.w #$00FF
BEQ .done_message
JSR DrawFailureVWFChar
BRA .next
.done_message
RTS
.done_row
LDA.b VWFR
ASL
TAX
LDA.w .row_offset,X
STA.w VMADDL
INC.b VWFR
LDA.w #$1800
STA.w DMAP0
LDA.w #20*16
STA.w DAS0L
LDA.w #$1000
STA.w A1T0L
SEP #$20
STZ.w VMAIN
STZ.w A1B0
LDA.b #$01
STA.w MDMAEN
REP #$20
JSR ResetVFW
BRA .next
.row_offset
dw $10F8
dw $11E8
dw $12D8
dw $13C8
dw $14B8
dw $15A8
dw $1698
dw $1788
dw $1878
dw $1968
dw $1A58
;===================================================================================================
VWFL = $40
VWFX = $44
VWFS = $46
VWFP = $48
VWFR = $4A
;===================================================================================================
DrawFailureVWFChar:
AND.w #$00FF
STA.b VWFL
TAX
ASL
ASL
ASL
ADC.w #BSODFontGFX
STA.b Scrap08
LDA.b VWFP
AND.w #$FFF8
STA.w VWFX
LDA.b VWFP
AND.w #$0007
STA.w VWFS
; carry set on purpose to add letter spacing
SEC
LDA.w BSODCharWidths,X
AND.w #$00FF
ADC.b VWFP
STA.b VWFP
LDY.w #$0000
.next_row
LDA.b ($08),Y
AND.w #$00FF
XBA
LDX.w VWFS
BEQ ++
-- LSR
DEX
BNE --
++ LDX.w VWFX
SEP #$20
ORA.w $1008,X
STA.w $1008,X
XBA
ORA.w $1000,X
STA.w $1000,X
REP #$20
INX
STX.w VWFX
INY
CPY.w #$0008
BCC .next_row
RTS
;===================================================================================================
LoadBSODHexFont:
REP #$20
LDA.w #BSODHex
STA.w A1T0L
LDA.w #$1801
STA.w DMAP0
LDA.w #$1000
STA.w DAS0L
LDA.w #$2800
STA.w VMADDL
SEP #$20
LDA.b #BSODHex>>16
STA.w A1B0
LDA.b #$01
STA.w MDMAEN
REP #$30
RTS
;===================================================================================================
ConfigureBSODVWF:
REP #$30
LDA.w #$2100
TCD
SEP #$30
LDX.b #$FF
LDY.b #$7F
STZ.b CGADD
STZ.b CGDATA : STZ.b CGDATA
STX.b CGDATA : STY.b CGDATA
LDA.b #$05
STA.b CGADD
LDA.b #$11 : STA.b CGDATA : STY.b CGDATA
LDA.b #$21 : STA.b CGADD
STX.b CGDATA : STY.b CGDATA
LDA.b #$25 : STA.b CGADD
LDA.b #$11 : STA.b CGDATA : STY.b CGDATA
REP #$30
PEA.w $0001
LDA.w #15
STA.w $28
LDA.w #$0042>>1
BRA .start
.next_row
PHA
LDA.w $20
CLC
LDA.w $20
ADC.w #32
.start
STA.w $20
STA.b VMADDL
PLA
LDY.w #30
.next_char
STA.b VMDATAL
INC
DEY
BNE .next_char
DEC.w $28
BNE .next_row
LDA.w #$0000
TCD
ResetVFW:
REP #$30
LDX.w #$0400
-- STZ.w $1000,X
DEX
DEX
BPL --
STZ.b VWFL
STZ.b VWFX
STZ.b VWFS
STZ.b VWFP
RTS
;===================================================================================================
ConfigurePPUForFailureReport:
SEP #$30
PHK
PLB
STZ.w BGMODE ; BG mode 0
STZ.w MOSAIC ; no mosaic
STZ.w BG1SC ; BG1 tilemap to $0000
STZ.w TS
STZ.w BG1HOFS : STZ.w BG1HOFS
STZ.w BG1VOFS : STZ.w BG1VOFS
STZ.w BG2HOFS : STZ.w BG2HOFS
STZ.w BG2VOFS : STZ.w BG2VOFS
STZ.w W12SEL
STZ.w CGADSUB
STZ.w SETINI
LDA.b #$04
STA.w BG2SC ; BG1 tilemap to $0800
LDA.b #$21
STA.w BG12NBA
LDA.b #$03
STA.w TM
RTS
;===================================================================================================
BSODHex:
incbin "data/bsodhex.2bpp"
BSODFontGFX:
incbin "data/bsodfont.1bpp"
BSODCharWidths:
; [space]
db 3
; A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
db 4, 4, 3, 4, 3, 3, 4, 3, 1, 4, 3, 3, 5, 4, 4, 4, 5, 4, 4, 3, 4, 5, 5, 3, 3, 3
; 0 1 2 3 4 5 6 7 8 9 . # ? - / , '
db 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 5, 8, 3, 3, 2, 1
;===================================================================================================
pulltable