From e6ea9ea94f6495313e0456a7d1d6309b87f4334e Mon Sep 17 00:00:00 2001 From: ayabusa Date: Sun, 11 Jan 2026 17:23:26 +0100 Subject: [PATCH] Added some stuff --- Makefile | 5 ++-- src/main.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++- src/processor.c | 48 +++++++++++++++++++++++++---- src/processor.h | 9 ++++++ 4 files changed, 133 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index c393e7b..ae10134 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,9 @@ flags = -g -Wall -Wextra -fsanitize=address +sdl_flag = -I/usr/include/SDL2 -D_REENTRANT -lSDL2 src = src/*.c inc = src/ out = build/chipy8 -rom = test.ch8 +rom = display.ch8 all: clean comp run @@ -10,7 +11,7 @@ clean: rm -f $(out) comp: - $(CC) $(src) -I $(inc) -o $(out) $(flags) + $(CC) $(src) -I $(inc) -o $(out) $(flags) $(sdl_flag) run: ./$(out) $(rom) diff --git a/src/main.c b/src/main.c index 2620ad0..96071ce 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,13 @@ +#include +#include +#include +#include #include #include #include #include #include +#include #include "font.h" #include "processor.h" @@ -29,6 +34,76 @@ int init(ch8_ctx * context, char * rom_name){ 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){ @@ -37,7 +112,7 @@ int main(int argc, char * argv[]){ } 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; @@ -45,12 +120,15 @@ int main(int argc, char * argv[]){ 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); } + */ free(context.RAM); return 0; } diff --git a/src/processor.c b/src/processor.c index 980c53e..d15cd02 100644 --- a/src/processor.c +++ b/src/processor.c @@ -3,12 +3,30 @@ #include #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){ - //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){ - //TODO + uint8_t collision = 0; + for(int i=0;iREGS[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){ @@ -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){ - //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){ - //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){ - //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){ @@ -160,7 +190,7 @@ void ins_LD_V_I(ch8_ctx * ctx, uint8_t x){ } 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]); } @@ -173,6 +203,11 @@ void print_ctx(ch8_ctx * ctx){ 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){ @@ -184,6 +219,7 @@ void execute_instruction(ch8_ctx * ctx){ return; }if(b1==0 && b2 == 0xEE){ ins_RET(ctx); + ctx->PC+=2; return; } // Instruction: (3 is the instruction prefix, so n1 here) diff --git a/src/processor.h b/src/processor.h index d214f58..da2d3c4 100644 --- a/src/processor.h +++ b/src/processor.h @@ -2,6 +2,7 @@ #define PROCESSOR_H #include "stdint.h" +#include 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]; + +// The screen matrix +uint64_t SCREEN[32]; + +// The keyboard state +uint8_t KEYBOARD[16]; } ch8_ctx; +uint64_t power(uint64_t x, uint64_t y); + void execute_instruction(ch8_ctx * ctx); void print_ctx(ch8_ctx * ctx); -- 2.43.0