From e60c3c684f87f32ba0500892ff85215faaaad0d5 Mon Sep 17 00:00:00 2001 From: Manuel Palenzuela Date: Mon, 19 Jul 2021 17:30:55 +0100 Subject: Feature: Added timers thread --- Makefile | 2 +- emulator.c | 21 +++++++++++++++++++++ emulator.h | 15 ++++++++++----- main.c | 3 --- 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 #include #include -#include #include +#include +#include +#include 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 -#include -#include #include int main(int argc, char** argv); -- cgit 1.4.1