ULA

Annotated main.py

32:da771131998b
2011-12-20 Paul Boddie Merged general architecture-related changes.
paul@6 1
#!/usr/bin/env python
paul@6 2
paul@6 3
"""
paul@6 4
Acorn Electron ULA simulation controller.
paul@6 5
"""
paul@6 6
paul@6 7
from ula import *
paul@6 8
import pygame
paul@6 9
paul@22 10
WIDTH = 640
paul@22 11
HEIGHT = 512
paul@22 12
INTENSITY = 255
paul@22 13
paul@22 14
def update_screen(screen, screen_array):
paul@22 15
paul@22 16
    """
paul@22 17
    Update the host's 'screen' surface with the contents of the 'screen_array'
paul@22 18
    containing the pixel content of the screen.
paul@22 19
    """
paul@22 20
paul@25 21
    surface = pygame.Surface((MAX_WIDTH, MAX_HEIGHT), 0, screen)
paul@29 22
    sa = pygame.surfarray.pixels3d(surface)
paul@29 23
    a = sa.transpose(1, 0, 2)
paul@22 24
paul@22 25
    # Copy the array to a surface and apply a pixel intensity.
paul@22 26
paul@22 27
    try:
paul@29 28
        a.flat[:] = screen_array
paul@22 29
        a *= INTENSITY
paul@22 30
    finally:
paul@29 31
        del a, sa
paul@22 32
paul@22 33
    # Scale the surface to the dimensions of the host's screen and copy the
paul@22 34
    # result to the host's screen.
paul@22 35
paul@22 36
    screen.blit(pygame.transform.scale(surface, (WIDTH, HEIGHT)), (0, 0))
paul@10 37
paul@6 38
def mainloop():
paul@6 39
    while 1:
paul@6 40
        pygame.display.flip()
paul@6 41
        event = pygame.event.wait()
paul@6 42
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
paul@6 43
            break
paul@6 44
paul@6 45
if __name__ == "__main__":
paul@6 46
    pygame.init()
paul@6 47
    screen = pygame.display.set_mode((WIDTH, HEIGHT), 0)
paul@6 48
paul@11 49
    ula = get_ula()
paul@6 50
paul@6 51
    # Test MODE 2.
paul@6 52
paul@31 53
    ula.set_mode(2); ula.reset()
paul@6 54
paul@11 55
    ula.fill(0x3000, 0x5800 - 320, encode((1, 6), 4))
paul@11 56
    ula.fill(0x5800 - 320, 0x8000, encode((2, 7), 4))
paul@29 57
    ula_screen = update(ula)
paul@22 58
    update_screen(screen, ula_screen)
paul@6 59
    mainloop()
paul@6 60
paul@6 61
    ula.screen_start = 0x3000 + 2
paul@29 62
    ula_screen = update(ula)
paul@22 63
    update_screen(screen, ula_screen)
paul@6 64
    mainloop()
paul@6 65
paul@6 66
    # Test MODE 6.
paul@6 67
paul@31 68
    ula.set_mode(6); ula.reset()
paul@6 69
paul@11 70
    ula.fill(0x6000, 0x6f00 + 160, encode((1, 0, 1, 1, 0, 0, 1, 1), 1))
paul@11 71
    ula.fill(0x6f00 + 160, 0x7f40, encode((1, 0, 1, 0, 1, 0, 1, 0), 1))
paul@29 72
    ula_screen = update(ula)
paul@22 73
    update_screen(screen, ula_screen)
paul@6 74
    mainloop()
paul@6 75
paul@6 76
    ula.screen_start = 0x6f00 + 160
paul@29 77
    ula_screen = update(ula)
paul@22 78
    update_screen(screen, ula_screen)
paul@6 79
    mainloop()
paul@6 80
paul@6 81
# vim: tabstop=4 expandtab shiftwidth=4