]> git.ayabusa.dev Git - chipy-8.git/commitdiff
Added some stuff main
authorayabusa <lebgpub@gmail.com>
Sun, 11 Jan 2026 16:23:26 +0000 (17:23 +0100)
committerayabusa <lebgpub@gmail.com>
Sun, 11 Jan 2026 16:23:26 +0000 (17:23 +0100)
Makefile
src/main.c
src/processor.c
src/processor.h

index c393e7b21888ad1a438bc0216b0078fbcb5a8788..ae10134d06c36917ffaca124e91ca03273228128 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,9 @@
 flags = -g -Wall -Wextra -fsanitize=address
 flags = -g -Wall -Wextra -fsanitize=address
+sdl_flag = -I/usr/include/SDL2 -D_REENTRANT -lSDL2
 src = src/*.c
 inc = src/
 out = build/chipy8
 src = src/*.c
 inc = src/
 out = build/chipy8
-rom = test.ch8
+rom = display.ch8
 
 all: clean comp run
 
 
 all: clean comp run
 
@@ -10,7 +11,7 @@ clean:
        rm -f $(out)
 
 comp:
        rm -f $(out)
 
 comp:
-       $(CC) $(src) -I $(inc) -o $(out) $(flags)
+       $(CC) $(src) -I $(inc) -o $(out) $(flags) $(sdl_flag)
 
 run:
        ./$(out) $(rom)
 
 run:
        ./$(out) $(rom)
index 2620ad0de1e4a7911c44327111fa0138f0adeda4..96071ce852c87fcac4a9a338934672840ca11643 100644 (file)
@@ -1,8 +1,13 @@
+#include <SDL2/SDL_render.h>
+#include <SDL2/SDL_scancode.h>
+#include <SDL2/SDL_stdinc.h>
+#include <SDL2/SDL_timer.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
 #include <time.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
 #include <time.h>
+#include <SDL2/SDL.h>
 #include "font.h"
 #include "processor.h"
 
 #include "font.h"
 #include "processor.h"
 
@@ -29,6 +34,76 @@ int init(ch8_ctx * context, char * rom_name){
        return 0;
 }
 
        return 0;
 }
 
+void update_keyboard(ch8_ctx * ctx, const Uint8 * keystate){
+    ctx->KEYBOARD[0] = keystate[SDL_SCANCODE_KP_0];
+    ctx->KEYBOARD[1] = keystate[SDL_SCANCODE_KP_1];
+    ctx->KEYBOARD[2] = keystate[SDL_SCANCODE_KP_2];
+    ctx->KEYBOARD[3] = keystate[SDL_SCANCODE_KP_3];
+    ctx->KEYBOARD[4] = keystate[SDL_SCANCODE_KP_4];
+    ctx->KEYBOARD[5] = keystate[SDL_SCANCODE_KP_5];
+    ctx->KEYBOARD[6] = keystate[SDL_SCANCODE_KP_6];
+    ctx->KEYBOARD[7] = keystate[SDL_SCANCODE_KP_7];
+    ctx->KEYBOARD[8] = keystate[SDL_SCANCODE_KP_8];
+    ctx->KEYBOARD[9] = keystate[SDL_SCANCODE_KP_9];
+    ctx->KEYBOARD[10] = keystate[SDL_SCANCODE_Q];
+    ctx->KEYBOARD[11] = keystate[SDL_SCANCODE_W];
+    ctx->KEYBOARD[12] = keystate[SDL_SCANCODE_E];
+    ctx->KEYBOARD[13] = keystate[SDL_SCANCODE_R];
+    ctx->KEYBOARD[14] = keystate[SDL_SCANCODE_T];
+    ctx->KEYBOARD[15] = keystate[SDL_SCANCODE_Y];
+}
+
+#define SDL_scale 16
+int launch_SDL_game(ch8_ctx * ctx){
+    SDL_Window *window = NULL;
+    SDL_Renderer *renderer = NULL;
+    SDL_Event event;
+    int running = 1;
+    SDL_Color black={4,26,10,255}, white={168,255,232,255};
+    if(0 != SDL_Init(SDL_INIT_VIDEO)){
+        printf("Failed to init SDL D:\n");
+        return 1;
+    }
+    window = SDL_CreateWindow("Chipy 8 Emulator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
+                                  64*SDL_scale, 32*SDL_scale, SDL_WINDOW_SHOWN);
+    if(NULL == window)
+    {
+        fprintf(stderr, "Erreur SDL_CreateWindow : %s", SDL_GetError());
+        return 1;
+    }
+    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
+    if(NULL == renderer)
+    {
+        fprintf(stderr, "Erreur SDL_CreateRenderer : %s", SDL_GetError());
+        return 1;
+    }
+    while(running){
+        while(SDL_PollEvent(&event))
+                if(event.type == SDL_QUIT)
+                    running = 0;
+        SDL_SetRenderDrawColor(renderer, black.r, black.g, black.b, black.a);
+        SDL_RenderClear(renderer);
+        SDL_SetRenderDrawColor(renderer, white.r, white.g, white.b, white.a);
+        // Draw the screen from memory
+        for (int y=0; y<32; y++){
+            for(uint64_t x=0; x<64; x++){
+                if(ctx->SCREEN[y] & power(2, 63-x)){
+                    SDL_Rect rect = {x*SDL_scale+SDL_scale, y*SDL_scale, SDL_scale, SDL_scale};
+                    SDL_RenderFillRect(renderer, &rect);
+                }
+            }
+        }
+        SDL_RenderPresent(renderer);
+        const Uint8* keystate = SDL_GetKeyboardState( NULL );
+        update_keyboard(ctx, keystate);
+        execute_instruction(ctx);
+        printf("0x%hx\n", ctx->PC);
+        //print_ctx(ctx);
+        //SDL_Delay(10);
+    }
+    SDL_Quit();
+    return 0;
+}
 
 int main(int argc, char * argv[]){
        if (argc!=2){
 
 int main(int argc, char * argv[]){
        if (argc!=2){
@@ -37,7 +112,7 @@ int main(int argc, char * argv[]){
        }
        printf("Welcome to the Chipy 8 emulator!\n");
 
        }
        printf("Welcome to the Chipy 8 emulator!\n");
 
-       ch8_ctx context = {NULL, {0}, 0, 0, 0, 0x200, 0, {0}};
+       ch8_ctx context = {NULL, {0}, 0, 0, 0, 0x200, 0, {0}, {0}};
        if(init(&context, argv[1])){ // If error in init
                free(context.RAM);
                return 1;
        if(init(&context, argv[1])){ // If error in init
                free(context.RAM);
                return 1;
@@ -45,12 +120,15 @@ int main(int argc, char * argv[]){
        int running = 1;
        srand( time( NULL ) );
        char placeholder;
        int running = 1;
        srand( time( NULL ) );
        char placeholder;
+       launch_SDL_game(&context);
+       /*
        while (running){
            execute_instruction(&context);
                print_ctx(&context);
                printf("Press enter to resume");
                scanf("%c", &placeholder);
        }
        while (running){
            execute_instruction(&context);
                print_ctx(&context);
                printf("Press enter to resume");
                scanf("%c", &placeholder);
        }
+       */
        free(context.RAM);
        return 0;
 }
        free(context.RAM);
        return 0;
 }
