From 1d057626e4a3981163e1a71bc4ac1f3f15c543d4 Mon Sep 17 00:00:00 2001 From: giomba Date: Sun, 8 Oct 2023 20:50:13 +0200 Subject: [PATCH] Add instructions to compile C programs. --- README.md | 20 +++++++++++++++++++- main.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 main.c diff --git a/README.md b/README.md index e1e30a1..3f34f28 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,22 @@ Here you can find some utilities to jump start your computer from scratch! :warning: Mess with electronics only if you know what you are doing. Beware: you may harm your computer, or even yourself! Everything you do is at your own risk. ## How to -- to be written +Just some random notes. + +Compile a C program: +```sh +zcc +z80 -create-app --no-crt -pragma-define:REGISTER_SP=0xC000 -m -o build/main main.c +``` +Important: this is a linker-less and C-runtime-less compilation, so be very very careful what you do. +Example minimal starter: +```c +int entrypoint(void) { + __asm + ld sp,0xc000 + __endasm; + + main(); + for (;;) + ; +} +``` diff --git a/main.c b/main.c new file mode 100644 index 0000000..1e0b1e8 --- /dev/null +++ b/main.c @@ -0,0 +1,43 @@ +#include + +char* const VIDEO_MEMORY = (char* const)0xd000; + +static void vprint(int row, int column, const char* str); +static void out(uint8_t ioaddr, uint8_t value); + +int entrypoint() { + __asm + ld sp,0xc000 + __endasm; + + vprint(20,10,"hello world"); + out(0x81, 0x80); + vprint(20,15,"aaaaa"); + + for (;;) + ; +} + +static void out(uint8_t ioaddr, uint8_t value) { + __asm + ld ix,$0000 + add ix,sp + ld b,(ix + 5) + ld c,(ix + 4) + out (c),b + __endasm; +} + +static void cls(void) { + for (int i = 0; i < 2000; ++i) { + VIDEO_MEMORY[i] = 0x20; + } +} + +static void vprint(int row, int column, const char* str) { + char* p = VIDEO_MEMORY + row * 80 + column; + while (*str != '\0') { + *p++ = *str++; + } +} +