home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / simtel / sigm / vols000 / vol098 / scrc.mac < prev    next >
Text File  |  1984-04-29  |  3KB  |  121 lines

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