******************************************************************
* Routines used by the JModem.c file. (C) 1989 Kenneth Österberg *
* Use the Manx assembler to compile this file, and link it with  *
* the object file of the C program.                              *
******************************************************************

		public	_docrc

; This routine calculates the CRC (== checksum) word of a string buffer.
; To call the function from C:
; docrc(buffer,size)
; UBYTE *buffer;
; UWORD size;
; The resulting CRC value is placed in the last word of the buffer. If the
; old value matched the new one, TRUE (nonzero) is returned.

_docrc:
		movem.l	d1-d3/a0,-(a7)
		move.l	4*4+4(a7),a0       ; A0 = bufptr
		move.w	4*4+8(a7),d0       ; D0 = size
		subq.w	#2,d0              ; Exclude CRC word in buffer
		clr.l	d1                 ; Holds read byte
		clr.l	d2                 ; D2 = crc
crc0:
		move.b	(a0)+,d1
		add.w	d1,d2
		move.w	d0,d3
		and.w	#7,d3              ; (size and 7)
		rol.w	d3,d2              ; shift left
		subq.w	#1,d0
		bne.s	crc0

		moveq	#1,d0              ; assume CRC match
		move.b	1(a0),d1           ; Get high byte of old CRC
		lsl.w	#8,d1
		move.b	(a0),d1            ; Get low byte of old CRC
		cmp.w	d2,d1              ; New & old equal?
		beq.s	crcmatch

		clr.l	d0                 ; CRC mismatch
		move.b	d2,(a0)+           ; Store new CRC, low byte
		lsr.w	#8,d2
		move.b	d2,(a0)            ; Store high byte
crcmatch:
		movem.l	(a7)+,d1-d3/a0
		rts

		end
