From ab5e068900fa738f75e08ecec903402f1ac5b104 Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 8 Jan 2024 20:07:29 +0100 Subject: [PATCH] consistent code style Signed-off-by: Tim --- include/core/emu_definition.h | 2 +- src/core/emu_core.c | 55 ++++++++++++++------------- src/core/emu_main.c | 25 ++++++------ src/gui/main_window.c | 71 +++++++++++++++++++++-------------- src/gui/options_window.c | 10 ++--- 5 files changed, 89 insertions(+), 74 deletions(-) diff --git a/include/core/emu_definition.h b/include/core/emu_definition.h index 6bf0efb..d2d3276 100644 --- a/include/core/emu_definition.h +++ b/include/core/emu_definition.h @@ -59,7 +59,7 @@ static uint8_t FONT[FONT_SIZE] = { typedef struct Chip8 { uint8_t delay, sound; // Delay and sound timers - uint8_t ram[RAM_SIZE], reg[REGISTER_SIZE+1]; + uint8_t ram[RAM_SIZE], reg[REGISTER_SIZE + 1]; uint16_t stack[STACK_SIZE]; uint16_t pc, I; uint16_t opcode; diff --git a/src/core/emu_core.c b/src/core/emu_core.c index 0d02764..46b0fd2 100644 --- a/src/core/emu_core.c +++ b/src/core/emu_core.c @@ -81,25 +81,25 @@ int32_t decode_exec(emu *chip8, options_config *config) { case (0x3): if (chip8->reg[X] == N2) { - chip8->pc+=2; + chip8->pc += 2; } break; case (0x4): if (chip8->reg[X] != N2) { - chip8->pc+=2; + chip8->pc += 2; } break; case (0x5): - if(chip8->reg[X] == chip8->reg[Y]) { - chip8->pc+=2; + if (chip8->reg[X] == chip8->reg[Y]) { + chip8->pc += 2; } break; case (0x9): - if(chip8->reg[X] != chip8->reg[Y]) { - chip8->pc+=2; + if (chip8->reg[X] != chip8->reg[Y]) { + chip8->pc += 2; } break; @@ -115,27 +115,27 @@ int32_t decode_exec(emu *chip8, options_config *config) { switch (N1) { case (0x0): - chip8->reg[X]=chip8->reg[Y]; + chip8->reg[X] = chip8->reg[Y]; break; case (0x1): - chip8->reg[X]|=chip8->reg[Y]; + chip8->reg[X] |= chip8->reg[Y]; chip8->reg[0xf] = 0x0; break; case (0x2): - chip8->reg[X]&=chip8->reg[Y]; + chip8->reg[X] &= chip8->reg[Y]; chip8->reg[0xf] = 0x0; break; case (0x3): - chip8->reg[X]^=chip8->reg[Y]; + chip8->reg[X] ^= chip8->reg[Y]; chip8->reg[0xf] = 0x0; break; case (0x4): N1 = chip8->reg[0xf]; - chip8->reg[X]+=chip8->reg[Y]; + chip8->reg[X] += chip8->reg[Y]; if (N1 + chip8->reg[Y] > 255) { chip8->reg[0xf] = 1; } else { @@ -145,7 +145,7 @@ int32_t decode_exec(emu *chip8, options_config *config) { case (0x5): N1 = chip8->reg[X]; - chip8->reg[X]-=chip8->reg[Y]; + chip8->reg[X] -= chip8->reg[Y]; if (N1 < chip8->reg[Y]) { chip8->reg[0xF] = 0; } else { @@ -204,13 +204,13 @@ int32_t decode_exec(emu *chip8, options_config *config) { if (N2 == 0x9e) { if (chip8->key == chip8->reg[X]) { - chip8->pc+=2; + chip8->pc += 2; chip8->key = -1; } } else if (N2 == 0xa1) { if (chip8->key != chip8->reg[X]) { - chip8->pc+=2; + chip8->pc += 2; chip8->key = -1; } @@ -223,21 +223,21 @@ int32_t decode_exec(emu *chip8, options_config *config) { switch (N2) { case (0x07): - chip8->reg[X]=chip8->delay; + chip8->reg[X] = chip8->delay; break; case (0x15): - chip8->delay=chip8->reg[X]; + chip8->delay = chip8->reg[X]; break; case (0x18): - chip8->sound=chip8->reg[X]; + chip8->sound = chip8->reg[X]; break; case (0x1e): N1 = chip8->I; chip8->I += chip8->reg[X]; - if (N1+chip8->reg[X] > 0x0FFF) { + if (N1 + chip8->reg[X] > 0x0FFF) { chip8->reg[0xf] = 1; } else { chip8->reg[0xf] = 0; @@ -246,32 +246,33 @@ int32_t decode_exec(emu *chip8, options_config *config) { case (0x0a): if (chip8->key != -1) { - chip8->reg[X]=chip8->key; + chip8->reg[X] = chip8->key; } else { - chip8->pc-=2; + chip8->pc -= 2; } break; case (0x29): - chip8->I = ((chip8->reg[X]&0xf)*5); + chip8->I = ((chip8->reg[X] & 0xf) * 5); break; case (0x33): N1 = chip8->reg[X]; - chip8->ram[(chip8->I)%RAM_SIZE] = N1 / 100; - chip8->ram[(chip8->I+1)%RAM_SIZE] = N1 % (((chip8->ram[(chip8->I)%RAM_SIZE])*100))/ 10; - chip8->ram[(chip8->I+2)%RAM_SIZE] = N1 % (((chip8->ram[(chip8->I)%RAM_SIZE])*100+(chip8->ram[(chip8->I+1)%RAM_SIZE])*10)); + chip8->ram[(chip8->I) % RAM_SIZE] = N1 / 100; + chip8->ram[(chip8->I + 1) % RAM_SIZE] = N1 % (((chip8->ram[(chip8->I) % RAM_SIZE]) * 100)) / 10; + chip8->ram[(chip8->I + 2) % RAM_SIZE] = N1 % (((chip8->ram[(chip8->I) % RAM_SIZE]) * 100 + + (chip8->ram[(chip8->I + 1) % RAM_SIZE]) * 10)); break; case (0x55): for (int i = 0x0; i <= X; ++i) { - chip8->ram[(chip8->I+i)%RAM_SIZE] = chip8->reg[i]; + chip8->ram[(chip8->I + i) % RAM_SIZE] = chip8->reg[i]; } break; case (0x65): for (int i = 0x0; i <= X; ++i) { - chip8->reg[i] = chip8->ram[(chip8->I+i)%RAM_SIZE]; + chip8->reg[i] = chip8->ram[(chip8->I + i) % RAM_SIZE]; } break; @@ -291,7 +292,7 @@ int32_t decode_exec(emu *chip8, options_config *config) { uint8_t pixel = chip8->ram[chip8->I + y]; for (int x = 0; x < 8; ++x) { if ((pixel & (0x80 >> x)) != 0) { - if (X+x < DISPLAY_WIDTH && Y+y < DISPLAY_HEIGHT) { + if (X + x < DISPLAY_WIDTH && Y + y < DISPLAY_HEIGHT) { if (chip8->pixels[X + x][Y + y] == 1) { chip8->reg[0xF] = 1; } diff --git a/src/core/emu_main.c b/src/core/emu_main.c index 6f55cd7..1aaee75 100644 --- a/src/core/emu_main.c +++ b/src/core/emu_main.c @@ -77,14 +77,15 @@ void check_input(emu *chip8) { // I'm sorry, I tried it with a switch but // This is from a raylib audio example :) // Audio frequency values float frequency = 440.0f, audio_frequency = 440.0f, sine_index = 0.0f, sample = 35000.0f; + void generate_beep(void *buffer, unsigned int frames) { audio_frequency = frequency + (audio_frequency - frequency) * 0.95f; float increase = audio_frequency / sample; - short *d = (short *)buffer; + short *d = (short *) buffer; for (unsigned int i = 0; i < frames; i++) { - d[i] = (short)(16000.0f*sinf(2 * PI * sine_index)); + d[i] = (short) (16000.0f * sinf(2 * PI * sine_index)); sine_index += increase; if (sine_index > 1.0f) sine_index -= 1.0f; } @@ -137,29 +138,29 @@ int32_t emu_main(options_config *config, ui_scale *scale) { while (!IsKeyPressed(KEY_ESCAPE)) { BeginDrawing(); - if(IsWindowResized()) { + if (IsWindowResized()) { config->display_scaling = GetScreenWidth() / DISPLAY_WIDTH; scale->window_width = DISPLAY_WIDTH * config->display_scaling; scale->window_height = DISPLAY_HEIGHT * config->display_scaling; SetWindowSize(scale->window_width, scale->window_height); - scale->button_width = (float)(scale->window_width/4.8); - scale->button_height = (float)((float)scale->window_height/16); - scale->button_x = (float)((float)scale->window_width/2-(scale->window_width/9.6)); - scale->font_size = (scale->window_height/12); + scale->button_width = (float) (scale->window_width / 4.8); + scale->button_height = (float) ((float) scale->window_height / 16); + scale->button_x = (float) ((float) scale->window_width / 2 - (scale->window_width / 9.6)); + scale->font_size = (scale->window_height / 12); } UnloadDroppedFiles(LoadDroppedFiles()); // Draw Pixels from virtual Texture - DrawTextureEx(chip8.display, (Vector2){0, 0}, 0, (float)config->display_scaling, WHITE); + DrawTextureEx(chip8.display, (Vector2) {0, 0}, 0, (float) config->display_scaling, WHITE); - if(config->show_fps) { - DrawText(TextFormat("%dhz", GetFPS()+1), (int)config->display_scaling,(int)config->display_scaling/2, - (int)(GetScreenHeight()/(config->display_scaling*1.5)), DARKGREEN); + if (config->show_fps) { + DrawText(TextFormat("%dhz", GetFPS() + 1), (int) config->display_scaling, (int) config->display_scaling / 2, + (int) (GetScreenHeight() / (config->display_scaling * 1.5)), DARKGREEN); } - for (int i = 0; i < (int)(CLOCK_RATE/REFRESH_RATE); ++i) { + for (int i = 0; i < (int) (CLOCK_RATE / REFRESH_RATE); ++i) { check_input(&chip8); if (!undefined && fetch(&chip8) == -1) { undefined = true; diff --git a/src/gui/main_window.c b/src/gui/main_window.c index 15f42d1..149b8e2 100644 --- a/src/gui/main_window.c +++ b/src/gui/main_window.c @@ -26,6 +26,7 @@ // RAYLIB INCLUDE #include "raylib.h" + #define RAYGUI_IMPLEMENTATION #include "raygui.h" @@ -38,7 +39,7 @@ void main_window(options_config *config) { float temp; // Show debug info? - if(config->show_debug == true) { + if (config->show_debug == true) { SetTraceLogLevel(LOG_INFO); } else { SetTraceLogLevel(LOG_ERROR); @@ -49,16 +50,18 @@ void main_window(options_config *config) { SetConfigFlags(FLAG_WINDOW_RESIZABLE); - InitWindow(scale.window_width,scale.window_height,WINDOW_TITLE VERSION); + InitWindow(scale.window_width, scale.window_height, WINDOW_TITLE VERSION); - scale.button_width = (float)(scale.window_width/4.8); - scale.button_height = (float)((float)scale.window_height/16); - scale.button_x = (float)((float)scale.window_width/2-(scale.window_width/9.6)); + scale.button_width = (float) (scale.window_width / 4.8); + scale.button_height = (float) ((float) scale.window_height / 16); + scale.button_x = (float) ((float) scale.window_width / 2 - (scale.window_width / 9.6)); - scale.font_size = (scale.window_height/12); + scale.font_size = (scale.window_height / 12); GuiLoadStyleDark(); - enum menu_state_counter {normal, options, init}; + enum menu_state_counter { + normal, options, init + }; uint32_t menu_state = normal; SetTargetFPS(REFRESH_RATE); @@ -66,21 +69,21 @@ void main_window(options_config *config) { BeginDrawing(); ClearBackground(BLACK); - if(IsWindowResized()) { + if (IsWindowResized()) { config->display_scaling = GetScreenWidth() / DISPLAY_WIDTH; scale.window_width = DISPLAY_WIDTH * config->display_scaling; scale.window_height = DISPLAY_HEIGHT * config->display_scaling; SetWindowSize(scale.window_width, scale.window_height); - scale.button_width = (float)(scale.window_width/4.8); - scale.button_height = (float)((float)scale.window_height/16); - scale.button_x = (float)((float)scale.window_width/2-(scale.window_width/9.6)); - scale.font_size = (scale.window_height/12); + scale.button_width = (float) (scale.window_width / 4.8); + scale.button_height = (float) ((float) scale.window_height / 16); + scale.button_x = (float) ((float) scale.window_width / 2 - (scale.window_width / 9.6)); + scale.font_size = (scale.window_height / 12); } if (IsFileDropped()) { // Initialize Emulation EndDrawing(); - if(emu_main(config, &scale) == -2) {break;} + if (emu_main(config, &scale) == -2) { break; } menu_state = normal; } @@ -89,19 +92,23 @@ void main_window(options_config *config) { switch (menu_state) { /*-------------------------------------------------------------------------------------------------------------*/ case (normal): - DrawText("Chisel8 Emulator", scale.window_width/2-(scale.font_size*4), (scale.window_height/12), scale.font_size, RAYWHITE); + DrawText("Chisel8 Emulator", scale.window_width / 2 - (scale.font_size * 4), (scale.window_height / 12), + scale.font_size, RAYWHITE); - if (GuiButton((Rectangle){ scale.button_x, scale.window_height/2-scale.window_height/6-10, scale.button_width, scale.button_height }, - GuiIconText(ICON_CPU, "Load ROM") )) { + if (GuiButton((Rectangle) {scale.button_x, scale.window_height / 2 - scale.window_height / 6 - 10, + scale.button_width, scale.button_height}, + GuiIconText(ICON_CPU, "Load ROM"))) { menu_state = init; } - if (GuiButton((Rectangle){ scale.button_x, scale.window_height/2-scale.window_height/16, scale.button_width, scale.button_height }, + if (GuiButton((Rectangle) {scale.button_x, scale.window_height / 2 - scale.window_height / 16, + scale.button_width, scale.button_height}, GuiIconText(ICON_GEAR, "Settings"))) { menu_state = options; } - if (GuiButton((Rectangle){ scale.button_x, scale.window_height-(scale.window_height/6), scale.button_width, scale.button_height }, + if (GuiButton((Rectangle) {scale.button_x, scale.window_height - (scale.window_height / 6), + scale.button_width, scale.button_height}, GuiIconText(ICON_EXIT, "Quit"))) { EndDrawing(); CloseWindow(); @@ -109,55 +116,61 @@ void main_window(options_config *config) { } break; - /*-------------------------------------------------------------------------------------------------------------*/ + /*-------------------------------------------------------------------------------------------------------------*/ case (options): // TODO: Call function from options_window source file and actually add options to choose from // TODO: Should allow the user to change the window scaling and other options... - DrawText("Settings", scale.window_width/2-(scale.font_size*2), (scale.window_height/12), scale.font_size, RAYWHITE); + DrawText("Settings", scale.window_width / 2 - (scale.font_size * 2), (scale.window_height / 12), + scale.font_size, RAYWHITE); if (config->volume != 0) { - if (GuiButton((Rectangle){ scale.button_x, scale.window_height-(scale.window_height/2), scale.button_width, scale.button_height }, + if (GuiButton((Rectangle) {scale.button_x, scale.window_height - (scale.window_height / 2), + scale.button_width, scale.button_height}, GuiIconText(ICON_AUDIO, "Mute Audio"))) { temp = config->volume; config->volume = 0.0f; } } else { - if (GuiButton((Rectangle){ scale.button_x, scale.window_height-(scale.window_height/2), scale.button_width, scale.button_height }, + if (GuiButton((Rectangle) {scale.button_x, scale.window_height - (scale.window_height / 2), + scale.button_width, scale.button_height}, GuiIconText(ICON_AUDIO, "Unmute Audio"))) { config->volume = temp; } } - if (GuiButton((Rectangle){ scale.button_x, scale.window_height-(scale.window_height/6), scale.button_width, scale.button_height }, + if (GuiButton((Rectangle) {scale.button_x, scale.window_height - (scale.window_height / 6), + scale.button_width, scale.button_height}, GuiIconText(ICON_REREDO_FILL, "Return"))) { menu_state = normal; break; } break; - /*-------------------------------------------------------------------------------------------------------------*/ + /*-------------------------------------------------------------------------------------------------------------*/ case (init): DrawRectangle(0, 0, scale.window_width, scale.window_height, Fade(RAYWHITE, 0.2f)); - DrawText("Please drag a ROM file into the window", scale.window_width/2-(scale.font_size*10)+(scale.font_size/2), - (scale.window_height/2)-scale.font_size*1.5, scale.font_size, RAYWHITE); + DrawText("Please drag a ROM file into the window", + scale.window_width / 2 - (scale.font_size * 10) + (scale.font_size / 2), + (scale.window_height / 2) - scale.font_size * 1.5, scale.font_size, RAYWHITE); - if (GuiButton((Rectangle){ scale.button_x, scale.window_height-(scale.window_height/6), scale.button_width, scale.button_height }, + if (GuiButton((Rectangle) {scale.button_x, scale.window_height - (scale.window_height / 6), + scale.button_width, scale.button_height}, GuiIconText(ICON_REREDO_FILL, "Return"))) { menu_state = normal; break; } if (IsFileDropped()) { // Initialize Emulation EndDrawing(); - if(emu_main(config, &scale) == -2) {break;} + if (emu_main(config, &scale) == -2) { break; } menu_state = normal; break; } break; - /*-------------------------------------------------------------------------------------------------------------*/ + /*-------------------------------------------------------------------------------------------------------------*/ } EndDrawing(); } diff --git a/src/gui/options_window.c b/src/gui/options_window.c index 8cdeb72..19f6cbf 100644 --- a/src/gui/options_window.c +++ b/src/gui/options_window.c @@ -65,7 +65,7 @@ void load_settings(options_config *config) { int32_t temp; for (int i = 0; i < items; ++i) { fgets(line, 100, file); - uint16_t color[4] = {0,0,0,0}; + uint16_t color[4] = {0, 0, 0, 0}; uint32_t counter = 0; // Skip line if it is not a value @@ -116,7 +116,7 @@ void load_settings(options_config *config) { case 3: for (temp = 0; line[temp] != ';'; ++temp) {} - if(strncmp(&line[0],"true", sizeof(line[temp])) == 0) { + if (strncmp(&line[0], "true", sizeof(line[temp])) == 0) { config->show_debug = true; } else { config->show_debug = false; @@ -125,7 +125,7 @@ void load_settings(options_config *config) { case 4: for (temp = 0; line[temp] != ';'; ++temp) {} - if(strncmp(&line[0],"true", sizeof(line[temp])) == 0) { + if (strncmp(&line[0], "true", sizeof(line[temp])) == 0) { config->show_fps = true; } else { config->show_fps = false; @@ -134,8 +134,8 @@ void load_settings(options_config *config) { case 5: for (temp = 0; line[temp] != ';'; ++temp) {} - if(strncmp(&line[0],"0.", sizeof(line[temp])) == 0) { - config->volume = (float)(line[2] - '0') / 10 + (float)(line[3] - '0') / 100; + if (strncmp(&line[0], "0.", sizeof(line[temp])) == 0) { + config->volume = (float) (line[2] - '0') / 10 + (float) (line[3] - '0') / 100; } else { config->volume = 1.0f; }