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

Subversion Repositories lattice6502

[/] [lattice6502/] [ghdl/] [kernel4.asm] - Rev 7

Compare with Previous | Blame | View Log

;       Kernel
;       Reads uart input
;       Processes the command
;               raddress = reads one byte outs to uart
;               Raddress = reads 16 bytes outs to uart
;               waddress data = write one databyte to address location
;               Faddress data nn = Fill databyte into nn locations
;               S1bcadd d d d d d chksum= see Motorolla hex format used to load short program
;               Gaddress start from this address
;       Sends the stuff in the output buffer to uart tx.
;
;
        cpu 6502
PAGE 40,120

led     equ     $4007
dat232  equ     $4000
stat232 equ     $4001
tx232   equ     $4000
irq     equ     $4002
nmi     equ     $4003
break   equ     $10
intrpt  equ     $04

;dat232 equ     $290            ;used to help debug
;stat232        equ     $291
;tx232  equ     $292

tx_rdy  equ     $80     ;bit 7
rx_rdy  equ     $40     ;bit 6
;
inbuflen        equ     $40     ;input buffer table length
otbuflen        equ     $40     ;output buffer table length
cr      equ     $0d     ;cariage return


;       ZERO PAGE STUFF
*       =       $0
ptr     dw      $0      ;reserve 2 bytes
hexword dw      $0      ;accumulates hex word


;       STUFF IN UPPER MEMORY
*       =       $200

inbuff  db      0        ;reserve 64 bytes for command input
*       =       inbuff + inbuflen
outbuff db      0
*       =       outbuff + otbuflen

txoutpt db      0        ;output pointer index
txinpt  db      0        ;input pointer index
txoutct db      0        ;Counter
inbufpt db      0        ;Ponts where to put kb text
txtpt   db      0        ;Points to text to be processed
sp1     db      $0
sp2     db      $0
sp3     db      $0
temp_y  db      0
bytcnt  db      0
n       db      0


page
*       =       $fc00
;       This is the main loop.
;
main    ldx     #$0
        txs             ;Set stack pointer to x100
        stx     txoutpt
        stx     txinpt
        stx     txoutct
        stx     inbufpt
        stx     txtpt

        lda     #"?"    ;Illegal char or restart
        jsr     sendtxt ;or bad frap

loop    jmp     input   ;Read serial character
back1   jmp     process ;It's in reg a and stored in inbuff
back2   jmp     sendbuf
back3   jmp     loop
;
;
page
;       This is where the uart receiver  character is picked up and dumped
;       into the input buffer (inbuff)
code
input   jmp     getchar
;       beq     back1           ;No new character
.loop   ldx     inbufpt         ;Returns with it in y
        sta     inbuff,x        ;input buffer
        tay
        inx
        txa
        and     #inbuflen - 1
        sta     inbufpt
        tya
        jmp     back1           ;return with character in a

getchar lda     stat232
        and     #rx_rdy
        beq     .exit
        lda     dat232          ;Character in a
        jmp     .loop
.exit   lda     #$0
        jmp     back1
code
page
code
;       This is where the complicated stuff is done
process beq     .exit   ;It's a null character
        pha
        jsr     sendtxt ;Echo character to terminal
        pla
        cmp     #"\r"
        beq     got_cr
        cmp     #"\n"
        beq     got_cr
.exit   jmp     back2

got_cr  ldx     txtpt           ;Decode the KB input
        lda     inbuff,x
        jsr     inctxt
        cmp     #"r"
        beq     read1   ;Read one byte rnnnn

        cmp     #"R"
        bne     next0
        jmp     readn

next0   cmp     #"w"    ;Write one byte wnnnn xx
        bne     next1
        jmp     write1

next1   cmp     #"F"    ;Fill Fnnnn nn xx
        bne     next2
        jmp     fill

next2   cmp     #"G"    ;goto nnnn
        bne     next3
        jmp     goto

next3   cmp     #"g"    ;goto 290
        bne     next4
        jsr     inctxt  ;Get to end of buffer
        jmp     $290


