\ 6809 TUBE PARASITE MOS CODE
\ ===========================
\ J.G.HARSTON



TUBE1S: EQU &FEF0
TUBE1:  EQU &FEF1
TUBE2S: EQU &FEF2
TUBE2:  EQU &FEF3
TUBE3S: EQU &FEF4
TUBE3:  EQU &FEF5
TUBE4S: EQU &FEF6
TUBE4:  EQU &FEF7


ORG &F800
RESET:
\ INITILAISE VECTORS, ETC
JMP STARTUP


\ STARTUP
\ =======
BANNER:
EQUB 10
EQUS "ACORN TUBE 6809 64K 0.10"
EQUB 10
EQUB 10
EQUB 13
EQUB 0
STARTUP:
LDX  #BANNER  \ POINT TO STARTUP BANNER
JSR  SEND_TXT \ PRINT IT VIA TUBE WRCH PROTOCOL
BRA  CLI_WAIT \ WAIT FOR RESULT BYTE


\ OSCLI - SEND COMMAND LINE TO HOST
\ =================================
CLI:
LDA #2
JSR SEND_R2 \ SEND COMMAND &02 = OSCLI
JSR SEND_STR \ SEND STRING AT X
CLI_WAIT:
JSR  GET_R2 \ WAIT FOR RESULT VIA TUBE R2
CMPA #&80   \ CHECK RETURN CODE
BNE  CLI_DONE \ NOTHING TO EXECUTE, RETURN
JSR [ADDRESS] \ CALL PROGRAM
CLI_DONE:
RTS PC


\ OSWRCH - SEND CHAR TO TUBE R1
\ =============================
WRCH:
PSHS A         \ SAVE A
SEND_R1LP:
LDA  >TUBE1S   \ READ TUBE R1 STATUS
ANDA #&40      \ CHECK B6 OF STATUS
BEQ  SEND_R1LP \ LOOP UNTIL B6 SET
PULS A         \ GET A BACK
STA  >TUBE1    \ SEND BYTE TO TUBE R1
RTS

\ PRINT STRING AT X
\ =================
SEND_TXT:
LDA  X+       \ GET BYTE FROM X, INCREMENT X
JSR  OSASCI   \ SEND TO OSASCI
CMPA #0       \ TEST CURRENT CHARACTER
BNE  SEND_TXT \ LOOP UNTIL &00 SENT
RTS


\ SEND BYTE TO TUBE R2
\ ====================
SEND_R2:
PSHS A         \ SAVE A
SEND_R2LP:
LDA  >TUBE2S   \ READ TUBE R2 STATUS
ANDA #&40      \ CHECK B6 OF STATUS
BEQ  SEND_R2LP \ LOOP UNTIL B6 SET
PULS A         \ GET A BACK
STA  >TUBE2    \ SEND BYTE TO TUBE R2
RTS


\ SEND <CR>-STRING AT X TO TUBE R2
\ ================================
SEND_STR:
LDA  X+        \ GET BYTE FROM X, INCREMENT X
JSR  SEND_R2   \ SEND BYTE VIA TUBE R2
CMPA #13       \ TEST CURRENT CHARACTER
BNE  SEND_STR  \ LOOP UNTIL <CR> SENT
RTS


\ SEND BLOCK AT X TO TUBE R2, Y=BLOCK LENGTH
\ ==========================================
SEND_BLK:
TFR  X,A
ADDA Y         \ Y POINTS TO END OF BLOCK+1
SEND_LP:
LDA  -X        \ DECREMENT X, GET BYTE FROM X
JSR  SEND_R2   \ SEND BYTE VIA TUBE R2
DEY            \ DECREMENT COUNT OF BYTES TO SEND
BNE  SEND_LP   \ LOOP UNTIL ALL BYTES SENT
RTS


\ OSRDCH - WAIT FOR CHARACTER FROM INPUT STREAM
\ =============================================
RDCH:
LDA  #0
JSR  SEND_R2 \ SEND COMMAND &00 - OSRDCH
GET_CHAR:
JSR  GET_R2  \ GET RETURNED BYTE
ADDA #&80    \ COPY B7 INTO CARRY
\ CONTINUE TO FETCH BYTE FROM TUBE R2

GET_R2:
LDA  >TUBE2S   \ READ TUBE R2 STATUS
BPL  GET_R2    \ WAIT FOR B7 SET
LDA  >TUBE2    \ GET BYTE FROM TUBE R2
RTS


\ WAIT FOR BLOCK AT R1 FROM TUBE R2, Y=BLOCK LENGTH
\ =================================================
GET_BLK:
TFR  X,A
ADDA Y         \ Y POINTS TO END OF BLOCK+1
GET_LP:
JSR  GET_R2    \ GET BYTE VIA TUBE R2
STA  -X        \ DECREMENT X, STORE BYTE AT X
DEY            \ DECREMENT COUNT OF BYTES
BNE  GET_LP    \ LOOP UNTIL ALL BYTES SENT
RTS


\ OSBYTE
\ ======
\ ON ENTRY: A,X,Y=OSBYTE PARAMETERS
\ ON EXIT:  A PRESERVED
\ IF A<&80, X=RETURNED VALUE
\ IF A>&7F, A,X,Y,CARRY=RETURNED VALUES
\
BYTE:
CMPA #&80
BCC  BYTE_HI
PSHS A
LDA #4
JSR  SEND_R2 \ SEND COMMAND &04 - SHORT BYTE
TFR X,A
JSR SEND_R2 \ SEND SECOND PARAMETER
PULS A
PSHS A
JSR SEND_R2 \ SEND FIRST PARAMETER
JSR GET_R2 \ WAIT FOR RESPONSE
TFR A,X
PULS A
RTS

