about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2021-07-19 23:43:08 +0100
committerManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2021-07-19 23:43:08 +0100
commitfd3aac5d52d468be2e971fcaf00882d9ef6a89fd (patch)
tree4847a7302105b5f2e0d2bf703507936e7da55d1c
parentDebug: Added draw debug prints (diff)
downloadCHIP8-Emulator-fd3aac5d52d468be2e971fcaf00882d9ef6a89fd.tar.gz
CHIP8-Emulator-fd3aac5d52d468be2e971fcaf00882d9ef6a89fd.tar.bz2
CHIP8-Emulator-fd3aac5d52d468be2e971fcaf00882d9ef6a89fd.zip
Feature: Added draw instruction implementation
-rw-r--r--config.h2
-rw-r--r--emulator.c40
-rw-r--r--main.c17
3 files changed, 21 insertions, 38 deletions
diff --git a/config.h b/config.h
index cf73451..a40e76f 100644
--- a/config.h
+++ b/config.h
@@ -1,7 +1,7 @@
 #ifndef _CONFIG_H_
 #define _CONFIG_H_
 
-#define DEBUG 0
+#define DEBUG 1
 
 #define INSTRUCTIONS_PER_SECOND 700
 #define GAME_LOAD_LOCATION 0x200
diff --git a/emulator.c b/emulator.c
index 15c5e39..082cbfb 100644
--- a/emulator.c
+++ b/emulator.c
@@ -62,20 +62,6 @@ int emulator_load_rom(Emulator* emulator, char* rom_name)
     return 0;
 }
 
-static void show_screen(Emulator* em)
-{
-    printf("SCREEN: \n");
-    for(int x = 0; x < 64; ++x)
-    {
-        for(int y = 0; y < 32; ++y)
-        {
-            printf("%d ", em->display[x][y]);
-        }
-        printf("\n");
-    }
-    printf("\n");
-}
-
 int emulator_tick(Emulator* emulator)
 {
     uint16_t* pc = &emulator->pc;
@@ -138,30 +124,24 @@ int emulator_tick(Emulator* emulator)
             break;
         case 0xD:
             dbgprintf("DRAW!\n");
-            //show_screen(emulator);
             int x = emulator->regs.V[X] % 64;
             int y = emulator->regs.V[Y] % 32;
-            printf("DRAWING 0x%x AT (%d, %d)\n", emulator->regs.I, x, y);
-
 
             emulator->regs.VF = 0;
 
             for(int row = 0; row < N; ++row)
             {
-                uint8_t pixel = emulator->memory[emulator->regs.I + row];
-                for(int xline = 0; xline < 8; xline++)
-                                {
-                                    if((pixel & (0x80 >> xline)) != 0)
-                                    {
-                                        if(emulator->display[x][y] == 1)
-                                        {
-                                            emulator->regs.V[0xF] = 1;
-                                        }
-                                        emulator->display[x][y] ^= 1;
-                                    }
-                                }
+                uint8_t pixels = emulator->memory[emulator->regs.I + row];
+                for(int offset = 0; offset < 8; ++offset)
+                {
+                    if((pixels & (0x80 >> offset)) != 0)
+                    {
+                        if(emulator->display[x + offset][y + row] == 1)
+                          emulator->regs.V[0xF] = 1;
+                        emulator->display[x + offset][y + row] ^= 1;
+                    }
+                }
             }
-            //show_screen(emulator);
 
             emulator->draw_flag = 1;
             break;
diff --git a/main.c b/main.c
index 3e4773d..79ec96c 100644
--- a/main.c
+++ b/main.c
@@ -28,13 +28,6 @@ int main(int argc, char** argv)
     printf("Hello brother!\n");
 
     uint32_t pixels[32 * 64];
-    for(int w = 0; w < 64; ++w)
-        for(int h = 0; h < 32; ++h)
-        {
-            uint8_t pixel = emulator.display[w][h];
-            pixels[64 * h + w] = (0x00FFFFFF * pixel) | 0xFF000000;
-        }
-
     SDL_Event event;
     while(emulator.is_on == 1)
     {
@@ -48,6 +41,16 @@ int main(int argc, char** argv)
 
         if(emulator.draw_flag == 1)
         {
+            //update pixel buffer with the emulator's display pixels
+            for(int w = 0; w < 64; ++w)
+            {
+                for(int h = 0; h < 32; ++h)
+                {
+                    uint8_t pixel = emulator.display[w][h];
+                    pixels[64 * h + w] = (0x00FFFFFF * pixel) | 0xFF000000;
+                }
+            }
+
             SDL_UpdateTexture(texture, NULL, pixels, sizeof(uint32_t) * 64);
             SDL_RenderClear(renderer);
             SDL_RenderCopy(renderer, texture, NULL, NULL);