refactor: code cleanup

This commit is contained in:
shokre 2021-09-19 01:42:36 +02:00
parent 682b764ee3
commit 0e41b615c5
2 changed files with 30 additions and 15 deletions

13
orao.py
View file

@ -26,10 +26,6 @@ pygame.mixer.pre_init(44100, 8, 1, buffer=2048)
pygame.init()
pygame.time.set_timer(pygame.USEREVENT + 1, 40)
# setup surfaces
screen = pygame.display.set_mode((512+1+8+8+1+1+32*3, 512+ 3*8 + 2))
pygame.display.set_caption('Orao Emulator v0.1')
# create CPU
cpu = CPU(bytearray([0xFF]*0xC000) + bytearray(open('ORAO13.ROM', 'rb').read()))
cpu.channel = pygame.mixer.Channel(0)
@ -46,6 +42,15 @@ view_heatmap = MemHeatmap()
status_line = pygame.Surface((64 * 8, 3*8), depth=24)
status_line.fill((0, 0, 0))
chargen_draw_str(status_line, 0, 0, 'Orao Emulator v0.1')
# setup screen
screen = pygame.display.set_mode((
terminal.get_width() * 2 + 1 + int(max(view_heatmap.width, view_cpu_state.width*1.8)),
terminal.get_height() * 2 + 3*8 + 2
))
pygame.display.set_caption('Orao Emulator v0.1')
if MEM_LOAD_PRG is not None:
chargen_draw_str(status_line, 0, 16, 'F8:', color=(0, 0, 0), bg=(0, 255, 0))
chargen_draw_str(status_line, 24, 16, ' %s' % MEM_LOAD_PRG)

View file

@ -2,7 +2,7 @@ import pygame
import numpy
from ..chargen import chargen_draw_str
MAX_LABELS = 33
SURF_SCALE = 2
palette = []
# below chars
@ -12,41 +12,51 @@ palette += [(i * 2, 255, 0) for i in range(128 - 32)]
# other
palette += [(i * 2, 0, 255) for i in range(128)]
class MemHeatmap:
dims = (32, 192)
surf = None
tick_color = (0xff, 0xcc, 0x00)
label_color = (0xff, 0xcc, 0x00)
tick_size = 2
start_addr = 0
def __init__(self):
def __init__(self, size=16, start_addr=0):
# mem view surface
self.start_addr = start_addr
self.dims = (32, size * 8)
self.surf = pygame.Surface(self.dims, depth=8)
self.surf.set_palette(palette)
# label surface
self.surf_labels = pygame.Surface((2 * 8 + 1, MAX_LABELS * 8 * 2), depth=24)
self.surf_labels = pygame.Surface((2 * 8 + self.tick_size, size * 8 * 2), depth=24)
self.width = self.surf.get_width()
self.width += self.surf_labels.get_width() + 1
self.width = self.surf.get_width() * SURF_SCALE
self.width += self.surf_labels.get_width()
self.height = self.surf.get_height()
self.surf.fill((0, 0, 0))
self.surf_labels.fill((0, 0, 0))
# build labels
for i in range(0, MAX_LABELS):
chargen_draw_str(self.surf_labels, 0, i * 8 * 3, '%02X' % i, color=(0xff, 0xcc, 0x00))
self.surf_labels.set_at((16, i * 8 * 3), (0, 255, 0))
for i in range(0, size):
y = i * 8 * SURF_SCALE
chargen_draw_str(self.surf_labels, 0, y, '%02X' % i, color=self.label_color)
# draw ticks
for t in range(0, self.tick_size):
self.surf_labels.set_at((16 + t, y), self.tick_color)
def scale(self, f):
return pygame.transform.scale(self.surf, (int(self.surf.get_width() * f), int(self.surf.get_height() * f)))
def render(self, cpu):
w, h = self.dims
arr = numpy.reshape(cpu.memory[0:(w * h)], (h, w))
arr = numpy.reshape(cpu.memory[self.start_addr:(w * h)], (h, w))
pygame.surfarray.blit_array(self.surf, numpy.transpose(arr))
pass
def blit(self, screen, pos, scale=1):
x, y = pos
screen.blit(self.surf_labels, pos)
screen.blit(self.scale(3), [x + self.surf_labels.get_width()+1, y])
x += self.surf_labels.get_width()
screen.blit(self.scale(SURF_SCALE), [x, y])
return [x + self.width, y + self.height]