paul@1 | 1 | #!/usr/bin/env python |
paul@1 | 2 | |
paul@1 | 3 | """ |
paul@1 | 4 | Acorn Electron ULA simulation. |
paul@1 | 5 | """ |
paul@1 | 6 | |
paul@7 | 7 | import array |
paul@7 | 8 | import itertools |
paul@7 | 9 | |
paul@1 | 10 | WIDTH = 640 |
paul@1 | 11 | HEIGHT = 512 |
paul@3 | 12 | INTENSITY = 255 |
paul@1 | 13 | |
paul@1 | 14 | LINES_PER_ROW = 8 |
paul@1 | 15 | MAX_HEIGHT = 256 |
paul@1 | 16 | SCREEN_LIMIT = 0x8000 |
paul@1 | 17 | MAX_MEMORY = 0x10000 |
paul@3 | 18 | BLANK = (0, 0, 0) |
paul@1 | 19 | |
paul@2 | 20 | def update(screen, ula): |
paul@1 | 21 | |
paul@1 | 22 | """ |
paul@2 | 23 | Update the 'screen' array by reading from the 'ula'. |
paul@1 | 24 | """ |
paul@1 | 25 | |
paul@2 | 26 | ula.vsync() |
paul@2 | 27 | y = 0 |
paul@2 | 28 | while y < HEIGHT: |
paul@2 | 29 | x = 0 |
paul@2 | 30 | while x < WIDTH: |
paul@2 | 31 | colour = ula.get_pixel_colour() |
paul@17 | 32 | screen[x][y] = colour |
paul@2 | 33 | x += 1 |
paul@2 | 34 | ula.hsync() |
paul@2 | 35 | y += 1 |
paul@1 | 36 | |
paul@2 | 37 | class ULA: |
paul@2 | 38 | |
paul@2 | 39 | "The ULA functionality." |
paul@1 | 40 | |
paul@2 | 41 | modes = [ |
paul@2 | 42 | (640, 1, 32), (320, 2, 32), (160, 4, 32), # (width, depth, rows) |
paul@3 | 43 | (640, 1, 25), (320, 1, 32), (160, 2, 32), |
paul@3 | 44 | (320, 1, 25) |
paul@2 | 45 | ] |
paul@2 | 46 | |
paul@2 | 47 | palette = range(0, 8) * 2 |
paul@2 | 48 | |
paul@2 | 49 | def __init__(self, memory): |
paul@1 | 50 | |
paul@2 | 51 | "Initialise the ULA with the given 'memory'." |
paul@2 | 52 | |
paul@2 | 53 | self.memory = memory |
paul@2 | 54 | self.set_mode(6) |
paul@1 | 55 | |
paul@2 | 56 | # Internal state. |
paul@2 | 57 | |
paul@2 | 58 | self.buffer = [0] * 8 |
paul@2 | 59 | |
paul@2 | 60 | def set_mode(self, mode): |
paul@1 | 61 | |
paul@2 | 62 | """ |
paul@2 | 63 | For the given 'mode', initialise the... |
paul@1 | 64 | |
paul@2 | 65 | * width in pixels |
paul@2 | 66 | * colour depth in bits per pixel |
paul@2 | 67 | * number of character rows |
paul@2 | 68 | * character row size in bytes |
paul@2 | 69 | * screen size in bytes |
paul@2 | 70 | * default screen start address |
paul@2 | 71 | * horizontal pixel scaling factor |
paul@2 | 72 | * vertical pixel scaling factor |
paul@2 | 73 | * line spacing in pixels |
paul@2 | 74 | * number of entries in the pixel buffer |
paul@2 | 75 | """ |
paul@1 | 76 | |
paul@3 | 77 | self.width, self.depth, rows = self.modes[mode] |
paul@3 | 78 | |
paul@3 | 79 | row_size = (self.width * self.depth * LINES_PER_ROW) / 8 # bits per row -> bytes per row |
paul@2 | 80 | |
paul@3 | 81 | # Memory access configuration. |
paul@4 | 82 | # Note the limitation on positioning the screen start. |
paul@3 | 83 | |
paul@4 | 84 | screen_size = row_size * rows |
paul@4 | 85 | self.screen_start = (SCREEN_LIMIT - screen_size) & 0xff00 |
paul@4 | 86 | self.screen_size = SCREEN_LIMIT - self.screen_start |
paul@3 | 87 | |
paul@3 | 88 | # Scanline configuration. |
paul@1 | 89 | |
paul@3 | 90 | self.xscale = WIDTH / self.width # pixel width in display pixels |
paul@3 | 91 | self.yscale = HEIGHT / (rows * LINES_PER_ROW) # pixel height in display pixels |
paul@3 | 92 | |
paul@3 | 93 | self.spacing = MAX_HEIGHT / rows - LINES_PER_ROW # pixels between rows |
paul@3 | 94 | |
paul@3 | 95 | # Start of unused region. |
paul@3 | 96 | |
paul@3 | 97 | self.footer = rows * LINES_PER_ROW |
paul@3 | 98 | self.margin = MAX_HEIGHT - rows * (LINES_PER_ROW + self.spacing) + self.spacing |
paul@3 | 99 | |
paul@3 | 100 | # Internal pixel buffer size. |
paul@3 | 101 | |
paul@2 | 102 | self.buffer_limit = 8 / self.depth |
paul@1 | 103 | |
paul@2 | 104 | def vsync(self): |
paul@2 | 105 | |
paul@2 | 106 | "Signal the start of a frame." |
paul@1 | 107 | |
paul@2 | 108 | self.line_start = self.address = self.screen_start |
paul@5 | 109 | self.line = self.line_start % LINES_PER_ROW |
paul@2 | 110 | self.ysub = 0 |
paul@3 | 111 | self.ssub = 0 |
paul@2 | 112 | self.reset_horizontal() |
paul@1 | 113 | |
paul@2 | 114 | def reset_horizontal(self): |
paul@2 | 115 | |
paul@2 | 116 | "Reset horizontal state." |
paul@2 | 117 | |
paul@2 | 118 | self.xsub = 0 |
paul@2 | 119 | self.buffer_index = self.buffer_limit # need refill |
paul@1 | 120 | |
paul@2 | 121 | def hsync(self): |
paul@2 | 122 | |
paul@2 | 123 | "Signal the end of a line." |
paul@2 | 124 | |
paul@3 | 125 | # Support spacing between character rows. |
paul@3 | 126 | |
paul@3 | 127 | if self.ssub: |
paul@3 | 128 | self.ssub -= 1 |
paul@3 | 129 | return |
paul@3 | 130 | |
paul@2 | 131 | self.reset_horizontal() |
paul@2 | 132 | |
paul@2 | 133 | # Scale pixels vertically. |
paul@1 | 134 | |
paul@2 | 135 | self.ysub += 1 |
paul@2 | 136 | |
paul@2 | 137 | # Re-read the current line if appropriate. |
paul@2 | 138 | |
paul@2 | 139 | if self.ysub < self.yscale: |
paul@2 | 140 | self.address = self.line_start |
paul@2 | 141 | return |
paul@2 | 142 | |
paul@2 | 143 | # Otherwise, move on to the next line. |
paul@1 | 144 | |
paul@2 | 145 | self.ysub = 0 |
paul@2 | 146 | self.line += 1 |
paul@2 | 147 | |
paul@3 | 148 | # If not on a row boundary, move to the next line. |
paul@3 | 149 | |
paul@3 | 150 | if self.line % LINES_PER_ROW: |
paul@2 | 151 | self.address = self.line_start + 1 |
paul@2 | 152 | self.wrap_address() |
paul@2 | 153 | |
paul@2 | 154 | # After the end of the last line in a row, the address should already |
paul@2 | 155 | # have been positioned on the last line of the next column. |
paul@1 | 156 | |
paul@2 | 157 | else: |
paul@2 | 158 | self.address -= LINES_PER_ROW - 1 |
paul@2 | 159 | self.wrap_address() |
paul@1 | 160 | |
paul@3 | 161 | # Test for the footer region. |
paul@3 | 162 | |
paul@3 | 163 | if self.spacing and self.line == self.footer: |
paul@3 | 164 | self.ssub = self.margin * self.yscale |
paul@3 | 165 | return |
paul@1 | 166 | |
paul@3 | 167 | # Support spacing between character rows. |
paul@2 | 168 | |
paul@3 | 169 | self.ssub = self.spacing * self.yscale |
paul@3 | 170 | |
paul@3 | 171 | self.line_start = self.address |
paul@1 | 172 | |
paul@2 | 173 | def get_pixel_colour(self): |
paul@1 | 174 | |
paul@2 | 175 | """ |
paul@2 | 176 | Return a pixel colour by reading from the pixel buffer. |
paul@2 | 177 | """ |
paul@2 | 178 | |
paul@3 | 179 | # Detect spacing between character rows. |
paul@3 | 180 | |
paul@3 | 181 | if self.ssub: |
paul@3 | 182 | return BLANK |
paul@3 | 183 | |
paul@2 | 184 | # Scale pixels horizontally. |
paul@1 | 185 | |
paul@2 | 186 | if self.xsub == self.xscale: |
paul@2 | 187 | self.xsub = 0 |
paul@2 | 188 | self.buffer_index += 1 |
paul@1 | 189 | |
paul@2 | 190 | if self.buffer_index == self.buffer_limit: |
paul@2 | 191 | self.buffer_index = 0 |
paul@2 | 192 | self.fill_pixel_buffer() |
paul@2 | 193 | |
paul@2 | 194 | self.xsub += 1 |
paul@2 | 195 | return self.buffer[self.buffer_index] |
paul@2 | 196 | |
paul@2 | 197 | def fill_pixel_buffer(self): |
paul@1 | 198 | |
paul@2 | 199 | """ |
paul@2 | 200 | Fill the pixel buffer by translating memory content for the current |
paul@2 | 201 | mode. |
paul@2 | 202 | """ |
paul@1 | 203 | |
paul@2 | 204 | byte_value = self.memory[self.address] |
paul@1 | 205 | |
paul@2 | 206 | i = 0 |
paul@2 | 207 | for colour in decode(byte_value, self.depth): |
paul@2 | 208 | self.buffer[i] = get_physical_colour(self.palette[colour]) |
paul@2 | 209 | i += 1 |
paul@2 | 210 | |
paul@2 | 211 | # Advance to the next column. |
paul@1 | 212 | |
paul@2 | 213 | self.address += LINES_PER_ROW |
paul@2 | 214 | self.wrap_address() |
paul@2 | 215 | |
paul@2 | 216 | def wrap_address(self): |
paul@2 | 217 | if self.address >= SCREEN_LIMIT: |
paul@2 | 218 | self.address -= self.screen_size |
paul@1 | 219 | |
paul@11 | 220 | # Convenience methods. |
paul@11 | 221 | |
paul@11 | 222 | def fill(self, start, end, value): |
paul@11 | 223 | fill(self.memory, start, end, value) |
paul@11 | 224 | |
paul@1 | 225 | def get_physical_colour(value): |
paul@1 | 226 | |
paul@1 | 227 | """ |
paul@1 | 228 | Return the physical colour as an RGB triple for the given 'value'. |
paul@1 | 229 | """ |
paul@1 | 230 | |
paul@1 | 231 | return value & 1, value >> 1 & 1, value >> 2 & 1 |
paul@1 | 232 | |
paul@1 | 233 | def decode(value, depth): |
paul@1 | 234 | |
paul@1 | 235 | """ |
paul@1 | 236 | Decode the given byte 'value' according to the 'depth' in bits per pixel, |
paul@1 | 237 | returning a sequence of pixel values. |
paul@1 | 238 | """ |
paul@1 | 239 | |
paul@1 | 240 | if depth == 1: |
paul@1 | 241 | return (value >> 7, value >> 6 & 1, value >> 5 & 1, value >> 4 & 1, |
paul@1 | 242 | value >> 3 & 1, value >> 2 & 1, value >> 1 & 1, value & 1) |
paul@1 | 243 | elif depth == 2: |
paul@1 | 244 | return (value >> 6 & 2 | value >> 3 & 1, value >> 5 & 2 | value >> 2 & 1, |
paul@1 | 245 | value >> 4 & 2 | value >> 1 & 1, value >> 3 & 2 | value & 1) |
paul@1 | 246 | elif depth == 4: |
paul@1 | 247 | return (value >> 4 & 8 | value >> 3 & 4 | value >> 2 & 2 | value >> 1 & 1, |
paul@1 | 248 | value >> 3 & 8 | value >> 2 & 4 | value >> 1 & 2 | value & 1) |
paul@1 | 249 | else: |
paul@1 | 250 | raise ValueError, "Only depths of 1, 2 and 4 are supported, not %d." % depth |
paul@1 | 251 | |
paul@1 | 252 | # Convenience functions. |
paul@1 | 253 | |
paul@1 | 254 | def encode(values, depth): |
paul@1 | 255 | |
paul@1 | 256 | """ |
paul@1 | 257 | Encode the given 'values' according to the 'depth' in bits per pixel, |
paul@1 | 258 | returning a byte value for the pixels. |
paul@1 | 259 | """ |
paul@1 | 260 | |
paul@1 | 261 | result = 0 |
paul@1 | 262 | |
paul@1 | 263 | if depth == 1: |
paul@1 | 264 | for value in values: |
paul@1 | 265 | result = result << 1 | (value & 1) |
paul@1 | 266 | elif depth == 2: |
paul@1 | 267 | for value in values: |
paul@1 | 268 | result = result << 1 | (value & 2) << 3 | (value & 1) |
paul@1 | 269 | elif depth == 4: |
paul@1 | 270 | for value in values: |
paul@1 | 271 | result = result << 1 | (value & 8) << 3 | (value & 4) << 2 | (value & 2) << 1 | (value & 1) |
paul@1 | 272 | else: |
paul@1 | 273 | raise ValueError, "Only depths of 1, 2 and 4 are supported, not %d." % depth |
paul@1 | 274 | |
paul@1 | 275 | return result |
paul@1 | 276 | |
paul@11 | 277 | def get_ula(): |
paul@11 | 278 | |
paul@11 | 279 | "Return a ULA initialised with a memory array." |
paul@11 | 280 | |
paul@11 | 281 | return ULA(get_memory()) |
paul@11 | 282 | |
paul@7 | 283 | def get_memory(): |
paul@10 | 284 | |
paul@10 | 285 | "Return an array representing the computer's memory." |
paul@10 | 286 | |
paul@7 | 287 | return array.array("B", itertools.repeat(0, MAX_MEMORY)) |
paul@7 | 288 | |
paul@10 | 289 | def get_screen(): |
paul@10 | 290 | |
paul@10 | 291 | "Return a list of arrays representing the display." |
paul@10 | 292 | |
paul@10 | 293 | x = 0 |
paul@10 | 294 | screen = [] |
paul@10 | 295 | while x < WIDTH: |
paul@10 | 296 | y = 0 |
paul@10 | 297 | column = [] |
paul@10 | 298 | while y < HEIGHT: |
paul@10 | 299 | column.append((0, 0, 0)) |
paul@10 | 300 | y += 1 |
paul@10 | 301 | screen.append(column) |
paul@10 | 302 | x += 1 |
paul@10 | 303 | return screen |
paul@10 | 304 | |
paul@1 | 305 | def fill(memory, start, end, value): |
paul@1 | 306 | for i in xrange(start, end): |
paul@1 | 307 | memory[i] = value |
paul@1 | 308 | |
paul@7 | 309 | # Test program providing coverage (necessary for compilers like Shedskin). |
paul@7 | 310 | # NOTE: Running this will actually cause an IndexError. |
paul@7 | 311 | |
paul@7 | 312 | if __name__ == "__main__": |
paul@11 | 313 | ula = get_ula() |
paul@7 | 314 | ula.set_mode(2) |
paul@11 | 315 | ula.fill(0x5800 - 320, 0x8000, encode((2, 7), 4)) |
paul@7 | 316 | |
paul@7 | 317 | # Make a simple two-dimensional array of tuples (three-dimensional in pygame |
paul@7 | 318 | # terminology). |
paul@7 | 319 | |
paul@10 | 320 | a = get_screen() |
paul@7 | 321 | update(a, ula) |
paul@7 | 322 | |
paul@1 | 323 | # vim: tabstop=4 expandtab shiftwidth=4 |