Page 1 of 2

Compiling ZGE for linux ARM / aarch64

Posted: Thu Apr 15, 2021 11:16 pm
by Ats
Today I was planning to run ZGE on RG351P, which is a great little console running under ARKOS, a linux for ARM. Perfect for emulation and homebrews.
Webp.net-resizeimage.jpg
Webp.net-resizeimage.jpg (156.81 KiB) Viewed 5013 times
And so I managed to cross compile FPC arm-linux from x86_64-linux. Quite easily in fact. I think I'm getting good at this :lol:
I noted everything to make a great tutorial afterwards.
I tested a simple Pascal program to verify it is running on the console, and it is! Everything is running smoothly until now.

Code: Select all

Program test;
uses Crt;

var
  i : longint;
begin
  ClrScr;
  WriteLn('Counting Down');
  for i:=10 downto 1 do
   begin
     WriteLn(i);
     Delay(1000);
   end;
  WriteLn('BOOM!!!');
end.
Then I started cross compiling ZzDC.dpr using :

/usr/local/lib/fpc/3.3.1/ppcrossarm -al -XXis -O2 -dZZDC_SDL -dMINIMAL -FU./Build/arm-linux/obj/ -Fu/usr/local/lib/fpc/3.3.1/units/arm-linux/pthreads/ -Fu/usr/local/lib/fpc/3.3.1/units/arm-linux/fcl-base/ -Fu/usr/local/lib/fpc/3.3.1/units/arm-linux/paszlib/ -Fu/usr/local/lib/fpc/3.3.1/units/arm-linux/hash/ -Fu/usr/local/lib/fpc/3.3.1/units/arm-linux/rtl-objpas/ -Tlinux -Parm -XParm-linux- -B -Mdelphi -FE./Build/arm-linux ZzDC.dpr


But I'm getting errors with the assembly inside ZExpressions.pas :

ZExpressions.pas(2504,5) Error: Unrecognized opcode call
ZExpressions.pas(2504,10) Error: Assembler syntax error
ZExpressions.pas(2506,16) Error: Unknown identifier "EAX"
ZExpressions.pas(2514,14) Error: Unknown identifier "ESP"
ZExpressions.pas(2529,7) Error: Unrecognized opcode fstp
ZExpressions.pas(2529,12) Error: Assembler syntax error
ZExpressions.pas(2530,7) Error: Unrecognized opcode wait


Maybe a missing lib? Or unreachable code in ZExpressions.pas due to $ifndef ? I'm a bit stuck.

Re: Compiling ZGE for linux ARM

Posted: Thu Apr 15, 2021 11:46 pm
by Kjell
Hi Ats,
Ats wrote: Thu Apr 15, 2021 11:16 pmMaybe a missing lib? Or unreachable code in ZExpressions.pas due to $ifndef?
You / the-compiler is trying to use a incompatible implementation of TExpExternalFuncCall.Execute. The selected implementation targets x86 CPUs running MacOS, which uses inline assembly instructions that don't exist on ARM CPUs. There is already a implementation that uses ARM instructions though ( for Android ), so it shouldn't take too much effort to adapt the code.

K

Re: Compiling ZGE for linux ARM

Posted: Fri Apr 16, 2021 7:30 am
by Ats
All right. So after checking ZExpression.pas more thoughtfully, here's what has already been defined :

2092: {$if defined(android)}
2173: {$elseif defined(cpux64)}
2435: {$elseif defined(cpux86) or defined(CPU32)}
2554: {$if defined(cpuaarch64)} // ARM64
2777: {$ifend}

Since the processor of the console is an ARM64 RK3326, maybe I could simply compile for aarch64 instead of arm?
The problem is that there are no instructions to create the cross-compiler for aarch64: https://wiki.lazarus.freepascal.org/Cross_compiling

I'll see if I can do it without those 8)

Edit:
And so I simply replaced arm per aarch64 in:
sudo make crossinstall CPU_TARGET=aarch64 OS_TARGET=linux CROSSBINDIR=/home/ats/fpc/binutils/ OPT=-dFPC_ARMEL INSTALL_PREFIX=/usr/local

But I end up with errors:

