chip8

CHIP-8 emulator
git clone git://git.christosmarg.xyz/chip8.git
Log | Files | Refs | README | LICENSE

commit eaafa9a499574c6feaf4ba1cb8f25f20f63e0f85
parent 7a3b70aed4c1fbc5dd1869290be3b411de342a36
Author: Christos Margiolis <christos@margiolis.net>
Date:   Mon, 25 May 2020 14:12:00 +0300

tabs -> spaces

Diffstat:
MREADME.md | 10+++++++++-
Mbin/chip8 | 0
Mobj/main.o | 0
Msrc/chip8.c | 554++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/chip8.h | 28++++++++++++++--------------
Msrc/main.c | 144++++++++++++++++++++++++++++++++++++++++----------------------------------------
6 files changed, 372 insertions(+), 364 deletions(-)

diff --git a/README.md b/README.md @@ -9,12 +9,20 @@ Your typical CHIP-8 copy. This one uses SDL2 as a rendering API. ## Usage -``` +```shell $ cd path/to/chip8 $ make $ cd bin $ ./chip8 [../path/to/ROM] ``` +In order to install do +```shell +$ cd path/to/chip8 +$ make +$ sudo make install +$ make clean # optional +``` +The binary will be installed at `/usr/local/bin/` ## To Do diff --git a/bin/chip8 b/bin/chip8 Binary files differ. diff --git a/obj/main.o b/obj/main.o Binary files differ. diff --git a/src/chip8.c b/src/chip8.c @@ -1,327 +1,327 @@ #include "chip8.h" -#define V chip8->V -#define pc chip8->pc -#define opcode chip8->opcode -#define I chip8->I -#define sp chip8->sp -#define memory chip8->memory -#define gfx chip8->gfx -#define stack chip8->stack -#define keys chip8->keys -#define delaytimer chip8->delaytimer -#define soundtimer chip8->soundtimer -#define drawflag chip8->drawflag +#define V chip8->V +#define pc chip8->pc +#define opcode chip8->opcode +#define I chip8->I +#define sp chip8->sp +#define memory chip8->memory +#define gfx chip8->gfx +#define stack chip8->stack +#define keys chip8->keys +#define delaytimer chip8->delaytimer +#define soundtimer chip8->soundtimer +#define drawflag chip8->drawflag void chip8_init(Chip8 *chip8) { - uint8_t fontset[80] = - { - 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0 - 0x20, 0x60, 0x20, 0x20, 0x70, // 1 - 0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2 - 0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3 - 0x90, 0x90, 0xF0, 0x10, 0x10, // 4 - 0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5 - 0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6 - 0xF0, 0x10, 0x20, 0x40, 0x40, // 7 - 0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8 - 0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9 - 0xF0, 0x90, 0xF0, 0x90, 0x90, // A - 0xE0, 0x90, 0xE0, 0x90, 0xE0, // B - 0xF0, 0x80, 0x80, 0x80, 0xF0, // C - 0xE0, 0x90, 0x90, 0x90, 0xE0, // D - 0xF0, 0x80, 0xF0, 0x80, 0xF0, // E - 0xF0, 0x80, 0xF0, 0x80, 0x80 // F - }; + uint8_t fontset[80] = + { + 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0 + 0x20, 0x60, 0x20, 0x20, 0x70, // 1 + 0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2 + 0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3 + 0x90, 0x90, 0xF0, 0x10, 0x10, // 4 + 0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5 + 0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6 + 0xF0, 0x10, 0x20, 0x40, 0x40, // 7 + 0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8 + 0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9 + 0xF0, 0x90, 0xF0, 0x90, 0x90, // A + 0xE0, 0x90, 0xE0, 0x90, 0xE0, // B + 0xF0, 0x80, 0x80, 0x80, 0xF0, // C + 0xE0, 0x90, 0x90, 0x90, 0xE0, // D + 0xF0, 0x80, 0xF0, 0x80, 0xF0, // E + 0xF0, 0x80, 0xF0, 0x80, 0x80 // F + }; - pc = 0x200; - opcode = 0; - I = 0; - sp = 0; - delaytimer = 0; - soundtimer = 0; + pc = 0x200; + opcode = 0; + I = 0; + sp = 0; + delaytimer = 0; + soundtimer = 0; - memset(V, 0, 16 * sizeof(uint8_t)); - memset(keys, 0, 16 * sizeof(uint8_t)); - memset(stack, 0, 16 * sizeof(uint16_t)); - memset(gfx, 0, 2048 * sizeof(uint8_t)); - memset(memory, 0, 4096 * sizeof(uint8_t)); - int i; - for (i = 0; i < 80; i++) - memory[i] = fontset[i]; + memset(V, 0, 16 * sizeof(uint8_t)); + memset(keys, 0, 16 * sizeof(uint8_t)); + memset(stack, 0, 16 * sizeof(uint16_t)); + memset(gfx, 0, 2048 * sizeof(uint8_t)); + memset(memory, 0, 4096 * sizeof(uint8_t)); + int i; + for (i = 0; i < 80; i++) + memory[i] = fontset[i]; } int load(Chip8 *chip8, const char *fpath) { - FILE *rom = fopen(fpath, "rb"); - if (rom == NULL) - { - fprintf(stderr, "Error loading ROM (%s). Exiting. . .\n", fpath); - return FALSE; - } - fseek(rom, 0, SEEK_END); - long romsize = ftell(rom); - rewind(rom); + FILE *rom = fopen(fpath, "rb"); + if (rom == NULL) + { + fprintf(stderr, "Error loading ROM (%s). Exiting. . .\n", fpath); + return FALSE; + } + fseek(rom, 0, SEEK_END); + long romsize = ftell(rom); + rewind(rom); - char *buf = (char *)malloc(romsize * sizeof(char)); - if (buf == NULL) - { - fprintf(stderr, "Cannot allocate memory. Exiting. . .\n"); - return FALSE; - } + char *buf = (char *)malloc(romsize * sizeof(char)); + if (buf == NULL) + { + fprintf(stderr, "Cannot allocate memory. Exiting. . .\n"); + return FALSE; + } - size_t res = fread(buf, sizeof(char), (size_t)romsize, rom); - if (res != romsize) - { - fprintf(stderr, "Error reading ROM. Exiting. . .\n"); - return FALSE; - } + size_t res = fread(buf, sizeof(char), (size_t)romsize, rom); + if (res != romsize) + { + fprintf(stderr, "Error reading ROM. Exiting. . .\n"); + return FALSE; + } - int i; - if ((4096 - 512) > romsize) - for (i = 0; i < romsize; i++) - memory[i + 512] = (uint8_t)buf[i]; - else - { - fprintf(stderr, "ROM can't fit into memory. Exiting. . .\n"); - return FALSE; - } + int i; + if ((4096 - 512) > romsize) + for (i = 0; i < romsize; i++) + memory[i + 512] = (uint8_t)buf[i]; + else + { + fprintf(stderr, "ROM can't fit into memory. Exiting. . .\n"); + return FALSE; + } - fclose(rom); - free(buf); - return TRUE; + fclose(rom); + free(buf); + return TRUE; } void emulate(Chip8 *chip8) { - fetch(chip8); - if (decode(chip8)) - { - execute(chip8); - update_timers(chip8); - } + fetch(chip8); + if (decode(chip8)) + { + execute(chip8); + update_timers(chip8); + } - printf("opcode: %x\tmemory: %x\tI: %x\tsp: %x\tpc: %d\n", - opcode, memory[pc] << 8 | memory[pc + 1], I, sp, pc); + printf("opcode: %x\tmemory: %x\tI: %x\tsp: %x\tpc: %d\n", + opcode, memory[pc] << 8 | memory[pc + 1], I, sp, pc); } void fetch(Chip8 *chip8) { - opcode = memory[pc] << 8 | memory[pc + 1]; + opcode = memory[pc] << 8 | memory[pc + 1]; } int decode(Chip8 *chip8) { - int i; - switch (opcode & 0xF000) - { - case 0x0000: // 00E_ - switch (opcode & 0x00FF) - { - case 0xE0: // 00E0 - Clear screen - memset(gfx, 0, 2048 * sizeof(uint8_t)); - drawflag = 1; - break; - case 0xEE: // 00EE - Return from subroutine - pc = stack[--sp]; - break; - default: - fprintf(stderr, "Unknown opcode: %x\n", opcode); - return FALSE; - } - break; - case 0x1000: // 1NNN - Jump to address NNN - pc = (opcode & 0x0FFF) - 2; - break; - case 0x2000: // 2NNN - Call subroutine at NNN - stack[sp++] = pc; - pc = (opcode & 0x0FFF) - 2; - case 0x3000: // 3NNN - Skip next instruction if VX == NN - if (V[(opcode & 0x0F00) >> 8] == (opcode & 0x00FF)) pc += 2; - break; - case 0x4000: // 4NNN - Skip next instruction if VX != NN - if (V[(opcode & 0x0F00) >> 8] != (opcode & 0x00FF)) pc += 2; - break; - case 0x5000: // 5XY0 - Skip next instruction if VX == VY - if (V[(opcode & 0x0F00) >> 8] == V[(opcode & 0x00F0) >> 4]) pc += 2; - break; - case 0x6000: // 6XNN - Set VX to NN - V[(opcode & 0x0F00) >> 8] = opcode & 0x00FF; - break; - case 0x7000: // 7XNN - Add NN to VX - V[(opcode & 0x0F00) >> 8] += opcode & 0x00FF; - break; - case 0x8000: // 8XY_ - switch (opcode & 0x000F) - { - case 0x0000: // 8XY0 - Set VX to VY - V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x00F0) >> 4]; - break; - case 0x0001: // 8XY1 - Set VX to (VX OR VY) - V[(opcode & 0x0F00) >> 8] |= V[(opcode & 0x00F0) >> 4]; - break; - case 0x0002: // 8XY2 - Set VX to (VX AND VY) - V[(opcode & 0x0F00) >> 8] &= V[(opcode & 0x00F0) >> 4]; - break; - case 0x0003: // 8XY3 - Set VX to (VX XOR VY) - V[(opcode & 0x0F00) >> 8] ^= V[(opcode & 0x00F0) >> 4]; - break; - case 0x0004: // 8XY4 - Add VY to VX, VF = 1 if there is a carry - V[(opcode & 0x0F00) >> 8] += V[(opcode & 0x00F0) >> 4]; - V[0xF] = (V[(opcode & 0x00F0) >> 4] > (0xFF - V[(opcode & 0x0F00) >> 8])) ? 1 : 0; - break; - case 0x0005: // 8XY5 - Sub VY from VX, VF = 0 if there is a borrow - V[0xF] = (V[(opcode & 0x00F0) >> 4] > V[(opcode & 0x0F00) >> 8]) ? 0 : 1; - V[(opcode & 0x0F00) >> 8] -= V[(opcode & 0x00F0) >> 4]; - break; - case 0x0006: // 8XY6 - Shift VX right by 1. VF = LSB of VX before shift - V[0xF] = V[(opcode & 0x0F00) >> 8] & 0x1; - V[(opcode & 0x0F00) >> 8] >>= 1; - break; - case 0x0007: // 8XY7 - Set VX to VY-VX. VF = 0 if there is a borrow - V[0xF] = (V[(opcode & 0x0F00) >> 8] > V[(opcode & 0x00F0) >> 4]) ? 0 : 1; - V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x00F0) >> 4] - V[(opcode & 0x0F00) >> 8]; - break; - case 0x000E: // 8XYE - Shift VX left by 1. VF = MSB of VX before shift - V[0xF] = V[(opcode & 0x0F00) >> 8] >> 7; - V[(opcode & 0x0F00) >> 8] <<= 1; - break; - default: - fprintf(stderr, "Unknown opcode: %x\n", opcode); - return FALSE; - } - break; - case 0x9000: // 9XY0 - Skip next instruction if VX != VY - if (V[(opcode & 0x0F00) >> 8] != V[(opcode & 0x00F0) >> 4]) pc += 2; - break; - case 0xA000: // ANNN - Set I to the address NNN - I = opcode & 0x0FFF; - break; - case 0xB000: // BNNN - Jump to NNN + V0 - pc = ((opcode & 0x0FFF) + V[0]) - 2; - break; - case 0xC000: // CNNN - Set VX to random number masked by NN - V[(opcode & 0x0F00) >> 8] = (rand() % (0xFF + 1)) & (opcode & 0x00FF); - break; - case 0xD000: // Draw an 8 pixel sprite at (VX, VY) - { - uint8_t VX = V[(opcode & 0x0F00) >> 8]; - uint8_t VY = V[(opcode & 0x00F0) >> 4]; - uint16_t h = opcode & 0x000F; - uint16_t pixel; + int i; + switch (opcode & 0xF000) + { + case 0x0000: // 00E_ + switch (opcode & 0x00FF) + { + case 0xE0: // 00E0 - Clear screen + memset(gfx, 0, 2048 * sizeof(uint8_t)); + drawflag = 1; + break; + case 0xEE: // 00EE - Return from subroutine + pc = stack[--sp]; + break; + default: + fprintf(stderr, "Unknown opcode: %x\n", opcode); + return FALSE; + } + break; + case 0x1000: // 1NNN - Jump to address NNN + pc = (opcode & 0x0FFF) - 2; + break; + case 0x2000: // 2NNN - Call subroutine at NNN + stack[sp++] = pc; + pc = (opcode & 0x0FFF) - 2; + case 0x3000: // 3NNN - Skip next instruction if VX == NN + if (V[(opcode & 0x0F00) >> 8] == (opcode & 0x00FF)) pc += 2; + break; + case 0x4000: // 4NNN - Skip next instruction if VX != NN + if (V[(opcode & 0x0F00) >> 8] != (opcode & 0x00FF)) pc += 2; + break; + case 0x5000: // 5XY0 - Skip next instruction if VX == VY + if (V[(opcode & 0x0F00) >> 8] == V[(opcode & 0x00F0) >> 4]) pc += 2; + break; + case 0x6000: // 6XNN - Set VX to NN + V[(opcode & 0x0F00) >> 8] = opcode & 0x00FF; + break; + case 0x7000: // 7XNN - Add NN to VX + V[(opcode & 0x0F00) >> 8] += opcode & 0x00FF; + break; + case 0x8000: // 8XY_ + switch (opcode & 0x000F) + { + case 0x0000: // 8XY0 - Set VX to VY + V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x00F0) >> 4]; + break; + case 0x0001: // 8XY1 - Set VX to (VX OR VY) + V[(opcode & 0x0F00) >> 8] |= V[(opcode & 0x00F0) >> 4]; + break; + case 0x0002: // 8XY2 - Set VX to (VX AND VY) + V[(opcode & 0x0F00) >> 8] &= V[(opcode & 0x00F0) >> 4]; + break; + case 0x0003: // 8XY3 - Set VX to (VX XOR VY) + V[(opcode & 0x0F00) >> 8] ^= V[(opcode & 0x00F0) >> 4]; + break; + case 0x0004: // 8XY4 - Add VY to VX, VF = 1 if there is a carry + V[(opcode & 0x0F00) >> 8] += V[(opcode & 0x00F0) >> 4]; + V[0xF] = (V[(opcode & 0x00F0) >> 4] > (0xFF - V[(opcode & 0x0F00) >> 8])) ? 1 : 0; + break; + case 0x0005: // 8XY5 - Sub VY from VX, VF = 0 if there is a borrow + V[0xF] = (V[(opcode & 0x00F0) >> 4] > V[(opcode & 0x0F00) >> 8]) ? 0 : 1; + V[(opcode & 0x0F00) >> 8] -= V[(opcode & 0x00F0) >> 4]; + break; + case 0x0006: // 8XY6 - Shift VX right by 1. VF = LSB of VX before shift + V[0xF] = V[(opcode & 0x0F00) >> 8] & 0x1; + V[(opcode & 0x0F00) >> 8] >>= 1; + break; + case 0x0007: // 8XY7 - Set VX to VY-VX. VF = 0 if there is a borrow + V[0xF] = (V[(opcode & 0x0F00) >> 8] > V[(opcode & 0x00F0) >> 4]) ? 0 : 1; + V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x00F0) >> 4] - V[(opcode & 0x0F00) >> 8]; + break; + case 0x000E: // 8XYE - Shift VX left by 1. VF = MSB of VX before shift + V[0xF] = V[(opcode & 0x0F00) >> 8] >> 7; + V[(opcode & 0x0F00) >> 8] <<= 1; + break; + default: + fprintf(stderr, "Unknown opcode: %x\n", opcode); + return FALSE; + } + break; + case 0x9000: // 9XY0 - Skip next instruction if VX != VY + if (V[(opcode & 0x0F00) >> 8] != V[(opcode & 0x00F0) >> 4]) pc += 2; + break; + case 0xA000: // ANNN - Set I to the address NNN + I = opcode & 0x0FFF; + break; + case 0xB000: // BNNN - Jump to NNN + V0 + pc = ((opcode & 0x0FFF) + V[0]) - 2; + break; + case 0xC000: // CNNN - Set VX to random number masked by NN + V[(opcode & 0x0F00) >> 8] = (rand() % (0xFF + 1)) & (opcode & 0x00FF); + break; + case 0xD000: // Draw an 8 pixel sprite at (VX, VY) + { + uint8_t VX = V[(opcode & 0x0F00) >> 8]; + uint8_t VY = V[(opcode & 0x00F0) >> 4]; + uint16_t h = opcode & 0x000F; + uint16_t pixel; - V[0xF] = 0; - int yl, xl; - for (yl = 0; yl < h; yl++) - { - pixel = memory[I + yl]; - for (xl = 0; xl < 8; xl++) - { - if ((pixel & (0x80 >> xl)) != 0) - { - if (gfx[VX + xl + ((VY + yl) * 64)] == 1) - V[0xF] = 1; - gfx[VX + xl + ((VY + yl) * 64)] ^= 1; - } - } - } + V[0xF] = 0; + int yl, xl; + for (yl = 0; yl < h; yl++) + { + pixel = memory[I + yl]; + for (xl = 0; xl < 8; xl++) + { + if ((pixel & (0x80 >> xl)) != 0) + { + if (gfx[VX + xl + ((VY + yl) * 64)] == 1) + V[0xF] = 1; + gfx[VX + xl + ((VY + yl) * 64)] ^= 1; + } + } + } - drawflag = 1; - } - break; - case 0xE000: // EX__ - switch (opcode & 0x00FF) - { - case 0x009E: // EX9E - Skip next instruction if key in VX is pressed - if (keys[V[(opcode & 0x0F00) >> 8]] != 0) pc += 2; - break; - case 0x00A1: // EXA1 - Skip next instruction if key in VX isn't pressed - if (keys[V[(opcode & 0x0F00) >> 8]] == 0) pc += 2; - break; - default: - fprintf(stderr, "Unknown opcode: %x\n", opcode); - return FALSE; - } - break; - case 0xF000: // FX__ - switch (opcode & 0x00FF) - { - case 0x0007: // FX07 - Set VX to delaytimer - V[(opcode & 0x0F00) >> 8] = delaytimer; - break; - case 0x000A: // FX0A - Wait for key press and then store it in VX - { - int keypressed = 0; - for (i = 0; i < 16; i++) - { - if (keys[i] != 0) - { - V[(opcode & 0x0F00) >> 8] = i; - keypressed = 1; - } - } + drawflag = 1; + } + break; + case 0xE000: // EX__ + switch (opcode & 0x00FF) + { + case 0x009E: // EX9E - Skip next instruction if key in VX is pressed + if (keys[V[(opcode & 0x0F00) >> 8]] != 0) pc += 2; + break; + case 0x00A1: // EXA1 - Skip next instruction if key in VX isn't pressed + if (keys[V[(opcode & 0x0F00) >> 8]] == 0) pc += 2; + break; + default: + fprintf(stderr, "Unknown opcode: %x\n", opcode); + return FALSE; + } + break; + case 0xF000: // FX__ + switch (opcode & 0x00FF) + { + case 0x0007: // FX07 - Set VX to delaytimer + V[(opcode & 0x0F00) >> 8] = delaytimer; + break; + case 0x000A: // FX0A - Wait for key press and then store it in VX + { + int keypressed = 0; + for (i = 0; i < 16; i++) + { + if (keys[i] != 0) + { + V[(opcode & 0x0F00) >> 8] = i; + keypressed = 1; + } + } - if (!keypressed) return FALSE; - } - break; - case 0x0015: // FX15 - Set the delaytimer to VX - delaytimer = V[(opcode & 0x0F00) >> 8]; - break; - case 0x0018: // FX18 - Set the soundtimer to VX - soundtimer = V[(opcode & 0x0F00) >> 8]; - break; - case 0x001E: // FX1E - Add VX to I - V[0xF] = ((I + V[(opcode & 0x0F00) >> 8]) > 0xFFF) ? 1 : 0; - I += V[(opcode & 0x0F00) >> 8]; - break; - case 0x0029: // FX29 - Set I to the location of the sprite for char VX - I = V[(opcode & 0x0F00) >> 8] * 0x5; - break; - case 0x0033: // FX33 - Store bin coded decimal of VX at I, I+1 and I+2 - memory[I] = V[(opcode & 0x0F00) >> 8] / 100; - memory[I+1] = (V[(opcode & 0x0F00) >> 8] / 10) % 10; - memory[I+2] = V[(opcode & 0x0F00) >> 8] % 10; - break; - case 0x0055: // FX55 - Store V0 to VX in memory starting at I - for (i = 0; i <= ((opcode & 0x0F00) >> 8); i++) - memory[I + i] = V[i]; - I += ((opcode & 0x0F00) >> 8) + 1; - break; - case 0x0065: // FX65 - Fill V0 to VX with vals from memory starting at I - for (i = 0; i <= ((opcode & 0x0F00) >> 8); i++) - V[i] = memory[I + i]; - I += ((opcode & 0x0F00) >> 8) + 1; - break; - default: - fprintf(stderr, "Unknown opcode: %x\n", opcode); - return FALSE; - } - break; - default: - fprintf(stderr, "Unimplemented opcode\n"); - return FALSE; - } + if (!keypressed) return FALSE; + } + break; + case 0x0015: // FX15 - Set the delaytimer to VX + delaytimer = V[(opcode & 0x0F00) >> 8]; + break; + case 0x0018: // FX18 - Set the soundtimer to VX + soundtimer = V[(opcode & 0x0F00) >> 8]; + break; + case 0x001E: // FX1E - Add VX to I + V[0xF] = ((I + V[(opcode & 0x0F00) >> 8]) > 0xFFF) ? 1 : 0; + I += V[(opcode & 0x0F00) >> 8]; + break; + case 0x0029: // FX29 - Set I to the location of the sprite for char VX + I = V[(opcode & 0x0F00) >> 8] * 0x5; + break; + case 0x0033: // FX33 - Store bin coded decimal of VX at I, I+1 and I+2 + memory[I] = V[(opcode & 0x0F00) >> 8] / 100; + memory[I+1] = (V[(opcode & 0x0F00) >> 8] / 10) % 10; + memory[I+2] = V[(opcode & 0x0F00) >> 8] % 10; + break; + case 0x0055: // FX55 - Store V0 to VX in memory starting at I + for (i = 0; i <= ((opcode & 0x0F00) >> 8); i++) + memory[I + i] = V[i]; + I += ((opcode & 0x0F00) >> 8) + 1; + break; + case 0x0065: // FX65 - Fill V0 to VX with vals from memory starting at I + for (i = 0; i <= ((opcode & 0x0F00) >> 8); i++) + V[i] = memory[I + i]; + I += ((opcode & 0x0F00) >> 8) + 1; + break; + default: + fprintf(stderr, "Unknown opcode: %x\n", opcode); + return FALSE; + } + break; + default: + fprintf(stderr, "Unimplemented opcode\n"); + return FALSE; + } - return TRUE; + return TRUE; } void execute(Chip8 *chip8) { - pc += 2; + pc += 2; } void update_timers(Chip8 *chip8) { - if (delaytimer > 0) --delaytimer; - if (soundtimer > 0) --soundtimer; + if (delaytimer > 0) --delaytimer; + if (soundtimer > 0) --soundtimer; } #undef V diff --git a/src/chip8.h b/src/chip8.h @@ -11,20 +11,20 @@ #define FALSE 0 typedef struct { - uint8_t memory[4096]; - uint16_t stack[16]; - uint16_t sp; - - uint8_t V[16]; - uint16_t opcode; - uint16_t I; - uint16_t pc; - - uint8_t delaytimer; - uint8_t soundtimer; - uint8_t gfx[64 * 32]; - uint8_t keys[16]; - int drawflag; + uint8_t memory[4096]; + uint16_t stack[16]; + uint16_t sp; + + uint8_t V[16]; + uint16_t opcode; + uint16_t I; + uint16_t pc; + + uint8_t delaytimer; + uint8_t soundtimer; + uint8_t gfx[64 * 32]; + uint8_t keys[16]; + int drawflag; } Chip8; extern Chip8 chip8; diff --git a/src/main.c b/src/main.c @@ -17,85 +17,85 @@ static const uint8_t keymap[16] = { int main(int argc, char **argv) { - SDL_Window *win = NULL; - int w = 1024, h = 512; - uint32_t pixels[2048]; - srand(time(NULL)); + SDL_Window *win = NULL; + int w = 1024, h = 512; + uint32_t pixels[2048]; + srand(time(NULL)); - if (argc != 2) - { - fprintf(stderr, "Usage: ./chip8 [ROM]\n"); - return EXIT_FAILURE; - } + if (argc != 2) + { + fprintf(stderr, "Usage: ./chip8 [ROM]\n"); + return EXIT_FAILURE; + } - if (SDL_Init(SDL_INIT_EVERYTHING) < 0) - { - fprintf(stderr, "Cannot initialize SDL. Exiting. . .\n"); - return EXIT_FAILURE; - } + if (SDL_Init(SDL_INIT_EVERYTHING) < 0) + { + fprintf(stderr, "Cannot initialize SDL. Exiting. . .\n"); + return EXIT_FAILURE; + } - win = SDL_CreateWindow("CHIP-8 Emulator", SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, w, h, - SDL_WINDOW_SHOWN); - if (win == NULL) - { - fprintf(stderr, "Cannot create SDL window. Exiting. . .\n%s\n", - SDL_GetError()); - return EXIT_FAILURE; - } + win = SDL_CreateWindow("CHIP-8 Emulator", SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, w, h, + SDL_WINDOW_SHOWN); + if (win == NULL) + { + fprintf(stderr, "Cannot create SDL window. Exiting. . .\n%s\n", + SDL_GetError()); + return EXIT_FAILURE; + } - SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, 0); - SDL_RenderSetLogicalSize(renderer, w, h); - SDL_Texture *texture = SDL_CreateTexture( - renderer, SDL_PIXELFORMAT_ARGB8888, - SDL_TEXTUREACCESS_STREAMING, 64, 32); + SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, 0); + SDL_RenderSetLogicalSize(renderer, w, h); + SDL_Texture *texture = SDL_CreateTexture( + renderer, SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, 64, 32); - Chip8 chip8; - chip8_init(&chip8); - if (!load(&chip8, argv[1])) return EXIT_FAILURE; + Chip8 chip8; + chip8_init(&chip8); + if (!load(&chip8, argv[1])) return EXIT_FAILURE; - for (;;) - { - SDL_Event e; - emulate(&chip8); - - int i; - while (SDL_PollEvent(&e)) - { - if (e.type == SDL_QUIT) return EXIT_FAILURE; - if (e.type == SDL_KEYDOWN) - { - if (e.key.keysym.sym == SDLK_ESCAPE) return EXIT_FAILURE; - for (i = 0; i < 16; i++) - if (e.key.keysym.sym == keymap[i]) - chip8.keys[i] = 1; - } + for (;;) + { + SDL_Event e; + emulate(&chip8); + + int i; + while (SDL_PollEvent(&e)) + { + if (e.type == SDL_QUIT) return EXIT_FAILURE; + if (e.type == SDL_KEYDOWN) + { + if (e.key.keysym.sym == SDLK_ESCAPE) return EXIT_FAILURE; + for (i = 0; i < 16; i++) + if (e.key.keysym.sym == keymap[i]) + chip8.keys[i] = 1; + } - if (e.type == SDL_KEYUP) - for (i = 0; i < 16; i++) - if (e.key.keysym.sym == keymap[i]) - chip8.keys[i] = 0; - } + if (e.type == SDL_KEYUP) + for (i = 0; i < 16; i++) + if (e.key.keysym.sym == keymap[i]) + chip8.keys[i] = 0; + } - if (chip8.drawflag) - { - chip8.drawflag = 0; - for (i = 0; i < 2048; i++) - { - uint8_t pixel = chip8.gfx[i]; - pixels[i] = (0x00FFFFFF * pixel) | 0xFF000000; - } - SDL_UpdateTexture(texture, NULL, pixels, 64 * sizeof(Uint32)); - SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, texture, NULL, NULL); - SDL_RenderPresent(renderer); - } - usleep(2000); - } + if (chip8.drawflag) + { + chip8.drawflag = 0; + for (i = 0; i < 2048; i++) + { + uint8_t pixel = chip8.gfx[i]; + pixels[i] = (0x00FFFFFF * pixel) | 0xFF000000; + } + SDL_UpdateTexture(texture, NULL, pixels, 64 * sizeof(Uint32)); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_RenderPresent(renderer); + } + usleep(1500); + } - SDL_DestroyTexture(texture); - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(win); - SDL_Quit(); - return EXIT_SUCCESS; + SDL_DestroyTexture(texture); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(win); + SDL_Quit(); + return EXIT_SUCCESS; }