repos/6502

minimal 6502 cpu emulator
Commits Files Refs README LICENSE
commit ca3c2f0f1666282ff9d4bb42d9bb935035f2d4ca
parent 87b9a3eb0c956a70efaff38862222f5b088285f7
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date:   Sun, 12 Oct 2025 19:07:05 -0300

do not exit if file `6502_functional_test.bin` not found

run cpu with all zeroes in memory anyway

Diffstat:
Mmain.c | 27++++++++++++---------------
1 file changed, 12 insertions(+), 15 deletions(-)
diff --git a/main.c b/main.c
@@ -7,9 +7,7 @@
 #include <stdlib.h>
 #include <stdbool.h>
 #include <signal.h>
-
 #include <unistd.h>
-
 #include <time.h>
 
 #define usleep(t) do {                                     \
@@ -21,20 +19,23 @@
 
 /* TODO: Add support for command line arguments */
 /* TODO: Add proper system monitor (memory dump, cpu registers) */
+/* TODO: Tick based execution to properly emulate clock cycles */
+/* TODO: Move `CPU_dump` to tui */
 
 static bool brk = false;
 uint32_t t_us_delay = 50000;
 
 void CPU_brk(uint16_t pc) {
-    if (CPU_get_pc () == pc) {
+    if (CPU_get_pc() == pc) {
         printf("BRK: %2X reached\n", pc);
         brk = true;
     }
 }
 
 void sig_handler(int signo) {
-    if (signo == SIGINT) brk = true;
-    if (signo == SIGQUIT) brk = true;
+    if ((signo == SIGINT) || (signo == SIGQUIT)) {
+        brk = true;
+    }
 }
 
 int main(void) {
@@ -44,8 +45,7 @@ int main(void) {
     MEM_init();
 
     /* Load program to memory */
-    if(MEM_load_from_file(INPUT_FILE_PATH))
-        return 1;
+    MEM_load_from_file(INPUT_FILE_PATH);
 
     /* set the first address that the pc should be set to */
     MEM_set_pc_start(0x0400);
@@ -62,18 +62,15 @@ int main(void) {
     atexit(reset_input_mode);
 
     do {
-        /* set break point at pc 0x336d */
+        /* check if breakpoint reached */
         CPU_brk(0x336d);
 
-        CPU_fetch(&ins);
+        /* update tui */
         CPU_dump();
 
-        if(CPU_exec(ins) != 0) {
-            /* exit if invalid instruction */
-            char c;
-            read(STDIN_FILENO, &c, 1);
-            brk = true;
-        }
+        /* fetch execute instruction */
+        CPU_fetch(&ins);
+        CPU_exec(ins);
 
         usleep(t_us_delay);
     } while(!brk);