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

Subversion Repositories rtf65002

[/] [rtf65002/] [trunk/] [software/] [asm/] [bootrom.asm] - Diff between revs 28 and 31

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 28 Rev 31
Line 57... Line 57...
TS_TIMEOUT      =1
TS_TIMEOUT      =1
TS_WAITMSG      =2
TS_WAITMSG      =2
TS_PREEMPT      =4
TS_PREEMPT      =4
TS_RUNNING      =8
TS_RUNNING      =8
TS_READY        =16
TS_READY        =16
TS_WAITFOCUS    = 32
TS_SLEEP        =32
 
 
 
MAX_TASKNO      = 255
 
 
 
DIRENT_NAME             =0x00   ; file name
 
DIRENT_EXT              =0x1C   ; file name extension
 
DIRENT_ATTR             =0x20   ; attributes
 
DIRENT_DATETIME =0x28
 
DIRENT_CLUSTER  =0x30   ; starting cluster of file
 
DIRENT_SIZE             =0x34   ; file size (6 bytes)
 
 
 
; One FCB is allocated and filled out for each file that is open.
 
;
 
nFCBs   = 128
 
FCB_DE_NAME             =0x00
 
FCB_DE_EXT              =0x1C
 
FCB_DE_ATTR             =0x20
 
FCB_DE_DATETIME =0x28
 
FCB_DE_CLUSTER  =0x30   ; starting cluster of file
 
FCB_DE_SIZE             =0x34   ; 6 byte file size
 
FCB_DIR_SECTOR  =0x40   ; LBA directory sector this is from
 
FCB_DIR_ENT             =0x44   ; offset in sector for dir entry
 
FCB_LDRV                =0x48   ; logical drive this is on
 
FCB_MODE                =0x49   ; 0 read, 1=modify
 
FCB_NUSERS              =0x4A   ; number of users of this file
 
FCB_FMOD                =0x4B   ; flag: this file was modified
 
FCB_RESV                =0x4C   ; padding out to 80 bytes
 
FCB_SIZE                =0x50
 
 
 
FUB_JOB         =0x00   ; User's job umber
 
FUB_iFCB        =0x02   ; FCB number for this file
 
FUB_CrntLFA     =0x04   ; six byte current logical file address
 
FUB_pBuf        =0x0C   ; pointer to buffer if in stream mode
 
FUB_sBuf        =0x10   ; size of buffer for stream file
 
FUB_LFABuf      =0x14   ; S-First LFA in Clstr Buffer
 
FUB_LFACluster  =0x18   ; LFA of cluster
 
FUB_Clstr       = 0x20          ; The last cluster read
 
FUB_fModified   = 0x24  ; data in buffer was modified
 
FUB_fStream             = 0x25  ; non-zero for stream mode
 
FUB_PAD         =0x26
 
FUB_SIZE        =0x30
 
 
 
; Boot sector info (62 byte structure) */
 
BSI_JMP         = 0x00
 
BSI_OEMName     = 0x03
 
BSI_bps         = 0x0B
 
BSI_SecPerCluster       = 0x0D
 
BSI_ResSectors  = 0x0E
 
BSI_FATS        = 0x10
 
BSI_RootDirEnts = 0x11
 
BSI_Sectors     = 0x13
 
BSI_Media       = 0x15
 
BSI_SecPerFAT   = 0x16
 
BSI_SecPerTrack = 0x18
 
BSI_Heads       = 0x1A
 
BSI_HiddenSecs  = 0x1C
 
BSI_HugeSecs    = 0x1E
 
 
 
BSI_DriveNum    = 0x24
 
BSI_Rsvd1               = 0x25
 
BSI_BootSig             = 0x26
 
BSI_VolID               = 0x27
 
BSI_VolLabel    = 0x2B
 
BSI_FileSysType = 0x36
 
 
 
 
 
MEM_CHK         =0
 
MEM_FLAG        =1
 
MEM_PREV        =2
 
MEM_NEXT        =3
 
 
; message queuing strategy
; message queuing strategy
MQS_UNLIMITED   =0      ; unlimited queue size
MQS_UNLIMITED   =0      ; unlimited queue size
MQS_NEWEST              =1      ; buffer queue size newest messages
MQS_NEWEST              =1      ; buffer queue size newest messages
MQS_OLDEST              =2      ; buffer queue size oldest messages
MQS_OLDEST              =2      ; buffer queue size oldest messages
 
 
 
LEDS            EQU             0xFFDC0600
TEXTSCR         EQU             0xFFD00000
TEXTSCR         EQU             0xFFD00000
COLORSCR        EQU             0xFFD10000
COLORSCR        EQU             0xFFD10000
TEXTREG         EQU             0xFFDA0000
TEXTREG         EQU             0xFFDA0000
TEXT_COLS       EQU             0x0
TEXT_COLS       EQU             0x0
TEXT_ROWS       EQU             0x1
TEXT_ROWS       EQU             0x1
TEXT_CURPOS     EQU             11
TEXT_CURPOS     EQU             11
KEYBD           EQU             0xFFDC0000
KEYBD           EQU             0xFFDC0000
KEYBDCLR        EQU             0xFFDC0001
KEYBDCLR        EQU             0xFFDC0001
PIC                     EQU             0xFFDC0FF0
PIC                     EQU             0xFFDC0FF0
PIC_IE          EQU             0xFFDC0FF1
PIC_IE          EQU             0xFFDC0FF1
 
PIC_ES          EQU             0xFFDC0FF4
 
PIC_RSTE        EQU             0xFFDC0FF5
TASK_SELECT     EQU             0xFFDD0008
TASK_SELECT     EQU             0xFFDD0008
RQ_SEMA         EQU             0xFFDB0000
RQ_SEMA         EQU             0xFFDB0000
TO_SEMA         EQU             0xFFDB0010
TO_SEMA         EQU             0xFFDB0010
SERIAL_SEMA     EQU             0xFFDB0020
SERIAL_SEMA     EQU             0xFFDB0020
KEYBD_SEMA      EQU             0xFFDB0030
KEYBD_SEMA      EQU             0xFFDB0030
IOF_LIST_SEMA   EQU     0xFFDB0040
IOF_LIST_SEMA   EQU     0xFFDB0040
MBX_SEMA        EQU             0xFFDB0050
MBX_SEMA        EQU             0xFFDB0050
 
MEM_SEMA        EQU             0xFFDB0060
 
 
SPIMASTER       EQU             0xFFDC0500
SPIMASTER       EQU             0xFFDC0500
SPI_MASTER_VERSION_REG  EQU     0x00
SPI_MASTER_VERSION_REG  EQU     0x00
SPI_MASTER_CONTROL_REG  EQU     0x01
SPI_MASTER_CONTROL_REG  EQU     0x01
SPI_TRANS_TYPE_REG      EQU             0x02
SPI_TRANS_TYPE_REG      EQU             0x02
Line 172... Line 243...
BIOS_SCREENS    EQU     0x05C00000      ; 0x05C00000 to 0x05DFFFFF
BIOS_SCREENS    EQU     0x05C00000      ; 0x05C00000 to 0x05DFFFFF
 
 
BYTE_SECTOR_BUF EQU     SECTOR_BUF<<2
BYTE_SECTOR_BUF EQU     SECTOR_BUF<<2
PROG_LOAD_AREA  EQU             0x4180000<<2
PROG_LOAD_AREA  EQU             0x4180000<<2
 
 
 
FCBs                    EQU             0x5F40000       ; room for 128 FCB's
 
 
 
FATOFFS                 EQU             0x5F50000       ; offset into FAT on card
 
FATBUF                  EQU             0x5F60000
 
DIRBUF                  EQU             0x5F70000
eth_rx_buffer   EQU             0x5F80000
eth_rx_buffer   EQU             0x5F80000
eth_tx_buffer   EQU             0x5F84000
eth_tx_buffer   EQU             0x5F84000
 
 
; Mailboxes, room for 2048
; Mailboxes, room for 2048
MBX_LINK        EQU             0x05F90000
MBX_LINK        EQU             0x05F90000
Line 215... Line 290...
TCB_mbq_prev    EQU             0x05FBD500      ; mailbox queue previous
TCB_mbq_prev    EQU             0x05FBD500      ; mailbox queue previous
TCB_iof_next    EQU             0x05FBD600
TCB_iof_next    EQU             0x05FBD600
TCB_iof_prev    EQU             0x05FBD700
TCB_iof_prev    EQU             0x05FBD700
TCB_SP8Save             EQU             0x05FBD800      ; TCB_SP8Save area
TCB_SP8Save             EQU             0x05FBD800      ; TCB_SP8Save area
TCB_SPSave              EQU             0x05FBD900      ; TCB_SPSave area
TCB_SPSave              EQU             0x05FBD900      ; TCB_SPSave area
 
TCB_ABS8Save    EQU             0x05FBDA00
 
 
KeybdHead       EQU             0x05FBEA00
KeybdHead       EQU             0x05FBEA00
KeybdTail       EQU             0x05FBEB00
KeybdTail       EQU             0x05FBEB00
KeybdEcho       EQU             0x05FBEC00
KeybdEcho       EQU             0x05FBEC00
KeybdBad        EQU             0x05FBED00
KeybdBad        EQU             0x05FBED00
KeybdAck        EQU             0x05FBEE00
KeybdAck        EQU             0x05FBEE00
KeybdLocks      EQU             0x05FBEF00
KeybdLocks      EQU             0x05FBEF00
KeybdBuffer     EQU             0x05FBF000      ; buffer is 16 chars
KeybdBuffer     EQU             0x05FBF000      ; buffer is 16 chars
 
 
 
HeapStart       EQU             0x04200000
 
HeapEnd         EQU             0x05AFFFFF
 
 
; Bitmap of tasks requesting the I/O focus
; Bitmap of tasks requesting the I/O focus
;
;
IOFocusTbl      EQU             0x05FBD000
IOFocusTbl      EQU             0x05FBD000
 
 
; EhBASIC vars:
; EhBASIC vars:
Line 291... Line 370...
Milliseconds    EQU             0xFC5
Milliseconds    EQU             0xFC5
IRQFlag         EQU             0xFC6
IRQFlag         EQU             0xFC6
RdyQueTick      EQU             0xFC7
RdyQueTick      EQU             0xFC7
eth_unique_id   EQU             0xFC8
eth_unique_id   EQU             0xFC8
LineColor       EQU             0xFC9
LineColor       EQU             0xFC9
 
QIndex          EQU             0xFCA
 
 
Uart_rxfifo             EQU             0x05FBC000
Uart_rxfifo             EQU             0x05FBC000
Uart_rxhead             EQU             0xFD0
Uart_rxhead             EQU             0xFD0
Uart_rxtail             EQU             0xFD1
Uart_rxtail             EQU             0xFD1
Uart_ms                 EQU             0xFD2
Uart_ms                 EQU             0xFD2
Line 315... Line 395...
        cpu             rtf65002
        cpu             rtf65002
        code
        code
 
 
message "jump table"
message "jump table"
        ; jump table of popular BIOS routines
        ; jump table of popular BIOS routines
        org             $FFFFC000
        org             $FFFF8000
        dw      DisplayChar
        dw      DisplayChar
        dw      KeybdCheckForKeyDirect
        dw      KeybdCheckForKeyDirect
        dw      KeybdGetCharDirect
        dw      KeybdGetCharDirect
        dw      KeybdGetChar
        dw      KeybdGetChar
        dw      KeybdCheckForKey
        dw      KeybdCheckForKey
Line 328... Line 408...
        dw      ClearScreen
        dw      ClearScreen
        dw      HomeCursor
        dw      HomeCursor
        dw      ExitTask
        dw      ExitTask
        dw      SetKeyboardEcho
        dw      SetKeyboardEcho
        dw      Sleep
        dw      Sleep
 
        dw      do_load
 
        dw      do_save
 
 
        org             $FFFFC200               ; leave room for 128 vectors
        org             $FFFF8400               ; leave room for 256 vectors
