From ef88f0e530b898ebcb14f9e3dff1d58e83089ada Mon Sep 17 00:00:00 2001 From: user Date: Thu, 16 Jul 2026 00:49:58 +0200 Subject: 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'. --- c/libc.s | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 c/libc.s (limited to 'c/libc.s') 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 -- cgit v1.3.1-sl0p