2016-05-19 10:42:39 +00:00
|
|
|
#include "vm.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2016-05-19 11:19:38 +00:00
|
|
|
void dump_proc(Process *process)
|
|
|
|
{
|
2016-05-19 13:41:18 +00:00
|
|
|
if(process->flags & VM_FLAG_Z)
|
|
|
|
printf("z");
|
|
|
|
else
|
|
|
|
printf(" ");
|
|
|
|
|
|
|
|
if(process->flags & VM_FLAG_N)
|
|
|
|
printf("n");
|
|
|
|
else
|
|
|
|
printf(" ");
|
2016-05-21 11:34:14 +00:00
|
|
|
|
|
|
|
printf(
|
|
|
|
"%2d %2d ",
|
|
|
|
process->codePointer,
|
|
|
|
process->basePointer);
|
2016-05-19 13:41:18 +00:00
|
|
|
|
2016-05-19 11:19:38 +00:00
|
|
|
printf("[ ");
|
|
|
|
for(int i = 1; i <= process->stackPointer; i++) {
|
|
|
|
printf("%d ", process->stack[i]);
|
|
|
|
}
|
|
|
|
printf("]\n");
|
|
|
|
}
|
2016-05-19 10:42:39 +00:00
|
|
|
|
2016-05-19 11:19:38 +00:00
|
|
|
void vm_assert(int assertion, const char *msg)
|
|
|
|
{
|
|
|
|
if(assertion != 0)
|
|
|
|
return;
|
|
|
|
printf("Assertion failed: %s\n", msg);
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-05-21 15:57:13 +00:00
|
|
|
|
|
|
|
void vm_syscall(Process *p, CommandInfo *info)
|
|
|
|
{
|
|
|
|
printf("Some syscall: (%d, %d, %d, %d)\n",
|
|
|
|
info->input0,
|
|
|
|
info->input1,
|
|
|
|
info->argument,
|
|
|
|
info->additional);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vm_hwio(Process *p, CommandInfo *info)
|
|
|
|
{
|
|
|
|
printf("Some hardware IO: (%d, %d, %d, %d)\n",
|
|
|
|
info->input0,
|
|
|
|
info->input1,
|
|
|
|
info->argument,
|
|
|
|
info->additional);
|
|
|
|
}
|
2016-05-19 10:42:39 +00:00
|
|
|
|
2016-05-19 11:19:38 +00:00
|
|
|
int main(int argc, const char **argv)
|
|
|
|
{
|
2016-05-21 11:34:14 +00:00
|
|
|
if(argc < 2) {
|
|
|
|
printf("Usage: vm [binary]\n");
|
|
|
|
printf(" [binary]: The file to be executed.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Module module = { 0 };
|
|
|
|
{ // Read module
|
|
|
|
FILE *f = fopen(argv[1], "rb");
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
size_t len = ftell(f);
|
|
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
module.code = malloc(len);
|
|
|
|
module.length = len / sizeof(Instruction);
|
|
|
|
|
|
|
|
char *ptr = (char*)module.code;
|
|
|
|
for(int p = 0; p < len; p += fread(&ptr[p], 1, sizeof(Instruction), f));
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
printf("Loaded module %s with %d instructions.\n", argv[1], module.length);
|
|
|
|
for(int i = 0; i < module.length; i++)
|
|
|
|
{
|
|
|
|
Instruction c = module.code[i];
|
|
|
|
printf(
|
|
|
|
"%3d: %10d %1d %1d %5d %3d %1d %1d %1d %1d\n",
|
|
|
|
i,
|
|
|
|
c.argument,
|
|
|
|
c.output,
|
|
|
|
c.flags,
|
|
|
|
c.cmdinfo,
|
|
|
|
c.command,
|
|
|
|
c.input1,
|
|
|
|
c.input0,
|
|
|
|
c.execN,
|
|
|
|
c.execZ);
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:19:38 +00:00
|
|
|
Process process = { &module, 0, 0, 0, 0 };
|
|
|
|
Process *p = &process;
|
|
|
|
|
2016-05-19 12:09:15 +00:00
|
|
|
dump_proc(p);
|
2016-05-19 13:41:18 +00:00
|
|
|
while(vm_step_process(p)) {
|
|
|
|
dump_proc(p);
|
|
|
|
}
|
2016-05-19 12:09:15 +00:00
|
|
|
dump_proc(p);
|
2016-05-19 11:19:38 +00:00
|
|
|
|
2016-05-19 10:42:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|