; > UnixIO ; General position-independent I/O interface to Bell Unix ; ------------------------------------------------------- ; All registers preserved unless mentioned ; IO_QUIT Quit ; IO_NEWL Write CRLF to output ; IO_WRCH Write character in R0 to output ; IO_RDCH Read character from keyboard to R0, CS=Escape pressed ; IO_WrString Write zero-terminated string from R1 to output ; IO_ReadLine Read string from keyboard to R1, CS=Escape pressed ; Quit program ; ------------ ; On exit, R0=return status ; .IO_QUIT0 clr r0 .IO_QUIT trap 1 ; exit() halt ; In case we come back ; Write character to stdout ; ------------------------- ; IO_ASCI converts to ; IO_NEWL sends ; IO_WRCR sends ; .IO_ASCI ; Print ASCII character cmp r0,#13 bne IO_WRCH .IO_NEWL ; Print NEWLINE mov #10,r0 jsr pc,IO_WRCH .IO_WRCR ; Print CR mov #13,r0 ; Write character to stdout ; ------------------------- ; On entry, R0=character to write ; On exit, R0=preserved ; .IO_WRCH mov r0,-(sp) ; Push r0 mov sp,r0 ; r0=>buffer mov #&0087,-(sp) ; rts pc mov #1,-(sp) ; count mov r0,-(sp) ; buffer mov #&8904,-(sp) ; TRAP 4 = write() mov #1,r0 ; STDOUT=1 jsr pc,(sp) ; Call code on stack add #8,sp ; Drop TRAP call from stack mov (sp)+,r0 ; Pop r0 rts pc ; Read character from stdin ; ------------------------- ; On exit, R0=character read ; CC=ok, CS=Escape pressed ; .IO_RDCH tst -(sp) ; Space for buffer mov sp,r0 ; r0=>buffer mov #&0087,-(sp) ; rts pc mov #1,-(sp) ; count mov r0,-(sp) ; buffer mov #&8903,-(sp) ; TRAP 3 = read() clr r0 ; STDIN=0 jsr pc,(sp) ; Call code on stack add #8,sp ; Drop TRAP call from stack mov (sp)+,r0 ; Get character cmp r0,#27 ; Is it Escape? clc ; CC=Not Escape bne IO_RDCHok sec ; CS=Escape .IO_RDCHok rts pc ; Print text at R1 ; ---------------- ; On entry, R1=>zero-terminated string ; .IO_WrStrLp jsr pc,IO_ASCI .IO_WrString ; Print text pointed to by R1, term. by &00 movb (r1)+,r0 ; Get byte from r1, inc r1 bne IO_WrStrLp ; Not final byte, print it rts pc ; and exit ; Read line of text ; ----------------- ; On entry, R1=>memory to read string to ; On exit, CC=ok, CS=Escape pressed ; R0 corrupted ; R1 preserved ; R2=length of string ; .IO_ReadLine mov r3,-(sp) ; Save R3 mov r1,-(sp) ; Save pointer to buffer clr r2 ; Zero number of characters read .IO_ReadLnLp jsr pc,IO_RDCH ; Get a character bcs IO_ReadLnEsc ; Escape state cmp r0,#13 beq IO_ReadLnCR ; - End of line cmp r0,#10 beq IO_ReadLnCR ; - End of line cmp r0,#21 beq IO_ReadLnU ; Ctrl-U - delete line cmp r0,#127 beq IO_ReadLnDel ; - del a character cmp r0,#8 beq IO_ReadLnDel ; - del a character cmp r2,#240 bcc IO_ReadLnLp ; No more room for characters jsr pc,IO_WRCH ; Output the character cmp r0,#ASC" " bcs IO_ReadLnLp ; Ignore control characters movb r0,(r1)+ ; Put character into memory inc r2 ; Inc. number of characters br IO_ReadLnLp ; Go back for another ; .IO_ReadLnU mov r2,r3 ; We want to delete all characters br IO_ReadLnDelete .IO_ReadLnDel mov #1,r3 ; We only want to delete one char .IO_ReadLnDelete mov #&2008,r0 ; Output by backspacing .IO_ReadLnDelp tst r2 ; Check line length beq RdLnLoop ; Length=0, jump back to main loop jsr pc,IO_WRCH ; swab r0 jsr pc,IO_WRCH ; swab r0 jsr pc,IO_WRCH ; again dec r1 ; Back address pointer dec r2 ; Dec character counter dec r3 ; Dec number to delete bne IO_ReadLnDelLp ; Delete more br IO_ReadLnLp ; Go back into ReadLine loop ; .IO_ReadLnEsc ; Will enter here with CS mov #0,r2 ; Length = 0, preserve Carry .IO_ReadLnCR ; Will enter here with CC rol r3 ; R3=Carry jsr pc,IO_NEWL ; Print newline clrb (r1)+ ; Put <00> terminator in ror r3 ; Carry=Escape perssed mov (sp)+,r1 ; Get buffer address back mov (sp)+,r3 ; Restore R3 rts pc ; r0=, r1=>buffer, r2=length