Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.19 KB | None | 0 0
  1. // chip8.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <SDL.h>
  7. #include <fstream>
  8. #include <string>
  9. #pragma warning(disable:4996)
  10.  
  11.  
  12. class chip8 {
  13. public:
  14.     unsigned short opcode;
  15.     unsigned char memory[4096];
  16.     unsigned char V[16];
  17.     unsigned short I;
  18.     unsigned short pc;
  19.     unsigned char gfx[64 * 32];
  20.     unsigned char delay_timer;
  21.     unsigned char sound_timer;
  22.     size_t lSize;
  23.     void initialize();
  24.     void emulateCycle();
  25.     unsigned short stack[16];
  26.     unsigned char key[16];
  27.     unsigned short sp;
  28.  
  29. };
  30.  
  31.  
  32. unsigned char chip8_fontset[80] =
  33. {
  34.     0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
  35.     0x20, 0x60, 0x20, 0x20, 0x70, // 1
  36.     0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
  37.     0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
  38.     0x90, 0x90, 0xF0, 0x10, 0x10, // 4
  39.     0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
  40.     0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
  41.     0xF0, 0x10, 0x20, 0x40, 0x40, // 7
  42.     0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
  43.     0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
  44.     0xF0, 0x90, 0xF0, 0x90, 0x90, // A
  45.     0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
  46.     0xF0, 0x80, 0x80, 0x80, 0xF0, // C
  47.     0xE0, 0x90, 0x90, 0x90, 0xE0, // D
  48.     0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
  49.     0xF0, 0x80, 0xF0, 0x80, 0x80  // F
  50. };
  51.  
  52. void chip8::initialize()
  53. {
  54.     //Clear memory
  55.     for (int i = 0; i < 4096; i++) {
  56.         memory[i] = 0;
  57.     }
  58.  
  59.     //Clear registers
  60.  
  61.     for (int i = 0; i < 16; i++) {
  62.         V[i] = 0;
  63.     }
  64.  
  65.     pc = 0x200;
  66.     opcode = 0;
  67.     I = 0;
  68.  
  69.     for (int i = 0; i < 80; ++i) {
  70.         memory[i] = chip8_fontset[i];
  71.     }
  72.  
  73.     //  0x000 - 0x1FF - Chip 8 interpreter(contains font set in emu)
  74.     //  0x050 - 0x0A0 - Used for the built in 4x5 pixel font set(0 - F)
  75.     //  0x200 - 0xFFF - Program ROM and work RAM
  76.  
  77.     FILE * pFile;
  78.     lSize = 0;
  79.     pFile = fopen("spaceinvaders.ch8", "rb");
  80.  
  81.    
  82.     if (pFile == NULL) perror("Error opening file");
  83.     else
  84.     {
  85.         char * buffer;
  86.         fseek(pFile, 0, SEEK_END);   // non-portable
  87.         lSize = ftell(pFile);
  88.         printf("Size of rom: %ld bytes.\n", lSize);
  89.         rewind(pFile);
  90.         size_t result=0;
  91.        
  92.  
  93.         if ((4095 - 512) > lSize) {
  94.             buffer = (char*)malloc(sizeof(char)*lSize);
  95.  
  96.             result = fread(buffer, 1, lSize, pFile);
  97.             if (buffer == NULL) { fputs("Memory error\n", stderr); exit(2); }
  98.             if (result != lSize) { fputs("Reading error\n", stderr); exit(3); }
  99.            
  100.             for (int i = 0; i < lSize; i++) {
  101.                 memory[i + 512] = buffer[i];
  102.             }
  103.            
  104.             free(buffer);
  105.             fclose(pFile);
  106.  
  107.  
  108.  
  109.  
  110.  
  111.         }
  112.  
  113.  
  114.     }
  115.  
  116. }
  117.  
  118. void chip8::emulateCycle() {
  119.     unsigned short vx = 0;
  120.     unsigned short vy = 0;
  121.     //decide opcode
  122.     opcode = memory[pc] << 8 | memory[pc + 1];
  123.  
  124.     printf("The opcode undecoded is: %x\n", opcode);
  125.    
  126.     switch (opcode & 0xF000) {
  127.     case 0xA000: //Sets I to the address NNN.
  128.         I = opcode & 0x0FFF;
  129.         pc += 2;
  130.         break;
  131.  
  132.     case 0x1000: //Jumps to address NNN.
  133.         pc = opcode & 0x0FFF;
  134.         pc += 2;
  135.         break;
  136.  
  137.     case 0x2000: //Calls subroutine at NNN.
  138.         stack[sp] = pc;
  139.         sp++;
  140.         pc = 0x0FFF & opcode;
  141.         break;
  142.  
  143.     case 0x3000: //Skips the next instruction if VX equals NN. (Usually the next instruction is a jump to skip a code block)
  144.         unsigned short temp = opcode & 0x0F00;
  145.         temp = temp >> 8;
  146.  
  147.         if (V[temp] = opcode & 0x00FF) {
  148.             pc += 2;
  149.         }
  150.         break;
  151.  
  152.     case 0x4000: //Skips the next instruction if VX doesn't equal NN. (Usually the next instruction is a jump to skip a code block)
  153.         unsigned short temp = opcode & 0x0F00;
  154.         temp = temp >> 8;
  155.  
  156.         if (V[temp] != opcode & 0x00FF) {
  157.             pc += 2;
  158.         }
  159.         break;
  160.  
  161.     case 0x5000: //Skips the next instruction if VX equals VY. (Usually the next instruction is a jump to skip a code block)
  162.         vx = opcode & 0x0F00;
  163.         vx = vx >> 8;
  164.  
  165.         vy = opcode & 0x00F0;
  166.         vy = vy >> 4;
  167.  
  168.         if (V[vx] == V[vy]) {
  169.             pc += 2;
  170.         }
  171.         break;
  172.  
  173.     case 0x6000: //Sets VX to NN.
  174.         vx = opcode & 0xF00;
  175.         vx = vx >> 8;
  176.         V[vx] = opcode & 0x00FF;
  177.         pc += 2;
  178.         break;
  179.  
  180.     case 0x7000: //Adds NN to VX. (Carry flag is not changed)
  181.         vx = opcode & 0x0F00;
  182.         V[vx] = V[vx] + opcode & 0x00FF;
  183.         pc += 2;
  184.         break;
  185.  
  186.     case 0x8000:    //Sets VX to the value of VY.
  187.         switch (opcode & 0x000F) {
  188.         case 0x0000:
  189.  
  190.         }
  191.  
  192.     }
  193.  
  194.  
  195. }
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. int main(int argc, char *argv[])
  205. {
  206.     chip8 myChip8;
  207.     myChip8.initialize();
  208.     myChip8.emulateCycle();
  209.  
  210.     return 0;
  211. }
  212.  
  213. bool init()
  214. {
  215.     return false;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement