home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol098 / scrc2.mac < prev    next >
Encoding:
Text File  |  1984-04-29  |  2.7 KB  |  119 lines

  1. ;
  2. ; SYSLIB Module Name:  SCRC2
  3. ; Author:  Richard Conn
  4. ; SYSLIB Version Number:  2.3
  5. ; Module Version Number:  1.0
  6. ; Module Entry Points:
  7. ;    CRC2CLR        CRC2DONE    CRC2UPD
  8. ; Module External References:
  9. ;    None
  10. ; Author Credits:  As Given in the Source Code
  11. ;
  12.  
  13. ;
  14. ;         These subroutines will compute and check a true 16-bit
  15. ;    Cyclic Redundancy Code for a message of arbitrary length.
  16. ;
  17. ;    The  use  of this scheme will guarantee detection of all
  18. ;    single and double bit errors, all  errors  with  an  odd
  19. ;    number  of  error bits, all burst errors of length 16 or
  20. ;    less, 99.9969% of all 17-bit error bursts, and  99.9984%
  21. ;    of  all  possible  longer  error bursts.  (Ref: Computer
  22. ;    Networks, Andrew S.  Tanenbaum, Prentiss-Hall, 1981)
  23. ;
  24. ;    These routines are typically used as follows:
  25. ;      CRC$MAKE:        ; ROUTINE TO ESTABLISH CRC VALUE
  26. ;        CALL    CRC2CLR    ; CLEAR CRC
  27. ;        <loop CALLing CRC2UPD>    ; ACQUIRE VALUES
  28. ;        CALL    CRC2DONE    ; GET VALUE
  29. ;        SHLD    CRCVAL    ; SAVE VALUE
  30. ;      CRC$CHECK:        ; ROUTINE TO CHECK CRC VALUE
  31. ;        CALL    CRC2CLR    ; CLEAR CRC
  32. ;        <loop CALLing CRC2UPD>    ; ACQUIRE VALUES
  33. ;        CALL    CRC2DONE    ; NOW DONE
  34. ;        XCHG        ; DE=RETURNED CRC
  35. ;        LHLD    CRCVAL    ; FROM BEFORE
  36. ;        CALL    COMPHD    ; COMPARE HL TO DE FOR EQUALITY
  37. ;        JNZ    ERROR    ; ERROR IF NOT EQUAL
  38. ;
  39.  
  40. ;
  41. ; CRC2CLR - Clear the CRC accumulator
  42. ;     This routine must be called at the start of each byte stream.
  43. ;
  44. ;     Input Parameters: None
  45. ;
  46. ;     Output Parameters: None
  47. ;
  48. CRC2CLR::
  49.     PUSH    H
  50.     LXI    H,0    ;SET CRC TO ZERO
  51.     SHLD    CRCVAL
  52.     POP    H
  53.     RET
  54.  
  55. ;
  56. ;  BUFFER FOR CRC VALUE
  57. ;
  58. CRCVAL:    DS    2
  59.  
  60. ;
  61. ; CRC2UPD - Update the CRC accumulator
  62. ;     This routine must be called once for each byte in the
  63. ;     byte stream for which the CRC is being calculated.
  64. ;
  65. ;     Input Parameters: A = byte to be included in CRC
  66. ;
  67. ;     Output Parameters: None
  68. ;
  69. ;    Adapted from Keith Petersen's CRCK 4.2 program.
  70. ;    Routine Originally By Fred Gutman.
  71. ;    From 'EDN' magazine, June 5, 1979 issue, page 84.
  72. ;
  73. CRC2UPD::
  74.     PUSH    H    ;SAVE HL
  75.     PUSH    B    ;SAVE BC
  76.     PUSH    PSW    ;SAVE BYTE TO UPDATE
  77.     MOV    B,A    ;SAVE BYTE IN B
  78.     LHLD    CRCVAL    ;GET REMAINDER
  79.     MOV    A,H
  80.     ANI    128    ;Q-BIT MASK
  81.     PUSH    PSW    ;SAVE STATUS
  82.     DAD    H    ;2 X R(X)
  83.     MOV    A,B    ;GET BYTE
  84.     ADD    L
  85.     MOV    L,A
  86.     POP    PSW
  87.     JZ    CRCU1    ;IF Q-BIT IS ZERO
  88. ;
  89.     MOV    A,H
  90.     XRI    0A0H    ;MS HALF OF GEN. POLY
  91.     MOV    H,A
  92.     MOV    A,L
  93.     XRI    97H    ;LS HALF OF GEN. POLY
  94.     MOV    L,A
  95. ;
  96. CRCU1:
  97.     SHLD    CRCVAL    ;SAVE RESULT
  98.     POP    PSW    ;RESTORE REGS
  99.     POP    B
  100.     POP    H
  101.     RET
  102.  
  103. ;
  104. ; CRC2DONE - Complete the CRC calculation
  105. ;    This routine is called after the last byte of the byte stream
  106. ;    has passed thru CRCUPD, and it returns the calculated
  107. ;    CRC bytes, which must be transmitted as the final
  108. ;    two bytes of the message (first H, then L).
  109. ;
  110. ;     Input Parameters: None
  111. ;
  112. ;     Output Parameters:  HL = calculated CRC bytes
  113. ;
  114. CRC2DONE::
  115.     LHLD    CRCVAL    ;RETURN CRC VALUE IN HL
  116.     RET
  117.  
  118.     END
  119.