Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/core/emu_definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
55 changes: 28 additions & 27 deletions src/core/emu_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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;
}
Expand Down
25 changes: 13 additions & 12 deletions src/core/emu_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
Loading