/
flash.asm
843 lines (799 loc) · 15.6 KB
/
flash.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
; TODO: Add routines to erase certificate sectors (or add this to eraseFlashSector?)
rst 0 ; Prevent runaway code from unlocking flash
;; unlockFlash [Flash]
;; Unlocks Flash and unlocks protected ports.
;; Notes:
;; **Do not use this unless you know what you're doing.**
;;
;; Please call [[lockFlash]] when you finish what you're doing and don't spend too
;; much time with Flash unlocked. Disable interrupts while Flash is unlocked.
unlockFlash:
push af
push bc
getBankA
push af
setBankA(privledgedPage)
ld b, 0x01
ld c, 0x14
call 0x4001
pop af
setBankA
pop bc
pop af
ret
;; lockFlash [Flash]
;; Locks Flash and locks protected ports.
lockFlash:
push af
push bc
getBankA
push af
setBankA(privledgedPage)
ld b, 0x00
ld c, 0x14
call 0x4004
pop af
setBankA
pop bc
pop af
ret
;; writeFlashByte [Flash]
;; Writes a single byte to Flash.
;; Inputs:
;; HL: Destination
;; A: Value
;; Notes:
;; Flash must be unlocked. This can only *reset* bits of Flash.
writeFlashByte:
; The procedure is thus:
; 0xAA -> (0xAAA)
; 0x55 -> (0x555)
; 0xA0 -> (0xAAA)
; DATA -> (PDEST)
; Poll to completion
push hl
push bc
push de
push af
ld b, a
ld a, i
push af
di
ld a, b
and (hl) ; Remove any bits that cannot be written
push hl
ld hl, .ram
ld de, flashFunctions
ld bc, .ram_end - .ram
ldir
pop hl
ld b, a
jp flashFunctions
.return:
pop af
jp po, _
ei
_: pop af
pop de
pop bc
pop hl
ret
.ram:
ld a, 0xF0
ld (0), a ; Reset to be safe
ld a, 0xAA
ld (0xAAA), a
ld a, 0x55
ld (0x555), a
ld a, 0xA0
ld (0xAAA), a
ld (hl), b
.poll:
; To poll, read the data back at (hl) to get the chip status. This is
; referred to as Q. The data programmed is B.
; 1. If Q7 == B7, we're done.
; 2. If Q5 == 1, poll again.
; 3. Read (HL) into Q again.
; 4. If Q7 == B7, we're done, otherwise the program failed.
ld c, (hl)
ld a, c
xor b
bit 7, a
jp z, .return ; 1: Check Q7 == B7 and return if so
bit 5, c ; 2: Check Q5 != 0 and poll if so
jr z, .poll
ld a, (hl) ; 3: Read again
xor b
bit 7, a
jp z, .return ; 4: Check Q7 == B7 and return if so
; Operation failed, abort
ld a, 0xF0
ld (0), a
jp .return
.ram_end:
;; writeFlashBuffer [Flash]
;; Writes several bytes of memory to Flash
;; Inputs:
;; DE: Address to write to
;; HL: Address to read from (in RAM)
;; BC: Length of data to write
;; Notes:
;; Flash must be unlocked. Do not attempt to read your source data
;; from Flash, you must load any data to be written into RAM. This
;; will only *reset* bits of Flash.
writeFlashBuffer:
push hl
push bc
push de
push af
ld a, i
push af
di
push hl
push de
push bc
ld hl, .ram
ld de, flashFunctions
ld bc, .ram_end - .ram
ldir
pop bc
pop de
pop hl
jp flashFunctions
.return:
pop af
jp po, _
ei
_: pop af
pop de
pop bc
pop hl
ret
.ram:
.loop:
ex de, hl
ld a, (de)
and (hl)
ex af, af'
ld a, 0xAA
ld (0x0AAA), a ; Unlock
ld a, 0x55
ld (0x0555), a ; Unlock
ld a, 0xA0
ld (0x0AAA), a ; Write command
ex af, af'
ld (hl), a ; Data
ex de, hl
.poll:
; This is completely wrong but doing it right crashes the fucking system
xor (hl)
bit 7, a
jr z, .continue
bit 5, a
jr z, .poll
; Error, abort
ld a, 0xF0
ld (0), a
jp .return
.continue:
ld a, 0xF0
ld (de), a ; Reset
inc de
inc hl
dec bc
xor a
cp c
jr nz, .loop
cp b
jr nz, .loop
jp .return
.ram_end:
;; eraseSwapSector [Flash]
;; Erases the swap sector.
;; Notes:
;; Flash must be unlocked.
eraseSwapSector:
push af
ld a, swapSector
call eraseFlashSector
pop af
ret
;; eraseFlashSector [Flash]
;; Erases one sector of Flash (generally 4 pages of Flash, or 64K)
;; by setting each byte to 0xFF.
;; Inputs:
;; A: Any page within the target sector
;; Notes:
;; Flash must be unlocked.
eraseFlashSector:
push bc
ld b, a
push af
ld a, i
push af
di
ld a, b
and 0b11111100
push hl
push de
push bc
ld hl, .ram
ld de, flashFunctions
ld bc, .ram_end - .ram
ldir
pop bc
pop de
pop hl
jp flashFunctions
.return:
pop af
jp po, _
ei
_: pop af
pop bc
ret
.ram:
setBankA
ld a, 0xAA
ld (0x0AAA), a ; Unlock
ld a, 0x55
ld (0x0555), a ; Unlock
ld a, 0x80
ld (0x0AAA), a ; Write command
ld a, 0xAA
ld (0x0AAA), a ; Unlock
ld a, 0x55
ld (0x0555), a ; Unlock
ld a, 0x30
ld (0x4000), a ; Erase
; Wait for chip
_: ld a, (0)
bit 7, a
jp nz, .return
bit 5, a
jr z, -_
; Error, abort
ld a, 0xF0
ld (0x4000), a
jp .return
.ram_end:
;; eraseFlashPage [Flash]
;; Erases a single page of Flash.
;; Inputs:
;; A: Target page
;; Notes:
;; Flash must be unlocked. This is a very costly operation, and you
;; may want to consider handling this logic yourself if you have to
;; erase more than one page in a single sector
eraseFlashPage:
push af
push bc
push af
call copySectorToSwap
pop af
push af
call eraseFlashSector
pop af
ld c, a
and 0b1111100
ld b, swapSector
; b is page in swap sector, a is page in target sector, c is target page
_:
cp c
jr z, .skipPage
call copyFlashPage
.skipPage:
inc b
inc a
push af
ld a, b
and 0b0000011
or a
jr z, .return
pop af
jr -_
.return:
pop af
pop bc
pop af
ret
;; copySectorToSwap [Flash]
;; Copies a single sector of Flash to the swap sector.
;; Inputs:
;; A: Any page within the sector to be copied
;; Notes:
;; Flash must be unlocked.
copySectorToSwap:
call eraseSwapSector
push bc
ld b, a
push af
ld a, i
push af
push de
push hl
di
ld a, b
and 0b11111100
push hl
push bc
push de
ld hl, .ram
ld de, flashFunctions
ld bc, .ram_end - .ram
ldir
pop de
pop bc
pop hl
#ifdef CPU15
; We'll move flashFunctions into bank 3 so that we can put both
; pages involved into bank 1 and 2 for a while
push af
ld a, 1
out (0x05), a
pop af
jp flashFunctions + 0x4000
#else
jp flashFunctions
#endif
.return:
#ifdef CPU15
; Switch us back to normal, with R:00 in bank 3 and R:01 in bank 2
xor a
out (0x05), a
setBankB(0x81)
#endif
pop hl
pop de
pop af
jp po, _
ei
_: pop af
pop bc
ret
; On 15 MHz calcs, we can do this faster with clever mapping
; This is not possible on the TI-73 and TI-83+
#ifdef CPU15
.ram:
setBankB
setBankA(swapSector)
.page_loop:
ld hl, 0x8000
ld de, 0x4000
ld bc, 0x4000
.inner_loop:
ex de, hl
ld a, (de)
and (hl)
ld (flashFunctions + 0x4000 + 0xFF), a
ex af, af'
ld a, 0xAA
ld (0x0AAA), a
ld a, 0x55
ld (0x0555), a
ld a, 0xA0
ld (0x0AAA), a
ex af, af'
ld (hl), a
.poll:
ld a, (flashFunctions + 0x4000 + 0xFF)
xor (hl)
bit 7, a
jr z, .continue
bit 5, a
jr z, .poll
ld a, (flashFunctions + 0x4000 + 0xFF)
xor (hl)
bit 7, a
jr z, .continue
; Error, skip this byte
.continue:
ex de, hl
ld a, 0xF0
ld (de), a
inc de
inc hl
dec bc
xor a
cp c
jr nz, .inner_loop
cp b
jr nz, .inner_loop
getBankA
inc a
cp swapSector + 4
jp z, .return
setBankA
getBankB
inc a
setBankB
jr .page_loop
.ram_end:
#else ; TI-73, TI-83+
.ram:
ld d, a
setBankA
ld e, swapSector
.page_loop:
ld hl, 0x4000
ld bc, 0x4000
.inner_loop:
ld a, (hl) ; source sector
ld (flashFunctions + 0xFF), a
ex af, af'
ld a, e ; swap sector
setBankA
ld a, 0xF0
ld (0), a
ld a, 0xAA
ld (0x0AAA), a
ld a, 0x55
ld (0x0555), a
ld a, 0xA0
ld (0x0AAA), a
ex af, af'
ld (hl), a
.poll:
ld a, (flashFunctions + 0xFF)
xor (hl)
bit 7, a
jr z, .continue
bit 5, a
jr z, .poll
ld a, (flashFunctions + 0xFF)
bit 7, a
jr z, .continue
; Error, skip this byte
.continue:
ld a, 0xF0
ld (hl), a
inc hl
dec bc
ld a, d
setBankA
xor a
cp c
jr nz, .inner_loop
cp b
jr nz, .inner_loop
inc d \ inc e
ld a, d
setBankA
and 0b00000011
jr nz, .page_loop
jp .return
.ram_end:
#endif
;; copyFlashExcept [Flash]
;; Copies one Flash page to another, but omits a certain range of bytes in increments of
;; 0x100 bytes.
;; Inputs:
;; A: Destination page
;; B: Source page
;; H: High byte of address to stop copying at
;; L: High byte of address to resume copying at
;; Notes:
;; Flash must be unlocked and the destination page must be cleared.
;;
;; If you want to copy all but 0x6000 to 0x6500, set HL to 0x6065.
copyFlashExcept:
; Note: If we ever get short on space on page 0, we can merge this with copyFlashPage
push de
push bc
ld d, a
push af
ld a, i
push af
di
ld a, d
push hl
push de
push af
push bc
push hl
ld hl, .ram
#ifdef CPU15
ld a, 1
out (PORT_RAM_PAGING), a
; This routine can perform better on some models if we rearrange memory
ld de, flashFunctions + 0x4000
ld bc, .ram_end - .ram
ldir
#else
ld de, flashFunctions
ld bc, .ram_end - .ram
ldir
#endif
pop hl
pop bc
pop af
#ifdef CPU15
jp flashFunctions + 0x4000
.return:
xor a
out (PORT_RAM_PAGING), a ; Restore correct memory mapping
#else
jp flashFunctions
.return:
#endif
pop de
pop hl
pop af
jp po, _
ei
_: pop af
pop bc
pop de
ret
#ifdef CPU15
.ram:
setBankA ; Destination
ld a, b
setBankB ; Source
ld a, h
ld (.skip_check_smc - .ram + flashFunctions + 0x4000 + 1), a
ld a, l
ld (.skip_apply_smc - .ram + flashFunctions + 0x4000 + 1), a
ld (.skip_apply_smc_2 - .ram + flashFunctions + 0x4000 + 1), a
.preLoop:
ld de, 0x8000
ld hl, 0x4000
ld bc, 0x4000
.loop:
.skip_check_smc:
ld a, 0
cp h
jr z, .skip
.continue_loop:
ld a, (de)
ld (.smc - .ram + flashFunctions + 0x4000 + 1), a
ld (_ + - .ram + flashFunctions + 0x4000 + 1), a
ld a, 0xAA
ld (0x0AAA), a ; Unlock
ld a, 0x55
ld (0x0555), a ; Unlock
ld a, 0xA0
ld (0x0AAA), a ; Write command
.smc:
ld a, 0
ld (hl), a
_: ld a, 0 ; also smc, don't optimize
xor (hl)
bit 7, a
jr z, _
bit 5, a
jr z, -_
jr _ ; See note on copySectorToSwap
; Error, abort
ld a, 0xF0
ld (0), a
setBankB(0x81)
jp .return
_:
inc de
inc hl
dec bc
ld a, b
or c
jr nz, .loop
setBankB(0x81)
jp .return
.skip:
.skip_apply_smc:
ld a, 0
sub h
neg
add a, b
ld b, a
.skip_apply_smc_2:
ld h, 0
ld d, h
res 6, d
set 7, d ; Bump up to 0x8000 range
jr .continue_loop
.ram_end:
#else ; Models that don't support placing RAM page 01 in bank 3 (slower)
.ram:
; Called with destination in A, target in B
ld (.smc2 - .ram + flashFunctions + 1), a
ld a, b
ld (.smc - .ram + flashFunctions + 1), a
ld a, h
ld (.skip_check_smc - .ram + flashFunctions + 1), a
ld a, l
ld (.skip_apply_smc - .ram + flashFunctions + 1), a
ld (.skip_apply_smc_2 - .ram + flashFunctions + 1), a
.preLoop:
ld hl, 0x4000
ld bc, 0x4000
.loop:
.skip_check_smc:
ld a, 0
cp h
jr z, .skip
.continue_loop:
.smc:
ld a, 0
setBankA
ld d, (hl)
.smc2:
ld a, 0
setBankA
; copy D to (HL)
ld a, 0xAA
ld (0x0AAA), a ; Unlock
ld a, 0x55
ld (0x0555), a ; Unlock
ld a, 0xA0
ld (0x0AAA), a ; Write command
ld (hl), d ; Data
ld a, d
_: xor (hl)
bit 7, a
jr z, _
bit 5, a
jr z, -_
jr _ ; See note on copySectorToSwap
; Error, abort
ld a, 0xF0
ld (0), a
jp .return
_: inc hl
dec bc
ld a, b
or c
jr nz, .loop
jp .return
.skip:
.skip_apply_smc:
ld a, 0
sub h
neg
add a, b
ld b, a
.skip_apply_smc_2:
ld h, 0
jr .continue_loop
.ram_end:
#endif
;; copyFlashPage [Flash]
;; Copies one page of Flash to another.
;; Inputs:
;; A: Destination page
;; B: Source page
;; Notes:
;; Flash must be unlocked and the desination page must be cleared.
copyFlashPage:
push de
push bc
ld d, a
push af
ld a, i
push af
di
ld a, d
push hl
push de
push af
push bc
ld hl, .ram
#ifdef CPU15
ld a, 1
out (PORT_RAM_PAGING), a
; This routine can perform better on some models if we rearrange memory
ld de, flashFunctions + 0x4000
ld bc, .ram_end - .ram
ldir
#else
ld de, flashFunctions
ld bc, .ram_end - .ram
ldir
#endif
pop bc
pop af
#ifdef CPU15
jp flashFunctions + 0x4000
.return:
xor a
out (PORT_RAM_PAGING), a ; Restore correct memory mapping
#else
jp flashFunctions
.return:
#endif
pop de
pop hl
pop af
jp po, _
ei
_: pop af
pop bc
pop de
ret
#ifdef CPU15
.ram:
setBankA ; Destination
ld a, b
setBankB ; Source
.preLoop:
ld de, 0x8000
ld hl, 0x4000
ld bc, 0x4000
.loop:
ld a, (de)
ld (.smc - .ram + flashFunctions + 0x4000 + 1), a
ld (_ + - .ram + flashFunctions + 0x4000 + 1), a
ld a, 0xAA
ld (0x0AAA), a ; Unlock
ld a, 0x55
ld (0x0555), a ; Unlock
ld a, 0xA0
ld (0x0AAA), a ; Write command
.smc:
ld a, 0
ld (hl), a
_: ld a, 0 ; also smc, don't optimize
xor (hl)
bit 7, a
jr z, _
bit 5, a
jr z, -_
jr _ ; See note on copySectorToSwap
; Error, abort
ld a, 0xF0
ld (0), a
setBankB(0x81)
jp .return
_:
inc de
inc hl
dec bc
ld a, b
or c
jr nz, .loop
setBankB(0x81)
jp .return
.ram_end:
#else ; Models that don't support placing RAM page 01 in bank 3 (slower)
.ram:
; Called with destination in A, target in B
ld (.smc2 - .ram + flashFunctions + 1), a
ld a, b
ld (.smc - .ram + flashFunctions + 1), a
.preLoop:
ld hl, 0x4000
ld bc, 0x4000
.loop:
.smc:
ld a, 0
setBankA
ld d, (hl)
.smc2:
ld a, 0
setBankA
; copy D to (HL)
ld a, 0xAA
ld (0x0AAA), a ; Unlock
ld a, 0x55
ld (0x0555), a ; Unlock
ld a, 0xA0
ld (0x0AAA), a ; Write command
ld (hl), d ; Data
ld a, d
_: xor (hl)
bit 7, a
jr z, _
bit 5, a
jr z, -_
jr _ ; See note on copySectorToSwap
; Error, abort
ld a, 0xF0
ld (0), a
jp .return
_: inc hl
dec bc
ld a, b
or c
jr nz, .loop
jp .return
.ram_end:
#endif