diff options
| author | 2025-08-13 19:33:41 +0330 | |
|---|---|---|
| committer | 2025-08-13 19:33:41 +0330 | |
| commit | 8d072d8dc0d22ffd716f53f40893644b8e748fc2 (patch) | |
| tree | ca73a83f5ffdcea61199f74130257045856ccc6b /pu2 | |
| parent | deleted binaries (diff) | |
arithmetics unit in emu
Diffstat (limited to 'pu2')
| -rw-r--r-- | pu2/code.i2 | 19 | ||||
| -rw-r--r-- | pu2/pu2.c | 39 | ||||
| -rw-r--r-- | pu2/pu2.out.c | 16 | ||||
| -rw-r--r-- | pu2/testing.i2 | 5 |
4 files changed, 50 insertions, 29 deletions
diff --git a/pu2/code.i2 b/pu2/code.i2 index 216cd54..14a065f 100644 --- a/pu2/code.i2 +++ b/pu2/code.i2 @@ -1,20 +1,11 @@ -macro setup_io() { +macro puts() { @1 = 0; // write - @2 = 10; // ptr - @3 = 1; // size -} - -macro putchar() { - @10 = /1/; + @2 = /1/; // ptr + @3 = /2/; // size system(); } -setup_io(); - -putchar(['h']); -putchar(['e']); -putchar(['l']); -putchar(['l']); -putchar(['o']); +puts(["hello world +"], [12]); exit(0);
\ No newline at end of file @@ -89,6 +89,37 @@ emu() { m[ 2 ] = getchar(); } + // 2: arithmetics unit + else if (int_code == 2) { + int op = m[ 2 ]; + int val1 = m[ 3 ]; + int val2 = m[ 4 ]; + + m[ 2 ] = 0; + + if (op == 0) { + // multiply + m[ 3 ] = val1 * val2; + } else if (op == 1) { + // divide + m[ 3 ] = val1 / val2; + } else if (op == 2) { + // modulo + m[ 3 ] = val1 % val2; + } else if (op == 3) { + // left shift + m[ 3 ] = val1 << val2; + } else if (op == 4) { + // right shift + m[ 3 ] = val1 >> val2; + } + + else { + // unknown op, set m[2] + m[ 2 ] = 1; + } + } + else { // unknown interrupt. what to do? } @@ -194,7 +225,7 @@ clean_slice_ref(slice *val) { free(val->ref); } str_len++; \ int *dest_start = Data -= str_len; \ int y; \ - for (y = 0; y < str_len-1; y++) { dest_start[ y ] = str_start[ y ]; } \ + for (y = 0; y < str_len - 1; y++) { dest_start[ y ] = str_start[ y ]; } \ Data[ -1 ] = 0; \ value = Data - m; \ } else { \ @@ -204,7 +235,7 @@ clean_slice_ref(slice *val) { free(val->ref); } value; \ }) -T; +char T; string ERR; string ERR_code; int *Code = &m[ 4000 ]; @@ -218,7 +249,7 @@ _ir2(string I, entry macros[ 1 << 9 ]) { if (T == '/' && I[ 1 ] == '/') { // comment I += 2; - while (*I != '\n') I++; + while (*I != '\n' && *I != '\0') I++; } // handle assignments! @@ -499,7 +530,7 @@ string op_names[] = { "hlt", "imm", "lod", "str", "swp", "add", "sub", "and", "n main() { size_t o_size; - ir2(read_file("testing.i2", &o_size)); + ir2(read_file("code.i2", &o_size)); m[ 7 ] = 4000; size_t res_size = Code - &m[ 4000 ]; diff --git a/pu2/pu2.out.c b/pu2/pu2.out.c index e13c983..63c41f1 100644 --- a/pu2/pu2.out.c +++ b/pu2/pu2.out.c @@ -9,16 +9,19 @@ // 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 +#define A 8 +// change this for error reporting on unknown instructions! +// examples: +// #define A __builtin_trap() +// ^ does SIGTRAP +// #define A my_custom_cpu_error_trap() +// ^ calls my_custom_cpu_error_trap() // 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;}} +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:A;u=v;}} // clang-format on // there's just memory. no, really @@ -54,4 +57,5 @@ char*C="hltimmlodstrswpaddsubandnotacc",*Y[1<<10],*E,*L,i;*K;as2(char*I){J(c=*I+ #undef S #undef H -#undef J
\ No newline at end of file +#undef J +#undef E
\ No newline at end of file diff --git a/pu2/testing.i2 b/pu2/testing.i2 deleted file mode 100644 index f7ca192..0000000 --- a/pu2/testing.i2 +++ /dev/null @@ -1,5 +0,0 @@ -@1 = 0; // write -@2 = "hello"; // ptr -@3 = 5; // size -system(); // interrupt - |
