Guest User

chip8.h

a guest
Mar 2nd, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <array>
  5. #include <map>
  6. #include <random>
  7.  
  8. using onebyte = unsigned char;
  9. using twobyte = unsigned short;
  10.  
  11. /* memory map of the system:
  12. 0x000 - 0x1FF: Chip 8 interpreter (contains font set in emu)
  13. 0x050 - 0x0A0: Used for the built in 4x5 pixel font set (0 - F)
  14. 0x200 - 0xFFF: Program ROM and work RAM
  15.  
  16. Original                 Implemented
  17. +-+-+-+-+                +-+-+-+-+
  18. |1|2|3|C|                |1|2|3|4|
  19. +-+-+-+-+                +-+-+-+-+
  20. |4|5|6|D|                |Q|W|E|R|
  21. +-+-+-+-+       =>       +-+-+-+-+
  22. |7|8|9|E|                |A|S|D|F|
  23. +-+-+-+-+                +-+-+-+-+
  24. |A|0|B|F|                |Z|X|C|V|
  25. +-+-+-+-+                +-+-+-+-+ */
  26.  
  27. class Chip8 {
  28.     using funcptr = void (Chip8::*)();
  29.    
  30.     static constexpr int MEM_SIZE = 4096;
  31.     static constexpr int GFX_SIZE = 128*64;//64*32;
  32.     static constexpr int REG_SIZE = 16;
  33.     static constexpr int STK_SIZE = 16;
  34.     static constexpr int KEY_SIZE = 16;
  35.     static constexpr int FNT_SIZE = 80;
  36.     //static constexpr int RPL_SIZE = 8;
  37.  
  38.     private:
  39.     twobyte opcode = 0; // operation code, specifies the operation to be performed
  40.     twobyte pc = 0x200; // program counter. increased by 2 because every instruction is 2 bytes long (except on jump or subroutine call)
  41.     twobyte I = 0; // index register
  42.     twobyte sp = 0; // stack pointer, holds the current level of the stack
  43.     onebyte delayTimer = '\0', soundTimer = '\0'; // timers that count at 60 Hz
  44.     bool waitForKey = false;
  45.    
  46.     std::array<onebyte, MEM_SIZE> memory{};
  47.     std::array<onebyte, REG_SIZE> V{}; // CPU registers, V0-VE are general purpose and VF is the 'carry flag'
  48.     std::array<onebyte, GFX_SIZE> gfx{}; // graphics (black and white, so 1 and 0)
  49.     std::array<onebyte, KEY_SIZE> key{}; // current state of the HEX based keypad (0x0 - 0xF)
  50.     std::array<twobyte, STK_SIZE> stack{}; // remembers the starting location before a subroutine call
  51.    
  52.     std::map<twobyte, funcptr> opMap; // maps the opcodes to their respective function
  53.    
  54.     std::random_device rd;
  55.     std::mt19937 mt;
  56.     std::uniform_int_distribution<int> dist;
  57.    
  58.     //bool extendedScreen = false;
  59.     //bool forceExit = false;
  60.    
  61.     //std::array<onebyte, RPL_SIZE> rpl{}}; // RPL user flags
  62.    
  63.     public:
  64.     bool drawFlag = true;
  65.  
  66.     public:
  67.     Chip8() = default;
  68.     Chip8(const Chip8& rhs) = delete;
  69.     Chip8& operator=(const Chip8& rhs) = delete;
  70.     ~Chip8() = default;
  71.  
  72.     onebyte gfxValueAt(unsigned int index) const;
  73.     void mapKey(unsigned int index, onebyte value);
  74.     //bool extendedScreenEnabled() const { return extendedScreen; };
  75.     //bool forceExitEnabled() const { return forceExit; };
  76.    
  77.     void initialize();
  78.     bool loadGame(const std::string& name);
  79.     void emulateCycle();
  80.     void updateTimers();
  81.    
  82.     private:
  83.     void initOpMap();
  84.     void fetchOpcode();
  85.     void executeOpcode();
  86.    
  87.     // opcodes
  88.     void CLS();
  89.     void RET();
  90.     //void EXIT();
  91.     //void LOW();
  92.     //void HIGH();
  93.     void JP();
  94.     void CALL();
  95.     void SE();
  96.     void SNE();
  97.     void LD();
  98.     void ADD();
  99.     void OR();
  100.     void AND();
  101.     void XOR();
  102.     void SUB();
  103.     void SHR();
  104.     void SUBN();
  105.     void SHL();
  106.     void RND();
  107.     void DRW();
  108.     void SKP();
  109.     void SKNP();
  110. };
Add Comment
Please, Sign In to add comment