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 / CPM / GENASM / CRC120.AQM / CRC120.ASM
Assembly Source File  |  2000-06-30  |  4KB  |  144 lines

  1. ;************************************************************************
  2. ;* CRCSUBS (Cyclic Redundancy Code Subroutines) version 1.20        *
  3. ;* 8080 Mnemonics                            *
  4. ;*                                    *
  5. ;*         These subroutines will compute and check a true 16-bit        *
  6. ;*    Cyclic Redundancy Code for a message of arbitrary length.    *
  7. ;*                                    *
  8. ;*    The  use  of this scheme will guarantee detection of all    *
  9. ;*    single and double bit errors, all  errors  with  an  odd    *
  10. ;*    number  of  error bits, all burst errors of length 16 or    *
  11. ;*    less, 99.9969% of all 17-bit error bursts, and  99.9984%    *
  12. ;*    of  all  possible  longer  error bursts.  (Ref: Computer    *
  13. ;*    Networks, Andrew S.  Tanenbaum, Prentiss-Hall, 1981)        *
  14. ;*                                    *
  15. ;*                                    *
  16. ;*    There are four entry points, which are used as follows:        *
  17. ;*                                    *
  18. ;*    CLRCRC - A call to this entry resets the CRC accumulator.    *
  19. ;*         It must be called at the start of each message.    *
  20. ;*                                    *
  21. ;*         Entry Parameters: None.                *
  22. ;*                                    *
  23. ;*         Exit Conditions:  CRC accumulator cleared.        *
  24. ;*                   All registers preserved.        *
  25. ;*                                    *
  26. ;*                                    *
  27. ;*    UPDCRC - A call to this entry updates the CRC accumulator.    *
  28. ;*         It must be called once for each byte in the        *
  29. ;*         message for which the CRC is being calculated.        *
  30. ;*                                    *
  31. ;*         Entry Parameters: (A) = a byte to be included        *
  32. ;*                     in the CRC calculation.    *
  33. ;*                                    *
  34. ;*         Exit Conditions:  CRC accumulator updated.        *
  35. ;*                   All registers preserved.        *
  36. ;*                                    *
  37. ;*                                    *
  38. ;*    FINCRC - A call to this entry finishes the CRC calculation    *
  39. ;*         for a message which is to be TRANSMITTED. It must    *
  40. ;*         be called after the last byte of the message has    *
  41. ;*         been passed thru UPDCRC. It returns the calculated    *
  42. ;*         CRC bytes, which must be transmitted as the final    *
  43. ;*         two bytes of the message (first D, then E).        *
  44. ;*                                    *
  45. ;*         Entry Parameters: None.                *
  46. ;*                                    *
  47. ;*         Exit Conditions:  (DE) = calculated CRC bytes.        *
  48. ;*                   All other registers preserved.    *
  49. ;*                                    *
  50. ;*                                    *
  51. ;*    CHKCRC - A call to this routine checks the CRC bytes of        *
  52. ;*         a RECEIVED message and returns a code to indicate    *
  53. ;*         whether the message was received correctly. It must    *
  54. ;*         be called after the message AND the two CRC bytes    *
  55. ;*         have been received AND passed thru UPDCRC.        *
  56. ;*                                    *
  57. ;*         Entry Parameters: None.                *
  58. ;*                                    *
  59. ;*         Exit Conditions:  (A) =  0 if message ok.        *
  60. ;*                   (A) = -1 if message garbled.        *
  61. ;*                   All other registers preserved.    *
  62. ;*                                    *
  63. ;************************************************************************
  64. ;*                                    *
  65. ;*    Designed & coded by Paul Hansknecht, June 13, 1981        *
  66. ;*                                    *
  67. ;*                                    *
  68. ;*    Copyright (c) 1981, Carpenter Associates            *
  69. ;*                Box 451                    *
  70. ;*                Bloomfield Hills, MI 48013            *
  71. ;*                313/855-3074                *
  72. ;*                                    *
  73. ;*    This program may be freely reproduced for non-profit use.    *
  74. ;*                                    *
  75. ;************************************************************************
  76. ;
  77. ;    ENTRY    CLRCRC,UPDCRC,FINCRC,CHKCRC
  78. ;
  79. CLRCRC:    EQU    $        ; Reset CRC Accumulator for a new message.
  80.     PUSH    H
  81.     LXI    H,0
  82.     SHLD    CRCVAL
  83.     POP    H
  84.     RET
  85. ;
  86. UPDCRC:    EQU    $        ; Update CRC Accumulator using byte in (A).
  87.     PUSH    PSW
  88.     PUSH    B
  89.     PUSH    H
  90.     MVI    B,8
  91.     MOV    C,A
  92.     LHLD    CRCVAL
  93. UPDLOOP:MOV    A,C
  94.     RLC
  95.     MOV    C,A
  96.     MOV    A,L
  97.     RAL
  98.     MOV    L,A
  99.     MOV    A,H
  100.     RAL
  101.     MOV    H,A
  102.     JNC    SKIPIT
  103.     MOV    A,H        ; The generator is X^16 + X^12 + X^5 + 1
  104.     XRI    10H        ; as recommended by CCITT.
  105.     MOV    H,A        ; An alternate generator which is often
  106.     MOV    A,L        ; used in synchronous transmission protocols
  107.     XRI    21H        ; is X^16 + X^15 + X^2 + 1. This may be
  108.     MOV    L,A        ; used by substituting XOR 80H for XOR 10H
  109. SKIPIT:    DCR    B        ; and XOR 05H for XOR 21H in the adjacent code.
  110.     JNZ    UPDLOOP
  111.     SHLD    CRCVAL
  112.     POP    H
  113.     POP    B
  114.     POP    PSW
  115.     RET
  116. ;
  117. FINCRC:    EQU    $        ; Finish CRC calc for outbound message.
  118.     PUSH    PSW
  119.     XRA    A
  120.     CALL    UPDCRC
  121.     CALL    UPDCRC
  122.     PUSH    H
  123.     LHLD    CRCVAL
  124.     MOV    D,H
  125.     MOV    E,L
  126.     POP    H
  127.     POP    PSW
  128.     RET
  129. ;
  130. CHKCRC:    EQU    $        ; Check CRC bytes of received message.
  131.     PUSH    H
  132.     LHLD    CRCVAL
  133.     MOV    A,H
  134.     ORA    L
  135.     POP    H
  136.     RZ
  137.     MVI    A,-1
  138.     RET
  139. ;
  140. ;
  141. CRCVAL    DW    0
  142. ;
  143.     END
  144.