CHIP8模拟器的python3实现-3-指令实现
class Chip8CPU(object):
def __init__(self, screen):
self.registers = {
'v': [],
'index': 0,
'pc': 0,
'sp': 0,
'rpl': []
}
self.timers = {
'delay': 0,
'sound': 0,
} self.operation_lookup = {
0x0: self.screen_return,
0x1: self.jump_to_address,
0x2: self.jump_to_subroutine,
0x3: self.skip_if_reg_equal_val,
0x4: self.skip_if_reg_not_equal_val,
0x5: self.skip_if_reg_equal_reg,
0x6: self.move_value_to_reg,
0x7: self.add_value_to_reg,
0x8: self.execute_logical_instruction,
0x9: self.skip_if_reg_not_equal_reg,
0xA: self.load_index_reg_with_value,
0xB: self.jump_to_index_plus_value,
0xC: self.generate_random_number_to_reg,
0xD: self.draw_sprite,
0xE: self.keyboard_routines,
0xF: self.misc_routines, } self.logical_operation_lookup = {
0x0: self.move_reg_into_reg,
0x1: self.logical_or,
0x2: self.logical_and,
0x3: self.exclusive_or,
0x4: self.add_reg_to_reg,
0x5: self.subtract_reg_from_reg,
0x6: self.right_shift_reg,
0x7: self.substract_reg_from_reg1,
0xE: self.left_shift_reg,
} self.misc_routine_lookup = {
0x07: self.move_delay_timer_into_reg,
0x0A: self.wait_for_keypress,
0x15: self.move_reg_into_delay_timer,
0x18: self.move_reg_into_sound_timer,
0x1E: self.add_reg_into_index,
0x29: self.load_index_with_reg_sprite,
0x30: self.load_index_with_extended_reg_sprite,
0x33: self.store_bcd_in_memory,
0x55: self.store_regs_in_memory,
0x65: self.read_regs_from_memory,
0x75: self.store_regs_in_rpl,
0x85: self.read_regs_from_rpl
} self.operand = 0
self.mode = MODE_NORMAL
self.screen = screen self.memory = bytearray(MAX_MEMORY)
self.reset()
self.running = True def execute_instruction(self, operand=None):
if operand:
self.operand = operand
else:
self.operand = int(self.memory[self.registers['pc']])
self.operand = self.operand << 8
self.operand += int(self.memory[self.registers['pc'] + 1])
self.registers['pc'] += 2
operation = (self.operand & 0xF000) >> 12
self.operation_lookup[operation]()
return self.operand def screen_return(self):
operation = self.operand & 0x00FF
sub_operation = operation & 0x00F0
if sub_operation == 0x00C0:
num_lines = self.operand & 0x000F
self.screen.scroll_down(num_lines)
elif operation == 0x00E0:
self.screen.clear_screen()
elif operation == 0x00EE:
self.return_from_subroutine()
elif operation == 0x00FB:
self.screen.scroll_right()
elif operation == 0x00FC:
self.screen.scroll_left()
elif operation == 0x00FD:
self.running = False
elif operation == 0x00FE:
self.disable_extended_mode()
elif operation == 0x00FF:
self.enable_extended_mode() def return_from_subroutine(self):
self.registers['sp'] -= 1
self.registers['pc'] = self.memory[self.registers['sp']] << 8
self.registers['sp'] -= 1
self.registers['pc'] += self.memory[self.registers['sp']] def enable_extended_mode(self):
self.screen.set_extended()
self.mode = MODE_EXTENDED def disable_extended_mode(self):
self.screen.set_normal()
self.mode = MODE_NORMAL def jump_to_address(self):
self.registers['pc'] = self.operand & 0x0FFF def jump_to_subroutine(self):
self.memory[self.registers['sp']] = self.registers['pc'] & 0x00FF
self.registers['sp'] += 1
self.memory[self.registers['sp']] = (self.registers['pc'] & 0xFF00) >> 8
self.registers['sp'] += 1
self.registers['pc'] = self.operand & 0x0FFF def skip_if_reg_equal_val(self):
source = (self.operand & 0x0F00) >> 8
if self.registers['v'][source] == (self.operand & 0x00FF):
self.registers['pc'] += 2 def skip_if_reg_not_equal_val(self):
source = (self.operand & 0x0F00) >> 8
if self.registers['v'][source] != (self.operand & 0x00FF):
self.registers['pc'] += 2 def skip_if_reg_equal_reg(self):
source = (self.operand & 0x0F00) >> 8
target = (self.operand & 0x00F0) >> 4
if self.registers['v'][source] == self.registers['v'][target]:
self.registers['pc'] += 2 def move_value_to_reg(self):
target = (self.operand & 0x0F00) >> 8
self.registers['v'][target] = self.operand & 0x00FF def add_value_to_reg(self):
target = (self.operand & 0x0F00) >> 8
temp = self.registers['v'][target] + (self.operand & 0x00FF)
self.registers['v'][target] = temp if temp < 256 else temp - 256 def skip_if_reg_not_equal_reg(self):
source = (self.operand & 0x0F00) >> 8
target = (self.operand & 0x00F0) >> 4
if self.registers['v'][source] != self.registers['v'][target]:
self.registers['pc'] += 2 def load_index_reg_with_value(self):
self.registers['index'] = self.operand & 0x0FFF def jump_to_index_plus_value(self):
self.registers['pc'] = self.registers['index'] + (self.operand & 0x0FFF) def generate_random_number_to_reg(self):
value = self.operand & 0x00FF
target = (self.operand & 0x0F00) >> 8
self.registers['v'][target] = value & randint(0, 255) def draw_sprite(self):
x_source = (self.operand & 0x0F00) >> 8
y_source = (self.operand & 0x00F0) >> 4
x_pos = self.registers['v'][x_source]
y_pos = self.registers['v'][y_source]
num_bytes = self.operand & 0x000F
self.registers['v'][0xF] = 0 if self.mode == MODE_EXTENDED and num_bytes == 0:
self.draw_extended(x_pos, y_pos, 16)
else:
self.draw_normal(x_pos, y_pos, num_bytes) def draw_normal(self, x_pos, y_pos, num_bytes):
for y_index in range(num_bytes):
color_byte = bin(self.memory[self.registers['index'] + y_index])
color_byte = color_byte[2:].zfill(8)
y_coord = y_pos + y_index
y_coord = y_coord % self.screen.get_height() for x_index in range(8):
x_coord = x_pos + x_index
x_coord = x_coord % self.screen.get_width() color = int(color_byte[x_index])
current_color = self.screen.get_pixel(x_coord, y_coord) if color == 1 and current_color == 1:
self.registers['v'][0xF] = 1
color = 0
elif color == 0 and current_color == 1:
color = 1 self.screen.draw_pixel(x_coord, y_coord, color) self.screen.update() def draw_extended(self, x_pos, y_pos, num_bytes):
for y_index in range(num_bytes):
for x_byte in range(2):
color_byte = bin(self.memory[self.registers['index'] + (y_index * 2) + x_byte])
color_byte = color_byte[2:].zfill(8)
y_coord = y_pos + y_index
y_coord = y_coord % self.screen.height for x_index in range(8):
x_coord = x_pos + x_index + (x_byte * 8)
x_coord = x_coord % self.screen.width
color = int(color_byte[x_index])
current_color = self.screen.get_pixel(x_coord, y_coord) if color == 1 and current_color == 1:
self.registers['v'][0xF] = 1
color = 0 elif color == 0 and current_color == 1:
color = 1 self.screen.draw_pixel(x_coord, y_coord, color) self.screen.update() def keyboard_routines(self):
operation = self.operand & 0x00FF
source = (self.operand & 0x0F00) >> 8 key_to_check = self.registers['v'][source]
keys_pressed = key.get_pressed()
if operation == 0x9E:
if keys_pressed[KEY_MAPPINGS[key_to_check]]:
self.registers['pc'] += 2
elif operation == 0xA1:
if not keys_pressed[KEY_MAPPINGS[key_to_check]]:
self.registers['pc'] += 2 def execute_logical_instruction(self):
operation = self.operand & 0x000F
self.logical_operation_lookup[operation]() def move_reg_into_reg(self):
target = (self.operand & 0x0F00) >> 8
source = (self.operand & 0x00F0) >> 4
self.registers['v'][target] = self.registers['v'][source] def logical_or(self):
target = (self.operand & 0x0F00) >> 8
source = (self.operand & 0x00F0) >> 4
self.registers['v'][target] |= self.registers['v'][source] def logical_and(self):
target = (self.operand & 0x0F00) >> 8
source = (self.operand & 0x00F0) >> 4
self.registers['v'][target] &= self.registers['v'][source] def exclusive_or(self):
target = (self.operand & 0x0F00) >> 8
source = (self.operand & 0x00F0) >> 4
self.registers['v'][target] ^= self.registers['v'][source] def add_reg_to_reg(self):
target = (self.operand & 0x0F00) >> 8
source = (self.operand & 0x00F0) >> 4
temp = self.registers['v'][target] + self.registers['v'][source]
if temp > 255:
self.registers['v'][target] = temp - 256
self.registers['v'][0xF] = 1
else:
self.registers['v'][target] = temp
self.registers['v'][0xF] = 0 def subtract_reg_from_reg(self):
target = (self.operand & 0x0F00) >> 8
source = (self.operand & 0x00F0) >> 4
source_reg = self.registers['v'][source]
target_reg = self.registers['v'][target]
if target_reg > source_reg:
target_reg -= source_reg
self.registers['v'][0xF] = 1
else:
target_reg = 256 + target_reg - source_reg
self.registers['v'][0xF] = 0
self.registers['v'][target] = target_reg def right_shift_reg(self):
source = (self.operand & 0x0F00) >> 8
target = (self.operand & 0x00F0) >> 4
bit_zero = self.registers['v'][source] & 0x1
self.registers['v'][target] = self.registers['v'][source] >> 1
self.registers['v'][0xF] = bit_zero def substract_reg_from_reg1(self):
target = (self.operand & 0x0F00) >> 8
source = (self.operand & 0x00F0) >> 4
source_reg = self.registers['v'][source]
target_reg = self.registers['v'][target]
if source_reg > target_reg:
target_reg = source_reg - target_reg
self.registers['v'][0xF] = 1
else:
target_reg = 256 + source_reg - target_reg
self.registers['v'][0xF] = 0
self.registers['v'][target] = target_reg def left_shift_reg(self):
source = (self.operand & 0x0F00) >> 8
target = (self.operand & 0x00F0) >> 4
bit_seven = (self.registers['v'][source] & 0x80) >> 8
self.registers['v'][target] = self.registers['v'][source] << 1
self.registers['v'][0xF] = bit_seven def misc_routines(self):
operation = self.operand & 0x00FF
self.misc_routine_lookup[operation]() def move_delay_timer_into_reg(self):
target = (self.operand & 0x0F00) >> 8
self.registers['v'][target] = self.timers['delay'] def wait_for_keypress(self):
target = (self.operand & 0x0F00) >> 8
key_pressed = False
while not key_pressed:
event = pygame.event.wait()
if event.type == pygame.KEYDOWN:
keys_pressed = key.get_pressed()
for keyval, lookup_key in KEY_MAPPINGS.items():
if keys_pressed[lookup_key]:
self.registers['v'][target] = keyval
key_pressed = True
break def move_reg_into_delay_timer(self):
source = (self.operand & 0x0F00) >> 8
self.timers['delay'] = self.registers['v'][source] def move_reg_into_sound_timer(self):
source = (self.operand & 0x0F00) >> 8
self.timers['sound'] = self.registers['v'][source] def load_index_with_reg_sprite(self):
source = (self.operand & 0x0F00) >> 8
self.registers['index'] = self.registers['v'][source] * 5 def load_index_with_extended_reg_sprite(self):
source = (self.operand & 0x0F00) >> 8
self.registers['index'] = self.registers['v'][source] * 10 def add_reg_into_index(self):
source = (self.operand & 0x0F00) >> 8
self.registers['index'] += self.registers['v'][source] def store_bcd_in_memory(self):
source = (self.operand & 0x0F00) >> 8
bcd_value = '{:03d}'.format(self.registers['v'][source])
self.memory[self.registers['index']] = int(bcd_value[0])
self.memory[self.registers['index'] + 1] = int(bcd_value[1])
self.memory[self.registers['index'] + 2] = int(bcd_value[2]) def store_regs_in_memory(self):
source = (self.operand & 0x0F00) >> 8
for counter in range(source + 1):
self.memory[self.registers['index'] + counter] = \
self.registers['v'][counter] def read_regs_from_memory(self):
source = (self.operand & 0x0F00) >> 8
for counter in range(source + 1):
self.registers['v'][counter] = \
self.memory[self.registers['index'] + counter] def store_regs_in_rpl(self):
source = (self.operand & 0x0F00) >> 8
for counter in range(source + 1):
self.registers['rpl'][counter] = self.registers['v'][counter] def read_regs_from_rpl(self):
source = (self.operand & 0x0F00) >> 8
for counter in range(source + 1):
self.registers['v'][counter] = self.registers['rpl'][counter] def reset(self):
self.registers['v'] = [0] * NUM_REGISTERS
self.registers['pc'] = PROGRAM_COUNTER_START
self.registers['sp'] = STACK_POINTER_START
self.registers['index'] = 0
self.registers['rpl'] = [0] * NUM_REGISTERS
self.timers['delay'] = 0
self.timers['sound'] = 0 def load_rom(self, filename, offset=PROGRAM_COUNTER_START):
rom_data = open(filename, 'rb').read()
for index, val in enumerate(rom_data):
self.memory[offset + index] = val def decrement_timers(self):
if self.timers['delay'] != 0:
self.timers['delay'] -= 1 if self.timers['sound'] != 0:
self.timers['delay'] -= 1
CHIP8模拟器的python3实现-3-指令实现的更多相关文章
- chip8模拟器的python3实现-1-CHIP8简介
打算编写一个NES模拟器,先从简单的chip8模拟器入手 1.CHIP-8简介 CHIP-8是一个解释型语言,由Joseph Weisbecker开发.最初CHIP-8在上个世纪70年代被使用在COS ...
- chip8模拟器的python3实现-2-指令介绍
CHIP指令表 CHIP-8有35个指令,都为两字节长,以大端方式存储.指令表的指令格式规定如下: NNN:地址 NN:8位常量 N:4位常量 V:寄存器 X和Y:4位,标识寄存器 PC:程序计数器 ...
- QEMU模拟器Windows版本模拟ARMX86CPU指令
http://qemu.weilnetz.de/ QEMU Binaries for Windows
- Python3执行top指令
import subprocess top_info = subprocess.Popen(["], stdout=subprocess.PIPE) out, err = top_info. ...
- C++版 Chip8游戏模拟器
很早就想写个FC模拟器,但真是一件艰难的事情.. 所以先写个Chip8模拟器,日后再继续研究FC模拟器. Chip8只有35条指令,属于RISC指令集,4k内存,2k显存,16个寄存器(其中15个通用 ...
- 如何实现模拟器(CHIP-8 interpreter) 绝佳杰作.
转自 http://www.multigesture.net/articles/how-to-write-an-emulator-chip-8-interpreter/ How to write an ...
- CS:APP2e Y86处理器模拟器∗指南
CS:APP2e Y86处理器模拟器∗指南 Randal E.Bryant David R. O'Hallaron 2013年7月29日 本文档描述了处理器模拟器,伴随的表示在第4章Y86处理器架构的 ...
- Appium+python自动化19-iOS模拟器(iOS Simulator)安装自家APP
前言 做过iOS上app测试的小伙伴应该都知道,普通用户安装app都是从appstore下载安装,安装测试版本的app,一般就是开发给的二维码扫码安装, 或者开发给个.ipa的安装包文件,通过itoo ...
- Appium+python自动化19-iOS模拟器(iOS Simulator)安装自家APP【转载】
前言 做过iOS上app测试的小伙伴应该都知道,普通用户安装app都是从appstore下载安装,安装测试版本的app,一般就是开发给的二维码扫码安装, 或者开发给个.ipa的安装包文件,通过itoo ...
随机推荐
- IAP15W4K58S4引脚定义 STC15
- C语言、指针(一)
指针(一) “带*类型” 的特征探测:宽度 “带*类型” 的特征探测:声明 “带*类型” 的特征探测:赋值 “带*类型” 的特征探测:++ -- “带*类型” 的特征探测:加上/减去 一个整数 “带* ...
- docker镜像管理(二)
docker镜像 docker镜像含有启动容器所需要的文件系统和内容,因此,其用于创建并启动docker容器 docker镜像采用分层构建机制,最底层为bootfs,其之为rootfs bootfs: ...
- 智能指针类模板(中)——Qt中的智能指针
Qt中的智能指针-QPointer .当其指向的对象被销毁时,它会被自动置空 .析构时不会自动销毁所指向的对象-QSharedPointer .引用计数型智能指针 .可以被自由的拷贝和赋值 .当引用计 ...
- 2019.6.13_SQL语句中----删除表数据drop、truncate和delete的用法
一.SQL中的语法 1.drop table 表名称 eg: drop table dbo.Sys_Test 2.truncate table 表 ...
- MongoDB Shell基本操作(五)
官网文档:https://docs.mongodb.com/ 1. 创建数据库 #如果数据库不存在,则创建数据库,否则切换到指定数据库 use DATABASE_NAME 示例: use runoob ...
- CF1146D Frog Jumping
CF1146D Frog Jumping 洛谷评测传送门 题目描述 A frog is initially at position 00 on the number line. The frog ha ...
- sqlserver deadlock
当时系统测试的时候,由于使用了自动化测试跑脚本,一下子出了很多sqlserver deadlock的问题. 都处于system test阶段了,哪儿还有时间仔细分析这些死锁是怎么出来的,直接上retr ...
- wcf callback在主线程被调用
记得当时往wcf迁移了部分service以后,tester发现有时候系统会没有任何反应,cpu占用倒是没有问题,可就是做不了事情. 感觉是哪里死锁了,但也不是每次都能再现,后来发现如果把callbac ...
- SecureCRT 8.1工具下载和破解附Xshell6
附教程:https://jingyan.baidu.com/article/eae078275917861fec548592.html 这一篇教程实际上已经说得非常明确了,只需要把注册机放在和secu ...