next4   cmp     #"S"
        bne     next5
        ldx     txtpt
        lda     inbuff,x
        jsr     inctxt
        cmp     #"1"
        bne     next5
        jmp     scode
next5   cmp     #"9"
        bne     next6
        jmp     flush   ;Last S code packet not used so flush

next6   jmp     main    ;restart bad input




code
page
code
;       This is where stuff is pulled from the output buffer (outbuff) 
;       and sent to the terminal
sendbuf lda     stat232 ;All regs destroyed
        and     #tx_rdy ;Test uart tx status
        bne     .exit   ;In use
        jmp     .gettxt         ;char in A for return
.bak    sta     tx232   ;Byte to terminal
.exit   jmp     back3
;                       Pull txt from buffer if not empty
.gettxt lda     txoutct
        beq     .exit
        dec     txoutct
        lda     txoutpt ;out text out pointer
        tay
        tax
        iny
        tya
        and     #inbuflen - 1
        sta     txoutpt
        lda     outbuff,x
        jmp     .bak

code
page

page                    ;This routing reads one byt"\n"e from address
read1   jsr     txt2hex ;Convert string to 16 bit word

        lda     hexword + 1
        jsr     hex2txt

        lda     hexword
        jsr     hex2txt

        lda     #" "
        jsr     sendtxt
        ldy     #$0
        lda     (hexword),y
        jsr     hex2txt
        lda     #"\r"
        jsr     sendtxt
        jmp     back2
;       ===============================================

readn   jsr     txt2hex ;Convert string to 16 bit word

        lda     hexword + 1
        sta     ptr + 1
        jsr     hex2txt

        lda     hexword
        and     #$f0
        sta     ptr
        jsr     hex2txt

        lda     #$0
        sta     temp_y
read_lp lda     #" "
        jsr     sendtxt
        ldy     temp_y
        lda     (ptr),y
        jsr     hex2txt
        ldy     temp_y
        iny
        tya
        sta     temp_y
        cmp     #$10
        bne     read_lp

        lda     #"\r"
        jsr     sendtxt
        jmp     back2
;       ==================================================

;       This routine writes one byte to memory
;       address space data
write1  jsr     txt2hex ;Convert string to 16 bit word

        lda     hexword + 1     ;Save these locations
        sta     ptr + 1
        jsr     hex2txt

        lda     hexword
        sta     ptr
        jsr     hex2txt

        jsr     txt2hex         ;Convert data
        lda     hexword

        ldy     #$0
        sta     (ptr),y
        lda     #"\r"
        jsr     sendtxt
        jmp     back2
;       ==================================================
fill    jsr     txt2hex ;Convert string to 16 bit word

        lda     hexword + 1     ;Save these locations
        pha
        lda     hexword
        pha

        jsr     txt2hex         ;Convert data
        lda     hexword
        pha                     ;Data on stack

        jsr     txt2hex         ;how many bytes?
        lda     hexword
        sta     sp1             ;Number of bytes to fill in sp1

        pla
        tay                     ;Data into y
        pla                     ;Restore hexword
        sta     hexword
        pla
        sta     hexword + 1
        tya
        pha

        ldy     #$0
        sta     temp_y
        pla
fill_lp sta     (hexword),y
        iny
        sta     temp_y
        cpy     sp1
        bne     fill_lp

        lda     #"\r"
        jsr     sendtxt
        jmp     back2

goto    jsr     txt2hex
        jmp     (hexword)       ;goto user code best return to main x"fc00"

page
code
;       This routine takes the Motorolla  S code generated
;       by crasm and loads it into RAM.
;       The checksum is not checked since there is no comms.

scode   ldx     #$2             ;convert two bytes byte count
        jsr     cod2hex
        lda     hexword
        clc
        sbc     #3
        sta     bytcnt          ;total bytes to transfer

        ldx     #$4             ;convert four bytes to address
        jsr     cod2hex ;hexword points to where data goes
        lda     hexword
        sta     ptr
        lda     hexword + 1
        sta     ptr + 1         ;ptr holds point

        lda     #$0
        sta     n

