chip8

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.vgx.fr/chip8
Log | Files | Refs

commit fbf1de0925e14397eae524abba98913bea52903b
parent f5a1325664fa0b7dda67a7273c5ec3c33654963d
Author: Léo Villeveygoux <leo.villeveygoux@u-bordeaux.fr>
Date:   Sun,  6 Jan 2019 19:43:18 +0100

Fix keys, frequency, add draw asserts

Diffstat:
Mchip8.c | 27+++++++++++++++------------
Mmedia-sdl.c | 29+++++++++++++++++------------
2 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/chip8.c b/chip8.c @@ -1,10 +1,13 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <time.h> #include <unistd.h> #include "media.h" +#define FREQ 840 + unsigned char RAM[0x1000]; unsigned char V[16]; @@ -335,22 +338,17 @@ void step(){ } void display(){ - printf("\033[H"); for(int y=0 ; y<32 ; y++){ - for(int xc=0 ; xc<8 ; xc++){ - for(int xb=7 ; xb>=0 ; xb--){ - draw(xc*8 + 7-xb, y, SCREEN[y*8+xc]&1<<xb ? 1:0); - } + for(int x=0 ; x<64 ; x++){ + draw(x, y, SCREEN[y*8+x/8]&1<<(7-x%8) ? 1:0); } } - send_draw(); } int main(int argc, char **argv){ /* Set up char sprites */ - for(int i=0 ; i<sizeof(char_sprites) ; i++) - RAM[CHAR_SPRITES_OFFSET+i] = char_sprites[i]; + memcpy(RAM+CHAR_SPRITES_OFFSET, char_sprites, sizeof(char_sprites)); /* Load ROM */ if(argc<2) @@ -374,16 +372,21 @@ int main(int argc, char **argv){ step(); - //nanosleep(&(struct timespec){0,1000000},NULL); - wait_tick(); + nanosleep(&(struct timespec){0,(1000000000/840)},NULL); + //wait_tick(); - if(!(i%16)){ + if(!(i%(FREQ/60))){ + send_draw(); if(DT) DT--; if(ST) ST--; } - printf("i=%5d, DT=%3d \r", i, (int)DT); + printf("i=%5d, DT=%3d, K=[", i, (int)DT); + for(int k=0 ; k<16;k++) + printf("%s", KEYBOARD & 1<<k ? "1":"0"); + + printf("] \r"); } m_quit(); diff --git a/media-sdl.c b/media-sdl.c @@ -1,6 +1,8 @@ -#include <SDL2/SDL.h> +#include <assert.h> #include <string.h> +#include <SDL2/SDL.h> + #include "media.h" #define PIXEL_RADIUS 16 @@ -21,24 +23,24 @@ SDL_Rect rect = {.x=0,.y=0,.w=SCREEN_WIDTH,.h=SCREEN_HEIGHT}; unsigned clock; /* -┌────┬────┬────┬────┐ -│ 1 │ 2 │ 3 │ C │ -├────┼────┼────┼────┤ -│ 4 │ 5 │ 6 │ D │ -├────┼────┼────┼────┤ -│ 7 │ 8 │ 9 │ E │ -├────┼────┼────┼────┤ -│ A │ 0 │ B │ F │ -└────┴────┴────┴────┘ +┌───┬───┬───┬───┐ +│ 1 │ 2 │ 3 │ C │ +├───┼───┼───┼───┤ +│ 4 │ 5 │ 6 │ D │ +├───┼───┼───┼───┤ +│ 7 │ 8 │ 9 │ E │ +├───┼───┼───┼───┤ +│ A │ 0 │ B │ F │ +└───┴───┴───┴───┘ */ SDL_Keycode keys[16] = { SDLK_v, - SDLK_QUOTEDBL, SDLK_QUOTE, SDLK_LEFTPAREN, + SDLK_3, SDLK_4, SDLK_5, SDLK_e, SDLK_r, SDLK_t, SDLK_d, SDLK_f, SDLK_g, SDLK_c, SDLK_b, - SDLK_MINUS, SDLK_y, SDLK_h, SDLK_n + SDLK_6, SDLK_y, SDLK_h, SDLK_n }; int m_init(int argc, char **argv){ @@ -117,6 +119,9 @@ void clear_screen(void){ } void draw(int x, int y, int value){ + assert(0<=x && x<64); + assert(0<=y && y<32); + for(int i=0 ; i<PIXEL_RADIUS ; i++){ for(int j=0 ; j<PIXEL_RADIUS ; j++){ pixels[(y*PIXEL_RADIUS+i)*SCREEN_WIDTH +