about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2021-07-19 17:30:55 +0100
committerManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2021-07-19 17:30:55 +0100
commite60c3c684f87f32ba0500892ff85215faaaad0d5 (patch)
tree7707021822ebdf348d0dc6468d216ed33d207bff
parentMisc: Added the dbgprintf() macro (diff)
downloadCHIP8-Emulator-e60c3c684f87f32ba0500892ff85215faaaad0d5.tar.gz
CHIP8-Emulator-e60c3c684f87f32ba0500892ff85215faaaad0d5.tar.bz2
CHIP8-Emulator-e60c3c684f87f32ba0500892ff85215faaaad0d5.zip
Feature: Added timers thread
-rw-r--r--Makefile2
-rw-r--r--emulator.c21
-rw-r--r--emulator.h15
-rw-r--r--main.c3
4 files changed, 32 insertions, 9 deletions
diff --git a/Makefile b/Makefile
index 17f9717..2b88fe9 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 CC=egcc
-CFLAGS=-std=c99 -g -Wall -I.
+CFLAGS=-std=c99 -g -Wall -I. -lpthread
 
 all: chip8_emulator
 
diff --git a/emulator.c b/emulator.c
index 94cd3d8..ce607fe 100644
--- a/emulator.c
+++ b/emulator.c
@@ -28,6 +28,9 @@ int emulator_initialise(Emulator* emulator)
     memcpy(emulator->memory + FONT_LOAD_LOCATION, font, sizeof(font));
     emulator->is_on = 1;
 
+    pthread_t emulator_timers_thread_id;
+    pthread_create(&emulator_timers_thread_id, NULL, &emulator_timers_thread, emulator);
+
     return 0;
 }
 
@@ -133,6 +136,24 @@ int emulator_tick(Emulator* emulator)
     return 0;
 }
 
+void* emulator_timers_thread(Emulator* emulator)
+{
+    printf("STARTED TIMERS THREAD\n");
+
+    while(emulator->is_on)
+    {
+        if(emulator->delay_timer > 0)
+            --emulator->delay_timer;
+
+        if(emulator->sound_timer > 0)
+            --emulator->sound_timer;
+
+        usleep(1000000 / 60);
+    }
+
+    return NULL;
+}
+
 void emulator_dump_registers(Emulator* emulator)
 {
     printf("REGISTERS: \n");
diff --git a/emulator.h b/emulator.h
index 05cd3ef..cf1e81c 100644
--- a/emulator.h
+++ b/emulator.h
@@ -4,8 +4,10 @@
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/stat.h>
 #include <string.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <sys/stat.h>
 
 typedef struct
 {
@@ -41,13 +43,14 @@ typedef struct
 
 typedef struct
 {
-    uint8_t is_on;
-    uint16_t pc; //program counter
+    volatile uint8_t is_on;
+    volatile uint8_t delay_timer;
+    volatile uint8_t sound_timer;
+    uint8_t display[64][32];
     Registers regs;
     uint8_t sp; //stack pointer
     uint16_t stack[16];
-    volatile uint8_t delay_timer;
-    volatile uint8_t sound_timer;
+    uint16_t pc; //program counter
     uint8_t memory[4096];
 } Emulator;
 
@@ -56,4 +59,6 @@ int emulator_load_rom(Emulator* emulator, char* rom_name);
 int emulator_tick(Emulator* emulator);
 void emulator_dump_registers(Emulator* emulator);
 
+void* emulator_timers_thread();
+
 #endif
diff --git a/main.c b/main.c
index b97ab9f..a257c96 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,3 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <config.h>
 #include <emulator.h>
 
 int main(int argc, char** argv);