forked from BrickBot/brickEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rom.S
3377 lines (2999 loc) · 62.1 KB
/
rom.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
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
/*
* rom.S
* Replacement for MindStorm RCX rom.
* mostly address compatible
*/
.globl _start
_start:
. = 0
irq_vectors:
.word reset_trampoline /* 0 */
.word 0
.word 0
.word nmi_trampoline
.word irq0_trampoline
.word irq1_trampoline /* 5 */
.word irq2_trampoline
.word 0
.word 0
.word 0
.word 0 /* 10 */
.word 0
.word icia_trampoline
.word icib_trampoline
.word icic_trampoline
.word icid_trampoline /* 15 */
.word ocia_trampoline
.word ocib_trampoline
.word fovi_trampoline
.word cmi0a_trampoline
.word cmi0b_trampoline /* 20 */
.word ovi0_trampoline
.word cmi1a_trampoline
.word cmi1b_trampoline
.word ovi1_trampoline
.word 0 /* 25 */
.word 0
.word eri_trampoline
.word rxi_trampoline
.word txi_trampoline
.word tei_trampoline /* 30 */
.word 0
.word 0
.word 0
.word 0
.word adi_trampoline /* 35 */
.word wovf_trampoline
. = 0x4a
softirq_vectors:
.word umulhi3
.word umodhi3
.word udivmodhi3
.word modhi3
.word divhi3
.word mulsi3
.word udivmodsi3
.word divmodsi3
. = 0x130
umulhi3:
/* calculate r6 := r6 * r5, clobber r5 */
push r0
mov.w r6, r0
mulxu.b r5h, r0 /* r0 = r5h * r6l */
mov.b r6h, r0h
mulxu.b r5l, r6 /* r6 = r5l * r6l */
add.b r0l, r6h /* r6 += r0 << 8 */
mulxu.b r0h, r5 /* r5 = r5l * r6h */
add.b r5l, r6h /* r6 += r5 << 8 */
pop r0
rts
. = 0x14a
umodhi3:
/* calculate r6 := r6 % r5 */
bsr udivmodhi3
mov.w r5, r6
rts
. = 0x156
divhi3:
/* calculate r6 := r6 / r5 (signed) */
mov.w r6, r6
bmi 1f
mov.w r5, r5 /* r6 positive */
bpl udivmodhi3
not.b r5h
not.b r5l
adds #1, r5
2:
bsr udivmodhi3
not.b r6h
not.b r6l
adds #1, r6
rts
1: /* r6 negative */
not.b r6h
not.b r6l
adds #1, r6
mov.w r5, r5
bpl 2b
not.b r5h
not.b r5l
adds #1, r5
bra udivmodhi3
. = 0x188
modhi3:
/* calculate r6 := r6 % r5 (signed) */
mov.w r5, r5
bpl 1f
not.b r5h
not.b r5l
adds #1, r5
1:
mov.w r6, r6 /* r5 is now positive */
bpl umodhi3
not.b r6h /* r6 negative */
not.b r6l
adds #1, r6
bsr udivmodhi3
not.b r5h
not.b r5l
adds #1, r5
mov.w r5, r6
rts
. = 0x1be
udivmodhi3:
/* r6, r5 := r6 / r5 , r6 % r5 (unsigned) */
push r0
mov.b #0, r0h
mov.b r6h, r0l /* r0h:r0l:r6l = dividend */
mov.b r5h, r5h /* Test for r5h == 0 */
bne 1f
divxu.b r5l, r0 /* r0l,r0h = r6 >> 8 (/,%) r5l */
mov.b r0l, r6h /* r6h = high part of dividend */
mov.b r6l, r0l
divxu.b r5l, r0
mov.b r0l, r6l /* r6l = low part of dividend */
mov.b r0h, r5l /* r5l = modulo */
pop r0
rts
1:
mov.b r6h, r0l
/* result is also built into r6l */
mov.b #8, r6h
2: rotxl r6l /* store bit of result and rotate dividend */
rotxl r0l
rotxl r0h
cmp.w r0, r5 /* test if we can subtract.
bcs 1f
sub.w r0, r5
dec r6l
bne 2b
rotxl r6l /* put last bit into r6 */
not.b r6l
mov.b #0, r6h /* clear high byte of result */
mov r0, r5 /* store modulo into r5 */
pop r0
rts
. = 0x1fe
mulsi3:
/* r5h r5l r6h r6l * r3h r3l r4h r4l */
/* r3h*r6l r3l*r6l r4h*r6l r4l*r6l */
/* r3l*r6h r4h*r6h r4l*r6h */
/* r4h*r5l r4l*r5l */
/* r4l*r5h */
/* r5r6 = r5r6 * r3r4 */
push r0
push r1
mov.w r6, r0
mulxu.b r3h, r0 /* r0 = r6l*r3h */
mov.w r3, r1
mulxu.b r6h, r1 /* r1 = r6h*r3l */
add.b r1l, r0l
mulxu.b r6l, r3 /* r3 = r6l*r3l */
add.b r1l, r3h
add.b r0l, r3h /* r3 = r6*r3 */
mov.w r4, r0
mulxu.b r5h, r0 /* r0 = r4l*r5h */
mov.w r5, r1
mulxu.b r4h, r1 /* r1 = r4h*r5l */
add.b r1l, r0l
mulxu.b r4l, r5 /* r5 = r4l*r5l */
add.b r1l, r5h
add.b r0l, r5h /* r5 = r4*r5 */
add.w r3, r5 /* r5 = r4*r5 + r6*r3 */
mov.b r4h, r1l
mulxu.b r6h, r1 /* r1 = r4h*r6h */
add.w r1, r5
mov.w r6, r0
mulxu.b r4h, r0 /* r0 = r4h*r6l */
mov.w r4, r1
mulxu.b r6h, r1 /* r1 = r4l*r6h */
mulxu.b r4l, r6 /* r6 = r4l*r6l */
add.b r1l, r6h
addx r1h, r5l
addx #0, r5h
add.b r0l, r6h
addx r0h, r5l
addx #0, r5h
pop r1
pop r0
rts
. = 0x26e
divmodsi3:
/* divide r5:r6 / r3:r4 (signed)
* quotient in r5:r6
* remainder in r3:r4
*/
mov.w r5, r5 /* check sign of dividend */
bpl 2f
not.b r6l
not.b r6h
not.b r5l
not.b r5h
add.b #1, r6l
addx #0, r6h
addx #0, r5l
addx #0, r5h
mov.w r3, r3 /* check sign of divisor */
bpl 1f
/* -x / -y */
not.b r4l
not.b r4h
not.b r3l
not.b r3h
add.b #1, r4l
addx #0, r4h
addx #0, r3l
addx #0, r3h
bsr udivmodsi3
bra 3f
1: bsr udivmodsi3
not.b r6l /* negate result */
not.b r6h
not.b r5l
not.b r5h
add.b #1, r6l
addx #0, r6h
addx #0, r5l
addx #0, r5h
3:
not.b r4l /* negate modulo */
not.b r4h
not.b r3l
not.b r3h
add.b #1, r4l
addx #0, r4h
addx #0, r3l
addx #0, r3h
rts
2: /* dividend positive */
mov.w r3, r3 /* check sign of divisor */
bpl udivmodsi3 /* divisor positive, too */
not.b r4l
not.b r4h
not.b r3l
not.b r3h
add.b #1, r4l
addx #0, r4h
addx #0, r3l
addx #0, r3h
bsr udivmodsi3
not.b r6l /* negate result */
not.b r6h
not.b r5l
not.b r5h
add.b #1, r6l
addx #0, r6h
addx #0, r5l
addx #0, r5h
rts
. = 0x306
udivmodsi3:
/* divide r5:r6 / r3:r4
* quotient in r5:r6
* remainder in r3:r4
*/
mov.w r3, r3
bne 6f
/* Simple case: r3 is zero */
cmp.b #0, r4h
bne 5f
/* Most simple case: only r4l; we can clobber r3 */
mov.b r5h, r3l
bne 1f
/* r5h is zero, skip it */
mov.b r5l, r3l
bne 2f
/* r5 is zero skip it */
mov.b r6h, r3l
bne 3f
bra 4f
1: divxu.b r4l, r3
mov.b r3l, r5h
mov.b r5l, r3l
2: divxu.b r4l, r3
mov.b r3l, r5l
mov.b r6h, r3l
3: divxu.b r4l, r3
mov.b r3h, r6h
4: divxu.b r4l, r6
mov.b r6h, r4l
mov.b r3l, r6h
sub.w r3, r3
rts
5: /* r3 = 0, r4h != 0 */
mov.b r5h, r3l /* dividend is in r3:r5l:r6 */
mov.b #24, r5h
4: rotxl r6l /* store bit of result and rotate dividend*/
rotxl r6h
rotxl r5l
rotxl r3l
rotxl r3h
cmp.w r4, r3 /* test if we can subtract. */
bcs 1f
sub.w r4, r3
1: dec r5h
bne 4b
rotxl r6l /* put last bit into r5:r6 */
rotxl r6h
rotxl r5l
not.b r6l
not.b r6h
not.b r5l
mov.b #0, r5h /* clear high byte of result */
mov r3, r4 /* store modulo into r4 */
sub.w r3, r3
rts
6: /* r3 != 0, r4 != 0 */
push r0
push r1
sub.w r0, r0 /* dividend is in r0:r5:r6 */
mov.b #16, r1l
4: rotxl r6l
rotxl r6h
rotxl r5l
rotxl r5h
rotxl r0l
rotxl r0h
sub.w r4, r5
subx r3l, r0l
subx r3h, r0h
bcc 1f
add.w r4, r5
addx r3l, r0l
addx r3h, r0h
1: dec r1l
bne 4b
rotxl r6l
rotxl r6h
not.b r6h
not.b r6l
mov.w r5, r4
mov.w r0, r3
sub.w r5, r5
pop r1
pop r0
rts
. = 0x3ae
reset_trampoline:
mov.w #0xff7e, r7
jsr @init_ports
bsr init_vectors
jsr @main
jmp halt
. = 0x3ca
init_vectors:
mov.w #rom_main_loop_state, r0
mov.w #0xf000, r1
bsr memclr
mov.w #dummy, r0
mov.w #nmi_vector, r1
mov.w #wovf_vector+2, r2
1:
mov.w r0, @-r2
cmp.w r1, r2
bne 1b
rts
. = 0x42a - 6
1: mov.b @r0+,r3l
mov.b r3l, @r2
adds #1, r2
. = 0x42a
/* used by brickOS library */
memcopy:
/* r0 = start, r1 = end, r2 = dest */
cmp.w r0, r1
bne 1b
rts
. = 0x436
memclr:
/* r0 = start, r1 = end */
mov.b #0, r2l
2:
cmp.w r0, r1
beq 1f
mov.b r2l, @-r1
bra 2b
1: rts
. = 0x442
init_ports:
bset #2, @_PORT5_DDR:8
bclr #2, @_PORT5:8
mov.b #0xff, r6l
mov.b r6l, @_PORT1_DDR:8
mov.b r6l, @_PORT2_DDR:8
mov.b r6l, @PORT1_DDR_SHADOW:16
mov.b r6l, @PORT2_DDR_SHADOW:16
mov.b #2, r6l
mov.b r6l, @0x8001
mov.b #1, r6l
mov.b r6l, @0x8000
rts
. = 0x468
halt:
bra halt
. = 0x46a
dummy: rts
. = 0x46c
nmi_trampoline:
push r6
mov.w @nmi_vector:16, r6
jsr @r6
pop r6
rte
irq0_trampoline:
push r6
mov.w @irq0_vector:16, r6
jsr @r6
pop r6
rte
irq1_trampoline:
push r6
mov.w @irq1_vector:16, r6
jsr @r6
pop r6
rte
irq2_trampoline:
push r6
mov.w @irq2_vector:16, r6
jsr @r6
pop r6
rte
icia_trampoline:
push r6
mov.w @icia_vector:16, r6
jsr @r6
pop r6
rte
icib_trampoline:
push r6
mov.w @icib_vector:16, r6
jsr @r6
pop r6
rte
icic_trampoline:
push r6
mov.w @icic_vector:16, r6
jsr @r6
pop r6
rte
icid_trampoline:
push r6
mov.w @icid_vector:16, r6
jsr @r6
pop r6
rte
ocia_trampoline:
push r6
mov.w @ocia_vector:16, r6
jsr @r6
pop r6
rte
ocib_trampoline:
push r6
mov.w @ocib_vector:16, r6
jsr @r6
pop r6
rte
fovi_trampoline:
push r6
mov.w @fovi_vector:16, r6
jsr @r6
pop r6
rte
cmi0a_trampoline:
push r6
mov.w @cmi0a_vector:16, r6
jsr @r6
pop r6
rte
cmi0b_trampoline:
push r6
mov.w @cmi0b_vector:16, r6
jsr @r6
pop r6
rte
ovi0_trampoline:
push r6
mov.w @ovi0_vector:16, r6
jsr @r6
pop r6
rte
cmi1a_trampoline:
push r6
mov.w @cmi1a_vector:16, r6
jsr @r6
pop r6
rte
cmi1b_trampoline:
push r6
mov.w @cmi1b_vector:16, r6
jsr @r6
pop r6
rte
ovi1_trampoline:
push r6
mov.w @ovi1_vector:16, r6
jsr @r6
pop r6
rte
eri_trampoline:
push r6
mov.w @eri_vector:16, r6
jsr @r6
pop r6
rte
rxi_trampoline:
push r6
mov.w @rxi_vector:16, r6
jsr @r6
pop r6
rte
txi_trampoline:
push r6
mov.w @txi_vector:16, r6
jsr @r6
pop r6
rte
tei_trampoline:
push r6
mov.w @tei_vector:16, r6
jsr @r6
pop r6
rte
adi_trampoline:
push r6
mov.w @adi_vector:16, r6
jsr @r6
pop r6
rte
wovf_trampoline:
push r6
mov.w @wovf_vector:16, r6
jsr @r6
pop r6
rte
. = 0x580
.globl _main
_main:
main:
mov.b @_T0_CNT:8, r6l
beq 1f
mov.b #0, r6l
mov.b r6l, @rom_main_loop_state:16
bra 2f
1: mov.b #8, r6l
mov.b r6l, @rom_main_loop_state:16
mov.b #1, r6l
mov.b r6l, @rom_ready_to_sleep:16
mov.w #15, r6
mov.w r6, @power_off_timer:16
mov.w #rom_handler_struct, r6
push r6
add.b #0x10, r6l
jsr @init_timer
adds #0x2, r7
mov.w #rom_main_loop_state, r6
jsr @init_handlers
mov.w #rom_main_loop_state, r6
jsr @init_builtin_programs
2:
main_loop:
mov.b @rom_main_loop_state:16, r6l
cmp.b #0x13, r6l
bne 1f
mov.w @0xee82, r6
bne main_loop
jsr @shutdown_handlers
jsr @shutdown_timer
mov.w @firmware_entry, r6
jsr @r6
bra main_loop
1: cmp.b #8, r6l
bne 1f
mov.w #rom_main_loop_state, r6
jsr @update
mov.b @sound_playing, r6l /* get sound playing flag */
cmp.b #0, r6l
bne main_loop
mov.b @rom_ready_to_sleep, r6l
beq main_loop
jsr @shutdown_handlers
jsr @shutdown_timer
jsr @rom_power_off
mov.b #0,r6l
mov.b r6l, @rom_main_loop_state:16
1: cmp.b #0, r6l
bne 1f
mov.w #0xf, r6
mov.w r6, @power_off_timer
mov.w #rom_handler_struct, r6
push r6
add.b #0x10, r6l
jsr @init_timer
adds #2, r6
mov.w #0xee5e, r6
bsr init_handlers
mov.w #0xee5e, r6
jsr @init_builtin_programs
mov.b #0xd, r6l
mov.b r6l, @rom_main_loop_state
1: mov.w #rom_main_loop_state, r6
jsr @update
mov.b @0xee64:16, r6l
btst #7, r6l /* New sensor values? */
beq 1f
mov.w #rom_main_loop_state, r6
jsr @run_builtin_programs
1: jmp main_loop
. = 0x688
/* needed by fast download rom */
init_handlers:
jsr @init_sensors
jsr @init_motors
jsr @init_buttons
jsr @init_power
mov.b #1, r6l
push r6
push r6
mov.w #rom_timer_struct, r6
push r6
mov.w #rom_handler_struct, r6
jsr @init_serial
adds #2,r7
adds #2,r7
adds #2,r7
jsr @set_port_6_bit_3_output
mov #0x3c, r6l
mov.b r6l, @0xee82
jsr @lcd_clear
jsr @lcd_refresh
mov.w #50, r6
mov.b r6h, @rom_update_state
mov.b r6l, @rom_key_state
rts
. = 0x70c
update:
push r5
mov.b @rom_update_state, r6l
bne 1f
/* rom_update_state == 0: START */
/* original ROM checks memory here but that code is broken anyway. */
mov.w #man_stand, r6 /* show standing man */
jsr @lcd_show
mov.b #7, r6l /* state = 7 (beep twice) */
mov.b r6l, @rom_update_state
bra end_switch_1
1: dec r6l
bne 1f
push r3
push r4
sub.w r3, r3
mov.w r3, r4
/* rom_update_state == 1: CHECK FIRMWARE */
/* We don't check for "Do you byte when I knock" magic */
mov.w #0x8000, r5 /* start address */
mov.w #0xcc00, r6 /* end address */
2: mov.b @r5+, r4l
add.w r4, r3
cmp.w r5, r6
bne 2b
mov.w @high_firmware_checksum, r4
cmp.w r4, r3
bne 3f
mov.w @high_firmware_checksum_inv, r4
adds #1, r4
add.w r3, r4
bne 3f
mov.b #3, r6l /* send unlock ok */
bra 4f
3: mov.w #4, r6 /* low buzz */
push r6
mov.w #0x4004, r6
jsr play_system_sound
adds #2, r7
mov.b #0xff, r6l
mov.b r6l, @last_received_opcode
mov.b #2, r6l /* wait for message */
4: pop r4
pop r3
mov.b r6l, @rom_update_state
bra end_switch_1
1: dec r6l
bne 1f
/* rom_update_state == 2: WAIT FOR MESSAGE */
mov.b @serial_valid_incoming_data, r5l
beq end_switch_1
mov.w #15, r6
mov.w r6, @power_off_timer:16
mov.b #4, r6l
mov.b r6l, @rom_update_state
end_switch_1:
jmp end_switch
1: dec r6l
bne 1f
/* rom_update_state == 3: SEND UNLOCK OK */
mov.w #0x19, r6
push r6
mov.w #Just_a_bit, r6
push r6
mov.b @rom_receive_buffer+0, r6l
push r6
mov.w #0x1776, r6 /* long packet */
jsr @send_packet
adds #2, r7
adds #2, r7
adds #2, r7
cmp.b #0x4c, r6l
beq end_switch_1
mov.w #30, r6
mov.w r6, @firmware_holdoff_timer
mov.w @high_firmware_entry, r6
mov.w r6, @firmware_entry
mov.b #0x13, r6l
mov.b r6l, @rom_main_loop_state
mov.b #8, r6l
mov.b r6l, @rom_update_state
jmp end_switch
1: dec r6l
beq 2f
jmp 1f
2:
/* rom_update_state == 4: PROCESS MESSAGE */
subs #2, r7
push r7
mov.w #10, r6
push r6
mov.w #rom_receive_buffer, r6
jsr @receive_data
adds #2, r7
adds #2, r7
adds #2, r7
mov.b @rom_receive_buffer, r6l
mov.b @last_received_opcode, r6h
cmp.b r6l, r6h
beq send_reply
mov.b r6l, @last_received_opcode
and.b #0xf7, r6l
cmp.b #0x10, r6l
bne 2f
/* opcode = 0x10 (alive) */
send_simple_reply:
mov.b #1, r6l
send_inverse_opcode:
mov.b r6l, @rom_send_packet_length
mov.b @rom_receive_buffer, r6l
not.b r6l
mov.b r6l, @rom_send_buffer
send_reply:
mov.b #5, r6l
mov.b r6l, @rom_update_state
jmp end_switch
2: cmp.b #0x15, r6l
bne 2f
/* opcode = 0x15 (get_version) */
mov.w #3, r6
mov.b r6h, @rom_send_buffer+1
mov.b r6l, @rom_send_buffer+2
mov.b #1, r6l
mov.b r6h, @rom_send_buffer+3
mov.b r6l, @rom_send_buffer+4
mov.b r6h, @rom_send_buffer+5
mov.b r6h, @rom_send_buffer+6
mov.b r6h, @rom_send_buffer+7
mov.b r6h, @rom_send_buffer+8
mov.b #9, r6l
bra send_inverse_opcode
2: cmp.b #0x65, r6l
beq send_simple_reply
/* opcode = 0x65 (delete firmware (nothing to do)) */
2: cmp.b #0x75, r6l
bne 2f
/* opcode = 0x75 (start firmware download) */
mov.b @rom_receive_buffer+1, r6l
mov.b @rom_receive_buffer+2, r6h
mov.w r6, @high_firmware_entry /* firmware enty */
mov.b @rom_receive_buffer+3, r6l
mov.b @rom_receive_buffer+4, r6h
mov.w r6, @high_firmware_checksum /* firmware checksum */
not.b r6l
not.b r6h
mov.w r6, @high_firmware_checksum_inv /* inverted firmware checksum */
mov.w #0xffff, r6
mov.w r6, @serial_transfer_sequence_number
mov.w #0x8000, r6
mov.w r6, @serial_incoming_dataptr
push r0
push r1
mov.w r6, r0
mov.w #0xcc00, r1
jsr @memclr
pop r1
pop r0
mov.w #1, r6
mov.w r6, @rom_transfer_data_index
mov.b #0, r6l
send_two_byte_reply:
mov.b r6l, @rom_send_buffer+1
mov.b #2, r6l
jmp send_inverse_opcode
2: cmp.b #0x45, r6l
bne 2f
/* opcode = 0x45 (transfer firmware) */
mov.b @rom_receive_buffer+1, r6l
bne 3f
mov.b @rom_receive_buffer+2, r6h
beq transfer_done
3:
mov.b @rom_receive_buffer+2, r6h
mov.w @rom_transfer_data_index, r5
cmp.w r6, r5
bne wait_for_message_1
adds #1, r6
mov.w r6, @rom_transfer_data_index
mov.b @rom_receive_buffer+5, r6l
mov.b @rom_receive_buffer+6, r6h
sub.b r6h, r6l
beq send_two_byte_reply /* send 0: okay */
mov.b #3, r6l
bra send_two_byte_reply /* send 3: block chksum fail */
transfer_done:
sub.w r6, r6
mov.w r6, @serial_incoming_dataptr
mov.b @rom_receive_buffer+5, r6l
mov.b @rom_receive_buffer+6, r6h
sub.b r6h, r6l
bne 3f
/* Do we want to checksum here? */
mov.w #5, r6 /* fast tones up */
push r6
mov.w #0x4004, r6
jsr @play_system_sound
adds #2, r7
mov.b #0, r6l /* send 0: okay */
bra send_two_byte_reply
3: mov.b #3, r6l
bra send_two_byte_reply /* send 3: block chksum fail */
2: cmp.b #0xa5, r6l
bne wait_for_message
/* opcode = 0xa5 (unlock firmware) */
/* Check for LEGO(R) signature? */
mov.b #1, r6l
mov.b r6l, @rom_update_state
bra end_switch
1: dec r6l
bne 1f
/* rom_update_state == 5: send reply */
mov.b @rom_send_packet_length, r6l
mov.b #0, r6h
push r6
mov.w #rom_send_buffer, r6
push r6
sub.w r6,r6
push r6
mov.w #0x1775, r6 /* short packet */
jsr @send_packet
adds #2, r7
adds #2, r7
adds #2, r7
cmp.b #0x4c, r6l
wait_for_message_1:
bne wait_for_message
bra end_switch
1: dec r6l
bne 1f
/* rom_update_state == 6: send reply and check firmware */
mov.b @rom_send_packet_length, r6l
mov.b #0, r6h
push r6
mov.w #rom_send_buffer, r6
push r6
sub.w r6,r6
push r6
mov.w #0x1775, r6 /* short packet */
jsr @send_packet
adds #2, r7
adds #2, r7
adds #2, r7
cmp.b #0x4c, r6l
beq end_switch
mov.b #1, r6l
mov.b r6l, @rom_update_state
bra end_switch
1: dec r6l
bne end_switch
/* rom_update_state == 7: beep twice */
mov.w #1, r6 /* beep twice */
push r6
mov.w #0x4004, r6
jsr @play_system_sound
adds #2, r7
wait_for_message:
mov.b #2, r6l
mov.b r6l, @rom_update_state