Implementing part of instructions

This commit is contained in:
Vinicius Silva 2024-03-03 22:45:33 -03:00
parent 545d70c6b1
commit 9ae2ca642f
5 changed files with 109 additions and 33 deletions

View File

@ -33,7 +33,6 @@ int font[FONT_SIZE][SPRITE_SIZE] = {
{0xF0, 0x80, 0xF0, 0x80, 0x80}, // F {0xF0, 0x80, 0xF0, 0x80, 0x80}, // F
}; };
typedef uint16_t* stack;
typedef struct Chip8{ typedef struct Chip8{
uint8_t RAM[MEM_SIZE]; uint8_t RAM[MEM_SIZE];
@ -43,8 +42,11 @@ typedef struct Chip8{
uint8_t sound_timer; uint8_t sound_timer;
uint16_t I; uint16_t I;
uint16_t PC; uint16_t PC;
uint16_t SP; int SP;
stack chip8_stack; uint16_t* chip8_stack;
}Chip8; }Chip8;
void push(Chip8* chip8, uint16_t value);
uint16_t pop(Chip8* chip8);
#endif // __CHIP8_H__ #endif // __CHIP8_H__

View File

@ -24,7 +24,7 @@
extern void __9XY0(Chip8 *chip8, uint8_t x, uint8_t y); extern void __9XY0(Chip8 *chip8, uint8_t x, uint8_t y);
extern void __ANNN(Chip8 *chip8, uint8_t nnn); extern void __ANNN(Chip8 *chip8, uint8_t nnn);
extern void __BNNN(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 __DXYN(Chip8 *chip8, uint8_t x, uint8_t y, uint8_t n);
extern void __EX9E(Chip8 *chip8, uint8_t x); extern void __EX9E(Chip8 *chip8, uint8_t x);
extern void __EXA1(Chip8 *chip8, uint8_t x); extern void __EXA1(Chip8 *chip8, uint8_t x);

View File

@ -12,11 +12,25 @@ Chip8 *init_cpu()
chip8->sound_timer = 0x00; chip8->sound_timer = 0x00;
chip8->PC = PROGRAM_ADDRESS; 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; 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) void start_cpu(Chip8 *chip8, uint8_t instructions[], int size)
{ {
int ram_index = 0; int ram_index = 0;
@ -69,7 +83,6 @@ void run(Chip8 *chip8, uint16_t instruction)
default: default:
panic("Invalid Instruction!"); panic("Invalid Instruction!");
break;
} }
break; break;
@ -136,7 +149,6 @@ void run(Chip8 *chip8, uint16_t instruction)
default: default:
panic("Invalid Instruction!"); panic("Invalid Instruction!");
break;
} }
break; break;
@ -168,7 +180,6 @@ void run(Chip8 *chip8, uint16_t instruction)
default: default:
panic("Invalid Instruction!"); panic("Invalid Instruction!");
break;
} }
break; break;
@ -204,11 +215,13 @@ void run(Chip8 *chip8, uint16_t instruction)
break; break;
default: default:
panic("Invalid Instruction!") panic("Invalid Instruction!");
break;
} }
break; break;
default:
panic("Invalid Instruction!");
} }
} }

View File

@ -1,5 +1,8 @@
#include "../include/instructions.h" #include "../include/instructions.h"
#include <stdint.h>
#include<stdlib.h>
void __00E0(Chip8 *chip8) void __00E0(Chip8 *chip8)
{ {
for(int y = 0; y < DISPLAY_HEIGHT; y++) for(int y = 0; y < DISPLAY_HEIGHT; y++)
@ -13,107 +16,115 @@ void __00E0(Chip8 *chip8)
void __00EE(Chip8 *chip8) void __00EE(Chip8 *chip8)
{ {
chip8->PC = pop(chip8);
} }
void __1NNN(Chip8 *chip8, uint8_t nnn) void __1NNN(Chip8 *chip8, uint8_t nnn)
{ {
chip8->PC = nnn;
} }
void __2NNN(Chip8 *chip8, uint8_t 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) 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) 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) 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) void __6XNN(Chip8 *chip8, uint8_t x, uint8_t nn)
{ {
chip8->V[x] = nn;
} }
void __7XNN(Chip8 *chip8, uint8_t x, uint8_t 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) void __ANNN(Chip8 *chip8, uint8_t nnn)
{ {
chip8->I = nnn;
} }
void __BNNN(Chip8 *chip8, uint8_t 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) 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) void __FX1E(Chip8 *chip8, uint8_t x)
{ {
chip8->I = chip8->V[x];
} }
void __FX29(Chip8 *chip8, uint8_t x) void __FX29(Chip8 *chip8, uint8_t x)

View File

@ -0,0 +1,50 @@
#include "../include/chip8.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <stdbool.h>
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;
}