********************************************************************
* sbc09 emulator
*
* $Id$
*
* Edt/Rev  YYYY/MM/DD  Modified by
* Comment
* ------------------------------------------------------------------
*   1      2018/08/20  S. Kono
* Emulatoe sbc09 on os9 lv2

         nam   Sbc09
         ttl   Sbc09 emulator

         ifp1
         use   defsfile
         endc

* Module header definitions
tylg     set   Prgrm+Objct   
atrv     set   ReEnt+rev
rev      set   $00
edition  set   1

         mod   eom,name,tylg,atrv,start,size

         org   0
ientry  rmb   $80
filepath rmb   2
parmptr  rmb   2
stdin    rmb   1
chksum   rmb   1
bcount   rmb   1
adr      rmb   2
work     rmb   2
readbuff rmb   $100
         org   $400
emstart  rmb   $e000-.
size     equ   .

name     fcs   /Sbc09/
         fcb   edition

start    clr   <stdin
         stx   <parmptr         save parameter pointer
         stu   <work            save parameter pointer
         lda   #READ.           read access mode
         os9   I$Open           open file
         lbcs   L0049            branch if error
         sta   <filepath        else save path to file
         stx   <parmptr         and updated parm pointer
L001F    lda   <filepath        get path
         leax  readbuff,u       point X to read buffer
         ldy   #200             read up to 200 bytes
         os9   I$ReadLn         read it!
         bcs   L0035            branch if error
         bsr   srecords
         bra   L001F            else exit
L0035    cmpb  #E$EOF           did we get an EOF error?
*         bne   L0049            exit if not
         lda   <filepath        else get path
         os9   I$Close          and close it
         bcs   L0049            branch if error
*         ldx   <parmptr         get param pointer
*         lda   ,x               get char
*         cmpa  #C$CR            end of command line?
*         bne   start            branch if not

copytbl
         lda   #$17             lbra
         sta   $e400
         leax  Exit,pcr
         leax  -$e403,x
         stx   $e401
         leax  iotbl,pcr
         leay  iotblend,pcr
         pshs  x,y
         ldy   #(iotblend-iotbl)
l1       ldb   #$7e     * JMP
         stb   ,u+
         ldd   ,x++
         addb  1,s
         adca  ,s
         std   ,u++
         cmpx  2,s
         ble   l1
         puls  x,y
         jmp   $400
 
Exit     lbsr        setecho
*        ldx         <work
*        leax        readbuff,x
*        ldb         #1
*        lbsr         getline
*        lbsr         getpoll
*        lda        <stdin        
*        os9        I$Close      

        clrb
        os9        F$Exit
*       no return


iotbl
         fdb   getchar-iotbl            ; 0
         fdb   putchar-iotbl            ; 3
         fdb   getline-iotbl            ; 6
         fdb   putline-iotbl            ; 9
         fdb   putcr-iotbl              ; $C
         fdb   getpoll-iotbl            ; $F
         fdb   xopenin-iotbl            ; $12
         fdb   xopenout-iotbl           ; $15
         fdb   xabortin-iotbl           ; $18
         fdb   xclosein-iotbl           ; $1B
         fdb   xcloseout-iotbl          ; $1E
         fdb   delay-iotbl              ; $21
         fdb   noecho-iotbl             ; $24
         fdb   setecho-iotbl            ; $27
         fdb   exit-iotbl               ; $2a
iotblend

L0049
err     ldb    #1
        bra     Exit

srecords
        leax   readbuff,u
        clr    <chksum
sline   lda    ,x+
        cmpa   #'S'
        bne    slast
        lda    ,x+
        cmpa   #'1'
        bne    slast
        bsr    gthex2
        subb   #3
        stb    <bcount
        bsr    gthex2
        stb    <adr
        bsr    gthex2
        stb    <adr+1
        lda    <bcount
        ldy    <adr
sbyte   bsr    gthex2
        stb    ,y+
        deca   
        bgt    sbyte
slast
        bsr    gthex2
        lda    <chksum
        cmpa   #$ff
        bne    err1
err1
        rts

gthex4  pshs   d
        bsr    gthex2
        stb    ,s
        bsr    gthex2
        stb    1,s
        puls   d,pc

gthex2  pshs   b
        bsr    gthex1
        aslb
        aslb
        aslb
        aslb
        stb    ,s
        bsr    gthex1
        addb   ,s
        stb    ,s
        addb   <chksum
        stb    <chksum
        puls   b,pc

gthex1  ldb    ,x+
        subb   #'0'
        blo    rgethex1 
        cmpb   #9
        bls    rgethex1
        subb   #7
rgethex1 
        rts

putchar                        * Output one character in B register.
        PSHS        X,Y
        BRA         OUTCH1
getchar                        * Input one character into B register.
        PSHS        A,B,X,Y
GETCH0
        LDA         #0
        LEAX        1,S
        LDY         #1
        OS9         I$Read
        BCS         GETCH0
        PULS        A,B,X,Y,PC
putcr                          * Output a newline.
        LDB         #C$CR
        bsr         putchar
        LDB         #C$LF
        PSHS        X,Y
OUTCH1  PSHS        A,B
        LEAX        1,S
        LDA         #1
        LDY         #1
        OS9         I$Write
        PULS        A,B,X,Y,PC
getpoll
        PSHS        X,Y,D
        LDA         #0
        LDB         #SS.Ready
        OS9         I$GetStt
        CMPB        #$F6       Not Ready
        BNE         RSENSE
        CLRB
        PULS        X,Y,D,PC
RSENSE
        ORCC        #1        set carry to indicate ready
RNSENSE
        PULS        X,Y,D,PC

getline                        * Input line at address in X, length in B.
        PSHS        A,B,X,Y
        clr         ,s
GETLN0
        ldy         ,s
        clra   
        OS9         I$ReadLn
        BCS         GETLN0
        LEAY        -1,Y
GETLN1  STY         ,S
        PULS        A,B,X,Y,PC
putline                        * Output string at address in X, length in B.
        PSHS        A,B,X,Y
        CLRA
        TFR         D,Y
        CLRA
        OS9         I$WritLn
        PULS        A,B,X,Y,PC
xopenin
xopenout
xabortin
xclosein
xcloseout
        RTS

setecho lda          #1
        bra          sss
noecho  clra
sss     leas         -128,s
        leax        ,s
        pshs         a
        clra  
        ldb          #SS.Opt
        OS9         I$GetStt
        bcs         err2
        LDA         ,s
        STA         PD.EKO-PD.OPT,X
setopts
        ldb         #SS.Opt         
        clra        
        OS9         I$SetStt
err2
        puls        a
        leas        128,s
        rts


delay   PSHS        D,X  * address **$21** 
                         * On input the D register contains the number of timer 
                         * ticks to wait. Each timer tick is 20ms
        TFR         D,X
        OS9         F$Sleep
        PULS        D,X,PC


         emod
eom      equ   *
         end
