You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CHIP8Emulator/main.cpp

236 lines
5.3 KiB

2 years ago
#include "pixelGameEngine.h"
#include "Polygon.h"
#include <ios>
#include <stack>
#include <bitset>
2 years ago
using namespace olc;
#define WIDTH 640
#define HEIGHT 480
class Chip8Emulator : public olc::PixelGameEngine
2 years ago
{
public:
Chip8Emulator()
2 years ago
{
sAppName = "CHIP-8!";
2 years ago
}
public:
std::array<uint8_t,4096>memory={
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
//Font starts at 0x50
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
};
std::array<bool,2048>display;
std::stack<uint8_t>stack;
float pulse=0;
uint8_t delay_timer,sound_timer;
uint16_t pc=0x200;
uint16_t index; //One 16-bit index register called “I” which is used to point at locations in memory
std::array<uint8_t,16>reg;
std::string Display8(int number){
std::bitset<8>numb(number);
return numb.to_string();
}
std::string Display16(int number){
std::bitset<16>numb(number);
return numb.to_string();
}
2 years ago
bool OnUserCreate() override
{
for (int i=0;i<display.size();i++){
display[i]=0;
}
std::ifstream file("test_opcode.ch8",std::ios_base::binary);
uint16_t counter=0x200;
while (file.good()){
int val = file.get();
if (val!=-1){
memory[counter++]=val;
}
}
/*memory[0x200]=0x00;
memory[0x201]=0xE0;
memory[0x202]=0xA0;
memory[0x203]=0x50;
memory[0x204]=0x60;
memory[0x205]=0x04;
memory[0x206]=0xD0;
memory[0x207]=0x05;
memory[0x208]=0xD0;
memory[0x209]=0x05;
memory[0x210]=0x12;
memory[0x211]=0x10;*/
//0x200 program start
2 years ago
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
pulse+=fElapsedTime;
if (pulse>=1/60.f){
if (delay_timer>0){
delay_timer--;
}
if (sound_timer>0){
sound_timer--;
}
pulse-=1/60.f;
for (int i=0;i<10;i++){
//FETCH
uint16_t opcode = memory[pc]<<8|memory[pc+1];
pc+=2;
//std::cout<<"Next instruction is 0x"<<std::hex<<pc<<std::endl;
//DECODE
uint8_t nibble1 = opcode>>12;
uint8_t X = opcode>>8&0xF;
uint8_t Y = opcode>>4&0xF;
uint8_t N = opcode&0xF;
uint8_t NN = opcode&0x00FF;
uint16_t NNN = opcode&0x0FFF;
//std::cout<<"Opcode 0x"<<std::hex<<opcode<<std::dec<<": "<<Display8(nibble1)<<", "<<Display8(X)<<"/"<<Display8(Y)<<"/"<<Display8(N)<<"/"<<Display8(NN)<<"/"<<Display16(NNN)<<std::endl;
switch(nibble1){
case 0x0:{
switch (NNN){
case 0x0E0:{ //Clear screen.
for (int i=0;i<display.size();i++){
display[i]=0;
}
}break;
case 0x0EE:{ //Return from subroutine.
uint8_t byte1,byte2;
byte1=stack.top();
stack.pop();
byte2=stack.top();
stack.pop();
pc=byte2<<8|byte1;
//std::cout<<"Exiting subroutine to 0x"<<std::hex<<pc<<std::endl;
}break;
}
}break;
case 0x1:{ //Jump.
pc=NNN;
}break;
case 0x2:{ //calls the subroutine at memory location NNN
stack.push(pc>>8);
stack.push(pc&0xFF);
//std::cout<<"Pushed 0x"<<std::hex<<pc<<" onto stack"<<std::endl;
pc=NNN;
//std::cout<<"Entering subroutine @ 0x"<<std::hex<<NNN<<std::endl;
}break;
case 0x3:{
}break;
case 0x4:{
}break;
case 0x5:{
}break;
case 0x6:{ //Set the register X to NN.
reg[X]=NN;
//std::cout<<(int)reg[X]<<std::endl;
}break;
case 0x7:{ //Add w/no carry
reg[X]+=NN;
//std::cout<<(int)reg[X]<<std::endl;
}break;
case 0x8:{
}break;
case 0x9:{
}break;
case 0xA:{ //This sets the index register I to the value NNN.
index=NNN;
}break;
case 0xB:{
}break;
case 0xC:{
}break;
case 0xD:{ //Display
uint8_t start_X=reg[X]%ScreenWidth();
uint8_t start_Y=reg[Y]%ScreenHeight();
reg[0xF]=0;
for (uint8_t y=0;y<N;y++){
uint16_t spriteRow=memory[index+y];
//std::cout<<spriteRow<<std::endl;
for (uint8_t x=0;x<8;x++){
if (start_X+x<ScreenWidth()&&start_Y+y<ScreenHeight()){
//Memory loc: 0x50
//0xF0,0x90,0x90,0x90,0xF0
bool pixel = spriteRow>>(8-x-1)&0x1;
if (display[(start_Y+y)*ScreenWidth()+(start_X+x)]&&pixel){
//std::cout<<"Double on."<<std::endl;
reg[0xF]=0;
}
display[(start_Y+y)*ScreenWidth()+(start_X+x)]=display[(start_Y+y)*ScreenWidth()+(start_X+x)]^pixel;
}
}
}
}break;
case 0xE:{
}break;
case 0xF:{
}break;
}
}
Clear(VERY_DARK_BLUE);
for (int x=0;x<ScreenWidth();x++){
for (int y=0;y<ScreenHeight();y++){
Draw({x,y},display[y*ScreenWidth()+x]?WHITE:BLACK);
}
}
}
2 years ago
return true;
}
bool OnUserDestroy()override{
return true;
}
};
int main()
{
Chip8Emulator demo;
if (demo.Construct(64, 32, 10, 10))
2 years ago
demo.Start();
return 0;
}