0320-Chip8-实现基础指令
环境
- Time 2023-07-26
- Zig 0.11.0-dev.4191+1bf16b172
- SLD2 2.28.1
前言
说明
参考资料:
- https://en.wikipedia.org/wiki/CHIP-8
- https://austinmorlan.com/posts/chip8_emulator/
- https://rsj217.github.io/chip8-py/
- https://github.com/Timendus/chip8-test-suite
其中最后一个提供了测试的套件,实现的过程中,可以检测哪些指令有问题,帮助很大。
目标
实现 00E0、6xnn、Annn、1nnn 四个指令。
cpu.zig
- 00E0,清屏指令,之前实现屏幕的时候已经实现,直接调用方法。
- 1nnn,跳转指令,直接修改pc的值即可跳转。
- 6xnn,将 x 寄存器的值修改为 nn。
- Annn,将索引寄存器的值修改为 nnn。
const std = @import("std");
const Instruct = @import("instruct.zig").Instruct;
const Memory = @import("memory.zig").Memory;
pub const CPU = struct {
instruct: Instruct = undefined,
register: [16]u8 = std.mem.zeroes([16]u8),
index: u16 = 0,
pc: u16,
pub fn cycle(self: *CPU, memory: *Memory) void {
self.fetch(memory);
self.decode();
self.execute(memory);
}
fn fetch(self: *CPU, memory: *Memory) void {
var opcode = memory.load(self.pc);
self.instruct = Instruct{ .opcode = opcode };
self.next();
}
fn next(self: *CPU) void {
self.pc += 2;
}
fn decode(self: *CPU) void {
self.instruct.decode();
}
fn execute(self: *CPU, memory: *Memory) void {
const ins = &self.instruct;
var reg = &self.register;
switch (ins.code) {
0x0 => memory.clearScreen(),
0x1 => self.pc = ins.nnn,
0x6 => reg[ins.x] = ins.nn,
0xA => self.index = ins.nnn,
else => std.log.info("unknown opcode: 0x{X:0>4}", .{ins.opcode}),
}
}
};
启动
zig build run
效果
除了看到显示的窗口外,在控制台还会看到打印 0xD01F 指令未实现,也只有这一个指令未实现。

总结
实现了基础的四个 CPU 指令。

浙公网安备 33010602011771号