
.TITL   HANDLER

.PROC   GETBUF < FUNCTION GETBUF( SOH, EOP, TIMEOUT : INTEGER;
                                  VAR S        : STRING ):BOOLEAN; >

;-----------------------------------------------------------;
;                                                           ;
; written by H Balen    March 1986                          ;
;                                                           ;
; This is a microcode routine to receive a packet for the   ;
; Magiscans KERMIT program.                                 ;
;                                                           ;
; SOH = 'my_soh' start of packet                            ;
; EOP = 'my_eop' end of the packet                          ;
; TIMEOUT = number of loops before giving up                ;
; S = the buffer in which to store the data                 ;
;                                                           ;
;                                                           ;
;-----------------------------------------------------------;


.REG EOP
.REG SOH
.REG STRPTR
.REG INDPSN
.REG WPSN
.REG CBYTE
.REG VALUE
.REG WRDPTR
.REG TCOUNT
.REG TIMOUT

GETBUF: NOP                     :JSR DUMP2      ; Zero the count
        ZER TCOUNT              :JSR ACPOP      ; and the posn
        MOV AC,STRPTR           :JSR ACPOP      ; Set the string and word pointers
        MOV AC,TIMOUT           :JSR ACPOP      ; get wait
        MOV AC,EOP              :JSR ACPOP      ; get special characters
        MOV AC,SOH
 
LAB1:   ZER INDPSN
        MOV STRPTR,AC
        MOV AC,WRDPTR

LOOP:   INC TCOUNT                              ; check the time out
        MOV TIMOUT,AC
        SUB AC,COUNT,#
        MOV %0004,AC            :JMP LEAVE ZR
        SUB AC,C16,RMSK                         ; check the status register
        MOV C255,AC             :JSR STATSET
        AND IO(RS),C1,AC
        NOP                     :JMP LOOP NZ
 
        MOV %0038,IOA                           ; read the port
        MOV IO,AC
        AND AC,%7F,AC
 
        SUB AC,SOH,#                            ; check the special chars
        SUB AC,EOP,#            :JMP LAB1 ZR
        MOV AC,CBYTE            :JMP PEND ZR
 
        NOP                     :JSR STORUP     ; store the byte
        NOP                     :JMP LOOP       ; continue to loop

PEND:   MOV STRPTR,MAF                          ; routine to leave the microcode procedure
        MOV MM,AC                               ; store the length of the string
        AND AC,%FF00,AC
        MOV AC,VALUE
        MOV INDPSN,AC
        AND AC,%00FF,AC
        OR  AC,VALUE,AC
        MOV AC,MM
        MOV C1,AC
FEND:   NOP                     :JSR ACPUSH
        NOP                     :JMP ENDIPC
LEAVE:  ZER AC                  :JMP FEND

STORUP: INC INDPSN                              ; find the index
        MOV INDPSN,AC
        MOV WRDPTR,MAF
        AND AC,C1,#                             ; if the index is odd then store in high byte of word
        MOV MM,AC               :JMP ODD NZ
        AND AC,%FF00,AC                         ; else store in the low byte
        MOV AC,VALUE
        MOV CBYTE,AC
        AND AC,%00FF,AC
        OR  AC,VALUE,AC
        MOV AC,MM               :RET
 
ODD:    AND AC,%00FF,AC                         ; store in high byte
        MOV AC,VALUE
        MOV CBYTE,AC
        AND AC(8L),%FF00,AC
        OR  AC,VALUE,AC
        MOV AC,MM
        INC WRDPTR              :RET

