refactor: extract pygame surfaces from cpu
This commit is contained in:
parent
1598d38222
commit
0e288f8063
2 changed files with 20 additions and 16 deletions
18
orao.py
18
orao.py
|
@ -4,12 +4,22 @@
|
||||||
import pygame, numpy, sys, datetime, wave, time
|
import pygame, numpy, sys, datetime, wave, time
|
||||||
from orao.cpu import CPU
|
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.mixer.pre_init(44100, 8, 1, buffer=2048)
|
||||||
pygame.init()
|
pygame.init()
|
||||||
ratio, cpu.channel, running = 0, pygame.mixer.Channel(0), True
|
ratio, running = 0, True
|
||||||
pygame.time.set_timer(pygame.USEREVENT + 1, 40)
|
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:
|
while running:
|
||||||
before, previous_loop_cycles = datetime.datetime.now(), cpu.cycles
|
before, previous_loop_cycles = datetime.datetime.now(), cpu.cycles
|
||||||
time_elapsed = lambda: (datetime.datetime.now()-before).microseconds + 1
|
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
|
cpu.memory[address] = ~numpy.dot(keys, [16,32,64,128][:len(keys)]) & 0xFF
|
||||||
|
|
||||||
if event.type == pygame.USEREVENT + 1:
|
if event.type == pygame.USEREVENT + 1:
|
||||||
cpu.screen.blit(cpu.background, [0, 0])
|
screen.blit(background, [0, 0])
|
||||||
cpu.screen.blit(pygame.transform.smoothscale(cpu.terminal, (512, 512)), [150, 140])
|
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.set_caption('({0:.2f} MHz) Orao Emulator v0.1'.format(ratio))
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
12
orao/cpu.py
12
orao/cpu.py
|
@ -1,10 +1,10 @@
|
||||||
#!/usr/bin/python2
|
|
||||||
# -*- coding: utf8 -*-
|
# -*- coding: utf8 -*-
|
||||||
|
|
||||||
import pygame, numpy, sys, datetime, wave, time
|
import wave
|
||||||
|
|
||||||
class CPU(object):
|
class CPU(object):
|
||||||
CARRY, ZERO, INTERRUPT, DECIMAL, BREAK, UNUSED, OVERFLOW, NEGATIVE = [2**i for i in range(8)]
|
CARRY, ZERO, INTERRUPT, DECIMAL, BREAK, UNUSED, OVERFLOW, NEGATIVE = [2**i for i in range(8)]
|
||||||
|
alphaarray = None
|
||||||
|
|
||||||
def __init__(self, memory):
|
def __init__(self, memory):
|
||||||
s, self.tape_out, self.filename, self.samples = self, None, None, 0
|
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.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}
|
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 get_flag(self, flag): return self.flags & flag != 0
|
||||||
|
|
||||||
def set_flag(self, flag, boolean):
|
def set_flag(self, flag, boolean):
|
||||||
|
@ -116,6 +109,7 @@ class CPU(object):
|
||||||
|
|
||||||
if addr == 0x8800: self.speaker() # Zvucnik
|
if addr == 0x8800: self.speaker() # Zvucnik
|
||||||
if 0x6000 <= addr <= 0x7FFF: # Video RAM
|
if 0x6000 <= addr <= 0x7FFF: # Video RAM
|
||||||
|
if self.alphaarray is not None:
|
||||||
y, x = divmod((addr - 0x6000) * 8, 256)
|
y, x = divmod((addr - 0x6000) * 8, 256)
|
||||||
for i in range(8):
|
for i in range(8):
|
||||||
self.alphaarray[x+i, y] = 255 if (val>>i) & 1 else 0 # Transparency mask
|
self.alphaarray[x+i, y] = 255 if (val>>i) & 1 else 0 # Transparency mask
|
||||||
|
|
Loading…
Reference in a new issue