kvisc/vm/pc/main.c

137 lines
2.5 KiB
C
Raw Normal View History

2019-05-30 12:44:56 +02:00
// The OS/K Team licenses this file to you under the MIT license.
2019-05-15 21:56:42 +02:00
// See the LICENSE file in the project root for more information.
2019-05-15 19:26:40 +02:00
2019-06-16 12:48:30 +02:00
#include <sys/time.h>
2019-06-08 09:47:19 +02:00
#include <signal.h>
2019-06-19 21:41:22 +02:00
2019-06-21 22:19:55 +02:00
#include <pc/device.h>
2019-06-20 17:44:48 +02:00
#include <pc/console.h>
2019-05-15 19:26:40 +02:00
2019-05-22 18:39:46 +02:00
#define FWPROGSIZE (1024 * 1024 * 1024)
static ssize_t fwsize;
static ushort *fwprog;
2019-05-15 19:26:40 +02:00
2019-06-05 19:31:48 +02:00
ctx_t main_ctx;
2019-08-08 18:39:12 +02:00
void sigcommon(void) { _except(E_BRK, "SIGNAL'ed"); }
void sigint(int _) { sigcommon(); }
2019-06-16 12:48:30 +02:00
2019-08-08 18:39:12 +02:00
void sigsegv(int _) { logerr("\nCaught: Segmentation fault\n\n"); dumpregs(); die(1); }
2019-06-05 22:59:32 +02:00
2019-06-21 22:19:55 +02:00
jmp_buf exc_jmp_buf;
2019-06-20 12:31:36 +02:00
//
// Main loop
//
2019-06-18 22:56:41 +02:00
void main_loop(void)
{
2019-06-21 22:19:55 +02:00
setjmp(exc_jmp_buf);
2019-06-18 22:56:41 +02:00
// Start decoding
2019-06-19 23:16:30 +02:00
while (!dying) {
2019-06-20 12:31:36 +02:00
// Execute one instruction
2019-08-03 19:01:12 +02:00
decode();
console_update();
2019-06-18 22:56:41 +02:00
}
2019-06-19 23:16:30 +02:00
die(0);
2019-06-18 22:56:41 +02:00
}
2019-05-16 21:42:23 +02:00
int main(int argc, char **argv)
2019-05-15 19:26:40 +02:00
{
2019-05-22 18:39:46 +02:00
FILE *fwfile;
2019-05-30 12:44:56 +02:00
2019-08-03 17:41:44 +02:00
#if 0 && !defined(NDEBUG)
2019-07-04 20:33:49 +02:00
main_ctx.dumpsw = 1;
2019-07-10 22:37:59 +02:00
#endif
2019-06-05 19:31:48 +02:00
2019-08-08 18:39:12 +02:00
main_ctx.r = arch_r;
main_ctx.i = arch_i;
2019-06-05 22:59:32 +02:00
//
// srand
//
struct timeval time;
2019-06-05 19:31:48 +02:00
gettimeofday(&time, NULL);
srandom((time.tv_sec * 1000) + (time.tv_usec / 1000));
2019-06-05 22:59:32 +02:00
//
// Signal handling
//
2019-06-08 09:47:19 +02:00
signal(SIGINT, sigint);
2019-06-18 22:56:41 +02:00
signal(SIGBUS, sigsegv);
2019-06-16 12:48:30 +02:00
signal(SIGSEGV, sigsegv);
2019-06-05 22:59:32 +02:00
//
// Load program
//
2019-06-19 13:47:10 +02:00
if (argc < 3) {
2019-06-19 21:41:22 +02:00
logerr("Not enough arguments\n");
2019-05-22 18:39:46 +02:00
exit(-3);
}
2019-05-30 12:44:56 +02:00
2019-06-19 13:47:10 +02:00
create_symtab(argv[2]);
2019-05-22 18:39:46 +02:00
fwfile = fopen(argv[1], "rb");
2019-05-30 12:44:56 +02:00
2019-05-22 18:39:46 +02:00
if (!fwfile) {
2019-06-19 21:41:22 +02:00
logerr("Couldn't open program file\n");
2019-05-22 18:39:46 +02:00
exit(-2);
}
2019-05-30 12:44:56 +02:00
2019-06-18 12:58:26 +02:00
fwprog = malloc(FWPROGSIZE);
if (!fwprog) {
2019-06-19 21:41:22 +02:00
logerr("Couldn't allocate firmware buffer\n");
2019-06-18 12:58:26 +02:00
exit(-1);
}
2019-05-22 18:39:46 +02:00
fwsize = fread(fwprog, 1, FWPROGSIZE, fwfile);
2019-05-30 12:44:56 +02:00
2019-05-22 18:39:46 +02:00
if (fwsize < 2) {
2019-06-19 21:41:22 +02:00
logerr("Program file too small or empty\n");
2019-06-18 12:58:26 +02:00
free(fwprog);
2019-05-22 18:39:46 +02:00
exit(-3);
}
2019-06-18 12:58:26 +02:00
fclose(fwfile);
//
// Register frame allocation
//
main_ctx.rf = calloc(NREGS, sizeof(ulong));
//
// Memory allocation
//
2019-06-07 22:23:38 +02:00
main_ctx.mp = malloc(MEMSIZE + 16);
main_ctx.mz = MEMSIZE;
2019-05-30 12:44:56 +02:00
2019-09-08 19:04:07 +02:00
main_ctx.rf[EIP] = MEMOFF;
2019-06-05 22:59:32 +02:00
2019-06-07 22:23:38 +02:00
if (main_ctx.mp == 0) {
2019-06-19 21:41:22 +02:00
logerr("Couldn't allocate RAM\n");
2019-06-18 12:58:26 +02:00
free(main_ctx.rf);
free(fwprog);
2019-06-07 22:23:38 +02:00
exit(-1);
2019-05-16 10:03:01 +02:00
}
2019-05-15 19:26:40 +02:00
2019-09-08 19:04:07 +02:00
memcpy(&main_ctx.mp[addr2real(main_ctx.rf[EIP])], fwprog, fwsize);
2019-05-16 21:42:23 +02:00
2019-06-18 12:58:26 +02:00
//
// Devices initialization
//
2019-06-07 22:23:38 +02:00
main_ctx.dh = 0;
2019-08-03 19:01:12 +02:00
if (devinitall() < 0) {
2019-06-19 21:41:22 +02:00
logerr("Couldn't initialize devices\n");
2019-06-18 12:58:26 +02:00
free(main_ctx.rf);
free(fwprog);
2019-06-07 22:23:38 +02:00
exit(-10);
2019-05-15 19:26:40 +02:00
}
2019-06-19 21:41:22 +02:00
// To be moved to some screen device
2019-08-03 19:01:12 +02:00
console_init();
2019-06-19 21:41:22 +02:00
2019-06-18 22:56:41 +02:00
main_loop();
2019-05-15 19:26:40 +02:00
}