Difference between revisions of "Z80"

From Free Pascal wiki
Jump to navigationJump to search
m (Add page template; categorise page (in template))
(14 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{Z80}}
 +
 
== Overview ==
 
== Overview ==
  
A Z80 port is currently under development in a branch:
+
A Z80 port is currently under development in FPC trunk:
  
  https://svn.freepascal.org/svn/fpc/branches/z80/
+
  https://svn.freepascal.org/svn/fpc/trunk
  
 
To check it out, use subversion:
 
To check it out, use subversion:
Line 9: Line 11:
 
  mkdir fpc-z80
 
  mkdir fpc-z80
 
  cd fpc-z80
 
  cd fpc-z80
  svn co https://svn.freepascal.org/svn/fpc/branches/z80/ fpc
+
  svn co https://svn.freepascal.org/svn/fpc/trunk fpc
  
 
== Requirements ==
 
== Requirements ==
Line 39: Line 41:
 
They 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 [https://rpmfusion.org/ RPMFusion - nonfree].
 
They 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 [https://rpmfusion.org/ RPMFusion - nonfree].
  
Producing TZX files requires a tool, called '''ihx2tzx''', which is written in Pascal and is available in the ''utils/ihx2tzx'' directory of the compiler.
+
Another emulator that works is Fuse:
 +
 
 +
http://fuse-emulator.sourceforge.net/fuse.php
 +
 
 +
Producing TZX files requires a tool, called '''ihxutil''', which is written in Pascal and is available in the ''utils/ihxutil'' directory of the compiler.
 +
 
 +
=== MSX-DOS ===
 +
 
 +
The 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.
 +
 
 +
Generating 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.
 +
 
 +
More information can be found [[MSX-DOS|here]].
  
 
== Building ==
 
== Building ==
Line 45: Line 59:
 
Here's the build script I use:
 
Here's the build script I use:
  
 +
<syntaxhighlight lang=bash>
 
  #! /bin/sh
 
  #! /bin/sh
 
   
 
   
Line 51: Line 66:
 
  FPC_Z80_DIR=/home/nickysn/tralala/fpc-z80
 
  FPC_Z80_DIR=/home/nickysn/tralala/fpc-z80
 
  STARTPP=fpc
 
  STARTPP=fpc
 +
BINUTILSPREFIX=
 
   
 
   
  CROSSOPT=-O-
+
  CROSSOPT=-O-1
 
  export CROSSOPT
 
  export CROSSOPT
  OPT=
+
  OPT="-CX -XXs"
 
  if [ -n "$EXTRAOPT" ]
 
  if [ -n "$EXTRAOPT" ]
 
  then
 
  then
Line 66: Line 82:
 
   
 
   
 
  cd $FPC_Z80_DIR/fpc
 
  cd $FPC_Z80_DIR/fpc
  make -j `nproc` clean all PP=$STARTPP BINUTILSPREFIX=
+
  make -j `nproc` clean PP=$STARTPP BINUTILSPREFIX=$BINUTILSPREFIX
  make -j `nproc` crossinstall PP=$FPC_Z80_DIR/fpc/compiler/ppcrossz80 INSTALL_PREFIX=$FPC_Z80_DIR/zxspectrum-snapshot BINUTILSPREFIX=
+
make -j `nproc` all PP=$STARTPP BINUTILSPREFIX=$BINUTILSPREFIX
 +
  make -j `nproc` crossinstall PP=$FPC_Z80_DIR/fpc/compiler/ppcrossz80 INSTALL_PREFIX=$FPC_Z80_DIR/zxspectrum-snapshot BINUTILSPREFIX=$BINUTILSPREFIX
 +
</syntaxhighlight>
 +
 
 +
Note that, if you use the SDCC package from the official Fedora repositories, you might need to replace
 +
 
 +
BINUTILSPREFIX=
 +
 
 +
with
 +
 
 +
BINUTILSPREFIX=sdcc-
 +
 
 +
When compiling programs, you should always enable smartlinking by adding these two options to the compiler:
 +
 
 +
-CX -XX
  
 
== Status ==
 
== Status ==
  
The code generator is not yet fully complete. The full RTL doesn't compile yet. However, simple programs like this work on the ZX Spectrum:
+
As 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:
  
 +
<syntaxhighlight lang=pascal>
 
  program Hello;
 
  program Hello;
 
  var
 
  var
 
   b: byte;
 
   b: byte;
 
  begin
 
  begin
   PrintChar('F');
+
   Write('FPC');
  PrintChar('P');
 
  PrintChar('C');
 
 
   for b := 0 to 255 do
 
   for b := 0 to 255 do
     PrintHexByte(b);
+
     Write(HexStr(b, 2));
 
  end.
 
  end.
 +
</syntaxhighlight>
  
 
Here's what this program produces in the FBZX emulator:
 
Here's what this program produces in the FBZX emulator:
  
 
[[File:ZX_Spectrum_hello_world_FBZX_emulator_screenshot.png]]
 
[[File:ZX_Spectrum_hello_world_FBZX_emulator_screenshot.png]]

Revision as of 06:29, 1 September 2020

English (en)

Overview

A Z80 port is currently under development in FPC trunk:

https://svn.freepascal.org/svn/fpc/trunk

To check it out, use subversion:

mkdir fpc-z80
cd fpc-z80
svn co https://svn.freepascal.org/svn/fpc/trunk fpc

Requirements

The assembler and linker from the SDCC package are required:

http://sdcc.sourceforge.net/

Targets

Embedded

The compiler produces files in the IHX format (Intel HEX Format).

ZX Spectrum

The 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:

https://www.rastersoft.com/programas/fbzx.html

ROM images for the emulator are available here:

http://www.chiark.greenend.org.uk/~cjwatson/code/spectrum-roms/

and here:

http://www.shadowmagic.org.uk/spectrum/roms.html

They 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.

Another emulator that works is Fuse:

http://fuse-emulator.sourceforge.net/fuse.php

Producing TZX files requires a tool, called ihxutil, which is written in Pascal and is available in the utils/ihxutil directory of the compiler.

MSX-DOS

The 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.

Generating 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.

More information can be found here.

Building

Here's the build script I use:

 #! /bin/sh
 
 set -e
 
 FPC_Z80_DIR=/home/nickysn/tralala/fpc-z80
 STARTPP=fpc
 BINUTILSPREFIX=
 
 CROSSOPT=-O-1
 export CROSSOPT
 OPT="-CX -XXs"
 if [ -n "$EXTRAOPT" ]
 then
     OPT+=" $EXTRAOPT"
 fi
 export OPT
 export CPU_TARGET=z80
 export OS_TARGET=zxspectrum
 export OS_SOURCE=`$STARTPP -iSO`
 export CPU_SOURCE=`$STARTPP -iSP`
 
 cd $FPC_Z80_DIR/fpc
 make -j `nproc` clean PP=$STARTPP BINUTILSPREFIX=$BINUTILSPREFIX
 make -j `nproc` all PP=$STARTPP BINUTILSPREFIX=$BINUTILSPREFIX
 make -j `nproc` crossinstall PP=$FPC_Z80_DIR/fpc/compiler/ppcrossz80 INSTALL_PREFIX=$FPC_Z80_DIR/zxspectrum-snapshot BINUTILSPREFIX=$BINUTILSPREFIX

Note that, if you use the SDCC package from the official Fedora repositories, you might need to replace

BINUTILSPREFIX=

with

BINUTILSPREFIX=sdcc-

When compiling programs, you should always enable smartlinking by adding these two options to the compiler:

-CX -XX

Status

As 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:

 program Hello;
 var
   b: byte;
 begin
   Write('FPC');
   for b := 0 to 255 do
     Write(HexStr(b, 2));
 end.

Here's what this program produces in the FBZX emulator:

ZX Spectrum hello world FBZX emulator screenshot.png