Last active
September 15, 2019 10:59
-
-
Save aneury1/8eda20b624a96f218e89db97f81ffd1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include <iostream> | |
#include <stdio.h> | |
#include <map> | |
using namespace std; | |
constexpr int MEMORY_SIZE=0xFFF; | |
struct Chip8 | |
{ | |
unsigned char memory[MEMORY_SIZE]; | |
bool loaded_rom; | |
unsigned char Vx[16]; | |
unsigned short I; | |
unsigned char dt; | |
unsigned char st; | |
short PC; | |
unsigned char stack; | |
unsigned char screen[32*64]; | |
}; | |
namespace{ | |
typedef void (*registry_function)(Chip8 &chip8, unsigned short current); | |
map<unsigned char, registry_function> registry_functions; | |
}; | |
/* | |
nnn or addr - A 12-bit value, the lowest 12 bits of the instruction | |
n or nibble - A 4-bit value, the lowest 4 bits of the instruction | |
x - A 4-bit value, the lower 4 bits of the high byte of the instruction | |
y - A 4-bit value, the upper 4 bits of the low byte of the instruction | |
kk or byte - An 8-bit value, the lowest 8 bits of the instruction | |
*/ | |
#define EXTRACT_NNN(value) value & 0x0FFF | |
#define EXTRACT_N(value) value & 0x000F | |
#define EXTRACT_X(value) (value>>8)&0x0F | |
#define EXTRACT_Y(value) (value>>12)&0x0F | |
#define EXTRACT_KK(value) (value)&0xFF | |
void ShowHexBuffer(const char *tag, unsigned char *buffer, int length) | |
{ | |
int cols=0; | |
int line_number=0; | |
printf("%s:\n", tag); | |
for(int i=0;i< length;i++) | |
{ | |
if(cols==0) | |
printf("%04d ", line_number); | |
printf("%02x ", buffer[i]); | |
cols++; | |
if(cols==16) | |
{ | |
for(int j=i;j<i+16;j++) | |
{ | |
printf("%c", (buffer[i]<31)?'0':buffer[i] ); | |
} | |
++line_number; | |
printf("\n"); | |
cols =0; | |
} | |
} | |
} | |
void ReadROM(Chip8 &ref, const char *filename) | |
{ | |
fstream stream(filename, ios::in|ios::binary); | |
if(stream.is_open()) | |
{ | |
int length =0; | |
stream.seekg(0 , ios::end); | |
length = stream.tellg(); | |
stream.seekg(0, ios::beg); | |
cout <<"File Length "<< length <<"\n"; | |
stream.read((char *)&ref.memory[0x200], length);///block of rom start at 512 byte, before that address is reserved for the mach. | |
ShowHexBuffer("ROM DUMP", ref.memory, length); | |
ref.loaded_rom=true; | |
stream.close(); | |
} | |
else | |
{ | |
cout <<"ROM Can't be opened\n"; | |
} | |
} | |
void Annn(Chip8 &ref, unsigned short opcode) | |
{ | |
} | |
int main(int argc, char *argv[]) | |
{ | |
Chip8 emulate; | |
ReadROM(emulate,"Breakout.ch8"); | |
emulate.PC = 0; | |
///while(emulate.loaded_rom) | |
{ | |
unsigned short current_instrucction = (emulate.memory[emulate.PC]<<8|emulate.memory[emulate.PC+1]); | |
/// printf("%04x\n", current_instrucction); | |
printf("\n\n\nValue %02x\n",EXTRACT_Y(current_instrucction)); | |
printf("\n\n\nValue NNN %04x\n",EXTRACT_NNN(current_instrucction)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment