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 / CRC16.ASM < prev    next >
Assembly Source File  |  2000-06-30  |  6KB  |  85 lines

  1.         TITLE   CRC16 - Cyclic Redundancy Checksum
  2. ;******************************************************************************
  3. ;*                                                                            *
  4. ;*                                                                            *
  5. ;*                         Improved CRC16 Routine                             *
  6. ;*                                                                            *
  7. ;*                                                                            *
  8. ;*      This routine performs CRC16 cyclic redundancy checksum calculations.  *
  9. ;*  It makes use of an idea given in the IEEE Micro publication by Aram       *
  10. ;*  Perez, and represents a considerable improvement over some of the         *
  11. ;*  methods used previously.  It receives a 16-bit CRC in BC and              *
  12. ;*  the next data byte in E.  It returns the updated CRC in BC.               *
  13. ;*                                                                            *
  14. ;*      The first step is to XOR the data byte into the right half of the     *
  15. ;*  the CRC.  Let the result of this be denoted by:                           *
  16. ;*                                                                            *
  17. ;*                   a b c d e f g h i j k l m n o p                          *
  18. ;*                                                                            *
  19. ;*  After the eight CRC16 steps, with the x^16+x^15+x^2+1 polynomial, this    *
  20. ;*  should become:                                                            *
  21. ;*                                                                            *
  22. ;*                   X i j k l m n o p 0 0 0 0 0 0 X                          *
  23. ;*                   + + + + + + + + + + + + + + + +                          *
  24. ;*                   0 X i j k l m n o p 0 0 0 0 0 0                          *
  25. ;*                   + + + + + + + + + + + + + + + +                          *
  26. ;*                   0 0 0 0 0 0 0 0 a b c d e f g h                          *
  27. ;*                                                                            *
  28. ;*  where X represents the parity of the 8 bits i,j,k,l,m,n,o,p, and          *
  29. ;*  where + represents the XOR of the respective columns.  The code below     *
  30. ;*  carries out this process directly, making use of the 8080's parity flag   *
  31. ;*  for finding X.  [Note that since the routine uses this parity flag        *
  32. ;*  after an XRA instruction, the routine will work properly even on a        *
  33. ;*  Z80 microprocessor.]                                                      *
  34. ;*                                                                            *
  35. ;******************************************************************************
  36. ;*                                                                            *
  37. ;*      Entry:                                                                *
  38. ;*      BC = old CRC16                                                        *
  39. ;*      E = next data byte                                                    *
  40. ;*                                                                            *
  41. ;*      CALL    CRC16                                                         *
  42. ;*                                                                            *
  43. ;*      Exit:                                                                 *
  44. ;*      BC = new CRC16                                                        *
  45. ;*      A, D, & E also affected                                               *
  46. ;*                                                                            *
  47. ;*                                                                            *
  48. ;*****************************************  R. Stafford - Aug. 9, 1983 ********
  49. ;
  50.         RSECT   ROM
  51.         INTERN  CRC16
  52.  
  53. CRC16:  EQU     $
  54. ; Assume the current 16 bit CRC is in BC and the next data byte in E.
  55.         MOV     A,E
  56.         XRA     C       ;A now has [i j k l m n o p] & parity flag has X
  57.         MOV     C,B     ;Put [a b c d e f g h] in C
  58.         MOV     D,A     ;Put [i j k l m n o p] in D
  59.         JPE     CRCJ    ;The parity flag has X (even on a Z80)
  60.         XRI     2       ;XOR X into A in bit 1
  61. CRCJ    XRA     D       ;A now has [0 0 0 0 0 0 X 0]
  62.         MOV     E,A     ;Now E = [0 0 0 0 0 0 X 0]
  63.         RAR
  64.         RAR             ;Move X into carry flag
  65.         MOV     A,D     ;Put [i j k l m n o p] back into A
  66.         RAR
  67.         MOV     D,A     ;Shift it to [X i j k l m n o] in D
  68.         MOV     A,E     ;Get [0 0 0 0 0 0 X 0] from E
  69.         RAR             ;This sets carry to zero
  70.         MOV     E,A     ;Put [p 0 0 0 0 0 0 X] back into E
  71.         MOV     A,D     ;Now proceed with yet another shift
  72.         RAR             ;Get [0 X i j k l m n] in A
  73.         MOV     B,A     ;Save it in B
  74.         MOV     A,E     ;Then get [p 0 0 0 0 0 0 X]
  75.         RAR             ;Shift it to [o p 0 0 0 0 0 0]
  76.         XRA     E       ;Then XOR in [p 0 0 0 0 0 0 X]
  77.         XRA     C       ;Finally XOR in [a b c d e f g h]
  78.         MOV     C,A     ;Establish this as the low byte of new CRC
  79.         MOV     A,D     ;Now get [X i j k l m n o]
  80.         XRA     B       ;XOR it with [0 X i j k l m n]
  81.         MOV     B,A     ;This constitutes the upper byte of new CRC
  82.         RET             ;Return with updated CRC in BC
  83.  
  84. ; End of CRC16
  85.