message "cold start point"
message "cold start point"
KeybdRST
KeybdRST
start
start
        sei                                             ; disable interrupts
        sei                                             ; disable interrupts
        cld                                             ; disable decimal mode
        cld                                             ; disable decimal mode
 
        lda             #1
 
        sta             LEDS
        ldx             #BIOS_STACKS+0x03FF     ; setup stack pointer top of memory
        ldx             #BIOS_STACKS+0x03FF     ; setup stack pointer top of memory
        txs
        txs
        trs             r0,dp                   ; set direct page register
        trs             r0,abs8                 ; set 8 bit mode absolute address offset
        trs             r0,dp8                  ; and 8 bit mode direct page
 
        trs             r0,abs8                 ; and 8 bit mode absolute address offset
        jsr             MemInit                 ; Initialize the heap
 
 
 
        lda             #2
 
        sta             LEDS
 
 
        ; setup interrupt vectors
        ; setup interrupt vectors
        ldx             #$05FB0001              ; interrupt vector table from $5FB0000 to $5FB01FF
        ldx             #$05FB0001              ; interrupt vector table from $5FB0000 to $5FB01FF
                                                        ; also sets nmoi policy (native mode on interrupt)
                                                        ; also sets nmoi policy (native mode on interrupt)
        trs             r2,vbr
        trs             r2,vbr
Line 360... Line 447...
        sta             448+3,x
        sta             448+3,x
        lda             #KeybdIRQ
        lda             #KeybdIRQ
        sta             448+15,x
        sta             448+15,x
        lda             #SerialIRQ
        lda             #SerialIRQ
        sta             448+8,x
        sta             448+8,x
 
        lda             #InvalidOpIRQ
 
        sta             495,x
        lda             #bus_err_rout
        lda             #bus_err_rout
        sta             508,x
        sta             508,x
        sta             509,x
        sta             509,x
 
 
        emm
        lda             #3
        cpu             W65C02
        sta             LEDS
        ldx             #$FF                    ; set 8 bit stack pointer
 
        txs
        ; stay in native mode in case emulation is not supported.
        nat
        ldx             #$1FF                   ; set 8 bit stack pointer
        cpu             rtf65002
        trs             r2,sp8
 
 
        ldx             #0
        ldx             #0
        stz             IrqBase                 ; support for EhBASIC's interrupt mechanism
        stz             IrqBase                 ; support for EhBASIC's interrupt mechanism
        stz             NmiBase
        stz             NmiBase
        ; Initialize the BIOS task
 
        lda             #TS_RUNNING|TS_READY
 
        sta             TCB_Status
 
        stz             TCB_Priority    ; set task#0 priority
 
        lda             #-1
 
        sta             TCB_NxtRdy              ; set task#0 next and previous fields
 
        sta             TCB_PrvRdy
 
        stz             TCB_Timeout
 
        stz             RunningTCB              ; the BIOS task is the running task
 
 
 
 
        lda             #-1
        sta             TimeoutList             ; no entries in timeout list
        sta             TimeoutList             ; no entries in timeout list
        sta             IOFocusNdx
        sta             HeadRdy0                ; task zero (the BIOS task) is always present
        stz             HeadRdy0                ; task zero (the BIOS task) is always present
 
        sta             HeadRdy1
        sta             HeadRdy1
        sta             HeadRdy2
        sta             HeadRdy2
        sta             HeadRdy3
        sta             HeadRdy3
        sta             HeadRdy4
        sta             HeadRdy4
        stz             TailRdy0
        sta             TailRdy0
        sta             TailRdy1
        sta             TailRdy1
        sta             TailRdy2
        sta             TailRdy2
        sta             TailRdy3
        sta             TailRdy3
        sta             TailRdy4
        sta             TailRdy4
 
 
 
 
        ; Initialize IO Focus List
        ; Initialize IO Focus List
        ;
        ;
        ldx             #0
        ldx             #1
st5:
st5:
        stz             IOFocusTbl,x
        stz             IOFocusTbl,x
        inx
        inx
        cpx             #8
        cpx             #8
        bne             st5
        bne             st5
 
 
        ldx             #0
        ldx             #1
        lda             #-1
        lda             #-1
st9:
st9:
        sta             TCB_iof_next,x
        sta             TCB_iof_next,x
        sta             TCB_iof_prev,x
        sta             TCB_iof_prev,x
        inx
        inx
Line 459... Line 540...
        inx
        inx
        cpx             #256
        cpx             #256
        bne             st2
        bne             st2
        lda             #-1
        lda             #-1
        sta             TCB_NxtTCB+255
        sta             TCB_NxtTCB+255
 
        lda             #4
 
        sta             LEDS
 
 
 
        ; Manually setup the BIOS task
 
        lda             #-1
 
        stz             RunningTCB              ; BIOS is task #0
 
        sta             TCB_NxtRdy              ; manually build the ready list
 
        sta             TCB_PrvRdy
 
        stz             HeadRdy0                ; insert at priority 4
 
        stz             TailRdy0
 
        stz             TCB_iof_next    ; manually build the IO focus list
 
        stz             TCB_iof_prev
 
        stz             IOFocusNdx              ; task #0 has the focus
 
        lda             #1
 
        sta             IOFocusTbl              ; set the task#0 request bit
 
        lda             #0
 
        sta             TCB_Priority
 
        lda             #TS_RUNNING|TS_READY
 
        sta             TCB_Status
 
        stz             TCB_CursorRow
 
        stz             TCB_CursorCol
 
 
        lda             #1
        lda             #1
        sta             MBX_SEMA
        sta             MBX_SEMA
        sta             IOF_LIST_SEMA
        sta             IOF_LIST_SEMA
        sta             RQ_SEMA                 ; set ready queue semaphore
        sta             RQ_SEMA                 ; set ready queue semaphore
        sta             TO_SEMA                 ; set timeout list semaphore
        sta             TO_SEMA                 ; set timeout list semaphore
        jsr             RequestIOFocus          ; Get the IO focus for the BIOS (must be after semaphore is initialized)
 
        lda             #$CE                    ; CE =blue on blue FB = grey on grey
        lda             #$CE                    ; CE =blue on blue FB = grey on grey
        sta             ScreenColor
        sta             ScreenColor
        sta             CharColor
        sta             CharColor
        sta             CursorFlash
        sta             CursorFlash
        jsr             ClearScreen
        jsr             ClearScreen
        jsr             ClearBmpScreen
        jsr             ClearBmpScreen
 
        lda             #4
 
        ldx             #0
 
        ldy             #IdleTask
 
        jsr             StartTask
 
        jsr             PICInit
 
        lda             CONFIGREC               ; do we have a serial port ?
 
        bit             #32
 
        beq             st7
 
        ; 19200 * 16
 
        ;-------------
 
        ; 25MHz / 2^32
 
        lda             #$03254E6E              ; constant for 19,200 baud at 25MHz
 
        jsr             SerialInit
 
st7:
 
        lda             #5
 
        sta             LEDS
        lda             CONFIGREC               ; do we have sprites ?
        lda             CONFIGREC               ; do we have sprites ?
        bit             #1
        bit             #1
        beq             st8
        beq             st8
        lda             #$3FFF                  ; turn on sprites
        lda             #$3FFF                  ; turn on sprites
        sta             SPRITEREGS+120
        sta             SPRITEREGS+120
        jsr             RandomizeSprram
        jsr             RandomizeSprram
st8:
st8:
        jsr             HomeCursor
        ; Enable interrupts.
        lda             #msgStart
        ; Keyboard initialization must take place after interrupts are
        jsr             DisplayStringB
        ; enabled.
 
        cli
 
        lda             #14
 
        sta             LEDS
        jsr             KeybdInit
        jsr             KeybdInit
        lda             #1
        lda             #1
        sta             KeybdEcho
        sta             KeybdEcho
        lda             CONFIGREC               ; do we have a serial port ?
        lda             #6
        bit             #32
        sta             LEDS
        beq             st7
 
        ; 19200 * 16
 
        ;-------------
 
        ; 25MHz / 2^32
 
        lda             #$03254E6E              ; constant for 19,200 baud at 25MHz
 
        jsr             SerialInit
 
st7:
 
        lda             #4
 
        ldx             #0
 
        ldy             #IdleTask
 
        jsr             StartTask
 
        jsr             PICInit
 
        cli                                             ; enable interrupts
 
        ; The following must be after interrupts are enabled.
        ; The following must be after interrupts are enabled.
 
        lda             #9
 
        sta             LEDS
 
        jsr             HomeCursor
 
        lda             #msgStart
 
        jsr             DisplayStringB
 
        jsr             ReportMemFree
 
        lda             #10
 
        sta             LEDS
 
 
        ; The AC97 setup uses the millisecond counter and the
        ; The AC97 setup uses the millisecond counter and the
        ; keyboard.
        ; keyboard.
        lda             CONFIGREC               ; do we have a sound generator ?
        lda             CONFIGREC               ; do we have a sound generator ?
        bit             #4
        bit             #4
        beq             st6
        beq             st6
        jsr             SetupAC97
        jsr             SetupAC97
        jsr             Beep
        lda             #2
 
        ldx             #0
 
        ldy             #Beep
 
        jsr             StartTask
st6:
st6:
 
        lda             #11
 
        sta             LEDS
        jmp             Monitor
        jmp             Monitor
st1
st1
        jsr             KeybdGetCharDirect
        jsr             KeybdGetCharDirect
        bra             st1
        bra             st1
        stp
        stp
Line 523... Line 646...
 
 
;----------------------------------------------------------
;----------------------------------------------------------
; Initialize programmable interrupt controller (PIC)
; Initialize programmable interrupt controller (PIC)
;  0 = nmi (parity error)
;  0 = nmi (parity error)
;  1 = keyboard reset
;  1 = keyboard reset
;  2 = 1000Hz pulse (context switcher)
;  2 = 1000Hz pulse
;  3 = 100Hz pulse (cursor flash)
;  3 = 100Hz pulse (cursor flash)
;  4 = ethmac
;  4 = ethmac
;  8 = uart
;  8 = uart
; 13 = raster interrupt
; 13 = raster interrupt
; 15 = keyboard char
; 15 = keyboard char
;----------------------------------------------------------
;----------------------------------------------------------
message "PICInit"
message "PICInit"
PICInit:
PICInit:
        ; enable: raster irq,
        ;
        lda             #$810F                  ; enable nmi,kbd_rst,and kbd_irq
        lda             #$000C                  ; clock pulses are edge sensitive
 
        sta             PIC_ES
 
        lda             #$000F                  ; enable nmi,kbd_rst
        ; A10F enable serial IRQ
        ; A10F enable serial IRQ
        sta             PIC_IE
        sta             PIC_IE
PICret:
PICret:
        rts
        rts
 
 
Line 645... Line 770...
        rts
        rts
 
 
msgIOFocusList:
msgIOFocusList:
        db      CR,LF,"Task Prv Nxt",CR,LF,0
        db      CR,LF,"Task Prv Nxt",CR,LF,0
 
 
 
RunningTCBErr:
 
        lda             #$FF
 
        sta             LEDS
 
        lda             #msgRunningTCB
 
        jsr             DisplayStringB
 
rtcberr1:
 
        jsr             KeybdGetChar
 
        cmp             #-1
 
        beq             rtcberr1
 
        jmp             start
 
 
 
msgRunningTCB:
 
        db      CR,LF,"RunningTCB is bad.",CR,LF,0
 
 
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
; IdleTask is a low priority task that is always running. It runs when there
; IdleTask is a low priority task that is always running. It runs when there
; is nothing else to run.
; is nothing else to run.
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
IdleTask:
IdleTask:
Line 676... Line 815...
        ld              r6,r1                           ; r6 = task priority
        ld              r6,r1                           ; r6 = task priority
        ld              r8,r2
        ld              r8,r2
 
 
        ; get a free TCB
        ; get a free TCB
        ;
        ;
 
        php
        sei
        sei
        lda             FreeTCB                         ; get free tcb list pointer
        lda             FreeTCB                         ; get free tcb list pointer
        bmi             stask1
        bmi             stask1
        tax
        tax
        lda             TCB_NxtTCB,x
        lda             TCB_NxtTCB,x
        sta             FreeTCB                         ; update the FreeTCB list pointer
        sta             FreeTCB                         ; update the FreeTCB list pointer
        cli
        plp
        txa
        txa
 
 
        ; setup the stack for the task
        ; setup the stack for the task
        ld              r7,r2
        ld              r7,r2
        asl             r2,r2,#10                       ; 1kW stack per task
        asl             r2,r2,#10                       ; 1kW stack per task
