#include <stdio.h>


/********************************************************************
 
  CRC-16  (x^16+x^15+x^2+x^0)
  No tables, No jumps.
 
  Input:  c = Data byte for CRC calculation
          usp_crc = pointer to 16 bit CRC register
 
  Output: CRC at pointer usp_crc is updated
 
 
  Copyright (C) February 11, 2000. All Right Reserved.
  Charles Ader, PO Box 940 Pleasanton, California, USA.
 
  This code started out as an example program found in 
  Dallas Semiconductor Application Note 27:
 
    Understanding and Using Cyclic Redundancy Checks 
    with Dallas Semiconductor iButton(TM) Products. 
 
  The application note shows an 8051 assembly language 
  routine that calculates the same CRC as the hardware 
  in the DS5001/2 secure micro.
 
********************************************************************/
void crc16(unsigned short *usp_crc, unsigned char c)
{
unsigned short crc;     /* use a local temp so the compiler will */
                        /* handle all the x86 pointer crap.      */
    crc = *usp_crc;

    _asm {
        mov     al,c
        mov     cx,crc

        XOR     AL,CL
        MOV     CL,CH
        MOV     CH,AL
	
        LAHF
        NOT     AH
        RCR     AH,1
        RCR     AH,1
        AND     AH,1
        XOR     CL,AH

        RCR     AX,1
        SBB     AH,AH
        AND     AH,0x40
        XOR     CL,AH

        MOV     AH,AL
        XOR     AL,CH
        RCL     AH,1
        RCR     AL,1
        MOV     CH,AL
        RCR     AL,1
        AND     AL,0x80
        XOR     CL,AL

        mov     crc,cx
    }
    *usp_crc = crc;
}

/******************************************************
*         MAIN FUNCTION                               *
*******************************************************/

main()
{
unsigned int crc;
    while(1) {
        crc = 0;          /* crc = 0x0000 */
        crc16(&crc,0x80); /* crc = 0xA001 */
        crc16(&crc,0x75); /* crc = 0x27A0 */
        crc16(&crc,0x8A); /* crc = 0xDFA6 */
        crc16(&crc,0x0B); /* crc = 0xBD1E */
        crc16(&crc,0x75); /* crc = 0xEFFC */
        crc16(&crc,0xC7); /* crc = 0xD3AE */
        crc16(&crc,0xAA); /* crc = 0xC3D2 */
        crc16(&crc,0x75); /* crc = 0xBA82 */
        crc16(&crc,0xC7); /* crc = 0xF37B */
        crc16(&crc,0x55); /* crc = 0x1C73 */
        crc16(&crc,0x43); /* crc = 0x141C */
        crc16(&crc,0x1C); /* crc = 0x0014 */
        crc16(&crc,0x14); /* crc = 0x0000 */
    }
} /* end of main */