Sign in Join
Wiki copytemporary

MorphOS: what changed

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

OldNewLine
11*(MorphOS only)*
22
33This page contains platform specific notes about the MorphOS port of Free Pascal. MorphOS is a massively enhanced, independent AmigaOS-clone, featuring ABI and API compatibility with classic [[Amiga]], running on various PowerPC platforms. For more information, [see its homepage](http://www.morphos-team.net).
44
55## Identification
66
77To identify MorphOS exclusively during compile-time, use **{\$IFDEF MORPHOS}**. To identify any Amiga-like systems, use **{\$IFDEF HASAMIGA}**. This is similar to **HASUNIX** which is defined across Unix systems. Please note that **HASAMIGA** define is only available in Free Pascal 3.0 and up, and the **AMIGA** define is *NOT* defined on MorphOS.
88
99## Stack
1010
1111Under MorphOS, the default stack is set to 256 KiB. Stack size can be set directly by the programmer with a compiler switch (-Cs), or with the **{\$MEMORY}** directive. The memory area for the stack is dynamically allocated on program startup, so if you set this value too high, in low memory situations your program may exit immediately on startup without any further notice.
1212
1313Note to classic Amiga programmers: Increasing stack size with the 'stack' utility provided with MorphOS **will not work** because it sets the size of the 68k stack for applications running in 68k emulation.
1414
1515## SysCalls
1616
1717### Introduction to SysCalls
1818
1919Free Pascal supports ABox style legacy, and four new calling conventions based on SysV ABI on MorphOS. Before that, only ABox style calls were supported. You don't need to use additional hand-written assembly to call any library function. However, you must declare every function you're going to use in the following way:
2020
2121``` pascal
2222 Var
2323 my_LibBase: Pointer;
2424
2525 function my_OldCall(param1: LongInt location 'd0',
2626 param2: LongInt location 'd1'): LongInt;
2727 SysCall Legacy my_LibBase 1234; { ABox-style call }
2828
2929 function my_SysVCall(param1: LongInt;
3030 param2: LongInt): LongInt;
3131 SysCall SysV my_LibBase 1240; { SysV ABI call }
3232
3333 function my_BaseSysVCall(param1: LongInt;
3434 param2: LongInt): LongInt;
3535 SysCall BaseSysV my_LibBase 1246; { Base,SysV ABI call }
3636
3737 function my_SysVBaseCall(param1: LongInt;
3838 param2: LongInt): LongInt;
3939 SysCall SysVBase my_LibBase 1252; { SysV,Base ABI call }
4040
4141 function my_R12BaseCall(param1: LongInt;
4242 param2: LongInt): LongInt;
4343 SysCall R12Base my_LibBase 1258; { SysV,r12Base ABI call }
4444```
4545
4646Where my_LibBase is the library base returned by exec's OpenLibrary() call, and 1234 is the call offset. Please note that offset values in Free Pascal must be specified as **positive values**, and not negative as shown in MorphOS SDK. Since compiler version 2004/08/22, my_LibBase can be a typed pointer, beside a dword and a void pointer. It's recommended to use PLibrary or the library's own type if one exists or possible.
4747
4848In case of ABox style calls, the value after location can be any 68k register from d0-d7/a0-a5. Register a6 is used internally to pass my_LibBase in it. Register a7/sp cannot be used. Register names are not case sensitive. In ABox-style SysCalls you must specify a location for every argument explicitly.
4949
5050If you don't want to specify SysCall method explicitly for every call, you can set a default SysCall method, using the {\$SYSCALL} directive. {\$SYSCALL LEGACY} (which is the built-in default) sets ABox-style, {\$SYSCALL SYSV} sets SysV style calls as default. Other possible arguments for {\$SYSCALL} directive are SYSVBASE, BASESYSV and R12BASE. Please note that the calling convention rules are not strict inside a library, so you might found different kinds of it inside a single library.
5151
5252### What different kinds of SysCalls do?
5353
5454Old libraries like exec and dos mostly using Legacy and SysV, newer PowerPC-native libs using SysV and BaseSysV, most current libs are using SysV and R12Base calls. SysVBase calls are rare, but do exist, so check for them when you create your own units for external libraries. If you want to figure out which calling convention a library uses, you can search its SDK for some fd/*.fd files, where this information is stored.
5555
5656#### Legacy
5757
5858Legacy calling convention puts arguments into the 68k emulation structure, which is pointed by register r2. 0(r2) equals to 68k register d0, 4(r2) to d1, and so on. It puts libbase into 56(r2) which equals to 68k register a6, then puts call offset into r3, and jumps to address stored at \$64(r2), which is the emulation entry point. On return it returns arguments in register r3. This calling convention is used to provide compatibility with existing 68k and classic Amiga PowerPC (WarpOS and PowerUP) apps.
5959
6060#### SysV
6161
6262SysV calling convention stores arguments according to SysV.4 ABI, then jumps to address stored at (LibBase - Offset). This calling convention is often used where no LibBase is needed.
6363
6464#### BaseSysV
6565
6666BaseSysV is a variation of SysV. It stores LibBase as first argument according to SysV.4 ABI, then puts all other arguments, then jumps to address stored at (LibBase - Offset). This calling convention also can be considered as legacy, R12Base convention replaced it, but still many libraries using it.
6767
6868#### SysVBase
6969
7070SysVBase is a variation of SysV. It stores all arguments according to SysV.4 ABI, then stores LibBase as last argument, then jumps to address stored at (LibBase - Offset). This calling convention also can be considered as legacy, as R12Base convention replaced it. This one is rare, not many libraries using it.
7171
7272#### R12Base
7373
7474R12Base is a variation of SysV. It stores all arguments according to SysV.4 ABI, then stores LibBase into register r12, then jumps to address stored at (LibBase - Offset). This calling convention superseeded BaseSysV and SysVBase, and should be used in new libraries.
7575
7676### Other hints about libraries and Syscalls
7777
7878**MOST IMPORTANT:** You must open the libraries you're going to use explicitly **before** using any function from them, because the compiler will not do it for you. Don't forget to close all libraries before you exit.
7979
8080Before declaring a SysCall function, please check the units provided with FPC, the function you want to use might already be declared in one of the units, ready to be used.
8181
8282## Naming conventions
8383
8484This section describes the differences between the official MorphOS SDK, which is for C language, and MorphOS-specific units in FPC RTL.
8585
8686### Constants
8787
8888System constants are named exactly like in MorphOS SDK.
8989
9090### Structures
9191
9292System structures are named similar like in MorphOS SDK, but follows the Pascal convention about type naming, so structure names has a T before, and each type has a pointer to type with P before the name. The following example should make things trivial:
9393
94-` struct Task equals to TTask`\
95-` struct Task * equals to PTask`
94+```text
95+ struct Task equals to TTask
96+ struct Task * equals to PTask
97+```
9698
9799### Function calls
98100
99101System calls are named like in MorphOS SDK, but there are a few exceptions. Unfortunately MorphOS legacy (Amiga compatible) API has some name conflicts with Free Pascal RTL functions, and this issue cannot be solved with function overloading. These calls are available with a name prefixed by their library's name. So for example, exec.library's FreeMem() is available under the name execFreeMem(), and FreeMem() means the FreeMem() call of FPC's own heap manager. The following functions are known to be affected:
100102
101103- exec.library
102104 - AllocMem() (only causes trouble inside System unit),
103105 - FreeMem()
104106 - Insert()
105107- dos.library
106108 - Close()
107109 - Read()
108110 - Write()
109111 - Input()
110112 - Output()
111113 - Seek()
112114 - Rename()
113115 - Exit()
114116 - Flush()
115117 - System()
116118 - DeleteFile() (conflicts with SysUtils)
117119 - CreateDir() (conflicts with SysUtils)
118120- graphics.library
119121 - Move() (conflicts with Video)
120122
121123Be prepared for more...
122124
123125#### MUI
124126
125127In the following section there are a few hints about using MUI with Free Pascal.
126128
1271291. set() and get() MUI macros available under name MUIset() and MUIget(), to avoid name conflicts with Pascal language. These are wrappers to the appropriate Intuition functions just like in C.
1281302. To avoid unnecessary typecasting, MUI_TRUE and MUI_FALSE declared instead of plain True and False. Using these is recommended.
1291313. There are various helper functions in MUIHelper unit, to make Pascal code using MUI much simpler and more readable. Use them.
1301324. Pascal preprocessor won't preparse strings like C does. So text formatting sequences widely used in MUI applications like \033c and so won't work. You have to use the defined constants instead. Example:
131133
132134C version:
133135
134136``` c
135137"\033cCentered \033bBold Text"
136138```
137139
138140Pascal version:
139141
140142``` pascal
141143MUIX_C+'Centered '+MUIX_B+'Bold Text'
142144```
143145
144146or
145147
146148``` pascal
147149#27'cCentered '#27'bBold Text'
148150```
149151
150152- Note: these constants are there in official mui.h includes as well, they're just rarely used.
151153
152154## Alignment
153155
154156Record elements are aligned to DWORD (4 bytes) under MorphOS by default. Use {\$PACKRECORDS 2} if you need word aligned structures. For byte aligned records, use a packed record.
155157
156158## Threading
157159
158160Threading is supported on MorphOS since Free Pascal 3.0, using the [[AThreads]] unit. Read there for possible caveats and unsupported features.
159161
160162## Things good to know
161163
162164Here are a few hints, which I think are good to remember, when using FPC on MorphOS.
163165
164166- On program exit, System unit will close all files opened via its own functions, so it's recommended to use them in most cases.
165167- Do not mix FPC and OS file functions on a single file. It may work in some cases, but that's purely a coincidence. It's bad programming and can stop working anytime without notice.
166168- On program exit, the heap manager will free all memory allocated via its own functions. It uses a memory pool to avoid fragmentation. It also makes debugging easier when using heaptrc unit. Using OS memory functions directly is not recommended.
167169
168170- Scribble, which is a programmer's editor based on [Scintilla](http://www.scintilla.org) is included in MorphOS SDK 2.x and part of MorphOS 3.x itself, and **supports Pascal syntax highlighting out of box**.
169171- Another option for syntax highlighted editing is GoldEd, [also known as Cubic IDE](http://www.softwareandcircuits.com/division/amiga/products/cubic/index.html) (or the lite version MorphEd, which was available as part of older MorphOS SDKs). A very nice Pascal syntax highlight addon was made for it by Rafal Zabdyr. The original site is down, but a backup is available here: <http://obligement.free.fr/files/ged5morphed_pas075.lha>
170172
171173- You can find some MorphOS specific examples in the SVN, directly accessible here: <http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/demo/morphos/?root=fpcbuild>
172174
173175## Cross compiling
174176
175177Cross compiling to MorphOS is supported since version 3.0.0 and up.
176178
177179### Cross compiling from Linux
178180
179181It's possible to cross-build the MorphOS compiler from Linux/i386 and Linux/x86-64 (probably other CPUs too, but it was not tested). To do that, you need Linux to MorphOS cross-binutils, mainly GNU as, ld, and optionally ar and strip. It's best if you build these from GNU sources, along with the rest of cross-build GNU toolchain. Mark "bigfoot" Olsen kindly provided a script for this, [available here](http://bigfoot.morphos-team.net/files/setup-cross-sdk.sh).
180182
181183Optionally, the binutils required by FPC are [available here as binary](http://charlie.amigaspirit.hu/software/fpc/morphos/cross-binutils-linux-i386.tar.gz) for Linux/i386 (works on Linux/x86-64 too, if you install the required 32 bit environment). It was only tested on Ubuntu and Debian Linuxes, but may work on other distributions too. **This package is provided "as-is" without any kind of support. Use it at your own risk.**
182184
183185To build a MorphOS cross-compiler, use the following steps:
184186
1851871. Install FPC the latest stable compiler, at the time of the writing of this article this is FPC 3.0.0. This will be used as the startup compiler.
1861882. Check out FPC SVN trunk into a directory (see version info above)
1871893. Make sure the aforementioned cross-binutils are actually in the PATH.
1881904. Go to the trunk's main directory and use the following command to build an FPC to MorphOS cross-compiler running on Linux:
189191
190192``` bash
191193 make clean crossall crossinstall OS_TARGET=morphos CPU_TARGET=powerpc INSTALL_PREFIX="<path/to/install>"
192194```
193195
194196If you've done everything right, you should find a working MorphOS cross-compiler in the install path you've specified.
195197
196198Now, lets create a default fpc.cfg for MorphOS cross compiling. Create a file called **\<path/to/install\>/lib/fpc/etc/fpc.cfg**. Put the following lines into that file, and fix up the paths:
197199
198-`#IFDEF CPUPOWERPC`\
199-`-Fu<path/to/install>/lib/fpc/$fpcversion/units/$FPCTARGET`\
200-`-Fu<path/to/install>/lib/fpc/$fpcversion/units/$FPCTARGET/*`\
201-`#IFDEF MORPHOS`\
202-`-FD</path/to/morphos-cross-binutils>`\
203-`-XPpowerpc-morphos-`\
204-`#ENDIF`\
205-`#ENDIF`
200+```text
201+#IFDEF CPUPOWERPC
202+-Fu<path/to/install>/lib/fpc/$fpcversion/units/$FPCTARGET
203+-Fu<path/to/install>/lib/fpc/$fpcversion/units/$FPCTARGET/*
204+#IFDEF MORPHOS
205+-FD</path/to/morphos-cross-binutils>
206+-XPpowerpc-morphos-
207+#ENDIF
208+#ENDIF
209+```
206210
207211Optionally add **\<path/to/install\>/lib/fpc/3.1.1/** directory to the PATH, so you'll have direct access to the cross compiler. If you've done everything right, you now should be able to build MorphOS executables from Pascal source on Linux the following way:
208212
209-`ppcrossppc -Tmorphos <source.pas>`
213+```text
214+ppcrossppc -Tmorphos <source.pas>
215+```
210216
211217Copy the resulting executable to MorphOS, and that's it!
212218
213219### Cross compiling from OSX
214220
215221In general, the same rules and steps apply as for Linux cross compiling. To build the binutils from sources using Mark Olsen's script (see in the previous section) some slight modifications to the script and/or additional GNU tools might be required.
216222
217223For convenience, a binutils package required by FPC are [available here as binary](http://charlie.amigaspirit.hu/software/fpc/morphos/cross-binutils-darwin-i386.tar.gz) for Darwin/i386. **This package is provided "as-is" without any kind of support. Use it at your own risk.**
218224
219225### Cross compiling from Windows
220226
221227In general, the same rules and steps apply as for Linux cross compiling. It's also possible to cross-build the binutils with [Cygwin](http://cygwin.com) using Mark Olsen's script, as linked in the previous section.
222228
223229For convenience, a binutils package required by FPC are [available here as binary](http://charlie.amigaspirit.hu/software/fpc/morphos/cross-binutils-cygwin-i686.zip) for Windows/x86 (Cygwin/i686). **This package is provided "as-is" without any kind of support. Use it at your own risk.**
224230
225231As FPC on Windows use a bit different directory structure the place to put our fpc.cfg is: **\<path/to/install\>/bin/i386-win32/fpc.cfg**. Also, for easy access, make sure directory **\<path/to/install\>/bin/i386-win32/** is part of the PATH.
226232
227233The cross-build from Windows/x64 was not tested, but should work with the obvious differences in paths and tools.
228234
229235## Bootstrapping a compiler from Linux
230236
231237To bootstrap a Free Pascal Compiler from Linux to MorphOS, first please complete the previous section. In order to have all the tools available, it's easiest to install the latest FPC compiler archive [available here.](http://charlie.amigaspirit.hu/software/fpc/morphos/)
232238
233239After you have a working cross compiler, go to the SVN **trunk/compiler** directory, and issue the following command:
234240
235241``` bash
236242make compiler OS_TARGET=morphos CPU_TARGET=powerpc FPC="<path/to/install>/lib/fpc/2.7.1/ppcrossppc"
237243```
238244
239245Copy the resulting **ppcppc** executable to MorphOS. On the MorphOS box, try to cycle a compiler the following way in the SVN's **trunk/compiler** directory:
240246
241-`make cycle FPC=/path/to/ppcppc`
247+```text
248+make cycle FPC=/path/to/ppcppc
249+```
242250
243251Congratulations, you've just successfully bootstrapped Free Pascal on MorphOS! Now, copy everything in place by hand to your FPC: assign, build more stuff, etc... :)
244252
245253## Linker
246254
247255Some version of Free Pascal on MorphOS, including latest SVN trunk versions default to **vlink** by Frank Wille as the default linker, when running natively on MorphOS. The cross-compilers still default to **GNU ld**. **vlink** is open source, and it is available [here](http://sun.hasenbraten.de/vlink). Binaries are available as part of the [vbcc compiler package](http://sun.hasenbraten.de/vbcc).
248256
249257You can change the linker back to **GNU ld** by adding *-XV-* argument when compiling. For cross compilers *-XV* argument enables vlink.
250258
251259## Assembler
252260
253261The MorphOS port supports both **GNU as** assembler and **vasm**. Advanced features like named sections support are only available with **vasm**. To compile with vasm, you need to specify the argument *-Avasm* compile time. Please note that mixing **GNU as** and **vasm** compiled objects might cause issues during linking.
254262
255263**vasm** is open source, and it is available [here](http://sun.hasenbraten.de/vasm). Binaries are available as part of the [vbcc compiler package](http://sun.hasenbraten.de/vbcc). Only vasm versions 1.7h and newer were tested, older versions might not work.
256264
257265If *-Avasm* is set as default, for example in the config file, one can switch back to **GNU as** by using the *-Aas-legacy* command line argument. Please note that since the MorphOS SDK has a very old **GNU as** bundled, there could be problems with some of the assembly code generated by using the normal **GNU as** argument *-Aas*.
258266
259267## More information
260268
261269More information regarding Free Pascal and MorphOS can be read on the [Free Pascal 4 Amiga wiki](http://fpcamigawiki.alb42.de). This wiki also contains links to (unofficial) nightly downloads and other (additional) information for MorphOS.
262270
263271## Thanks
264272
265273The initial [MorphOS](http://www.morphos-team.net) port of Free Pascal Compiler was possible, because [Genesi](http://genesi.pegasosppc.com) provided a free [Pegasos II/G4](http://en.wikipedia.org/wiki/Pegasos) machine to do the development on.
266274
267275---
268276*From the Free Pascal wiki, page "MorphOS", as it stood on 2023-12-11 after a change by Polluks, 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).*
269277Category: AmigaOS
270278Category: Operating Systems and Platforms

Back to history