CHIP8的話網上已經有許多的模擬器的解說了,這裏咱們就給出CPU的模擬過程git
CHIP8 CPU https://gitee.com/Luciferearth/EasyVGM/blob/master/modules/CHIP8/ 顯示器 https://gitee.com/Luciferearth/EasyVGM/tree/master/modules/Monitor64x32 測試程序 https://gitee.com/Luciferearth/EasyVGM/tree/master/test/test_monitor16x16數組
主體開發框架:C++ QT
平臺:Ubuntu 14 & Windows10框架
一個模擬器的運做過程大體以下:函數
一個CPU的週期內作的過程大體以下:佈局
CHIP8有35個cpu指令,每一個指令長度爲2字節,在C++中定義一個測試
unsigned short opcode;//operation code
來表示每個指令指針
CHIP8有16個單字節(1 byte)寄存器,名字爲V0,V1...到VF
前15個寄存器爲通用寄存器,最後一個寄存器(VF)是個進位標誌(carry flag).這16個寄存器表示爲:code
unsigned char V[16];
CHIP8有4K內存,表示爲blog
unsigned char memory[4*1024];
CHIP8的輸入設備是一個十六進制的鍵盤,其中有16個鍵值,0~F。「8」「6」「4」「2」通常用於方向輸入。有三個操做碼用來處理輸入,其中一個是當鍵值按下則執行下一個指令,對應的是另一個操做碼處理指定鍵值沒有按下則調到下一個指令。第三個操做碼是等待一個按鍵按下,而後將其存放一個寄存器裏。 用一個char數組去記錄和保存按鍵狀態。內存
unsigned char key[16];
CHIP8鍵盤佈局: ||||| | :-: | :-: | :-: | :-: | 1 |2 | 3 | C 4 | 5 | 6 | D 7 | 8 | 9 | E A | 0 | B | F 映射到咱們的電腦鍵盤
--------- --------- 1 2 3 C 1 2 3 4 4 5 6 D Q W E R 7 8 9 E A S D F A 0 B F Z X C V
咱們用一個KeyMap函數來作映射
int keymap(unsigned char k) { switch (k) { case '1': return 0x1; case '2': return 0x2; case '3': return 0x3; case '4': return 0xc; case 'Q': return 0x4; case 'W': return 0x5; case 'E': return 0x6; case 'R': return 0xd; case 'A': return 0x7; case 'S': return 0x8; case 'D': return 0x9; case 'F': return 0xe; case 'Z': return 0xa; case 'X': return 0x0; case 'C': return 0xb; case 'V': return 0xf; default: return -1; } }
CHIP8的輸出設備是一個64x32的LED顯示屏,在這裏咱們用QT OpenGL去做爲CHIP8的顯示器,且他的圖形是黑白的,總共有2048個像素。用一個數組就能夠很容易的保持它的狀態(1或0)
unsigned char gfx[64 * 32];
CHIP8有兩個定時器,以60HZ倒計。當設置大於0時,就倒計到0;
unsigned char delay_timer; unsigned char sound_timer;
CHIP8有16級堆棧,同時,也須要實現一個堆棧指針去表示當前執行的位置
unsigned short stack[16]; unsigned short sp;//stack point