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@29 | 7 | from array import array |
paul@29 | 8 | from itertools import repeat |
paul@29 | 9 | |
paul@22 | 10 | LINES_PER_ROW = 8 # the number of pixel lines per character row |
paul@22 | 11 | MAX_HEIGHT = 256 # the height of the screen in pixels |
paul@22 | 12 | MAX_SCANLINE = 312 # the number of scanlines in each frame |
paul@22 | 13 | MAX_WIDTH = 640 # the width of the screen in pixels |
paul@22 | 14 | MAX_SCANPOS = 1024 # the number of positions in each scanline |
paul@22 | 15 | SCREEN_LIMIT = 0x8000 # the first address after the screen memory |
paul@22 | 16 | MAX_MEMORY = 0x10000 # the number of addressable memory locations |
paul@3 | 17 | BLANK = (0, 0, 0) |
paul@1 | 18 | |
paul@29 | 19 | screen = array("B", repeat(0, MAX_WIDTH * 3 * MAX_HEIGHT)) |
paul@29 | 20 | |
paul@29 | 21 | def update(ula): |
paul@1 | 22 | |
paul@1 | 23 | """ |
paul@29 | 24 | Return a screen array by reading from the 'ula'. This function effectively |
paul@29 | 25 | has the role of the video circuit, but also provides the clock signal to the |
paul@29 | 26 | ULA. |
paul@1 | 27 | """ |
paul@1 | 28 | |
paul@2 | 29 | ula.vsync() |
paul@29 | 30 | pos = 0 |
paul@2 | 31 | y = 0 |
paul@22 | 32 | while y < MAX_SCANLINE: |
paul@2 | 33 | x = 0 |
paul@22 | 34 | while x < MAX_SCANPOS: |
paul@2 | 35 | colour = ula.get_pixel_colour() |
paul@22 | 36 | if x < MAX_WIDTH and y < MAX_HEIGHT: |
paul@29 | 37 | screen[pos] = colour[0]; pos += 1 |
paul@29 | 38 | screen[pos] = colour[1]; pos += 1 |
paul@29 | 39 | screen[pos] = colour[2]; pos += 1 |
paul@2 | 40 | x += 1 |
paul@2 | 41 | ula.hsync() |
paul@2 | 42 | y += 1 |
paul@1 | 43 | |
paul@29 | 44 | return screen |
paul@29 | 45 | |
paul@2 | 46 | class ULA: |
paul@2 | 47 | |
paul@2 | 48 | "The ULA functionality." |
paul@1 | 49 | |
paul@2 | 50 | modes = [ |
paul@2 | 51 | (640, 1, 32), (320, 2, 32), (160, 4, 32), # (width, depth, rows) |
paul@3 | 52 | (640, 1, 25), (320, 1, 32), (160, 2, 32), |
paul@3 | 53 | (320, 1, 25) |
paul@2 | 54 | ] |
paul@2 | 55 | |
paul@2 | 56 | palette = range(0, 8) * 2 |
paul@2 | 57 | |
paul@2 | 58 | def __init__(self, memory): |
paul@1 | 59 | |
paul@2 | 60 | "Initialise the ULA with the given 'memory'." |
paul@2 | 61 | |
paul@2 | 62 | self.memory = memory |
paul@2 | 63 | self.set_mode(6) |
paul@1 | 64 | |
paul@2 | 65 | # Internal state. |
paul@2 | 66 | |
paul@2 | 67 | self.buffer = [0] * 8 |
paul@2 | 68 | |
paul@2 | 69 | def set_mode(self, mode): |
paul@1 | 70 | |
paul@2 | 71 | """ |
paul@2 | 72 | For the given 'mode', initialise the... |
paul@1 | 73 | |
paul@2 | 74 | * width in pixels |
paul@2 | 75 | * colour depth in bits per pixel |
paul@2 | 76 | * number of character rows |
paul@2 | 77 | * character row size in bytes |
paul@2 | 78 | * screen size in bytes |
paul@2 | 79 | * default screen start address |
paul@2 | 80 | * horizontal pixel scaling factor |
paul@2 | 81 | * line spacing in pixels |
paul@2 | 82 | * number of entries in the pixel buffer |
paul@2 | 83 | """ |
paul@1 | 84 | |
paul@3 | 85 | self.width, self.depth, rows = self.modes[mode] |
paul@3 | 86 | |
paul@22 | 87 | self.columns = (self.width * self.depth) / 8 # bits read -> bytes read |
paul@22 | 88 | row_size = self.columns * LINES_PER_ROW |
paul@2 | 89 | |
paul@3 | 90 | # Memory access configuration. |
paul@4 | 91 | # Note the limitation on positioning the screen start. |
paul@3 | 92 | |
paul@4 | 93 | screen_size = row_size * rows |
paul@4 | 94 | self.screen_start = (SCREEN_LIMIT - screen_size) & 0xff00 |
paul@4 | 95 | self.screen_size = SCREEN_LIMIT - self.screen_start |
paul@3 | 96 | |
paul@3 | 97 | # Scanline configuration. |
paul@1 | 98 | |
paul@22 | 99 | self.xscale = MAX_WIDTH / self.width # pixel width in display pixels |
paul@3 | 100 | self.spacing = MAX_HEIGHT / rows - LINES_PER_ROW # pixels between rows |
paul@3 | 101 | |
paul@3 | 102 | # Start of unused region. |
paul@3 | 103 | |
paul@3 | 104 | self.footer = rows * LINES_PER_ROW |
paul@22 | 105 | self.margin = MAX_SCANLINE - rows * (LINES_PER_ROW + self.spacing) + self.spacing |
paul@3 | 106 | |
paul@3 | 107 | # Internal pixel buffer size. |
paul@3 | 108 | |
paul@2 | 109 | self.buffer_limit = 8 / self.depth |
paul@1 | 110 | |
paul@2 | 111 | def vsync(self): |
paul@2 | 112 | |
paul@2 | 113 | "Signal the start of a frame." |
paul@1 | 114 | |
paul@2 | 115 | self.line_start = self.address = self.screen_start |
paul@5 | 116 | self.line = self.line_start % LINES_PER_ROW |
paul@3 | 117 | self.ssub = 0 |
paul@2 | 118 | self.reset_horizontal() |
paul@1 | 119 | |
paul@2 | 120 | def reset_horizontal(self): |
paul@2 | 121 | |
paul@2 | 122 | "Reset horizontal state." |
paul@2 | 123 | |
paul@2 | 124 | self.xsub = 0 |
paul@22 | 125 | self.column = 0 |
paul@2 | 126 | self.buffer_index = self.buffer_limit # need refill |
paul@1 | 127 | |
paul@2 | 128 | def hsync(self): |
paul@2 | 129 | |
paul@2 | 130 | "Signal the end of a line." |
paul@2 | 131 | |
paul@3 | 132 | # Support spacing between character rows. |
paul@3 | 133 | |
paul@3 | 134 | if self.ssub: |
paul@3 | 135 | self.ssub -= 1 |
paul@3 | 136 | return |
paul@3 | 137 | |
paul@2 | 138 | self.reset_horizontal() |
paul@2 | 139 | self.line += 1 |
paul@2 | 140 | |
paul@3 | 141 | # If not on a row boundary, move to the next line. |
paul@3 | 142 | |
paul@3 | 143 | if self.line % LINES_PER_ROW: |
paul@2 | 144 | self.address = self.line_start + 1 |
paul@2 | 145 | self.wrap_address() |
paul@2 | 146 | |
paul@2 | 147 | # After the end of the last line in a row, the address should already |
paul@2 | 148 | # have been positioned on the last line of the next column. |
paul@1 | 149 | |
paul@2 | 150 | else: |
paul@2 | 151 | self.address -= LINES_PER_ROW - 1 |
paul@2 | 152 | self.wrap_address() |
paul@1 | 153 | |
paul@3 | 154 | # Test for the footer region. |
paul@3 | 155 | |
paul@3 | 156 | if self.spacing and self.line == self.footer: |
paul@22 | 157 | self.ssub = self.margin |
paul@3 | 158 | return |
paul@1 | 159 | |
paul@3 | 160 | # Support spacing between character rows. |
paul@2 | 161 | |
paul@22 | 162 | self.ssub = self.spacing |
paul@3 | 163 | |
paul@3 | 164 | self.line_start = self.address |
paul@1 | 165 | |
paul@2 | 166 | def get_pixel_colour(self): |
paul@1 | 167 | |
paul@2 | 168 | """ |
paul@2 | 169 | Return a pixel colour by reading from the pixel buffer. |
paul@2 | 170 | """ |
paul@2 | 171 | |
paul@3 | 172 | # Detect spacing between character rows. |
paul@3 | 173 | |
paul@3 | 174 | if self.ssub: |
paul@3 | 175 | return BLANK |
paul@3 | 176 | |
paul@2 | 177 | # Scale pixels horizontally. |
paul@1 | 178 | |
paul@2 | 179 | if self.xsub == self.xscale: |
paul@2 | 180 | self.xsub = 0 |
paul@2 | 181 | self.buffer_index += 1 |
paul@1 | 182 | |
paul@2 | 183 | if self.buffer_index == self.buffer_limit: |
paul@2 | 184 | self.buffer_index = 0 |
paul@22 | 185 | self.column += 1 |
paul@22 | 186 | |
paul@22 | 187 | # Detect the end of the scanline. |
paul@22 | 188 | |
paul@22 | 189 | if self.column > self.columns: |
paul@22 | 190 | return BLANK |
paul@22 | 191 | |
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@20 | 287 | return [0] * MAX_MEMORY |
paul@7 | 288 | |
paul@1 | 289 | def fill(memory, start, end, value): |
paul@1 | 290 | for i in xrange(start, end): |
paul@1 | 291 | memory[i] = value |
paul@1 | 292 | |
paul@7 | 293 | # Test program providing coverage (necessary for compilers like Shedskin). |
paul@7 | 294 | |
paul@7 | 295 | if __name__ == "__main__": |
paul@11 | 296 | ula = get_ula() |
paul@7 | 297 | ula.set_mode(2) |
paul@11 | 298 | ula.fill(0x5800 - 320, 0x8000, encode((2, 7), 4)) |
paul@7 | 299 | |
paul@7 | 300 | # Make a simple two-dimensional array of tuples (three-dimensional in pygame |
paul@7 | 301 | # terminology). |
paul@7 | 302 | |
paul@29 | 303 | a = update(ula) |
paul@7 | 304 | |
paul@1 | 305 | # vim: tabstop=4 expandtab shiftwidth=4 |