AmigaOS

From Lazarus wiki
Revision as of 13:31, 26 April 2014 by Chain-Q (talk | contribs) (warning about obsolete info added)
Jump to navigationJump to search

WARNING: This page contains obsolete information about the former AmigaOS4/PowerPC port attempt. About classic Amiga (OS 3.x, Motorola 680x0) port, please see the relevant page. Maintainers welcomed to pick up the OS4 version, the Free Pascal SVN still contains all related code.

This page is about how to realize a recent Free Pascal port to Hyperion's AmigaOS4 and possibly also to classic Amiga computers. This page was done by Karoly 'Chain-Q' Balogh, author of MorphOS port.

Frequently Asked Questions

  • Q: Is it possible to port Free Pascal Compiler to AmigaOS4?
    A: Yes.
  • Q: Is it possible to port Free Pascal Compiler to AmigaOS4, with simple recompilation?
    A: No. A lot of additional steps needed. See below.
  • Q: Don't you want to do the port?
    A: I'd like to create the port, but I don't have any hardware capable of running AmigaOS4. If you want me to do the port, send me hardware to run OS4 on. Currently this can be an AmigaOne mainboard, or also a phase5/DCE CyberstormPPC accelerator for Commodore Amiga 3000/4000 computers, with valid OS4 license. Contact me if you're interrested.
    A: UPDATE 2006.01.02.: Sometime in december 2005, i got an Eyetech uA1 computer from a friendly AmigaOS4 user, known as Colin Camper. Thanks to his support, Free Pascal will be ported to AmigaOS4. Further details will be available later.
  • Q: What if I'd like to do the port?
    A: Great! Welcome in the team. Start with reading the the steps required to do it below. If you've more questions, feel free to ask me in private e-mail, or on any Free Pascal Mailing List.
  • Q: Can i do the AmigaOS4 port using Free Pascal 2.0.0 for MorphOS?
    A: Yes. Any version of 2.0.0 will do for whatever platform you like, on x86 or PowerPC processors.
  • Q: Can i do the AmigaOS4 port using Free Pascal 1.0.10a for AmigaOS/m68k?
    A: No. And again: NO. That compiler is very old, and doesn't support quite a lot of things needed to compile the recent compiler sources, having PowerPC support. Simply forget about it.
  • Q: MorphOS sucks and it's dead. You should concentrate on OS4!
    A: You seriously need to get a life.

Equipment and knowledge needed

  • Any computer capable of running AmigaOS4. Basic OS4 compiler port might be possible on MorphOS, using OS4Emu, but this is only recommended for sado-masochists.
  • Any computer capable of running 2.0.0 version of Free Pascal Compiler. Linux/i386 or Linux/PPC recommended. This will be our host platform, while OS4 will be referred as target platform.
  • Recent GNU LD/AS for AmigaOS4, or any linker capable of creating ELF32-Amiga executables. An example is the great vlink by Frank Wille. Of course, you also need to know how to use such tools. OS4 targetted cross-AS and cross-LD would be nice as well, and makes life easier, but not required.
  • Knowledge about compiler internals, since you'll probably need to modify the very internals of the compiler.
  • PowerPC assembly knowledge, and understanding of SysV ABI. You need to know how arguments passed between OS functions and the application programs. You need to know how libraries and devices work on the very low level, and you also need some PowerPC assembly knowledge, because at least when starting, you'll do quite a lot of manual hacks into the generated assembler files.
  • Understanding of C language. Since most software for AmigaOS-like systems are written using C language, you'll only find examples in C. It's nice if you understand C language quite well, because otherwise you will not be able to rewrite such code to Pascal.
  • Read the FPC/MorphOS wiki page, hopefully it will make a lot of things more clear.

