z80asm File formats (v11)
=========================

This document describes the object and library formats used by *z80asm*.

The object and library files are stored in binary form as a set of 
contiguous sections, i.e. each section follows the previous one. 

The files start with a signature that includes a version number and is 
verified by the assembler before trying to parse the file.

A set of file pointers at the start of the object file point to the 
start of each section existing the in the file, or contain *0xFFFFFFFF* 
(-1) if that section does not exist.


Data types
----------

The following object types exist in the file:

* *char* : one signed 8-bit value containing an ASCII character;

* *byte* : one unsigned 8-bit value;

* *word* : one 16-bit value, stored in low byte - high byte order;

* *dword* :	one signed 32-bit value, stored in low byte - high byte order;

* *string* : one byte containing the string length followed by the 
  characters of the string.

* *lstring* : one word containing the string length followed by the
  characters of the string.


Object file format
------------------

The format of the object file is as follows:

|Addr|Type   | Object
|----|----   | ------
|    |       | **File header**
|   0|8 chars| 'Z80RMF11' - file signature and version
|    |       |
|    | dword | File pointer to *Module Name*
|    | dword | File pointer to *Expressions*, may be -1
|    | dword | File pointer to *Defined Symbols*, may be -1
|    | dword | File pointer to *External Symbols*, may be -1
|    | dword | File pointer to *Sections*, may be -1
|    |       |
|    |       | **Expressions**
|    |       | List of expressions used in this module:
|    | char  | * *range* Range type: 
|    |       |   0   : end marker
|    |       |   'J' : 8-bit relative offset for JR
|    |       |   'U' : 8-bit unsigned
|    |       |   'S' : 8-bit signed
|    |       |   'C' : 16-bit value little-endian
|    |       |   'B' : 16-bit value big-endian
|    |       |   'L' : 32-bit signed
|    |       |   'u' : unsigned byte extended to 16 bits
|    |       |   's' : signed byte sign-extended to 16 bits
|    |       |   'P' : 24-bit pointer
|    |lstring| * *file* : Source file name where expression was used;
|    |       |   empty string to reuse the previous expression source file
|    | dword | * *line* : Line number in source file where expression was used
|    | string| * *section* : Section name where expression was used
|    | word  | * *ASMPC* : relative module code address of the start of the
|    |       |   assembly instruction to be used as *ASMPC* during
|    |       |   expression evaluation
|    | word  | * *patchptr* : relative module code patch pointer to store 
|    |       |   the result of evaluating the expression
|    | string| * *target_name* : name of the symbol that receives the result
|    |       |   of evaluating the expression, only used for type '=' 
|    |       |   expressions, empty string for the other types
|    |lstring| * *text* : expression text as parsed from the source file
|... |  ...  |   ...
|    |       |
|    |       | **Defined Symbols**
|    |       | List of symbols defined in this module:
|    | char  | * *scope* : Scope of the symbol:
|    |       |   0   : end marker
|    |       |   'L' : local symbol
|    |       |   'G' : public symbol
|    | char  | * *type* : Type of symbol:
|    |       |   'C' : constant symbol
|    |       |   'A' : offset to base address
|    |       |   '=' : computed at link time with defined expression
|    | string| * *section* : Section name where symbol was defined
|    | dword | * *value* : Absolute value for a constant, or the relative 
|    |       |   address to the start of the section for a relocatable address
|    | string| * *name* : Symbol name
|    | string| * *file* : Source file name where symbol was defined
|    | dword | * *line* : Line number in source file where symbol was defined
|... |  ...  |   ...
|    |       |
|    |       | ** External Symbols **
|    |       | List of external symbol names referred by this module:
|    | string| * *name* : external symbol name, empty string to signal last
|... | ...   | ...
|    |       |
|    |       | ** Module Name **
|    |       | Base name of the object file
|    | string| * *name* : module name
|    |       |
|    |       | **Sections**
|    |       | Contains the binary code of each section of the module
|    | dword | * *size* : defines the total code size, -1 to signal the end
|    | string| * *section* : source file section name
|    | dword | * *ORG Address* : contains the user defined ORG address for the
|    |       |   start of this section, or:
|    |       |     -1 for no ORG address was defined, or
|    |       |     -2 to split section to a different binary file
|    |       |   If multiple sections are given with an ORG address each, 
|    |       |   the assembler generates one binary file for each section with
|    |       |   a defined ORG, followed by all sections without one
|    | dword | * *ALIGN* : defines the address alignment of this section, or
|    |       |   -1 if not defined
|    |       |   The previous section is padded to align the start address of
|    |       |   this section
|    | bytes | * *code* : contains the binary code
|... | ...   | ...
|    |       |


Library file format
-------------------

The library file format is a sequence of object files with additional
structures.

|Addr|Type   | Object
|----|----   | ------
|    |       | **File header**
|   0|8 chars| 'Z80LMF11' - file signature and version
|    |       |
|    |       | **Object Modules**
|    |       | List of object modules in the library:
| hN | dword | * *next* : contains the file pointer of the next object file
|    |       |   in the library *hN*, or
|    |       |   *-1* if this is the last object in the library
|    | dword | * *size* : contains the size of this object file, or
|    |       |   *0* if this object files has been marked "deleted"
|    |       |   and will not be used
|    |       |   An empty library has next=-1 and size=0
| oN | bytes | * Object module binary data (already aligned on 4-byte boundary)
|... | ...   | ...
|    |       |


History
-------

* *v01* : original z80asm version

* *v02* : allow expressions to use standard C operators instead of the
  original (legacy) z80asm specific syntax

* *v03* : include the address of the start of the assembly instruction
  in the object file, so that expressions with ASMPC are correctly computed
  at link time;
  remove type 'X' symbols (global library), no longer used

* *v04* : include the source file location of expressions in order
  to give meaningful link-time error messages

* *v05* : include source code sections

* *v06* : incomplete implementation, fixed in version *v07*

* *v07* : include *DEFC* symbols that are defined as an expression using
  other symbols and are computed at link time, after all
  addresses are allocated

* *v08* : include a user defined ORG address per section

* *v09* : include the file and line number where each symbol was defined

* *v10* : allow a section alignment to be defined

* *v11* : allow big-endian 16-bit expressions to be patched;
  these big-endian values are used in the ZXN coper unit