scodelp ldx     #$2     ;4 or 2 bytes to form number
        jsr     cod2hex
        ldy     n
        sta     (ptr),y
        iny
        sty     n
        cpy     bytcnt
        bne     scodelp
        lda     #"\r"
        jsr     sendtxt
        jsr     inctxt          ;skip checksum 1
        jsr     inctxt          ;and cs2
        jsr     inctxt          ;and cr
        jmp     back2

cod2hex lda     #$0
        sta     hexword
        sta     hexword + 1
        stx     sp3             ;Save No of bytes to convert this time

.lphex  ldx     sp3
        beq     .exit
        ldx     txtpt
        lda     inbuff,x
        jsr     inctxt
        ldx     sp3

        ldy     #$0
.nothis cmp     asctbl,y
        bne     .next
        dex                     ;Got 1 match
        stx     sp3
        jsr     accum
        jmp     .lphex
.next   iny
        cpy     #$10
        bne     .nothis ;next hex char
.exit   rts     ;last char

flush   jsr     cod2hex ;empty buffer
        jmp     back2   ;result of subroutine ignored


code


page
;       This is where the output buffer (outbuf) is loaded.
code
sendtxt pha                     ;A hold character to be place in buffer
        sty     sp2
        lda     txoutct
        cmp     #otbuflen -1
        bpl     .exit           ;buffer should never be full
        inc     txoutct
        lda     txinpt  ;out text out pointer
        tay
        tax
        iny
        tya
        and     #otbuflen - 1
        sta     txinpt
        pla
        sta     outbuff,x
        ldy     sp2
        rts
.exit   pla
        ldy     sp2
        rts


code
page
code
txt2hex ldy     #$0
        sty     hexword
        sty     hexword + 1
irq     equ     $4002
nmi     equ     $4003
break   equ     $10
intrpt  equ     $04
loophex ldx     txtpt
        lda     inbuff,x        ;input buffer
        jsr     inctxt          ;inc counter
        ldy     #$0
nomatch cmp     asctbl,y
        bne     .next
        jsr     accum
        jmp     loophex
.next   iny
        cpy     #$10
        bne     nomatch ;next hex char
        rts     ;not hex char


accum   stx     sp1
        ldx     #$4             ;y holds hex of character
accloop asl     hexword
        rol     hexword + 1
        dex
        bne     accloop
        tya
        ora     hexword
        sta     hexword
        ldx     sp1
        rts


hex2txt tay             ;A holds byte to convert
        clc
        ror     a
        ror     a
        ror     a
        ror     a
        and     #$0f
        tax
        lda     asctbl,x
        jsr     sendtxt
        tya
        and     #$0f
        tax
        lda     asctbl,x
        jsr     sendtxt
        rts
;                       inc after reading from inbuff
inctxt  pha             ;NOT before
        inc     txtpt
        lda     txtpt
        and     #inbuflen - 1
        sta     txtpt
        pla
        rts

asctbl  asc     "0123456789ABCDEF"
code

page    ;All the start up stuff

nmi_srv lda     #$01
        sta     nmi
        lda     #"N"
        jsr     sendtxt
        lda     #"M"
        jsr     sendtxt
        lda     #"I"
        jsr     sendtxt
        lda     #"\r"
        jsr     sendtxt
        rti

irq_srv pla
        pha
        and     #break
        beq     irqish
        lda     #"B"
        jsr     sendtxt
        lda     #"R"
        jsr     sendtxt
        lda     #"K"
        jsr     sendtxt
        lda     #"\r"
        jsr     sendtxt
        jmp     endish


irqish  lda     #$01
        sta     irq
        lda     #$01
        sta     irq
        lda     #"I"
        jsr     sendtxt
        lda     #"R"
        jsr     sendtxt
        lda     #"Q"
        jsr     sendtxt
        lda     #"\r"
        jsr     sendtxt
endish  rti

*       =$fff0
        jmp     sendtxt ;Links for usrcode
        jmp     hex2txt
        dw      back2   ;jmp ($FFF6) in user code.

*       = $fff8
;
indjmp  dw      main
nmi_int dw      nmi_srv
rst     dw      main    ;Should be main, this is for test only
irq_int dw      irq_srv

Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

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