;***************************************************************************;
;									    ;
;				    HASH				    ;
;				Hash Routines				    ;
;									    ;
;***************************************************************************;

	INTERN	HASH.MODULE,HASH.FILE

	SEARCH	SYS
	SEARCH	SYSSYM

;HASH.MODULE
;
;Entry:	A0 - address of memory module
;	A2 - address of buffer to output hash total to
;Exit:	A2 - updated past hash total

HASH.MODULE:
	SAVE	A0-A1,A3-A5,D0-D5	; save registers
	CLR     D3
	CLR     D4
	MOV	#2,D0			; scan file twice
	MOV	A0,A5

OPEN.MODULE:
	MOV	A5,A0
	CLR	D2
	MOV	-14(A0),D2
	SUB	#14,D2
	ASR	D2
	JEQ	OUTPUT.HASH

GET.WRD:
	MOVW	(A0)+,D1
	CLR     D7
	ADDW    D1,D3
	ADDXW   D7,D4
	SUBW    D2,D4
	ADDXW   D7,D3
	ADDW    D3,D4
	ADDXW   D7,D3
	LSLW    D3
	ROXLW   D4
	ADDXW   D7,D3
	SOB	D2,GET.WRD

NEXT.HASH:
	SOB	D0,OPEN.MODULE

;display hash

OUTPUT.HASH:
	JOBIDX	A0
	PUSHW   JOBTYP(A0)
	ANDW    #^C<J.HEX>,2(A0)
	CLR     D1
	MOVW    D4,D1
	OCVT	3,OT$MEM
	MOVB	#'-,(A2)+
	MOVW    D1,D7
	ASLW    D1,#10
	ANDW    #-400,D1
	ASRW    D7,#10
	ANDW    #377,D7
	ORW     D7,D1
	OCVT	3,OT$MEM
	MOVB	#'-,(A2)+
	MOVW    D3,D1
	OCVT	3,OT$MEM
	MOVB	#'-,(A2)+
	MOVW    D1,D7
	ASLW    D1,#10
	ANDW    #-400,D1
	ASRW    D7,#10
	ANDW    #377,D7
	ORW     D7,D1
	OCVT	3,OT$MEM
	POPW    JOBTYP(A0)
	REST	A0-A1,A3-A5,D0-D5	; restore registers
	RTN				; return

;HASH.FILE
;
;Entry:	A0 - address of INITed DDB of file to hash
;	A2 - address of buffer to output hash total to
;Exit:	A2 - updated past hash total
;
;Notes:	Contiguous files are not supported currently.

HASH.FILE:
	SAVE	D0-D4			; save registers
	CLR     D3
	CLR     D4
	MOV	#2,D0			; read file twice

OPEN.FILE:
	LOOKUP	@A0
	CMM	D.LSZ(A0),#512.
	JHI	RETURN
	MOV	D.FSZ(A0),D2
	DEC	D2
	MUL	D2,#510.
	MOV	D.LSZ(A0),D7
	SUB	#2,D7
	ADD	D7,D2
	ASR	D2			; D2 now contains word size
	OPENI	@A0

GET.WORD:
	FILINW	@A0
	TST	D.SIZ(A0)
	BEQ	CLOSE.FILE
	CLR     D7
	ADDW    D1,D3
	ADDXW   D7,D4
	SUBW    D2,D4
	ADDXW   D7,D3
	ADDW    D3,D4
	ADDXW   D7,D3
	LSLW    D3
	ROXLW   D4
	ADDXW   D7,D3
	DEC	D2
	BR	GET.WORD

CLOSE.FILE:
	CLOSE	@A0
	SOB	D0,OPEN.FILE

;display hash

DISPLAY.HASH:
	JOBIDX	A0
	PUSHW   JOBTYP(A0)
	ANDW    #^C<J.HEX>,2(A0)
	CLR     D1
	MOVW    D4,D1
	OCVT	3,OT$MEM
	MOVB	#'-,(A2)+
	MOVW    D1,D7
	ASLW    D1,#10
	ANDW    #-400,D1
	ASRW    D7,#10
	ANDW    #377,D7
	ORW     D7,D1
	OCVT	3,OT$MEM
	MOVB	#'-,(A2)+
	MOVW    D3,D1
	OCVT	3,OT$MEM
	MOVB	#'-,(A2)+
	MOVW    D1,D7
	ASLW    D1,#10
	ANDW    #-400,D1
	ASRW    D7,#10
	ANDW    #377,D7
	ORW     D7,D1
	OCVT	3,OT$MEM
	POPW    JOBTYP(A0)

RETURN:	REST	D0-D4			; restore registers
	RTN				; return

	END