home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / fileutil / scan.lha / src / addbfcrc.asm next >
Assembly Source File  |  1992-05-21  |  4KB  |  176 lines

  1. ;
  2. ; Copyright © 1991, 1992 by Walter Rothe. You may freely use and modify this
  3. ; program, but not for commercial profit. A modest fee for distribution is
  4. ; allowed. Derivative works must be released with source along with the
  5. ; executable or provisions made to provide the user source, if requested.
  6. ; Uploading source to a major bulletin board system within 6 months of the
  7. ; time of the request satisfies this requirement. This copyright notice
  8. ; must not be deleted from the source.
  9. ;
  10. ;:ts=8
  11.         far     data
  12.         machine mc68020
  13. ;/* adbfcrc.c
  14. ;
  15. ;addbfcrc() accepts a buffer address and a count and adds the CRC for
  16. ;all bytes in the buffer to the global variable crccode using
  17. ;CRC-16.
  18. ;*/
  19. ;
  20. ;#define TABLEN  256
  21. ;
  22. ;unsigned int crccode;
  23. ;unsigned char crctabl[TABLEN], crctabr[TABLEN];
  24. ;
  25. ;int addbfcrc(buffer,count)
  26. ;unsigned char *buffer;
  27. ;unsigned count;
  28. ;
  29. ;{
  30.         xdef    _addbfcrc
  31. _addbfcrc:
  32.         movem.l d2/d3/d4/d5/a2/a3,-(sp)
  33.         move.l  28(sp),a2
  34. ;   register unsigned char crcl, crcr, crct;
  35. ;   register unsigned char *ptr = buffer;
  36. ;   register unsigned char *endptr = &buffer[count];
  37. ;   crcl = crccode >> 8;
  38.         move.l  32(sp),a3
  39.         add.l   a2,a3
  40.         move.l  _crccode,d3
  41.         lsr.l   #8,d3
  42.         and.l   #255,d3
  43. ;   crcr = crccode & 0xff;
  44.         move.l  _crccode,d4
  45.         and.l   #255,d4
  46. ;
  47.         lea     _crctabr,a0
  48.         lea     _crctabl,a1
  49.         move.l  #0,d2
  50. ;
  51. ;   for ( ; ptr<endptr; ptr++) {
  52.         cmp.l   a3,a2
  53.         bcc     .10003
  54. .10001
  55. ;      crct = crcr ^ *ptr;
  56.         move.b  (a2)+,d2
  57.         eor.b   d4,d2
  58. ;      crcr = crcl ^ crctabr[ crct ];
  59.         move.b  (a0,d2.w),d4
  60.         eor.b   d3,d4
  61. ;      crcl = crctabl[ crct ];
  62.         move.b  (a1,d2.w),d3
  63. ;   }
  64.         cmp.l   a3,a2
  65.         bcs     .10001
  66. .10003
  67. ;   crccode = (crcl << 8) | crcr;
  68.         asl.l   #8,d3
  69.         or.w    d3,d4
  70.         move.l  d4,_crccode
  71. ;}
  72. .2
  73.         movem.l (sp)+,d2/d3/d4/d5/a2/a3
  74.         rts
  75. ;
  76. ;/* gentab() generates table for CRC calculation, as described in
  77. ;"C Programmer's Guide to Serial Communications" by Joe Campbell */
  78. ;
  79. ;/* reverse CRC-16 polynomial */
  80. ;#define CRC_FUNC        (unsigned) 0xa001
  81. ;
  82. ;unsigned int calcterm();
  83. ;
  84. ;unsigned int calcterm (data)
  85. ;register unsigned int data;
  86. ;{
  87.         xdef    _calcterm
  88. _calcterm:
  89.         movem.l d2/d3/d4,-(sp)
  90.         move.l  16(sp),d4
  91. ;   int i;
  92. ;   register unsigned int accum = 0;
  93. ;
  94. ;   data <<= 1;
  95.         move.l  #0,d2
  96.         add.l   d4,d4
  97. ;   for (i = 8;  i > 0;  i--) {
  98.         move.l  #8,d3
  99.         bra     .10005
  100. .10004
  101.         sub.l   #1,d3
  102. .10005
  103.         tst.l   d3
  104.         ble     .10006
  105. ;      data >>= 1;
  106.         lsr.l   #1,d4
  107. ;      if ((data ^ accum) & 0x0001)
  108. ;         accum = (accum >> 1) ^ CRC_FUNC;
  109.         move.l  d4,d0
  110.         eor.l   d2,d0
  111.         btst.l  #0,d0
  112.         beq     .10007
  113.         move.l  d2,d0
  114.         lsr.l   #1,d0
  115.         move.l  d0,d2
  116.         eor.l   #40961,d2
  117. ;      else
  118.         bra     .10008
  119. .10007
  120. ;         accum >>= 1;
  121.         lsr.l   #1,d2
  122. .10008
  123. ;   }
  124.         bra     .10004
  125. .10006
  126. ;   return accum;
  127.         move.l  d2,d0
  128. .3
  129.         movem.l (sp)+,d2/d3/d4
  130.         rts
  131. ;}
  132. ;
  133. ;int gentab()
  134. ;{
  135.         xdef    _gentab
  136. _gentab:
  137.         movem.l d2/d3,-(sp)
  138. ;   register unsigned int i, cx;
  139. ;   for (i = 0;  i < TABLEN;  i++) {
  140.         move.l  #0,d2
  141.         bra     .10010
  142. .10009
  143.         add.l   #1,d2
  144. .10010
  145.         cmp.l   #256,d2
  146.         bcc     .10011
  147. ;      cx = calcterm(i);
  148.         move.l  d2,-(sp)
  149.         jsr     _calcterm
  150.         move.l  d0,d3
  151. ;      crctabl[i] = cx >> 8;
  152.         move.l  d3,d0
  153.         lsr.l   #8,d0
  154.         lea     _crctabl,a0
  155.         move.b  d0,(a0,d2.l)
  156. ;      crctabr[i] = cx & 0xff;
  157.         move.l  d3,d0
  158.         and.l   #255,d0
  159.         lea     _crctabr,a0
  160.         move.b  d0,(a0,d2.l)
  161. ;   }
  162.         add.w   #4,sp
  163.         bra     .10009
  164. .10011
  165. ;}
  166. .4
  167.         movem.l (sp)+,d2/d3
  168.         rts
  169. ;
  170.         xref    .begin
  171.         dseg
  172.         global  _crctabr,256
  173.         global  _crctabl,256
  174.         global  _crccode,4
  175.         end
  176.