aboutsummaryrefslogtreecommitdiffstats
path: root/c
diff options
context:
space:
mode:
authoruser <user@clank>2026-07-16 00:49:58 +0200
committeruser <user@clank>2026-07-16 00:49:58 +0200
commitef88f0e530b898ebcb14f9e3dff1d58e83089ada (patch)
tree4b4e70fb12a92e5e254a8c937fbda34316141d54 /c
parentshell working: fix syscall ABI (trap clobbers HL) + document (diff)
downloadgbos-ef88f0e530b898ebcb14f9e3dff1d58e83089ada.tar.gz
gbos-ef88f0e530b898ebcb14f9e3dff1d58e83089ada.tar.xz
gbos-ef88f0e530b898ebcb14f9e3dff1d58e83089ada.zip
C toolchain: run SDCC-compiled C programs as gbos processes
- c/{gbos.h,crt0.s,libc.s,build.sh}: SDCC sm83 -> ROM-bank blob toolchain. libc syscall wrappers save/restore BC/DE/HL around rst $30 (trap clobbers them; SDCC expects them preserved). - build via SDCC native asxxxx path (sdasgb/sdldgb), crt0 linked first so _start is the $4000 entry; blob INCBIN'd into a ROM bank. - chello.c: prints a message + getpid() -> runs as 'chello' shell command. - Makefile: auto-build c/*.bin, track as a dep of programs.o; gitignore blobs. - README: document the C toolchain + the SDCC --asm=rgbds codegen bug that forced the native-toolchain approach. - verified: '$ chello' -> 'hello from C on gbos!' / 'my pid is 2'.
Diffstat (limited to 'c')
-rwxr-xr-xc/build.sh20
-rw-r--r--c/chello.c9
-rw-r--r--c/crt0.s11
-rw-r--r--c/gbos.h9
-rw-r--r--c/libc.s54
5 files changed, 103 insertions, 0 deletions
diff --git a/c/build.sh b/c/build.sh
new file mode 100755
index 0000000..6a2deab
--- /dev/null
+++ b/c/build.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+# Build a gbos C program into a self-contained 16 KiB ROM-bank image.
+# SDCC native toolchain; crt0 linked FIRST so _start is the $4000 entry.
+# usage: c/build.sh <prog.c> <out.bin>
+set -e
+SRC="$1"; OUT="$2"; B=build/c
+LIBDIR=/usr/share/sdcc/lib/sm83
+mkdir -p "$B"
+sdasgb -o "$B/crt0.rel" c/crt0.s
+sdasgb -o "$B/libc.rel" c/libc.s
+sdcc -msm83 -c -Ic "$SRC" -o "$B/prog.rel"
+# link: crt0 first (=> _start at 0x4000), then libc, program, and sm83 lib
+sdldgb -n -m -w -j -i "$B/prog.ihx" \
+ -b _CODE=0x4000 -b _DATA=0xa000 \
+ -k "$LIBDIR" -l sm83 \
+ "$B/crt0.rel" "$B/libc.rel" "$B/prog.rel"
+makebin -Z -p "$B/prog.ihx" "$B/prog.bin"
+dd if="$B/prog.bin" of="$OUT" bs=1024 skip=16 count=16 2>/dev/null
+echo "wrote $OUT ($(wc -c < "$OUT") bytes); entry:"
+grep -iE ' _start| _main' "$B/prog.map" | head -2
diff --git a/c/chello.c b/c/chello.c
new file mode 100644
index 0000000..f570b61
--- /dev/null
+++ b/c/chello.c
@@ -0,0 +1,9 @@
+#include "gbos.h"
+void main(void) {
+ writes("hello from C on gbos!", 21);
+ nl();
+ writes("my pid is ", 10);
+ /* print pid as a digit */
+ { unsigned char c = getpid() + '0'; char d = c; writes(&d, 1); }
+ nl();
+}
diff --git a/c/crt0.s b/c/crt0.s
new file mode 100644
index 0000000..cde4b67
--- /dev/null
+++ b/c/crt0.s
@@ -0,0 +1,11 @@
+ .module crt0
+ .globl _main
+ ;; gbos C entry. exec() maps our ROM bank and jumps to the start of _CODE
+ ;; (linked at 0x4000). Call main(), then exit(0). exec() already set SP.
+ .area _CODE
+_start::
+ call _main
+ ld b, #0
+ ld c, #0 ; SYS_EXIT
+ rst #0x30
+1$: jr 1$
diff --git a/c/gbos.h b/c/gbos.h
new file mode 100644
index 0000000..9a09a5d
--- /dev/null
+++ b/c/gbos.h
@@ -0,0 +1,9 @@
+/* gbos userland C API (thin wrappers over rst $30 syscalls; see c/libc.asm) */
+#ifndef GBOS_H
+#define GBOS_H
+void writes(const char *buf, unsigned char len); /* write to console */
+void nl(void); /* newline (CRLF) */
+unsigned char readc(void); /* read one byte */
+void sexit(unsigned char code); /* exit(code) */
+unsigned char getpid(void);
+#endif
diff --git a/c/libc.s b/c/libc.s
new file mode 100644
index 0000000..4fca5e3
--- /dev/null
+++ b/c/libc.s
@@ -0,0 +1,54 @@
+ .module libc
+ .globl _writes, _nl, _readc, _sexit, _getpid
+ .area _CODE
+ ;; void writes(const char *buf /*DE*/, unsigned char len /*A*/)
+_writes:
+ push hl
+ push de
+ push bc
+ ld b, a
+ ld c, #3 ; SYS_WRITE
+ rst #0x30
+ pop bc
+ pop de
+ pop hl
+ ret
+ ;; void nl(void) - CRLF straight to serial
+_nl:
+ ld a, #0x0d
+ ld (0xff01), a
+ ld a, #0x81
+ ld (0xff02), a
+ ld a, #0x0a
+ ld (0xff01), a
+ ld a, #0x81
+ ld (0xff02), a
+ ret
+ ;; unsigned char readc(void) -> A
+_readc:
+ push hl
+ push de
+ push bc
+ ld c, #2 ; SYS_READ
+ rst #0x30
+ pop bc
+ pop de
+ pop hl
+ ret
+ ;; void sexit(unsigned char code /*A*/)
+_sexit:
+ ld b, a
+ ld c, #0 ; SYS_EXIT
+ rst #0x30
+ ret
+ ;; unsigned char getpid(void) -> A
+_getpid:
+ push hl
+ push de
+ push bc
+ ld c, #8 ; SYS_GETPID
+ rst #0x30
+ pop bc
+ pop de
+ pop hl
+ ret