Line 695... Line 835...
        ldx             #$FF
        ldx             #$FF
        stx             TCB_SP8Save,r7
        stx             TCB_SP8Save,r7
        st              r6,TCB_Priority,r7
        st              r6,TCB_Priority,r7
        stz             TCB_Status,r7
        stz             TCB_Status,r7
        stz             TCB_Timeout,r7
        stz             TCB_Timeout,r7
 
        ; setup virtual video for the task
 
        stz             TCB_CursorRow,r7
 
        stz             TCB_CursorCol,r7
 
 
        ; setup the initial stack image for the task
        ; setup the initial stack image for the task
        ; Cause a return to the ExitTask routine when the task does a
        ; Cause a return to the ExitTask routine when the task does a
        ; final rts.
        ; final rts.
        ; fake an IRQ call by stacking the return address and processor
        ; fake an IRQ call by stacking the return address and processor
Line 728... Line 871...
        ply
        ply
        plx
        plx
        pla
        pla
        rts
        rts
stask1:
stask1:
        cli
        plp
        lda             #msgNoTCBs
        lda             #msgNoTCBs
        jsr             DisplayStringB
        jsr             DisplayStringB
        bra             stask2
        bra             stask2
 
 
msgNoTCBs:
msgNoTCBs:
Line 748... Line 891...
        sei
        sei
        ; release any aquired resources
        ; release any aquired resources
        ; - mailboxes
        ; - mailboxes
        ; - messages
        ; - messages
        lda             RunningTCB
        lda             RunningTCB
 
        bmi             RunningTCBErr
        jsr             RemoveTaskFromReadyList
        jsr             RemoveTaskFromReadyList
        stz             TCB_Status,r1                           ; set task status to TS_NONE
        stz             TCB_Status,r1                           ; set task status to TS_NONE
        jsr             ReleaseIOFocus
        jsr             ReleaseIOFocus
        ldx             FreeTCB                                         ; add the task control block to the free list
        ldx             FreeTCB                                         ; add the task control block to the free list
        stx             TCB_NxtTCB,r1
        stx             TCB_NxtTCB,r1
Line 762... Line 906...
; AddTaskToReadyList
; AddTaskToReadyList
; This subroutine is called from the timer ISR so it must be relatively
; This subroutine is called from the timer ISR so it must be relatively
; fast.
; fast.
; Parameters:
; Parameters:
; r1 = task number
; r1 = task number
;
; Returns:
 
;       none
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
message "AddTaskToReadyList"
message "AddTaskToReadyList"
AddTaskToReadyList:
AddTaskToReadyList:
 
        php
        pha
        pha
        phx
        phx
        phy
        phy
        php
 
        sei
        sei
        ldy             TCB_Priority,r1 ; make sure the priority value isn't screwy.
        ldy             TCB_Priority,r1
        cpy             #5
 
        bpl             arl3
 
arl2:
 
        ldx             TCB_Status,r1   ; set the task status to ready
        ldx             TCB_Status,r1   ; set the task status to ready
        bit             r2,#TS_READY    ; is the task already on the ready list ?
 
        bne             arl3
 
        or              r2,r2,#TS_READY
        or              r2,r2,#TS_READY
        stx             TCB_Status,r1
        stx             TCB_Status,r1
        ldx     TailRdy0,y              ; insert the task at the list tail
        ldx     TailRdy0,y              ; insert the task at the list tail
        bmi             arl1                    ; if no tail, then either the list is screwed up, or no tasks are on it.
        bmi             arl1                    ; if no tail, then either the list is screwed up, or no tasks are on it.
        sta             TCB_NxtRdy,x    ; add as next ready on tail
        sta             TCB_NxtRdy,x    ; add as next ready on tail
Line 794... Line 934...
        bpl             arl3
        bpl             arl3
        sta             HeadRdy0,y              ; Update the head of the ready list.
        sta             HeadRdy0,y              ; Update the head of the ready list.
        ldx             #-1                             ; There is no previous task at the head.
        ldx             #-1                             ; There is no previous task at the head.
        stx             TCB_PrvRdy,r1
        stx             TCB_PrvRdy,r1
arl3:
arl3:
        plp
 
        ply
        ply
        plx
        plx
        pla
        pla
 
        plp
        rts
        rts
        ; Here the tail of the ready list needed to be updated. Flag no prior task.
        ; Here the tail of the ready list needed to be updated. Flag no prior task.
arl1:
arl1:
        ldx             #-1
        ldx             #-1
        bra             arl4
        bra             arl4
Line 813... Line 953...
;
;
; r1 = task number
; r1 = task number
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
message "RemoveTaskFromReadyList"
message "RemoveTaskFromReadyList"
RemoveTaskFromReadyList:
RemoveTaskFromReadyList:
        cmp             #0
        php                                             ; save off interrupt mask state
        bmi             rfr9                    ; bad task number, must be >= 0
 
        cmp             #255                    ; and must be <= 255
 
        bpl             rfr9
 
        pha
        pha
        phx
        phx
        phy
        phy
        push    r4
        push    r4
        push    r5
        push    r5
 
 
        php                                             ; save off interrupt mask state
 
        sei
        sei
        ld              r4,TCB_Status,r1        ; check if the task is even on the ready list
 
        bit             r4,#TS_READY
 
        beq             rfr2                            ; not on ready list, then we're done
 
        ld              r4,TCB_NxtRdy,r1        ; Get previous and next fields.
        ld              r4,TCB_NxtRdy,r1        ; Get previous and next fields.
        ld              r5,TCB_PrvRdy,r1        ; if there is no previous task, then this is
        ld              r5,TCB_PrvRdy,r1        ; if there is no previous task, then this is
        bmi             rfr1                    ; the head of the list. Update.
        bmi             rfr1                    ; the head of the list. Update.
        st              r4,TCB_NxtRdy,r5
        st              r4,TCB_NxtRdy,r5
        cmp             r4,#0                   ; is there a next task to update ?
        cmp             r4,#0                   ; is there a next task to update ?
        bmi             rfr8
        bmi             rfr4
        st              r5,TCB_PrvRdy,r4
        st              r5,TCB_PrvRdy,r4
rfr4:
        bra             rfr4
        ld              r5,#-1
 
        st              r5,TCB_NxtRdy,r1
 
        st              r5,TCB_PrvRdy,r1
 
        bra             rfr8
 
 
 
        ; Update the head of the list
        ; Update the head of the list
rfr1:
rfr1:
        ldy             TCB_Priority,r1
        ldy             TCB_Priority,r1
        st              r4,HeadRdy0,y
        st              r4,HeadRdy0,y
        cmp             r4,#0                   ; did we empty the list ?
        cmp             r4,#0                   ; did we empty the list ?
        bmi             rfr3
        bmi             rfr3
        ld              r5,#-1                  ; flag no previous task for head of list
        ld              r5,#-1                  ; flag no previous task for head of list
        st              r5,TCB_PrvRdy,r4
        st              r5,TCB_PrvRdy,r4
        st              r5,TCB_NxtRdy,r1
rfr4:
        st              r5,TCB_PrvRdy,r1
        stz             TCB_NxtRdy,r1   ; Task is removed from ready list so the
rfr8:
        stz             TCB_PrvRdy,r1   ; contents of these fields shouldn't matter.
 
                                                        ; They are set to zero (the BIOS task) in case
 
                                                        ; there is a list problem.
 
 
        ldx             TCB_Status,r1   ; set the task status to no longer ready.
        ldx             TCB_Status,r1   ; set the task status to no longer ready.
        and             r2,r2,#~TS_READY
        and             r2,r2,#~TS_READY
        stx             TCB_Status,r1
        stx             TCB_Status,r1
rfr2:
rfr2:
        plp
 
        pop             r5
        pop             r5
        pop             r4
        pop             r4
        ply
        ply
        plx
        plx
        pla
        pla
 
        plp
rfr9:
rfr9:
        rts
        rts
rfr3:
rfr3:
        st              r4,TailRdy0,y   ;
        st              r4,TailRdy0,y   ;
        bra             rfr4
        bra             rfr4
Line 880... Line 1012...
;       r2 = timeout value
;       r2 = timeout value
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
message "AddToTimeoutList"
message "AddToTimeoutList"
AddToTimeoutList:
AddToTimeoutList:
        cmp             #0                                              ; quickly validate the task number
        cmp             #0                                              ; quickly validate the task number
        bmi             attl4                                   ; must be between 0 and 255
        bmi             RunningTCBErr                   ; must be between 0 and 255
        cmp             #255
        cmp             #MAX_TASKNO
        bpl             attl4
        bpl             RunningTCBErr
 
 
        phx
        phx
        push    r4
        push    r4
        push    r5
        push    r5
        php
        php
Line 961... Line 1093...
; r1 = task number
; r1 = task number
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
message "RemoveFromTimeoutList"
message "RemoveFromTimeoutList"
RemoveFromTimeoutList:
RemoveFromTimeoutList:
        cmp             #0                                              ; quickly validate the task number
        cmp             #0                                              ; quickly validate the task number
        bmi             rft4                                    ; must be between 0 and 255
        bmi             RunningTCBErr                   ; must be between 0 and 255
        cmp             #255
        cmp             #MAX_TASKNO
        bpl             rft4
        bpl             RunningTCBErr
 
 
        pha
        pha
        phx
        phx
        push    r4
        push    r4
        push    r5
        push    r5
Line 1014... Line 1146...
rft4:
rft4:
        rts
        rts
 
 
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
; Parameters:
; Parameters:
; r1 = time duration in centi-seconds
; r1 = time duration in centi-seconds (1/100 second).
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
Sleep:
Sleep:
        tax
        tax
        sei
        sei
        lda             RunningTCB
        lda             RunningTCB
Line 1051... Line 1183...
        ldx             MBX_LINK,r1             ; and update the head of the list
        ldx             MBX_LINK,r1             ; and update the head of the list
        stx             FreeMbx
        stx             FreeMbx
        dec             nMailbox                ; decrement number of available mailboxes
        dec             nMailbox                ; decrement number of available mailboxes
        tax
        tax
        ldy             RunningTCB                      ; set the mailbox owner
        ldy             RunningTCB                      ; set the mailbox owner
 
        bmi             RunningTCBErr
        lda             TCB_hJCB,y
        lda             TCB_hJCB,y
        sta             MBX_OWNER,x
        sta             MBX_OWNER,x
        lda             #-1                             ; initialize the head and tail of the queues
        lda             #-1                             ; initialize the head and tail of the queues
        sta             MBX_TQ_HEAD,x
        sta             MBX_TQ_HEAD,x
        sta             MBX_TQ_TAIL,x
        sta             MBX_TQ_TAIL,x
Line 1065... Line 1198...
        stz             MBX_MQ_MISSED,x
        stz             MBX_MQ_MISSED,x
        lda             #8                              ; set the max queue size
        lda             #8                              ; set the max queue size
        sta             MBX_MQ_SIZE,x   ; and
        sta             MBX_MQ_SIZE,x   ; and
        lda             #MQS_NEWEST             ; queueing strategy
        lda             #MQS_NEWEST             ; queueing strategy
        sta             MBX_MQ_STRATEGY,x
        sta             MBX_MQ_STRATEGY,x
 
ambx3:
        plp
        plp
        pop             r4
        pop             r4
        ply
        ply
        plx
        plx
        lda             #E_Ok
        lda             #E_Ok
Line 1318... Line 1452...
 
 
        ; Here there was no message available, remove the task from
        ; Here there was no message available, remove the task from
        ; the ready list, and optionally add it to the timeout list.
        ; the ready list, and optionally add it to the timeout list.
        ; Queue the task at the mailbox.
        ; Queue the task at the mailbox.
        lda             RunningTCB                              ; remove the task from the ready list
        lda             RunningTCB                              ; remove the task from the ready list
 
        bmi             wmsg8
        jsr             RemoveTaskFromReadyList
        jsr             RemoveTaskFromReadyList
        ld              r7,TCB_Status,r1                        ; set task status to waiting
        ld              r7,TCB_Status,r1                        ; set task status to waiting
        or              r7,r7,#TS_WAITMSG
        or              r7,r7,#TS_WAITMSG
        st              r7,TCB_Status,r1
        st              r7,TCB_Status,r1
        st              r6,TCB_hWaitMbx,r1                      ; set which mailbox is waited for
        st              r6,TCB_hWaitMbx,r1                      ; set which mailbox is waited for
