您的位置:首页 > 其它

CHIP8 Emulator(1)——CHIP8简介

2015-06-17 13:50 309 查看

CHIP8简介

对CHIP8一个简单的介绍,参考资料来自于CHIP8的Wiki。

CHIP8是什么?

我开始一直以为CHIP8是什么单片机的一种,后来才发现CHIP8是一种解释性的编程语言。对,是语言!最初被应用是在1970年代中期。CHIP8的程序运行在CHIP8虚拟机中,它的出现让电子游戏编程变得简单些了(相对于那个年代来说)。

用CHIP8实现的电子游戏不少,比如小蜜蜂,俄罗斯方块,吃豆人等。CHIP8还有个后代叫做SCHIP8(Super Chip8),它的出现让游戏编程相对于CHIP8来说更加简单了,因为它多了很多的操作码。它们两个在很多平台上都实现了相应的模拟器,也有纯硬件实现,比如用FPGA。

CHIP8虚拟机介绍

上文提到了运行CHIP8的程序是在CHIP8的虚拟机中,那么作为一个能运行CHIP8的虚拟机,具备了哪些要素呢?

内存

CHIP8基本是在一个有4K内存的系统上实现,也就是4096个字节。前512字节由CHIP8的解释器占据。所以CHIP8的程序都是从0x200地址开始的。最顶上的256个字(0xF00-0xFFF) 用于显示刷新,在这下面的96个字节 (0xEA0-0xEFF) 用于栈, 内部使用或者其他变量。

发展到今天,CHIP8的实现的话不需要使用的空间都可以不使用,也就是不用完全那么按照上面来。但是通常还是要在低512字节 (0x000-0x200)存放字体数据。

寄存器

CHIP8的话有16个数据寄存器,V0~VF。VF寄存器存放进位标识。还有一个地址寄存器叫做I,2个字节的长度。

栈用于存放函数返回的地址值和保存一些数据,现在CHIP8的栈实现一般有16级。

定时器

CHIP8有两个定时器,一个延时计时器,一个声音计时器。延时计时器一般处理用户游戏事件,可读可写。声音计时器顾名思义,用于发出声音的,当其值非零,就会发出一个声音。

输入

输入是一个十六进制的键盘,其中有16个键值,0~F。“8”“6”“4”“2”一般用于方向输入。有三个操作码用来处理输入,其中一个是当键值按下则执行下一个指令,对应的是另外一个操作码处理指定键值没有按下则调到下一个指令。第三个操作码是等待一个按键按下,然后将其存放一个寄存器里。

图像和声音

显示分辨率是64X32的像素,并且是单色的。某像素点为1则屏幕上显示相应像素点,为0则不显示。但某个像素点由有到无则进位标识被设置为1,可以用来进行冲撞检测。

操作码

这是CHIP8的重点,CHIP8一共有35个操作码。每个操作码都是两个字节长。最高字节首先被存放。下面是操作码表和其代表的意义:

NNN: address

NN: 8-bit constant

N: 4-bit constant

X and Y: 4-bit register identifier

OpcodeExplanation
0NNNCalls RCA 1802 program at address NNN.
00E0Clears the screen.
00EEReturns from a subroutine.
1NNNJumps to address NNN.
2NNNCalls subroutine at NNN.
3XNNSkips the next instruction if VX equals NN.
4XNNSkips the next instruction if VX doesn’t equal NN.
5XY0Skips the next instruction if VX equals VY.
6XNNSets VX to NN.
7XNNAdds NN to VX.
8XY0Sets VX to the value of VY.
8XY1Sets VX to VX or VY.
8XY2Sets VX to VX and VY.
8XY3Sets VX to VX xor VY.
8XY4Adds VY to VX. VF is set to 1 when there’s a carry, and to 0 when there isn’t.
8XY5VY is subtracted from VX. VF is set to 0 when there’s a borrow, and 1 when there isn’t.
8XY6Shifts VX right by one. VF is set to the value of the least significant bit of VX before the shift.[2]
8XY7Sets VX to VY minus VX. VF is set to 0 when there’s a borrow, and 1 when there isn’t.
8XYEShifts VX left by one. VF is set to the value of the most significant bit of VX before the shift.[2]
9XY0Skips the next instruction if VX doesn’t equal VY.
ANNNSets I to the address NNN.
BNNNJumps to the address NNN plus V0.
CXNNSets VX to a random number, masked by NN.
DXYNSprites stored in memory at location in index register (I), maximum 8bits wide. Wraps around the screen. If when drawn, clears a pixel, register VF is set to 1 otherwise it is zero. All drawing is XOR drawing (i.e. it toggles the screen pixels)
EX9ESkips the next instruction if the key stored in VX is pressed.
EXA1Skips the next instruction if the key stored in VX isn’t pressed.
FX07Sets VX to the value of the delay timer.
FX0AA key press is awaited, and then stored in VX.
FX15Sets the delay timer to VX.
FX18Sets the sound timer to VX.
FX1EAdds VX to I.[3]
FX29Sets I to the location of the sprite for the character in VX. Characters 0-F (in hexadecimal) are represented by a 4x5 font.
FX33Stores the Binary-coded decimal representation of VX, with the most significant of three digits at the address in I, the middle digit at I plus 1, and the least significant digit at I plus 2. (In other words, take the decimal representation of VX, place the hundreds digit in memory at location in I, the tens digit at location I+1, and the ones digit at location I+2.)
FX55Stores V0 to VX in memory starting at address I.[4]
FX65Fills V0 to VX with values from memory starting at address I.[4]
以上就是对CHIP8的一个简单介绍,要想实现CHIP8的模拟,重点就在于对其操作码的模拟实现。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: