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