;***************************************************************************
;* File Name		:crc.asm
;* Title		:CRC Check
;* Date	 		:09/20/97
;* Version 		:1.0
;* Target MCU		:AT90S1200
;* 
;* DESCRIPTION
;* Checking of data integrity by CRC
;***************************************************************************

;***** Directives

.device at90s1200		; Device is AT90S1200
.nolist
.include "1200def.inc"
.list


.equ	crcpoly_lo_byte	= $21	; CRC-CCITT is $1021
.equ	crcpoly_hi_byte	= $10

.def	temp_lo		= r0
.def	temp_hi		= r1

.def	temp 		= r16
.def	byte 		= r17
.def	crc_lo		= r18
.def	crc_hi		= r19
.def	crcpoly_lo	= r20
.def	crcpoly_hi	= r21
.def	count		= r22

;***** Macros



;***** Interrupt vector table

	rjmp	RESET		; Reset handle
	reti			; External Interrupt0 handle
	reti			; Overflow0 Interrupt handle
	reti			; Analog Comparator Interrupt handle

;***** Subroutines

calc_crc:
	eor	byte, crc_hi
	mov	crc_hi, byte
	ldi	count, 8
repeat:	mov	temp, crc_hi
	andi	temp, $80
	breq	shift_only
	lsl	crc_lo
	rol	crc_hi
	eor	crc_lo, crcpoly_lo
	eor	crc_hi, crcpoly_hi
	rjmp	next
shift_only:
	lsl	crc_lo
	rol	crc_hi
next:	dec	count
	brne	repeat
	ret

;***** Main

RESET:	ldi	crcpoly_lo, crcpoly_lo_byte	; Initialize CRC Polynom Lo Byte
	ldi	crcpoly_hi, crcpoly_hi_byte	; Initialize CRC Polynom Hi Byte
	
	clr	crc_lo 				; Clear CRC Lo Byte
	clr	crc_hi				; Clear CRC Hi Byte

	ldi	byte, $12			; Load 1. Byte
	rcall	calc_crc			; Call CRC Calculation

	ldi	byte, $34			; Load 2. Byte
	rcall	calc_crc			; Call CRC Calculation

	ldi	byte, $56			; Load 3. Byte
	rcall	calc_crc			; Call CRC Calculation

	mov	temp_lo, crc_lo
	mov	temp_hi, crc_hi

	clr	crc_lo 				; Clear CRC Lo Byte
	clr	crc_hi				; Clear CRC Hi Byte

	ldi	byte, $12			; Load 1. Byte
	rcall	calc_crc			; Call CRC Calculation

	ldi	byte, $34			; Load 2. Byte
	rcall	calc_crc			; Call CRC Calculation

	ldi	byte, $56			; Load 3. Byte
	rcall	calc_crc			; Call CRC Calculation

	mov	byte, temp_hi			; Load CRC Hi Byte
	rcall	calc_crc			; Call CRC Calculation

	mov	byte, temp_lo			; Load CRC Lo Byte
	rcall	calc_crc			; Call CRC Calculation
	
loop:	rjmp 	loop				; Repeat forever
	

