umx

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

commit 9c1d441ae0ba390b790332229e37df211ab5b20b
parent 78b6c121ea9a9e9106c677168677d4d1388adb03
Author: bsandro <brian.drosan@gmail.com>
Date:   Tue, 11 Jan 2022 09:36:18 +0200

ntohl/htonl conditional wrappers

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

diff --git a/main.c b/main.c @@ -8,7 +8,15 @@ #include <unistd.h> #include <sys/stat.h> -#define FILENAME "codex.umz" +#ifndef __MWERKS__ +#define _NTOHL ntohl +#define _HTONL htonl +#else +#define _NTOHL +#define _HTONL +#endif + +#define FILENAME "SANDMARK.UMZ" #define PLATTER_SIZE 4 struct array_t { @@ -49,7 +57,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); @@ -78,7 +86,7 @@ int main(void) while (1) { struct instruction_t instruction = {0}; - uint32_t platter = state->arena.arrays[0].data[state->finger]; + uint32_t platter = _NTOHL(state->arena.arrays[0].data[state->finger]); //printf("[%s]", int2bin(platter)); instruction.opcode = platter >> 28; if (instruction.opcode == ORTH) { @@ -122,10 +130,10 @@ void exec_instruction(struct state_t *state, struct instruction_t in) { if (state->registers[in.reg_c] != 0) state->registers[in.reg_a] = state->registers[in.reg_b]; break; case ARRI: - state->registers[in.reg_a] = state->arena.arrays[state->registers[in.reg_b]].data[state->registers[in.reg_c]]; + state->registers[in.reg_a] = _NTOHL(state->arena.arrays[state->registers[in.reg_b]].data[state->registers[in.reg_c]]); break; case ARRA: - state->arena.arrays[state->registers[in.reg_a]].data[state->registers[in.reg_b]] = state->registers[in.reg_c]; + state->arena.arrays[state->registers[in.reg_a]].data[state->registers[in.reg_b]] = _HTONL(state->registers[in.reg_c]); break; case ADD: state->registers[in.reg_a] = state->registers[in.reg_b] + state->registers[in.reg_c]; //@todo: modulo 2^32 ? @@ -200,12 +208,3 @@ void array_dup(struct arena_t *arena, uint32_t index) { 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