about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2021-07-19 15:09:40 +0100
committerManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2021-07-19 15:09:40 +0100
commitfc3b7e70867d0a6ce89c139e4ddc0d66329fea11 (patch)
tree2f736f37c18817593cf4cd4c3ecf7adb975c6626
parentFeature: Added a font (diff)
downloadCHIP8-Emulator-fc3b7e70867d0a6ce89c139e4ddc0d66329fea11.tar.gz
CHIP8-Emulator-fc3b7e70867d0a6ce89c139e4ddc0d66329fea11.tar.bz2
CHIP8-Emulator-fc3b7e70867d0a6ce89c139e4ddc0d66329fea11.zip
Misc: Properly intialise the emulator
-rw-r--r--config.h2
-rw-r--r--emulator.c17
-rw-r--r--emulator.h3
-rw-r--r--main.c6
4 files changed, 20 insertions, 8 deletions
diff --git a/config.h b/config.h
index c70f65b..dc5778a 100644
--- a/config.h
+++ b/config.h
@@ -2,5 +2,7 @@
 #define _CONFIG_H_
 
 #define INSTRUCTIONS_PER_SECOND 700
+#define GAME_LOAD_LOCATION 0x200
+#define FONT_LOAD_LOCATION 0x50
 
 #endif
diff --git a/emulator.c b/emulator.c
index 1c02036..57fc3f3 100644
--- a/emulator.c
+++ b/emulator.c
@@ -2,7 +2,8 @@
 
 int emulator_initialise(Emulator* emulator)
 {
-    uint8_t font[] = {
+    const uint8_t font[] =
+    {
         0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
         0x20, 0x60, 0x20, 0x20, 0x70, // 1
         0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
@@ -19,11 +20,15 @@ int emulator_initialise(Emulator* emulator)
         0xE0, 0x90, 0x90, 0x90, 0xE0, // D
         0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
         0xF0, 0x80, 0xF0, 0x80, 0x80  // F
-                     };
+    };
+
+    //zero out the memory
+    memset(emulator, 0, sizeof(Emulator));
 
-        memcpy(emulator->memory + 0x50, font, sizeof(font));
+    memcpy(emulator->memory + FONT_LOAD_LOCATION, font, sizeof(font));
+    emulator->is_on = 1;
 
-        return 0;
+    return 0;
 }
 
 int emulator_load_rom(Emulator* emulator, char* rom_name)
@@ -40,13 +45,15 @@ int emulator_load_rom(Emulator* emulator, char* rom_name)
     fstat(fileno(rom), &st);
 
      //rom loaded after 0x200 into memory
-    int bytes_read = fread(emulator->memory + 0x200, 1, st.st_size, rom);
+    int bytes_read = fread(emulator->memory + GAME_LOAD_LOCATION, 1, st.st_size, rom);
     if(bytes_read != st.st_size)
     {
         perror("doesnt cuadrar\n");
         return 2;
     }
 
+    emulator->pc = GAME_LOAD_LOCATION;
+
     return 0;
 }
 
diff --git a/emulator.h b/emulator.h
index cfbbeff..50fa9de 100644
--- a/emulator.h
+++ b/emulator.h
@@ -2,6 +2,7 @@
 #define _EMULATOR_H_
 
 #include <stdio.h>
+#include <config.h>
 #include <sys/stat.h>
 #include <string.h>
 
@@ -35,6 +36,8 @@ typedef struct
     Registers regs;
     uint8_t sp; //stack pointer
     uint16_t stack[16];
+    volatile uint8_t delay_timer;
+    volatile uint8_t sound_timer;
     uint8_t memory[4096];
 } Emulator;
 
diff --git a/main.c b/main.c
index 7c5a2fc..94e4a95 100644
--- a/main.c
+++ b/main.c
@@ -23,14 +23,14 @@ int main(int argc, char** argv)
 
     printf("Hello brother!\n");
 
-    for(int i = 0; i < sizeof(emulator.memory); ++i)
+    /*for(int i = 0; i < sizeof(emulator.memory); ++i)
     {
         printf("0x%x ", emulator.memory[i]);
     }
 
-    putchar('\n');
+    putchar('\n');*/
 
-    while(emulator.is_on)
+    while(emulator.is_on == 1)
     {
         emulator_tick(&emulator);
         usleep(1000000 / INSTRUCTIONS_PER_SECOND);