-
Notifications
You must be signed in to change notification settings - Fork 0
/
tama.asm
7884 lines (7296 loc) · 632 KB
/
tama.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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Addr (inst)
//
// Draws one of the asset graphics in the 0x1000 - 0x1800
// Toggles 0x07D to 0, then 0xF
// Disables interrupts until through rendering
//
render_asset:
ld a, m2 // 0x0 (0xFA2)
and a, 0x7 // 0x1 (0xC87)
ld b, 0x0 // 0x2 (0xE10)
add a, a // 0x3 (0xA80)
add a, a // 0x4 (0xA80)
adc b, b // 0x5 (0xA95)
calz clear_0x07D // 0x6 (0x512)
// Call with
// a = (m2 & 0x7) * 4
// {b, a} make up the graphics page selection
// This is the only way to reach the page of the jump table at 0x1000
pset 0x10 // 0x7 (0xE50)
// This will jp to set_f_0x07D
jp gfx_jp_ret_zp // 0x8 (0x20)
//
// Writes 0xF to 0x07D
// Enables interrupts
// Preseves A, X
//
set_f_0x07D:
push xp // 0x9 (0xFC4)
push xh // 0xA (0xFC5)
push xl // 0xB (0xFC6)
push a // 0xC (0xFC0)
calz zero_a_xp // 0xD (0x5EF)
ld x, 0x7D // 0xE (0xB7D)
// Set 0x07D to 0xF
ld mx, 0xF // 0xF (0xE2F)
// Turn on interrupts
set f, 0x8 // 0x10 (0xF48)
// Returns to caller
jp pop_a_x // 0x11 (0x1A)
//
// Writes 0 to 0x07D
// Clear interupt
// Preserves A, X
//
clear_0x07D:
push xp // 0x12 (0xFC4)
push xh // 0x13 (0xFC5)
push xl // 0x14 (0xFC6)
push a // 0x15 (0xFC0)
calz zero_a_xp // 0x16 (0x5EF)
ld x, 0x7D // 0x17 (0xB7D)
// Set 0x07D to 0
ld mx, 0x0 // 0x18 (0xE20)
// Clear interrupt
rst f, 0x7 // 0x19 (0xF57)
// Fallthrough from clear_0x07D
//
// Pops A, and XP, XH, XL, then returns to caller of set_f_0x07D/clear_0x07D
//
pop_a_x:
pop a // 0x1A (0xFD0)
pop xl // 0x1B (0xFD6)
pop xh // 0x1C (0xFD5)
pop xp // 0x1D (0xFD4)
// Return to caller of set_f_0x07D/clear_0x07D
ret // 0x1E (0xFDF)
//
// Store 0x02 into 0x022/3
// Loop until 0x022 | 0x023 == 0
// Copy 0x026/7 to 0x028/9
// Returns
//
store_0x02_into_0x022_3:
ld x, 0x2 // 0x1F (0xB02)
calz copy_xhl_to_0x022_or_loop_0x023 // 0x20 (0x53C)
//
// Copy 0x026-7 to 0x028-9, and zero 0x027
// Disables interrupts and 0x07D while performing this
// Returns
//
copy_0x026_7_to_8_9:
ld a, 0x0 // 0x21 (0xE00)
ld xp, a // 0x22 (0xE80)
// X is 0x026
ld x, 0x26 // 0x23 (0xB26)
calz clear_0x07D // 0x24 (0x512)
ldpx a, mx // 0x25 (0xEE2)
ld b, mx // 0x26 (0xEC6)
// A is 0x026
// B is 0x027
// Set 0x027 to 0
ldpx mx, 0x0 // 0x27 (0xE60)
calz set_f_0x07D // 0x28 (0x509)
// Set 0x028 to orig 0x026
ldpx mx, a // 0x29 (0xEE8)
// Set 0x029 to orig 0x027
ld mx, b // 0x2A (0xEC9)
ret // 0x2B (0xFDF)
//
// Copy XL, XH to 0x022, 0x023
// Disables interrupts and 0x07D while performing this
// Returns
//
copy_xhl_to_0x022:
ld a, 0x0 // 0x2C (0xE00)
ld yp, a // 0x2D (0xE90)
// Y is 0x022
ld y, 0x22 // 0x2E (0x822)
calz clear_0x07D // 0x2F (0x512)
// Copy XL, XH to 0x022, 0x023
ld my, xl // 0x30 (0xEAB)
ldpy a, a // 0x31 (0xEF0)
ld my, xh // 0x32 (0xEA7)
calz set_f_0x07D // 0x33 (0x509)
ret // 0x34 (0xFDF)
//
// ORs 0x022 against 0x023 and stores in A
// Returns
//
or_0x022_and_0x023:
ld a, 0x0 // 0x35 (0xE00)
ld yp, a // 0x36 (0xE90)
ld y, 0x23 // 0x37 (0x823)
// Load 0x023
ld a, my // 0x38 (0xEC3)
ld y, 0x22 // 0x39 (0x822)
// a = 0x022 | 0x023
or a, my // 0x3A (0xAD3)
ret // 0x3B (0xFDF)
//
// Copy XL, XH to 0x022, 0x023
// ORs 0x022 against 0x023 and stores in A
// Loops until 0x022 | 0x023 == 0
// Returns
//
copy_xhl_to_0x022_or_loop_0x023:
call copy_xhl_to_0x022 // 0x3C (0x42C)
loop_or_0x022_and_0x023:
call or_0x022_and_0x023 // 0x3D (0x435)
// If 0x022 | 0x023 != 0, loop
jp nz, loop_or_0x022_and_0x023 // 0x3E (0x73D)
ret // 0x3F (0xFDF)
//
// Clears the 0x100 page
// Clears 0x02A
// Returns
//
clear_page_0x100:
calz zero_a_xp // 0x40 (0x5EF)
// X is 0x02A
ld x, 0x2A // 0x41 (0xB2A)
// Set 0x02A to 0
lbpx mx, 0x0 // 0x42 (0x900)
// Zero RAM starting at 0x100
ld a, 0x1 // 0x43 (0xE01)
ld b, 0x0 // 0x44 (0xE10)
//
// Clears 8 nibbles at a time until b + 0xF becomes 0
// Starts at {A, 8'h0}
// Returns
//
clear_8_starting_at_a_xp:
ld xp, a // 0x45 (0xE80)
ld x, 0x0 // 0x46 (0xB00)
//
// Clears 8 nibbles at a time until b + 0xF becomes 0
// Returns
//
loop_clear_8:
lbpx mx, 0x0 // 0x47 (0x900)
lbpx mx, 0x0 // 0x48 (0x900)
lbpx mx, 0x0 // 0x49 (0x900)
lbpx mx, 0x0 // 0x4A (0x900)
lbpx mx, 0x0 // 0x4B (0x900)
lbpx mx, 0x0 // 0x4C (0x900)
lbpx mx, 0x0 // 0x4D (0x900)
lbpx mx, 0x0 // 0x4E (0x900)
add b, 0xF // 0x4F (0xC1F)
jp nz, loop_clear_8 // 0x50 (0x747)
ret // 0x51 (0xFDF)
label_13:
pset 0x4 // 0x52 (0xE44)
call label_145 // 0x53 (0x486)
calz_copy_buf_and_render_misc:
pset 0x4 // 0x54 (0xE44)
call copy_buf_and_render_misc // 0x55 (0x400)
//
// Copies video buffer data from {1, 0x02B, 0x02A} to VRAM at 0xE00/0xE80
// Returns
//
copy_video_buf_to_vram:
calz zero_a_xp // 0x56 (0x5EF)
ld x, 0x2A // 0x57 (0xB2A)
ld yl, mx // 0x58 (0xE9A)
ldpx a, a // 0x59 (0xEE0)
ld yh, mx // 0x5A (0xE96)
ld a, 0x1 // 0x5B (0xE01)
// Set Y to {1, 0x02B, 0x02A}
ld yp, a // 0x5C (0xE90)
ld a, 0xE // 0x5D (0xE0E)
ld xp, a // 0x5E (0xE80)
// Set X to 0xE00
// Start of video RAM
ld x, 0x0 // 0x5F (0xB00)
// Increments X to 0xE10 and Y to
call copy_16_mx_my_ret // 0x60 (0x48D)
// Clear carry (it shouldn't have been set?)
rst f, 0xE // 0x61 (0xF5E)
// Assuming we started at 0x100, we'd be at 0x110, then offset to 0x180
adc yh, 0x7 // 0x62 (0xA27)
// Set X to 0xE80 (second video RAM bank)
ld x, 0x80 // 0x63 (0xB80)
call copy_16_mx_my_ret // 0x64 (0x48D)
// Clear carry
rst f, 0xE // 0x65 (0xF5E)
// Assuming we started at 0x100, we'd be at 0x190, then offset to 0x110
adc yh, 0x8 // 0x66 (0xA28)
// Set X to 0xE12 (a bit offset from last first video bank write)
ld x, 0x12 // 0x67 (0xB12)
call copy_16_mx_my_ret // 0x68 (0x48D)
// Clear carry
rst f, 0xE // 0x69 (0xF5E)
// Assuming we started at 0x100, we'd be at 0x120, then offset to 0x190
adc yh, 0x7 // 0x6A (0xA27)
// Set X to 0xE92
ld x, 0x92 // 0x6B (0xB92)
call copy_16_mx_my_ret // 0x6C (0x48D)
// Clear carry
rst f, 0xE // 0x6D (0xF5E)
// Assuming we started at 0x100, we'd be at 0x1A0, then offset to 0x120
adc yh, 0x8 // 0x6E (0xA28)
// Set X to 0xE48
ld x, 0x48 // 0x6F (0xB48)
// Set carry
set f, 0x1 // 0x70 (0xF41)
call copy_10_mx_my_offset_ret // 0x71 (0x49F)
ld x, 0x3E // 0x72 (0xB3E)
set f, 0x1 // 0x73 (0xF41)
call copy_6_mx_my_offset_ret // 0x74 (0x4A7)
rst f, 0xE // 0x75 (0xF5E)
adc yh, 0x7 // 0x76 (0xA27)
ld x, 0xC8 // 0x77 (0xBC8)
set f, 0x1 // 0x78 (0xF41)
call copy_10_mx_my_offset_ret // 0x79 (0x49F)
ld x, 0xBE // 0x7A (0xBBE)
set f, 0x1 // 0x7B (0xF41)
call copy_6_mx_my_offset_ret // 0x7C (0x4A7)
rst f, 0xE // 0x7D (0xF5E)
adc yh, 0x8 // 0x7E (0xA28)
ld x, 0x36 // 0x7F (0xB36)
set f, 0x1 // 0x80 (0xF41)
call copy_8_mx_my_offset_ret // 0x81 (0x4A3)
ld x, 0x2E // 0x82 (0xB2E)
set f, 0x1 // 0x83 (0xF41)
call copy_8_mx_my_offset_ret // 0x84 (0x4A3)
rst f, 0xE // 0x85 (0xF5E)
adc yh, 0x7 // 0x86 (0xA27)
ld x, 0xB6 // 0x87 (0xBB6)
set f, 0x1 // 0x88 (0xF41)
call copy_8_mx_my_offset_ret // 0x89 (0x4A3)
ld x, 0xAE // 0x8A (0xBAE)
set f, 0x1 // 0x8B (0xF41)
jp copy_8_mx_my_offset_ret // 0x8C (0xA3)
copy_16_mx_my_ret:
call copy_8_mx_my_ret // 0x8D (0x48F)
ldpx a, a // 0x8E (0xEE0)
//
// Copy 8 nibbles from MX, MX + 7 to MY, MY + 7
// Returns
//
copy_8_mx_my_ret:
ldpy mx, my // 0x8F (0xEFB)
ldpx a, a // 0x90 (0xEE0)
ldpy mx, my // 0x91 (0xEFB)
ldpx a, a // 0x92 (0xEE0)
ldpy mx, my // 0x93 (0xEFB)
ldpx a, a // 0x94 (0xEE0)
ldpy mx, my // 0x95 (0xEFB)
ldpx a, a // 0x96 (0xEE0)
ldpy mx, my // 0x97 (0xEFB)
ldpx a, a // 0x98 (0xEE0)
//
// Copy three nibbles from MX, MX + 2 to MY, MY + 2
// Returns
//
copy_3_mx_my_ret:
ldpy mx, my // 0x99 (0xEFB)
ldpx a, a // 0x9A (0xEE0)
//
// Copy two nibbles from MX, MX + 1 to MY, MY + 1
// Returns
//
copy_2_mx_my_ret:
ldpy mx, my // 0x9B (0xEFB)
ldpx a, a // 0x9C (0xEE0)
ldpy mx, my // 0x9D (0xEFB)
ret // 0x9E (0xFDF)
//
// Copies 10 nibbles from MX, MX + 9 to MY, MY + 9
// Every two copies offsets XL by 0xC
// Returns
//
copy_10_mx_my_offset_ret:
ldpy mx, my // 0x9F (0xEFB)
ldpx a, a // 0xA0 (0xEE0)
ldpy mx, my // 0xA1 (0xEFB)
adc xl, 0xC // 0xA2 (0xA1C)
//
// Copies 8 nibbles from MX, MX + 7 to MY, MY + 7
// Every two copies offsets XL by 0xC
// Returns
//
copy_8_mx_my_offset_ret:
ldpy mx, my // 0xA3 (0xEFB)
ldpx a, a // 0xA4 (0xEE0)
ldpy mx, my // 0xA5 (0xEFB)
adc xl, 0xC // 0xA6 (0xA1C)
//
// Copies 6 nibbles from MX, MX + 5 to MY, MY + 5
// Every two copies offsets XL by 0xC
// Returns
//
copy_6_mx_my_offset_ret:
ldpy mx, my // 0xA7 (0xEFB)
ldpx a, a // 0xA8 (0xEE0)
ldpy mx, my // 0xA9 (0xEFB)
adc xl, 0xC // 0xAA (0xA1C)
ldpy mx, my // 0xAB (0xEFB)
ldpx a, a // 0xAC (0xEE0)
ldpy mx, my // 0xAD (0xEFB)
adc xl, 0xC // 0xAE (0xA1C)
ldpy mx, my // 0xAF (0xEFB)
ldpx a, a // 0xB0 (0xEE0)
ldpy mx, my // 0xB1 (0xEFB)
ret // 0xB2 (0xFDF)
//
// Check if 0x04A highest bit is set. If so, zero will be unset, otherwise set
// Returns
//
check_0x04A_highbit:
calz zero_a_xp // 0xB3 (0x5EF)
// Set X to 0x04A
ld x, 0x4A // 0xB4 (0xB4A)
fan mx, 0x8 // 0xB5 (0xDA8)
ret // 0xB6 (0xFDF)
//
// Checks if 0x07B is set, if so:
// Clears 0x032, copies YHL to 0x036/7
// Zeros B
// Sets 0x058/9 to A, B
// 0x07D is disabled during this operation
// Returns
//
xp_0_if_0x7B_set_clear_0x32_store_yhl:
calz zero_b_xp // 0xB7 (0x5F2)
jp if_0x7B_set_clear_0x32_store_yhl // 0xB8 (0xBF)
// TODO: I don't think this is reachable
calz zero_a_xp // 0xB9 (0x5EF)
jp if_0x7B_set_clear_0x32_store_yhl // 0xBA (0xBF)
//
// Checks if 0x07B is set, if so:
// Clears 0x032, copies 0x94 to 0x036/7
// Sets 0x058 to A, 0x059 to 0xF
// 0x07D is disabled during this operation
// Returns
//
if_0x7B_set_clear_0x32_store_0x94_to_0x036_7_set_0x059_to_0xF:
ld y, 0x94 // 0xBB (0x894)
//
// Checks if 0x07B is set, if so:
// Clears 0x032, copies YHL to 0x036/7
// Sets 0x058 to A, 0x059 to 0xF
// 0x07D is disabled during this operation
// Returns
//
if_0x7B_set_clear_0x32_store_yhl_set_0x059_to_0xF:
ld b, 0xF // 0xBC (0xE1F)
ld a, 0x0 // 0xBD (0xE00)
ld xp, a // 0xBE (0xE80)
//
// Checks if 0x?7B is set, if so:
// Clears 0x?32, copies YHL to 0x?36/7
// Sets 0x?58/9 to A, B
// 0x07D is disabled during this operation
// Returns
//
if_0x7B_set_clear_0x32_store_yhl:
// X is 0x?7B
ld x, 0x7B // 0xBF (0xB7B)
cp mx, 0x0 // 0xC0 (0xDE0)
// If 0x?7B is 0, return
jp z, if_0x7B_set_clear_0x32_store_yhl_ret // 0xC1 (0x6CD)
//
// Clears 0x?32, copies YHL to 0x?36/7
// Sets 0x?58/9 to A, B
// 0x07D is disabled during this operation
// Returns
//
clear_0x32_store_yhl:
// X is 0x?32
ld x, 0x32 // 0xC2 (0xB32)
calz clear_0x07D // 0xC3 (0x512)
// Set 0x?32 to 0
lbpx mx, 0x0 // 0xC4 (0x900)
// X is 0x?36
ld x, 0x36 // 0xC5 (0xB36)
// Set 0x?36 to yl
ld mx, yl // 0xC6 (0xEBA)
ldpx a, a // 0xC7 (0xEE0)
// Set 0x?37 to yh
ld mx, yh // 0xC8 (0xEB6)
// X is 0x?58
ld x, 0x58 // 0xC9 (0xB58)
// Set 0x?58 to A
ldpx mx, a // 0xCA (0xEE8)
// Set 0x?59 to B
ld mx, b // 0xCB (0xEC9)
calz set_f_0x07D // 0xCC (0x509)
if_0x7B_set_clear_0x32_store_yhl_ret:
ret // 0xCD (0xFDF)
label_30:
ld a, 0x0 // 0xCE (0xE00)
ld xp, a // 0xCF (0xE80)
ld x, 0x2A // 0xD0 (0xB2A)
lbpx mx, 0x40 // 0xD1 (0x940)
ld x, 0x3A // 0xD2 (0xB3A)
ret // 0xD3 (0xFDF)
label_31:
calz clear_0x07D // 0xD4 (0x512)
ldpy a, my // 0xD5 (0xEF3)
jp label_33 // 0xD6 (0xDB)
label_32:
calz clear_0x07D // 0xD7 (0x512)
ldpy a, my // 0xD8 (0xEF3)
or a, my // 0xD9 (0xAD3)
ldpy a, a // 0xDA (0xEF0)
label_33:
or a, my // 0xDB (0xAD3)
calz set_f_0x07D // 0xDC (0x509)
ret // 0xDD (0xFDF)
label_34:
calz label_30 // 0xDE (0x5CE)
lbpx mx, 0x40 // 0xDF (0x940)
pset 0x7 // 0xE0 (0xE47)
jp label_233 // 0xE1 (0x92)
label_35:
ld x, 0x12 // 0xE2 (0xB12)
set f, 0x4 // 0xE3 (0xF44)
label_36:
add mx, 0x1 // 0xE4 (0xC21)
ldpx a, a // 0xE5 (0xEE0)
adc mx, 0x0 // 0xE6 (0xC60)
cp mx, 0x6 // 0xE7 (0xDE6)
jp c, label_37 // 0xE8 (0x2EA)
ld mx, 0x0 // 0xE9 (0xE20)
label_37:
rst f, 0xB // 0xEA (0xF5B)
ret // 0xEB (0xFDF)
label_38:
ld a, 0x0 // 0xEC (0xE00)
springboard_label_238:
pset 0x7 // 0xED (0xE47)
jp label_238 // 0xEE (0xC0)
//
// Zeros A and XP, then returns
//
zero_a_xp:
ld a, 0x0 // 0xEF (0xE00)
ld xp, a // 0xF0 (0xE80)
ret // 0xF1 (0xFDF)
//
// Zeros B and XP, then returns
//
zero_b_xp:
ld b, 0x0 // 0xF2 (0xE10)
ld xp, b // 0xF3 (0xE81)
ret // 0xF4 (0xFDF)
//
// Sets A and XP to 1, then returns
//
one_a_xp:
ld a, 0x1 // 0xF5 (0xE01)
ld xp, a // 0xF6 (0xE80)
ret // 0xF7 (0xFDF)
//
// Using the provided XP, checks if 0xX5D == 1, then returns
//
check_0xX5D_is_1:
ld x, 0x5D // 0xF8 (0xB5D)
cp mx, 0x1 // 0xF9 (0xDE1)
ret // 0xFA (0xFDF)
//
// Zeros A and XP
// Checks if high bit of 0x048 is set (zero high if not)
// Returns
//
zero_a_xp_and_bit_high_at_0x048:
ld a, 0x0 // 0xFB (0xE00)
ld xp, a // 0xFC (0xE80)
//
// Checks if high bit of 0x048 is set (zero high if not), then returns
//
bit_high_at_0x048:
ld x, 0x48 // 0xFD (0xB48)
fan mx, 0x8 // 0xFE (0xDA8)
ret // 0xFF (0xFDF)
// Vector table
jp reset_vector // 0x100 (0x10)
jp generic_int // 0x101 (0x16)
jp clock_timer_int // 0x102 (0x1D)
jp generic_int // 0x103 (0x16)
jp generic_int // 0x104 (0x16)
jp generic_int // 0x105 (0x16)
jp generic_int // 0x106 (0x16)
jp generic_int // 0x107 (0x16)
jp generic_int // 0x108 (0x16)
jp generic_int // 0x109 (0x16)
jp generic_int // 0x10A (0x16)
jp generic_int // 0x10B (0x16)
jp prog_timer_int // 0x10C (0x6F)
jp generic_int // 0x10D (0x16)
jp generic_int // 0x10E (0x16)
jp generic_int // 0x10F (0x16)
reset_vector:
rst f, 0x0 // 0x110 (0xF50)
ld a, 0xF // 0x111 (0xE0F)
ld sph, a // 0x112 (0xFE0)
ld spl, a // 0x113 (0xFF0)
pset 0x2 // 0x114 (0xE42)
jp reset_vect_cont // 0x115 (0x2A)
generic_int:
push f // 0x116 (0xFCA)
push a // 0x117 (0xFC0)
push b // 0x118 (0xFC1)
push xp // 0x119 (0xFC4)
push xh // 0x11A (0xFC5)
push xl // 0x11B (0xFC6)
jp int_restore_ret // 0x11C (0x5B)
clock_timer_int:
push f // 0x11D (0xFCA)
push a // 0x11E (0xFC0)
push b // 0x11F (0xFC1)
push xp // 0x120 (0xFC4)
push xh // 0x121 (0xFC5)
push xl // 0x122 (0xFC6)
// Disable decimal
rst f, 0xB // 0x123 (0xF5B)
ld a, 0xF // 0x124 (0xE0F)
ld xp, a // 0x125 (0xE80)
ld x, 0x0 // 0x126 (0xB00)
// a = 0xF00. Clear clock timer factor flag
ld a, mx // 0x127 (0xEC2)
ld x, 0x76 // 0x128 (0xB76)
// Set 0xF77-6 to 0x01. Reset watchdog timer
lbpx mx, 0x1 // 0x129 (0x901)
// Set 0xF79-8 to 0x21
// 0xF78: Start progamable timer
// 0xF79: Choose prog timer clock interval 256Hz
lbpx mx, 0x21 // 0x12A (0x921)
ld x, 0x12 // 0x12B (0xB12)
// Set 0xF12 to 1. Enable prog timer interrupt mask
ld mx, 0x1 // 0x12C (0xE21)
ld a, 0x0 // 0x12D (0xE00)
ld xp, a // 0x12E (0xE80)
ld x, 0x57 // 0x12F (0xB57)
// Check if 0x057 is 0
cp mx, 0x0 // 0x130 (0xDE0)
// If so, skip add
jp z, clock_timer_int_skip_add // 0x131 (0x633)
// Otherwise, add F to 0x057
add mx, 0xF // 0x132 (0xC2F)
clock_timer_int_skip_add:
ld x, 0x3C // 0x133 (0xB3C)
// Check if 0x03C is 0
cp mx, 0x0 // 0x134 (0xDE0)
// If so, jump
jp z, label_52 // 0x135 (0x64A)
// Enable decimal
set f, 0x4 // 0x136 (0xF44)
ld x, 0x2E // 0x137 (0xB2E)
// Set 0x02E to 1
ld mx, 0x1 // 0x138 (0xE21)
ld x, 0x10 // 0x139 (0xB10)
// Add 1 to 0x010
// Looks like this is incrementing the second
add mx, 0x1 // 0x13A (0xC21)
// Increment X
ldpx a, a // 0x13B (0xEE0)
// Add carry from 0x010 add to 0x011
// Second digit of second
adc mx, 0x0 // 0x13C (0xC60)
// Check if 0x011 is 6
cp mx, 0x6 // 0x13D (0xDE6)
// If so, jump
jp c, label_51 // 0x13E (0x249)
// Set 0x011 to 0
ldpx mx, 0x0 // 0x13F (0xE60)
calz label_36 // 0x140 (0x5E4)
jp c, label_50 // 0x141 (0x247)
// TODO: Unused?
ld x, 0x14 // 0x142 (0xB14)
ldpx a, mx // 0x143 (0xEE2)
ldpx b, mx // 0x144 (0xEE6)
ld x, 0x14 // 0x145 (0xB14)
call label_67 // 0x146 (0x4F4)
label_50:
pset 0xC // 0x147 (0xE4C)
call label_312 // 0x148 (0x47A)
label_51:
// Clear decimal
rst f, 0xB // 0x149 (0xF5B)
label_52:
ld a, 0x0 // 0x14A (0xE00)
ld xp, a // 0x14B (0xE80)
// X is 0x02F
ld x, 0x2F // 0x14C (0xB2F)
add mx, 0x1 // 0x14D (0xC21)
ldpx a, a // 0x14E (0xEE0)
// If 0x02F carried, add 1 to 0x030
acpx mx, a // 0x14F (0xF28)
// If 0x030 carried, add 1 to 0x031
adc mx, 0x0 // 0x150 (0xC60)
jp nc, int_restore_ret // 0x151 (0x35B)
ld a, 0xF // 0x152 (0xE0F)
ld xp, a // 0x153 (0xE80)
// X is 0xF70
ld x, 0x70 // 0x154 (0xB70)
// AND with CPU voltage switch
fan mx, 0x3 // 0x155 (0xDA3)
// If voltage is < 3.1V, jump
jp z, int_restore_ret // 0x156 (0x65B)
call check_svd_voltage // 0x157 (0x4E5)
// If voltage is normal, jump
jp z, int_restore_ret // 0x158 (0x65B)
ld x, 0x70 // 0x159 (0xB70)
// Clear all CPU voltage settings
ld mx, 0x0 // 0x15A (0xE20)
int_restore_ret:
ld a, 0x0 // 0x15B (0xE00)
ld xp, a // 0x15C (0xE80)
ld x, 0x7D // 0x15D (0xB7D)
// Check if 0x07D is 0
cp mx, 0x0 // 0x15E (0xDE0)
// If so, jump, and don't re-enable interrupts
jp z, gen_int_dis_int // 0x15F (0x668)
pop xl // 0x160 (0xFD6)
pop xh // 0x161 (0xFD5)
pop xp // 0x162 (0xFD4)
pop b // 0x163 (0xFD1)
pop a // 0x164 (0xFD0)
pop f // 0x165 (0xFDA)
// Enable interrupts
set f, 0x8 // 0x166 (0xF48)
ret // 0x167 (0xFDF)
gen_int_dis_int:
pop xl // 0x168 (0xFD6)
pop xh // 0x169 (0xFD5)
pop xp // 0x16A (0xFD4)
pop b // 0x16B (0xFD1)
pop a // 0x16C (0xFD0)
pop f // 0x16D (0xFDA)
ret // 0x16E (0xFDF)
prog_timer_int:
push f // 0x16F (0xFCA)
push a // 0x170 (0xFC0)
push b // 0x171 (0xFC1)
push xp // 0x172 (0xFC4)
push xh // 0x173 (0xFC5)
push xl // 0x174 (0xFC6)
rst f, 0xB // 0x175 (0xF5B)
call configure_buzzer // 0x176 (0x4A0)
ld a, 0xF // 0x177 (0xE0F)
ld xp, a // 0x178 (0xE80)
// X is 0xF02
ld x, 0x2 // 0x179 (0xB02)
// Clear prog timer factor flags
ld a, mx // 0x17A (0xEC2)
// X is 0xF40
ld x, 0x40 // 0x17B (0xB40)
// Store current button input in B
ld b, mx // 0x17C (0xEC6)
// Get the off bits (the buttons that are pressed)
xor b, 0xF // 0x17D (0xD1F)
// The top bit is unused
and b, 0x7 // 0x17E (0xC97)
ld a, 0x0 // 0x17F (0xE00)
ld xp, a // 0x180 (0xE80)
// X is 0x05A
ld x, 0x5A // 0x181 (0xB5A)
// Increment 0x05A
add mx, 0x1 // 0x182 (0xC21)
// X is 0x022
ld x, 0x22 // 0x183 (0xB22)
// Decrement 0x022
add mx, 0xF // 0x184 (0xC2F)
ldpx a, a // 0x185 (0xEE0)
// Decrement 0x023, unless previous add carried
adc mx, 0xF // 0x186 (0xC6F)
// If carried, jump
jp c, prog_timer_int_skip_clear_0x022 // 0x187 (0x28A)
ld x, 0x22 // 0x188 (0xB22)
// Clear 0x022
lbpx mx, 0x0 // 0x189 (0x900)
prog_timer_int_skip_clear_0x022:
// X is 0x026
ld x, 0x26 // 0x18A (0xB26)
// A = 0x026, the previous button input
ld a, mx // 0x18B (0xEC2)
// Store current button input into 0x026
ldpx mx, b // 0x18C (0xEE9)
// Find differences between last input and current input
xor a, b // 0x18D (0xAE1)
and a, b // 0x18E (0xAC1)
// OR this value with 0x027
or mx, a // 0x18F (0xAD8)
// X is 0x03D
ld x, 0x3D // 0x190 (0xB3D)
cp mx, b // 0x191 (0xF09)
// If 0x03D == the current pressed buttons
jp z, prog_timer_int_check_button_hold // 0x192 (0x697)
// Update 0x03D to the latest pressed buttons
ld mx, b // 0x193 (0xEC9)
ld x, 0x3E // 0x194 (0xB3E)
// Clear 0x03E
ld mx, 0x0 // 0x195 (0xE20)
jp prog_timer_int_ret // 0x196 (0x9F)
prog_timer_int_check_button_hold:
// X is 0x03E
ld x, 0x3E // 0x197 (0xB3E)
// Increment 0x03E
add mx, 0x1 // 0x198 (0xC21)
// If 0x03E isn't 0, return
jp nz, prog_timer_int_ret // 0x199 (0x79F)
// Set 0x03E to 8
// Got the same button value for 8 ticks
ld mx, 0x8 // 0x19A (0xE28)
// X is 0x03F
ld x, 0x3F // 0x19B (0xB3F)
// AND current button value with 0x03F
and b, mx // 0x19C (0xAC6)
// X is 0x027
ld x, 0x27 // 0x19D (0xB27)
// Store this ANDed value into 0x027
or mx, b // 0x19E (0xAD9)
prog_timer_int_ret:
jp int_restore_ret // 0x19F (0x5B)
//
// Configure buzzer
// Looks like buzzer code is polled (timer based on 0x032/3), and it plays the previously
// loaded sound effect, and loads the next one
// Returns
//
configure_buzzer:
ld a, 0x0 // 0x1A0 (0xE00)
ld xp, a // 0x1A1 (0xE80)
// X is 0x032
ld x, 0x32 // 0x1A2 (0xB32)
// Decrement 0x032
add mx, 0xF // 0x1A3 (0xC2F)
ldpx a, a // 0x1A4 (0xEE0)
// Decrement 0x033, unless previous add carried
adc mx, 0xF // 0x1A5 (0xC6F)
// If add didn't carry, jump
jp nc, loop_59 // 0x1A6 (0x3AB)
// X is 0x034
ld x, 0x34 // 0x1A7 (0xB34)
fan mx, 0x8 // 0x1A8 (0xDA8)
// If high bit is set, jump
jp nz, dec_0x038_off_load_prot_buzzer // 0x1A9 (0x7D7)
ret // 0x1AA (0xFDF)
loop_59:
// XP is 0
// X is 0x036
ld x, 0x36 // 0x1AB (0xB36)
// A = 0x036
ld a, mx // 0x1AC (0xEC2)
// Increment 0x036 by 2
add mx, 0x2 // 0x1AD (0xC22)
ldpx a, a // 0x1AE (0xEE0)
// B = 0x037
ld b, mx // 0x1AF (0xEC6)
// If prev add overflowed, add 1 to 0x037
adc mx, 0x0 // 0x1B0 (0xC60)
// X is 0x032
ld x, 0x32 // 0x1B1 (0xB32)
pset 0xE // 0x1B2 (0xE4E)
call jp_table_0xE7D_buzzer // 0x1B3 (0x47C)
cp xl, 0x6 // 0x1B4 (0xA56)
// If XL is 6, jump
// This happens when we only loaded 4 nibbles, rather than 6
jp z, buzzer_control // 0x1B5 (0x6C1)
// X is 0x059
ld x, 0x59 // 0x1B6 (0xB59)
cp mx, 0xF // 0x1B7 (0xDEF)
// If 0x59 is 0xF, jump
jp z, buzzer_control // 0x1B8 (0x6C1)
// X is 0x058
ld x, 0x58 // 0x1B9 (0xB58)
// Subtract 1
add mx, 0xF // 0x1BA (0xC2F)
ldpx a, a // 0x1BB (0xEE0)
// Subtract 1 if no carry
adc mx, 0xF // 0x1BC (0xC6F)
// If carry, jump
jp c, buzzer_control // 0x1BD (0x2C1)
// X is 0x036
ld x, 0x36 // 0x1BE (0xB36)
// Set 0x036/7 to 0x7D
lbpx mx, 0x7D // 0x1BF (0x97D)
jp loop_59 // 0x1C0 (0xAB)
//
// Buzzer control
// Returns
//
buzzer_control:
// XP is 0
// X is 0x034
ld x, 0x34 // 0x1C1 (0xB34)
fan mx, 0x8 // 0x1C2 (0xDA8)
// If high bit set, jump. This means it was a one-shot buzzer call
jp nz, dec_0x038_off_load_prot_buzzer // 0x1C3 (0x7D7)
// X is 0x038
ld x, 0x38 // 0x1C4 (0xB38)
// Store 0x58 into 0x038/9
lbpx mx, 0x58 // 0x1C5 (0x958)
ld a, 0xF // 0x1C6 (0xE0F)
ld xp, a // 0x1C7 (0xE80)
// X is 0xF71
ld x, 0x71 // 0x1C8 (0xB71)
// Turn on heavy load protection mode
or mx, 0x1 // 0x1C9 (0xCE1)
push xp // 0x1CA (0xFC4)
ld a, 0x0 // 0x1CB (0xE00)
ld xp, a // 0x1CC (0xE80)
// X is 0x034
ld x, 0x34 // 0x1CD (0xB34)
// A = 0x034
ldpx a, mx // 0x1CE (0xEE2)
// B = 0x035
ld b, mx // 0x1CF (0xEC6)
pop xp // 0x1D0 (0xFD4)
// X is 0xF74
ld x, 0x74 // 0x1D1 (0xB74)
// Set buzzer frequency to 0x034
ldpx mx, a // 0x1D2 (0xEE8)
// Set buzzer envelope to 0x035
ld mx, b // 0x1D3 (0xEC9)
// X is 0xF54
ld x, 0x54 // 0x1D4 (0xB54)
// Turn on buzzer output port
and mx, 0x7 // 0x1D5 (0xCA7)
ret // 0x1D6 (0xFDF)
//
// Decrements 0x038 and maybe 0x039
// Sometimes turns off heavy load protection
// Turn off buzzer
// Returns
//
dec_0x038_off_load_prot_buzzer:
// XP is 0
// X is 0x038
ld x, 0x38 // 0x1D7 (0xB38)
// Subtract 1 from 0x038
add mx, 0xF // 0x1D8 (0xC2F)
ldpx a, a // 0x1D9 (0xEE0)
// Subtract 1 from 0x039 unless previous add overflowed (which is most of the time)
adc mx, 0xF // 0x1DA (0xC6F)
ld a, 0xF // 0x1DB (0xE0F)
ld xp, a // 0x1DC (0xE80)
// If carry is set (add overflowed, which is most of the time), jump
jp c, skip_load_prot_dec_0x038_off_load_prot_buzzer // 0x1DD (0x2E0)
// X is 0xF71
ld x, 0x71 // 0x1DE (0xB71)
// Disable Heavy load protection
and mx, 0xE // 0x1DF (0xCAE)
skip_load_prot_dec_0x038_off_load_prot_buzzer:
// X is 0xF54
ld x, 0x54 // 0x1E0 (0xB54)
// Turn off buzzer output port
or mx, 0x8 // 0x1E1 (0xCE8)
ret // 0x1E2 (0xFDF)
//
// Check SVD voltage
// Sets Zero if voltage is normal
// Sets XP to F
// Returns
//
check_svd_voltage_set_xp_f:
ld a, 0xF // 0x1E3 (0xE0F)
ld xp, a // 0x1E4 (0xE80)
//
// Check SVD voltage
// Sets Zero if voltage is normal
// Returns
//
check_svd_voltage:
// All paths set XP to 0xF
// X is 0xF73
ld x, 0x73 // 0x1E5 (0xB73)
// Turn on SVD circuit at the 2 setting (-3.1 V)
ld mx, 0x6 // 0x1E6 (0xE26)
nop5 // 0x1E7 (0xFFB)
// Read the current voltage status
ld a, mx // 0x1E8 (0xEC2)
// Disable the SVD circuit
ld mx, 0x0 // 0x1E9 (0xE20)
// Check if voltage is low (0x8 would be set)
and a, 0x8 // 0x1EA (0xC88)
ret // 0x1EB (0xFDF)
label_66:
ld x, 0x75 // 0x1EC (0xB75)
ldpx mx, 0x0 // 0x1ED (0xE60)
ldpx mx, a // 0x1EE (0xEE8)
ldpx mx, b // 0x1EF (0xEE9)
ld x, 0x57 // 0x1F0 (0xB57)
ldpx mx, b // 0x1F1 (0xEE9)
calz copy_0x026_7_to_8_9 // 0x1F2 (0x521)
ret // 0x1F3 (0xFDF)
label_67:
add a, 0x1 // 0x1F4 (0xC01)
adc b, 0x0 // 0x1F5 (0xC50)
cp b, 0x1 // 0x1F6 (0xDD1)
jp c, label_69 // 0x1F7 (0x2FD)
jp nz, label_68 // 0x1F8 (0x7FB)