about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2021-07-24 20:58:06 +0100
committerManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2021-07-24 20:58:06 +0100
commit5d8e1534937fefdf557dbde091a4bbfb34d603f1 (patch)
tree85b5cf473bd0209ad15762760ec5de6594d4d6ac
parentFeature: Implemented various instructions and added a few roms (diff)
downloadCHIP8-Emulator-5d8e1534937fefdf557dbde091a4bbfb34d603f1.tar.gz
CHIP8-Emulator-5d8e1534937fefdf557dbde091a4bbfb34d603f1.tar.bz2
CHIP8-Emulator-5d8e1534937fefdf557dbde091a4bbfb34d603f1.zip
Misc: Added the timer thread variable to the emulator struct
This way we can keep track of the thread to join it on
emulator_deinitialise()
-rw-r--r--config.h1
-rw-r--r--emulator.c19
-rw-r--r--emulator.h2
-rw-r--r--main.c10
4 files changed, 24 insertions, 8 deletions
diff --git a/config.h b/config.h
index b318965..38d0ce7 100644
--- a/config.h
+++ b/config.h
@@ -2,6 +2,7 @@
 #define _CONFIG_H_
 
 #define DEBUG 1
+//#define STOP_ON_INFINITE_LOOP
 
 #define SCREEN_WIDTH 852
 #define SCREEN_HEIGHT 480
diff --git a/emulator.c b/emulator.c
index 615c4fe..8fed056 100644
--- a/emulator.c
+++ b/emulator.c
@@ -48,8 +48,17 @@ int emulator_initialise(Emulator* emulator)
     emulator->keys[14].value = 'f'; //E
     emulator->keys[15].value = 'v'; //F
 
-    pthread_t emulator_timers_thread_id;
-    pthread_create(&emulator_timers_thread_id, NULL, &emulator_timers_thread, emulator);
+    pthread_create(&emulator->timers_thread, NULL, &emulator_timers_thread, emulator);
+
+    return 0;
+}
+
+int emulator_deinitialise(Emulator* emulator)
+{
+    //join timers thread
+    emulator->is_on = 0;
+
+    pthread_join(emulator->timers_thread, NULL);
 
     return 0;
 }
@@ -155,14 +164,16 @@ int emulator_tick(Emulator* emulator)
             dbgprintf("JUMP! (0x%x)\n", NNN);
             emulator->pc = NNN;
 
+#ifdef STOP_ON_INFINITE_LOOP
             if(NNN == instr_pc) //infinite loop
             {
                 printf("INFINITE LOOP DETECTED! EXITING...\n");
                 emulator_dump_registers(emulator);
-                getchar(); //block
+                sleep(2);
+                //getchar(); //block
                 emulator->is_on = 0;
             }
-
+#endif
             break;
         case 0x2:
             dbgprintf("CALL SUBROUTINE! (0x%x)\n", NNN);
diff --git a/emulator.h b/emulator.h
index fa455d9..2df3665 100644
--- a/emulator.h
+++ b/emulator.h
@@ -54,6 +54,7 @@ typedef struct
     volatile uint8_t draw_flag;
     volatile uint8_t delay_timer;
     volatile uint8_t sound_timer;
+    pthread_t timers_thread;
     uint8_t display[64][32];
     Key keys[16];
     Registers regs;
@@ -64,6 +65,7 @@ typedef struct
 } Emulator;
 
 int emulator_initialise(Emulator* emulator);
+int emulator_deinitialise(Emulator* emulator);
 int emulator_load_rom(Emulator* emulator, char* rom_name);
 int emulator_tick(Emulator* emulator);
 void emulator_step(Emulator* emulator);
diff --git a/main.c b/main.c
index e6f6cf1..2ce1656 100644
--- a/main.c
+++ b/main.c
@@ -12,7 +12,6 @@ int main(int argc, char** argv)
         return 1;
     }
 
-
     SDL_Init(SDL_INIT_VIDEO);
     SDL_Window * window = SDL_CreateWindow("CHIP8 Emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
     SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
@@ -20,9 +19,9 @@ int main(int argc, char** argv)
 
     Emulator emulator;
 
+    load:
     emulator_initialise(&emulator);
 
-    load:
     if(emulator_load_rom(&emulator, argv[1]) != 0)
         return 2;
 
@@ -30,7 +29,7 @@ int main(int argc, char** argv)
 
     uint32_t pixels[32 * 64];
     SDL_Event event;
-    while(emulator.is_on == 1)
+    while(emulator.is_on)
     {
         while(SDL_PollEvent(&event))
         {
@@ -45,7 +44,8 @@ int main(int argc, char** argv)
                     case SDLK_ESCAPE:
                         goto exit;
                     case SDLK_F5:
-                        goto load;
+                        emulator_deinitialise(&emulator);
+                        goto load; //join thread or smth??
                     default:
                         emulator_handle_key_press(&emulator, event.key.keysym.sym);
                 }
@@ -86,6 +86,8 @@ int main(int argc, char** argv)
     SDL_DestroyWindow(window);
     SDL_Quit();
 
+    emulator_deinitialise(&emulator);
+
     return 0;
 }