RCBEQT EQU 0
RCBGDT EQU 2
RCBSTA EQU 5
RCBDTT EQU 6
RCBDBA EQU 7
* STATUS EQU'S *
BUSY EQU $80
NTBUSY EQU $7F
* SWIHDR SWI INPUT TO CP/68 *
SWIHDR TSX
 LDX 5,X
 CLRA
 LDAB 0,X
 CMPB #46
 BLS *+5
 JMP $E113
 ASLB
 ROLA
 BSR START
START TSX
 ADDB 1,X
 ADCA 0,X
 ADDB #DSPTAB
 ADCA #DSPTAB/256
 STAA 0,X
 STAB 1,X
 LDX 0,X
 ADDB 1,X
 ADCA 0,X
 TSX
 STAA 0,X
 STAB 1,X
 LDAA 2,X
 TAP
 LDX 0,X
 INS
 INS
 JSR 0,X
 TSX
 INC 6,X
 BNE *+4
 INC 5,X
 RTI
* USER REGISTER EQUATES *
UC EQU 2
UB EQU 3
UA EQU 4
UXH EQU 5
UXL EQU 6
URH EQU 7
URL EQU 8
* PUSALL REGISTERS *
PSHAL DES
 DES
 DES
 DES
 DES
 LDAB #9
 TSX
PSHALS LDAA 5,X
 STAA 0,X
 INX
 DECB
 BNE PSHALS
 LDAB #5
 TSX
PSHALC LDAA UC,X
 STAA UC+7,X
 INX
 DECB
 BNE PSHALC
 RTS
* PULL ALL REGISTERS *
PULAL TSX
 LDAB #5
PULAC LDAA UC+7,X
 STAA UC,X
 INX
 DECB
 BNE PULAC
 LDAB #9
PULALS LDAA URL-5,X
 STAA URL,X
 DEX
 DECB
 BNE PULALS
 INS
 INS
 INS
 INS
 INS
 RTS
* TRANSFER  X TO A,B
TXAB TSX
 LDAA UXH,X
 LDAB UXL,X
STAB STAA UA,X
 STAB UB,X
 RTS
* TRANSFER A,B TO X *
TABX TSX
 LDAA UA,X
 STAA UXH,X
 LDAA UB,X
 STAA UXL,X
 RTS
* EXCHANGE A,B AND X *
XABX TSX
 LDAA UXH,X
 PSHA
 LDAB UXL,X
 BSR TABX+1
 PULA
 BRA STAB
* PUSH X *
PSHX DES
 DES
 TSX
 LDAA #9
PSHXA LDAB 2,X
 STAB 0,X
 INX
 DECA
 BNE PSHXA
 TSX
 LDAA UXH,X
 STAA UXH+4,X
 LDAA UXL,X
 STAA UXL+4,X
 RTS
* PULX *
PULX TSX
 LDAA UXH+4,X
 STAA UXH,X
 LDAA UXL+4,X
 STAA UXL,X
 LDAA #9
PULXA LDAB 8,X
 STAB 10,X
 DEX
 DECA
 BNE PULXA
 INS
 INS
 RTS
* ADD X TO A,B *
ADXAB TSX
 BSR XABX+1
 BSR ADABX+1
 BSR XABX+1
* ADD A,B TO X *
ADABX TSX
 LDAA UB,X
 LDAB UA,X
ADDAB ADDA UXL,X
 STAA UXL,X
 ADCB UXH,X
STAUXH TPA
 STAB UXH,X
 TST UXL,X
TESTZ BEQ TESTZA
 ANDA #$FB
TESTZA STAA UC,X
 RTS
* ADD A TO X *
ADDAX TSX
 LDAA UA,X
ADDZ LDAB #0
 BRA ADDAB
* ADD B TO X *
ADDBX TSX
 LDAA UB,X
 BRA ADDZ
SBXAB TSX
 BSR XABX+1
 BSR SBABX+1
 BRA XABX+1
* SUBTRACT A,B FROM X *
SBABX TSX
 LDAB UXH,X
 LDAA UXL,X
 SUBA UB,X
 STAA UXL,X
 SBCB UA,X
 BRA STAUXH
* SUBTRACT A FROM X *
SUBAX TSX
 LDAB UA,X
