summaryrefslogtreecommitdiff
path: root/ax/source
diff options
context:
space:
mode:
Diffstat (limited to 'ax/source')
-rw-r--r--ax/source/algo/cp.c16
-rw-r--r--ax/source/algo/cp.s51
-rw-r--r--ax/source/algo/cp8.s36
-rw-r--r--ax/source/algo/divmod.c18
-rw-r--r--ax/source/gfx/flip.s10
-rw-r--r--ax/source/gfx/getvbnk.s6
-rw-r--r--ax/source/gfx/setcol.s6
-rw-r--r--ax/source/string/memcpy.c15
-rw-r--r--ax/source/string/memcpy.s30
-rw-r--r--ax/source/string/memmove.s30
-rw-r--r--ax/source/string/memset.c16
-rw-r--r--ax/source/string/memset.s29
-rw-r--r--ax/source/string/strlen.c16
-rw-r--r--ax/source/string/strlen.s38
14 files changed, 243 insertions, 74 deletions
diff --git a/ax/source/algo/cp.c b/ax/source/algo/cp.c
deleted file mode 100644
index 4f9ae7a..0000000
--- a/ax/source/algo/cp.c
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- 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 <ax/priv.h>
-
-#include <ax/algo.h>
-
-void ax_cp(void const * const _in,ax_i02 const _num,void * const _out) {
- unsigned char const * in = _in;
- unsigned char * out = _out;
- unsigned char const * const afterbuf = in + _num;
- for (;in != afterbuf;++in,++out) {*out = *in;}
-}
diff --git a/ax/source/algo/cp.s b/ax/source/algo/cp.s
new file mode 100644
index 0000000..bcb6a6f
--- /dev/null
+++ b/ax/source/algo/cp.s
@@ -0,0 +1,51 @@
+@ 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>.
+
+.syntax unified
+
+.cpu arm7tdmi
+.thumb
+
+.globl ax_cp
+
+.func
+.thumb_func
+
+ax_cp:
+ @ ax_i02 tmp4;
+ @ ax_i8 tmp1;
+
+.wrdcp: @ wrdcp:;
+ @ Check if there are at least four bytes remaining:
+ cmp r1,0x4
+ blt .bytecp @ if (num < 0x4u) goto bytecp;
+
+ @ Copy one word:
+ ldm r0!,{r3} @ tmp4 = *(ax_i02 *)in; /* We use ldm/stm with an exclamation mark after the source/destination as this version saves the incremented address into the register, meaning we don't have to icrement it ourselves. */
+ stm r2!,{r3} @ *(ax_i02 *)out = tmp4;
+
+ @ Continue to the next word:
+ subs r1,0x4 @ num -= 0x4u;
+ b .wrdcp @ goto wrdcp;
+
+.bytecp: @ bytecp:;
+ @ Check if we have any bytes remaining:
+ cmp r1,0x0
+ beq .done @ if (num == 0x0u) goto done;
+
+ @ Copy one byte:
+ ldrb r3,[r0] @ tmp1 = *(ax_i8 *)in;
+ strb r3,[r2] @ *(ax_i8 *)out = tmp1;
+
+ @ Continue to the next byte:
+ adds r0,0x1 @ ++in;
+ adds r2,0x1 @ ++out;
+ subs r1,0x1 @ --num;
+ b .bytecp @ goto bytecp;
+
+.done: @ done:;
+ @ Return:
+ bx lr @ return;
+
+.endfunc
diff --git a/ax/source/algo/cp8.s b/ax/source/algo/cp8.s
new file mode 100644
index 0000000..94456ba
--- /dev/null
+++ b/ax/source/algo/cp8.s
@@ -0,0 +1,36 @@
+@ 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>.
+
+.syntax unified
+
+.cpu arm7tdmi
+.thumb
+
+.globl ax_cp8
+
+.func
+.thumb_func
+
+ax_cp8:
+ @ ax_i8 tmp;
+ adds r1,r0 @ void * end = in + num;
+.cp: @ cp:;
+ @ Check if we have any bytes remaining:
+ cmp r0,r1
+ beq .done @ if (num == end) goto done;
+
+ @ Copy one byte:
+ ldrb r3,[r0] @ tmp = *(ax_i8 *)in;
+ strb r3,[r2] @ *(ax_i8 *)out = tmp;
+
+ @ Continue to the next byte:
+ adds r0,0x1 @ ++in;
+ adds r2,0x1 @ ++out;
+ b .cp @ goto cp;
+
+.done: @ done:;
+ @ Return:
+ bx lr @ return;
+
+.endfunc
diff --git a/ax/source/algo/divmod.c b/ax/source/algo/divmod.c
new file mode 100644
index 0000000..0576ea3
--- /dev/null
+++ b/ax/source/algo/divmod.c
@@ -0,0 +1,18 @@
+/*
+ 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 <ax/priv.h>
+
+#include <ax/algo.h>
+
+ax_quotrem ax_divmod(ax_i02 const _num,ax_i02 const _den) {
+ if (__builtin_expect(_den == 0x0,0x0)) {
+ ax_done(ax_err_divzero);
+ }
+ ax_quotrem quotrem;
+ for (quotrem = (ax_quotrem){.quot = 0x0u,.rem = _num};quotrem.rem >= _den;++quotrem.quot,quotrem.rem -= _den) {}
+ return quotrem;
+}
diff --git a/ax/source/gfx/flip.s b/ax/source/gfx/flip.s
index 2676d95..57cb589 100644
--- a/ax/source/gfx/flip.s
+++ b/ax/source/gfx/flip.s
@@ -17,20 +17,20 @@
ax_flip:
@ Get the current value of dispcntrl:
ldr r0,.dispcntrladdr @ ax_i02 dispcntrladdr = 0x4000000u;
- ldrh r1,[r0] @ ax_i01 dispcntrl = *(ax_i01 *)dispcntrladdr;
+ ldrh r1,[r0] @ ax_i01 dispcntrl = *(ax_i01 *)dispcntrladdr;
@ XOR bit four:
movs r2,0b10000
- eors r1,r2 @ dispcntrl ^= 0b10000u;
+ eors r1,r2 @ dispcntrl ^= 0b10000u;
@ Save dispcntrl:
- strh r1,[r0] @ *(ax_i01 *)dispcntrladdr = dispcntrl;
+ strh r1,[r0] @ *(ax_i01 *)dispcntrladdr = dispcntrl;
@ Get the address of the video bank:
- b __ax_getvbnk @ ax_i02 vaddr = __ax_getvbnk();
+ b __ax_getvbnk @ ax_i02 vaddr = __ax_getvbnk();
@ Return:
- bx lr @ return vaddr;
+ bx lr @ return vaddr;
.endfunc
diff --git a/ax/source/gfx/getvbnk.s b/ax/source/gfx/getvbnk.s
index da4c30a..d526e47 100644
--- a/ax/source/gfx/getvbnk.s
+++ b/ax/source/gfx/getvbnk.s
@@ -16,12 +16,12 @@
ax_getvbnk:
@ Get the current value of dispcntrl:
ldr r0,.dispcntrladdr @ ax_i02 dispcntrladdr = 0x4000000u;
- ldrh r1,[r0] @ ax_i01 dispcntrl = *(ax_i01 *)dispcntrladdr;
+ ldrh r1,[r0] @ ax_i01 dispcntrl = *(ax_i01 *)dispcntrladdr;
@ Get the address:
- b __ax_getvbnk @ ax_i02 vaddr = __ax_getvbnk();
+ b __ax_getvbnk @ ax_i02 vaddr = __ax_getvbnk();
- bx lr @ return vaddr;
+ bx lr @ return vaddr;
.endfunc
diff --git a/ax/source/gfx/setcol.s b/ax/source/gfx/setcol.s
index 4738099..5276df4 100644
--- a/ax/source/gfx/setcol.s
+++ b/ax/source/gfx/setcol.s
@@ -15,11 +15,11 @@
ax_setcol:
@ Get the address of the colour:
ldr r2,.paladdr @ ax_i02 paladdr = 0x5000000u;
- adds r2,r0 @ paladdr += _n;
- adds r2,r0 @ paladdr += _n; /* Add the colour number twice as each colour value takes up two bytes. */
+ adds r2,r0 @ paladdr += n;
+ adds r2,r0 @ paladdr += n; /* Add the index number twice as each colour value takes up two bytes. This is also simpler than multiplying by two. */
@ Set the colour value:
- strh r1,[r2] @ *(ax_i01 *)paladdr = _col;
+ strh r1,[r2] @ *(ax_i01 *)paladdr = col;
@ Return:
bx lr @ return;
diff --git a/ax/source/string/memcpy.c b/ax/source/string/memcpy.c
deleted file mode 100644
index c38ca09..0000000
--- a/ax/source/string/memcpy.c
+++ /dev/null
@@ -1,15 +0,0 @@
-/*
- 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 <ax/priv.h>
-
-#include <ax/algo.h>
-#include <string.h>
-
-void * memcpy(void * const restrict _dest,void const * const restrict _src,size_t const _count) {
- ax_cp(_src,_count,_dest);
- return _dest;
-}
diff --git a/ax/source/string/memcpy.s b/ax/source/string/memcpy.s
new file mode 100644
index 0000000..c5efaa3
--- /dev/null
+++ b/ax/source/string/memcpy.s
@@ -0,0 +1,30 @@
+@ 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>.
+
+.syntax unified
+
+.cpu arm7tdmi
+.thumb
+
+.extern ax_cp
+
+.globl memcpy
+
+.func
+.thumb_func
+
+memcpy:
+ push {r0,lr} @ Save the value of s1 (r0) as we need to return it later.
+
+ @ Order the parameters correctly:
+ movs r3,r2
+ movs r2,r0
+ movs r0,r1
+ movs r1,r3
+
+ bl ax_cp
+ pop {r0,r1} @ We cannot pop into lr.
+ bx r1
+
+.endfunc
diff --git a/ax/source/string/memmove.s b/ax/source/string/memmove.s
new file mode 100644
index 0000000..8a2ec88
--- /dev/null
+++ b/ax/source/string/memmove.s
@@ -0,0 +1,30 @@
+@ 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>.
+
+.syntax unified
+
+.cpu arm7tdmi
+.thumb
+
+.extern ax_cp
+
+.globl memmove
+
+.func
+.thumb_func
+
+memmove:
+ push {r0,lr} @ Save the value of s1 (r0) as we need to return it later.
+
+ @ Order the parameters correctly:
+ movs r3,r2
+ movs r2,r0
+ movs r0,r1
+ movs r1,r3
+
+ bl ax_cp8
+ pop {r0,r1} @ We cannot pop into lr.
+ bx r1
+
+.endfunc
diff --git a/ax/source/string/memset.c b/ax/source/string/memset.c
deleted file mode 100644
index 3bf5545..0000000
--- a/ax/source/string/memset.c
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- 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 <ax/priv.h>
-
-#include <ax/algo.h>
-#include <string.h>
-
-void * memset(void * const _dest,int const _ch,size_t const _count) {
- unsigned char const byte = (unsigned char)_ch;
- ax_fill(_dest,_count,byte);
- return _dest;
-}
diff --git a/ax/source/string/memset.s b/ax/source/string/memset.s
new file mode 100644
index 0000000..8f419dc
--- /dev/null
+++ b/ax/source/string/memset.s
@@ -0,0 +1,29 @@
+@ 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>.
+
+.syntax unified
+
+.cpu arm7tdmi
+.thumb
+
+.extern ax_fill
+
+.globl memset
+
+.func
+.thumb_func
+
+memset:
+ push {r0,lr}
+
+ @ Order the parameters correctly:
+ movs r3,r1
+ movs r1,r2
+ movs r2,r3
+
+ bl ax_fill
+ pop {r0,r1}
+ bx r1
+
+.endfunc
diff --git a/ax/source/string/strlen.c b/ax/source/string/strlen.c
deleted file mode 100644
index 401a8a4..0000000
--- a/ax/source/string/strlen.c
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- 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 <ax/priv.h>
-
-#include <ax/algo.h>
-#include <string.h>
-
-size_t strlen(char const * const _s) {
- char const * pos = _s;
- while (*pos++) {}
- return (size_t)(pos - _s);
-}
diff --git a/ax/source/string/strlen.s b/ax/source/string/strlen.s
new file mode 100644
index 0000000..e9daab8
--- /dev/null
+++ b/ax/source/string/strlen.s
@@ -0,0 +1,38 @@
+@ 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>.
+
+.syntax unified
+
+.cpu arm7tdmi
+.thumb
+
+.globl strlen
+
+.func
+.thumb_func
+
+strlen:
+ @ char chr;
+
+ movs r1,r0 @ char const * start = s;
+
+.loop: @ loop:;
+ @ Move the character into a register:
+ ldrb r2,[r0] @ chr = *s;
+
+ @ Check if we have reached the null-terminator:
+ cmp r2,0x0
+ beq .done @ if (chr == '\x0') goto done;
+
+ @ Continue to the next character:
+ adds r0,0x1 @ ++s;
+ b .loop @ goto loop;
+
+.done:
+ @ Calculate the length:
+ subs r0,r1 @ s -= start;
+
+ bx lr @ return (size_t)s;
+
+.endfunc