Line 1498... Line 1633...
        rts
        rts
 
 
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
SetIOFocusBit:
SetIOFocusBit:
 
        and             r2,r2,#$FF
        and             r1,r2,#$1F              ; get bit index 0 to 31
        and             r1,r2,#$1F              ; get bit index 0 to 31
        ldy             #1
        ldy             #1
        asl             r3,r3,r1                ; shift bit to proper place
        asl             r3,r3,r1                ; shift bit to proper place
        lsr             r2,r2,#5                ; get word index /32 bits per word
        lsr             r2,r2,#5                ; get word index /32 bits per word
        lda             IOFocusTbl,x
        lda             IOFocusTbl,x
Line 1522... Line 1658...
        php
        php
        sei
        sei
        ldx             RunningTCB
        ldx             RunningTCB
        ldy             IOFocusNdx              ; Is the focus list empty ?
        ldy             IOFocusNdx              ; Is the focus list empty ?
        bmi             riof2
        bmi             riof2
        cpx             IOFocusNdx              ; does the task already have the focus ?
 
        beq             riof4
 
        lda             TCB_Status,x    ; update the task status to waiting for focus
 
        or              #TS_WAITFOCUS
 
        sta             TCB_Status,x
 
riof4:
riof4:
        lda             TCB_iof_next,x  ; is the task already in the IO focus list ?
        lda             TCB_iof_next,x  ; is the task already in the IO focus list ?
        bpl             riof3
        bpl             riof3
        lda             IOFocusNdx              ; Expand the list
        lda             IOFocusNdx              ; Expand the list
        ldy             TCB_iof_prev,r1
        ldy             TCB_iof_prev,r1
Line 1538... Line 1669...
        sta             TCB_iof_next,x
        sta             TCB_iof_next,x
        sty             TCB_iof_prev,x
        sty             TCB_iof_prev,x
        stx             TCB_iof_next,y
        stx             TCB_iof_next,y
riof3:
riof3:
        jsr             SetIOFocusBit
        jsr             SetIOFocusBit
 
 
        ; If the task doesn't have the I/O focus, then remove it
 
        ; from the ready list.
 
        ldx             RunningTCB
 
        cpx             IOFocusNdx
 
        beq             riof5
 
        txa
 
        jsr             RemoveTaskFromReadyList
 
riof5:
 
        plp
        plp
        ply
        ply
        plx
        plx
        pla
        pla
        rts
        rts
Line 1559... Line 1681...
        ; Update pointers to loop back to self.
        ; Update pointers to loop back to self.
riof2:
riof2:
        stx             IOFocusNdx
        stx             IOFocusNdx
        stx             TCB_iof_next,x
        stx             TCB_iof_next,x
        stx             TCB_iof_prev,x
        stx             TCB_iof_prev,x
        lda             TCB_Status,x
 
        and             #~TS_WAITFOCUS
 
        sta             TCB_Status,x
 
        bra             riof3
        bra             riof3
 
 
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
; Releasing the I/O focus causes the focus to switch if the running task
; Releasing the I/O focus causes the focus to switch if the running task
; had the I/O focus.
; had the I/O focus.
Line 1577... Line 1696...
        phy
        phy
        php
        php
        sei
        sei
        ldx             RunningTCB
        ldx             RunningTCB
        phx
        phx
        lda             TCB_Status,x    ; set the task status as no longer waiting for
 
        and             r1,r1,#~TS_WAITFOCUS    ; focus
 
        sta             TCB_Status,x
 
        ldy             #1
        ldy             #1
        and             r1,r2,#$1F              ; get bit index 0 to 31
        and             r1,r2,#$1F              ; get bit index 0 to 31
        asl             r3,r3,r1                ; shift bit to proper place
        asl             r3,r3,r1                ; shift bit to proper place
        eor             r3,r3,#-1               ; invert bit mask
        eor             r3,r3,#-1               ; invert bit mask
        lsr             r2,r2,#5                ; get word index /32 bits per word
        lsr             r2,r2,#5                ; get word index /32 bits per word
Line 1608... Line 1724...
        sta             IOFocusNdx
        sta             IOFocusNdx
rliof2:
rliof2:
        lda             #-1                             ; Update the next and prev fields to indicate
        lda             #-1                             ; Update the next and prev fields to indicate
        sta             TCB_iof_next,x  ; the task is no longer on the list.
        sta             TCB_iof_next,x  ; the task is no longer on the list.
        sta             TCB_iof_prev,x
        sta             TCB_iof_prev,x
 
rliof3:
        plp
        plp
        ply
        ply
        plx
        plx
        pla
        pla
        rts
        rts
 
 
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
 
; Get the location of the screen and screen attribute memory. The location
 
; depends on whether or not the task has the output focus.
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
GetScreenLocation:
GetScreenLocation:
        lda             RunningTCB
        lda             RunningTCB
        cmp             IOFocusNdx
        cmp             IOFocusNdx
        beq             gsl1
        beq             gsl1
 
        and             r1,r1,#$FF
        asl             r1,r1,#13                       ; 8192 words per screen
        asl             r1,r1,#13                       ; 8192 words per screen
        add             r1,r1,#BIOS_SCREENS
        add             r1,r1,#BIOS_SCREENS
        rts
        rts
gsl1:
gsl1:
        lda             #TEXTSCR
        lda             #TEXTSCR
Line 1631... Line 1751...
 
 
GetColorCodeLocation:
GetColorCodeLocation:
        lda             RunningTCB
        lda             RunningTCB
        cmp             IOFocusNdx
        cmp             IOFocusNdx
        beq             gccl1
        beq             gccl1
 
        and             r1,r1,#$FF
        asl             r1,r1,#13                       ; 8192 words per screen
        asl             r1,r1,#13                       ; 8192 words per screen
        add             r1,r1,#BIOS_SCREENS+4096
        add             r1,r1,#BIOS_SCREENS+4096
        rts
        rts
gccl1:
gccl1:
        lda             #TEXTSCR+$10000
        lda             #TEXTSCR+$10000
Line 1652... Line 1773...
        ldx             IOFocusNdx                      ; compute virtual screen location
        ldx             IOFocusNdx                      ; compute virtual screen location
        bmi             cvss3
        bmi             cvss3
        asl             r2,r2,#13                       ; 8192 words per screen
        asl             r2,r2,#13                       ; 8192 words per screen
        add             r2,r2,#BIOS_SCREENS     ; add in screens array base address
        add             r2,r2,#BIOS_SCREENS     ; add in screens array base address
        ldy             #TEXTSCR
        ldy             #TEXTSCR
;       mvn
        mvn
cvss1:
;cvss1:
        ld              r4,(x)
;       ld              r4,(x)
        st              r4,(y)
;       st              r4,(y)
        inx
;       inx
        iny
;       iny
        dea
;       dea
        bne             cvss1
;       bne             cvss1
        ; now copy the color codes
        ; now copy the color codes
        lda             #4095
        lda             #4095
        ldx             IOFocusNdx
        ldx             IOFocusNdx
        asl             r2,r2,#13
        asl             r2,r2,#13
        add             r2,r2,#BIOS_SCREENS+4096        ; virtual char color array
        add             r2,r2,#BIOS_SCREENS+4096        ; virtual char color array
        ldy             #TEXTSCR+$10000
        ldy             #TEXTSCR+$10000
cvss2:
        mvn
        ld              r4,(x)
;cvss2:
        st              r4,(y)
;       ld              r4,(x)
        inx
;       st              r4,(y)
        iny
;       inx
        dea
;       iny
        bne             cvss2
;       dea
 
;       bne             cvss2
cvss3:
cvss3:
        ; reset the cursor position in the text controller
        ; reset the cursor position in the text controller
        ldy             IOFocusNdx
        ldy             IOFocusNdx
        ldx             TCB_CursorRow,y
        ldx             TCB_CursorRow,y
        lda             TEXTREG+TEXT_COLS
        lda             TEXTREG+TEXT_COLS
Line 1698... Line 1820...
        ldx             #TEXTSCR
        ldx             #TEXTSCR
        ldy             IOFocusNdx
        ldy             IOFocusNdx
        bmi             csvs3
        bmi             csvs3
        asl             r3,r3,#13
        asl             r3,r3,#13
        add             r3,r3,#BIOS_SCREENS
        add             r3,r3,#BIOS_SCREENS
csvs1:
        mvn
        ld              r4,(x)
;csvs1:
        st              r4,(y)
;       ld              r4,(x)
        inx
;       st              r4,(y)
        iny
;       inx
        dea
;       iny
        bne             csvs1
;       dea
 
;       bne             csvs1
        lda             #4095
        lda             #4095
        ldx             #TEXTSCR+$10000
        ldx             #TEXTSCR+$10000
        ldy             IOFocusNdx
        ldy             IOFocusNdx
        asl             r3,r3,#13
        asl             r3,r3,#13
        add             r3,r3,#BIOS_SCREENS+4096
        add             r3,r3,#BIOS_SCREENS+4096
csvs2:
        mvn
        ld              r4,(x)
;csvs2:
        st              r4,(y)
;       ld              r4,(x)
        inx
;       st              r4,(y)
        iny
;       inx
        dea
;       iny
        bne             csvs2
;       dea
 
;       bne             csvs2
csvs3:
csvs3:
        pop             r4
        pop             r4
        ply
        ply
        plx
        plx
        pla
        pla
Line 1874... Line 1998...
; Set the cursor location to the top left of the screen.
; Set the cursor location to the top left of the screen.
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
HomeCursor:
HomeCursor:
        phx
        phx
        ldx             RunningTCB
        ldx             RunningTCB
 
        and             r2,r2,#$FF
        stz             TCB_CursorRow,x
        stz             TCB_CursorRow,x
        stz             TCB_CursorCol,x
        stz             TCB_CursorCol,x
 
        cpx             IOFocusNdx
 
        bne             hc1
 
        stz             TEXTREG+TEXT_CURPOS
 
hc1:
        plx
        plx
        rts
        rts
 
 
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
 
; Update the cursor position in the text controller based on the
 
;  CursorRow,CursorCol.
 
;------------------------------------------------------------------------------
 
;
 
UpdateCursorPos:
 
        pha
 
        phx
 
        push    r4
 
        ld              r4,RunningTCB
 
        and             r4,r4,#$FF
 
        cmp             r4,IOFocusNdx                   ; update cursor position in text controller
 
        bne             ucp1                                    ; only for the task with the output focus
 
        lda             TCB_CursorRow,r4
 
        and             #$3F                                    ; limit of 63 rows
 
        ldx             TEXTREG+TEXT_COLS
 
        mul             r2,r2,r1
 
        lda             TCB_CursorCol,r4
 
        and             #$7F                                    ; limit of 127 cols
 
        add             r2,r2,r1
 
        stx             TEXTREG+TEXT_CURPOS
 
ucp1:
 
        pop             r4
 
        plx
 
        pla
 
        rts
 
 
 
;------------------------------------------------------------------------------
; Calculate screen memory location from CursorRow,CursorCol.
; Calculate screen memory location from CursorRow,CursorCol.
; Also refreshes the cursor location.
; Also refreshes the cursor location.
; Returns:
; Returns:
; r1 = screen location
; r1 = screen location
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
;
;
CalcScreenLoc:
CalcScreenLoc:
        phx
        phx
        push    r4
        push    r4
        ld              r4,RunningTCB
        ld              r4,RunningTCB
 
        and             r4,r4,#$FF
        lda             TCB_CursorRow,r4
        lda             TCB_CursorRow,r4
 
        and             #$3F                                    ; limit to 63 rows
        ldx             TEXTREG+TEXT_COLS
        ldx             TEXTREG+TEXT_COLS
        mul             r2,r2,r1
        mul             r2,r2,r1
        add             r2,r2,TCB_CursorCol,r4
        ld              r1,TCB_CursorCol,r4
 
        and             #$7F                                    ; limit to 127 cols
 
        add             r2,r2,r1
        cmp             r4,IOFocusNdx                   ; update cursor position in text controller
        cmp             r4,IOFocusNdx                   ; update cursor position in text controller
        bne             csl1                                    ; only for the task with the output focus
        bne             csl1                                    ; only for the task with the output focus
        stx             TEXTREG+TEXT_CURPOS
        stx             TEXTREG+TEXT_CURPOS
