summaryrefslogtreecommitdiff
path: root/agbx
diff options
context:
space:
mode:
Diffstat (limited to 'agbx')
-rw-r--r--agbx/GNUmakefile72
-rw-r--r--agbx/include-private/agbx/priv.h49
-rw-r--r--agbx/include/agbx/bs.h39
-rw-r--r--agbx/include/agbx/gfx.h19
-rw-r--r--agbx/source/bs/done.c34
-rw-r--r--agbx/source/bs/get.c40
-rw-r--r--agbx/source/bs/set.c13
-rw-r--r--agbx/source/gfx/plot.c34
-rw-r--r--agbx/source/gfx/setpx.c33
-rw-r--r--agbx/source/priv/init.c14
10 files changed, 347 insertions, 0 deletions
diff --git a/agbx/GNUmakefile b/agbx/GNUmakefile
new file mode 100644
index 0000000..618b9d6
--- /dev/null
+++ b/agbx/GNUmakefile
@@ -0,0 +1,72 @@
+# Copyright 2022 Gabriel Jensen.
+# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+# If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
+
+ifneq ($(DEBUG),false)
+ifneq ($(DEBUG),true)
+$(error Invalid value \"$(DEBUG)\" for DEBUG)
+endif
+endif
+
+# TOOLS
+
+CC := clang -target arm-none-eabi
+#CC := arm-none-eabi-gcc
+OBJCOPY := arm-none-eabi-objcopy
+
+# TOOL FLAGS
+
+CFLAGS := \
+ -Iinclude \
+ -Iinclude-private \
+ -Ofast \
+ -Wall \
+ -Wextra \
+ -Wpedantic \
+ -ffreestanding \
+ -fno-builtin \
+ -mcpu=arm7tdmi \
+ -mthumb \
+ -mtune=arm7tdmi \
+ -nostdlib \
+ --std=c2x
+
+ifeq ($(DEBUG),true)
+CFLAGS := \
+ $(CFLAGS) \
+ -D__agbx_dbg
+endif
+
+# HEADERS
+
+HDRS := \
+ include/agbx/bs.h \
+ include/agbx/gfx.h \
+ include-private/agbx/priv.h
+
+# BINARIES
+
+OBJS := \
+ source/bs/done.o \
+ source/bs/get.o \
+ source/bs/set.o \
+ source/gfx/plot.o \
+ source/gfx/setpx.o \
+ source/priv/init.o
+
+LIB := libagbx.a
+
+# TARGETS
+
+.PHONY: clean purge
+
+$(LIB): $(OBJS)
+ $(AR) r $(@) $(^)
+
+$(OBJS): $(HDRS)
+
+clean:
+ $(RM) $(OBJS)
+
+purge: clean
+ $(RM) $(LIB)
diff --git a/agbx/include-private/agbx/priv.h b/agbx/include-private/agbx/priv.h
new file mode 100644
index 0000000..39c73f6
--- /dev/null
+++ b/agbx/include-private/agbx/priv.h
@@ -0,0 +1,49 @@
+/*
+ Copyright 2022 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
+*/
+
+#if !defined(__agbx_hdr_priv)
+#define __agbx_hdr_priv
+
+#include <agbx/gfx.h>
+
+#define __agbx_set10(_addr,_val) { \
+ __asm__ ( \
+ "strh %1,[%0]" \
+ : \
+ : "r" (_addr),"r" (_val) \
+ : "memory" \
+ ); \
+}
+
+#define __agbx_set20(_addr,_val) { \
+ __asm__ ( \
+ "str %1,[%0]" \
+ : \
+ : "r" (_addr),"r" (_val) \
+ : "memory" \
+ ); \
+}
+
+#define __agbx_set8(_addr,_val) { \
+ __asm__ ( \
+ "strb %1,[%0]" \
+ : \
+ : "r" (_addr),"r" (_val) \
+ : "memory" \
+ ); \
+}
+
+#define __agbx_setpx1(_px,_col) { \
+ agbx_i20 const addr = 0x6000000u + _px; \
+ __agbx_set8(addr,_col); \
+}
+
+#define __agbx_setpx2(_px,_col) { \
+ agbx_i20 const addr = 0x6000000u + _px * 0x2u; /* We multiply it by two as each pixel takes up two bytes. */ \
+ __agbx_set10(addr,_col); \
+}
+
+#endif
diff --git a/agbx/include/agbx/bs.h b/agbx/include/agbx/bs.h
new file mode 100644
index 0000000..630c50b
--- /dev/null
+++ b/agbx/include/agbx/bs.h
@@ -0,0 +1,39 @@
+/*
+ Copyright 2022 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
+*/
+
+#if !defined(__agbx_hdr_base)
+#define __agbx_hdr_base
+
+/* C23 compatibility: */
+#define constexpr static const
+#define nullptr ((void *)0x0u)
+
+typedef unsigned short agbx_i10;
+typedef unsigned int agbx_i20;
+typedef unsigned long long agbx_i40;
+typedef unsigned char agbx_i8;
+
+typedef enum {
+ agbx_err_badmd,
+ agbx_err_misc,
+ agbx_err_ok,
+ agbx_err_pos2big,
+ agbx_err_px2big,
+} agbx_err;
+
+constexpr agbx_i40 agbx_ver = 0x0u;
+
+[[noreturn]] void agbx_done(agbx_err err);
+
+agbx_i10 agbx_get10(agbx_i20 addr);
+agbx_i20 agbx_get20(agbx_i20 addr);
+agbx_i8 agbx_get8( agbx_i20 addr);
+
+void agbx_set10(agbx_i20 addr,agbx_i10 val);
+void agbx_set20(agbx_i20 addr,agbx_i20 val);
+void agbx_set8( agbx_i20 addr,agbx_i8 val);
+
+#endif
diff --git a/agbx/include/agbx/gfx.h b/agbx/include/agbx/gfx.h
new file mode 100644
index 0000000..31c3778
--- /dev/null
+++ b/agbx/include/agbx/gfx.h
@@ -0,0 +1,19 @@
+/*
+ Copyright 2022 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
+*/
+
+#if !defined(__agbx_hdr_gfx)
+#define __agbx_hdr_gfx
+
+#include <agbx/bs.h>
+
+void agbx_setpx1(agbx_i10 px,agbx_i8 col);
+void agbx_setpx2(agbx_i10 px,agbx_i10 col);
+
+agbx_i10 agbx_plot3(agbx_i8 x, agbx_i8 y,agbx_i10 col);
+agbx_i10 agbx_plot4(agbx_i8 x, agbx_i8 y,agbx_i8 col);
+agbx_i10 agbx_plot5(agbx_i8 x, agbx_i8 y,agbx_i10 col);
+
+#endif
diff --git a/agbx/source/bs/done.c b/agbx/source/bs/done.c
new file mode 100644
index 0000000..afac799
--- /dev/null
+++ b/agbx/source/bs/done.c
@@ -0,0 +1,34 @@
+/*
+ Copyright 2022 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
+*/
+
+#include <agbx/priv.h>
+
+void agbx_done(agbx_err const _err) {
+ if (_err == agbx_err_ok) {
+ __asm__ (
+ "swi 0x3\n"
+ );
+ __builtin_unreachable();
+ }
+ /* Display the barcode-like error message: */
+ agbx_set10(0x4000000u,0x403u);
+ agbx_i10 err[0x8u];
+ for (agbx_i8 pos = 0x0u;pos != 0x8u;++pos) {err[pos] = (agbx_i8)_err >> pos & 0x1u ? 0xFFFFu : 0x0u;}
+ for (agbx_i10 pos = 0x0u;pos != 0x9600u;pos += 0xF0u) {
+ agbx_setpx2(pos, err[0x0u]);
+ agbx_setpx2(pos + 0x1u,err[0x1u]);
+ agbx_setpx2(pos + 0x2u,err[0x2u]);
+ agbx_setpx2(pos + 0x3u,err[0x3u]);
+ agbx_setpx2(pos + 0x4u,err[0x4u]);
+ agbx_setpx2(pos + 0x5u,err[0x5u]);
+ agbx_setpx2(pos + 0x6u,err[0x6u]);
+ agbx_setpx2(pos + 0x7u,err[0x7u]);
+ }
+ __asm__ (
+ "swi 0x2\n"
+ );
+ for (;;) {}
+}
diff --git a/agbx/source/bs/get.c b/agbx/source/bs/get.c
new file mode 100644
index 0000000..38da5af
--- /dev/null
+++ b/agbx/source/bs/get.c
@@ -0,0 +1,40 @@
+/*
+ Copyright 2022 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
+*/
+
+#include <agbx/priv.h>
+
+agbx_i10 agbx_get10(agbx_i20 const _addr) {
+ agbx_i10 val;
+ __asm__ ( \
+ "ldrh %0,[%1]"
+ : "=r" (val)
+ : "r" (_addr)
+ : "memory"
+ );
+ return val;
+}
+
+agbx_i20 agbx_get20(agbx_i20 const _addr) {
+ agbx_i20 val;
+ __asm__ ( \
+ "ldr %0,[%1]"
+ : "=r" (val)
+ : "r" (_addr)
+ : "memory"
+ );
+ return val;
+}
+
+agbx_i8 agbx_get8(agbx_i20 const _addr) {
+ agbx_i8 val;
+ __asm__ ( \
+ "ldrb %0,[%1]"
+ : "=r" (val)
+ : "r" (_addr)
+ : "memory"
+ );
+ return val;
+}
diff --git a/agbx/source/bs/set.c b/agbx/source/bs/set.c
new file mode 100644
index 0000000..8aa9976
--- /dev/null
+++ b/agbx/source/bs/set.c
@@ -0,0 +1,13 @@
+/*
+ Copyright 2022 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
+*/
+
+#include <agbx/priv.h>
+
+void agbx_set10(agbx_i20 const _addr,agbx_i10 const _val) {__agbx_set10(_addr,_val)}
+
+void agbx_set20(agbx_i20 const _addr,agbx_i20 const _val) {__agbx_set20(_addr,_val)}
+
+void agbx_set8(agbx_i20 const _addr,agbx_i8 const _val) {__agbx_set8(_addr,_val)}
diff --git a/agbx/source/gfx/plot.c b/agbx/source/gfx/plot.c
new file mode 100644
index 0000000..5a65972
--- /dev/null
+++ b/agbx/source/gfx/plot.c
@@ -0,0 +1,34 @@
+/*
+ Copyright 2022 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
+*/
+
+#include <agbx/priv.h>
+
+agbx_i10 agbx_plot3(agbx_i8 const _x,agbx_i8 const _y,agbx_i10 const _col) {
+#if defined(__agbx_dbg)
+ if (_x >= 0xF0u || _y >= 0xA0u) {agbx_done(agbx_err_pos2big);}
+#endif
+ agbx_i10 const px = _y * 0xF0u + _x;
+ __agbx_setpx2(px,_col)
+ return px;
+}
+
+agbx_i10 agbx_plot4(agbx_i8 const _x,agbx_i8 const _y,agbx_i8 const _col) {
+#if defined(__agbx_dbg)
+ if (_x >= 0xF0u || _y >= 0xA0u) {agbx_done(agbx_err_pos2big);}
+#endif
+ agbx_i10 const px = _y * 0xF0u + _x;
+ __agbx_setpx1(px,_col)
+ return px;
+}
+
+agbx_i10 agbx_plot5(agbx_i8 const _x,agbx_i8 const _y,agbx_i10 const _col) {
+#if defined(__agbx_dbg)
+ if (_x >= 0xA0u || _y >= 0x80u) {agbx_done(agbx_err_pos2big);}
+#endif
+ agbx_i10 const px = _y * 0xA0u + _x;
+ __agbx_setpx2(px,_col)
+ return px;
+}
diff --git a/agbx/source/gfx/setpx.c b/agbx/source/gfx/setpx.c
new file mode 100644
index 0000000..ace2463
--- /dev/null
+++ b/agbx/source/gfx/setpx.c
@@ -0,0 +1,33 @@
+/*
+ Copyright 2022 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
+*/
+
+#include <agbx/priv.h>
+
+void agbx_setpx1(agbx_i10 const _px,agbx_i8 const _col) {
+#if defined(__agbx_dbg)
+ agbx_i10 const dispcnt = agbx_get10(0x4000000u);
+ agbx_i8 const md = dispcnt & 0x7u;
+ if (md == 0x3u || md == 0x5u) {agbx_done(agbx_err_badmd);}
+ if (_px >= 0x9600u) {agbx_done(agbx_err_px2big);}
+#endif
+ __agbx_setpx1(_px,_col)
+}
+
+void agbx_setpx2(agbx_i10 const _px,agbx_i10 const _col) {
+#if defined(__agbx_dbg)
+ agbx_i10 const dispcnt = agbx_get10(0x4000000u);
+ agbx_i8 const md = dispcnt & 0x7u;
+ if (md == 0x4u) {agbx_done(agbx_err_badmd);}
+ if (md == 0x5u) {
+ if (_px >= 0x5000u) {agbx_done(agbx_err_px2big);}
+ }
+ else {
+ if (md != 0x3u) {agbx_done(agbx_err_badmd);}
+ if (_px >= 0x9600u) {agbx_done(agbx_err_px2big);}
+ }
+#endif
+ __agbx_setpx2(_px,_col)
+}
diff --git a/agbx/source/priv/init.c b/agbx/source/priv/init.c
new file mode 100644
index 0000000..3ba1f17
--- /dev/null
+++ b/agbx/source/priv/init.c
@@ -0,0 +1,14 @@
+/*
+ Copyright 2022 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
+*/
+
+#include <agbx/priv.h>
+
+agbx_err agbx_main(void);
+
+[[noreturn]] void __agbx_init(void) {
+ agbx_err const err = agbx_main();
+ agbx_done(err);
+}