make[6]: Entering directory '/home/ats/fpc/rtl/linux'
make[6]: '/home/ats/fpc/rtl/units/aarch64-linux' is up to date.
make[6]: Leaving directory '/home/ats/fpc/rtl/linux'
/home/ats/fpc/binutils//as -o /home/ats/fpc/rtl/units/aarch64-linux/prt0.o aarch64/prt0.as
aarch64/prt0.as: Assembler messages:
aarch64/prt0.as:14: Error: ARM register expected -- `ldr x10,=__dl_fini'
aarch64/prt0.as:15: Error: ARM register expected -- `str x0,[x10]'
aarch64/prt0.as:22: Error: ARM register expected -- `mov x29,#0'
aarch64/prt0.as:25: Error: ARM register expected -- `ldr x1,[sp]'
aarch64/prt0.as:26: Error: ARM register expected -- `add x2,sp,#8'
aarch64/prt0.as:27: Error: ARM register expected -- `add x11,x1,#1'
aarch64/prt0.as:28: Error: ARM register expected -- `add x11,x2,x11,lsl#3'
aarch64/prt0.as:31: Error: ARM register expected -- `ldr x10,=operatingsystem_parameter_argc'
aarch64/prt0.as:32: Error: ARM register expected -- `str x1,[x10]'
aarch64/prt0.as:33: Error: ARM register expected -- `ldr x10,=operatingsystem_parameter_argv'
aarch64/prt0.as:34: Error: ARM register expected -- `str x2,[x10]'
aarch64/prt0.as:35: Error: ARM register expected -- `ldr x10,=operatingsystem_parameter_envp'
aarch64/prt0.as:36: Error: ARM register expected -- `str x11,[x10]'
aarch64/prt0.as:37: Error: ARM register expected -- `ldr x10,=__stkptr'
aarch64/prt0.as:38: Error: ARM register expected -- `mov x6,sp'
aarch64/prt0.as:39: Error: ARM register expected -- `str x6,[x10]'
aarch64/prt0.as:47: Error: ARM register expected -- `ldr x10,=__dl_fini'
aarch64/prt0.as:48: Error: ARM register expected -- `ldr x0,[x10]'
aarch64/prt0.as:49: Error: selected processor does not support `cbz x0,.Lexit' in ARM mode
aarch64/prt0.as:50: Error: bad instruction `blr x0'
aarch64/prt0.as:52: Error: ARM register expected -- `ldr x10,=operatingsystem_result'
aarch64/prt0.as:53: Error: ARM register expected -- `ldr w0,[x10]'
aarch64/prt0.as:54: Error: ARM register expected -- `mov w8,#94'
make[5]: *** [Makefile:4376: prt0.o] Error 1
make[5]: Leaving directory '/home/ats/fpc/rtl/linux'
make[4]: *** [Makefile:3941: fpc_smart] Error 2
make[4]: Leaving directory '/home/ats/fpc/rtl/linux'
make[3]: *** [Makefile:2287: linux_smart] Error 2
make[3]: Leaving directory '/home/ats/fpc/rtl'
make[2]: *** [Makefile:2926: rtl_smart] Error 2
make[2]: Leaving directory '/home/ats/fpc'
make[1]: *** [Makefile:3169: build-stamp.aarch64-linux] Error 2
make[1]: Leaving directory '/home/ats/fpc'
make: *** [Makefile:3223: crossinstall] Error 2


Assembly, again... :roll:

Re: Compiling ZGE for linux ARM

Posted: Fri Apr 16, 2021 8:20 am
by VilleK
Please check here for information about building Fpc for aarch64: https://wiki.freepascal.org/macOS_Big_S ... on_Support

I've spent a bit of time on this recently because we are in the process of making FL Studio (with the ZGameEditor Visualizer plugin) running natively on Apple M1 CPUs.

Re: Compiling ZGE for linux ARM

Posted: Fri Apr 16, 2021 1:36 pm
by Ats
After a few tries to cross compile for linux aarch64 from linux x86_64, I went the other way around: compiling fpc directly on a Raspberry 3, since it is supposed to be an ARM 64 processor. But when I compile Free Pascal using:

svn checkout http://svn.freepascal.org/svn/fpc/trunk ~/fpc
cd ~/fpc
sudo make all
sudo make install

It gives me the same ppcarm elf. Not the 64 one. So I'm at the same point here...

By the way, I was surprised how smooth raspbian was working on the raspberry :o

Re: Compiling ZGE for linux ARM

Posted: Fri Apr 16, 2021 2:16 pm
by Ats
The 32/64 problems is coming from raspbian which is 32bits OS. https://www.raspberrypi.org/forums/view ... p?t=251721

I'll try again with a 64bits OS when I'll get home: https://downloads.raspberrypi.org/raspios_arm64/images/

Re: Compiling ZGE for linux ARM

Posted: Fri Apr 16, 2021 10:45 pm
by Ats
It went a bit farther with my raspberry under raspbian 64. It compiles but I have a problem during the linking:

Linking ./Build/aarch64-linux/ZzDC
ZzDC.dpr(94) Warning: "crtbegin.o" not found, this will probably cause a linking failure
ZzDC.dpr(94) Warning: "crtend.o" not found, this will probably cause a linking failure
/usr/bin/ld: ./Build/aarch64-linux/obj/ZExpressions.o: in function `ZEXPRESSIONS_$$_GENERATETRAMPOLINE$LONGINT$PCHAR$POINTER$$POINTER':
ZExpressions.pas:(.text.n_zexpressions_$$_generatetrampoline$longint$pchar$pointer$$pointer+0x2fc): undefined reference to `sys_icache_invalidate'
ZzDC.dpr(94) Error: Error while linking

I tried adding -Fu/usr/lib/gcc/aarch64-linux-gnu/10 where those files are located, without success.
I also tried -Fl and -Fo instead.


Edit:
Just find out about link.res useful information, which don't seems to search for crtbegin/end.o in the folder I provided in the command line:

Code: Select all

SEARCH_DIR("/lib/aarch64-linux-gnu/")
SEARCH_DIR("/usr/lib/aarch64-linux-gnu/")
SEARCH_DIR("/lib/")
SEARCH_DIR("/usr/lib/")
SEARCH_DIR("/usr/lib/printer-driver-escpr/")
SEARCH_DIR("/usr/lib/environment.d/")
SEARCH_DIR("/usr/lib/raspi-config/")
SEARCH_DIR("/usr/lib/notification-daemon/")
SEARCH_DIR("/usr/lib/xorg/")
SEARCH_DIR("/usr/lib/rsyslog/")
SEARCH_DIR("/usr/lib/vnc/")
SEARCH_DIR("/usr/lib/init/")
SEARCH_DIR("/usr/lib/rp-prefapps/")
SEARCH_DIR("/usr/lib/X11/")
SEARCH_DIR("/usr/lib/lsb/")
SEARCH_DIR("/usr/lib/terminfo/")
SEARCH_DIR("/usr/lib/lxinput/")
SEARCH_DIR("/usr/lib/menu-cache/")
SEARCH_DIR("/usr/lib/qpdfview/")
SEARCH_DIR("/usr/lib/gcc/")
SEARCH_DIR("/usr/lib/tc/")
SEARCH_DIR("/usr/lib/modprobe.d/")
SEARCH_DIR("/usr/lib/rc-gui/")
SEARCH_DIR("/usr/lib/mime/")
SEARCH_DIR("/usr/lib/blt2.5/")
SEARCH_DIR("/usr/lib/gvfs/")
SEARCH_DIR("/usr/lib/NetworkManager/")
SEARCH_DIR("/usr/lib/ifupdown/")
SEARCH_DIR("/usr/lib/file/")
SEARCH_DIR("/usr/lib/valgrind/")
SEARCH_DIR("/usr/lib/emacsen-common/")
SEARCH_DIR("/usr/lib/compat-ld/")
SEARCH_DIR("/usr/lib/kernel/")
SEARCH_DIR("/usr/lib/sudo/")
SEARCH_DIR("/usr/lib/groff/")
SEARCH_DIR("/usr/lib/gold-ld/")
SEARCH_DIR("/usr/lib/sysusers.d/")
SEARCH_DIR("/usr/lib/sysctl.d/")
SEARCH_DIR("/usr/lib/ispell/")
SEARCH_DIR("/usr/lib/tasksel/")
SEARCH_DIR("/usr/lib/piclone/")
SEARCH_DIR("/usr/lib/python3/")
SEARCH_DIR("/usr/lib/locale/")
SEARCH_DIR("/usr/lib/ssl/")
SEARCH_DIR("/usr/lib/pipanel/")
SEARCH_DIR("/usr/lib/packagekit/")
SEARCH_DIR("/usr/lib/pkgconfig/")
SEARCH_DIR("/usr/lib/pm-utils/")
SEARCH_DIR("/usr/lib/udisks2/")
SEARCH_DIR("/usr/lib/debug/")
SEARCH_DIR("/usr/lib/python3.9/")
SEARCH_DIR("/usr/lib/binfmt.d/")
SEARCH_DIR("/usr/lib/girepository-1.0/")
SEARCH_DIR("/usr/lib/pulse-14.2/")
SEARCH_DIR("/usr/lib/python2.7/")
SEARCH_DIR("/usr/lib/python3.7/")
SEARCH_DIR("/usr/lib/bfd-plugins/")
SEARCH_DIR("/usr/lib/man-db/")
SEARCH_DIR("/usr/lib/arandr/")
SEARCH_DIR("/usr/lib/gnupg2/")
SEARCH_DIR("/usr/lib/crda/")
SEARCH_DIR("/usr/lib/cups/")
SEARCH_DIR("/usr/lib/gui-pkinst/")
SEARCH_DIR("/usr/lib/runit-helper/")
SEARCH_DIR("/usr/lib/tcltk/")
SEARCH_DIR("/usr/lib/thunar-archive-plugin/")
SEARCH_DIR("/usr/lib/pypy/")
SEARCH_DIR("/usr/lib/dhcpcd/")
SEARCH_DIR("/usr/lib/firmware/")
SEARCH_DIR("/usr/lib/raspberrypi-sys-mods/")
SEARCH_DIR("/usr/lib/modules/")
SEARCH_DIR("/usr/lib/dhcpcd5/")
SEARCH_DIR("/usr/lib/klibc/")
SEARCH_DIR("/usr/lib/resolvconf/")
SEARCH_DIR("/usr/lib/openssh/")
SEARCH_DIR("/usr/lib/modules-load.d/")
SEARCH_DIR("/usr/lib/gnupg/")
SEARCH_DIR("/usr/lib/arm-linux-gnueabihf/")
SEARCH_DIR("/usr/lib/chromium/")
SEARCH_DIR("/usr/lib/apt/")
SEARCH_DIR("/usr/lib/tmpfiles.d/")
SEARCH_DIR("/usr/lib/sasl2/")
SEARCH_DIR("/usr/lib/systemd/")
SEARCH_DIR("/usr/lib/aspell/")
SEARCH_DIR("/usr/lib/dpkg/")
SEARCH_DIR("/usr/lib/dbus-1.0/")
SEARCH_DIR("/usr/lib/console-setup/")
SEARCH_DIR("/usr/lib/udev/")
SEARCH_DIR("/usr/lib/colord/")
SEARCH_DIR("/usr/lib/policykit-1/")
SEARCH_DIR("/usr/local/lib/fpc/3.3.1/units/aarch64-linux/pthreads/")
SEARCH_DIR("/usr/local/lib/fpc/3.3.1/units/aarch64-linux/fcl-base/")
SEARCH_DIR("/usr/local/lib/fpc/3.3.1/units/aarch64-linux/paszlib/")
SEARCH_DIR("/usr/local/lib/fpc/3.3.1/units/aarch64-linux/hash/")
SEARCH_DIR("/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl-objpas/")
SEARCH_DIR("/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/")
SEARCH_DIR("/usr/local/lib/fpc/3.3.1/")
INPUT(
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/cprt0.o
/lib/aarch64-linux-gnu/crti.o
./Build/aarch64-linux/obj/ZzDC.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/system.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/objpas.o
./Build/aarch64-linux/obj/ZClasses.o
./Build/aarch64-linux/obj/ZOpenGL.o
./Build/aarch64-linux/obj/ZBitmap.o
./Build/aarch64-linux/obj/BitmapProducers.o
./Build/aarch64-linux/obj/ZPlatform.o
./Build/aarch64-linux/obj/ZApplication.o
./Build/aarch64-linux/obj/ZLog.o
./Build/aarch64-linux/obj/Animators.o
./Build/aarch64-linux/obj/Meshes.o
./Build/aarch64-linux/obj/Renderer.o
./Build/aarch64-linux/obj/ZMath.o
./Build/aarch64-linux/obj/Commands.o
./Build/aarch64-linux/obj/ZExpressions.o
./Build/aarch64-linux/obj/Collision.o
./Build/aarch64-linux/obj/Steering.o
./Build/aarch64-linux/obj/AudioPlayer.o
./Build/aarch64-linux/obj/AudioComponents.o
./Build/aarch64-linux/obj/ImplicitMeshes.o
./Build/aarch64-linux/obj/ZFile.o
./Build/aarch64-linux/obj/NanoJpeg.o
./Build/aarch64-linux/obj/GLDrivers.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/math.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/paszlib/zstream.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl-objpas/strutils.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/sysutils.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/linux.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/unix.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/errors.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/sysconst.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/unixtype.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/baseunix.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/unixutil.o
./Build/aarch64-linux/obj/SDL.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/fcl-base/syncobjs.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/dl.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/pthreads/pthreads.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/initc.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/ctypes.o
./Build/aarch64-linux/obj/BeRoAudioOGGVorbisTremor.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/classes.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/paszlib/zbase.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/paszlib/gzio.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/types.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/typinfo.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/rtlconsts.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/rtl/sortbase.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/hash/crc.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/paszlib/zdeflate.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/paszlib/zinflate.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/paszlib/trees.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/paszlib/adler.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/paszlib/infblock.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/paszlib/infutil.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/paszlib/infcodes.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/paszlib/inftrees.o
/usr/local/lib/fpc/3.3.1/units/aarch64-linux/paszlib/inffast.o
)
INPUT(
-lpthread
-lSDL
-ldl
)
GROUP(
-lc
)
INPUT(
/lib/aarch64-linux-gnu/crtn.o
)
SECTIONS
{
  .fpcdata           :
  {
    KEEP (*(.fpc .fpc.n_version .fpc.n_links))
  }
  .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }
}
INSERT AFTER .data;
So I added links to those:
sudo ln /usr/lib/gcc/aarch64-linux-gnu/10/crtbegin.o /usr/lib/aarch64-linux-gnu/crtbegin.o
sudo ln /usr/lib/gcc/aarch64-linux-gnu/10/crtend.o /usr/lib/aarch64-linux-gnu/crtend.o

Now there are no warnings about those at the end of the compilation, but it still crash with the same error message.

Re: Compiling ZGE for linux ARM

Posted: Sat Apr 17, 2021 12:48 pm
by VilleK
Try commenting out the call to

sys_icache_invalidate

In Zexpressions.

It should only be needed on Mac I think.

Re: Compiling ZGE for linux ARM

Posted: Sat Apr 17, 2021 1:51 pm
by Ats
Thanks, with the two sys_icache_invalidate lines commented out, the player is building nice.

It's almost working:
Zge projects compiled with linux aarch64 bin gives the exact same problems as linux x86_64 bin :lol:

Re: Compiling ZGE for linux ARM

Posted: Sat Apr 17, 2021 2:10 pm
by VilleK
I wonder if there is something wrong in how ZGE calls SDL in a 64-bit environment. I could not find a prebuilt 64-bit DLL for Windows to test with.

On this platform, do you build SDL yourself or do you use a prebuilt binary? From where do you get the SDL files?

Re: Compiling ZGE for linux ARM

Posted: Sat Apr 17, 2021 4:27 pm
by Ats
Since compilation isn't working with the included libsdl2-dev, I'm installing libsdl1.2-dev simply like that:

sudo apt-get update
sudo apt-get install libsdl1.2-dev

It works for i386 linux. But you're right, maybe it could come from this...
https://www.libsdl.org/download-1.2.php

I'm formatting the raspberry again to see what's going on with libsdl2 :wink:

Re: Compiling ZGE for linux ARM

Posted: Sat Apr 17, 2021 6:13 pm
by VilleK
Note that SDL2 is not backwards compatible with SDL1. I should probably update ZGE to SDL2 but I have no idea how much work it is.

Re: Compiling ZGE for linux ARM

Posted: Sat Apr 17, 2021 6:58 pm
by Ats
I just searched your projects for all the old SDL functions, listed here: https://wiki.libsdl.org/MigrationGuide
And you are using them at least 3 times each.
That's not huge, but there's definitely some work :?

Re: Compiling ZGE for linux ARM

Posted: Sun Apr 18, 2021 7:53 am
by VilleK
Thanks for that link. I'll try to make the transition to SDL2 in the next few days.

Re: Compiling ZGE for linux ARM

Posted: Mon Apr 19, 2021 8:13 am
by VilleK
Committed support for SDL2. It was very straightforward following that guide. Tested working with 64-bit Windows.

You need to add "./3rdparty/sdl2" to the unit search path when building.