csl1:
csl1:
        jsr             GetScreenLocation
        jsr             GetScreenLocation
        add             r1,r2,r1
        add             r1,r2,r1
 
        pop             r4
 
        plx
 
        rts
csl2:
csl2:
 
        lda             #TEXTSCR
        pop             r4
        pop             r4
        plx
        plx
        rts
        rts
 
 
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
Line 1916... Line 2080...
;
;
message "DisplayChar"
message "DisplayChar"
DisplayChar:
DisplayChar:
        push    r4
        push    r4
        ld              r4,RunningTCB
        ld              r4,RunningTCB
 
        and             r4,r4,#$FF
        and             #$FF                            ; mask off any higher order bits (called from eight bit mode).
        and             #$FF                            ; mask off any higher order bits (called from eight bit mode).
        cmp             #'\r'                           ; carriage return ?
        cmp             #'\r'                           ; carriage return ?
        bne             dccr
        bne             dccr
        stz             TCB_CursorCol,r4        ; just set cursor column to zero on a CR
        stz             TCB_CursorCol,r4        ; just set cursor column to zero on a CR
        jsr             CalcScreenLoc
        jsr             UpdateCursorPos
 
dcx14:
        pop             r4
        pop             r4
        rts
        rts
dccr:
dccr:
        cmp             #$91                            ; cursor right ?
        cmp             #$91                            ; cursor right ?
        bne             dcx6
        bne             dcx6
Line 1933... Line 2099...
        cmp             #83
        cmp             #83
        bcs             dcx7
        bcs             dcx7
        ina
        ina
        sta             TCB_CursorCol,r4
        sta             TCB_CursorCol,r4
dcx7:
dcx7:
        jsr             CalcScreenLoc
        jsr             UpdateCursorPos
        pla
        pla
        pop             r4
        pop             r4
        rts
        rts
dcx6:
dcx6:
        cmp             #$90                            ; cursor up ?
        cmp             #$90                            ; cursor up ?
Line 2043... Line 2209...
IncCursorPos:
IncCursorPos:
        pha
        pha
        phx
        phx
        push    r4
        push    r4
        ld              r4,RunningTCB
        ld              r4,RunningTCB
 
        and             r4,r4,#$FF
        lda             TCB_CursorCol,r4
        lda             TCB_CursorCol,r4
        ina
        ina
        sta             TCB_CursorCol,r4
        sta             TCB_CursorCol,r4
        ldx             TEXTREG+TEXT_COLS
        ldx             TEXTREG+TEXT_COLS
        cmp             r1,r2
        cmp             r1,r2
Line 2056... Line 2223...
IncCursorRow:
IncCursorRow:
        pha
        pha
        phx
        phx
        push    r4
        push    r4
        ld              r4,RunningTCB
        ld              r4,RunningTCB
 
        and             r4,r4,#$FF
icr1:
icr1:
        lda             TCB_CursorRow,r4
        lda             TCB_CursorRow,r4
        ina
        ina
        sta             TCB_CursorRow,r4
        sta             TCB_CursorRow,r4
        ldx             TEXTREG+TEXT_ROWS
        ldx             TEXTREG+TEXT_ROWS
Line 2068... Line 2236...
        beq             icc1
        beq             icc1
        dex                                                     ; backup the cursor row, we are scrolling up
        dex                                                     ; backup the cursor row, we are scrolling up
        stx             TCB_CursorRow,r4
        stx             TCB_CursorRow,r4
        jsr             ScrollUp
        jsr             ScrollUp
icc1:
icc1:
        jsr             CalcScreenLoc
        jsr             UpdateCursorPos
 
icc2:
        pop             r4
        pop             r4
        plx
        plx
        pla
        pla
        rts
        rts
 
 
Line 2095... Line 2264...
dsretB:
dsretB:
        plx
        plx
        pla
        pla
        rts
        rts
 
 
 
DisplayStringQ:
 
        pha
 
        phx
 
        tax                                             ; r2 = pointer to string
 
        lda             #TEXTSCR
 
        sta             QIndex
 
dspj1Q:
 
        lb              r1,0,x                  ; move string char into acc
 
        inx                                             ; increment pointer
 
        cmp             #0                              ; is it end of string ?
 
        beq             dsretQ
 
        jsr             DisplayCharQ    ; display character
 
        bra             dspj1Q
 
dsretQ:
 
        plx
 
        pla
 
        rts
 
 
 
DisplayCharQ:
 
        pha
 
        phx
 
        jsr             AsciiToScreen
 
        ldx             #0
 
        sta             (QIndex,x)
 
        lda             QIndex
 
        ina
 
        sta             QIndex
 
;       inc             QIndex
 
        plx
 
        pla
 
        rts
 
 
 
 
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
; Display a string on the screen.
; Display a string on the screen.
; The characters are packed 1 per word
; The characters are packed 1 per word
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
;
;
Line 2139... Line 2341...
;
;
message "KeybdInit"
message "KeybdInit"
KeybdInit:
KeybdInit:
        lda             #1                      ; setup semaphore
        lda             #1                      ; setup semaphore
        sta             KEYBD_SEMA
        sta             KEYBD_SEMA
 
        lda             #32
 
        sta             LEDS
        ldx             #0
        ldx             #0
kbdi1:
kbdi1:
        stz             KeybdHead,x             ; setup keyboard buffer
        stz             KeybdHead,x             ; setup keyboard buffer
        stz             KeybdTail,x
        stz             KeybdTail,x
        lda             #1                      ; turn on keyboard echo
        lda             #1                      ; turn on keyboard echo
        sta             KeybdEcho,x
        sta             KeybdEcho,x
        stz             KeybdBad,x
        stz             KeybdBad,x
        inx
        inx
        cpx             #256
        cpx             #MAX_TASKNO+1
        bne             kbdi1
        bne             kbdi1
 
 
 
        lda             PIC_IE
 
        or              r1,r1,#$8000            ; enable kbd_irq
 
        sta             PIC_IE
 
 
 
        lda             #33
 
        sta             LEDS
        lda             #$ff            ; issue keyboard reset
        lda             #$ff            ; issue keyboard reset
        jsr             SendByteToKeybd
        jsr             SendByteToKeybd
 
        lda             #38
 
        sta             LEDS
        lda             #1000000                ; delay a bit
        lda             #1000000                ; delay a bit
kbdi5:
kbdi5:
        dea
        dea
 
        sta             LEDS
        bne             kbdi5
        bne             kbdi5
 
        lda             #34
 
        sta             LEDS
        lda             #0xf0           ; send scan code select
        lda             #0xf0           ; send scan code select
        jsr             SendByteToKeybd
        jsr             SendByteToKeybd
 
        lda             #35
 
        sta             LEDS
        ldx             #0xFA
        ldx             #0xFA
        jsr             WaitForKeybdAck
        jsr             WaitForKeybdAck
        cmp             #$FA
        cmp             #$FA
        bne             kbdi2
        bne             kbdi2
 
        lda             #36
 
        sta             LEDS
        lda             #2                      ; select scan code set#2
        lda             #2                      ; select scan code set#2
        jsr             SendByteToKeybd
        jsr             SendByteToKeybd
 
        lda             #39
 
        sta             LEDS
kbdi2:
kbdi2:
        rts
        rts
 
 
msgBadKeybd:
msgBadKeybd:
        db              "Keyboard not responding.",0
        db              "Keyboard not responding.",0
 
 
SendByteToKeybd:
SendByteToKeybd:
        phx
        phx
        ldx             RunningTCB
        ldx             RunningTCB
        sta             KEYBD
        sta             KEYBD
 
        lda             #40
 
        sta             LEDS
        tsr             TICK,r3
        tsr             TICK,r3
kbdi4:                                          ; wait for transmit complete
kbdi4:                                          ; wait for transmit complete
        tsr             TICK,r4
        tsr             TICK,r4
        sub             r4,r4,r3
        sub             r4,r4,r3
        cmp             r4,#1000000
        cmp             r4,#1000000
        bcs             kbdbad
        bcs             kbdbad
 
        lda             #41
 
        sta             LEDS
        lda             KEYBD+3
        lda             KEYBD+3
        bit             #64
        bit             #64
        beq             kbdi4
        beq             kbdi4
        bra             sbtk1
        bra             sbtk1
kbdbad:
kbdbad:
 
        lda             #42
 
        sta             LEDS
        lda             KeybdBad,x
        lda             KeybdBad,x
        bne             sbtk1
        bne             sbtk1
        lda             #1
        lda             #1
        sta             KeybdBad,x
        sta             KeybdBad,x
 
        lda             #43
 
        sta             LEDS
        lda             #msgBadKeybd
        lda             #msgBadKeybd
        jsr             DisplayStringCRLFB
        jsr             DisplayStringCRLFB
sbtk1:
sbtk1:
 
        lda             #44
 
        sta             LEDS
        plx
        plx
        rts
        rts
 
 
; Wait for keyboard to respond with an ACK (FA)
; Wait for keyboard to respond with an ACK (FA)
;
;
WaitForKeybdAck:
WaitForKeybdAck:
 
        lda             #64
 
        sta             LEDS
        tsr             TICK,r3
        tsr             TICK,r3
wkbdack1:
wkbdack1:
        tsr             TICK,r4
        tsr             TICK,r4
        sub             r4,r4,r3
        sub             r4,r4,r3
        cmp             r4,#1000000
        cmp             r4,#1000000
        bcs             wkbdbad
        bcs             wkbdbad
 
        lda             #65
 
        sta             LEDS
        lda             KEYBD
        lda             KEYBD
        bit             #$8000
        bit             #$8000
        beq             wkbdack1
        beq             wkbdack1
;       lda             KEYBD+8
;       lda             KEYBD+8
        and             #$ff
        and             #$ff
Line 2317... Line 2552...
        plx
        plx
        rts
        rts
 
 
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
; SwitchIOFocus
; SwitchIOFocus
 
;
; Switches the IO focus to the next task requesting the I/O focus. This
; Switches the IO focus to the next task requesting the I/O focus. This
; routine may be called when a task releases the I/O focus as well as when
; routine may be called when a task releases the I/O focus as well as when
; the user presses ALT-TAB on the keyboard.
; the user presses ALT-TAB on the keyboard.
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
;
;
SwitchIOFocus:
SwitchIOFocus:
        pha
        pha
        phx
 
        phy
        phy
 
 
        ; First check if it's even possible to switch the focus to another
        ; First check if it's even possible to switch the focus to another
        ; task. The I/O focus list could be empty or there may be only a
        ; task. The I/O focus list could be empty or there may be only a
        ; single task in the list. In either case it's not possible to
        ; single task in the list. In either case it's not possible to
Line 2337... Line 2572...
        bmi             siof3                   ; Is the list empty ?
        bmi             siof3                   ; Is the list empty ?
        lda             TCB_iof_next,y  ; Get the next task on the list.
        lda             TCB_iof_next,y  ; Get the next task on the list.
        cmp             r1,r3                   ; Will the list head change ?
        cmp             r1,r3                   ; Will the list head change ?
        beq             siof3                   ; If not then no switch will occur
        beq             siof3                   ; If not then no switch will occur
 
 
        ; Check if outgoing task still wants the focus. If the task doesn't
 
        ; want the focus there's no reason to set the status as waiting or
 
        ; remove it from the ready list.
 
        tya
 
        jsr             GetIOFocusBit   ; don't set the 'waiting for focus' status
 
        cmp             #0                              ; if not.
 
        beq             siof1
 
        lda             TCB_Status,y    ; Set outgoing task status to 'waiting for focus'
 
        or              r1,r1,#TS_WAITFOCUS
 
        sta             TCB_Status,y
 
        tya                                             ; The outgoing task will now be waiting for the focus
 
        jsr             RemoveTaskFromReadyList
 
