+#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 <SDL2/SDL.h>
#include "font.h"
#include "processor.h"
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){
}
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;
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;
}
#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){
- //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;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_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){
}
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]);
}
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){
return;
}if(b1==0 && b2 == 0xEE){
ins_RET(ctx);
+ ctx->PC+=2;
return;
}
// Instruction: (3 is the instruction prefix, so n1 here)