DEFINITION MODULE CRCBuild;

FROM SYSTEM  IMPORT ADDRESS, BYTE, WORD;
FROM Texts   IMPORT TEXT;

(* ------------------------------------------------------------------------- *)
(* CRC16 and CRC32 over the .DEF files for the Z80-Assembler routines *)

(* This CRC3216 Z80 Assembler Module generates CRC32 and CRC16 in one step *)
FROM CRC3216 IMPORT

	Pcrc32,	 (* TYPE definition: Of a Z80 pointer (Z80 address) for a CRC32
		    variable (4 BYTE, 32 Bit)
		 *)
	Tcrc32,	 (* TYPE definition *)

	Pcrc16,	 (* TYPE definition: Of a Z80 pointer (Z80 address) for a CRC16
		    variable (WORD (2 BYTE), 16 Bit)
		 *)
	Tcrc16,  (* TYPE definition *)

	GPCRC32, (* Routine: Gives back a Z80 pointer to the CRC32 variable *)
	GPCRC16, (* Routine: Gives back a Z80 Pointer to the CRC16 variable *)

	InitCRC, (* Routine: Initialise the CRC32 and CRC16 variables. It's not
		    necessary to complement the CRC32 variable at the beginning
		    of mkCRC!
		 *)

	mkCRC,	 (* Routine: Makes in one step a CRC32 and a CRC16 genaration
		    over the buffer (first parameter). The second parameter are
		    the count of the bytes which will be processed (maximum
		    16 KByte).

		    This routine processes a maximum of 16 KByte and is called
		    repeatedly until an entire file has been processed in the
		    corresponding 16 KByte chunks, or correspondingly less when
		    the last chunk of the file is reached.
		 *)

	CPLCRC3; (* Complements the value of CRC32 variable. This is necessary
		    after the repated generation of the CRC32 value with the
		    routine mkCRC to get a final value of the CRC32.
		 *)

(*---------------------------------------------------------------------------*)
(* This Z80 Assembler routine generates only CRC16 *)

(*
FROM CRC16 IMPORT

	xPcrc16, (* TYPE definition: Of a Z80 pointer (Z80 address) for a CRC16
		    variable (WORD (2 BYTE), 16 Bit)
		 *)

	xGPCRC1, (* Routine: Gives back a Z80 Pointer to the CRC16 variable *)

	xInitCR, (* Routine: Initialize the CRC16 variables. *)

	xmkCRC;	 (* Routine: Makes a CRC16 genaration over the buffer (first
		    parameter). The second parameter are the count of the bytes
		    which will be processed.

		    This routine processes a maximum of 16 KByte and is called
		    repeatedly until an entire file has been processed in the
		    corresponding 16 KByte chunks, or correspondingly less when
		    the last chunk of the file is reached.
		 *)
*)

(*---------------------------------------------------------------------------*)
(* Interface for the CRC32 routines in YAZE-AG. It is calling the YBIOS over
   the module YBIOS.DEF/YBIOS.Z80. *)

(*
IMPORT YAZEAG;   (* FROM YAZEAG IMPORT SetBufferPointer, InitCRC32Pointer,
		    computeCRC32; *)
*)

(*---------------------------------------------------------------------------*)

CONST
    BufferSize = 128 * 128;	(* BufferSize is 16 KByte *)
    BufferTop  = BufferSize - 1;
    CR = 13; LF = 10; TAB = 9;

CONST HEXchars = '0123456789ABCDEF';

TYPE
    TFName = ARRAY [0..12] OF CHAR;

    PFileElement = POINTER TO TFileElement;
    TFileElement = RECORD
		     FName : TFName;
		     crc32 : Tcrc32;
		     crc16 : Tcrc16;
		     lastelement : BOOLEAN;
		     next  : PFileElement;
		   END;

    TFCB = ARRAY [0..35] OF CHAR;

VAR pcrc32	: Pcrc32;	(* Pointer to crc32 / crc16 *)
    pcrc16	: Pcrc16;
    buffer	: ARRAY [0..BufferTop] OF CHAR;
    BufferP,xxxP: ADDRESS;

    Filelist	: PFileElement;

    outputFile	: TEXT;

    HEXch	: ARRAY [0..15] OF CHAR;

(*---------------------------------------------------------------------------*)
PROCEDURE PrCRCByte( crc : BYTE );
PROCEDURE PrCRCByteOF( crc : BYTE );

 (* Zeichen crc in hexadezimaler Form ausgegeben
 *)

(*---------------------------------------------------------------------------*)
PROCEDURE PrCRC32( crc32 : Pcrc32 );

 (* Gibt die 4 Bytes von crc32 aus. crc32 ist ein Pointer.
 *)

(*---------------------------------------------------------------------------*)
PROCEDURE PrTCRC32( crc32 : Tcrc32 );
PROCEDURE PrTCRC32OF( crc32 : Tcrc32 );

 (* Gibt die 4 Bytes von crc32.crc aus. crc32 ist vom TYPE Tcrc32.
 *)

(*---------------------------------------------------------------------------*)
PROCEDURE PrCRC16( crc16 : Pcrc16 );

 (* Gibt die 2 Bytes von crc16 aus. crc16 ist ein Pointertyp aus diesem Module
    und es hat nichts mit der Definition aus dem Module CRC16 zu tun.
    Dieser CRC16 wir gleichzeitig mit dem CRC32 in diesem Module generiert.
 *)

(*---------------------------------------------------------------------------*)
PROCEDURE PrTCRC16( crc16 : Tcrc16 );
PROCEDURE PrTCRC16OF( crc16 : Tcrc16 );

 (* Gibt die 2 Bytes von crc16.crc aus. crc16 ist von TYPE crc16.
 *)

(*---------------------------------------------------------------------------*)

(*
PROCEDURE xPrCRC16( crc16 : xPcrc16 );
*)

 (* Dasselbe nur mit einem anderen Pointertyp (xPcrc16) aus dem Module CRC16.
 *)

(*---------------------------------------------------------------------------*)

(*
PROCEDURE yazeGenCRC(FName : TFName);
*)

 (* Generiert ueber das Module YAZEAG (CRC32 Routine in YAZE-AG) den CRC32.
    Diese CRC32 Routine ist in C Geschrieben und benutzt 16 Tabellen zu je 256
    Eintraegen um den CRC32 zu bilden. Ist deswegen besonders schnell.
    TFName ist hier in diesem Module definiert.
 *)

(*---------------------------------------------------------------------------*)
PROCEDURE GenCRC(FLE : PFileElement) : BOOLEAN;

 (* Generiert ueber das Z80-Assember Module CRC3216 gleichzeitig CRC32 und
    CRC16 in einem Durchgang.
    TFName ist in diesem Module definiert.
 *)

(*---------------------------------------------------------------------------*)

END CRCBuild.
