-
Notifications
You must be signed in to change notification settings - Fork 32
/
minivm.c
44 lines (32 loc) · 1.41 KB
/
minivm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//-----------------------------------------------------------------------------
// minivm.c -- The basic definitions for the vm.
// Copyright (C) Philip Conrad 4/30/2013 @ 3:56 PM -- MIT License
//
//-----------------------------------------------------------------------------
#include "minivm.h"
//---------------------------------------------------------
// FUNCTION IMPLEMENTATIONS:
// Defers decoding of register args to the called function.
// dispatch :: VMContext -> uint32_t -> Effect()
void dispatch(struct VMContext* ctx, const uint32_t instr) {
const uint8_t i = EXTRACT_B0(instr);
(*ctx->funtable[i])(ctx, instr);
}
// Initializes a VMContext in-place.
// initVMContext :: VMContext -> uint32_t -> uint32_t -> [Reg] -> [FunPtr] -> Effect()
void initVMContext(struct VMContext* ctx, const uint32_t numRegs, const uint32_t numFuns, Reg* registers, FunPtr* funtable) {
ctx->numRegs = numRegs;
ctx->numFuns = numFuns;
ctx->r = registers;
ctx->funtable = funtable;
}
// Reads an instruction, executes it, then steps to the next instruction.
// stepVMContext :: VMContext -> uint32_t** -> Effect()
void stepVMContext(struct VMContext* ctx, uint32_t** pc) {
// Read a 32-bit bytecode instruction.
uint32_t instr = **pc;
// Dispatch to an opcode-handler.
dispatch(ctx, instr);
// Increment to next instruction.
(*pc)++;
}