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