aboutsummaryrefslogtreecommitdiff
path: root/pu.c
diff options
context:
space:
mode:
authorGravatar Moon Flower <moon@coffin.ir> 2025-08-06 22:26:40 +0330
committerGravatar Moon Flower <moon@coffin.ir> 2025-08-06 22:26:40 +0330
commitf693a74d6e08c65fcf621ad049e103e761f01064 (patch)
treef3ec25b54143c9c5f62c1026bcd70c8304cdc80b /pu.c
wowie
Diffstat (limited to 'pu.c')
-rw-r--r--pu.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/pu.c b/pu.c
new file mode 100644
index 0000000..cfe0afb
--- /dev/null
+++ b/pu.c
@@ -0,0 +1,56 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#pragma clang diagnostic ignored "-Wreturn-type"
+#pragma clang diagnostic ignored "-Wc23-extensions"
+
+#define S 65536
+#define H !--c ?
+m[S],c,a;pu(){while((c=m[m[7]++]))H*m=m[m[7]++]:c--<9?a?m[c]=*m:0:8>(c-=8)?*m=m[c]:!(c-=8)?m[m[1]]=*m:H*m=a:H a+=*m:H a-=*m:H a=!a:H a=0:0;}
+
+// a is acc
+// first 8 memory bytes are registers actually
+// c is pc, read from m[7]
+
+// --- opcodes ---
+// 0 = halt DONE
+// 1 = const in r0 DONE
+// 2...9 = r0 in rX (only if acc is non-zero) DONE
+// 10...17 = rX in r0 DONE
+// 18 = r0 in m[r1] DONE
+// 19 = acc in r0 DONE
+// 20 = acc += r0 DONE
+// 21 = acc -= r0 DONE
+// 22 = acc = !acc DONE
+// 23 = acc = 0 DONE
+// for ops 2..9 and 10..17 it basically works like this: 2 handles r0, 3 r1, 4 r2, etc. 10 handles
+// r0, 11 r1, 12 r2 and so on.
+
+// compiler!
+// the switch skips whitespace. then, the beautiful loop thingymajigy emits bytecode.
+
+#define W case
+#define Q goto M;
+char *R="hlt\0 ar1\2 ar2\3 ar3\4 ar4\5 ar5\6 ar6\7 ar7\x8 ar8\x9 r1a\xA r2a\xB r3a\xC r4a\xD r5a\xE r6a\xF r7a\20 r8a\21 mem\22 aca\23 add\24 sub\25 not\26 clr\27";K;as(char*I,int*O){char*V=R;while((K=*I)){V=R;switch(K){case 32:case 10:case 9:I++;Q}while(*V)V+=3,(strncmp(V-3,I,3)?:({*O++=*V;I+=3;Q})),V+=2;strncmp("con ",I,4)?:({*O++=1,*O++=atoi(I+=4);while(*I>47&*I<58)I++;Q});return 1;M:}return 0;}
+
+#undef Q
+#undef H
+#undef W
+#undef S
+
+// ----------
+
+#ifndef __WRAPPER
+int main() {
+ // __asm__("int $3");
+ m[ 7 ] = 1024;
+ m[ 1024 ] = 1;
+ m[ 1025 ] = 9;
+ m[ 1026 ] = 1;
+ m[ 1027 ] = 5;
+
+ printf("before: %i\n", m[ 0 ]);
+ pu();
+ printf("after: %i\n", m[ 0 ]);
+}
+#endif \ No newline at end of file