Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // chip8.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <SDL.h>
- #include <fstream>
- #include <string>
- #pragma warning(disable:4996)
- class chip8 {
- public:
- unsigned short opcode;
- unsigned char memory[4096];
- unsigned char V[16];
- unsigned short I;
- unsigned short pc;
- unsigned char gfx[64 * 32];
- unsigned char delay_timer;
- unsigned char sound_timer;
- size_t lSize;
- void initialize();
- void emulateCycle();
- unsigned short stack[16];
- unsigned char key[16];
- unsigned short sp;
- };
- unsigned char chip8_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
- };
- void chip8::initialize()
- {
- //Clear memory
- for (int i = 0; i < 4096; i++) {
- memory[i] = 0;
- }
- //Clear registers
- for (int i = 0; i < 16; i++) {
- V[i] = 0;
- }
- pc = 0x200;
- opcode = 0;
- I = 0;
- for (int i = 0; i < 80; ++i) {
- memory[i] = chip8_fontset[i];
- }
- // 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
- FILE * pFile;
- lSize = 0;
- pFile = fopen("spaceinvaders.ch8", "rb");
- if (pFile == NULL) perror("Error opening file");
- else
- {
- char * buffer;
- fseek(pFile, 0, SEEK_END); // non-portable
- lSize = ftell(pFile);
- printf("Size of rom: %ld bytes.\n", lSize);
- rewind(pFile);
- size_t result=0;
- if ((4095 - 512) > lSize) {
- buffer = (char*)malloc(sizeof(char)*lSize);
- result = fread(buffer, 1, lSize, pFile);
- if (buffer == NULL) { fputs("Memory error\n", stderr); exit(2); }
- if (result != lSize) { fputs("Reading error\n", stderr); exit(3); }
- for (int i = 0; i < lSize; i++) {
- memory[i + 512] = buffer[i];
- }
- free(buffer);
- fclose(pFile);
- }
- }
- }
- void chip8::emulateCycle() {
- unsigned short vx = 0;
- unsigned short vy = 0;
- //decide opcode
- opcode = memory[pc] << 8 | memory[pc + 1];
- printf("The opcode undecoded is: %x\n", opcode);
- switch (opcode & 0xF000) {
- case 0xA000: //Sets I to the address NNN.
- I = opcode & 0x0FFF;
- pc += 2;
- break;
- case 0x1000: //Jumps to address NNN.
- pc = opcode & 0x0FFF;
- pc += 2;
- break;
- case 0x2000: //Calls subroutine at NNN.
- stack[sp] = pc;
- sp++;
- pc = 0x0FFF & opcode;
- break;
- case 0x3000: //Skips the next instruction if VX equals NN. (Usually the next instruction is a jump to skip a code block)
- unsigned short temp = opcode & 0x0F00;
- temp = temp >> 8;
- if (V[temp] = opcode & 0x00FF) {
- pc += 2;
- }
- break;
- case 0x4000: //Skips the next instruction if VX doesn't equal NN. (Usually the next instruction is a jump to skip a code block)
- unsigned short temp = opcode & 0x0F00;
- temp = temp >> 8;
- if (V[temp] != opcode & 0x00FF) {
- pc += 2;
- }
- break;
- case 0x5000: //Skips the next instruction if VX equals VY. (Usually the next instruction is a jump to skip a code block)
- vx = opcode & 0x0F00;
- vx = vx >> 8;
- vy = opcode & 0x00F0;
- vy = vy >> 4;
- if (V[vx] == V[vy]) {
- pc += 2;
- }
- break;
- case 0x6000: //Sets VX to NN.
- vx = opcode & 0xF00;
- vx = vx >> 8;
- V[vx] = opcode & 0x00FF;
- pc += 2;
- break;
- case 0x7000: //Adds NN to VX. (Carry flag is not changed)
- vx = opcode & 0x0F00;
- V[vx] = V[vx] + opcode & 0x00FF;
- pc += 2;
- break;
- case 0x8000: //Sets VX to the value of VY.
- switch (opcode & 0x000F) {
- case 0x0000:
- }
- }
- }
- int main(int argc, char *argv[])
- {
- chip8 myChip8;
- myChip8.initialize();
- myChip8.emulateCycle();
- return 0;
- }
- bool init()
- {
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement