OpenCores
URL https://opencores.org/ocsvn/rtf65002/rtf65002/trunk

Subversion Repositories rtf65002

[/] [rtf65002/] [trunk/] [software/] [asm/] [bootrom.asm] - Blame information for rev 18

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 11 robfinch
; ============================================================================
2
;        __
3
;   \\__/ o\    (C) 2013  Robert Finch, Stratford
4
;    \  __ /    All rights reserved.
5
;     \/_//     robfinch@opencores.org
6
;       ||
7
;
8
;
9
; This source file is free software: you can redistribute it and/or modify
10
; it under the terms of the GNU Lesser General Public License as published
11
; by the Free Software Foundation, either version 3 of the License, or
12
; (at your option) any later version.
13
;
14
; This source file is distributed in the hope that it will be useful,
15
; but WITHOUT ANY WARRANTY; without even the implied warranty of
16
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
; GNU General Public License for more details.
18
;
19
; You should have received a copy of the GNU General Public License
20
; along with this program.  If not, see .
21
;
22
; ============================================================================
23
;
24
CR      EQU     0x0D            ;ASCII equates
25
LF      EQU     0x0A
26
TAB     EQU     0x09
27
CTRLC   EQU     0x03
28
CTRLH   EQU     0x08
29
CTRLI   EQU     0x09
30
CTRLJ   EQU     0x0A
31
CTRLK   EQU     0x0B
32
CTRLM   EQU 0x0D
33
CTRLS   EQU     0x13
34
CTRLX   EQU     0x18
35
XON             EQU     0x11
36
XOFF    EQU     0x13
37
 
38
TEXTSCR         EQU             0xFFD00000
39
COLORSCR        EQU             0xFFD10000
40
TEXTREG         EQU             0xFFDA0000
41
TEXT_COLS       EQU             0x0
42
TEXT_ROWS       EQU             0x1
43
TEXT_CURPOS     EQU             11
44
KEYBD           EQU             0xFFDC0000
45
KEYBDCLR        EQU             0xFFDC0001
46 15 robfinch
PIC                     EQU             0xFFDC0FF0
47
PIC_IE          EQU             0xFFDC0FF1
48 11 robfinch
 
49
SPIMASTER       EQU             0xFFDC0500
50
SPI_MASTER_VERSION_REG  EQU     0x00
51
SPI_MASTER_CONTROL_REG  EQU     0x01
52
SPI_TRANS_TYPE_REG      EQU             0x02
53
SPI_TRANS_CTRL_REG      EQU             0x03
54
SPI_TRANS_STATUS_REG    EQU     0x04
55
SPI_TRANS_ERROR_REG             EQU     0x05
56
SPI_DIRECT_ACCESS_DATA_REG              EQU     0x06
57
SPI_SD_SECT_7_0_REG             EQU     0x07
58
SPI_SD_SECT_15_8_REG    EQU     0x08
59
SPI_SD_SECT_23_16_REG   EQU     0x09
60
SPI_SD_SECT_31_24_REG   EQU     0x0a
61
SPI_RX_FIFO_DATA_REG    EQU     0x10
62
SPI_RX_FIFO_DATA_COUNT_MSB      EQU     0x12
63
SPI_RX_FIFO_DATA_COUNT_LSB  EQU 0x13
64
SPI_RX_FIFO_CTRL_REG            EQU     0x14
65
SPI_TX_FIFO_DATA_REG    EQU     0x20
66
SPI_TX_FIFO_CTRL_REG    EQU     0x24
67
SPI_RESP_BYTE1                  EQU     0x30
68
SPI_RESP_BYTE2                  EQU     0x31
69
SPI_RESP_BYTE3                  EQU     0x32
70
SPI_RESP_BYTE4                  EQU     0x33
71
SPI_INIT_SD                     EQU             0x01
72
SPI_TRANS_START         EQU             0x01
73
SPI_TRANS_BUSY          EQU             0x01
74
SPI_INIT_NO_ERROR       EQU             0x00
75
SPI_READ_NO_ERROR       EQU             0x00
76 15 robfinch
SPI_WRITE_NO_ERROR      EQU             0x00
77 11 robfinch
RW_READ_SD_BLOCK        EQU             0x02
78
RW_WRITE_SD_BLOCK       EQU             0x03
79
 
80 15 robfinch
BITMAPSCR       EQU             0x04000000
81
SECTOR_BUF      EQU             0x05FFEC00
82
BYTE_SECTOR_BUF EQU     SECTOR_BUF<<2
83
PROG_LOAD_AREA  EQU             0x4080000<<2
84 11 robfinch
 
85
macro m_lsr8
86
lsr
87
lsr
88
lsr
89
lsr
90
lsr
91
lsr
92
lsr
93
lsr
94
endm
95
 
96 15 robfinch
macro m_asl8
97
asl
98
asl
99
asl
100
asl
101
asl
102
asl
103
asl
104
asl
105
endm
106 11 robfinch
 
107 15 robfinch
; BIOS vars at the top of the 8kB scratch memory
108
;
109 18 robfinch
JMPTMP          EQU             0x7A0
110
SP8Save         EQU             0x7AE
111
SRSave          EQU             0x7AF
112
R1Save          EQU             0x7B0
113
R2Save          EQU             0x7B1
114
R3Save          EQU             0x7B2
115
R4Save          EQU             0x7B3
116
R5Save          EQU             0x7B4
117
R6Save          EQU             0x7B5
118
R7Save          EQU             0x7B6
119
R8Save          EQU             0x7B7
120
R9Save          EQU             0x7B8
121
R10Save         EQU             0x7B9
122
R11Save         EQU             0x7BA
123
R12Save         EQU             0x7BB
124
R13Save         EQU             0x7BC
125
R14Save         EQU             0x7BD
126
R15Save         EQU             0x7BE
127
SPSave          EQU             0x7BF
128 15 robfinch
 
129
CharColor       EQU             0x7C0
130
ScreenColor     EQU             0x7C1
131
CursorRow       EQU             0x7C2
132
CursorCol       EQU             0x7C3
133
CursorFlash     EQU             0x7C4
134
Milliseconds    EQU             0x7C5
135 18 robfinch
IRQFlag         EQU             0x7C6
136 15 robfinch
 
137
KeybdHead       EQU             0x7D0
138
KeybdTail       EQU             0x7D1
139
KeybdEcho       EQU             0x7D2
140
KeybdBad        EQU             0x7D3
141
KeybdAck        EQU             0x7D4
142
KeybdBuffer     EQU             0x7D5   ; buffer is 16 chars
143
KeybdLocks      EQU             0x7E5
144
 
145
startSector     EQU             0x7F0
146
 
147
 
148 11 robfinch
        cpu             rtf65002
149
        code
150 15 robfinch
 
151
        ; jump table of popular BIOS routines
152 11 robfinch
        org             $FFFFC000
153 15 robfinch
        dw      DisplayChar
154
        dw      KeybdCheckForKeyDirect
155
        dw      KeybdGetCharDirect
156
 
157
        org             $FFFFC200               ; leave room for 128 vectors
158
KeybdRST
159 11 robfinch
start
160
        sei                                             ; disable interrupts
161
        cld                                             ; disable decimal mode
162 15 robfinch
        ldx             #$05FFFFF8              ; setup stack pointer top of memory
163 11 robfinch
        txs
164 15 robfinch
        trs             r0,dp                   ; set direct page register
165
        trs             r0,dp8                  ; and 8 bit mode direct page