SUB LDAA UXL,X
 SBA
 STAA UXL,X
 LDAB UXH,X
 SBCB #0
 BRA STAUXH
* SUBTRACT B FROM X *
SUBBX TSX
 LDAB UB,X
 BRA SUB
* INDEX X=X+A*B
INDEX BSR MPY8
 PSHB
 TAB
 PULA
 TSX
 BRA ADDAB
* MULTIPLY 8 *
MUL8 BSR MPY8
 TSX
 STAB UB,X
 STAA UA,X
 TPA
 TSTB
JMPTZ BRA TESTZ
* MULTIPLY 16 *
MUL16 LDAA #16
 PSHA
 TSX
 CLRA
 CLRB
 ROR UXH+1,X
 ROR UXL+1,X
MUL16L BCC MUL16S
 ADDB UB+1,X
 ADCA UA+1,X
MUL16S RORA
 RORB
 ROR UXH+1,X
 ROR UXL+1,X
 DEC 0,X
 BNE MUL16L
 INS
 TSX
 STAB UB,X
 STAA UA,X
 TPA
 ORAB UXH,X
 ORAA UXL,X
 BRA JMPTZ
* MPY 8 *
MPY8 LDAA #8
 PSHA
 CLRA
 TSX
 LDAB UB+3,X
 RORB
MPY8L BCC MPY8S
 ADDA UA+3,X
MPY8S RORA
 RORB
 DEC 0,X
 BNE MPY8L
 INS
 RTS
* MOVC *
FROM EQU 9
TOO EQU 11
MOVC TSX
 LDAB UB,X
MOVC1 LDX FROM,X
 LDAA 0,X
 TSX
 LDX TOO,X
 STAA 0,X
 TSX
 DECB
 BEQ MOVC3
 INC FROM+1,X
 BNE MOVC2
 INC FROM,X
MOVC2 INC TOO+1,X
 BNE MOVC1
 INC TOO,X
 BRA MOVC1
MOVC3 RTS
* COMPARE TWO STRINGS *
PARM1 EQU 9
PARM2 EQU 11
CMPC TSX
 LDAB UB,X
CMP0 LDX PARM1,X
 LDAA 0,X
 TSX
 LDX PARM2,X
 CMPA 0,X
 BNE CDONE
 TSX
 INC PARM1+1,X
 BNE CMP1
 INC PARM1,X
CMP1 INC PARM2+1,X
 BNE CMP2
 INC PARM2,X
CMP2 DECB
 BNE CMP0
CDONE TSX
 TPA
 STAA UC,X
 STAB UB,X
 RTS
* MOVE STRING *
MOVS TSX
MOVS1 LDX FROM,X
 LDAA 0,X
 TSX
 LDX TOO,X
 STAA 0,X
 TSX
 CMPA #4
 BEQ MOVS3
 INC FROM+1,X
 BNE MOVS2
 INC FROM,X
MOVS2 INC TOO+1,X
 BNE MOVS1
 INC TOO,X
 BRA MOVS1
MOVS3 RTS
* DISPATCH TABLE *
DSPTAB EQU *-START
 FDB *-PSHAL*$FFFF
 FDB *-PULAL*$FFFF
 FDB *-TXAB*$FFFF
 FDB *-TABX*$FFFF
 FDB *-PSHX*$FFFF
 FDB *-PULX*$FFFF
 FDB *-ADXAB*$FFFF
 FDB *-ADABX*$FFFF
 FDB *-ADDAX*$FFFF
 FDB *-ADDBX*$FFFF
 FDB *-SBXAB*$FFFF
 FDB *-SBABX*$FFFF
 FDB *-SUBAX*$FFFF
 FDB *-SUBBX*$FFFF
 FDB *-MUL8*$FFFF
 FDB *-MUL16*$FFFF
 FDB *-MOVC*$FFFF
 FDB *-CMPC*$FFFF
 FDB *-IOHDR*$FFFF
 FDB *-OPEN*$FFFF
 FDB *-CLOSE*$FFFF
 FDB *-REWD*$FFFF
 FDB *-OPEND*$FFFF
 FDB *-READ*$FFFF
 FDB *-WRITE*$FFFF
 FDB *-GETDR*$FFFF
 FDB *-PUTDR*$FFFF
 FDB *-DELET*$FFFF
 FDB *-RENAM*$FFFF
 FDB *-APPND*$FFFF
 FDB *-OPENR*$FFFF
 FDB *-CLOSR*$FFFF
 FDB *-READR*$FFFF
 FDB *-WRITR*$FFFF
 FDB *-CRETR*$FFFF
 FDB *-POSTR*$FFFF
 FDB *-LOADB*$FFFF
 FDB *-LOADR*$FFFF
 FDB *-USR1*$FFFF
 FDB *-USR2*$FFFF
 FDB *-USR3*$FFFF
 FDB *-USR4*$FFFF
 FDB *-USR5*$FFFF
 FDB *-USR6*$FFFF
 FDB *-MOVS*$FFFF
 FDB *-INDEX*$FFFF