\ OSBYTE >&7F
\ -----------
BYTE_HI:
CMPA #&82
BEQ  MEM82 \ FETCH ADDRESS HIGH WORD
CMPA #&83
BEQ  MEM83 \ FETCH LOW MEMORY LIMIT
CMPA #&84
BEQ  MEM84 \ FETCH HIGH MEMORY LIMIT
PSHS A
LDA #6
JSR SEND_R2 \ SEND COMMAND &06 - LONG BYTE
TFX Y,A
JSR SEND_R2 \ SEND THIRD PARAMETER
TFR X,A
JSR SEND_R2 \ SEND SECOND PARAMETER
PULS A
PSHS A
JSR SEND_R2 \ SEND FIRST PARAMETER
CMPA #&9D \ WAS IT FAST BPUT?
BEQ BYTE_DONE \ DON'T WAIT FOR RESPSONSE
JSR GET_R2 \ WAIT FOR RESPONSE
ADDA #&80 \ COPY B7 INTO CARRY
PSHS P
JSR GET_R2 \ WAIT FOR RESPONSE
TFR A,Y
JSR GET_R2 \ WAIT FOR RESPONSE
TFR A,X
PULS P
BYTE_DONE:
PULS A
RTS

MEM82: \ MEMORY ADDRESS HIGH WORD
LDX  #0
RTS

MEM83: \ LOWEST AVAILABLE MEMORY ADDRESS
LDX [MEMBOT]
RTS

MEM84: \ HIGHEST AVAILABLE MEMORY ADDRESS
LDX [MEMTOP]
RTS

\ OSWORD A=ACTION, X=>BLOCK
\ =========================
CMPA #0
BEQ  RDLINE
RTS
\PSHS A
\LDA #8
\JSR SEND_R2 \ SEND COMMAND &08 - OSWORD
\
\ SEND PARAM BLOCK, ETC.
\
\RTS

\ OSWORD 0
\ --------
RDLINE:
LDA #10
JSR SEND_R2 \ SEND COMMAND &0A - RDLINE
INC X
INC X
LDY #3
JSR SEND_BLK \ SEND 3-BYTE CONTROL BLOCK
DEC X
DEC X
LDA #7
JSR SEND_R2 \ SEND &0700
LDA #0
JSR SEND_R2
JSR GET_R2 \ WAIT FOR RESPONSE
ADDA #&80 \ COPY B7 INTO CARRY
BCS  RD_DONE
LDA  X
READ_STR:
LDY #0 \ Y=NUMBER RECEIVED
JSR GET_R2 \ WAIT FOR BYTE FROM TUBE R2
STA X+
INC Y
CMPA #13 \ CHECK CURRENT BYTE
BNE  READ_STR \ LOOP UNTIL <CR>
DEC Y
CLC \ CLEAR CARRY
RD_DONE:
RTS



\ OSARGS - A=ACTION, X=>DATA, Y=HANDLE
\ ====================================
ARGS:
RTS
\ OSBGET - Y=HANDLE
\ =================
RTS
\ OSBPUT - A=BYTE, Y=HANDLE
\ =========================
RTS
\ OSFIND - A=ACTION, X=>STRING, Y=HANDLE
\ ======================================
RTS
\ OSFILE - A=ACTION, X=>CONTROL BLOCK
\ ===================================
RTS
\ OSGBPB - A=ACTION, X=>CONTROL BLOCK
\ ===================================
RTS

SWI:
\ STACK=CC,A,B,DP,Xhi,Xlo,Yhi,Ylo,Uhi,Ulo,PChi,PClo
LDX 10,S \ GET STACKED PC
LDA X    \ GET FOLLOWING BYTE
INX
STX 10,S \ UPDATE STACKED PC
CMP #16
BHI SWIEXIT
JSR DISPATCH
LEAS 8,S \ CLEAR STACK
PSHS CC,A,B,DP,X,Y \ STACK RETURN VALUES
SWIEXIT:
RTI      \ RETURN, PULLING RETURN VALUES

DISPATCH:
ASL A
LDX [SWITABLE,A]
PSHS X \ STACK ADDRESS
LDX 4,S \ RESTORE X
LDA 1,S \ RESTORE Y
RTS     \ JUMP TO ROUTINE

SWITABLE
EQUW SWI0
EQUW SWI1
EQUW SWI2
ETC


ORG &FFCE
OSFIND: JMP FIND \ &FFCE
OSGBPB: JMP FIND \ &FFD1
OSBGET: JMP FIND \ &FFD4
OSBPUT: JMP FIND \ &FFD7
OSARGS: JMP FIND \ &FFDA
OSFILE: JMP FIND \ &FFDD
OSRDCH: JMP RDCH \ &FFE0
OSASCI: CMPA #13 \ &FFE3
        BNE OSWRCH
OSNEWL: LDA #10  \ &FFE7
        JSR OSWRCH
OSWRCR: LDA #13  \ &FFEC

\ THE FOLLOWING OVERLAP RESV, SWI2V, SWI3V, FIQV, SO CAN'T NOT BE USED
\
\OSWRCH: \JMP WRCH \ &FFEE
\OSWORD: \JMP WORD \ &FFF1
\OSBYTE: \JMP BYTE \ &FFF4
\OS_CLI: \JMP IRQCLI   \ &FFF7

ORG &FFF0
RESV:   EQUW  RES    \ &FFF0
SWI2V:  EQUW  SWI2   \ &FFF2
SWI3V:  EQUW  SWI3   \ &FFF4
FIRQV:  EQUW  FIRQ   \ &FFF6
IRQV:   EQUW  IRQ    \ &FFF8
NMIV:   EQUW  NMI    \ &FFFA
SWIV:   EQUW  SWI    \ &FFFC
RESETV: EQUW  RESET  \ &FFFE

