aboutsummaryrefslogtreecommitdiff
path: root/pu2/pu2.out.c
diff options
context:
space:
mode:
authorGravatar Moon Flower <moon@coffin.ir> 2025-08-12 20:27:34 +0330
committerGravatar Moon Flower <moon@coffin.ir> 2025-08-12 20:27:34 +0330
commit15914727d83a6f46ad724e126179713d935e5e6f (patch)
tree2f601872b7575e4f3c0ca8c366fb2a1e1ea1b4fb /pu2/pu2.out.c
parentir2 initial release! (diff)
ir2 prototype! includes strings and DATA segment and other stuffs :3
Diffstat (limited to 'pu2/pu2.out.c')
-rw-r--r--pu2/pu2.out.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/pu2/pu2.out.c b/pu2/pu2.out.c
new file mode 100644
index 0000000..e13c983
--- /dev/null
+++ b/pu2/pu2.out.c
@@ -0,0 +1,57 @@
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#pragma clang diagnostic ignored "-Wparentheses"
+#pragma clang diagnostic ignored "-Wreturn-type"
+#pragma clang diagnostic ignored "-Wempty-body"
+
+// pu2!
+// maybe compacter? idk
+// last cpu: 167 chars
+
+// NOTE: if you want the cpu to
+// crash or do something when an invalid opcode is met,
+// replace the `8` in the code with whatever behavior you desire.
+// you can use ({...}) to insert normal code, or use __builtin_trap() for a SIGTRAP
+
+// clang-format off
+#define S 1<<20
+#define H !--c
+#define J while
+m[S],c,v,a,b,u;pu2(){J(c=m[m[7]++],v=m[m[7]++],c){H?a=v:H?a=m[b]:H?u?m[a]=b:0:H?v=a,a=b,b=v:H?a+=b:H?a-=b:H?a&=b:H?a=!a:H?u=1:8;u=v;}}
+// clang-format on
+
+// there's just memory. no, really
+// ok actually there's two registers called a and b
+// 0 HLT: halt
+// 1 IMM: a = imm
+// 2 LOD: a = m[b]
+// 3 STR: m[a] = b (only happens if acc != 0)
+// 4 SWP: swap a and b
+// 5 ADD: a += b
+// 6 SUB: a -= b
+// 7 AND: a &= b
+// 8 NOT: a = !a
+// 9 ACC: u = imm
+
+// NOTE: Memory indexes less than 1024 should be considered as temporary/compute memory, and
+// programs should be written at index 1024 and upwards.
+//
+// NOTE: Memory index 7 is the program
+// counter, in which the current instruction is read from m[m[7]] at each cycle
+
+// -------------------------
+
+// yeehaw baby it's time for as2
+// E is the error code loc. zero it before calling as2
+// Y is macro storage. extend as needed
+// set O for output before running. keep in mind, it gets modified whilst as2 is running.
+// therefore, i recommend you store a copy somewhere!
+
+// clang-format off
+char*C="hltimmlodstrswpaddsubandnotacc",*Y[1<<10],*E,*L,i;*K;as2(char*I){J(c=*I++){if(c==35){J(*++I-10);c=*I++;}L=C;(!isspace(c)?c-39?c-125?c-123?c-64?({i=0,I--;J(i++,*L)strncmp(L,I,3)?L+=3:({I+=3;*K++=i-1;*K++=(i-2&&i-9)?0:strtol(I,&I,10);goto Q;});E=I;return 1;Q:;}):as2(Y[strtol(I,&I,10)]):({Y[strtol(I,&I,10)]=I+1;J(*I++-125);}):({break;}):({*K++=1;*K++=*I++;}):0);}}
+// clang-format on
+
+#undef S
+#undef H
+#undef J \ No newline at end of file