Sign in Join
Wiki copytemporary

Z80: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Overview
22
33A Z80 port is currently under development in Git "main" branch. To check it out, use:
44
5-`mkdir fpc-z80`\
6-`cd fpc-z80`\
7-`git clone `[`https://gitlab.com/freepascal.org/fpc/source.git`](https://gitlab.com/freepascal.org/fpc/source.git)` fpc`
5+```text
6+mkdir fpc-z80
7+cd fpc-z80
8+git clone https://gitlab.com/freepascal.org/fpc/source.git fpc
9+```
810
911## Requirements
1012
1113The compiler now includes an internal assembler and linker, so no extra packages are needed. Optionally, the assembler and linker from the SDCC package can be used as well:
1214
1315<http://sdcc.sourceforge.net/>
1416
1517## Targets
1618
1719### Embedded
1820
1921The compiler produces files in the IHX format (Intel HEX Format).
2022
2123### ZX Spectrum
2224
2325The compiler produces ZX Spectrum tape files in the TZX file format. They are suitable for running in emulators. An open-source emulator that works is FBZX:
2426
2527<https://www.rastersoft.com/programas/fbzx.html>
2628
2729ROM images for the emulator are available here:
2830
2931<http://www.chiark.greenend.org.uk/~cjwatson/code/spectrum-roms/>
3032
3133and here:
3234
3335<http://www.shadowmagic.org.uk/spectrum/roms.html>
3436
3537They are copyrighted and are not free (as in freedom), but are legally available for use in emulators (not in real hardware), and are also repackaged and redistributed in Linux repositories such as [RPMFusion - nonfree](https://rpmfusion.org/).
3638
3739Another emulator that works is Fuse:
3840
3941<http://fuse-emulator.sourceforge.net/fuse.php>
4042
4143Producing TZX files requires a tool, called **ihxutil**, which is written in Pascal and is available in the *utils/ihxutil* directory of the compiler.
4244
4345### MSX-DOS
4446
4547The compiler produces flat COM files that can be run from the MSX-DOS environment. Please note that you need to take extreme care that you don't link in too much code especially if you want to also include a heap and a stack. The default size for the heap is 256 and for the stack 1024 and these can be changed using the *\$MEMORY* directive.
4648
4749Generating the COM files requires the **ihxutil** utility which is written in Pascal and is available in the *utils/ihxutil* directory of the Free Pascal sources.
4850
4951More information can be found [[MSX-DOS|here]].
5052
5153## Building
5254
5355Here's the build script I use:
5456
5557``` bash
5658#!/bin/sh
5759
5860set -e
5961
6062FPC_Z80_DIR=/home/nickysn/tralala/fpc-z80
6163STARTPP=fpc
6264BINUTILSPREFIX=
6365
6466CROSSOPT=-O-1
6567export CROSSOPT
6668OPT="-CX -XXs"
6769if [ -n "$EXTRAOPT" ]
6870then
6971 OPT+=" $EXTRAOPT"
7072fi
7173export OPT
7274export CPU_TARGET=z80
7375export OS_TARGET=zxspectrum
7476export OS_SOURCE=`$STARTPP -iSO`
7577export CPU_SOURCE=`$STARTPP -iSP`
7678
7779cd $FPC_Z80_DIR/fpc
7880make -j `nproc` clean PP=$STARTPP BINUTILSPREFIX=$BINUTILSPREFIX
7981make -j `nproc` all PP=$STARTPP BINUTILSPREFIX=$BINUTILSPREFIX
8082make -j `nproc` crossinstall PP=$FPC_Z80_DIR/fpc/compiler/ppcrossz80 INSTALL_PREFIX=$FPC_Z80_DIR/zxspectrum-snapshot BINUTILSPREFIX=$BINUTILSPREFIX
8183```
8284
8385Note that, if you use the SDCC package from the official Fedora repositories, you might need to replace
8486
85-`BINUTILSPREFIX=`
87+```text
88+BINUTILSPREFIX=
89+```
8690
8791with
8892
89-`BINUTILSPREFIX=sdcc-`
93+```text
94+BINUTILSPREFIX=sdcc-
95+```
9096
9197When compiling programs, you should always enable smartlinking by adding these two options to the compiler:
9298
93-`-CX -XX`
99+```text
100+-CX -XX
101+```
94102
95103## Status
96104
97105As of Apr 27, 2020, the code generator is stable enough to compile the full system unit. Standard output via write/writeln works now. As of May 17, 2020, optimization level 1 also works, so you can try compiling with -O1. Note that there aren't many Z80-specific optimizations implemented yet, but enabling optimization still has some small effect. Here's a simple program that works:
98106
99107``` pascal
100108program Hello;
101109var
102110 b: byte;
103111begin
104112 Write('FPC');
105113 for b := 0 to 255 do
106114 Write(HexStr(b, 2));
107115end.
108116```
109117
110118Here's what this program produces in the FBZX emulator:
111119
112120![](/wiki-images/c/c9/ZX_Spectrum_hello_world_FBZX_emulator_screenshot.png)
113121
114122---
115123*From the Free Pascal wiki, page "Z80", as it stood on 2022-05-24 after a change by Alextp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history