This document describes how it is possible to store useful data and/or code in the header of a disc file. (This is useful especially for coding competitions where there is a limit to the file size).
The method that is described here is known to work on the standard Amstrad CPC disc operating system (AMSDOS). It may not work if a different disc operating system is installed because it relies on the structure of AMSDOS's work RAM. This method will work even if there are additional ROMs attached.
The method described here relies on these facts:
Whenever a file is opened, AMSDOS will do the following:
If the checksums match, then a valid header has been identified.
If a valid header is detected, then the program data will immediatly follow the file header, otherwise the program data begins at the start of the file.
Notes:
To load a file, AMSDOS doesn't require all the variables in the header.
This table gives a list of the variables required by AMSDOS:
Offset | Length | Description |
---|---|---|
18 | 1 | Filetype (note 1) |
21 | 2 | Load address (note 2) |
24 | 2 | Length (note 3) |
26 | 2 | Execution Address (note 4) |
64 | 3 | Length (note 5) |
67 | 2 | Checksum (note 6) |
Notes:
The remaining bytes are not used and can be filled with code and/or program data.
The first 128 bytes of the file are stored in a 128-byte buffer.
This can be accessed immediatly after loading and will exist until another file is loaded.
The code to get the start of the stored header in AMSDOS's work RAM is:
;; get start of AMSDOS work RAM ld hl,(&be7d) ; this variable is initialised by AMSDOS ;; add on offset of loaded header ld bc,&e4 add hl,bc ;; HL = start of record buffer
The header written to the start of a file is located at offset &9f from the start of AMSDOS work RAM.
The header can't be filled with user data and/or code before the file is opened for output with "cas out open" because this function modifies the header data in the following way:
If the in-memory header is filled with user data and/or code after "cas out open" but before "cas out direct", then the following variables are changed:
Offset | Length | Variable |
---|---|---|
9 | 3 | Extension (note 8) |
18 | 1 | Filetype (note 1) |
19,20 | 2 | (note 7) |
21 | 2 | Load address (note 2) |
23 | 2 | (note 7) |
24 | 2 | Length (note 3) |
26 | 2 | Execution Address (note 4) |
64 | 2 | Length (note 5) |
67 | 2 | Checksum (note 6) |
NOTES:
Therefore to use all the bytes that are unused during loading the following method must be used:
If "cas out char" is used, then the in-memory header will be defined as ASCII, and the header will not be written to the output file, and the user has full control over the data that will be in the output file.