2023-09-09 17:57:57 +00:00
|
|
|
#ifndef CHIP8_CHIP8_H
|
|
|
|
#define CHIP8_CHIP8_H
|
|
|
|
|
|
|
|
#include <string>
|
2023-09-09 18:20:10 +00:00
|
|
|
#include <stack>
|
|
|
|
#include "display.h"
|
2023-09-09 17:57:57 +00:00
|
|
|
|
|
|
|
class InstructionHandler;
|
|
|
|
|
2023-09-09 19:35:24 +00:00
|
|
|
/**
|
|
|
|
* The main Chip8 used to emulate/interpret the Chip8 program
|
|
|
|
*/
|
2023-09-09 17:57:57 +00:00
|
|
|
class Chip8 {
|
|
|
|
private:
|
2023-09-09 19:35:24 +00:00
|
|
|
/**
|
|
|
|
* The memory of the Chip8 program
|
|
|
|
*/
|
2023-09-09 17:57:57 +00:00
|
|
|
unsigned char memory[0xFFF] = {};
|
2023-09-09 19:35:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The 16 general purpose 8-bit registers, generally used to store memory addresses
|
|
|
|
*/
|
2023-09-09 17:57:57 +00:00
|
|
|
unsigned char v[0x10] = {};
|
2023-09-09 19:35:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stack of 16-bit values, used to store addresses the interpreter should return to when finished with a subroutine
|
|
|
|
*/
|
2023-09-09 18:20:10 +00:00
|
|
|
std::stack<unsigned short> stack{};
|
2023-09-09 19:35:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The program counter which holds the currently executing address
|
|
|
|
*/
|
2023-09-09 17:57:57 +00:00
|
|
|
unsigned short pc;
|
2023-09-09 19:35:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Display object holding information and functions used for the display
|
|
|
|
*/
|
2023-09-09 18:20:10 +00:00
|
|
|
Display display;
|
2023-09-09 19:35:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The instruction handler used to handle instructions
|
|
|
|
*/
|
2023-09-09 17:57:57 +00:00
|
|
|
InstructionHandler* instructionHandler;
|
|
|
|
public:
|
|
|
|
Chip8();
|
|
|
|
~Chip8();
|
2023-09-09 19:35:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a rom in the memory
|
|
|
|
* @param rom The rom to be loaded in the memory
|
|
|
|
*/
|
|
|
|
void loadRom(const std::string& rom);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the Display object
|
|
|
|
* @return
|
|
|
|
*/
|
2023-09-09 18:20:10 +00:00
|
|
|
[[nodiscard]] Display getDisplay() const;
|
2023-09-09 19:35:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cycle to handle the next instruction
|
|
|
|
*/
|
2023-09-09 17:57:57 +00:00
|
|
|
void emulateCycle();
|
2023-09-09 19:35:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pops a 16-bit value from the stack
|
|
|
|
* @return a unsigned 16-bit integer
|
|
|
|
*/
|
2023-09-09 18:20:10 +00:00
|
|
|
[[nodiscard]] unsigned short popFromStack();
|
2023-09-09 19:35:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the program counter (PC) to the given value
|
|
|
|
*/
|
|
|
|
void setProgramCounter(unsigned short value);
|
2023-09-09 17:57:57 +00:00
|
|
|
};
|
|
|
|
#endif //CHIP8_CHIP8_H
|