Implement math, jump, convert opcodes, syscall stub,

This commit is contained in:
zongor 2026-01-04 15:43:52 -08:00
parent 6b625c4663
commit da9bc0f587
10 changed files with 268 additions and 630 deletions

View File

@ -1,17 +1,36 @@
#include "../../../vm/vm.h"
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string.h>
int main() {
VM vm = {0};
#define CODE_SIZE 8192
#define MEMORY_SIZE 65536
u8 lmem[MEMORY_SIZE] = {0};
u32 lcode[CODE_SIZE] = {0};
bool init_vm() {
mem = lmem;
memset(mem, 0, MEMORY_SIZE*sizeof(u8));
code = lcode;
mp = 0;
cp = 0;
pc = 0;
interrupt = 0;
return true;
}
u32 syscall(u32 id, u32 args, u32 mem_ptr) {
return 0; // success
}
i32 main() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL initialization failed: %s\n", SDL_GetError());
return 1;
}
SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
while(step_vm(&vm)) {
while(step_vm()) {
// do stuff
}

View File

@ -1,10 +1,33 @@
#include "../../../vm/vm.h"
#include <stdio.h>
#include <string.h>
int main() {
VM vm = {0};
#define CODE_SIZE 8192
#define MEMORY_SIZE 65536
u8 lmem[MEMORY_SIZE] = {0};
u32 lcode[CODE_SIZE] = {0};
while(step_vm(&vm)) {
bool init_vm() {
mem = lmem;
memset(mem, 0, MEMORY_SIZE*sizeof(u8));
code = lcode;
mp = 0;
cp = 0;
pc = 0;
interrupt = 0;
return true;
}
u32 syscall(u32 id, u32 args, u32 mem_ptr) {
USED(id);
USED(args);
USED(mem_ptr);
return 0; // success
}
i32 main() {
while(step_vm()) {
// do stuff
}

View File

@ -4,20 +4,40 @@
#undef true
#undef false
#include <stdio.h>
#include <string.h>
#include <SDL2/SDL.h>
#include <emscripten.h>
#include <emscripten/html5.h>
VM vm = {0};
#define CODE_SIZE 8192
#define MEMORY_SIZE 65536
u8 lmem[MEMORY_SIZE] = {0};
u32 lcode[CODE_SIZE] = {0};
bool init_vm() {
mem = lmem;
memset(mem, 0, MEMORY_SIZE*sizeof(u8));
code = lcode;
mp = 0;
cp = 0;
pc = 0;
interrupt = 0;
return true;
}
u32 syscall(u32 id, u32 args, u32 mem_ptr) {
return 0; // success
}
void mainloop() {
if (!step_vm(&vm)) {
if (!step_vm()) {
emscripten_cancel_main_loop();
return;
}
}
int main() {
i32 main() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL initialization failed: %s\n", SDL_GetError());
return 1;

View File

@ -1,288 +0,0 @@
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Emscripten-Generated Code</title>
<style>
.emscripten { padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
textarea.emscripten { font-family: monospace; width: 80%; }
div.emscripten { text-align: center; }
div.emscripten_border { border: 1px solid black; }
/* the canvas *must not* have any border or padding, or mouse coords will be wrong */
canvas.emscripten { border: 0px none; background-color: black; }
.spinner {
height: 50px;
width: 50px;
margin: 0px auto;
-webkit-animation: rotation .8s linear infinite;
-moz-animation: rotation .8s linear infinite;
-o-animation: rotation .8s linear infinite;
animation: rotation 0.8s linear infinite;
border-left: 10px solid rgb(0,150,240);
border-right: 10px solid rgb(0,150,240);
border-bottom: 10px solid rgb(0,150,240);
border-top: 10px solid rgb(100,0,200);
border-radius: 100%;
background-color: rgb(200,100,250);
}
@-webkit-keyframes rotation {
from {-webkit-transform: rotate(0deg);}
to {-webkit-transform: rotate(360deg);}
}
@-moz-keyframes rotation {
from {-moz-transform: rotate(0deg);}
to {-moz-transform: rotate(360deg);}
}
@-o-keyframes rotation {
from {-o-transform: rotate(0deg);}
to {-o-transform: rotate(360deg);}
}
@keyframes rotation {
from {transform: rotate(0deg);}
to {transform: rotate(360deg);}
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
line-height: 1.6;
color: #ddd;
background: black;
padding: 0;
margin: 0;
}
main {
max-width: 700px;
margin: 0 auto;
padding: 1.5rem;
}
header {
background: #111;
padding: 1rem 1.5rem;
border-bottom: 1px solid #222;
text-align: center;
}
header h1 {
font-size: 1.5rem;
font-weight: 600;
color: #ddd;
}
nav a {
color: #ddd;
padding: 12px 15px;
text-decoration: none;
text-align: center;
cursor: pointer;
transition: all 0.3s ease;
flex-grow: 1;
}
nav a:hover {
background-color: #1b1b1b;
}
a {
color: #ddd;
}
textarea {
background: #111;
padding: 1rem 1.5rem;
border-top: 1px solid #222;
text-align: center;
margin-top: 2rem;
font-size: 0.9rem;
color: #ddd;
}
p {
margin-bottom: 1.25rem;
}
code {
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
background-color: #1a1a1a;
padding: 0.2em 0.4em;
border-radius: 3px;
font-size: 0.9em;
}
pre {
background: #262620;
color: #f8f8f2;
padding: 1rem;
border-radius: 5px;
overflow-x: auto;
margin: 1.5rem 0;
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
font-size: 0.95rem;
line-height: 1.5;
}
pre code {
background: transparent;
padding: 0;
font-size: 1em;
color: #f8f8f2;
}
@media (min-width: 768px) {
main {
padding: 2rem;
}
header h1 {
font-size: 2rem;
}
}
@media (prefers-color-scheme: light) {
body {
background-color: white;
color: black;
}
header {
background: #ddd;
color: black;
}
a {
color: black;
}
nav a {
color: black;
}
nav a:hover {
background-color: #eee;
color: black;
}
main {
background-color: white;
}
pre {
background-color: #f5f5f5;
border: 1px solid #ddd;
}
pre code {
color: #333;
}
textarea {
background: #ddd;
}
}
</style>
</head>
<body>
<hr/>
<figure style="overflow:visible;" id="spinner"><div class="spinner"></div><center style="margin-top:0.5em"><strong>emscripten</strong></center></figure>
<div class="emscripten" id="status">Downloading...</div>
<div class="emscripten">
<progress value="0" max="100" id="progress" hidden=1></progress>
</div>
<div class="emscripten_border">
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" tabindex=-1></canvas>
</div>
<hr/>
<div class="emscripten">
<input type="checkbox" id="resize">Resize canvas
<input type="checkbox" id="pointerLock" checked>Lock/hide mouse pointer
&nbsp;&nbsp;&nbsp;
<input type="button" value="Fullscreen" onclick="Module.requestFullscreen(document.getElementById('pointerLock').checked,
document.getElementById('resize').checked)">
</div>
<hr/>
<textarea class="emscripten" id="output" rows="8"></textarea>
<hr>
<script type='text/javascript'>
var statusElement = document.getElementById('status');
var progressElement = document.getElementById('progress');
var spinnerElement = document.getElementById('spinner');
var canvasElement = document.getElementById('canvas');
var outputElement = document.getElementById('output');
if (outputElement) outputElement.value = ''; // clear browser cache
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvasElement.addEventListener("webglcontextlost", (e) => {
alert('WebGL context lost. You will need to reload the page.');
e.preventDefault();
}, false);
var Module = {
print(...args) {
// These replacements are necessary if you render to raw HTML
//text = text.replace(/&/g, "&amp;");
//text = text.replace(/</g, "&lt;");
//text = text.replace(/>/g, "&gt;");
//text = text.replace('\n', '<br>', 'g');
console.log(...args);
if (outputElement) {
var text = args.join(' ');
outputElement.value += text + "\n";
outputElement.scrollTop = outputElement.scrollHeight; // focus on bottom
}
},
canvas: canvasElement,
setStatus(text) {
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
if (text === Module.setStatus.last.text) return;
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
var now = Date.now();
if (m && now - Module.setStatus.last.time < 30) return; // if this is a progress update, skip it if too soon
Module.setStatus.last.time = now;
Module.setStatus.last.text = text;
if (m) {
text = m[1];
progressElement.value = parseInt(m[2])*100;
progressElement.max = parseInt(m[4])*100;
progressElement.hidden = false;
spinnerElement.hidden = false;
} else {
progressElement.value = null;
progressElement.max = null;
progressElement.hidden = true;
if (!text) spinnerElement.hidden = true;
}
statusElement.innerHTML = text;
},
totalDependencies: 0,
monitorRunDependencies(left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
}
};
Module.setStatus('Downloading...');
window.onerror = () => {
Module.setStatus('Exception thrown, see JavaScript console');
spinnerElement.style.display = 'none';
Module.setStatus = (text) => {
if (text) console.error('[post-exception status] ' + text);
};
};
</script>
{{{ SCRIPT }}}
</body>
</html>

View File

@ -3,19 +3,38 @@
#undef false
#include <stdio.h>
#include <string.h>
#include <emscripten.h>
#include <emscripten/html5.h>
VM vm = {0};
#define CODE_SIZE 8192
#define MEMORY_SIZE 65536
u8 lmem[MEMORY_SIZE] = {0};
u32 lcode[CODE_SIZE] = {0};
bool init_vm() {
mem = lmem;
memset(mem, 0, MEMORY_SIZE*sizeof(u8));
code = lcode;
mp = 0;
cp = 0;
pc = 0;
interrupt = 0;
return true;
}
u32 syscall(u32 id, u32 args, u32 mem_ptr) {
return 0; // success
}
void mainloop(void) {
if (!step_vm(&vm)) {
if (!step_vm()) {
emscripten_cancel_main_loop();
return;
}
}
int main(void) {
i32 main(void) {
printf("VM loaded successfully\n");
emscripten_set_main_loop(mainloop, 0, 1);

View File

@ -1,288 +0,0 @@
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Emscripten-Generated Code</title>
<style>
.emscripten { padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
textarea.emscripten { font-family: monospace; width: 80%; }
div.emscripten { text-align: center; }
div.emscripten_border { border: 1px solid black; }
/* the canvas *must not* have any border or padding, or mouse coords will be wrong */
canvas.emscripten { border: 0px none; background-color: black; }
.spinner {
height: 50px;
width: 50px;
margin: 0px auto;
-webkit-animation: rotation .8s linear infinite;
-moz-animation: rotation .8s linear infinite;
-o-animation: rotation .8s linear infinite;
animation: rotation 0.8s linear infinite;
border-left: 10px solid rgb(0,150,240);
border-right: 10px solid rgb(0,150,240);
border-bottom: 10px solid rgb(0,150,240);
border-top: 10px solid rgb(100,0,200);
border-radius: 100%;
background-color: rgb(200,100,250);
}
@-webkit-keyframes rotation {
from {-webkit-transform: rotate(0deg);}
to {-webkit-transform: rotate(360deg);}
}
@-moz-keyframes rotation {
from {-moz-transform: rotate(0deg);}
to {-moz-transform: rotate(360deg);}
}
@-o-keyframes rotation {
from {-o-transform: rotate(0deg);}
to {-o-transform: rotate(360deg);}
}
@keyframes rotation {
from {transform: rotate(0deg);}
to {transform: rotate(360deg);}
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
line-height: 1.6;
color: #ddd;
background: black;
padding: 0;
margin: 0;
}
main {
max-width: 700px;
margin: 0 auto;
padding: 1.5rem;
}
header {
background: #111;
padding: 1rem 1.5rem;
border-bottom: 1px solid #222;
text-align: center;
}
header h1 {
font-size: 1.5rem;
font-weight: 600;
color: #ddd;
}
nav a {
color: #ddd;
padding: 12px 15px;
text-decoration: none;
text-align: center;
cursor: pointer;
transition: all 0.3s ease;
flex-grow: 1;
}
nav a:hover {
background-color: #1b1b1b;
}
a {
color: #ddd;
}
textarea {
background: #111;
padding: 1rem 1.5rem;
border-top: 1px solid #222;
text-align: center;
margin-top: 2rem;
font-size: 0.9rem;
color: #ddd;
}
p {
margin-bottom: 1.25rem;
}
code {
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
background-color: #1a1a1a;
padding: 0.2em 0.4em;
border-radius: 3px;
font-size: 0.9em;
}
pre {
background: #262620;
color: #f8f8f2;
padding: 1rem;
border-radius: 5px;
overflow-x: auto;
margin: 1.5rem 0;
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
font-size: 0.95rem;
line-height: 1.5;
}
pre code {
background: transparent;
padding: 0;
font-size: 1em;
color: #f8f8f2;
}
@media (min-width: 768px) {
main {
padding: 2rem;
}
header h1 {
font-size: 2rem;
}
}
@media (prefers-color-scheme: light) {
body {
background-color: white;
color: black;
}
header {
background: #ddd;
color: black;
}
a {
color: black;
}
nav a {
color: black;
}
nav a:hover {
background-color: #eee;
color: black;
}
main {
background-color: white;
}
pre {
background-color: #f5f5f5;
border: 1px solid #ddd;
}
pre code {
color: #333;
}
textarea {
background: #ddd;
}
}
</style>
</head>
<body>
<hr/>
<figure style="overflow:visible;" id="spinner"><div class="spinner"></div><center style="margin-top:0.5em"><strong>emscripten</strong></center></figure>
<div class="emscripten" id="status">Downloading...</div>
<div class="emscripten">
<progress value="0" max="100" id="progress" hidden=1></progress>
</div>
<div class="emscripten_border">
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" tabindex=-1></canvas>
</div>
<hr/>
<div class="emscripten">
<input type="checkbox" id="resize">Resize canvas
<input type="checkbox" id="pointerLock" checked>Lock/hide mouse pointer
&nbsp;&nbsp;&nbsp;
<input type="button" value="Fullscreen" onclick="Module.requestFullscreen(document.getElementById('pointerLock').checked,
document.getElementById('resize').checked)">
</div>
<hr/>
<textarea class="emscripten" id="output" rows="8"></textarea>
<hr>
<script type='text/javascript'>
var statusElement = document.getElementById('status');
var progressElement = document.getElementById('progress');
var spinnerElement = document.getElementById('spinner');
var canvasElement = document.getElementById('canvas');
var outputElement = document.getElementById('output');
if (outputElement) outputElement.value = ''; // clear browser cache
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvasElement.addEventListener("webglcontextlost", (e) => {
alert('WebGL context lost. You will need to reload the page.');
e.preventDefault();
}, false);
var Module = {
print(...args) {
// These replacements are necessary if you render to raw HTML
//text = text.replace(/&/g, "&amp;");
//text = text.replace(/</g, "&lt;");
//text = text.replace(/>/g, "&gt;");
//text = text.replace('\n', '<br>', 'g');
console.log(...args);
if (outputElement) {
var text = args.join(' ');
outputElement.value += text + "\n";
outputElement.scrollTop = outputElement.scrollHeight; // focus on bottom
}
},
canvas: canvasElement,
setStatus(text) {
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
if (text === Module.setStatus.last.text) return;
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
var now = Date.now();
if (m && now - Module.setStatus.last.time < 30) return; // if this is a progress update, skip it if too soon
Module.setStatus.last.time = now;
Module.setStatus.last.text = text;
if (m) {
text = m[1];
progressElement.value = parseInt(m[2])*100;
progressElement.max = parseInt(m[4])*100;
progressElement.hidden = false;
spinnerElement.hidden = false;
} else {
progressElement.value = null;
progressElement.max = null;
progressElement.hidden = true;
if (!text) spinnerElement.hidden = true;
}
statusElement.innerHTML = text;
},
totalDependencies: 0,
monitorRunDependencies(left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
}
};
Module.setStatus('Downloading...');
window.onerror = () => {
Module.setStatus('Exception thrown, see JavaScript console');
spinnerElement.style.display = 'none';
Module.setStatus = (text) => {
if (text) console.error('[post-exception status] ' + text);
};
};
</script>
{{{ SCRIPT }}}
</body>
</html>

4
build
View File

@ -81,7 +81,7 @@ case $UI in
LINK_FLAGS=""
;;
"emcc")
LINK_FLAGS="-s ASYNCIFY --shell-file $SRC_DIR/shell_minimal.html"
LINK_FLAGS="-s ASYNCIFY --shell-file $SRC_DIR/../shell_minimal.html"
;;
esac
;;
@ -91,7 +91,7 @@ case $UI in
LINK_FLAGS="$(sdl2-config --libs --cflags)"
;;
"emcc")
LINK_FLAGS="-s USE_SDL=2 -s ASYNCIFY --shell-file $SRC_DIR/shell_minimal.html"
LINK_FLAGS="-s USE_SDL=2 -s ASYNCIFY --shell-file $SRC_DIR/../shell_minimal.html"
;;
esac
;;

View File

@ -22,6 +22,9 @@ typedef float f32;
#define AS_INT(v) ((i32)(v))
#define AS_NAT(v) ((u32)(v))
#define AS_REAL(v) ((i32)(v))
#define FLOAT_TO_REAL(v) (((i32)(v)) * 65536.0f)
#define REAL_TO_FLOAT(v) (((f32)(v)) / 65536.0f)
void mcpy(u8 *dest, const u8 *src, u32 n);
i32 scpy(char* to, const char *from, u32 length);

144
vm/vm.c
View File

@ -1,11 +1,13 @@
#include "vm.h"
u32 mp; /* memory pointer */
u32 cp; /* code pointer */
u32 pc; /* program counter */
u32 flag; /* flag */
u32 *code; /* code */
u8 *mem; /* memory */
u32 pc; /* program counter */
u32 cp; /* code pointer */
u32 mp; /* memory pointer */
u32 fp; /* frame pointer */
u32 flag; /* flag */
u8 interrupt; /* device interrupt */
u32 *code; /* code */
u8 *mem; /* memory */
bool step_vm() {
@ -14,18 +16,32 @@ bool step_vm() {
switch (opcode) {
case OP_HALT: {
/* no need to decode, all are zeros */
return false;
}
case OP_CALL: {
}
case OP_RETURN: {
}
case OP_SYSCALL: {
DECODE_A(instruction)
u32 id = fp + dest;
u32 args = fp + src1;
u32 mem_ptr = fp + src2;
flag = syscall(id, args, mem_ptr);
return true;
}
case OP_LOAD_IMM: {
DECODE_B(instruction)
u32 rd = fp + dest;
mem[rd] = imm;
return true;
}
case OP_LOAD_UPPER_IMM: {
DECODE_B(instruction)
u32 rd = fp + dest;
mem[rd] = (mem[rd] | (((u32)(imm)) << 16));
return true;
}
case OP_LOAD_IND_8: {
}
@ -76,52 +92,121 @@ bool step_vm() {
case OP_REG_MOV: {
}
case OP_ADD_INT: {
MATH_OP(i32, +);
}
case OP_SUB_INT: {
MATH_OP(i32, -);
}
case OP_MUL_INT: {
MATH_OP(i32, *);
}
case OP_DIV_INT: {
}
case OP_ABS_INT: {
}
case OP_NEG_INT: {
MATH_OP(i32, /);
}
case OP_ADD_NAT: {
MATH_OP(u32, +);
}
case OP_SUB_NAT: {
MATH_OP(u32, -);
}
case OP_MUL_NAT: {
MATH_OP(u32, *);
}
case OP_DIV_NAT: {
}
case OP_ABS_NAT: {
}
case OP_NEG_NAT: {
MATH_OP(u32, /);
}
case OP_ADD_REAL: {
MATH_OP(i32, +);
}
case OP_SUB_REAL: {
MATH_OP(i32, -);
}
case OP_MUL_REAL: {
DECODE_A(instruction)
u32 rd = fp + dest;
u32 r1 = fp + src1;
u32 r2 = fp + src2;
i32 src1_whole = (i32)mem[r1] >> 16;
i32 src2_whole = (i32)mem[r2] >> 16;
i32 src1_decimal = (i32)mem[r1] & 16;
i32 src2_decimal = (i32)mem[r2] & 16;
i32 result = 0;
result += (src1_whole * src2_whole) << 16;
result += (src1_whole * src2_decimal);
result += (src1_decimal * src2_whole);
result += ((src1_decimal * src2_decimal) >> 16) & 16;
mem[rd] = result;
return true;
}
case OP_DIV_REAL: {
}
case OP_ABS_REAL: {
}
case OP_NEG_REAL: {
DECODE_A(instruction)
i32 result;
u32 rd = fp + dest;
u32 r1 = fp + src1;
u32 r2 = fp + src2;
i32 src1_val = (i32)mem[r1];
i32 src2_val = (i32)mem[r2];
u32 src2_reciprocal = 1;
src2_reciprocal <<= 31;
src2_reciprocal = (u32)(src2_reciprocal / src2_val);
result = src1_val * src2_reciprocal;
result <<= 1;
mem[rd] = result;
return true;
}
case OP_INT_TO_REAL: {
DECODE_A(instruction)
u32 rd = fp + dest;
u32 r1 = fp + src1;
USED(src2);
mem[rd] = (i32)mem[r1] << 16;
return true;
}
case OP_INT_TO_NAT: {
DECODE_A(instruction)
u32 rd = fp + dest;
u32 r1 = fp + src1;
USED(src2);
mem[rd] = (u32)mem[r1];
return true;
}
case OP_NAT_TO_REAL: {
DECODE_A(instruction)
u32 rd = fp + dest;
u32 r1 = fp + src1;
USED(src2);
mem[rd] = (i32)mem[r1] << 16;
return true;
}
case OP_NAT_TO_INT: {
DECODE_A(instruction)
u32 rd = fp + dest;
u32 r1 = fp + src1;
USED(src2);
mem[rd] = (i32)mem[r1];
return true;
}
case OP_REAL_TO_INT: {
DECODE_A(instruction)
u32 rd = fp + dest;
u32 r1 = fp + src1;
USED(src2);
mem[rd] = (i32)mem[r1] >> 16;
return true;
}
case OP_REAL_TO_NAT: {
DECODE_A(instruction)
u32 rd = fp + dest;
u32 r1 = fp + src1;
USED(src2);
mem[rd] = (u32)mem[r1] >> 16;
return true;
}
case OP_BIT_SHIFT_LEFT: {
}
@ -136,6 +221,9 @@ bool step_vm() {
case OP_BIT_XOR: {
}
case OP_JMP_IMM: {
DECODE_C(instruction)
pc = imm;
return true;
}
case OP_JMP_ABS: {
}
@ -144,40 +232,58 @@ bool step_vm() {
case OP_JMP_FLAG: {
}
case OP_JEQ_INT: {
COMPARE_AND_JUMP(i32, ==);
}
case OP_JNE_INT: {
COMPARE_AND_JUMP(i32, !=);
}
case OP_JGT_INT: {
COMPARE_AND_JUMP(i32, >);
}
case OP_JLT_INT: {
COMPARE_AND_JUMP(i32, <);
}
case OP_JLE_INT: {
COMPARE_AND_JUMP(i32, <=);
}
case OP_JGE_INT: {
COMPARE_AND_JUMP(i32, >=);
}
case OP_JEQ_NAT: {
COMPARE_AND_JUMP(u32, ==);
}
case OP_JNE_NAT: {
COMPARE_AND_JUMP(u32, !=);
}
case OP_JGT_NAT: {
COMPARE_AND_JUMP(u32, >);
}
case OP_JLT_NAT: {
COMPARE_AND_JUMP(u32, <);
}
case OP_JLE_NAT: {
COMPARE_AND_JUMP(u32, <=);
}
case OP_JGE_NAT: {
COMPARE_AND_JUMP(u32, >=);
}
case OP_JEQ_REAL: {
COMPARE_AND_JUMP(i32, ==);
}
case OP_JNE_REAL: {
COMPARE_AND_JUMP(i32, !=);
}
case OP_JGE_REAL: {
COMPARE_AND_JUMP(i32, >=);
}
case OP_JGT_REAL: {
COMPARE_AND_JUMP(i32, >);
}
case OP_JLT_REAL: {
COMPARE_AND_JUMP(i32, <);
}
case OP_JLE_REAL: {
COMPARE_AND_JUMP(i32, <=);
}
}

66
vm/vm.h
View File

@ -35,7 +35,7 @@
u32 imm = ((u32)(instruction)) & 0xFFFFFF;
typedef enum {
OP_HALT, /* halt : C : all zeros : halt execution */
OP_HALT, /* halt : A : all zeros : halt execution */
OP_CALL, /* call : A : dest args fn_ptr : creates a new frame */
OP_RETURN, /* return : A : dest args : returns from a frame to the parent frame */
OP_SYSCALL, /* syscall : A : id args mem_ptr : does a system call based on id with args */
@ -69,20 +69,14 @@ typedef enum {
OP_SUB_INT, /* sub_int : A : locals[dest] = locals[src1] - locals[src2] */
OP_MUL_INT, /* mul_int : A : locals[dest] = locals[src1] * locals[src2] */
OP_DIV_INT, /* div_int : A : locals[dest] = locals[src1] / locals[src2] */
OP_ABS_INT, /* abs_int : A : locals[dest] = | locals[src1] | */
OP_NEG_INT, /* neg_int : A : locals[dest] = -locals[src1] */
OP_ADD_NAT, /* add_nat : A : locals[dest] = locals[src1] + locals[src2] */
OP_SUB_NAT, /* sub_nat : A : locals[dest] = locals[src1] - locals[src2] */
OP_MUL_NAT, /* mul_nat : A : locals[dest] = locals[src1] * locals[src2] */
OP_DIV_NAT, /* div_nat : A : locals[dest] = locals[src1] / locals[src2] */
OP_ABS_NAT, /* abs_nat : A : locals[dest] = | locals[src1] | */
OP_NEG_NAT, /* neg_nat : A : locals[dest] = -locals[src1] */
OP_ADD_REAL, /* add_real : A : locals[dest] = locals[src1] + locals[src2] */
OP_SUB_REAL, /* sub_real : A : locals[dest] = locals[src1] - locals[src2] */
OP_MUL_REAL, /* mul_real : A : locals[dest] = locals[src1] * locals[src2] */
OP_DIV_REAL, /* div_real : A : locals[dest] = locals[src1] / locals[src2] */
OP_ABS_REAL, /* abs_real : A : locals[dest] = | locals[src1] | */
OP_NEG_REAL, /* neg_real : A : locals[dest] = -locals[src1] */
OP_INT_TO_REAL, /* int_to_real : A : locals[dest] = locals[src1] as real */
OP_INT_TO_NAT, /* int_to_nat : A : locals[dest] = locals[src1] as nat */
OP_NAT_TO_REAL, /* nat_to_real : A : locals[dest] = locals[src1] as real */
@ -120,33 +114,33 @@ typedef enum {
OP_MAX_OPCODE /* not an opcode count of instructions */
} Opcode;
#define MEM_SIZE 65536
extern u32 pc; /* program counter */
extern u32 cp; /* code pointer */
extern u32 mp; /* memory pointer */
extern u32 fp; /* frame pointer */
extern u32 flag; /* flag */
extern u8 interrupt; /* device interrupt */
extern u32 *code; /* code */
extern u8 *mem; /* memory */
extern u32 mp; /* memory pointer */
extern u32 cp; /* code pointer */
extern u32 pc; /* program counter */
extern u32 flag; /* flag */
extern u32 *code; /* code */
extern u8 *mem; /* memory */
#define READ_U8(addr) (mem[addr])
#define read_u8(addr) (mem[addr])
#define read_u16(addr) \
#define READ_U16(addr) \
(((u16)mem[(addr) + 1] << 8) | ((u16)mem[(addr)]))
#define read_u32(addr) \
#define READ_U32(addr) \
(((u32)mem[(addr) + 3] << 24) | \
((u32)mem[(addr) + 2] << 16) | \
((u32)mem[(addr) + 1] << 8) | ((u32)mem[(addr)]))
#define write_u8(addr, value) \
#define WRITE_U8(addr, value) \
do { \
if ((addr) < sizeof(mem)) { \
mem[(addr)] = (value) & 0xFF; \
} \
} while (0)
#define write_u16(addr, value) \
#define WRITE_U16(addr, value) \
do { \
if ((addr) + 1 < sizeof(mem)) { \
mem[(addr)] = (value) & 0xFF; \
@ -154,7 +148,7 @@ extern u8 *mem; /* memory */
} \
} while (0)
#define write_u32(addr, value) \
#define WRITE_U32(addr, value) \
do { \
if ((addr) + 3 < sizeof(mem)) { \
mem[(addr)] = (value) & 0xFF; \
@ -164,7 +158,37 @@ extern u8 *mem; /* memory */
} \
} while (0)
#define MATH_OP(type, op) \
do { \
DECODE_A(instruction) \
u32 rd = fp + dest; \
u32 r1 = fp + src1; \
u32 r2 = fp + src2; \
mem[rd] = (type)mem[r1] op(type) mem[r2]; \
return true; \
} while (0)
#define COMPARE_AND_JUMP(type, op) \
do { \
DECODE_A(instruction) \
i32 cond; \
u32 mask, target; \
type value; \
type value2; \
u32 rd = fp + dest; \
u32 r1 = fp + src1; \
u32 r2 = fp + src2; \
target = mem[rd]; \
value = (type)mem[r1]; \
value2 = (type)mem[r2]; \
cond = !!(value op value2); \
mask = -(u32)cond; \
pc = (target & mask) | (pc & ~mask); \
return true; \
} while (0)
extern bool init_vm();
extern u32 syscall(u32 id, u32 args, u32 mem_ptr);
bool step_vm();
#endif