siof1:
 
        ; Copy the current task's screen to it's virtual screen buffer.
        ; Copy the current task's screen to it's virtual screen buffer.
        jsr             CopyScreenToVirtualScreen
        jsr             CopyScreenToVirtualScreen
 
 
        lda             TCB_iof_next,y  ; Get the next task on the list.
 
        sta             IOFocusNdx              ; Make task the new head of list.
        sta             IOFocusNdx              ; Make task the new head of list.
        tay
 
        lda             TCB_Status,y
 
        beq             siof2                   ; Check: task is exiting (status==0) so don't add it back
 
        and             #~TS_WAITFOCUS  ; to the ready list.
 
        sta             TCB_Status,y
 
        tya
 
        jsr             AddTaskToReadyList
 
siof2:
 
        ; Copy the virtual screen of the task recieving the I/O focus to the
        ; Copy the virtual screen of the task recieving the I/O focus to the
        ; text screen.
        ; text screen.
        jsr             CopyVirtualScreenToScreen
        jsr             CopyVirtualScreenToScreen
siof3:
siof3:
        ply
        ply
        plx
 
        pla
        pla
        rts
        rts
 
 
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
; Get character from keyboard buffer
; Get character from keyboard buffer
Line 2385... Line 2598...
        phx
        phx
        push    r4
        push    r4
        php
        php
        sei
        sei
        ld              r4,RunningTCB
        ld              r4,RunningTCB
 
        bmi             RunningTCBErr
 
        cmp             #MAX_TASKNO
 
        bpl             RunningTCBErr
        ldx             KeybdTail,r4    ; if keybdTail==keybdHead then there are no
        ldx             KeybdTail,r4    ; if keybdTail==keybdHead then there are no
        lda             KeybdHead,r4    ; characters in the keyboard buffer
        lda             KeybdHead,r4    ; characters in the keyboard buffer
        cmp             r1,r2
        cmp             r1,r2
        beq             nochar
        beq             nochar
        asl             r4,r4,#4                        ; * 16
        asl             r4,r4,#4                        ; * 16
Line 2934... Line 3150...
        bra             Prompt3
        bra             Prompt3
 
 
; Process the screen line that the CR was keyed on
; Process the screen line that the CR was keyed on
;
;
Prompt1:
Prompt1:
 
        lda             #80
 
        sta             LEDS
        ldx             RunningTCB
        ldx             RunningTCB
 
        bmi             RunningTCBErr
 
        cpx             #MAX_TASKNO
 
        bpl             RunningTCBErr
 
        lda             #81
 
        sta             LEDS
        stz             TCB_CursorCol,x ; go back to the start of the line
        stz             TCB_CursorCol,x ; go back to the start of the line
        jsr             CalcScreenLoc   ; r1 = screen memory location
        jsr             CalcScreenLoc   ; r1 = screen memory location
        tay
        tay
 
        lda             #82
 
        sta             LEDS
        lda             (y)
        lda             (y)
        iny
        iny
        jsr             ScreenToAscii
        jsr             ScreenToAscii
        cmp             #'$'
        cmp             #'$'
        bne             Prompt2                 ; skip over '$' prompt character
        bne             Prompt2                 ; skip over '$' prompt character
 
        lda             #83
 
        sta             LEDS
        lda             (y)
        lda             (y)
        iny
        iny
        jsr             ScreenToAscii
        jsr             ScreenToAscii
 
 
; Dispatch based on command character
; Dispatch based on command character
Line 2959... Line 3186...
        lda             (y)
        lda             (y)
        iny
        iny
        jsr             ScreenToAscii
        jsr             ScreenToAscii
        cmp             #'R'
        cmp             #'R'
        beq             DumpReg
        beq             DumpReg
 
        cmp             #'I'
 
        beq             DoDir
        dey
        dey
        bra             DumpMem
        bra             DumpMem
Prompt8:
Prompt8:
        cmp             #'F'
        cmp             #'F'
        bne             Prompt7
        bne             Prompt7
Line 2972... Line 3201...
        cmp             #'L'
        cmp             #'L'
        bne             Prompt8a
        bne             Prompt8a
        jsr             DumpIOFocusList
        jsr             DumpIOFocusList
        jmp             Monitor
        jmp             Monitor
Prompt8a:
Prompt8a:
 
        cmp             #'M'
 
        beq             DoFmt
        dey
        dey
        bra             FillMem
        bra             FillMem
Prompt7:
Prompt7:
        cmp             #'B'                    ; $B - start tiny basic
        cmp             #'B'                    ; $B - start tiny basic
        bne             Prompt4
        bne             Prompt4
Line 3024... Line 3255...
;       jmp             RandomLinesCall
;       jmp             RandomLinesCall
Prompt12:
Prompt12:
Prompt13:
Prompt13:
        cmp             #'P'
        cmp             #'P'
        bne             Prompt14
        bne             Prompt14
        jmp             Piano
        lda             #2
 
        ldx             #0
 
        ldy             #Piano
 
        jsr             StartTask
 
        jmp             Monitor
Prompt14:
Prompt14:
        cmp             #'T'
        cmp             #'T'
        bne             Prompt15
        bne             Prompt15
        jsr             DumpTaskList
        jsr             DumpTaskList
        jmp             Monitor
        jmp             Monitor
Line 3075... Line 3310...
message "Prompt16"
message "Prompt16"
RandomLinesCall:
RandomLinesCall:
;       jsr             RandomLines
;       jsr             RandomLines
        jmp             Monitor
        jmp             Monitor
 
 
 
DoDir:
 
        jsr             do_dir
 
        jmp             Monitor
 
DoFmt:
 
        jsr             do_fmt
 
        jmp             Monitor
 
 
TestCLS:
TestCLS:
        lda             (y)
        lda             (y)
        iny
        iny
        jsr             ScreenToAscii
        jsr             ScreenToAscii
        cmp             #'L'
        cmp             #'L'
Line 3520... Line 3762...
        ; sustain level C
        ; sustain level C
        lda             #0xCA12
        lda             #0xCA12
        sta             PSGADSR0
        sta             PSGADSR0
        lda             #0x1104                 ; gate, output enable, triangle waveform
        lda             #0x1104                 ; gate, output enable, triangle waveform
        sta             PSGCTRL0
        sta             PSGCTRL0
        lda             #2500000                ; delay about 1s
        lda             #100                    ; delay about 1s
beep1:
        jsr             Sleep
        dea
 
        bne             beep1
 
        lda             #0x0104                 ; gate off, output enable, triangle waveform
        lda             #0x0104                 ; gate off, output enable, triangle waveform
        sta             PSGCTRL0
        sta             PSGCTRL0
        lda             #2500000                ; delay about 1s
        lda             #100                    ; delay about 1s
beep2:
        jsr             Sleep
        dea
 
        bne             beep2
 
        lda             #0x0000                 ; gate off, output enable off, no waveform
        lda             #0x0000                 ; gate off, output enable off, no waveform
        sta             PSGCTRL0
        sta             PSGCTRL0
        pla
        pla
        rts
        rts
 
 
;--------------------------------------------------------------------------
;--------------------------------------------------------------------------
;--------------------------------------------------------------------------
;--------------------------------------------------------------------------
;
;
Piano:
Piano:
 
        jsr             RequestIOFocus
        lda             #15                             ; master volume to max
        lda             #15                             ; master volume to max
        sta             PSG+128
        sta             PSG+128
playnt:
playnt:
        jsr             KeybdGetChar
        jsr             KeybdGetChar
        cmp             #CTRLC
        cmp             #CTRLC
        beq             Monitor
        beq             PianoX
        cmp             #'a'
        cmp             #'a'
        beq             playnt1a
        beq             playnt1a
        cmp             #'b'
        cmp             #'b'
        beq             playnt1b
        beq             playnt1b
        cmp             #'c'
        cmp             #'c'
Line 3560... Line 3799...
        cmp             #'f'
        cmp             #'f'
        beq             playnt1f
        beq             playnt1f
        cmp             #'g'
        cmp             #'g'
        beq             playnt1g
        beq             playnt1g
        bra             playnt
        bra             playnt
 
PianoX:
 
        jsr             ReleaseIOFocus
 
        rts
 
 
playnt1a:
playnt1a:
        lda             #7217
        lda             #7217
        jsr             Tone
        jsr             Tone
        bra             playnt
        bra             playnt
Line 3637... Line 3879...
        beq             spi_init1
        beq             spi_init1
        lda             SPIMASTER+SPI_TRANS_ERROR_REG
        lda             SPIMASTER+SPI_TRANS_ERROR_REG
        and             #3
        and             #3
        cmp             #SPI_INIT_NO_ERROR
        cmp             #SPI_INIT_NO_ERROR
        bne             spi_error
        bne             spi_error
        lda             #spi_init_ok_msg
;       lda             #spi_init_ok_msg
        jsr             DisplayStringB
;       jsr             DisplayStringB
        lda             #0
        lda             #0
        rts
        rts
spi_error
spi_error
        jsr             DisplayByte
        jsr             DisplayByte
        lda             #spi_init_error_msg
        lda             #spi_init_error_msg
Line 3797... Line 4039...
spi_write_ret:
spi_write_ret:
        ply
        ply
        plx
        plx
        rts
        rts
 
 
 
; SPI read multiple sector
 
;
 
; r1= sector number to read
 
; r2= address to write data
 
; r3= number of sectors to read
 
;
 
; Returns:
 
; r1 = 0 if successful
 
;
 
spi_read_multiple:
 
        push    r4
 
        ld              r4,#0
 
spi_rm1:
 
        pha
 
        jsr             spi_read_sector
 
        add             r4,r4,r1
 
        add             r2,r2,#512
 
        pla
 
        ina
 
        dey
 
        bne             spi_rm1
 
        ld              r1,r4
 
        pop             r4
 
        rts
 
 
; SPI write multiple sector
; SPI write multiple sector
;
;
; r1= sector number to write
; r1= sector number to write
; r2= address to get data from
; r2= address to get data from
; r3= number of sectors to write
; r3= number of sectors to write
Line 3808... Line 4075...
; Returns:
; Returns:
; r1 = 0 if successful
; r1 = 0 if successful
;
;
spi_write_multiple:
spi_write_multiple:
        push    r4
        push    r4
 
        ld              r4,#0
spi_wm1:
spi_wm1:
        pha
        pha
        jsr             spi_write_sector
        jsr             spi_write_sector
        add             r4,r4,r1                ; accumulate an error count
        add             r4,r4,r1                ; accumulate an error count
        add             r2,r2,#512              ; 512 bytes per sector
        add             r2,r2,#512              ; 512 bytes per sector
Line 3907... Line 4175...
 
 
; Load the root directory from disk
; Load the root directory from disk
; r2 = where to place root directory in memory
; r2 = where to place root directory in memory
;
;
loadBootFile:
loadBootFile:
        lb              r1,BYTE_SECTOR_BUF+$17                  ; sectors per FAT
        lb              r1,BYTE_SECTOR_BUF+BSI_SecPerFAT+1                      ; sectors per FAT
        asl             r1,r1,#8
        asl             r1,r1,#8
        orb             r1,r1,BYTE_SECTOR_BUF+$16
        orb             r1,r1,BYTE_SECTOR_BUF+BSI_SecPerFAT
        bne             loadBootFile7
        bne             loadBootFile7
        lb              r1,BYTE_SECTOR_BUF+$27                  ; sectors per FAT, FAT32
        lb              r1,BYTE_SECTOR_BUF+$27                  ; sectors per FAT, FAT32
        asl             r1,r1,#8
        asl             r1,r1,#8
        orb             r1,r1,BYTE_SECTOR_BUF+$26
        orb             r1,r1,BYTE_SECTOR_BUF+$26
        asl             r1,r1,#8
        asl             r1,r1,#8
Line 3981... Line 4249...
spi_read_error_msg:
spi_read_error_msg:
        db      "SD card read error",CR,LF,0
        db      "SD card read error",CR,LF,0
spi_write_error_msg:
spi_write_error_msg:
        db      "SD card write error",0
        db      "SD card write error",0
 
 
 