166
        trs             r0,abs8                 ; and 8 bit mode absolute address offset
167
 
168
        ; setup interrupt vectors
169 18 robfinch
        ldx             #$05FFF001              ; interrupt vector table from $5FFF000 to $5FFF1FF
170
                                                        ; also sets nmoi policy (native mode on interrupt)
171 15 robfinch
        trs             r2,vbr
172 18 robfinch
        dex
173 15 robfinch
        lda             #brk_rout
174
        sta             (x)
175
        lda             #slp_rout
176
        sta             1,x
177
        lda             #KeybdRST
178
        sta             448+1,x
179
        lda             #p1000Hz
180
        sta             448+2,x
181
        lda             #p100Hz
182
        sta             448+3,x
183
        lda             #KeybdIRQ
184
        sta             448+15,x
185
 
186
        emm
187
        cpu             W65C02
188
        ldx             #$FF                    ; set 8 bit stack pointer
189
        txs
190
        nat
191
        cpu             rtf65002
192 11 robfinch
        lda             #$CE                    ; CE =blue on blue FB = grey on grey
193
        sta             ScreenColor
194
        sta             CharColor
195
        jsr             ClearScreen
196 15 robfinch
        jsr             ClearBmpScreen
197 11 robfinch
        stz             CursorRow
198
        stz             CursorCol
199 15 robfinch
        lda             #msgStart
200 11 robfinch
        jsr             DisplayStringB
201 15 robfinch
        jsr             KeybdInit
202
        lda             #1
203
        sta             KeybdEcho
204
        jsr             PICInit
205
        cli                                             ; enable interrupts
206
        jmp             Monitor
207
st1
208
        jsr             KeybdGetCharDirect
209
        bra             st1
210 11 robfinch
        stp
211
        bra             start
212
 
213
msgStart
214
        db              "RTF65002 system starting.",$0d,$0a,00
215
 
216 15 robfinch
;----------------------------------------------------------
217
; Initialize programmable interrupt controller (PIC)
218
;  0 = nmi (parity error)
219
;  1 = keyboard reset
220
;  2 = 1000Hz pulse (context switcher)
221
;  3 = 100Hz pulse (cursor flash)
222
;  4 = ethmac
223
;  8 = uart
224
; 13 = raster interrupt
225
; 15 = keyboard char
226
;----------------------------------------------------------
227
PICInit:
228
        ; enable: raster irq,
229
        lda             #$000F                  ; enable nmi,kbd_rst,and kbd_irq
230
        ; A10F enable serial IRQ
231
        sta             PIC_IE
232
PICret:
233
        rts
234
 
235 11 robfinch
;------------------------------------------------------------------------------
236
; Clear the screen and the screen color memory
237
; We clear the screen to give a visual indication that the system
238
; is working at all.
239
;------------------------------------------------------------------------------
240
;
241
ClearScreen:
242
        pha                                                     ; holds a space character
243
        phx                                                     ; loop counter
244
        phy                                                     ; memory addressing
245
        push    r4                                      ; holds the screen color
246
        lda             TEXTREG+TEXT_COLS       ; calc number to clear
247
        ldx             TEXTREG+TEXT_ROWS
248
        mul             r2,r1,r2                        ; r2 = # chars to clear
249
        lda             #' '                            ; space char
250
        ld              r4,ScreenColor
251
        jsr             AsciiToScreen
252
        ldy             #TEXTSCR                        ; text screen address
253
csj4:
254
        sta             (y)
255
        st              r4,$10000,y                     ; color screen is 0x10000 higher
256
        iny
257
        dex
258
        bne             csj4
259
        pop             r4
260
        ply
261
        plx
262
        pla
263
        rts
264
 
265
;------------------------------------------------------------------------------
266
; Scroll text on the screen upwards
267
;------------------------------------------------------------------------------
268
;
269
ScrollUp:
270
        pha
271
        phx
272
        phy
273
        push    r4
274
        push    r5
275
        lda             TEXTREG+TEXT_COLS       ; acc = # text columns
276
        ldx             TEXTREG+TEXT_ROWS
277
        mul             r2,r1,r2                        ; calc number of chars to scroll
278
        sub             r2,r2,r1                        ; one less row
279
        ldy             #TEXTSCR
280
scrup1:
281
        add             r5,r3,r1
282
        ld              r4,(r5)                         ; move character
283
        st              r4,(y)
284
        ld              r4,$10000,r5            ; and move color code
285
        st              r4,$10000,y
286
        iny
287
        dex
288
        bne             scrup1
289
        lda             TEXTREG+TEXT_ROWS
290
        dea
291
        jsr             BlankLine
292
        pop             r5
293
        pop             r4
294
        ply
295
        plx
296
        pla
297
        rts
298
 
299
;------------------------------------------------------------------------------
300
; Blank out a line on the display
301
; line number to blank is in acc
302
;------------------------------------------------------------------------------
303
;
304
BlankLine:
305
        pha
306
        phx
307
        phy
308
        ldx             TEXTREG+TEXT_COLS       ; x = # chars to blank out from video controller
