umx

UMX VM implementation (ifcfc '06)
git clone git://bsandro.tech/umx
Log | Files | Refs

commit 6472a5ff315b33ad4253e5a329d499081b540469
parent e3c58656dcb744bd6bf43e848028f4e2031ac11d
Author: bsandro <brian.drosan@gmail.com>
Date:   Mon, 10 Jan 2022 04:01:23 +0200

Still trying to fix opcode #13

Diffstat:
Mmain.c | 28++++++++++++++++++++--------
1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/main.c b/main.c @@ -5,6 +5,7 @@ #include <stdint.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include <sys/stat.h> #define FILENAME "sandmark.umz" @@ -48,7 +49,7 @@ struct instruction_t { uint8_t reg_a, reg_b, reg_c; uint32_t value; }; - +uint32_t ntohl(const uint32_t num); char * int2bin(uint32_t num); void print_instruction(struct instruction_t instruction); void exec_instruction(struct state_t *state, struct instruction_t in); @@ -75,26 +76,28 @@ int main(void) read_platters = fread(state.arena.arrays[0].data, PLATTER_SIZE, fileinfo.st_size / PLATTER_SIZE, f); assert(fileinfo.st_size == read_platters * PLATTER_SIZE); printf("read ok, %lu platters (%lu bytes)\n", read_platters, state.arena.arrays[0].size); + fclose(f); while (1) { struct instruction_t instruction = {0}; uint32_t platter = state.arena.arrays[0].data[state.finger]; + printf("[%s]", int2bin(platter)); instruction.opcode = platter >> 28; if (instruction.opcode == ORTH) { instruction.reg_a = (platter >> 25) & 7; - instruction.value = platter & 0x1FFFFFF; + instruction.value = platter & 0x1FFFFFFUL; } else { instruction.reg_a = (platter & 448) >> 6; // mask 111 000 000 = 448 instruction.reg_b = (platter & 56) >> 3; // mask 000 111 000 = 56 instruction.reg_c = platter & 7; // mask 000 000 111 = 7 } - printf("[%2d][%s][%x]", state.finger, int2bin(platter), platter); + printf("[%2d][%8x]", state.finger, platter); print_instruction(instruction); state.finger++; exec_instruction(&state, instruction); + sleep(1); } - - fclose(f); + //getchar(); // in case SIOUX starts closing the window after exec again return 0; } @@ -154,14 +157,14 @@ void exec_instruction(struct state_t *state, struct instruction_t in) { case INP: { int c = getchar(); - state->registers[in.reg_c] = (c == EOF) ? 0xFFFFFFFF : c; + state->registers[in.reg_c] = (c == EOF) ? 0xFFFFFFFFUL : c; } break; case LOAD: - { + if (state->registers[in.reg_b] != 0) { array_dup(&state->arena, state->registers[in.reg_b]); - state->finger = state->registers[in.reg_c]; } + state->finger = state->registers[in.reg_c]; break; case ORTH: state->registers[in.reg_a] = in.value; @@ -199,4 +202,13 @@ void array_dup(struct arena_t *arena, uint32_t index) { memcpy(arena->arrays[0].data, arena->arrays[index].data, arena->arrays[index].size * PLATTER_SIZE); arena->arrays[0].size = arena->arrays[index].size; //printf("memcpy ok\n"); +} + +uint32_t ntohl(const uint32_t num) { + uint8_t data[4] = {0}; + memcpy(&data, &num, sizeof(data)); + return ((uint32_t)data[3] << 0) + | ((uint32_t)data[2] << 8) + | ((uint32_t)data[3] << 16) + | ((uint32_t)data[4] << 24); } \ No newline at end of file