ULA

Annotated main.py

88:195f22f02848
2016-06-19 Paul Boddie Replaced row spacing variables with row height and row offset variables.
paul@6 1
#!/usr/bin/env python
paul@6 2
paul@6 3
"""
paul@6 4
Acorn Electron ULA simulation controller.
paul@71 5
paul@71 6
Copyright (C) 2011, 2012, 2013, 2014 Paul Boddie <paul@boddie.org.uk>
paul@71 7
paul@71 8
This program is free software; you can redistribute it and/or modify it under
paul@71 9
the terms of the GNU General Public License as published by the Free Software
paul@71 10
Foundation; either version 3 of the License, or (at your option) any later
paul@71 11
version.
paul@71 12
paul@71 13
This program is distributed in the hope that it will be useful, but WITHOUT ANY
paul@71 14
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
paul@71 15
PARTICULAR PURPOSE.  See the GNU General Public License for more details.
paul@71 16
paul@71 17
You should have received a copy of the GNU General Public License along
paul@71 18
with this program.  If not, see <http://www.gnu.org/licenses/>.
paul@6 19
"""
paul@6 20
paul@6 21
from ula import *
paul@6 22
import pygame
paul@6 23
paul@22 24
WIDTH = 640
paul@22 25
HEIGHT = 512
paul@22 26
INTENSITY = 255
paul@22 27
paul@22 28
def update_screen(screen, screen_array):
paul@22 29
paul@22 30
    """
paul@22 31
    Update the host's 'screen' surface with the contents of the 'screen_array'
paul@22 32
    containing the pixel content of the screen.
paul@22 33
    """
paul@22 34
paul@25 35
    surface = pygame.Surface((MAX_WIDTH, MAX_HEIGHT), 0, screen)
paul@29 36
    sa = pygame.surfarray.pixels3d(surface)
paul@29 37
    a = sa.transpose(1, 0, 2)
paul@22 38
paul@22 39
    # Copy the array to a surface and apply a pixel intensity.
paul@22 40
paul@22 41
    try:
paul@29 42
        a.flat[:] = screen_array
paul@22 43
        a *= INTENSITY
paul@22 44
    finally:
paul@29 45
        del a, sa
paul@22 46
paul@22 47
    # Scale the surface to the dimensions of the host's screen and copy the
paul@22 48
    # result to the host's screen.
paul@22 49
paul@22 50
    screen.blit(pygame.transform.scale(surface, (WIDTH, HEIGHT)), (0, 0))
paul@10 51
paul@6 52
def mainloop():
paul@6 53
    while 1:
paul@6 54
        pygame.display.flip()
paul@6 55
        event = pygame.event.wait()
paul@6 56
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
paul@6 57
            break
paul@6 58
paul@6 59
if __name__ == "__main__":
paul@6 60
    pygame.init()
paul@6 61
    screen = pygame.display.set_mode((WIDTH, HEIGHT), 0)
paul@6 62
paul@11 63
    ula = get_ula()
paul@6 64
paul@6 65
    # Test MODE 2.
paul@6 66
paul@31 67
    ula.set_mode(2); ula.reset()
paul@6 68
paul@40 69
    ula.ram.fill(0x3000, 0x5800 - 320, encode((1, 6), 4))
paul@40 70
    ula.ram.fill(0x5800 - 320, 0x8000, encode((2, 7), 4))
paul@29 71
    ula_screen = update(ula)
paul@22 72
    update_screen(screen, ula_screen)
paul@40 73
    print "Screen updated."
paul@6 74
    mainloop()
paul@6 75
paul@6 76
    ula.screen_start = 0x3000 + 2
paul@29 77
    ula_screen = update(ula)
paul@22 78
    update_screen(screen, ula_screen)
paul@40 79
    print "Screen updated."
paul@6 80
    mainloop()
paul@6 81
paul@6 82
    # Test MODE 6.
paul@6 83
paul@31 84
    ula.set_mode(6); ula.reset()
paul@6 85
paul@40 86
    ula.ram.fill(0x6000, 0x6f00 + 160, encode((1, 0, 1, 1, 0, 0, 1, 1), 1))
paul@40 87
    ula.ram.fill(0x6f00 + 160, 0x7f40, encode((1, 0, 1, 0, 1, 0, 1, 0), 1))
paul@29 88
    ula_screen = update(ula)
paul@22 89
    update_screen(screen, ula_screen)
paul@40 90
    print "Screen updated."
paul@6 91
    mainloop()
paul@6 92
paul@6 93
    ula.screen_start = 0x6f00 + 160
paul@29 94
    ula_screen = update(ula)
paul@22 95
    update_screen(screen, ula_screen)
paul@40 96
    print "Screen updated."
paul@6 97
    mainloop()
paul@6 98
paul@6 99
# vim: tabstop=4 expandtab shiftwidth=4