Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "chip8.h"
- #include <fstream>
- //#include "Windows.h"
- unsigned char fontset[80] = {
- 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
- };
- Chip8::Chip8()
- {
- Initialise();
- }
- Chip8::~Chip8()
- {
- }
- /*
- The systems memory map :
- 0x000 - 0x1FF - Chip 8 interpreter(contains font set in emu)
- 0x050 - 0x0A0 - Used for the built in 4x5 pixel font set(0 - F)
- 0x200 - 0xFFF - Program ROM and work RAM
- */
- void Chip8::Initialise()
- {
- // Initialse registers and memory
- for (int i = 0; i < 4096; ++i)
- {
- //set the fontset at the start of the memory
- if (i < 80)
- {
- memory[i] = fontset[i];
- }
- else
- {
- memory[i] = 0;
- }
- }
- for (int i = 0; i < 16; ++i)
- {
- Reg[i] = 0;
- }
- regPc = 0x200; // program starts from this position
- delay_timer = 0;
- sound_timer = 0;
- opcode = 0;
- regI = 0;
- sp = 0;
- m_ScreenRefresh = true;
- EmptyScreen();
- for (int i = 0; i < 16; ++i)
- {
- stack[i] = 0;
- }
- for (unsigned int i = 0; i < 16; i++) {
- key[i] = false;
- }
- //todo: fill 0x000 - 0x1ff with the needed information (like font-types)
- }
- void Chip8::LoadGame(std::string gameName)
- {
- //load game into memory
- Initialise();
- //load the data from the file in the memory starting from 0x200
- U16 startPos = 0x200;
- std::ifstream romStream;
- //romStream.open("c8_Games\\" + gameName, std::ios::binary);
- romStream.open(gameName, std::ios::binary);
- romStream.seekg(0, std::ios::end);// goes to end of the file
- int size = romStream.tellg();
- romStream.seekg(0, std::ios::beg);//goes back to beginning of the file
- char *rom = NULL;
- //if the file size is too large, error
- if (romStream.is_open())
- {
- if (startPos + size >= 4096)
- {
- //OutputDebugString(L"-----------File too large for memory-----------\n");
- }
- else
- {
- //else add to tempmemory
- rom = new char[size + 1];
- romStream.read(rom, size);
- }
- }
- else
- {
- //OutputDebugString(L"-----------Could not open file-----------\n");
- }
- romStream.close();
- //add tempmemory to memory
- if (rom != NULL)
- {
- for (U16 i = startPos; i < startPos + size; ++i)
- {
- memory[i] = (unsigned char)rom[i - startPos];
- }
- delete [] rom;
- rom = NULL;
- }
- }
- void Chip8::NextPc()
- {
- regPc += 2;
- }
- void Chip8::EmulateCycle()
- {
- //fetch opcode
- opcode = memory[regPc] << 8 | memory[regPc + 1];
- std::cout << std::hex << opcode << std::endl;
- //decode opcode
- switch (opcode & 0xF000) // takes the first byte of the opcode
- {
- case 0x0000:
- //std::cout << "0x0000" << std::endl;
- switch (opcode & 0x000F) // last bytes
- {
- //skipped 0x0NNN!
- case 0x0000:
- //clears the screen
- EmptyScreen();
- m_ScreenRefresh = true;
- NextPc();
- break;
- case 0x000E:
- //returns from a subroutine
- regPc = stack[--sp];
- NextPc();
- break;
- //default:
- //unknown opcode;
- //OutputDebugString(L"Unknown opcode [0]");
- }
- break;
- case 0x1000:
- //std::cout << "0x1000" << std::endl;
- //jumps to adress NNN
- regPc = opcode & 0x0FFF;
- break;
- case 0x2000:
- //std::cout << "0x2000" << std::endl;
- //calls subroutine at NNN
- //we are temporarely jumping to another address(NNN) so we must store te current address in the stack;
- stack[sp] = regPc;
- ++sp; // increase stackCounter to avoid overwriting
- //jump to address
- regPc = opcode & 0x0FFF;
- break;
- case 0x3000:
- //std::cout << "0x3000" << std::endl;
- //skips the next instruction if VX equals NN
- if (Reg[(opcode & 0x0F00) >> 8] == (opcode & 0x00FF))
- {
- NextPc();
- }
- NextPc();
- break;
- case 0x4000:
- //std::cout << "0x4000" << std::endl;
- //skips the next instruction if VX doesn't equal NN
- if (Reg[(opcode & 0x0F00) >> 8] != (opcode & 0x00FF))
- {
- NextPc();
- }
- NextPc();
- break;
- case 0x5000:
- //std::cout << "0x5000" << std::endl;
- //skips the next instruction if VX equals VY
- if (Reg[(opcode & 0x0F00) >> 8] == Reg[(opcode & 0x00F0) >> 4])
- {
- NextPc();
- }
- NextPc();
- break;
- case 0x6000:
- //Sets VX to NN
- //std::cout << "0x6000" << std::endl;
- Reg[(opcode & 0x0F00) >> 8] = opcode & 0x00FF;
- NextPc();
- break;
- case 0x7000:
- //Adds NN to VX
- //std::cout << "0x7000" << std::endl;
- Reg[(opcode & 0x0F00) >> 8] += opcode & 0x00FF;
- NextPc();
- break;
- case 0x8000:// I think something is going wrong here
- //std::cout << "0x8000" << std::endl;
- switch (opcode & 0x000F)
- {
- case 0x0000:
- //Sets VX to te value of VY
- Reg[(opcode & 0x0F00) >> 8] = Reg[(opcode & 0x00F0) >> 4];
- NextPc();
- break;
- case 0x0001:
- //Sets VX to VX or VY
- Reg[(opcode & 0x0F00) >> 8] |= Reg[(opcode & 0x00F0) >> 4];
- NextPc();
- break;
- case 0x0002:
- //Sets VX to VX and VY
- Reg[(opcode & 0x0F00) >> 8] &= Reg[(opcode & 0x00F0) >> 4];
- NextPc();
- break;
- case 0x0003:
- //Sets VX to VX xor VY
- Reg[(opcode & 0x0F00) >> 8] ^= Reg[(opcode & 0x00F0) >> 4];
- NextPc();
- break;
- case 0x0004: // CHECK
- /*
- if the sum of VX and VY is larger than 255, it canβt be stored in the register (or actually it starts counting from 0 again).
- If the sum of VX and VY is larger than 255, we use the carry flag to let the system know that the total sum of both values was indeed larger than 255(0xFF).
- */
- //Adds VY to VX. VF is set to 1 when there's a carry, and to 0 when there isn't
- //Reg[(opcode & 0x0F00) >> 8] += Reg[(opcode & 0x00F0) >> 4];
- //if (Reg[(opcode & 0x00F0) >> 4] > (0xFF - Reg[(opcode & 0x0F00) >> 8]))
- //{
- // Reg[0xF] = 1; // collision
- //}
- //else
- //{
- // Reg[0xF] = 0; // no collision
- //}
- if (Reg[(opcode & 0x00F0) >> 4] > (0xFF - Reg[(opcode & 0x0F00) >> 8]))
- {
- Reg[0xF] = 1;
- }
- else
- {
- Reg[0xF] = 0;
- }
- Reg[(opcode & 0x0F00) >> 8] += Reg[(opcode & 0x00F0) >> 4];
- NextPc();
- break;
- case 0x0005:
- //VY is substracted from VX. VF is set to 0 when there's a borrow, and 1 when there isn't
- if (Reg[(opcode & 0x00F0) >> 4] > (Reg[(opcode & 0x0F00) >> 8]))
- {
- Reg[0xF] = 0;
- }
- else
- {
- Reg[0xF] = 1;
- }
- Reg[(opcode & 0x0F00) >> 8] -= Reg[(opcode & 0x00F0) >> 4];
- NextPc();
- break;
- case 0x0006:
- //Shifts VX right by one. VF is set to the value of the least significant bit of VX before the shifts
- Reg[0xF] = Reg[(opcode & 0x0F00) >> 8] & 0x1;
- Reg[(opcode & 0x0F00) >> 8] >>= 1;
- NextPc();
- break;
- case 0x0007:
- //Sets VX to VY minus VX. VF is set to 0 when there's a borrow, and 1 when there isn't
- if (Reg[(opcode & 0x0F00) >> 8] > Reg[(opcode & 0x00F0) >> 4])
- {
- Reg[0xF] = 0;
- }
- else
- {
- Reg[0xF] = 1;
- }
- Reg[(opcode & 0x0F00) >> 8] = (Reg[(opcode & 0x00F0) >> 4] - Reg[(opcode & 0x0F00) >> 8]);
- NextPc();
- break;
- case 0x000E:
- //Shifts VX left by one. VF is set to the value of the most significant bit of VX before the shift
- Reg[0xF] = Reg[(opcode & 0x0F00) >> 8] >> 7;
- Reg[(opcode & 0x0F00) >> 8] <<= 1;
- NextPc();
- break;
- default:
- //unknown opcode
- //OutputDebugString(L"Unknown opcode [8]");
- break;
- }
- break;
- case 0x9000:
- //std::cout << "0x9000" << std::endl;
- //Skips the next instruction if VX doesn't equal VY
- if (Reg[(opcode & 0x0F00) >> 8] != Reg[(opcode & 0x00F0) >> 4])
- {
- NextPc();
- }
- NextPc();
- break;
- case 0xA000:
- //Sets I to the address NNN
- //std::cout << "0xA000" << std::endl;
- regI = opcode & 0x0FFF;
- NextPc(); //next memoryspace;
- break;
- case 0xB000:
- //std::cout << "0xB000" << std::endl;
- //Jumps to the adress NNN plus V0
- regPc = (opcode & 0x0FFF) + Reg[0];
- break;
- case 0xC000:
- //Sets VX to a random number(between 0 - 255) and NN
- //test = (opcode & 0x0F00);
- //test2 = (opcode & 0x0F00) >> 8;
- //std::cout << "0xC000" << std::endl;
- //Reg[(opcode & 0x0F00)] = (rand() & 0xFF) & (opcode & 0x00FF); // didn't do anything
- Reg[(opcode & 0x0F00) >> 8] = (rand() % 0xFF) & (opcode & 0x00FF);
- NextPc(); //next memoryspace;
- break;
- case 0xD000:
- {
- //std::cout << "0xD000" << std::endl;
- /*
- Sprites stored in memory at location in index register (I), 8bits wide. Wraps around the screen.
- If when drawn, clears a pixel, register VF is set to 1 otherwise it is zero. All drawing is XOR drawing (i.e. it toggles the screen pixels).
- Sprites are drawn starting at position VX, VY. N is the number of 8bit rows that need to be drawn. If N is greater than 1, second line continues at position VX, VY+1, and so on.
- */
- //get position and height of the sprite
- U16 xPos = Reg[(opcode & 0x0F00) >> 8];
- U16 yPos = Reg[(opcode & 0x00F0) >> 4];
- U16 height = opcode & 0x000F;//amount of rows
- U16 pixelValue;
- Reg[0xF] = 0;//reset register F
- for (unsigned int row = 0; row < height; ++row)//loop over each row
- {
- pixelValue = memory[regI + row];//fetch pixel value
- //loop over the bits of 1 row(8 bit)
- for (unsigned int bit = 0; bit < 8; ++bit)
- {
- //check if the current evaluated bit is set to 1
- if ((pixelValue & (0x80 >> bit)) != 0)
- {
- //its 1
- //check if pixel on the screen is set to 1, if it is we need to register the collision by setting the VF register
- if (gfx[(xPos + bit + ((yPos + row) * SCREEN_WIDTH))] == 1)
- {
- Reg[0xF] = 1;
- }
- //set pixel value by using XOR (only if on bit is 1 and the other is 0 you will get 1 otherwise 0)
- gfx[(xPos + bit + ((yPos + row) * SCREEN_WIDTH))] ^= 1;
- }
- }
- }
- //set Draw screen
- m_ScreenRefresh = true;
- NextPc();
- break;
- }
- case 0xE000:
- //std::cout << "0xE000" << std::endl;
- switch (opcode & 0x00FF)
- {
- case 0x009E:
- //Skips the next instruction if the key stored in VX is pressed
- if (key[Reg[(opcode & 0x0F00) >> 8]])
- {
- NextPc();
- }
- NextPc();
- break;
- case 0x00A1:
- //Skips the next instruction if the key stored in VX isn't pressed
- if (!key[Reg[(opcode & 0x0F00) >> 8]])
- {
- NextPc();
- }
- NextPc();
- break;
- default:
- //unknown opcode
- //OutputDebugString(L"Unknown opcode [E]");
- break;
- }
- break;
- case 0xF000:
- //std::cout << "0xF000" << std::endl;
- switch (opcode & 0x00FF)
- {
- case 0x0007:
- //Sets VX to the value of the delay timer.
- Reg[(opcode & 0x0F00) >> 8] = delay_timer;
- NextPc();
- break;
- case 0x000A:
- //A key pressed is awaited and then stored in VX TODO
- {
- bool keyIsPressed = false;
- for (int i = 0; i < 16; ++i)
- {
- if (key[i])
- {
- keyIsPressed = true;
- Reg[(opcode & 0x0F00) >> 8] = i;
- break;
- }
- }
- //if you don't have a keypress don't do NextPC
- if (keyIsPressed)
- NextPc();
- break;
- }
- case 0x0015:
- //Sets the delay timer to VX
- delay_timer = Reg[(opcode & 0x0F00) >> 8];
- NextPc();
- break;
- case 0x0018:
- //Sets the sound timer to VX
- sound_timer = Reg[(opcode & 0x0F00) >> 8];
- NextPc();
- break;
- case 0x001E:
- //Adds VX to I
- regI += Reg[(opcode & 0x0F00) >> 8];
- NextPc();
- break;
- case 0x0029:
- //Sets I to the location of the sprite for the character in VX. Character 0-F are represented by a 4x5 font
- regI = Reg[(opcode & 0x0F00) >> 8] * 5; //location of sprite in memory (each character has 5 spots of te memory)
- NextPc();
- break;
- case 0x0033:
- /*
- Stores the Binary-coded decimal representation of VX, with the most significant of three digits at the address in I, the middle digit at I plus 1, and the least significant digit at I plus 2.
- (In other words, take the decimal representation of VX, place the hundreds digit in memory at location in I, the tens digit at location I+1, and the ones digit at location I+2.)
- */
- //store decimal representation of VX in memory at address regI, regI +1 and regI+2 (hunreds digit at regI)
- memory[regI] = Reg[(opcode & 0x0F00) >> 8] /100;// example: (160 / 100) = 1;
- //tens digits in regI +1
- memory[regI + 1] = (Reg[(opcode & 0x0F00) >> 8] / 10) % 10; // example: (160/10) = 16 % 10 = 6;
- //one digits at regI +2
- memory[regI + 2] = (Reg[(opcode & 0X0F00) >> 8] % 100) % 10;// example: (160 % 100) = 60 % 10 = 0;
- NextPc();
- break;
- case 0x0055:
- //Stores V0 to VX in memory starting at address I
- for (int i = 0; i <= ((opcode & 0x0F00) >> 8); ++i)
- {
- memory[regI + i] = Reg[i];
- }
- NextPc();
- break;
- case 0x0065:
- //Fills V0 to VX with values from memory starting at address I
- for (int i = 0; i <= ((opcode & 0x0F00) >> 8); ++i)
- {
- Reg[i] = memory[regI + i];
- }
- NextPc();
- break;
- default:
- //unknown opcode
- //OutputDebugString(L"Unknown opcode [F]");
- break;
- }
- break;
- default:
- //unknown opcode
- //OutputDebugString(L"This opcode can not exist!");
- break;
- }
- //if a time ris higher then 0 they will count down to 0
- //update timers
- if (delay_timer > 0)
- --delay_timer;
- if (sound_timer > 0)
- --sound_timer;
- }
- U8 * Chip8::GetScreen()
- {
- return gfx;
- }
- bool Chip8::GetScreenRefresh()
- {
- return m_ScreenRefresh;
- }
- void Chip8::GetScreenRefreshFalse()
- {
- m_ScreenRefresh = false;
- }
- void Chip8::EmptyScreen()
- {
- for (unsigned int i = 0; i < (SCREEN_WIDTH*SCREEN_HEIGHT); ++i)
- {
- gfx[i] = 0;
- }
- }
- void Chip8::SetKeyState(unsigned int keycode, bool state)
- {
- key[keycode] = state;
- }
Add Comment
Please, Sign In to add comment