Embedding ReWave
Adi (Addy) edited this page Sep 15, 2021
·
5 revisions
Embedding ReWave to your program is fairly easy. In fact, all you need to do is include ReWave's header(s) and link it to your build unit.
/* host.cpp */
#include "vm.hpp" /* ReWave's main header */
#include <stdio.h> /* For reading the program */
VM g_VM; /* A global VM definition */
#define RETURN(x) g_VM.memory[REG::AX] = x
/*
This is the function where interrupts are handled.
If it's not present the compilation will fail.
"call" is the interrupt code.
*/
void interrupt(const uint32_t call) {
enum {
print,
add
};
switch (call) {
case print: /* BX = char_ptr */
puts((const char*) &g_VM.program[g_VM.memory[REG::BX]]);
break;
case add: /* BX = x, CX = y, AX = return */
RETURN(g_VM.memory[REG::BX] + g_VM.memory[REG::CX]);
break;
}
}
int main() {
/* Feed the VM with the program code */
uint32_t i = 0;
FILE *f = fopen("file_goes_here.o", "r+");
while (!feof(f))
{
g_VM.program[i++] = fgetc(f);
}
fclose(f);
/* Restart the VM and cycle while it's running */
g_VM.restart();
while (g_VM.running) {
g_VM.cycle();
}
}
; test.asm
__init:
jmp main
ret
print = 0x00
msg: #d "hello, world!", 0x00
main:
; void print(msg)
mov BX, msg
int print
ext
Compiling the runtime with GCC and the source code with customasm:
g++ vm.cpp -c -o vm.o
g++ host.cpp vm.o -o host
customasm rulesdef.asm test.asm -f binary -o file_goes_here.o
And now you're ready to run your first ReWave program!
$ ./main
hello, world!
$