aboutsummaryrefslogtreecommitdiff
path: root/pu3
diff options
context:
space:
mode:
authorGravatar Moon Flower <moon@coffin.ir> 2025-08-28 11:00:44 +0330
committerGravatar Moon Flower <moon@coffin.ir> 2025-08-28 11:00:44 +0330
commit1d789ab15afc711152a432e1cf112c50e1db2d1a (patch)
tree743af0d8b049449d926b798bd405770379ded5c8 /pu3
parentpu3 poc4 (diff)
pu3 poc5
Diffstat (limited to 'pu3')
-rw-r--r--pu3/pu3.cpu.c54
1 files changed, 31 insertions, 23 deletions
diff --git a/pu3/pu3.cpu.c b/pu3/pu3.cpu.c
index 312b5a8..a10ca3f 100644
--- a/pu3/pu3.cpu.c
+++ b/pu3/pu3.cpu.c
@@ -86,6 +86,7 @@ typedef struct {
// uint16_t data;
// uint32_t dest;
// } pu3_inst_t;
+
// instructions
// 0: nop
// 1: hlt
@@ -109,14 +110,14 @@ typedef struct {
#define prep_raw() \
({ \
rhs = (uint32_t) get_data_u16(cur); \
- if (get_spare(cur) & 0x40) rhs = ctx.memory.memory_u32[ rhs ]; \
+ if (get_spare(cur) & 0x40) rhs = mem32[ rhs ]; \
})
#define prep_vals() \
({ \
spare = get_spare(cur); \
lhs = get_dest_u32(cur); \
rhs = (uint32_t) get_data_u16(cur); \
- if (spare & 0x40) rhs = ctx.memory.memory_u32[ rhs ]; \
+ if (spare & 0x40) rhs = mem32[ rhs ]; \
if (unlikely(spare & 0x80)) { \
out = lhs; \
lhs = rhs; \
@@ -129,7 +130,10 @@ pu3(pu3_ctx ctx) {
uint64_t i = 0;
pu3_inst_t cur;
- pu3_inst_t *pc = ctx.memory.memory_u64 + *ctx.pc;
+ uint32_t *mem32 = ctx.memory.memory_u32;
+ pu3_inst_t *mem64 = ctx.memory.memory_u64;
+
+ pu3_inst_t *pc = mem64 + *ctx.pc;
byte op;
byte spare;
@@ -162,82 +166,86 @@ pu3_inst_0: // nop
pu3_inst_2: // mov
prep_vals();
- ctx.memory.memory_u32[ lhs ] = rhs;
+ mem32[ lhs ] = rhs;
goto pu3_start;
pu3_inst_3: // add
prep_vals();
- ctx.memory.memory_u32[ lhs ] += rhs;
+ mem32[ lhs ] += rhs;
goto pu3_start;
pu3_inst_4: // sub
prep_vals();
- ctx.memory.memory_u32[ lhs ] -= rhs;
+ mem32[ lhs ] -= rhs;
goto pu3_start;
pu3_inst_5: // inc
prep_only_left();
- ctx.memory.memory_u32[ lhs ]++;
+ mem32[ lhs ]++;
goto pu3_start;
pu3_inst_6: // dec
prep_only_left();
- ctx.memory.memory_u32[ lhs ]--;
+ mem32[ lhs ]--;
goto pu3_start;
pu3_inst_7: // mul
prep_vals();
- ctx.memory.memory_u32[ lhs ] *= rhs;
+ mem32[ lhs ] *= rhs;
goto pu3_start;
pu3_inst_8: // lsh
prep_vals();
- ctx.memory.memory_u32[ lhs ] <<= rhs;
+ mem32[ lhs ] <<= rhs;
goto pu3_start;
pu3_inst_9: // rsh
prep_vals();
- ctx.memory.memory_u32[ lhs ] >>= rhs;
+ mem32[ lhs ] >>= rhs;
goto pu3_start;
pu3_inst_10: // and
prep_vals();
- ctx.memory.memory_u32[ lhs ] &= rhs;
+ mem32[ lhs ] &= rhs;
goto pu3_start;
pu3_inst_11: // not
prep_vals();
- ctx.memory.memory_u32[ lhs ] = !rhs;
+ mem32[ lhs ] = !rhs;
goto pu3_start;
pu3_inst_12: // jmp
prep_only_left();
- pc = ctx.memory.memory_u64 + lhs;
+ pc = mem64 + lhs;
goto pu3_start;
pu3_inst_13: // jnz
prep_vals();
- if (rhs) pc = ctx.memory.memory_u64 + lhs;
+ if (rhs) pc = mem64 + lhs;
goto pu3_start;
-pu3_inst_14: // unused
+pu3_inst_14: // dmv
+ prep_vals();
+ mem32[ lhs ] = mem32[ mem32[ lhs ] ];
+ goto pu3_start;
pu3_inst_15: // unused
+ goto pu3_start;
pu3_inst_1: // hlt
- *ctx.pc = pc - ctx.memory.memory_u64;
+ *ctx.pc = pc - mem64;
return *ctx.time_slice - i;
}
const string op_names[] = { "nop", "hlt", "mov", "add", "sub", "inc", "dec", "mul",
- "lsh", "rsh", "and", "not", "jmp", "jnz", "???", "???" };
+ "lsh", "rsh", "and", "not", "jmp", "jnz", "dmv", "???" };
// bench: measure using clock()
double bench_cycle(byte instr, pu3_ctx ctx) {
int i;
for (i = 16; i < ctx.mem_size - 16; i++) {
set_scroll_byte(ctx.memory.memory_u64[ i ], instr & 0b1111);
- set_data_u16(ctx.memory.memory_u64[ i ], 2);
- set_dest_u32(ctx.memory.memory_u64[ i ], 4);
+ set_data_u16(ctx.memory.memory_u64[ i ], 4);
+ set_dest_u32(ctx.memory.memory_u64[ i ], i);
if (i == ctx.mem_size - 20) {
let j = 0;
@@ -270,7 +278,7 @@ double bench_cycle(byte instr, pu3_ctx ctx) {
}
int main() {
- const uint32_t SIZE = 1 << 24;
+ const uint32_t SIZE = 1 << 23;
let mem_arr = calloc(SIZE, sizeof(pu3_inst_t));
if (!mem_arr) {
@@ -286,8 +294,8 @@ int main() {
int i;
int total = 0;
double benchs = 0;
- for (i = 0; i < 12; i++) {
- if (i == 1) continue;
+ for (i = 0; i < 15; i++) {
+ if (i == 1 || i == 12 || i == 13) continue;
total++;
benchs += bench_cycle(i, ctx);
}