do_fmt:
 
        jsr             spi_init
 
        cmp             #0
 
        bne             fmt_abrt
 
        ldx             #DIRBUF
 
        ldy             #65536
 
        ; clear out the directory buffer
 
dfmt1:
 
        stz             (x)
 
        inx
 
        dey
 
        bne             dfmt1
 
        jsr             store_dir
 
fmt_abrt:
 
        rts
 
 
 
do_dir:
 
        jsr             CRLF
 
        jsr             spi_init
 
        cmp             #0
 
        bne             dirabrt
 
        jsr             load_dir
 
        ld              r4,#0                   ; r4 = entry counter
 
ddir3:
 
        asl             r3,r4,#6                ; y = start of entry, 64 bytes per entry
 
        ldx             #32                             ; 32 chars in filename
 
ddir4:
 
        lb              r1,DIRBUF<<2,y
 
        beq             ddir2                   ; move to next dir entry if null is found
 
        cmp             #$20                    ; don't display control chars
 
        bmi             ddir1
 
        jsr             DisplayChar
 
        bra             ddir5
 
ddir1:
 
        lda             #' '
 
        jsr             DisplayChar
 
ddir5:
 
        iny
 
        dex
 
        bne             ddir4
 
        lda             #' '
 
        jsr             DisplayChar
 
        asl             r3,r4,#4                ; y = start of entry, 16 words per entry
 
        lda             DIRBUF+$D,y
 
        ldx             #5
 
        jsr             PRTNUM
 
        jsr             CRLF
 
ddir2:
 
        jsr             KeybdGetChar
 
        cmp             #CTRLC
 
        beq             ddir6
 
        inc             r4
 
        cmp             r4,#512         ; max 512 dir entries
 
        bne             ddir3
 
ddir6:
 
 
 
dirabrt:
 
        rts
 
 
 
load_dir:
 
        pha
 
        phx
 
        phy
 
        lda             #4000
 
        ldx             #DIRBUF<<2
 
        ldy             #64
 
        jsr             spi_read_multiple
 
        ply
 
        plx
 
        pla
 
        rts
 
store_dir:
 
        pha
 
        phx
 
        phy
 
        lda             #4000
 
        ldx             #DIRBUF<<2
 
        ldy             #64
 
        jsr             spi_write_multiple
 
        ply
 
        plx
 
        pla
 
        rts
 
 
 
; r1 = pointer to file name
 
; r2 = pointer to buffer to save
 
; r3 = length of buffer
 
;
 
do_save:
 
        pha
 
        jsr             spi_init
 
        cmp             #0
 
        bne             dsavErr
 
        pla
 
        jsr             load_dir
 
        ld              r4,#0
 
dsav4:
 
        asl             r5,r4,#6
 
        ld              r7,#0
 
        ld              r10,r1
 
dsav2:
 
        lb              r6,DIRBUF<<2,r5
 
        lb              r8,0,r10
 
        cmp             r6,r8
 
        bne             dsav1
 
        inc             r5
 
        inc             r7
 
        inc             r10
 
        cmp             r7,#32
 
        bne             dsav2
 
        ; here the filename matched
 
dsav8:
 
        asl             r7,r4,#7        ; compute file address  64k * entry #
 
        add             r7,r7,#5000     ; start at sector 5,000
 
        ld              r1,r7           ; r1 = sector number
 
        lsr             r3,r3,#9        ; r3/512
 
        iny                                     ; +1
 
        jsr             spi_write_multiple
 
dsav3:
 
        rts
 
        ; Here the filename didn't match
 
dsav1:
 
        inc             r4
 
        cmp             r4,#512
 
        bne             dsav4
 
        ; Here none of the filenames in the directory matched
 
        ; Find an empty entry.
 
        ld              r4,#0
 
dsav6:
 
        asl             r5,r4,#6
 
        lb              r6,DIRBUF<<2,r5
 
        beq             dsav5
 
        inc             r4
 
        cmp             r4,#512
 
        bne             dsav6
 
        ; Here there were no empty entries
 
        lda             #msgDiskFull
 
        jsr             DisplayStringB
 
        rts
 
dsav5:
 
        ld              r7,#32
 
        ld              r10,r1
 
dsav7:
 
        lb              r6,0,r10        ; copy the filename into the directory entry
 
        sb              r6,DIRBUF<<2,r5
 
        inc             r5
 
        inc             r10
 
        dec             r7
 
        bne             dsav7
 
                                                ; copy the file size into the directory entry
 
        asl             r5,r4,#4        ; 16 words per dir entry
 
        sty             DIRBUF+$D,r5
 
        jsr             store_dir
 
        bra             dsav8
 
dsavErr:
 
        pla
 
        rts
 
 
 
msgDiskFull
 
        db      CR,LF,"The disk is full, unable to save file.",CR,LF,0
 
 
 
do_load:
 
        pha
 
        jsr             spi_init
 
        cmp             #0
 
        bne             dsavErr
 
        pla
 
        jsr             load_dir
 
        ld              r4,#0
 
dlod4:
 
        asl             r5,r4,#6
 
        ld              r7,#0
 
        ld              r10,r1
 
dlod2:
 
        lb              r6,DIRBUF<<2,r5
 
        lb              r8,0,r10
 
        cmp             r6,r8
 
        bne             dlod1
 
        inc             r5
 
        inc             r7
 
        inc             r10
 
        cmp             r7,#32
 
        bne             dlod2
 
        ; here the filename matched
 
dlod8:
 
        asl             r5,r4,#4                                ; 16 words
 
        ld              r3,DIRBUF+$d,r5                 ; get file size into y register
 
        asl             r7,r4,#7        ; compute file address  64k * entry #
 
        add             r7,r7,#5000     ; start at sector 5,000
 
        ld              r1,r7           ; r1 = sector number
 
        lsr             r3,r3,#9        ; r3/512
 
        iny                                     ; +1
 
        jsr             spi_read_multiple
 
dlod3:
 
        rts
 
        ; Here the filename didn't match
 
dlod1:
 
        inc             r4
 
        cmp             r4,#512
 
        bne             dlod4
 
        ; Here none of the filenames in the directory matched
 
        ;
 
        lda             #msgFileNotFound
 
        jsr             DisplayStringB
 
        rts
 
 
 
msgFileNotFound:
 
        db      CR,LF,"File not found.",CR,LF
 
 
;==============================================================================
;==============================================================================
; Ethernet
; Ethernet
;==============================================================================
;==============================================================================
my_MAC1 EQU     0x00
my_MAC1 EQU     0x00
my_MAC2 EQU     0xFF
my_MAC2 EQU     0xFF
Line 4658... Line 5135...
        pla
        pla
        rts
        rts
 
 
;include "float.asm"
;include "float.asm"
 
 
 
 
 
;==============================================================================
 
; Memory Management routines follow.
 
;==============================================================================
 
MemInit:
 
        lda             #1                                      ; initialize memory semaphore
 
        sta             MEM_SEMA
 
        lda             #$4D454D20
 
        sta             HeapStart+MEM_CHK
 
        sta             HeapStart+MEM_FLAG
 
        sta             HeapEnd-2
 
        sta             HeapEnd-3
 
        lda             #0
 
        sta             HeapStart+MEM_PREV      ; prev of first MEMHDR
 
        sta             HeapEnd                 ; next of last MEMHDR
 
        lda             #HeapEnd
 
        ina
 
        sub             #$4
 
        sta             HeapStart+MEM_NEXT      ; next of first MEMHDR
 
        lda             #HeapStart
 
        sta             HeapEnd-1               ; prev of last MEMHDR
 
        rts
 
 
 
ReportMemFree:
 
        jsr             CRLF
 
        lda             #HeapEnd
 
        ina
 
        sub             #HeapStart
 
        ldx             #5
 
        jsr             PRTNUM
 
        lda             #msgMemFree
 
        jsr             DisplayStringB
 
        rts
 
 
 
msgMemFree:
 
        db      " words free",CR,LF,0
 
 
 
;------------------------------------------------------------------------------
 
; Allocate memory from the heap.
 
;------------------------------------------------------------------------------
 
MemAlloc:
 
        phx
 
        phy
 
        push    r4
 
memaSpin:
 
        ldx             MEM_SEMA+1
 
        beq             memaSpin
 
        ldx             #HeapStart
 
mema4:
 
        ldy             MEM_FLAG,x              ; Check the flag word to see if this block is available
 
        cpy             #$4D454D20
 
        bne             mema1                   ; block not available, go to next block
 
        ld              r4,MEM_NEXT,x   ; compute the size of this block
 
        sub             r4,r4,r2
 
        sub             r4,r4,#4                ; minus size of block header
 
        cmp             r1,r4                   ; is the block large enough ?
 
        bmi             mema2                   ; if yes, go allocate
 
mema1:
 
        ldx             MEM_NEXT,x              ; go to the next block
 
        beq             mema3                   ; if no more blocks, out of memory error
 
        bra             mema4
 
mema2:
 
        ldy             #$6D656D20
 
        sty             MEM_FLAG,x
 
        sub             r4,r4,r1
 
        cmp             r4,#4                   ; is the block large enough to split
 
        bpl             memaSplit
 
        stz             MEM_SEMA+1
 
        txa
 
        pop             r4
 
        ply
 
        plx
 
        rts
 
mema3:                                          ; insufficient memory
 
        stz             MEM_SEMA+1
 
        pop             r4
 
        ply
 
        plx
 
        lda             #0
 
        rts
 
memaSplit:
 
        add             r4,r1,r2
 
        add             r4,r4,#4
 
        ldy             #$4D454D20
 
        sty             (r4)
 
        sty             MEM_FLAG,r4
 
        stx             MEM_PREV,r4
 
        ldy             MEM_NEXT,x
 
        sty             MEM_NEXT,r4
 
        st              r4,MEM_PREV,y
 
        ld              r1,r4
 
        stz             MEM_SEMA+1
 
        pop             r4
 
        ply
 
        plx
 
        rts
 
 
 
;------------------------------------------------------------------------------
 
; Free previously allocated memory. Recombine with next and previous blocks
 
; if they are free as well.
 
;------------------------------------------------------------------------------
 
MemFree:
 
        cmp             #0                      ; null pointer ?
 
        beq             memf2
 
        phx
 
        phy
 
memfSpin:
 
        ldx             MEM_SEMA+1
 
        beq             memfSpin
 
        ldx             MEM_FLAG,r1
 
        cpx             #$6D656D20      ; is the block allocated ?
 
        bne             memf1
 
        ldx             #$4D454D20
 
        stx             MEM_FLAG,r1     ; mark block as free
 
        ldx             MEM_PREV,r1     ; is the previous block free ?
 
        beq             memf3           ; no previous block
 
        ldy             MEM_FLAG,x
 
        cpy             #$4D454D20
 
        bne             memf3           ; the previous block is not free
 
        ldy             MEM_NEXT,r1
 
        sty             MEM_NEXT,x
 
        beq             memf1           ; no next block
 
        stx             MEM_PREV,y
 
memf3:
 
        ldy             MEM_NEXT,r1
 
        ldx             MEM_FLAG,y
 
        cpx             #$4D454D20
 
        bne             memf1           ; next block not free
 
        ldx             MEM_PREV,r1
 
        stx             MEM_PREV,y
 
        beq             memf1           ; no previous block
 
        sty             MEM_NEXT,x
 
memf1:
 
        stz             MEM_SEMA+1
 
        ply
 
        plx
 
memf2:
 
        rts
 
 
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
; Bus Error Routine
; Bus Error Routine
; This routine display a message then restarts the BIOS.
; This routine display a message then restarts the BIOS.
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
;
;
Line 4684... Line 5300...
        cli                                                     ; enable interrupts so we can get a char
        cli                                                     ; enable interrupts so we can get a char
ber1:
ber1:
        jsr             KeybdGetChar
        jsr             KeybdGetChar
        cmp             #-1
        cmp             #-1
        beq             ber1
        beq             ber1
        jmp             start
        jmp             ber1
 
 
msgBusErr:
msgBusErr:
        db              "Bus error at: ",0
        db              "Bus error at: ",0
msgDataAddr:
msgDataAddr:
        db              " data address: ",0
        db              " data address: ",0
 
 
