/*
============================================================================
                       C R C A . C  -  vypocet CRC-16
                          (algoritmus v asembleru)
============================================================================

(C) P.Tesar, T.Pecina, LP Praha, December 1989

*/

#pragma inline

unsigned CRC(unsigned old, char byte)
  {
/*

     Puvodni algoritmus pro Z80:
     ---------------------------

; Vypocet CRC-16 (IEEE MICRO 83)
;   vstup: A - byte
;          BC - CRC
;  vystup: BC - CRC
;  nici:   A,F,B,C
CRCB::  push    hl
        xor     c
        ld      l,a
        ld      h,a
        add     a,a
        xor     l
        ld      l,a
        ld      a,h
        add     a,a
        ld      h,10b
        jr      c,c1
        ld      h,0
c1:     or      a
        jp      pe,c2
        ld      a,11b
        sub     h
        ld      h,a
c2:     ld      a,h
        rra
        rept    6
        add     hl,hl
        endm
        or      l
        xor     b
        ld      c,a
        ld      b,h
        pop     hl
        ret
*/

  unsigned new;

/* prenos parametru do registru 8086 */
asm  mov al,byte
asm  mov bx,old
/* A = AL, BC = BX, HL = DX */

/* algoritmus */
asm  xor al,bl
asm  mov dl,al
asm  mov dh,al
asm  add al,al
asm  xor al,dl
asm  mov dl,al
asm  mov al,dh
asm  add al,al
asm  mov dh,2
asm  jc c1
asm  mov dh,0
c1:
asm  or al,al
asm  jpe c2
asm  mov al,3
asm  sub al,dh
asm  mov dh,al
c2:
asm  mov al,dh
asm  rcr al,1
asm  add dx,dx
asm  add dx,dx
asm  add dx,dx
asm  add dx,dx
asm  add dx,dx
asm  add dx,dx
asm  or al,dl
asm  xor al,bh

/* prenos vysledku */
asm  mov dl,al
asm  mov new,dx

  return new;
  }
