diff --git a/include/chip8.h b/include/chip8.h index f5e984f..a103e7e 100644 --- a/include/chip8.h +++ b/include/chip8.h @@ -33,7 +33,6 @@ int font[FONT_SIZE][SPRITE_SIZE] = { {0xF0, 0x80, 0xF0, 0x80, 0x80}, // F }; -typedef uint16_t* stack; typedef struct Chip8{ uint8_t RAM[MEM_SIZE]; @@ -43,8 +42,11 @@ typedef struct Chip8{ uint8_t sound_timer; uint16_t I; uint16_t PC; - uint16_t SP; - stack chip8_stack; + int SP; + uint16_t* chip8_stack; }Chip8; +void push(Chip8* chip8, uint16_t value); +uint16_t pop(Chip8* chip8); + #endif // __CHIP8_H__ \ No newline at end of file diff --git a/include/instructions.h b/include/instructions.h index c5786fc..6f22578 100644 --- a/include/instructions.h +++ b/include/instructions.h @@ -24,7 +24,7 @@ extern void __9XY0(Chip8 *chip8, uint8_t x, uint8_t y); extern void __ANNN(Chip8 *chip8, uint8_t nnn); extern void __BNNN(Chip8 *chip8, uint8_t nnn); - extern void __CXNN(Chip8 *chip8, uint8_t nn); + extern void __CXNN(Chip8 *chip8, uint8_t x, uint8_t nn); extern void __DXYN(Chip8 *chip8, uint8_t x, uint8_t y, uint8_t n); extern void __EX9E(Chip8 *chip8, uint8_t x); extern void __EXA1(Chip8 *chip8, uint8_t x); diff --git a/src/chip8.c b/src/chip8.c index 640c4b1..53014b4 100644 --- a/src/chip8.c +++ b/src/chip8.c @@ -12,11 +12,25 @@ Chip8 *init_cpu() chip8->sound_timer = 0x00; chip8->PC = PROGRAM_ADDRESS; - chip8->chip8_stack = (stack)malloc(STACK_SIZE*sizeof(stack)); + chip8->chip8_stack = (uint16_t*)malloc(STACK_SIZE*sizeof(uint16_t)); return chip8; } +void push(Chip8* chip8, uint16_t value) +{ + chip8->chip8_stack[chip8->SP++] = value; +} + +uint16_t pop(Chip8* chip8) +{ + if(chip8->PC > 0){ + return chip8->chip8_stack[--chip8->SP]; + }else{ + perror("Stack is empty!"); + } +} + void start_cpu(Chip8 *chip8, uint8_t instructions[], int size) { int ram_index = 0; @@ -69,7 +83,6 @@ void run(Chip8 *chip8, uint16_t instruction) default: panic("Invalid Instruction!"); - break; } break; @@ -136,7 +149,6 @@ void run(Chip8 *chip8, uint16_t instruction) default: panic("Invalid Instruction!"); - break; } break; @@ -168,7 +180,6 @@ void run(Chip8 *chip8, uint16_t instruction) default: panic("Invalid Instruction!"); - break; } break; @@ -204,11 +215,13 @@ void run(Chip8 *chip8, uint16_t instruction) break; default: - panic("Invalid Instruction!") - break; + panic("Invalid Instruction!"); } - + break; + + default: + panic("Invalid Instruction!"); } } \ No newline at end of file diff --git a/src/instructions.c b/src/instructions.c index 7ff3831..45e287b 100644 --- a/src/instructions.c +++ b/src/instructions.c @@ -1,5 +1,8 @@ #include "../include/instructions.h" +#include +#include + void __00E0(Chip8 *chip8) { for(int y = 0; y < DISPLAY_HEIGHT; y++) @@ -13,107 +16,115 @@ void __00E0(Chip8 *chip8) void __00EE(Chip8 *chip8) { - + chip8->PC = pop(chip8); } void __1NNN(Chip8 *chip8, uint8_t nnn) { - + chip8->PC = nnn; } void __2NNN(Chip8 *chip8, uint8_t nnn) { - + push(chip8, chip8->PC); + chip8->PC = nnn; } void __3XNN(Chip8 *chip8, uint8_t x, uint8_t nn) { - + if(chip8->V[x] == nn){ chip8->PC += 2; } } void __4XNN(Chip8 *chip8, uint8_t x, uint8_t nn) { - + if(chip8->V[x] != nn){ chip8->PC += 2; } } void __5XY0(Chip8 *chip8, uint8_t x, uint8_t y) { - + if(chip8->V[x] == chip8->V[y]){ chip8->PC += 2; } } void __6XNN(Chip8 *chip8, uint8_t x, uint8_t nn) { - + chip8->V[x] = nn; } void __7XNN(Chip8 *chip8, uint8_t x, uint8_t nn) { - + chip8->V[x] += nn; } void __8XY0(Chip8 *chip8, uint8_t x, uint8_t y) { - + chip8->V[x] = chip8->V[y]; } void __8XY1(Chip8 *chip8, uint8_t x, uint8_t y) { - + chip8->V[x] |= chip8->V[y]; } void __8XY2(Chip8 *chip8, uint8_t x, uint8_t y) { - + chip8->V[x] &= chip8->V[y]; } void __8XY3(Chip8 *chip8, uint8_t x, uint8_t y) { - + chip8->V[x] ^= chip8->V[y]; } void __8XY4(Chip8 *chip8, uint8_t x, uint8_t y) { - + chip8->V[x] += chip8->V[y]; } void __8XY5(Chip8 *chip8, uint8_t x, uint8_t y) { - + chip8->V[x] -= chip8->V[y]; } void __8XY6(Chip8 *chip8, uint8_t x, uint8_t y) { - + chip8->V[x] = chip8->V[x] >> 1; } void __8XY7(Chip8 *chip8, uint8_t x, uint8_t y) { + chip8->V[x] = chip8->V[y] - chip8->V[x]; + if(chip8->V[y] >= chip8->V[x]) + { + chip8->V[F] = 1; + }else{ + chip8->V[F] = 0; + } } void __8XYE(Chip8 *chip8, uint8_t x, uint8_t y) { - + chip8->V[x] = chip8->V[x] >> 1; } void __9XY0(Chip8 *chip8, uint8_t x, uint8_t y) { - + if(chip8->V[x] != chip8->V[y]){ chip8->PC += 2; } } void __ANNN(Chip8 *chip8, uint8_t nnn) { - + chip8->I = nnn; } void __BNNN(Chip8 *chip8, uint8_t nnn) { - + chip8->PC = chip8->V[0] + nnn; } -void __CXNN(Chip8 *chip8, uint8_t nn) +void __CXNN(Chip8 *chip8, uint8_t x, uint8_t nn) { - + chip8->V[x] = (rand() % 255) & nn; } void __DXYN(Chip8 *chip8, uint8_t x, uint8_t y, uint8_t n) @@ -153,7 +164,7 @@ void __FX18(Chip8 *chip8, uint8_t x) void __FX1E(Chip8 *chip8, uint8_t x) { - + chip8->I = chip8->V[x]; } void __FX29(Chip8 *chip8, uint8_t x) diff --git a/src/main.c b/src/main.c index e69de29..23e468c 100644 --- a/src/main.c +++ b/src/main.c @@ -0,0 +1,50 @@ +#include "../include/chip8.h" +#include +#include +#include +#include + +noreturn void get_graphics() +{ + while(true) + { + + } +} + +uint8_t load_rom(char* rom_path) +{ + FILE *file; + char* buffer; + long file_size; + + file = fopen(rom_path,"rb"); + + if(file == NULL) + { + perror("File can not be opened!"); + return 1; + } + + fseek(file, 0, SEEK_END); + file_size = ftell(file); + rewind(file); + + buffer = (char*)malloc(file_size*sizeof(char*)); + + size_t bytes_read = fread(buffer, 1, file_size, file); +} + +int main(int argc, char** argv) +{ + + if(argc > 1) + { + load_rom(argv[1]); + }else + { + perror("ROM not specified!"); + } + + return 0; +} \ No newline at end of file