* SET PARAMETERS *
SETAB EQU *
BS FCB $8
DL FCB $15
CS FCB $3B
DP FCB $0
WD FCB $0
NL FCB $0
TB FCB $9
DX FCB $0
EJ FCB $0
PS FCB $FF
ES FCB $1B
* EQUIPMENT TABLE *
EQTAB EQU *
CONSOL FDB INLIN
 FDB OTLIN
 FDB $8008
PTRDR FDB INRDR
 FDB NULL
 FDB $8010
PTPCH FDB NULL
 FDB OTPCH
 FDB $8010
DISK FDB RDSEC
 FDB WTSEC
 FDB 0
LPTR FDB NULL
 FDB OTLPT
 FDB $8002
MTAPE FDB MTIN
 FDB MTOT
 FDB 0
TTYIO FDB INLIN
 FDB OTLIN
 FDB $8010
NULLIO FDB NULL
 FDB NULL
 FDB 0000
* PHYSICAL DEVICE TABLE *
PDTAB FCC /CON/
 FDB CONSOL
 FCC /PTR/
 FDB PTRDR
 FCC /PTP/
 FDB PTPCH
 FCC /DSK/
 FDB DISK
 FCC /LPT/
 FDB LPTR
 FCC /MTA/
 FDB MTAPE
 FCC /TTY/
 FDB TTYIO
 FCC /NUL/
 FDB NULLIO
 FCB 0
* I/O HANDLER *
IOHDR TSX
 LDX UXH,X
 LDAA #BUSY
 STAA RCBSTA,X
 JSR PDSRCH
 BCS IOHDRA
 SWI
 FCB 4
 JSR 0,X
IOHDRA LDAA #NTBUSY
 ANDA RCBSTA,X
 STAA RCBSTA,X
 RTS
* SEARCH PDTAB FOR DEVICE *
PDSRCH SWI
 FCB 5
 INX
 INX
 SWI
 FCB 5
 LDX #PDTAB
PDSRCA SWI
 FCB 5
 LDAB #3
 SWI
 FCB 18
 BEQ PDSRCB
 SWI
 FCB 6
 SWI
 FCB 10
 INX
 INX
 SWI
 FCB 2
 SWI
 FCB 6
 SWI
 FCB 6
 SWI
 FCB 5
 INX
 INX
 SWI
 FCB 5
 SWI
 FCB 3
 TST 0,X
 BNE PDSRCAp SWI
 FCB 6
 SWI
 FCB 6
 SEC
 LDAA #5
 ANDA RCBSTA,X
 STAA RCBSTA,X
 RTS
PDSRCB SWI
 FCB 6
 LDX 0,X
 SWI
 FCB 2
 SWI
 FCB 6
 SWI
 FCB 6
 SWI
 FCB 5
 STAA RCBEQT,X
 STAB RCBEQT+1,X
 TST RCBDTT,X
 BPL *+6
 ADDB #2
 ADCA #0
 SWI
 FCB 3
 LDX 0,X
 SWI
 FCB 2
 SWI
 FCB 2
 CLC
 RTS
*NULL *
NULL SWI
 FCB 4
 RTS
* INPUT A LINE FROM THE CONSOLE *
INLIN SWI
 FCB 3
INLIN1 SWI
 FCB 5
 LDX RCBDBA,X
 LDAA #$2E
 JSR OUTCON
