# HG changeset patch # User Paul Boddie # Date 1324232960 -3600 # Node ID 2d6bb0876770d810e7b1898f1d524b4240753ff7 # Parent 6d55dd10da35d44bff06745d6c3c48b0809da8fa Replaced the use of nested lists with a flat, genuine, global screen array accessed directly from the update function. Made use of array transposition when copying the screen array into a surface array. diff -r 6d55dd10da35 -r 2d6bb0876770 main.py --- a/main.py Sun Dec 18 18:55:01 2011 +0100 +++ b/main.py Sun Dec 18 19:29:20 2011 +0100 @@ -19,15 +19,16 @@ """ surface = pygame.Surface((MAX_WIDTH, MAX_HEIGHT), 0, screen) - a = pygame.surfarray.pixels3d(surface) + sa = pygame.surfarray.pixels3d(surface) + a = sa.transpose(1, 0, 2) # Copy the array to a surface and apply a pixel intensity. try: - a[:] = screen_array + a.flat[:] = screen_array a *= INTENSITY finally: - del a + del a, sa # Scale the surface to the dimensions of the host's screen and copy the # result to the host's screen. @@ -46,7 +47,6 @@ screen = pygame.display.set_mode((WIDTH, HEIGHT), 0) ula = get_ula() - ula_screen = get_screen() # Test MODE 2. @@ -54,12 +54,12 @@ ula.fill(0x3000, 0x5800 - 320, encode((1, 6), 4)) ula.fill(0x5800 - 320, 0x8000, encode((2, 7), 4)) - update(ula_screen, ula) + ula_screen = update(ula) update_screen(screen, ula_screen) mainloop() ula.screen_start = 0x3000 + 2 - update(ula_screen, ula) + ula_screen = update(ula) update_screen(screen, ula_screen) mainloop() @@ -69,12 +69,12 @@ ula.fill(0x6000, 0x6f00 + 160, encode((1, 0, 1, 1, 0, 0, 1, 1), 1)) ula.fill(0x6f00 + 160, 0x7f40, encode((1, 0, 1, 0, 1, 0, 1, 0), 1)) - update(ula_screen, ula) + ula_screen = update(ula) update_screen(screen, ula_screen) mainloop() ula.screen_start = 0x6f00 + 160 - update(ula_screen, ula) + ula_screen = update(ula) update_screen(screen, ula_screen) mainloop() diff -r 6d55dd10da35 -r 2d6bb0876770 ula.py --- a/ula.py Sun Dec 18 18:55:01 2011 +0100 +++ b/ula.py Sun Dec 18 19:29:20 2011 +0100 @@ -4,6 +4,9 @@ Acorn Electron ULA simulation. """ +from array import array +from itertools import repeat + LINES_PER_ROW = 8 # the number of pixel lines per character row MAX_HEIGHT = 256 # the height of the screen in pixels MAX_SCANLINE = 312 # the number of scanlines in each frame @@ -13,26 +16,33 @@ MAX_MEMORY = 0x10000 # the number of addressable memory locations BLANK = (0, 0, 0) -def update(screen, ula): +screen = array("B", repeat(0, MAX_WIDTH * 3 * MAX_HEIGHT)) + +def update(ula): """ - Update the 'screen' array by reading from the 'ula'. This function - effectively has the role of the video circuit, but also provides the clock - signal to the ULA. + Return a screen array by reading from the 'ula'. This function effectively + has the role of the video circuit, but also provides the clock signal to the + ULA. """ ula.vsync() + pos = 0 y = 0 while y < MAX_SCANLINE: x = 0 while x < MAX_SCANPOS: colour = ula.get_pixel_colour() if x < MAX_WIDTH and y < MAX_HEIGHT: - screen[x][y] = colour + screen[pos] = colour[0]; pos += 1 + screen[pos] = colour[1]; pos += 1 + screen[pos] = colour[2]; pos += 1 x += 1 ula.hsync() y += 1 + return screen + class ULA: "The ULA functionality." @@ -276,17 +286,6 @@ return [0] * MAX_MEMORY -def get_screen(): - - "Return a list of arrays representing the display." - - x = 0 - screen = [] - while x < MAX_WIDTH: - screen.append([(0, 0, 0)] * MAX_HEIGHT) - x += 1 - return screen - def fill(memory, start, end, value): for i in xrange(start, end): memory[i] = value @@ -301,7 +300,6 @@ # Make a simple two-dimensional array of tuples (three-dimensional in pygame # terminology). - a = get_screen() - update(a, ula) + a = update(ula) # vim: tabstop=4 expandtab shiftwidth=4