1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/vm/pc/main.c
julianb0 d6089b3999
vm
2019-06-08 09:47:19 +02:00

136 lines
2.4 KiB
C

// The OS/K Team licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include <signal.h>
#include <dv/dev.h>
#include <sys/time.h>
#include <termio.h>
#define FWPROGSIZE (1024 * 1024 * 1024)
static ssize_t fwsize;
static ushort *fwprog;
ushort bget(ctx_t *ctx)
{
if (rip % 2) {
_except(ctx, E_ALI, "Misaligned RIP register: 0x%016lX",
rip);
}
if (addr2real(rip) >= ctx->mz) {
_except(ctx, E_ACC, "Executing out of memory: 0x%016lX",
rip);
}
ushort c = ctx->mp[addr2real(rip)];
rip += 2;
return c;
}
ctx_t main_ctx;
void sigint(int _)
{
//
// Enable stdin echoing
//
struct termios t;
tcgetattr(0, &t);
t.c_lflag |= ECHO;
tcsetattr(0, TCSANOW, &t);
//
// Shut down devices
//
devfiniall(&main_ctx);
exit(0);
}
int main(int argc, char **argv)
{
FILE *fwfile;
main_ctx.r = arch_r;
main_ctx.i = arch_i;
//
// srand
//
struct timeval time;
gettimeofday(&time, NULL);
srandom((time.tv_sec * 1000) + (time.tv_usec / 1000));
//
// Disable stdin echoing
//
struct termios t;
tcgetattr(0, &t);
t.c_lflag &= ~ECHO;
tcsetattr(0, TCSANOW, &t);
//
// Signal handling
//
signal(SIGINT, sigint);
//
// Load program
//
if (argc < 2) {
log("Not enough arguments\n");
exit(-3);
}
fwprog = malloc(FWPROGSIZE);
fwfile = fopen(argv[1], "rb");
if (!fwprog) {
log("Couldn't allocate firmware buffer\n");
exit(-1);
}
if (!fwfile) {
log("Couldn't open program file\n");
exit(-2);
}
fwsize = fread(fwprog, 1, FWPROGSIZE, fwfile);
if (fwsize < 2) {
log("Program file too small or empty\n");
exit(-3);
}
main_ctx.mp = malloc(MEMSIZE + 16);
main_ctx.mz = MEMSIZE;
main_ctx.get = bget;
main_ctx.r[RIP].val = MEMOFF;
if (main_ctx.mp == 0) {
log("Couldn't allocate RAM\n");
exit(-1);
}
memcpy(&main_ctx.mp[addr2real(main_ctx.r[RIP].val)], fwprog, fwsize);
main_ctx.dh = 0;
if (devinitall(&main_ctx) < 0) {
log("Couldn't initialize devices\n");
exit(-10);
}
while (1) {
decode(&main_ctx);
if (main_ctx.step)
getchar();
}
}