From 0e288f8063027ac42370a7fe6e00da4d68b5f930 Mon Sep 17 00:00:00 2001 From: shokre Date: Thu, 16 Sep 2021 15:00:54 +0200 Subject: [PATCH] refactor: extract pygame surfaces from cpu --- orao.py | 18 ++++++++++++++---- orao/cpu.py | 18 ++++++------------ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/orao.py b/orao.py index 88c6652..9a0f52e 100755 --- a/orao.py +++ b/orao.py @@ -4,12 +4,22 @@ import pygame, numpy, sys, datetime, wave, time from orao.cpu import CPU -cpu = CPU(bytearray([0xFF]*0xC000) + bytearray(open('ORAO13.ROM', 'rb').read())) pygame.mixer.pre_init(44100, 8, 1, buffer=2048) pygame.init() -ratio, cpu.channel, running = 0, pygame.mixer.Channel(0), True +ratio, running = 0, True pygame.time.set_timer(pygame.USEREVENT + 1, 40) +# setup surfaces +screen = pygame.display.set_mode((800, 900)) +background = pygame.image.load("pozadina.png").convert_alpha() +terminal = pygame.Surface((256, 256), pygame.SRCALPHA, depth=32) +terminal.fill((255, 255, 255)) + +# create CPU +cpu = CPU(bytearray([0xFF]*0xC000) + bytearray(open('ORAO13.ROM', 'rb').read())) +cpu.alphaarray = pygame.surfarray.pixels_alpha(terminal) +cpu.channel = pygame.mixer.Channel(0) + while running: before, previous_loop_cycles = datetime.datetime.now(), cpu.cycles time_elapsed = lambda: (datetime.datetime.now()-before).microseconds + 1 @@ -32,8 +42,8 @@ while running: cpu.memory[address] = ~numpy.dot(keys, [16,32,64,128][:len(keys)]) & 0xFF if event.type == pygame.USEREVENT + 1: - cpu.screen.blit(cpu.background, [0, 0]) - cpu.screen.blit(pygame.transform.smoothscale(cpu.terminal, (512, 512)), [150, 140]) + screen.blit(background, [0, 0]) + screen.blit(pygame.transform.smoothscale(terminal, (512, 512)), [150, 140]) pygame.display.set_caption('({0:.2f} MHz) Orao Emulator v0.1'.format(ratio)) pygame.display.update() diff --git a/orao/cpu.py b/orao/cpu.py index 49a4f0b..5f5a709 100755 --- a/orao/cpu.py +++ b/orao/cpu.py @@ -1,10 +1,10 @@ -#!/usr/bin/python2 # -*- coding: utf8 -*- -import pygame, numpy, sys, datetime, wave, time +import wave class CPU(object): CARRY, ZERO, INTERRUPT, DECIMAL, BREAK, UNUSED, OVERFLOW, NEGATIVE = [2**i for i in range(8)] + alphaarray = None def __init__(self, memory): s, self.tape_out, self.filename, self.samples = self, None, None, 0 @@ -64,13 +64,6 @@ class CPU(object): s.ticks = {s.im: 1, s.zp: 1, s.zx: 1, s.zy: 1, s.ab: 2, s.ax: 2, s.no: 0, s.ay: 2, s.jm: 2, s.id: 2, s.ix: 1, s.iy: 1, s.re: 1} - self.screen = pygame.display.set_mode((800, 900)) - self.terminal = pygame.Surface((256, 256), pygame.SRCALPHA, depth=32) - self.terminal.fill((255, 255, 255)) - self.alphaarray = pygame.surfarray.pixels_alpha(self.terminal) - - self.background = pygame.image.load("pozadina.png").convert_alpha() - def get_flag(self, flag): return self.flags & flag != 0 def set_flag(self, flag, boolean): @@ -116,9 +109,10 @@ class CPU(object): if addr == 0x8800: self.speaker() # Zvucnik if 0x6000 <= addr <= 0x7FFF: # Video RAM - y, x = divmod((addr - 0x6000) * 8, 256) - for i in range(8): - self.alphaarray[x+i, y] = 255 if (val>>i) & 1 else 0 # Transparency mask + if self.alphaarray is not None: + y, x = divmod((addr - 0x6000) * 8, 256) + for i in range(8): + self.alphaarray[x+i, y] = 255 if (val>>i) & 1 else 0 # Transparency mask self.memory[addr] = val & 0xFF