home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.barnyard.co.uk
/
2015.02.ftp.barnyard.co.uk.tar
/
ftp.barnyard.co.uk
/
cpm
/
walnut-creek-CDROM
/
ZSYS
/
SIMTEL20
/
SYSLIB
/
SLIB1.LBR
/
SCRC1.Z80
< prev
next >
Wrap
Text File
|
2000-06-30
|
3KB
|
118 lines
;
; SYSLIB Module Name: SCRC1
; Author: Richard Conn
; SYSLIB Version Number: 3.6
; Module Version Number: 1.1
public crc1clr,crc1done,crc1upd
;
; These subroutines will compute and check a true 16-bit
; Cyclic Redundancy Code for a message of arbitrary length.
;
; The use of this scheme will guarantee detection of all
; single and double bit errors, all errors with an odd
; number of error bits, all burst errors of length 16 or
; less, 99.9969% of all 17-bit error bursts, and 99.9984%
; of all possible longer error bursts. (Ref: Computer
; Networks, Andrew S. Tanenbaum, Prentiss-Hall, 1981)
;
; These routines are typically used as follows:
; CRC1$MAKE: ; ROUTINE TO ESTABLISH CRC VALUE
; CALL CRC1CLR ; CLEAR CRC
; <loop CALLing CRC1UPD> ; ACQUIRE VALUES
; CALL CRC1DONE ; GET VALUE
; SHLD CRC1VAL ; SAVE VALUE
; CRC1$CHECK: ; ROUTINE TO CHECK CRC VALUE
; CALL CRC1CLR ; CLEAR CRC1
; <loop CALLing CRC1UPD> ; ACQUIRE VALUES
; CALL CRC1DONE ; NOW DONE
; XCHG ; VALUE IN DE
; LHLD CRC1VAL ; FROM BEFORE
; CALL COMPHD ; COMPARE HL TO DE (SYSLIB ROUTINE)
; JNZ ERROR ; ERROR IF NOT EQUAL
;
;
; CRC1CLR - Clear the CRC accumulator
; This routine must be called at the start of each byte stream.
;
; Input Parameters: None
;
; Output Parameters: None
;
CRC1CLR:
PUSH HL
LD HL,0 ;SET CRC TO ZERO
LD (CRCVAL),HL
POP HL
RET
;
; BUFFER FOR CRC VALUE
;
CRCVAL: DS 2
;
; CRC1UPD - Update the CRC accumulator
; This routine must be called once for each byte in the
; byte stream for which the CRC is being calculated.
;
; Input Parameters: A = byte to be included in CRC
;
; Output Parameters: None
;
CRC1UPD:
PUSH AF ;SAVE ALL REGS
PUSH BC
PUSH HL
LD B,8 ;ROTATE 8 BITS
LD C,A ;BYTE IN C
LD HL,(CRCVAL) ;HL=OLD CRC VALUE
UPDLOOP:
LD A,C ;ROTATE HLC AS A 24-BIT ACC LEFT 1 BIT
RLCA
LD C,A
LD A,L
RLA
LD L,A
LD A,H
RLA
LD H,A
JP NC,SKIPIT
LD A,H ; The generator is X^16 + X^15 + X^2 + 1
XOR 80H ; as recommended by CCITT.
LD H,A ; An alternate generator which is often
LD A,L ; used in synchronous transmission protocols
XOR 05H ; is X^16 + X^12 + X^5 + 1. This may be
LD L,A ; used by substituting XOR 10H for XOR 80H
SKIPIT: ; and XOR 21H for XOR 05H in the adjacent code.
DEC B ;COUNT DOWN 8 BITS
JP NZ,UPDLOOP
LD (CRCVAL),HL ;SAVE NEW CRC VALUE
POP HL ;RESTORE ALL
POP BC
POP AF
RET
;
; CRC1DONE - Complete the CRC calculation
; This routine is called after the last byte of the byte stream
; has passed thru CRC1UPD, and it returns the calculated
; CRC bytes, which must be transmitted as the final
; two bytes of the message (first H, then L).
;
; Input Parameters: None
;
; Output Parameters: HL = calculated CRC bytes
;
CRC1DONE:
PUSH AF ;SAVE A
XOR A ;SEND OUT 2 ZEROES
CALL CRC1UPD
CALL CRC1UPD
LD HL,(CRCVAL) ;RETURN CRC VALUE IN HL
POP AF
RET
END