10
20 :
30
40
50 DEFPROCusb_Sync:REPEATA%=?usb_D:UNTIL(?usb_S AND 128):ENDPROC
60 DEFPROCusb_Flush:IF(?usb_S AND 128):ENDPROC
70 PRINT'"Flushing USB";:REPEAT:REPEATA%=?usb_D:UNTIL(?usb_S AND 128)
80 A%=TIME+20:REPEATUNTILTIME>A%:UNTIL(?usb_S AND 128):ENDPROC
90 DEFFNusb_Err:IF?usb_result%=13 OR usb_result%?1=ASC":":=0 ELSE PRINT"USB Error: "$usb_result%:=TRUE
100 DEFPROCusb_Cmd(A$):PROCusb_WrStr(A$+CHR$13):PROCusb_RdStr:ENDPROC
110 DEFFNusb_Cmd(A$):PROCusb_Cmd(A$):=FNusb_Err
120 DEFPROCusb_WrStr(A$):FOR A%=1 TO LEN A$:REPEATUNTIL(?usb_S AND 64)=0
130 ?usb_D=ASCMID$(A$,A%,1):NEXT:ENDPROC
140 DEFPROCusb_RdStr:A%=usb_result%-1:REPEATA%=A%+1:?A%=FNusb_Rd:UNTIL?A%=13:ENDPROC
150 DEFPROCusb_Wr(A%):REPEATUNTIL(?usb_S AND 64)=0:?usb_D=A%:ENDPROC
160 DEFFNusb_Rd:REPEATUNTIL(?usb_S AND 128)=0:=?usb_D
170 DEFPROCusb_Init
180 usb_D=&FCF8:usb_S=&FCF9
190 usb_result%=usb_result%:IF usb_result%:ENDPROC
200 DIM usb_result% 63,mc% 99
210 FOR P=0 TO 1:P%=mc%:[OPT P*2
220 .usb_RdData
230 TAY:LDA #data% AND 255:STA usb_RdLp+4
240 LDA #data% DIV 256:STA usb_RdLp+5:LDX #0
250 .usb_RdLp:JSR usb_Rd:STA data%,X:INX:BNE usb_RdLp
260 INC usb_RdLp+5:DEY:BNE usb_RdLp:RTS
270 .usb_Rd:BIT usb_S:BPL usb_Rd2:BIT &FF:BPL usb_Rd
280 .usb_Rd2:LDA usb_D:RTS
290 .usb_WrData
300 TAY:LDA #data% AND 255:STA usb_WrLp+1
310 LDA #data% DIV 256:STA usb_WrLp+2:LDX #0
320 .usb_WrLp:LDA data%,X:JSR usb_Wr
330 .usb_WrLp2:LDA usb_D:BIT usb_S:BPL usb_WrLp2
340 INX:BNE usb_WrLp
350 INC usb_WrLp+2:DEY:BNE usb_WrLp:RTS
360 .usb_Wr:BIT usb_S:BVC usb_Wr2:BIT &FF:BPL usb_Wr
370 .usb_Wr2:STA usb_D:RTS
380 ]NEXT:ENDPROC
390 :