about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2021-07-23 00:31:39 +0100
committerManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2021-07-23 00:31:39 +0100
commitde1aea22e649c246edb7976b6bcce38a210e1851 (patch)
treed578125e533c5d2f94e8a5d267956dd45669b936
parentFeature: Implemented various instructions (diff)
downloadCHIP8-Emulator-de1aea22e649c246edb7976b6bcce38a210e1851.tar.gz
CHIP8-Emulator-de1aea22e649c246edb7976b6bcce38a210e1851.tar.bz2
CHIP8-Emulator-de1aea22e649c246edb7976b6bcce38a210e1851.zip
Feature: Added key detection
-rw-r--r--emulator.c71
-rw-r--r--emulator.h10
-rw-r--r--main.c4
-rw-r--r--roms/clock.ch8bin0 -> 280 bytes
-rw-r--r--roms/key_test.ch8bin0 -> 114 bytes
5 files changed, 83 insertions, 2 deletions
diff --git a/emulator.c b/emulator.c
index 1972cc4..8809687 100644
--- a/emulator.c
+++ b/emulator.c
@@ -29,6 +29,23 @@ int emulator_initialise(Emulator* emulator)
 
     memcpy(emulator->memory + FONT_LOAD_LOCATION, font, sizeof(font));
     emulator->is_on = 1;
+    emulator->keys[0].value = '1'; //1
+    emulator->keys[1].value = '2'; //2
+    emulator->keys[2].value = '3'; //3
+    emulator->keys[3].value = '4'; //4
+    emulator->keys[4].value = 'q'; //Q
+    emulator->keys[5].value = 'w'; //W
+    emulator->keys[6].value = 'e'; //E
+    emulator->keys[7].value = 'r'; //R
+    emulator->keys[8].value = 'a'; //A
+    emulator->keys[9].value = 's'; //S
+    emulator->keys[10].value = 's'; //D
+    emulator->keys[11].value = 'f'; //F
+    emulator->keys[12].value = 'z'; //Z
+    emulator->keys[13].value = 'x'; //X
+    emulator->keys[14].value = 'c'; //C
+    emulator->keys[15].value = 'v'; //V
+
 
     pthread_t emulator_timers_thread_id;
     pthread_create(&emulator_timers_thread_id, NULL, &emulator_timers_thread, emulator);
@@ -64,6 +81,38 @@ int emulator_load_rom(Emulator* emulator, char* rom_name)
     return 0;
 }
 
+int emulator_handle_key_press(Emulator* emulator, uint8_t key)
+{
+    for(size_t i = 0; i < (sizeof(emulator->keys) / sizeof(emulator->keys[0])); ++i)
+    {
+        if(emulator->keys[i].value == key)
+        {
+            printf("KEY ACTIVATED: %c-%d!\n", key, key);
+            //sleep(2);
+            emulator->keys[i].activated = 1;
+            break;
+        }
+    }
+
+    return 0;
+}
+
+int emulator_handle_key_release(Emulator* emulator, uint8_t key)
+{
+    for(size_t i = 0; i < (sizeof(emulator->keys) / sizeof(emulator->keys[0])); ++i)
+    {
+        if(emulator->keys[i].value == key)
+        {
+            printf("KEY RELEASE: %c-%d!\n", key, key);
+            //sleep(2);
+            emulator->keys[i].activated = 0;
+            break;
+        }
+    }
+
+    return 0;
+}
+
 int emulator_tick(Emulator* emulator)
 {
     uint16_t* pc = &emulator->pc;
@@ -274,6 +323,28 @@ int emulator_tick(Emulator* emulator)
                 dbgprintf("SET V[X] TO THE DELAY TIMER!\n");
                 emulator->regs.V[X] = emulator->delay_timer;
                 break;
+            case 0x0A:
+            {
+                uint8_t key_pressed = 0;
+                for(size_t i = 0; i < (sizeof(emulator->keys) / sizeof(emulator->keys[0])); ++i)
+                {
+                    if(emulator->keys[i].activated == 1)
+                    {
+                        emulator->regs.V[X] = i;
+                        key_pressed = 1;
+                        break;
+                    }
+
+                }
+
+                if(!key_pressed)
+                   *pc -= 2; //block (loop)
+            }
+                break;
+            case 0x29:
+                printf("TODO: Instr: 0x%x\n", instr);
+                assert(0);
+                break;
             default:
                 printf("DEFAULT: Instr: 0x%x\n", instr);
                 assert(0);
diff --git a/emulator.h b/emulator.h
index 489ad71..fa455d9 100644
--- a/emulator.h
+++ b/emulator.h
@@ -44,11 +44,18 @@ typedef struct
 
 typedef struct
 {
+    uint8_t activated;
+    uint8_t value;
+} Key;
+
+typedef struct
+{
     volatile uint8_t is_on;
     volatile uint8_t draw_flag;
     volatile uint8_t delay_timer;
     volatile uint8_t sound_timer;
     uint8_t display[64][32];
+    Key keys[16];
     Registers regs;
     uint8_t sp; //stack pointer
     uint16_t stack[16];
@@ -62,6 +69,9 @@ int emulator_tick(Emulator* emulator);
 void emulator_step(Emulator* emulator);
 void emulator_dump_registers(Emulator* emulator);
 
+int emulator_handle_key_press(Emulator* emulator, uint8_t key);
+int emulator_handle_key_release(Emulator* emulator, uint8_t key);
+
 void* emulator_timers_thread();
 
 #endif
diff --git a/main.c b/main.c
index d061a59..e0529b7 100644
--- a/main.c
+++ b/main.c
@@ -37,10 +37,10 @@ int main(int argc, char** argv)
                 goto exit;
             if(event.type == SDL_WINDOWEVENT)
                 SDL_RenderPresent(renderer);
-            /*if(event.type == SDL_KEYDOWN)
+            if(event.type == SDL_KEYDOWN)
                 emulator_handle_key_press(&emulator, event.key.keysym.sym);
             if(event.type == SDL_KEYUP)
-                emulator_handle_key_release(&emulator, event.key.keysym.sym);*/
+                emulator_handle_key_release(&emulator, event.key.keysym.sym);
         }
 
         emulator_tick(&emulator);
diff --git a/roms/clock.ch8 b/roms/clock.ch8
new file mode 100644
index 0000000..ec137bd
--- /dev/null
+++ b/roms/clock.ch8
Binary files differdiff --git a/roms/key_test.ch8 b/roms/key_test.ch8
new file mode 100644
index 0000000..c60558e
--- /dev/null
+++ b/roms/key_test.ch8
Binary files differ