309
        mul             r3,r2,r1                        ; y = screen index (row# * #cols)
310
        add             r3,r3,#TEXTSCR          ; y = screen address
311
        lda             #' '
312
blnkln1:
313
        sta             (y)
314
        iny
315
        dex
316
        bne             blnkln1
317
        ply
318
        plx
319
        pla
320
        rts
321
 
322
;------------------------------------------------------------------------------
323
; Convert ASCII character to screen display character.
324
;------------------------------------------------------------------------------
325
;
326
AsciiToScreen:
327
        and             #$FF
328
        cmp             #'A'
329
        bcc             atoscr1         ; blt
330
        cmp             #'Z'
331
        bcc             atoscr1
332
        beq             atoscr1
333 15 robfinch
        cmp             #'z'+1
334 11 robfinch
        bcs             atoscr1
335
        cmp             #'a'
336
        bcc             atoscr1
337
        sub             #$60
338
atoscr1:
339
        or              #$100
340
        rts
341
 
342
;------------------------------------------------------------------------------
343
; Convert screen character to ascii character
344
;------------------------------------------------------------------------------
345
;
346
ScreenToAscii:
347
        and             #$FF
348 15 robfinch
        cmp             #26+1
349 11 robfinch
        bcs             stasc1
350
        add             #$60
351
stasc1:
352
        rts
353
 
354
;------------------------------------------------------------------------------
355
; Calculate screen memory location from CursorRow,CursorCol.
356
; Also refreshes the cursor location.
357
; Returns:
358
; r1 = screen location
359
;------------------------------------------------------------------------------
360
;
361
CalcScreenLoc:
362
        phx
363
        lda             CursorRow
364
        ldx             TEXTREG+TEXT_COLS
365
        mul             r2,r2,r1
366
        add             r2,r2,CursorCol
367
        stx             TEXTREG+TEXT_CURPOS
368
        add             r1,r2,#TEXTSCR  ; r1 = screen location
369
        plx
370
        rts
371
 
372
;------------------------------------------------------------------------------
373
; Display a character on the screen
374
; r1 = char to display
375
;------------------------------------------------------------------------------
376
;
377
DisplayChar:
378
        cmp             #'\r'                           ; carriage return ?
379
        bne             dccr
380
        stz             CursorCol                       ; just set cursor column to zero on a CR
381
        jsr             CalcScreenLoc
382
        rts
383
dccr:
384
        cmp             #$91                            ; cursor right ?
385
        bne             dcx6
386
        pha
387
        lda             CursorCol
388
        cmp             #83
389
        bcs             dcx7
390
        ina
391
        sta             CursorCol
392
dcx7:
393
        jsr             CalcScreenLoc
394
        pla
395
        rts
396
dcx6:
397
        cmp             #$90                            ; cursor up ?
398
        bne             dcx8
399
        pha
400
        lda             CursorRow
401
        beq             dcx7
402
        dea
403
        sta             CursorRow
404
        bra             dcx7
405
dcx8:
406
        cmp             #$93                            ; cursor left ?
407
        bne             dcx9
408
        pha
409
        lda             CursorCol
410
        beq             dcx7
411
        dea
412
        sta             CursorCol
413
        bra             dcx7
414
dcx9:
415
        cmp             #$92                            ; cursor down ?
416
        bne             dcx10
417
        pha
418
        lda             CursorRow
419
        cmp             #46
420
        beq             dcx7
421
        ina
422
        sta             CursorRow
423
        bra             dcx7
424
dcx10:
425
        cmp             #$94                            ; cursor home ?
426
        bne             dcx11
427
        pha
428
        lda             CursorCol
429
        beq             dcx12
430
        stz             CursorCol
431
        bra             dcx7
432
dcx12:
433
        stz             CursorRow
434
        bra             dcx7
435
dcx11:
436
        pha
437
        phx
438
        phy
439
        cmp             #$99                            ; delete ?
440
        bne             dcx13
441
        jsr             CalcScreenLoc
442
        tay                                                     ; y = screen location
443
        lda             CursorCol                       ; acc = cursor column
444
        bra             dcx5
445
dcx13
446
        cmp             #CTRLH                          ; backspace ?
447
        bne             dcx3
448
        lda             CursorCol
449
        beq             dcx4
450
        dea
451
        sta             CursorCol
452
        jsr             CalcScreenLoc           ; acc = screen location
453
        tay                                                     ; y = screen location
454
        lda             CursorCol
455
dcx5:
456
        ldx             $4,y
457
        stx             (y)
458
        iny
459
        ina
460
        cmp             TEXTREG+TEXT_COLS
461
        bcc             dcx5
462
        lda             #' '
463
        jsr             AsciiToScreen
464
        dey
465
        sta             (y)
466
        bra             dcx4
467
dcx3:
468
        cmp             #'\n'                   ; linefeed ?
469
        beq             dclf
470
        tax                                             ; save acc in x
471
        jsr     CalcScreenLoc   ; acc = screen location
472
        tay                                             ; y = screen location
473
        txa                                             ; restore r1
474
        jsr             AsciiToScreen   ; convert ascii char to screen char
475
        sta             (y)
476
        lda             CharColor
477
        sta             $10000,y
478
        jsr             IncCursorPos
479
        bra             dcx4
480
dclf:
481
        jsr             IncCursorRow
482
dcx4:
483
        ply
484
        plx
485
        pla
486
        rts
487
 
488
;------------------------------------------------------------------------------
489
; Increment the cursor position, scroll the screen if needed.
490
;------------------------------------------------------------------------------
491
;
492
IncCursorPos:
493
        pha
494
        phx
495
        lda             CursorCol
496
        ina
497
        sta             CursorCol
498
        ldx             TEXTREG+TEXT_COLS
499
        cmp             r1,r2
500
        bcc             icc1
501
        stz             CursorCol               ; column = 0
502
        bra             icr1
503
IncCursorRow:
504
        pha
505
        phx
506
icr1:
507
        lda             CursorRow
508
        ina
509
        sta             CursorRow
510
        ldx             TEXTREG+TEXT_ROWS
511
        cmp             r1,r2
512
        bcc             icc1
513
        beq             icc1
514
        dex                                                     ; backup the cursor row, we are scrolling up
515
        stx             CursorRow
516
        jsr             ScrollUp
517
icc1:
518
        jsr             CalcScreenLoc
519
        plx
520
        pla
521
        rts
522
 
523
;------------------------------------------------------------------------------
524
; Display a string on the screen.
525
; The characters are packed 4 per word
526
;------------------------------------------------------------------------------
527
;
528
DisplayStringB:
529
        pha
530
        phx
531
        tax                                             ; r2 = pointer to string
532
dspj1B:
533 15 robfinch
        lb              r1,0,x                  ; move string char into acc
534 11 robfinch
        inx                                             ; increment pointer
535
        cmp             #0                              ; is it end of string ?
536
        beq             dsretB
537
        jsr             DisplayChar             ; display character
538 15 robfinch
        bra             dspj1B
539 11 robfinch
dsretB:
540
        plx
541
        pla
542
        rts
543
 
544
;------------------------------------------------------------------------------
545
; Display a string on the screen.
546
; The characters are packed 1 per word
547
;------------------------------------------------------------------------------
548
;
549
DisplayStringW:
550
        pha
551
        phx
552
        tax                                             ; r2 = pointer to string
553
dspj1W:
554
        lda             (x)                             ; move string char into acc
555
        inx                                             ; increment pointer
556
        cmp             #0                              ; is it end of string ?
557
        beq             dsretW
558
        jsr             DisplayChar             ; display character
559
        bra             dspj1W                  ; go back for next character
560
dsretW:
561
        plx
562
        pla
563
        rts
564
 
565
DisplayStringCRLFB:
566
        jsr             DisplayStringB
567
CRLF:
568
        pha
569
        lda             #'\r'
570
        jsr             DisplayChar
571
        lda             #'\n'
572
        jsr             DisplayChar
573
        pla
574
        rts
575
 
576
;------------------------------------------------------------------------------
577 15 robfinch
; Initialize keyboard
578
;
579
; Issues a 'reset keyboard' command to the keyboard, then selects scan code
580
; set #2 (the most common one). Also sets up the keyboard buffer and
581
; initializes the keyboard semaphore.
582
;------------------------------------------------------------------------------
583
;
584
KeybdInit:
585
        lda             #1                      ; setup semaphore
586
;       sta             KEYBD_SEMA
587
        stz             KeybdHead               ; setup keyboard buffer
588
        stz             KeybdTail
589
        lda             #1                      ; turn on keyboard echo
590
        sta             KeybdEcho
591
        stz             KeybdBad
592
 
593
        lda             #$ff            ; issue keyboard reset
594
        jsr             SendByteToKeybd
595
        lda             #1000000                ; delay a bit
596
kbdi5:
597
        dea
598
        bne             kbdi5
599
        lda             #0xf0           ; send scan code select
600
        jsr             SendByteToKeybd
601
        ldx             #0xFA
602
        jsr             WaitForKeybdAck
603
        cmp             #$FA
604
        bne             kbdi2
605
        lda             #2                      ; select scan code set#2
606
        jsr             SendByteToKeybd
607
kbdi2:
608
        rts
609
 
610
msgBadKeybd:
611
        db              "Keyboard not responding.",0
612
 
613
SendByteToKeybd:
614
        sta             KEYBD
615
        tsr             TICK,r3
616
kbdi4:                                          ; wait for transmit complete
617
        tsr             TICK,r4
618
        sub             r4,r4,r3
619
        cmp             r4,#1000000
620
        bcs             kbdbad
621
        lda             KEYBD+3
622
        bit             #64
623
        beq             kbdi4
624
        bra             sbtk1
625
kbdbad:
626
        lda             KeybdBad
627
        bne             sbtk1
628
        lda             #1
629
        sta             KeybdBad
630
        lda             #msgBadKeybd
631
        jsr             DisplayStringCRLFB
632
sbtk1:
633
        rts
634
 
635
; Wait for keyboard to respond with an ACK (FA)
636
;
637
WaitForKeybdAck:
638
        tsr             TICK,r3
639
wkbdack1:
640
        tsr             TICK,r4
641
        sub             r4,r4,r3
642
        cmp             r4,#1000000
643
        bcs             wkbdbad
644
        lda             KEYBD
645
        bit             #$8000
646
        beq             wkbdack1
647
;       lda             KEYBD+8
648
        and             #$ff
649
wkbdbad:
650
        rts
651
 
652
; Wait for keyboard to respond with an ACK (FA)
653
; This routine picks up the ack status left by the
654
; keyboard IRQ routine.
655
; r2 = 0xFA (could also be 0xEE for echo command)
656
;
657
WaitForKeybdAck2:
658
        lda             KeybdAck
659
        cmp             r1,r2
660
        bne             WaitForKeybdAck2
661
        stz             KeybdAck
662
        rts
663
 
664
;------------------------------------------------------------------------------
665
; Normal keyboard interrupt, the lowest priority interrupt in the system.
666
; Grab the character from the keyboard device and store it in a buffer.
667
; Doesn't use the stack.
668
;------------------------------------------------------------------------------
669
;
670
KeybdIRQ:
671
        pha
672
        phx
673
        phy
674
        ldx             KEYBD                           ; get keyboard character
675
        ld              r0,KEYBD+1                      ; clear keyboard strobe (turns off the IRQ)
676
        txy                                                     ; check for a keyboard ACK code
677
        and             r3,r3,#$ff
678
        cmp             r3,#$FA
679
        bne             KeybdIrq1
680
        sty             KeybdAck
681
        bra             KeybdIRQc
682
KeybdIrq1:
683
        bit             r2,#$800                                ; test bit #11
684
        bne             KeybdIRQc                               ; ignore keyup messages for now
685
        lda             KeybdHead
686
        ina                                                             ; increment head pointer
687
        and             #$f                                             ; limit
688
        ldy             KeybdTail                               ; check for room in the keyboard buffer
689
        cmp             r1,r3
690
        beq             KeybdIRQc                               ; if no room, the newest char will be lost
691
        sta             KeybdHead
692
        dea
693
        and             #$f
694
        stx             KeybdBuffer,r1                  ; store character in buffer
695
        stx             KeybdLocks
696
KeybdIRQc:
697
        ply
698
        plx
699
        pla
700
        rti
701
 
702
KeybdRstIRQ:
703
        jmp             ColdStart
704
 
705
;------------------------------------------------------------------------------
706
; r1 0=echo off, non-zero = echo on
707
;------------------------------------------------------------------------------
708
SetKeyboardEcho:
709
        sta             KeybdEcho
710
        rts
711
 
712
;-----------------------------------------
713
; Get character from keyboard buffer
714
; return character in acc or -1 if no
715
; characters available
716
;-----------------------------------------
717
KeybdGetChar:
718
        phx
719
        ldx             KeybdTail       ; if keybdTail==keybdHead then there are no
720
        lda             KeybdHead       ; characters in the keyboard buffer
721
        cmp             r1,r2
722
        beq             nochar
723
        lda             KeybdBuffer,x
724
        and             r1,r1,#$ff              ; mask off control bits
725
        inx                                             ; increment index
726
        and             r2,r2,#$0f
727
        stx             KeybdTail
728
        ldx             KeybdEcho
729
        beq             kgc3
730
        cmp             #CR
731
        bne             kgc2
732
        jsr             CRLF                    ; convert CR keystroke into CRLF
733
        bra             kgc3
734
kgc2:
735
        jsr             DisplayChar
736
        bra             kgc3
737
nochar:
738
        lda             #-1
739
kgc3:
740
        plx
741
        rts
742
 
743
;------------------------------------------------------------------------------
744
; Check if there is a keyboard character available in the keyboard buffer.
745
;------------------------------------------------------------------------------
746
;
747
KeybdCheckForKey:
748
        phx
749
        lda             KeybdTail
750
        ldx             KeybdHead
751
        sub             r1,r1,r2
752
        bne             kcfk1
753
        plx
754
        rts
755
kcfk1
756
        lda             #1
757
        plx
758
        rts
759
message "668"
760
;------------------------------------------------------------------------------
761
; Check if there is a keyboard character available. If so return true (1)
762
; otherwise return false (0) in r1.
763
;------------------------------------------------------------------------------
764
;
765
KeybdCheckForKeyDirect:
766
        lda             KEYBD
767
        and             #$8000
768
        beq             kcfkd1
769
        lda             #1
770
kcfkd1
771
        rts
772
 
773
;------------------------------------------------------------------------------
774
; Get character directly from keyboard. This routine blocks until a key is
775
; available.
776
;------------------------------------------------------------------------------
777
;
778
KeybdGetCharDirect:
779
        phx
780
kgc1:
781
        lda             KEYBD
782
        bit             #$8000
783
        beq             kgc1
784
        ld              r0,KEYBD+1              ; clear keyboard strobe
785
        bit             #$800                   ; is it a keydown event ?
786
        bne             kgc1
787
        and             #$ff                    ; remove strobe bit
788
        ldx             KeybdEcho               ; is keyboard echo on ?
789
        beq             gk1
790
        cmp             #CR
791
        bne             gk2                             ; convert CR keystroke into CRLF
792
        jsr             CRLF
793
        bra             gk1
794
gk2:
795
        jsr             DisplayChar
796
gk1:
797
        plx
798
        rts
799
 
800
 
801
;------------------------------------------------------------------------------
802 11 robfinch
; Display nybble in r1
803
;------------------------------------------------------------------------------
804
;
805
DisplayNybble:
806
        pha
807
        and             #$0F
808
        add             #'0'
809 15 robfinch
        cmp             #'9'+1
810 11 robfinch
        bcc             dispnyb1
811
        add             #7
812
dispnyb1:
813
        jsr             DisplayChar
814
        pla
815
        rts
816
 
817
;------------------------------------------------------------------------------
818
; Display the byte in r1
819
;------------------------------------------------------------------------------
820
;
821
DisplayByte:
822
        pha
823 18 robfinch
        lsr             r1,r1,#4
824 11 robfinch
        jsr             DisplayNybble
825
        pla
826
        jmp             DisplayNybble   ; tail rts
827 15 robfinch
message "785"
828
;------------------------------------------------------------------------------
829
; Display the half-word in r1
830
;------------------------------------------------------------------------------
831
;
832
DisplayHalf:
833
        pha
834 18 robfinch
        lsr             r1,r1,#8
835 15 robfinch
        jsr             DisplayByte
836
        pla
837
        jsr             DisplayByte
838
        rts
839 11 robfinch
 
840 15 robfinch
message "797"
841 11 robfinch
;------------------------------------------------------------------------------
842 15 robfinch
; Display the half-word in r1
843 11 robfinch
;------------------------------------------------------------------------------
844 15 robfinch
;
845
DisplayWord:
846
        pha
847 18 robfinch
        lsr             r1,r1,#16
848 15 robfinch
        jsr             DisplayHalf
849
        pla
850
        jsr             DisplayHalf
851
        rts
852
message "810"
853
;------------------------------------------------------------------------------
854
; Display memory pointed to by r2.
855
; destroys r1,r3
856
;------------------------------------------------------------------------------
857
;
858
DisplayMemW:
859
        pha
860
        lda             #':'
861
        jsr             DisplayChar
862
        txa
863
        jsr             DisplayWord
864
        lda             #' '
865
        jsr             DisplayChar
866
        lda             (x)
867
        jsr             DisplayWord
868
        inx
869
        lda             #' '
870
        jsr             DisplayChar
871
        lda             (x)
872
        jsr             DisplayWord
873
        inx
874
        lda             #' '
875
        jsr             DisplayChar
876
        lda             (x)
877
        jsr             DisplayWord
878
        inx
879
        lda             #' '
880
        jsr             DisplayChar
881
        lda             (x)
882
        jsr             DisplayWord
883
        inx
884
        jsr             CRLF
885
        pla
886
        rts
887
message "845"
888
;==============================================================================
889
; System Monitor Program
890
;==============================================================================
891
;
892
Monitor:
893 18 robfinch
        ldx             #$05FFFFF8              ; setup stack pointer top of memory
894 15 robfinch
        txs
895
        stz             KeybdEcho               ; turn off keyboard echo
896
PromptLn:
897
        jsr             CRLF
898
        lda             #'$'
899
        jsr             DisplayChar
900
 
901
; Get characters until a CR is keyed
902
;
903
Prompt3:
904
;       lw              r1,#2                   ; get keyboard character
905
;       syscall #417
906
        jsr             KeybdCheckForKeyDirect
907
        cmp             #0
908
        beq             Prompt3
909
        jsr             KeybdGetCharDirect
910
        cmp             #CR
911
        beq             Prompt1
912
        jsr             DisplayChar
913
        bra             Prompt3
914
 
915
; Process the screen line that the CR was keyed on
916
;
917
Prompt1:
918
        stz             CursorCol               ; go back to the start of the line
919
        jsr             CalcScreenLoc   ; r1 = screen memory location
920
        tay
921
        lda             (y)
922
        iny
923
        jsr             ScreenToAscii
924
        cmp             #'$'
925
        bne             Prompt2                 ; skip over '$' prompt character
926
        lda             (y)
927
        iny
928
        jsr             ScreenToAscii
929
 
930
; Dispatch based on command character
931
;
932
Prompt2:
933
        cmp             #':'
934
        beq             EditMem
935
        cmp             #'D'
936 18 robfinch
        bne             Prompt8
937
        lda             (y)
938
        iny
939
        jsr             ScreenToAscii
940
        cmp             #'R'
941
        beq             DumpReg
942
        dey
943
        bra             DumpMem
944
Prompt8:
945 15 robfinch
        cmp             #'F'
946
        beq             FillMem
947
Prompt7:
948
        cmp             #'B'                    ; $B - start tiny basic
949
        bne             Prompt4
950
        jsr             CSTART
951
        bra             Monitor
952
Prompt4:
953
        cmp             #'b'
954
        bne             Prompt5
955
        emm
956
        cpu             W65C02
957
        jml             $0C000
958
        cpu             rtf65002
959
Prompt5:
960
        cmp             #'J'                    ; $J - execute code
961
        beq             ExecuteCode
962
        cmp             #'L'                    ; $L - load S19 file
963
        bne             Prompt9
964
        jmp             LoadSector
965
Prompt9:
966
        cmp             #'?'                    ; $? - display help
967
        bne             Prompt10
968
        lda             #HelpMsg
969
        jsr             DisplayStringB
970
        jmp             Monitor
971
Prompt10:
972
        cmp             #'C'                    ; $C - clear screen
973
        beq             TestCLS
974
        cmp             #'R'
975
        bne             Prompt12
976
        jmp             RandomLinesCall
977
Prompt12:
978
Prompt13:
979
        cmp             #'P'
980
        bne             Prompt14
981
        jmp             Piano
982
Prompt14:
983
        cmp             #'T'
984
        bne             Prompt15
985
        call    tmp_read
986
Prompt15:
987
        cmp             #'S'
988
        bne             Prompt16
989
        jsr             spi_init
990
        cmp             #0
991
        bne             Monitor
992
        jsr             spi_read_part
993
        cmp             #0
994
        bne             Monitor
995
        jsr             spi_read_boot
996
        cmp             #0
997
        bne             Monitor
998
        jsr             loadBootFile
999
        jmp             Monitor
1000
Prompt16:
1001
        jmp             Monitor
1002
message "Prompt16"
1003
RandomLinesCall:
1004
        jsr             RandomLines
1005
        jmp             Monitor
1006
 
1007
TestCLS:
1008
        lda             (y)
1009
        iny
1010
        jsr             ScreenToAscii
1011
        cmp             #'L'
1012
        bne             Monitor
1013
        lda             (y)
1014
        iny
1015
        jsr             ScreenToAscii
1016
        cmp             #'S'
1017
        bne             Monitor
1018
        jsr     ClearScreen
1019
        stz             CursorCol
1020
        stz             CursorRow
1021
        jsr             CalcScreenLoc
1022
        jmp             Monitor
1023
 
1024
HelpMsg:
1025
        db      "? = Display help",CR,LF
1026
        db      "CLS = clear screen",CR,LF
1027
        db      "S = Boot from SD Card",CR,LF
1028
        db      ": = Edit memory bytes",CR,LF
1029
        db      "L = Load S19 file",CR,LF
1030 18 robfinch
        db  "DR = Dump registers",CR,LF
1031 15 robfinch
        db      "D = Dump memory",CR,LF
1032
        db      "F = Fill memory",CR,LF
1033
        db      "B = start tiny basic",CR,LF
1034
        db      "b = start EhBasic 6502",CR,LF
1035
        db      "J = Jump to code",CR,LF
1036 18 robfinch
        db      "R[n] = Set register value",CR,LF
1037 15 robfinch
        db      "T = get temperature",CR,LF
1038
        db      "P = Piano",CR,LF,0
1039
 
1040
;------------------------------------------------------------------------------
1041
; Ignore blanks in the input
1042
; r3 = text pointer
1043
; r1 destroyed
1044
;------------------------------------------------------------------------------
1045
;
1046
ignBlanks:
1047
ignBlanks1:
1048
        lda             (y)
1049
        iny
1050
        jsr             ScreenToAscii
1051
        cmp             #' '
1052
        beq             ignBlanks1
1053
        dey
1054
        rts
1055
 
1056
;------------------------------------------------------------------------------
1057
; Edit memory byte(s).
1058
;------------------------------------------------------------------------------
1059
;
1060
EditMem:
1061
        jsr             ignBlanks
1062
        jsr             GetHexNumber
1063
        or              r5,r1,r0
1064
        ld              r4,#3
1065
edtmem1:
1066
        jsr             ignBlanks
1067
        jsr             GetHexNumber
1068
        sta             (r5)
1069
        add             r5,r5,#1
1070
        dec             r4
1071
        bne             edtmem1
1072
        jmp             Monitor
1073
 
1074
;------------------------------------------------------------------------------
1075
; Execute code at the specified address.
1076
;------------------------------------------------------------------------------
1077
;
1078
ExecuteCode:
1079
        jsr             ignBlanks
1080
        jsr             GetHexNumber
1081 18 robfinch
        st              r1,JMPTMP
1082
        lda             #xcret                  ; push return address so we can do an indirect jump
1083
        pha
1084
        ld              r1,R1Save
1085
        ld              r2,R2Save
1086
        ld              r3,R3Save
1087
        ld              r4,R4Save
1088
        ld              r5,R5Save
1089
        ld              r6,R6Save
1090
        ld              r7,R7Save
1091
        ld              r8,R8Save
1092
        ld              r9,R9Save
1093
        ld              r10,R10Save
1094
        ld              r11,R11Save
1095
        ld              r12,R12Save
1096
        ld              r13,R13Save
1097
        ld              r14,R14Save
1098
        ld              r15,R15Save
1099
        jmp             (JMPTMP)
1100
xcret:
1101
        php
1102
        st              r1,R1Save
1103
        st              r2,R2Save
1104
        st              r3,R3Save
1105
        st              r4,R4Save
1106
        st              r5,R5Save
1107
        st              r6,R6Save
1108
        st              r7,R7Save
1109
        st              r8,R8Save
1110
        st              r9,R9Save
1111
        st              r10,R10Save
1112
        st              r11,R11Save
1113
        st              r12,R12Save
1114
        st              r13,R13Save
1115
        st              r14,R14Save
1116
        st              r15,R15Save
1117
        tsr             sp,r1
1118
        st              r1,SPSave
1119
        tsr             sp8,r1
1120
        st              r1,SP8Save
1121
        pla
1122
        sta             SRSave
1123 15 robfinch
        jmp     Monitor
1124
 
1125
LoadSector:
1126
        jsr             ignBlanks
1127
        jsr             GetHexNumber
1128
        ld              r2,#0x3800
1129
        jsr             spi_read_sector
1130
        jmp             Monitor
1131
 
1132
;------------------------------------------------------------------------------
1133 18 robfinch
; Dump the register set.
1134
;------------------------------------------------------------------------------
1135
DumpReg:
1136
        ldy             #0
1137
DumpReg1:
1138
        jsr             CRLF
1139
        lda             #':'
1140
        jsr             DisplayChar
1141
        lda             #'R'
1142
        jsr             DisplayChar
1143
        ldx             #0
1144
        tya
1145
        jsr             PRTNUM
1146
        lda             #' '
1147
        jsr             DisplayChar
1148
        lda             R1Save,y
1149
        jsr             DisplayWord
1150
        iny
1151
        cpy             #15
1152
        bne             DumpReg1
1153
        jsr             CRLF
1154
        lda             #':'
1155
        jsr             DisplayChar
1156
        lda             #'S'
1157
        jsr             DisplayChar
1158
        lda             #'P'
1159
        jsr             DisplayChar
1160
        lda             #' '
1161
        jsr             DisplayChar
1162
        lda             SPSave
1163
        jsr             DisplayWord
1164
        jsr             CRLF
1165
        rts
1166
 
1167
;------------------------------------------------------------------------------
1168 15 robfinch
; Do a memory dump of the requested location.
1169
;------------------------------------------------------------------------------
1170
;
1171
DumpMem:
1172
        jsr             ignBlanks
1173
        jsr             GetHexNumber    ; get start address of dump
1174
        tax
1175
        jsr             ignBlanks
1176
        jsr             GetHexNumber    ; get number of words to dump
1177
        lsr                                             ; 1/4 as many dump rows
1178
        lsr
1179
        bne             Dumpmem2
1180
        lda             #1                              ; dump at least one row
1181
Dumpmem2:
1182
        jsr             CRLF
1183
        bra             DumpmemW
1184
DumpmemW:
1185
        jsr             DisplayMemW
1186
        dea
1187
        bne             DumpmemW
1188
        jmp             Monitor
1189
 
1190
 
1191
        bra             Monitor
1192
 
1193
FillMem:
1194
        jsr             ignBlanks
1195
        jsr             GetHexNumber    ; get start address of dump
1196
        tax
1197
        jsr             ignBlanks
1198
        jsr             GetHexNumber    ; get number of bytes to fill
1199
        or              r5,r1,r0
1200
        jsr             ignBlanks
1201
        jsr             GetHexNumber    ; get the fill byte
1202
FillmemW:
1203
        sta             (x)
1204
        inx
1205
        dec             r5
1206
        bne             FillmemW
1207
        jmp             Monitor
1208
 
1209
;------------------------------------------------------------------------------
1210
; Get a hexidecimal number. Maximum of eight digits.
1211
; R3 = text pointer (updated)
1212
; R1 = hex number
1213
;------------------------------------------------------------------------------
1214
;
1215
GetHexNumber:
1216
        phx
1217
        push    r4
1218
        ldx             #0
1219
        ld              r4,#8
1220
gthxn2:
1221
        lda             (y)
1222
        iny
1223
        jsr             ScreenToAscii
1224
        jsr             AsciiToHexNybble
1225
        cmp             #-1
1226
        beq             gthxn1
1227 18 robfinch
        asl             r2,r2,#4
1228 15 robfinch
        and             #$0f
1229
        or              r2,r2,r1
1230
        dec             r4
1231
        bne             gthxn2
1232
gthxn1:
1233
        txa
1234
        pop             r4
1235
        plx
1236
        rts
1237
 
1238
;------------------------------------------------------------------------------
1239
; Convert ASCII character in the range '0' to '9', 'a' to 'f' or 'A' to 'F'
1240
; to a hex nybble.
1241
;------------------------------------------------------------------------------
1242
;
1243
AsciiToHexNybble:
1244
        cmp             #'0'
1245
        bcc             gthx3
1246
        cmp             #'9'+1
1247
        bcs             gthx5
1248
        sub             #'0'
1249
        rts
1250
gthx5:
1251
        cmp             #'A'
1252
        bcc             gthx3
1253
        cmp             #'F'+1
1254
        bcs             gthx6
1255
        sub             #'A'
1256
        add             #10
1257
        rts
1258
gthx6:
1259
        cmp             #'a'
1260
        bcc             gthx3
1261
        cmp             #'z'+1
1262
        bcs             gthx3
1263
        sub             #'a'
1264
        add             #10
1265
        rts
1266
gthx3:
1267
        lda             #-1             ; not a hex number
1268
        rts
1269
 
1270
;------------------------------------------------------------------------------
1271
;------------------------------------------------------------------------------
1272 11 robfinch
ClearBmpScreen:
1273
        pha
1274
        phx
1275
        phy
1276
        ldx             #(1364*768)>>2          ; x = # words to clear
1277
        lda             #0x29292929                     ; acc = color for four pixels
1278
        ldy             #BITMAPSCR                      ; y = screen address
1279 15 robfinch
cbsj4
1280 11 robfinch
        sta             (y)                                     ; store pixel data
1281
        iny                                                     ; advance screen address
1282
        dex                                                     ; decrement pixel count and loop back
1283 15 robfinch
        bne             cbsj4
1284 11 robfinch
        ply
1285
        plx
1286
        pla
1287
        rts
1288
 
1289
;==============================================================================
1290
;==============================================================================
1291
;
1292
; Initialize the SD card
1293
; Returns
1294
; acc = 0 if successful, 1 otherwise
1295
;
1296
spi_init
1297
        lda             #SPI_INIT_SD
1298
        sta             SPIMASTER+SPI_TRANS_TYPE_REG
1299
        lda             #SPI_TRANS_START
1300
        sta             SPIMASTER+SPI_TRANS_CTRL_REG
1301
        nop
1302
spi_init1
1303
        lda             SPIMASTER+SPI_TRANS_STATUS_REG
1304
        nop
1305
        nop
1306
        cmp             #SPI_TRANS_BUSY
1307
        beq             spi_init1
1308
        lda             SPIMASTER+SPI_TRANS_ERROR_REG
1309
        and             #3
1310
        cmp             #SPI_INIT_NO_ERROR
1311
        bne             spi_error
1312 15 robfinch
        lda             #spi_init_ok_msg
1313 11 robfinch
        jsr             DisplayStringB
1314
        lda             #0
1315
        bra             spi_init_exit
1316
spi_error
1317
        jsr             DisplayByte
1318 15 robfinch
        lda             #spi_init_error_msg
1319 11 robfinch
        jsr             DisplayStringB
1320
        lda             SPIMASTER+SPI_RESP_BYTE1
1321
        jsr             DisplayByte
1322
        lda             SPIMASTER+SPI_RESP_BYTE2
1323
        jsr             DisplayByte
1324
        lda             SPIMASTER+SPI_RESP_BYTE3
1325
        jsr             DisplayByte
1326
        lda             SPIMASTER+SPI_RESP_BYTE4
1327
        jsr             DisplayByte
1328
        lda             #1
1329
spi_init_exit
1330
        rts
1331
 
1332
 
1333
 
1334
; SPI read sector
1335
;
1336
; r1= sector number to read
1337
; r2= address to place read data
1338
; Returns:
1339
; r1 = 0 if successful
1340
;
1341
spi_read_sector:
1342
        phx
1343
        phy
1344
        push    r4
1345
 
1346
        sta             SPIMASTER+SPI_SD_SECT_7_0_REG
1347 18 robfinch
        lsr             r1,r1,#8
1348 11 robfinch
        sta             SPIMASTER+SPI_SD_SECT_15_8_REG
1349 18 robfinch
        lsr             r1,r1,#8
1350 11 robfinch
        sta             SPIMASTER+SPI_SD_SECT_23_16_REG
1351 18 robfinch
        lsr             r1,r1,#8
1352 11 robfinch
        sta             SPIMASTER+SPI_SD_SECT_31_24_REG
1353
 
1354
        ld              r4,#20  ; retry count
1355
 
1356
spi_read_retry:
1357
        ; Force the reciever fifo to be empty, in case a prior error leaves it
1358
        ; in an unknown state.
1359
        lda             #1
1360
        sta             SPIMASTER+SPI_RX_FIFO_CTRL_REG
1361
 
1362
        lda             #RW_READ_SD_BLOCK
1363
        sta             SPIMASTER+SPI_TRANS_TYPE_REG
1364
        lda             #SPI_TRANS_START
1365
        sta             SPIMASTER+SPI_TRANS_CTRL_REG
1366
        nop
1367
spi_read_sect1:
1368
        lda             SPIMASTER+SPI_TRANS_STATUS_REG
1369
        nop                                     ; just a delay between consecutive status reg reads
1370
        nop
1371
        cmp             #SPI_TRANS_BUSY
1372
        beq             spi_read_sect1
1373
        lda             SPIMASTER+SPI_TRANS_ERROR_REG
1374
        lsr
1375
        lsr
1376
        and             #3
1377
        cmp             #SPI_READ_NO_ERROR
1378
        bne             spi_read_error
1379
        ldy             #512            ; read 512 bytes from fifo
1380
spi_read_sect2:
1381
        lda             SPIMASTER+SPI_RX_FIFO_DATA_REG
1382 15 robfinch
        sb              r1,0,x
1383 11 robfinch
        inx
1384
        dey
1385
        bne             spi_read_sect2
1386
        lda             #0
1387
        bra             spi_read_ret
1388
spi_read_error:
1389 15 robfinch
        dec             r4
1390 11 robfinch
        bne             spi_read_retry
1391
        jsr             DisplayByte
1392 15 robfinch
        lda             #spi_read_error_msg
1393 11 robfinch
        jsr             DisplayStringB
1394
        lda             #1
1395
spi_read_ret:
1396
        pop             r4
1397
        ply
1398
        plx
1399
        rts
1400
 
1401
; SPI write sector
1402
;
1403
; r1= sector number to write
1404
; r2= address to get data from
1405
; Returns:
1406
; r1 = 0 if successful
1407
;
1408
spi_write_sector:
1409
        phx
1410
        phy
1411
        pha
1412
        ; Force the transmitter fifo to be empty, in case a prior error leaves it
1413
        ; in an unknown state.
1414
        lda             #1
1415
        sta             SPIMASTER+SPI_TX_FIFO_CTRL_REG
1416
        nop                     ; give I/O time to respond
1417
        nop
1418
 
1419
        ; now fill up the transmitter fifo
1420
        ldy             #512
1421
spi_write_sect1:
1422 15 robfinch
        lb              r1,0,x
1423 11 robfinch
        sta             SPIMASTER+SPI_TX_FIFO_DATA_REG
1424
        nop                     ; give the I/O time to respond
1425
        nop
1426
        inx
1427
        dey
1428
        bne             spi_write_sect1
1429
 
1430
        ; set the sector number in the spi master address registers
1431
        pla
1432
        sta             SPIMASTER+SPI_SD_SECT_7_0_REG
1433 18 robfinch
        lsr             r1,r1,#8
1434 11 robfinch
        sta             SPIMASTER+SPI_SD_SECT_15_8_REG
1435 18 robfinch
        lsr             r1,r1,#8
1436 11 robfinch
        sta             SPIMASTER+SPI_SD_SECT_23_16_REG
1437 18 robfinch
        lsr             r1,r1,#8
1438 11 robfinch
        sta             SPIMASTER+SPI_SD_SECT_31_24_REG
1439
 
1440
        ; issue the write command
1441
        lda             #RW_WRITE_SD_BLOCK
1442
        sta             SPIMASTER+SPI_TRANS_TYPE_REG
1443
        lda             #SPI_TRANS_START
1444
        sta             SPIMASTER+SPI_TRANS_CTRL_REG
1445
        nop
1446
spi_write_sect2:
1447
        lda             SPIMASTER+SPI_TRANS_STATUS_REG
1448
        nop                                                     ; just a delay between consecutive status reg reads
1449
        nop
1450
        cmp             #SPI_TRANS_BUSY
1451
        beq             spi_write_sect2
1452
        lda             SPIMASTER+SPI_TRANS_ERROR_REG
1453 18 robfinch
        lsr             r1,r1,#4
1454 11 robfinch
        and             #3
1455
        cmp             #SPI_WRITE_NO_ERROR
1456
        bne             spi_write_error
1457
        lda             #0
1458
        bra             spi_write_ret
1459
spi_write_error:
1460
        jsr             DisplayByte
1461 15 robfinch
        lda             #spi_write_error_msg
1462 11 robfinch
        jsr             DisplayStringB
1463
        lda             #1
1464
 
1465
spi_write_ret:
1466
        ply
1467
        plx
1468
        rts
1469
 
1470 15 robfinch
; read the partition table to find out where the boot sector is.
1471
;
1472
spi_read_part:
1473
        phx
1474
        stz             startSector                                             ; default starting sector
1475
        lda             #0                                                              ; r1 = sector number (#0)
1476
        ldx             #SECTOR_BUF<<2                                  ; r2 = target address (word to byte address)
1477
        jsr             spi_read_sector
1478 18 robfinch
        cmp             #0
1479
        bne             spi_rp1
1480 15 robfinch
        lb              r1,BYTE_SECTOR_BUF+$1C9
1481 18 robfinch
        asl             r1,r1,#8
1482 15 robfinch
        orb             r1,r1,BYTE_SECTOR_BUF+$1C8
1483 18 robfinch
        asl             r1,r1,#8
1484 15 robfinch
        orb             r1,r1,BYTE_SECTOR_BUF+$1C7
1485 18 robfinch
        asl             r1,r1,#8
1486 15 robfinch
        orb             r1,r1,BYTE_SECTOR_BUF+$1C6
1487
        sta             startSector                                             ; r1 = 0, for okay status
1488 18 robfinch
        plx
1489 15 robfinch
        lda             #0
1490 18 robfinch
        rts
1491
spi_rp1:
1492 15 robfinch
        plx
1493 18 robfinch
        lda             #1
1494 15 robfinch
        rts
1495
 
1496
; Read the boot sector from the disk.
1497
; Make sure it's the boot sector by looking for the signature bytes 'EB' and '55AA'.
1498
;
1499
spi_read_boot:
1500
        phx
1501
        phy
1502
        push    r5
1503
        lda             startSector                                     ; r1 = sector number
1504
        ldx             #BYTE_SECTOR_BUF                        ; r2 = target address
1505
        jsr             spi_read_sector
1506
        lb              r1,BYTE_SECTOR_BUF
1507
        cmp             #$EB
1508
        beq             spi_read_boot2
1509
spi_read_boot3:
1510
        lda             #1                                                      ; r1 = 1 for error
1511
        bra             spi_read_boot4
1512
spi_read_boot2:
1513
        lda             #msgFoundEB
1514
        jsr             DisplayStringB
1515
        lb              r1,BYTE_SECTOR_BUF+$1FE         ; check for 0x55AA signature
1516
        cmp             #$55
1517
        bne             spi_read_boot3
1518
        lb              r1,BYTE_SECTOR_BUF+$1FF         ; check for 0x55AA signature
1519
        cmp             #$AA
1520
        bne             spi_read_boot3
1521
        lda             #0                                              ; r1 = 0, for okay status
1522
spi_read_boot4:
1523
        pop             r5
1524
        ply
1525
        plx
1526
        rts
1527
 
1528
msgFoundEB:
1529
        db      "Found EB code.",CR,LF,0
1530
 
1531
 
1532
; Load the root directory from disk
1533
; r2 = where to place root directory in memory
1534
;
1535
loadBootFile:
1536
        lb              r1,BYTE_SECTOR_BUF+$17                  ; sectors per FAT
1537 18 robfinch
        asl             r1,r1,#8
1538 15 robfinch
        orb             r1,r1,BYTE_SECTOR_BUF+$16
1539
        bne             loadBootFile7
1540
        lb              r1,BYTE_SECTOR_BUF+$27                  ; sectors per FAT, FAT32
1541 18 robfinch
        asl             r1,r1,#8
1542 15 robfinch
        orb             r1,r1,BYTE_SECTOR_BUF+$26
1543 18 robfinch
        asl             r1,r1,#8
1544 15 robfinch
        orb             r1,r1,BYTE_SECTOR_BUF+$25
1545 18 robfinch
        asl             r1,r1,#8
1546 15 robfinch
        orb             r1,r1,BYTE_SECTOR_BUF+$24
1547
loadBootFile7:
1548
        lb              r4,BYTE_SECTOR_BUF+$10                  ; number of FATs
1549
        mul             r3,r1,r4                                                ; offset
1550
        lb              r1,BYTE_SECTOR_BUF+$F                   ; r1 = # reserved sectors before FAT
1551 18 robfinch
        asl             r1,r1,#8
1552 15 robfinch
        orb             r1,r1,BYTE_SECTOR_BUF+$E
1553
        add             r3,r3,r1                                                ; r3 = root directory sector number
1554
        ld              r6,startSector
1555
        add             r5,r3,r6                                                ; r5 = root directory sector number
1556
        lb              r1,BYTE_SECTOR_BUF+$D                   ; sectors per cluster
1557
        add             r3,r1,r5                                                ; r3 = first cluster after first cluster of directory
1558
        bra             loadBootFile6
1559
 
1560
loadBootFile6:
1561
        ; For now we cheat and just go directly to sector 512.
1562
        bra             loadBootFileTmp
1563
 
1564
loadBootFileTmp:
1565
        ; We load the number of sectors per cluster, then load a single cluster of the file.
1566
        ; This is 16kib
1567
        ld              r5,r3                                                   ; r5 = start sector of data area
1568
        ld              r2,#PROG_LOAD_AREA                              ; where to place file in memory
1569
        lb              r3,BYTE_SECTOR_BUF+$D                   ; sectors per cluster
1570
loadBootFile1:
1571
        ld              r1,r5                                                   ; r1=sector to read
1572
        jsr             spi_read_sector
1573
        inc             r5                                              ; r5 = next sector
1574
        add             r2,r2,#512
1575
        dec             r3
1576
        bne             loadBootFile1
1577
        lda             (PROG_LOAD_AREA>>2)+$80 ; make sure it's bootable
1578
        cmp             #$544F4F42
1579
        bne             loadBootFile2
1580
        lda             #msgJumpingToBoot
1581
        jsr             DisplayStringB
1582
        lda             (PROG_LOAD_AREA>>2)+$81
1583
        jsr             (r1)
1584
        jmp             Monitor
1585
loadBootFile2:
1586
        lda             #msgNotBootable
1587
        jsr             DisplayStringB
1588
        ldx             #PROG_LOAD_AREA>>2
1589
        jsr             DisplayMemW
1590
        jsr             DisplayMemW
1591
        jsr             DisplayMemW
1592
        jsr             DisplayMemW
1593
        jmp             Monitor
1594
 
1595 11 robfinch
msgJumpingToBoot:
1596
        db      "Jumping to boot",0
1597
msgNotBootable:
1598
        db      "SD card not bootable.",0
1599
spi_init_ok_msg:
1600
        db "SD card initialized okay.",0
1601
spi_init_error_msg:
1602
        db      ": error occurred initializing the SD card.",0
1603
spi_boot_error_msg:
1604
        db      "SD card boot error",0
1605
spi_read_error_msg:
1606
        db      "SD card read error",0
1607
spi_write_error_msg:
1608
        db      "SD card write error",0
1609
 
1610 15 robfinch
;------------------------------------------------------------------------------
1611
; 100 Hz interrupt
1612
; - takes care of "flashing" the cursor
1613
;------------------------------------------------------------------------------
1614
;
1615
p100Hz:
1616 18 robfinch
        inc             IRQFlag                 ; support tiny basic's IRQ rout
1617 15 robfinch
        inc             TEXTSCR+83
1618
        stz             0xFFDCFFFC              ; clear interrupt
1619
        rti
1620
 
1621
;------------------------------------------------------------------------------
1622
; 1000 Hz interrupt
1623
; This IRQ must be fast.
1624
; Increments the millisecond counter, and switches to the next context
1625
;------------------------------------------------------------------------------
1626
;
1627
p1000Hz:
1628
        stz             0xFFDCFFFD                              ; acknowledge interrupt
1629
        inc             Milliseconds                    ; increment milliseconds count
1630
        rti
1631
 
1632
slp_rout:
1633
        rti
1634
brk_rout:
1635
        rti
1636 11 robfinch
nmirout
1637
        rti
1638 15 robfinch
message "1298"
1639
include "TinyBasic65002.asm"
1640 18 robfinch
message "1640"
1641 11 robfinch
        org $0FFFFFFF4          ; NMI vector
1642
        dw      nmirout
1643
 
1644
        org     $0FFFFFFF8              ; reset vector, native mode
1645
        dw      start
1646
 
1647
        end
1648
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.