refactor: replace micro_mem_view with MemHeatmap

This commit is contained in:
shokre 2021-09-18 23:13:13 +02:00
parent fa11c47993
commit 98d7a694ea
3 changed files with 58 additions and 32 deletions

19
orao.py
View file

@ -10,6 +10,7 @@ from orao.chargen import chargen_init, chargen_draw_str
# views
from orao.views.cpu_state import CPUState
from orao.views.heatmap import MemHeatmap
MEM_LOAD_PRG = None
@ -27,7 +28,6 @@ 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')
from orao.micro_mem_view import micro_mem_view, store_mem_view, micro_mem_view_dims
# create CPU
cpu = CPU(bytearray([0xFF]*0xC000) + bytearray(open('ORAO13.ROM', 'rb').read()))
@ -39,6 +39,7 @@ chargen_init(cpu.memory[0xE000:])
# views
view_cpu_state = CPUState()
view_heatmap = MemHeatmap()
# status lines
status_line = pygame.Surface((64 * 8, 3*8), depth=24)
@ -48,18 +49,11 @@ 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)
# memory view labels
MAX_LABELS = 33
mem_map_labels = pygame.Surface((2*8+1, MAX_LABELS*8*2), depth=24)
mem_map_labels.fill((0, 0, 0))
for i in range(0, MAX_LABELS):
chargen_draw_str(mem_map_labels, 0, i*8*3, '%02x' % i, color=(0xff, 0xcc, 0x00))
mem_map_labels.set_at((16, i*8*3), (0, 255, 0))
def render_frame():
store_mem_view(cpu.memory)
view_cpu_state.render(cpu)
view_heatmap.render(cpu)
# blit
screen.fill((0, 0, 0))
screen.blit(pygame.transform.smoothscale(terminal, (512, 512)), [0, 0])
chargen_draw_str(status_line, 0, 8, 'Speed: {0:.2f} MHz'.format(ratio))
@ -69,9 +63,8 @@ def render_frame():
_, lsy = view_cpu_state.blit(screen, [lsx, lsy], scale=1.8)
lsy += 1
w, h = micro_mem_view_dims
screen.blit(mem_map_labels, [lsx, lsy])
screen.blit(pygame.transform.scale(micro_mem_view, (w * 3, h * 3)), [lsx + 8 + 8 + 1 + 1, lsy])
view_heatmap.blit(screen, [lsx, lsy])
# finish rendering
pygame.display.flip()

View file

@ -1,19 +0,0 @@
import pygame
import numpy
palette = []
# below chars
palette += [(i * 8, 0, 0) for i in range(32)]
# text chars
palette += [(i * 2, 255, 0) for i in range(128 - 32)]
# other
palette += [(i * 2, 0, 255) for i in range(128)]
micro_mem_view_dims = (32, 192)
micro_mem_view = pygame.Surface(micro_mem_view_dims, depth=8)
micro_mem_view.set_palette(palette)
def store_mem_view(memory):
w, h = micro_mem_view_dims
arr = numpy.reshape(memory[0:(w * h)], (h, w))
pygame.surfarray.blit_array(micro_mem_view, numpy.transpose(arr))

52
orao/views/heatmap.py Normal file
View file

@ -0,0 +1,52 @@
import pygame
import numpy
from ..chargen import chargen_draw_str
MAX_LABELS = 33
palette = []
# below chars
palette += [(i * 8, 0, 0) for i in range(32)]
# text chars
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
def __init__(self):
# mem view surface
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.width = self.surf.get_width()
self.width += self.surf_labels.get_width() + 1
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))
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))
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])
return [x + self.width, y + self.height]