From 5072f2c51e2b886cf8aaa8550dbf814d1a7828e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Quei=C3=9Fner?= Date: Sat, 21 May 2016 19:26:08 +0200 Subject: [PATCH] Adds single-byte memory interface to the VM. --- prototypes/supervm/vm.c | 42 +++++++++++++++++++++++++++++++++++++++++ prototypes/supervm/vm.h | 13 +++++++++++++ 2 files changed, 55 insertions(+) diff --git a/prototypes/supervm/vm.c b/prototypes/supervm/vm.c index 9a10a09..0323ef9 100644 --- a/prototypes/supervm/vm.c +++ b/prototypes/supervm/vm.c @@ -7,6 +7,20 @@ static void cmd_copy(CommandInfo *info) info->output = info->input0; } +// | 1 | STORE | output = MEMORY[input0] = input1 | +// | 2 | LOAD | output = MEMORY[input0] | + +static void cmd_load(Process *p, CommandInfo *info) +{ + info->output = vm_read_byte(p, info->input0); +} + +static void cmd_store(Process *p, CommandInfo *info) +{ + vm_write_byte(p, info->input0, info->input1); + info->output = info->input1; +} + static void cmd_math(CommandInfo *info) { switch(info->additional) @@ -97,6 +111,8 @@ int vm_step_process(Process *process) switch(instr.command) { case VM_CMD_COPY: cmd_copy(&info); break; + case VM_CMD_STORE: cmd_store(process, &info); break; + case VM_CMD_LOAD: cmd_load(process, &info); break; case VM_CMD_MATH: cmd_math(&info); break; case VM_CMD_SYSCALL: vm_syscall(process, &info); break; case VM_CMD_HWIO: vm_hwio(process, &info); break; @@ -154,3 +170,29 @@ uint32_t vm_peek(Process *process) vm_assert(process != NULL, "process must not be NULL."); return process->stack[process->stackPointer]; } + + + + + +uint8_t vm_read_byte(Process *process, uint32_t address) +{ + vm_assert(process != NULL, "process must not be NULL."); + + uint32_t page = address / process->mmap.pageSize; + uint32_t index = address % process->mmap.pageSize; + vm_assert(page < process->mmap.length, "Out of memory."); + + return process->mmap.pages[page][index]; +} + +void vm_write_byte(Process *process, uint32_t address, uint8_t value) +{ + vm_assert(process != NULL, "process must not be NULL."); + + uint32_t page = address / process->mmap.pageSize; + uint32_t index = address % process->mmap.pageSize; + vm_assert(page < process->mmap.length, "Out of memory."); + + process->mmap.pages[page][index] = value; +} \ No newline at end of file diff --git a/prototypes/supervm/vm.h b/prototypes/supervm/vm.h index e1c4caa..71452d1 100644 --- a/prototypes/supervm/vm.h +++ b/prototypes/supervm/vm.h @@ -140,6 +140,19 @@ uint32_t vm_pop(Process *process); */ uint32_t vm_peek(Process *process); +/** + * Reads a byte from process memory. + * @arg process + * @arg address The address to read from. + */ +uint8_t vm_read_byte(Process *process, uint32_t address); + +/** + * Writes a byte to process memory. + * @arg process + * @arg address The address to read from. + */ +void vm_write_byte(Process *process, uint32_t address, uint8_t value); // The following functions need to be host-implemented.