INLIN2 JSR INCON
 CMPA #$A
 BEQ INLIN2
 TST DX
 BNE *+5
 JSR OUTCON
 CMPA DL
 BNE INLIN3
 SWI
 FCB 6
 BRA INLIN1
INLIN3 CMPA BS
 BNE INLIN4
 SWI
 FCB 2
 SWI
 FCB 6
 CMPA RCBDBA,X
 BNE *+6
 CMPB RCBDBA+1,X
 BEQ INLIN5
 SUBB #1
 SBCA #0
INLIN5 SWI
 FCB 5
 SWI
 FCB 3
 BRA INLIN2
INLIN4 STAA 0,X
 INX
 CMPA #$D
 BNE INLIN2
INLIN6 LDAA #$A
 JSR OUTCON
 SWI
 FCB 6
 RTS
* OUTPUT A LINE TO CONSOLE *
OTLIN SWI
 FCB 3
 SWI
 FCB 5
 LDX RCBDBA,X
OTLIN1 LDAA 0,X
 JSR OUTCON
 INX
 CMPA #$D
 BNE OTLIN1
 LDAA #$A
 JSR OUTCON
 SWI
 FCB 6
 RTS
* GET CHAR FROM CONSOLE *
INCON SWI
 FCB 5
 TSX
 LDX 4,X
 LDX RCBEQT,X
 LDX 4,X
INCON1 LDAA 0,X
 ASRA
 BCC INCON1
 LDAA 1,X
 ANDA #$7F
 SWI
 FCB 6
  RTS
* OUTPUT A CHAR TO CONSOLE *
OUTCON SWI
 FCB 5
 TSX
 LDX 4,X
 LDX RCBEQT,X
 LDX 4,X
 PSHB
OUTCO1 LDAB 0,X
 ASRB
 ASRB
 BCC OUTCO1
 STAA 1,X
 PULB
 SWI
 FCB 6
 RTS
* READ A LINE FROM PAPER TAPE *
INRDR SWI
 FCB 3
 SWI
 FCB 5
 LDX  RCBDBA,X
 LDAA #$11
 JSR OUTPCH
INRD1 JSR RDRIN
 CMPA #$A
 BEQ INRD1
 CMPA #0
 BEQ INRD1
 STAA 0,X
 INX
 CMPA #$D
 BNE INRD1
INRD2 LDAA #$13
 JSR OUTPCH
 SWI
 FCB 6
 RTS
* READ A CHAR FROM PAPER TAPE *
RDRIN SWI
 FCB 5
 TSX
 LDX 4,X
 LDX RCBEQT,X
 LDX 4,X
RDRIN1 LDAA 0,X
 ASRA
 BCC RDRIN1
 LDAA 1,X
 SWI
 FCB 6
 RTS
* PUNCH A LINE OF PAPER TAPE *
OTPCH SWI
 FCB 3
 SWI
 FCB 5
 LDX RCBDBA,X
OTPCH1 LDAA 0,X
 INX
 JSR OUTPCH
 CMPA #$D
 BNE OTPCH1
 LDAA #$A
 JSR OUTPCH
 LDAB #4
OTPCH3 LDAA #0
 JSR OUTPCH
 DECB
 BNE OTPCH3
 SWI
 FCB 6
* PUNCH A CHAR ON PAPER TAPE *
OUTPCH SWI
 FCB 5
 TSX
 LDX 4,X
 LDX RCBEQT,X
 LDX 4,X
 PSHB
OUTPC1 LDAB 0,X
 ASRB
 ASRB
 BCC OUTPC1
 STAA 1,X
 PULB
 SWI
 FCB 6
 RTS
* OUTPUT A LINE TO PRINTER *
OTLPT SWI
 FCB 3
 SWI
 FCB 5
 LDX RCBDBA,X
OTLPT1 LDAA 0,X
 INX
 JSR OUTLPT
 CMPA #$D
 BNE OTLPT1
 LDAA #$A
 JSR OUTLPT
 SWI
 FCB 6
 RTS
* PRINT A CHAR ON LINE PRINTER *
OUTLPT SWI
 FCB 5
 TSX
 LDX 4,X
 LDX RCBEQT,X
 LDX 4,X
 PSH B
 LDAB 0,X
 STAA 0,X
 LDAB 1,X
 BPL *-2
 PULB
 SWI
 FCB 6
 RTS
 END
