#+TITLE: Chapter 4: Building Your First Application from Zero #+AUTHOR: Hermes & Claude #+DATE: 2026-10-07 #+PARENT: [[file:index.org][Index]] #+INCLUDE: goback.org * From Zero to a Running PRC In this chapter, we write, compile, link, package, patch, and verify a complete Palm OS application written in modern C++20 using our GCC 14 toolchain. * Step 1: Write the C++ Source (~hello.cpp~) Create a new directory for your first project: #+BEGIN_SRC bash mkdir -p ~/palm-projects/01-first-app && cd ~/palm-projects/01-first-app #+END_SRC Write ~hello.cpp~: #+BEGIN_SRC cpp // hello.cpp — Modern C++20 Palm OS Application using UInt16 = unsigned short; using UInt32 = unsigned long; using Err = short; constexpr UInt16 sysAppLaunchCmdNormalLaunch = 0; constexpr Err errNone = 0; // Minimal Palm OS API trap signatures extern "C" { UInt16 FrmCustomAlert(UInt16 alertId, const char* s1, const char* s2, const char* s3); } // A modern C++ class demonstrating encapsulation and methods template class Accumulator { T value_; public: constexpr explicit Accumulator(T initial = 0) : value_(initial) {} void add(T x) { value_ += x; } T get() const { return value_; } }; // Entry point: MUST be extern "C" extern "C" { UInt32 PilotMain(UInt16 cmd, void* cmdPBP, UInt16 launchFlags) { (void)cmdPBP; (void)launchFlags; // Ignore launch codes other than normal interactive launch if (cmd != sysAppLaunchCmdNormalLaunch) { return errNone; } // Modern C++ template class instantiated safely on the stack Accumulator acc(10); acc.add(32); // value is now 42 // Display a Palm OS alert dialog FrmCustomAlert(0, "C++20 on Palm OS", "Accumulator total: 42", "Enjoying GCC 14!"); return errNone; } } #+END_SRC * Step 2: The Universal Linker Script (~palmres.ld~) Save the universal Palm OS linker script in your project directory: #+BEGIN_SRC ld /* palmres.ld */ OUTPUT_FORMAT("coff-m68k") MEMORY { coderes : ORIGIN = 0x0, LENGTH = 32770 } SECTIONS { .text : { *(.text) } > coderes /DISCARD/ : { *(.note.GN) *(.comment) *(.ident) } } #+END_SRC * Step 3: The 6-Step Compilation Pipeline Now execute the complete toolchain pipeline: ** 1. Compile C++ to m68k Assembly #+BEGIN_SRC bash /tmp/g++14-palmos -S -m68000 -fno-pic -nostdlib -ffreestanding \ -std=c++20 -O2 -fno-ident -fno-exceptions -fno-rtti \ -o hello.s hello.cpp #+END_SRC - ~-S~: Emits assembly text only instead of binary ELF object code. - ~-m68000~: Targets the base 68000 architecture of the DragonBall CPU. - ~-fno-pic~: Generates absolute PC-relative code instead of PIC tables. - ~-nostdlib -ffreestanding~: Strips host OS libraries and standard includes. - ~-O2~: Optimizes aggressively, inlining the ~Accumulator~ methods. ** 2. Filter Modern Assembler Directives Modern GCC emits ELF directives that the Palm OS binutils assembler (~m68k-palmos-as~) does not understand. Strip them: #+BEGIN_SRC bash grep -vE '\.ident|\.type|\.size|\.section' hello.s > hello.clean.s #+END_SRC ** 3. Assemble with Palm OS Binutils #+BEGIN_SRC bash m68k-palmos-as -m68000 -o hello.o hello.clean.s #+END_SRC This creates an authentic Palm OS COFF object file. ** 4. Link with Palm OS Relocations #+BEGIN_SRC bash m68k-palmos-ld --embedded-relocs --no-check-sections -N \ -T palmres.ld -o hello.elf hello.o #+END_SRC - ~--embedded-relocs~: Generates the relocation fixups required by Palm OS to relocate the code section when loaded into RAM. - ~-T palmres.ld~: Uses our linker script to map ~.text~ into ~coderes~ and discard unnecessary notes. ** 5. Package as a Palm OS PRC #+BEGIN_SRC bash /usr/bin/build-prc -t appl -c GP14 -n "Gpp14Modern" -o Gpp14Modern.prc hello.elf #+END_SRC - ~-t appl~: Sets database type to ~appl~ (application). - ~-c GP14~: Assigns a 4-character Creator ID (~GP14~). - ~-n "Gpp14Modern"~: Sets the internal Palm OS application database name. * Step 4: The Critical PRC Flag Patch (Pitfall #30b) If you install ~Gpp14Modern.prc~ into an emulator right now, the emulator will reject it with: #+BEGIN_EXAMPLE "failed to install database" #+END_EXAMPLE *Why?* When ~build-prc~ packages an executable from an ELF binary directly, it sets the database header attribute flags at offset ~0x20~ to ~0x0001~ (~dmHdrAttrBackup~ only). Palm OS and modern emulators (like CloudpilotEmu) strictly require flag bit 3: ~dmHdrAttrLaunchableData~ (~0x0008~). Without it, the operating system treats the file as non-executable data! We patch the two-byte word at offset ~0x20~ from ~0x0001~ to ~0x0009~ (~dmHdrAttrBackup | dmHdrAttrLaunchableData~): #+BEGIN_SRC bash python3 -c " with open('Gpp14Modern.prc', 'r+b') as f: f.seek(0x20) flags = int.from_bytes(f.read(2), 'big') if not (flags & 0x0008): f.seek(0x20) f.write(bytes([0x00, 0x09])) print(f'Successfully patched header flags: 0x{flags:04x} -> 0x0009') else: print(f'Flags already correct (0x{flags:04x})') " #+END_SRC * Inspection & Size Verification Check the size of your generated PRC: #+BEGIN_SRC bash ls -lh Gpp14Modern.prc # Output: # -rw-r--r-- 1 node node 201 B Oct 7 15:30 Gpp14Modern.prc #+END_SRC Notice the remarkable size: **just 201 bytes!** Because modern GCC 14 inlines the C++ template code and computes calculations at compile-time with zero runtime overhead, the resulting ~.text~ section is approximately 16 bytes of pure 68000 machine instructions. This is the smallest valid modern C++ executable ever created for Palm OS. * Automation: The Master Build Script (~build.sh~) Save this reusable script to automate your build pipeline: #+BEGIN_SRC bash #!/usr/bin/env bash set -euo pipefail NAME="${1:-Gpp14Modern}" SRC="${2:-hello.cpp}" CRID="${3:-GP14}" echo "==> [1/5] Compiling $SRC with GCC 14 C++20..." /tmp/g++14-palmos -S -m68000 -fno-pic -nostdlib -ffreestanding \ -std=c++20 -O2 -fno-ident -fno-exceptions -fno-rtti \ -o "$NAME.s" "$SRC" echo "==> [2/5] Filtering ELF directives..." grep -vE '\.ident|\.type|\.size|\.section' "$NAME.s" > "$NAME.clean.s" echo "==> [3/5] Assembling m68k COFF object..." m68k-palmos-as -m68000 -o "$NAME.o" "$NAME.clean.s" echo "==> [4/5] Linking with palmres.ld..." m68k-palmos-ld --embedded-relocs --no-check-sections -N \ -T palmres.ld -o "$NAME.elf" "$NAME.o" echo "==> [5/5] Packaging PRC..." /usr/bin/build-prc -t appl -c "$CRID" -n "$NAME" -o "$NAME.prc" "$NAME.elf" # Header flag patch python3 -c " with open('$NAME.prc', 'r+b') as f: f.seek(0x20) flags = int.from_bytes(f.read(2), 'big') if not (flags & 0x0008): f.seek(0x20) f.write(bytes([0x00, 0x09])) " echo "==> SUCCESS: Created $NAME.prc ($(stat -c%s "$NAME.prc") bytes)" #+END_SRC #+LF * Further Reading & Reference Resources - **Packaging & PRC Internals:** - PRC File Format Specification: https://palm.wiki/development/docs/601/PalmOSReference/PalmOSFileFormats.html - Palm OS Database Header Attributes & Flag Bit Definitions: https://palmdb.net - ~build-prc~ Source Code & Command Reference: https://github.com/jichu4n/prc-tools-remix/blob/master/prc-tools-2.3/tools/build-prc.cpp - **Motorola 68000 Assembly Programming:** - Motorola. /M68000 Family Programmer's Reference Manual/ (M68000PM/AD). Available on Archive.org. - Mark Harman: /68000 Assembly Language Tutorial and Reference Guide/: https://www.retro-programming.org