index 980c53eecd56825614abc63876b2111955a2ba80..d15cd02947c60ce2b32106ca33c3ddf38a2a3281 100644 (file)
@@ -3,12 +3,30 @@
 #include <stdlib.h>
 #include "processor.h"
 
 #include <stdlib.h>
 #include "processor.h"
 
+
+uint64_t power(uint64_t x, uint64_t y){
+    if (y<=0 || y>=200)
+        return 1;
+    return power(x, y-1)*x;
+}
+
 void ins_CLS(ch8_ctx * ctx){
 void ins_CLS(ch8_ctx * ctx){
-    //TODO
+    for (int i=0; i<32; i++)
+        ctx->SCREEN[i]=0;
 }
 
 void ins_DRW(ch8_ctx * ctx, uint8_t x, uint8_t y, uint8_t n){
 }
 
 void ins_DRW(ch8_ctx * ctx, uint8_t x, uint8_t y, uint8_t n){
-    //TODO
+    uint8_t collision = 0;
+    for(int i=0;i<n;i++){
+        if(i+ctx->REGS[y]>=32)
+            break;
+        uint64_t bak = ctx->SCREEN[ctx->REGS[y]+i];
+        ctx->SCREEN[ctx->REGS[y]+i] ^= ctx->RAM[ctx->I + i] * power(2, 60-ctx->REGS[x]);
+        if (bak != ctx->SCREEN[ctx->REGS[y]+i]){
+            collision=1;
+        }
+    }
+    ctx->REGS[0xF] = collision;
 }
 
 void ins_JP(ch8_ctx * ctx, uint16_t nnn){
 }
 
 void ins_JP(ch8_ctx * ctx, uint16_t nnn){
@@ -109,15 +127,27 @@ void ins_RND(ch8_ctx * ctx, uint8_t x, uint8_t kk){
 }
 
 void ins_SKP(ch8_ctx * ctx, uint8_t x){
 }
 
 void ins_SKP(ch8_ctx * ctx, uint8_t x){
-    //TODO
+    if (ctx->REGS[x]>15)
+        return;
+    if (ctx->KEYBOARD[ctx->REGS[x]])
+        ctx->PC+=2;
 }
 
 void ins_SKNP(ch8_ctx * ctx, uint8_t x){
 }
 
 void ins_SKNP(ch8_ctx * ctx, uint8_t x){
-    //TODO
+    if (ctx->REGS[x]>15)
+        return;
+    if (!ctx->KEYBOARD[ctx->REGS[x]])
+        ctx->PC+=2;
 }
 
 void ins_LDK(ch8_ctx * ctx, uint8_t x){
 }
 
 void ins_LDK(ch8_ctx * ctx, uint8_t x){
-    //TODO
+    for(int i=0; i<16; i++){
+        if (ctx->KEYBOARD[i]){
+            ctx->REGS[x] = i;
+            return;
+        }
+    }
+    ctx->PC-=2;
 }
 
 void ins_LD_V_DT(ch8_ctx * ctx, uint8_t x){
 }
 
 void ins_LD_V_DT(ch8_ctx * ctx, uint8_t x){
@@ -160,7 +190,7 @@ void ins_LD_V_I(ch8_ctx * ctx, uint8_t x){
 }
 
 void print_ctx(ch8_ctx * ctx){
 }
 
 void print_ctx(ch8_ctx * ctx){
-    printf("PC: %hu \nSP: %hhu \nDT: %hhu \nST: %hhu \nRegisters:\nI: %hu\n", ctx->PC, ctx->SP, ctx->DT, ctx->ST, ctx->I);
+    printf("=====ctx=====\nPC: 0x%hx \nSP: %hhu \nDT: %hhu \nST: %hhu \nRegisters:\nI: %hu\n", ctx->PC, ctx->SP, ctx->DT, ctx->ST, ctx->I);
     for(uint8_t x=0; x<=0xF; x++){
         printf("V%hhu: %hhu\n", x, ctx->REGS[x]);
     }
     for(uint8_t x=0; x<=0xF; x++){
         printf("V%hhu: %hhu\n", x, ctx->REGS[x]);
     }
@@ -173,6 +203,11 @@ void print_ctx(ch8_ctx * ctx){
             printf("0x%x: 0x%hx\n", s, ctx->STACK[s]);
         }
     }
             printf("0x%x: 0x%hx\n", s, ctx->STACK[s]);
         }
     }
+    printf("Keyboard\n");
+    for(int k=0; k<16; k++){
+        printf("Key %x state: %hhu\n", k, ctx->KEYBOARD[k]);
+    }
+    printf("=============\n");
 }
 
 void execute_instruction(ch8_ctx * ctx){
 }
 
 void execute_instruction(ch8_ctx * ctx){
@@ -184,6 +219,7 @@ void execute_instruction(ch8_ctx * ctx){
                return;
        }if(b1==0 && b2 == 0xEE){
                ins_RET(ctx);
                return;
        }if(b1==0 && b2 == 0xEE){
                ins_RET(ctx);
+               ctx->PC+=2;
                return;
        }
        // Instruction: (3 is the instruction prefix, so n1 here)
                return;
        }
        // Instruction: (3 is the instruction prefix, so n1 here)
index d214f58d5d5d89b3dfdef25d503bbdb794535a3a..da2d3c46dd67f774e76ceda8c17bc4f7731f2c5b 100644 (file)
@@ -2,6 +2,7 @@
 #define PROCESSOR_H
 
 #include "stdint.h"
 #define PROCESSOR_H
 
 #include "stdint.h"
+#include <stdint.h>
 
 typedef struct{
 // The ram which store the ROM, DATA and some interpreter stuff
 
 typedef struct{
 // The ram which store the ROM, DATA and some interpreter stuff
@@ -23,8 +24,16 @@ uint16_t PC;
 // Stack and stack pointer
 uint8_t SP;
 uint16_t STACK[16];
 // Stack and stack pointer
 uint8_t SP;
 uint16_t STACK[16];
+
+// The screen matrix
+uint64_t SCREEN[32];
+
+// The keyboard state
+uint8_t KEYBOARD[16];
 } ch8_ctx;
 
 } ch8_ctx;
 
+uint64_t power(uint64_t x, uint64_t y);
+
 void execute_instruction(ch8_ctx * ctx);
 
 void print_ctx(ch8_ctx * ctx);
 void execute_instruction(ch8_ctx * ctx);
 
 void print_ctx(ch8_ctx * ctx);