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

  1. ;
  2. ; SYSLIB Module Name:  SCRC
  3. ; Author:  Richard Conn
  4. ; SYSLIB Version Number:  2.0
  5. ; Module Version Number:  1.0
  6. ; Module Entry Points:
  7. ;    CRCCLR        CRCDONE        CRCK        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. ;        LHLD    CRCVAL    ; FROM BEFORE
  33. ;        MOV    A,H    ; SEND HIGH TO UPDATE
  34. ;        CALL    CRCUPD
  35. ;        MOV    A,L    ; SEND LOW TO UPDATE
  36. ;        CALL    CRCUPD
  37. ;        CALL    CRCDONE    ; NOW DONE
  38. ;        CALL    CRCK    ; RETURN Z IF OK, NZ IF NOT
  39. ;
  40.  
  41. ;
  42. ; CRCCLR - Clear the CRC accumulator
  43. ;     This routine must be called at the start of each byte stream.
  44. ;
  45. ;     Input Parameters: None
  46. ;
  47. ;     Output Parameters: None
  48. ;
  49. CRCCLR::
  50.     PUSH    H
  51.     LXI    H,0    ;SET CRC TO ZERO
  52.     SHLD    CRCVAL
  53.     POP    H
  54.     RET
  55.  
  56. ;
  57. ;  BUFFER FOR CRC VALUE
  58. ;
  59. CRCVAL:    DS    2
  60.  
  61. ;
  62. ; CRCUPD - Update the CRC accumulator
  63. ;     This routine must be called once for each byte in the
  64. ;     byte stream for which the CRC is being calculated.
  65. ;
  66. ;     Input Parameters: A = byte to be included in CRC
  67. ;
  68. ;     Output Parameters: None
  69. ;
  70. CRCUPD::
  71.     PUSH    PSW    ;SAVE ALL REGS
  72.     PUSH    B
  73.     PUSH    H
  74.     MVI    B,8    ;ROTATE 8 BITS
  75.     MOV    C,A    ;BYTE IN C
  76.     LHLD    CRCVAL    ;HL=OLD CRC VALUE
  77. UPDLOOP:
  78.     MOV    A,C    ;ROTATE HLC AS A 24-BIT ACC LEFT 1 BIT
  79.     RLC
  80.     MOV    C,A
  81.     MOV    A,L
  82.     RAL
  83.     MOV    L,A
  84.     MOV    A,H
  85.     RAL
  86.     MOV    H,A
  87.     JNC    SKIPIT
  88.     MOV    A,H        ; The generator is X^16 + X^12 + X^5 + 1
  89.     XRI    10H        ; as recommended by CCITT.
  90.     MOV    H,A        ; An alternate generator which is often
  91.     MOV    A,L        ; used in synchronous transmission protocols
  92.     XRI    21H        ; is X^16 + X^15 + X^2 + 1. This may be
  93.     MOV    L,A        ; used by substituting XOR 80H for XOR 10H
  94. SKIPIT:                ; and XOR 05H for XOR 21H in the adjacent code.
  95.     DCR    B    ;COUNT DOWN 8 BITS
  96.     JNZ    UPDLOOP
  97.     SHLD    CRCVAL    ;SAVE NEW CRC VALUE
  98.     POP    H    ;RESTORE ALL
  99.     POP    B
  100.     POP    PSW
  101.     RET
  102. ;
  103. ; CRCDONE - Complete the CRC calculation
  104. ;    This routine is called after the last byte of the byte stream
  105. ;    has passed thru CRCUPD, and it returns the calculated
  106. ;    CRC bytes, which must be transmitted as the final
  107. ;    two bytes of the message (first H, then L).
  108. ;
  109. ;     Input Parameters: None
  110. ;
  111. ;     Output Parameters:  HL = calculated CRC bytes
  112. ;
  113. CRCDONE::
  114.     PUSH    PSW    ;SAVE A
  115.     XRA    A    ;SEND OUT 2 ZEROES
  116.     CALL    CRCUPD
  117.     CALL    CRCUPD
  118.     LHLD    CRCVAL    ;RETURN CRC VALUE IN HL
  119.     POP    PSW
  120.     RET
  121. ;
  122. ; CRCK - Check the CRC bytes of the RECEIVED byte stream and return an error
  123. ;    code to indicate whether the stream was received correctly. It must
  124. ;     be called after the stream and the two CRC bytes
  125. ;     have been received and passed thru CRCUPD.
  126. ;
  127. ;     Input Parameters: None
  128. ;
  129. ;     Output Parameters:  A =  0 if message ok (Z)
  130. ;                 A = 0FFH if message garbled (NZ)
  131. ;
  132. CRCK::
  133.     PUSH    H    ;SAVE HL
  134.     LHLD    CRCVAL    ;GET CRC VALUE
  135.     MOV    A,H    ;CHECK FOR ZERO
  136.     ORA    L
  137.     POP    H
  138.     RZ
  139.     MVI    A,0FFH    ;ERROR CODE
  140.     RET
  141.  
  142.     END
  143.