1 #define _POSIX_C_SOURCE 199309L 2 3 #include "6502.h" 4 #include "tui.h" 5 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <stdbool.h> 9 #include <signal.h> 10 #include <unistd.h> 11 #include <time.h> 12 13 #define usleep(t) do { \ 14 nanosleep((const struct timespec[]) \ 15 {{t / 1000000, (t % 1000000) * 1000L}}, NULL); \ 16 } while(0) 17 18 #define INPUT_FILE_PATH "6502_functional_test.bin" 19 20 /* TODO: Add support for command line arguments */ 21 /* TODO: Add proper system monitor (memory dump, cpu registers) */ 22 /* TODO: Tick based execution to properly emulate clock cycles */ 23 /* TODO: Move `CPU_dump` to tui */ 24 25 static bool brk = false; 26 uint32_t t_us_delay = 50000; 27 28 void CPU_brk(uint16_t pc) { 29 if (CPU_get_pc() == pc) { 30 printf("BRK: %2X reached\n", pc); 31 brk = true; 32 } 33 } 34 35 void sig_handler(int signo) { 36 if ((signo == SIGINT) || (signo == SIGQUIT)) { 37 brk = true; 38 } 39 } 40 41 int main(void) { 42 INS ins; 43 44 /* Initialize memory to 0 */ 45 MEM_init(); 46 47 /* Load program to memory */ 48 MEM_load_from_file(INPUT_FILE_PATH); 49 50 /* set the first address that the pc should be set to */ 51 MEM_set_pc_start(0x0400); 52 53 /* Initialize cpu registers to 0 */ 54 CPU_init(); 55 56 /* initialize registers from memmory */ 57 CPU_reset(); 58 59 set_input_mode(); 60 signal(SIGINT, sig_handler); 61 signal(SIGQUIT, sig_handler); 62 atexit(reset_input_mode); 63 64 do { 65 /* check if breakpoint reached */ 66 CPU_brk(0x336d); 67 68 /* update tui */ 69 CPU_dump(); 70 71 /* fetch execute instruction */ 72 CPU_fetch(&ins); 73 CPU_exec(ins); 74 75 usleep(t_us_delay); 76 } while(!brk); 77 78 reset_input_mode(); 79 return 0; 80 }
