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 / SCRC.Z80 < prev    next >
Text File  |  2000-06-30  |  3KB  |  118 lines

  1. ;
  2. ; SYSLIB Module Name:  SCRC
  3. ; Author:  Richard Conn
  4. ; SYSLIB Version Number:  3.6
  5. ; Module Version Number:  1.1
  6.  
  7.     public    crcclr,crcdone,crcupd
  8.  
  9. ;
  10. ;         These subroutines will compute and check a true 16-bit
  11. ;    Cyclic Redundancy Code for a message of arbitrary length.
  12. ;
  13. ;    The  use  of this scheme will guarantee detection of all
  14. ;    single and double bit errors, all  errors  with  an  odd
  15. ;    number  of  error bits, all burst errors of length 16 or
  16. ;    less, 99.9969% of all 17-bit error bursts, and  99.9984%
  17. ;    of  all  possible  longer  error bursts.  (Ref: Computer
  18. ;    Networks, Andrew S.  Tanenbaum, Prentiss-Hall, 1981)
  19. ;
  20. ;    These routines are typically used as follows:
  21. ;      CRC$MAKE:        ; ROUTINE TO ESTABLISH CRC VALUE
  22. ;        CALL    CRCCLR    ; CLEAR CRC
  23. ;        <loop CALLing CRCUPD>    ; ACQUIRE VALUES
  24. ;        CALL    CRCDONE    ; GET VALUE
  25. ;        SHLD    CRCVAL    ; SAVE VALUE
  26. ;      CRC$CHECK:        ; ROUTINE TO CHECK CRC VALUE
  27. ;        CALL    CRCCLR    ; CLEAR CRC
  28. ;        <loop CALLing CRCUPD>    ; ACQUIRE VALUES
  29. ;        CALL    CRCDONE    ; DONE
  30. ;        XCHG        ; VALUE IN DE
  31. ;        LHLD    CRCVAL    ; FROM BEFORE
  32. ;        CALL    COMPHD    ; COMPARE HL TO DE (SYSLIB ROUTINE)
  33. ;        JNZ    ERROR    ; ERROR IF NOT SAME
  34. ;
  35.  
  36. ;
  37. ; CRCCLR - Clear the CRC accumulator
  38. ;     This routine must be called at the start of each byte stream.
  39. ;
  40. ;     Input Parameters: None
  41. ;
  42. ;     Output Parameters: None
  43. ;
  44. CRCCLR:
  45.     PUSH    HL
  46.     LD    HL,0    ;SET CRC TO ZERO
  47.     LD    (CRCVAL),HL
  48.     POP    HL
  49.     RET
  50.  
  51. ;
  52. ;  BUFFER FOR CRC VALUE
  53. ;
  54. CRCVAL:    DS    2
  55.  
  56. ;
  57. ; CRCUPD - Update the CRC accumulator
  58. ;     This routine must be called once for each byte in the
  59. ;     byte stream for which the CRC is being calculated.
  60. ;
  61. ;     Input Parameters: A = byte to be included in CRC
  62. ;
  63. ;     Output Parameters: None
  64. ;
  65. CRCUPD:
  66.     PUSH    AF    ;SAVE ALL REGS
  67.     PUSH    BC
  68.     PUSH    HL
  69.     LD    B,8    ;ROTATE 8 BITS
  70.     LD    C,A    ;BYTE IN C
  71.     LD    HL,(CRCVAL)    ;HL=OLD CRC VALUE
  72. UPDLOOP:
  73.     LD    A,C    ;ROTATE HLC AS A 24-BIT ACC LEFT 1 BIT
  74.     RLCA
  75.     LD    C,A
  76.     LD    A,L
  77.     RLA
  78.     LD    L,A
  79.     LD    A,H
  80.     RLA
  81.     LD    H,A
  82.     JP    NC,SKIPIT
  83.     LD    A,H        ; The generator is X^16 + X^12 + X^5 + 1
  84.     XOR    10H        ; as recommended by CCITT.
  85.     LD    H,A        ; An alternate generator which is often
  86.     LD    A,L        ; used in synchronous transmission protocols
  87.     XOR    21H        ; is X^16 + X^15 + X^2 + 1. This may be
  88.     LD    L,A        ; used by substituting XOR 80H for XOR 10H
  89. SKIPIT:                ; and XOR 05H for XOR 21H in the adjacent code.
  90.     DEC    B    ;COUNT DOWN 8 BITS
  91.     JP    NZ,UPDLOOP
  92.     LD    (CRCVAL),HL    ;SAVE NEW CRC VALUE
  93.     POP    HL    ;RESTORE ALL
  94.     POP    BC
  95.     POP    AF
  96.     RET
  97. ;
  98. ; CRCDONE - Complete the CRC calculation
  99. ;    This routine is called after the last byte of the byte stream
  100. ;    has passed thru CRCUPD, and it returns the calculated
  101. ;    CRC bytes, which must be transmitted as the final
  102. ;    two bytes of the message (first H, then L).
  103. ;
  104. ;     Input Parameters: None
  105. ;
  106. ;     Output Parameters:  HL = calculated CRC bytes
  107. ;
  108. CRCDONE:
  109.     PUSH    AF    ;SAVE A
  110.     XOR    A    ;SEND OUT 2 ZEROES
  111.     CALL    CRCUPD
  112.     CALL    CRCUPD
  113.     LD    HL,(CRCVAL)    ;RETURN CRC VALUE IN HL
  114.     POP    AF
  115.     RET
  116.  
  117.     END
  118.