6502

minimal 6502 emulator written in C
Index Commits Files Refs README LICENSE
main.c (1391B)
   1 #include "6502.h"
   2 #include "tui.h"
   3 
   4 #include <stdio.h>
   5 #include <stdlib.h>
   6 #include <stdbool.h>
   7 #include <signal.h>
   8 
   9 #include <unistd.h>
  10 
  11 #include <time.h>
  12 #define nsleep(t) nanosleep((const struct timespec[]){{0, t * 1000L}}, NULL)
  13 
  14 #define INPUT_FILE_PATH    "6502_functional_test.bin"
  15 
  16 /* TODO: Add support for command line arguments */
  17 /* TODO: Add proper system monitor (memory dump, cpu registers) */
  18 
  19 static bool brk = false;
  20 
  21 void CPU_brk(uint16_t pc) {
  22     if (CPU_get_pc () == pc) {
  23         printf("BRK: %2X reached\n", pc);
  24         brk = true;
  25     }
  26 }
  27 
  28 void sig_handler(int signo) {
  29     if (signo == SIGINT) brk = true;
  30     if (signo == SIGQUIT) brk = true;
  31 }
  32 
  33 int main(void) {
  34     INS ins;
  35 
  36     /* Initialize memory to 0 */
  37     MEM_init();
  38 
  39     /* Load program to memory */
  40     if(MEM_load_from_file(INPUT_FILE_PATH))
  41         return 1;
  42 
  43     /* set the first address that the pc should be set to */
  44     MEM_set_pc_start(0x0400);
  45 
  46     /* Initialize cpu registers to 0 */
  47     CPU_init();
  48 
  49     /* initialize registers from memmory */
  50     CPU_reset();
  51 
  52     set_input_mode();
  53     signal(SIGINT, sig_handler);
  54     signal(SIGQUIT, sig_handler);
  55     atexit(reset_input_mode);
  56 
  57     do {
  58         /* set break point at pc 0x336d */
  59         CPU_brk(0x336d);
  60 
  61         CPU_fetch(&ins);
  62         CPU_dump();
  63 
  64         if(CPU_exec(ins) != 0) {
  65             /* exit if invalid instruction */
  66             char c;
  67             read(STDIN_FILENO, &c, 1);
  68             brk = true;
  69         }
  70 
  71         nsleep(1000);
  72     } while(!brk);
  73 
  74     reset_input_mode();
  75     return 0;
  76 }