Steps needed to port Free Pascal to PowerPC AmigaOS

  1. First you need to add AmigaOS4 target support to the compiler. This can be done by creating i_aos4.pas and t_aos4.pas units, or extending i_amiga.pas and t_amiga.pas units to support PowerPC. The units are in /compiler/systems directory.
    • Update (2006.01.09): this step is partially done (50%). The compiler knows about AmigaOS4 target (as AmigaOS on PowerPC), but still misses linker support.
  2. The hardest part is to provide API support for OS4. The current function declaration syntax is heavily tied to MorphOS, and it will generate calls to dynamic libraries using MorphOS ABI. (Check /rtl/morphos/execf.inc for an example.) To modify this, you need to add support code into /compiler/pdecsub.pas, pd_syscall function, /compiler/powerpc/nppccal.pas tppccallnode.do_syscall function, and /compiler/powerpc/cpupara.pas tppcparamanager.parseparaloc function. Be prepared to repeat this step a few times, since others may modify parts of compiler which may break your code.
    • Update (2006.01.09): this step is partially done (80%). The compiler now can generate code to call functions through AmigaOS4 interfaces. What's still missing is support for hidden arguments, like the Self pointer to interface as first argument.
  3. Create startup code for AmigaOS4. This is a short stub, written in assembly language, which allocates stack, boots the RTL (runtime-libraries), then the program itself. A trivial solution would be to modify MorphOS version, which is available in /rtl/morphos/prt0.as. It's currently using MorphOS API to allocate stack, but it's not that hard to solve this problem.
    • Update (2006.01.09): This step is done.
  4. Bootstrap a simple System unit. System unit skeletons are available, but it's possible to modify existing System units as well. Try to make a simple hello world! running. Use host platform to build units and executables, while test them on OS4.
  5. Extend System, DOS and Sysutils unit until you're able to compile the compiler itself. When doing MorphOS port, this was the hardest step, because the old 68k version was in really bad shape, had a huge amount of 68k assembly hacks, and it was really a nightmare to clean up and port. In case of OS4 port, your job is easy, simply reuse most of the MorphOS version, while extend it with AmigaOS4-specific functions and optimizations.
  6. Build an OS4 hosted compiler. If you did everything right, you will be able to compile the compiler then on OS4, and voila', you got Free Pascal Compiler on AmigaOS4.
  7. Extend the RTL to have more system-specific parts, and also to support more of the standard Pascal constructs like on other systems. This is more or less an endless step, since FPC is a moving target, and it's recommended to keep up with the developments.

Steps needed to port Free Pascal to Motorola 680x0-based AmigaOS

  1. Implement and finish the currently broken 68k code-generator for 2.0.0 series. It can be found in /compiler/m68k directory.
  2. Once you've finished, you need to follow steps very similar to AmigaOS/PowerPC port.

Hint: If you need manual about m68k instruction set, you can find the official Motorola manual here.

Preliminary information about ongoing OS4 port

BIG FAT WARNING! All information below this line is preliminary, and subject to change without any further notice. OK, you've been warned.

Stack management

For Free Pascal compiled programs, the default stack size on PowerPC AmigaOS is 256KB. Stack size can be set directly by the programmer with a compiler switch (-Cs), or with the {$MEMORY} directive. In both cases, compiler will set the requested stack size into the stack cookie, and the operating system will allocate the stack with the specified size for the program. You can also reduce the stacksize. Note that PowerPC AmigaOS won't allow stacksizes below 32KB.

Alignment

Record elements are aligned to double word (4 bytes) under PowerPC AmigaOS by default. Use {$PACKRECORDS 2} directive if you need word aligned structures. For byte aligned records, use a packed record.

Library interfaces and Syscalls

Syscalls are used in AmigaOS4, to provide calls to libraries through OS4 interfaces. Syntax is planned to be the following:

var
  myLibBase: PLibrary;
 
type
  TmyLibIface = record
    Data: TInterface;
 
    Obtain: function(self as hidden): longword; SysCall;
    Release: function(self as hidden): longword; SysCall;
    Expunge: procedure(self as hidden); SysCall;
    Clone: function(self as hidden): PInterface; SysCall;
     
    myCallFoo: function(self as hidden; a: longint; b: pointer): pointer; SysCall;
    myCallBar: procedure(self as hidden; c: pchar; d: byte); SysCall;
  end;
  PmyLibIface = ^TmyLibIface;
 
var
  ImyLib: PmyLibIface;
begin
  // here the opening the library and interface, blah...
  
  ImyLib^.myCallFoo(0,nil);
  ImyLib^.myCallBar('a',1);   

  // releasing stuff
end;

The above example calls will pass pointer pointer to the interface in r3, pass the remaining arguments according to SysV ABI. Syscall is needed to enable the passing of the hidden argument, which is only supported for syscalls. A hidden argument can be any integer or pointer variable. They're not need to be specified in the actual calls in the code, but they're still passed to called function. On AmigaOS4, identifier "Self" has a special meaning, and means a pointer to the actual interface.

It's planned to provide an utility which helps creating a pascal headers from interface definition files for existing libraries, similar to what IDLTool does for C language. (This utility is partially done, and already works.)