kvisc/vm/pc/main.c

137 lines
2.5 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 <sys/time.h>
#include <signal.h>
#include <pc/device.h>
#include <pc/console.h>
#define FWPROGSIZE (1024 * 1024 * 1024)
static ssize_t fwsize;
static ushort *fwprog;
ctx_t main_ctx;
void sigcommon(void) { _except(E_BRK, "SIGNAL'ed"); }
void sigint(int _) { sigcommon(); }
void sigsegv(int _) { logerr("\nCaught: Segmentation fault\n\n"); dumpregs(); die(1); }
jmp_buf exc_jmp_buf;
//
// Main loop
//
void main_loop(void)
{
setjmp(exc_jmp_buf);
// Start decoding
while (!dying) {
// Execute one instruction
decode();
console_update();
}
die(0);
}
int main(int argc, char **argv)
{
FILE *fwfile;
#if 0 && !defined(NDEBUG)
main_ctx.dumpsw = 1;
#endif
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));
//
// Signal handling
//
signal(SIGINT, sigint);
signal(SIGBUS, sigsegv);
signal(SIGSEGV, sigsegv);
//
// Load program
//
if (argc < 3) {
logerr("Not enough arguments\n");
exit(-3);
}
create_symtab(argv[2]);
fwfile = fopen(argv[1], "rb");
if (!fwfile) {
logerr("Couldn't open program file\n");
exit(-2);
}
fwprog = malloc(FWPROGSIZE);
if (!fwprog) {
logerr("Couldn't allocate firmware buffer\n");
exit(-1);
}
fwsize = fread(fwprog, 1, FWPROGSIZE, fwfile);
if (fwsize < 2) {
logerr("Program file too small or empty\n");
free(fwprog);
exit(-3);
}
fclose(fwfile);
//
// Register frame allocation
//
main_ctx.rf = calloc(NREGS, sizeof(ulong));
//
// Memory allocation
//
main_ctx.mp = malloc(MEMSIZE + 16);
main_ctx.mz = MEMSIZE;
main_ctx.rf[EIP] = MEMOFF;
if (main_ctx.mp == 0) {
logerr("Couldn't allocate RAM\n");
free(main_ctx.rf);
free(fwprog);
exit(-1);
}
memcpy(&main_ctx.mp[addr2real(main_ctx.rf[EIP])], fwprog, fwsize);
//
// Devices initialization
//
main_ctx.dh = 0;
if (devinitall() < 0) {
logerr("Couldn't initialize devices\n");
free(main_ctx.rf);
free(fwprog);
exit(-10);
}
// To be moved to some screen device
console_init();
main_loop();
}