strStartQue:
strStartQue:
        db              1,0,0,0,2,0,0,0,3,1,0,0,4,0,0,0
        db              1,0,0,0,2,0,0,0,3,1,0,0,4,0,0,0
 
;       db              0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
 
 
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
; 100 Hz interrupt
; 100 Hz interrupt
; - takes care of "flashing" the cursor
; - takes care of "flashing" the cursor
; - switching tasks
; - switching tasks
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
;
;
p100Hz:
p100Hz:
        ; Handle every other interrupt because 100Hz interrupts may be too fast.
        ; Handle every other interrupt because 100Hz interrupts may be too fast.
        pha
        pha
 
        lda             #3                              ; reset the edge sense circuit
 
        sta             PIC_RSTE
 
;       inc             TEXTSCR+83              ; update IRQ live indicator on screen
        lda             IRQFlag
        lda             IRQFlag
        ina
        ina
        sta             IRQFlag
        sta             IRQFlag
        ror
        ror
        bcc             p100Hz11
        bcc             p100Hz11
        stz             0xFFDCFFFC              ; clear interrupt
 
        pla
        pla
        rti
        rti
 
 
p100Hz11:
p100Hz11:
 
 
Line 4732... Line 5351...
        push    r12
        push    r12
        push    r13
        push    r13
        push    r14
        push    r14
        push    r15
        push    r15
        ldx             RunningTCB
        ldx             RunningTCB
        and             r2,r2,#$FF
 
        tsa                                             ; save off the stack pointer
        tsa                                             ; save off the stack pointer
        sta             TCB_SPSave,x
        sta             TCB_SPSave,x
        tsr             sp8,r1                  ; and the eight bit mode stack pointer
        tsr             sp8,r1                  ; and the eight bit mode stack pointer
        sta             TCB_SP8Save,x
        sta             TCB_SP8Save,x
 
        tsr             abs8,r1
 
        sta             TCB_ABS8Save,x  ; 8 bit emulation base register
        lda             TCB_Status,x    ; set the task status to PREEMPT
        lda             TCB_Status,x    ; set the task status to PREEMPT
        and             r1,r1,#~TS_RUNNING
        and             r1,r1,#~TS_RUNNING
        or              r1,r1,#TS_PREEMPT
        or              r1,r1,#TS_PREEMPT
        sta             TCB_Status,x
        sta             TCB_Status,x
 
p100Hz4:
 
 
        ; support EhBASIC's IRQ functionality
        ; support EhBASIC's IRQ functionality
        ; code derived from minimon.asm
        ; code derived from minimon.asm
        lda             #3                              ; Timer is IRQ #3
        lda             #3                              ; Timer is IRQ #3
        sta             IrqSource               ; stuff a byte indicating the IRQ source for PEEK()
        sta             IrqSource               ; stuff a byte indicating the IRQ source for PEEK()
        lb              r1,IrqBase              ; get the IRQ flag byte
        lb              r1,IrqBase              ; get the IRQ flag byte
        lsr             r2,r1
        lsr             r4,r1
        or              r1,r1,r2
        or              r1,r1,r4
        and             #$E0
        and             #$E0
        sb              r1,IrqBase
        sb              r1,IrqBase
 
 
 
 
        inc             TEXTSCR+83              ; update IRQ live indicator on screen
        inc             TEXTSCR+83              ; update IRQ live indicator on screen
        stz             0xFFDCFFFC              ; clear interrupt
 
 
 
        ; flash the cursor
        ; flash the cursor
 
        cpx             IOFocusNdx              ; only flash the cursor for the task with the IO focus.
 
        bne             p100Hz1a
        lda             CursorFlash             ; test if we want a flashing cursor
        lda             CursorFlash             ; test if we want a flashing cursor
        beq             p100Hz1a
        beq             p100Hz1a
        jsr             CalcScreenLoc   ; compute cursor location in memory
        jsr             CalcScreenLoc   ; compute cursor location in memory
        tay
        tay
        lda             $10000,y                ; get color code $10000 higher in memory
        lda             $10000,y                ; get color code $10000 higher in memory
        ldx             IRQFlag                 ; get counter
        ld              r4,IRQFlag              ; get counter
        lsr             r2,r2
        lsr             r4,r4
        and             r2,r2,#$0F              ; limit to low order nybble
        and             r4,r4,#$0F              ; limit to low order nybble
        and             #$F0                    ; prepare to or in new value, mask off foreground color
        and             #$F0                    ; prepare to or in new value, mask off foreground color
        or              r1,r1,r2                ; set new foreground color for cursor
        or              r1,r1,r4                ; set new foreground color for cursor
        sta             $10000,y                ; store the color code back to memory
        sta             $10000,y                ; store the color code back to memory
p100Hz1a
p100Hz1a
 
 
        ; Check the timeout list to see if there are items ready to be removed from
        ; Check the timeout list to see if there are items ready to be removed from
        ; the list. Also decrement the timeout of the item at the head of the list.
        ; the list. Also decrement the timeout of the item at the head of the list.
Line 4784... Line 5405...
        jsr             RemoveFromTimeoutList
        jsr             RemoveFromTimeoutList
        jsr             AddTaskToReadyList
        jsr             AddTaskToReadyList
        bra             p100Hz15                                ; go back and see if there's another task to be removed
        bra             p100Hz15                                ; go back and see if there's another task to be removed
 
 
p100Hz14:
p100Hz14:
        dea                                                             ; decrement the entries timeout
        dea                                                             ; decrement the entry's timeout
        sta             TCB_Timeout,x
        sta             TCB_Timeout,x
 
 
p100Hz12:
p100Hz12:
SelectTaskToRun:
SelectTaskToRun:
        ; Search the ready queues for a ready task.
        ; Search the ready queues for a ready task.
Line 4825... Line 5446...
p100Hz5
p100Hz5
        dec             r6
        dec             r6
        bne             p100Hz2
        bne             p100Hz2
 
 
        ; here there were no tasks ready
        ; here there were no tasks ready
        stz             RunningTCB                      ; select BIOS task
        ; this might happen if the interrupt is called before tasks
        ldx             TCB_Status              ; flag the task as the running task
        ; are setup. Otherwise the IDLE task should at least be running.
        or              r2,r2,#TS_RUNNING
        bra             p100Hz6
        and             r2,r2,#~TS_PREEMPT
 
        stx             TCB_Status
 
p100Hz3:
p100Hz3:
p100Hz10
p100Hz10
        ldx             RunningTCB
        ldx             RunningTCB
        and             r2,r2,#$FF
        lda             TCB_ABS8Save,x          ; 8 bit emulation base register
 
        trs             r1,abs8
        lda             TCB_SP8Save,x           ; get back eight bit stack pointer
        lda             TCB_SP8Save,x           ; get back eight bit stack pointer
        trs             r1,sp8
        trs             r1,sp8
        ldx             TCB_SPSave,x            ; get back stack pointer
        ldx             TCB_SPSave,x            ; get back stack pointer
        txs
        txs
        ; restore registers
        ; restore registers
 
p100Hz6:
        pop             r15
        pop             r15
        pop             r14
        pop             r14
        pop             r13
        pop             r13
        pop             r12
        pop             r12
        pop             r11
        pop             r11
Line 4864... Line 5485...
; This IRQ must be fast.
; This IRQ must be fast.
; Increments the millisecond counter
; Increments the millisecond counter
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
;
;
p1000Hz:
p1000Hz:
        stz             0xFFDCFFFD                              ; acknowledge interrupt
        pha
 
        lda             #2                                              ; reset edge sense circuit
 
        sta             PIC_RSTE
        inc             Milliseconds                    ; increment milliseconds count
        inc             Milliseconds                    ; increment milliseconds count
 
        inc             TEXTSCR+82                              ; update IRQ live indicator on screen
 
        pla
        rti
        rti
 
 
 
;------------------------------------------------------------------------------
 
; Sleep interrupt
 
; This interrupt just selects another task to run. The current task is
 
; stuck in an infinite loop.
 
;------------------------------------------------------------------------------
slp_rout:
slp_rout:
 
        cld             ; clear extended precision mode
 
        pha
 
        phx
 
        phy
 
        push    r4
 
        push    r5
 
        push    r6
 
        push    r7
 
        push    r8
 
        push    r9
 
        push    r10
 
        push    r11
 
        push    r12
 
        push    r13
 
        push    r14
 
        push    r15
 
        ldx             RunningTCB
 
        bmi             RunningTCBErr
 
        and             r2,r2,#$FF
 
        tsa                                             ; save off the stack pointer
 
        sta             TCB_SPSave,x
 
        tsr             sp8,r1                  ; and the eight bit mode stack pointer
 
        sta             TCB_SP8Save,x
 
        lda             TCB_Status,x    ; set the task status to SLEEP
 
        and             r1,r1,#~TS_RUNNING
 
        or              r1,r1,#TS_SLEEP
 
        sta             TCB_Status,x
 
slp1:
 
        jmp             SelectTaskToRun
 
 
 
;------------------------------------------------------------------------------
 
; Check for and emulate unsupoorted instructions.
 
;------------------------------------------------------------------------------
 
InvalidOpIRQ:
 
        pha
 
        phx
 
        phy
 
        tsx
 
        lda             4,x             ; get the address of the invalid op off the stack
 
        lb              r3,0,r1 ; get the opcode byte
 
        cpy             #$44    ; is it MVP ?
 
        beq             EmuMVP
 
        cpy             #$54    ; is it MVN ?
 
        beq             EmuMVN
 
        ; We don't know what the op is. Treat it like a NOP
 
        ; Increment the address and return.
 
        pha
 
        lda             #msgUnimp
 
        jsr             DisplayStringB
 
        pla
 
        jsr             DisplayWord
 
        jsr             CRLF
 
        ina
 
        sta             4,x             ; save incremented return address back to stack
 
        ldx             #64
 
ioi1:
 
        tsr             abs8,r1
 
        jsr             DisplayWord
 
        lda             #' '
 
        jsr             DisplayChar
 
        dex
 
        bne             ioi1
 
        jsr             CRLF
 
        ply
 
        plx
 
        pla
 
        rti
 
 
 
EmuMVP:
 
        push    r4
 
        push    r5
 
        tsr             sp,r4
 
        lda             4,r4
 
        ldx             3,r4
 
        ldy             2,r4
 
EmuMVP1:
 
        ld              r5,(x)
 
        st              r5,(y)
 
        dex
 
        dey
 
        dea
 
        cmp             #$FFFFFFFF
 
        bne             EmuMVP1
 
        sta             4,r4
 
        stx             3,r4
 
        sty             2,r4
 
        inc             6,r4            ; increment the return address by one.
 
        pop             r5
 
        pop             r4
 
        ply
 
        plx
 
        pla
        rti
        rti
 
 
 
EmuMVN:
 
        push    r4
 
        push    r5
 
        tsr             sp,r4
 
        lda             4,r4
 
        ldx             3,r4
 
        ldy             2,r4
 
EmuMVN1:
 
        ld              r5,(x)
 
        st              r5,(y)
 
        inx
 
        iny
 
        dea
 
        cmp             #$FFFFFFFF
 
        bne             EmuMVN1
 
        sta             4,r4
 
        stx             3,r4
 
        sty             2,r4
 
        inc             6,r4            ; increment the return address by one.
 
        pop             r5
 
        pop             r4
 
        ply
 
        plx
 
        pla
 
        rti
 
 
 
msgUnimp:
 
        db      "Unimplemented at: ",0
 
 
brk_rout:
brk_rout:
        rti
        rti
nmirout
nmirout:
 
        pha
 
        phx
 
        lda             #msgPerr
 
        jsr             DisplayStringB
 
        tsx
 
        lda             4,x
 
        jsr             DisplayWord
 
        jsr             CRLF
 
        plx
 
        pla
        rti
        rti
 
 
 
msgPerr:
 
        db      "Parity error at: ",0
 
 
message "1298"
message "1298"
include "TinyBasic65002.asm"
include "TinyBasic65002.asm"
message "1640"
message "1640"
        org $0FFFFFFF4          ; NMI vector
        org $0FFFFFFF4          ; NMI vector
        dw      nmirout
        dw      nmirout

powered by: WebSVN 2.1.0

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