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@40 | 12 | MAX_WIDTH = 640 # the width of the screen in pixels |
paul@40 | 13 | |
paul@40 | 14 | MAX_CSYNC = 2 # the scanline during which vsync ends |
paul@40 | 15 | MIN_PIXELLINE = 38 # the first scanline involving pixel generation |
paul@22 | 16 | MAX_SCANLINE = 312 # the number of scanlines in each frame |
paul@40 | 17 | |
paul@40 | 18 | MAX_PIXELLINE = MIN_PIXELLINE + MAX_HEIGHT |
paul@40 | 19 | |
paul@40 | 20 | MAX_HSYNC = 75 # the number of cycles in each hsync period |
paul@40 | 21 | MIN_PIXELPOS = 264 # the first cycle involving pixel generation |
paul@40 | 22 | MAX_SCANPOS = 1024 # the number of cycles in each scanline |
paul@40 | 23 | |
paul@40 | 24 | MAX_PIXELPOS = MIN_PIXELPOS + MAX_WIDTH |
paul@40 | 25 | |
paul@22 | 26 | SCREEN_LIMIT = 0x8000 # the first address after the screen memory |
paul@22 | 27 | MAX_MEMORY = 0x10000 # the number of addressable memory locations |
paul@40 | 28 | MAX_RAM = 0x10000 # the number of addressable RAM locations (64Kb in each IC) |
paul@3 | 29 | BLANK = (0, 0, 0) |
paul@1 | 30 | |
paul@29 | 31 | def update(ula): |
paul@1 | 32 | |
paul@1 | 33 | """ |
paul@31 | 34 | Update the 'ula' for one frame. Return the resulting screen. |
paul@31 | 35 | """ |
paul@31 | 36 | |
paul@31 | 37 | video = ula.video |
paul@31 | 38 | |
paul@31 | 39 | i = 0 |
paul@31 | 40 | limit = MAX_SCANLINE * MAX_SCANPOS |
paul@31 | 41 | while i < limit: |
paul@31 | 42 | ula.update() |
paul@31 | 43 | video.update() |
paul@31 | 44 | i += 1 |
paul@40 | 45 | |
paul@31 | 46 | return video.screen |
paul@31 | 47 | |
paul@31 | 48 | class Video: |
paul@31 | 49 | |
paul@31 | 50 | """ |
paul@31 | 51 | A class representing the video circuitry. |
paul@1 | 52 | """ |
paul@1 | 53 | |
paul@31 | 54 | def __init__(self): |
paul@31 | 55 | self.screen = array("B", repeat(0, MAX_WIDTH * 3 * MAX_HEIGHT)) |
paul@31 | 56 | self.colour = BLANK |
paul@31 | 57 | self.csync = 1 |
paul@31 | 58 | self.hs = 1 |
paul@40 | 59 | self.x = 0 |
paul@40 | 60 | self.y = 0 |
paul@1 | 61 | |
paul@40 | 62 | def set_csync(self, value): |
paul@40 | 63 | if self.csync and not value: |
paul@40 | 64 | self.y = 0 |
paul@40 | 65 | self.pos = 0 |
paul@40 | 66 | self.csync = value |
paul@40 | 67 | |
paul@40 | 68 | def set_hs(self, value): |
paul@40 | 69 | if self.hs and not value: |
paul@40 | 70 | self.x = 0 |
paul@40 | 71 | self.y += 1 |
paul@40 | 72 | self.hs = value |
paul@31 | 73 | |
paul@31 | 74 | def update(self): |
paul@40 | 75 | if MIN_PIXELLINE <= self.y < MAX_PIXELLINE: |
paul@40 | 76 | if MIN_PIXELPOS <= self.x < MAX_PIXELPOS: |
paul@31 | 77 | self.screen[self.pos] = self.colour[0]; self.pos += 1 |
paul@31 | 78 | self.screen[self.pos] = self.colour[1]; self.pos += 1 |
paul@31 | 79 | self.screen[self.pos] = self.colour[2]; self.pos += 1 |
paul@40 | 80 | self.x += 1 |
paul@40 | 81 | |
paul@40 | 82 | class RAM: |
paul@40 | 83 | |
paul@40 | 84 | """ |
paul@40 | 85 | A class representing the RAM circuits (IC4 to IC7). Each circuit |
paul@40 | 86 | traditionally holds 64 kilobits, with two accesses required to read 2 bits |
paul@40 | 87 | from each in order to obtain a whole byte. Here, we model the circuits with |
paul@40 | 88 | a list of 65536 half-bytes with each bit representing a bit stored on a |
paul@40 | 89 | separate IC. |
paul@40 | 90 | """ |
paul@40 | 91 | |
paul@40 | 92 | def __init__(self): |
paul@40 | 93 | |
paul@40 | 94 | "Initialise the RAM circuits." |
paul@40 | 95 | |
paul@40 | 96 | self.memory = [0] * MAX_RAM |
paul@40 | 97 | self.row_address = 0 |
paul@40 | 98 | self.column_address = 0 |
paul@40 | 99 | self.data = 0 |
paul@40 | 100 | |
paul@40 | 101 | def row_select(self, address): |
paul@40 | 102 | self.row_address = address |
paul@40 | 103 | |
paul@40 | 104 | def row_deselect(self): |
paul@40 | 105 | pass |
paul@40 | 106 | |
paul@40 | 107 | def column_select(self, address): |
paul@40 | 108 | self.column_address = address |
paul@40 | 109 | |
paul@40 | 110 | # Read the data. |
paul@40 | 111 | |
paul@40 | 112 | self.data = self.memory[self.row_address << 8 | self.column_address] |
paul@40 | 113 | |
paul@40 | 114 | def column_deselect(self): |
paul@40 | 115 | pass |
paul@40 | 116 | |
paul@40 | 117 | # Convenience methods. |
paul@40 | 118 | |
paul@40 | 119 | def fill(self, start, end, value): |
paul@40 | 120 | for i in xrange(start, end): |
paul@40 | 121 | self.memory[i << 1] = value >> 4 |
paul@40 | 122 | self.memory[i << 1 | 0x1] = value & 0xf |
paul@29 | 123 | |
paul@2 | 124 | class ULA: |
paul@2 | 125 | |
paul@31 | 126 | """ |
paul@31 | 127 | A class providing the ULA functionality. Instances of this class refer to |
paul@31 | 128 | the system memory, maintain internal state (such as information about the |
paul@31 | 129 | current screen mode), and provide outputs (such as the current pixel |
paul@31 | 130 | colour). |
paul@31 | 131 | """ |
paul@1 | 132 | |
paul@2 | 133 | modes = [ |
paul@2 | 134 | (640, 1, 32), (320, 2, 32), (160, 4, 32), # (width, depth, rows) |
paul@3 | 135 | (640, 1, 25), (320, 1, 32), (160, 2, 32), |
paul@3 | 136 | (320, 1, 25) |
paul@2 | 137 | ] |
paul@2 | 138 | |
paul@2 | 139 | palette = range(0, 8) * 2 |
paul@2 | 140 | |
paul@40 | 141 | def __init__(self, ram, video): |
paul@1 | 142 | |
paul@40 | 143 | "Initialise the ULA with the given 'ram' and 'video' instances." |
paul@2 | 144 | |
paul@40 | 145 | self.ram = ram |
paul@31 | 146 | self.video = video |
paul@2 | 147 | self.set_mode(6) |
paul@1 | 148 | |
paul@31 | 149 | self.reset() |
paul@31 | 150 | |
paul@31 | 151 | def reset(self): |
paul@31 | 152 | |
paul@31 | 153 | "Reset the ULA." |
paul@31 | 154 | |
paul@40 | 155 | # Internal state. |
paul@40 | 156 | |
paul@40 | 157 | self.cycle = 0 # counter within each 2MHz period |
paul@40 | 158 | self.access = 0 # counter used to determine whether a byte needs reading |
paul@40 | 159 | self.ram_address = 0 # address given to the RAM |
paul@40 | 160 | self.data = 0 # data read from the RAM |
paul@40 | 161 | self.buffer = [0] * 8 # pixel buffer for decoded RAM data |
paul@40 | 162 | |
paul@40 | 163 | self.reset_vertical() |
paul@31 | 164 | |
paul@2 | 165 | def set_mode(self, mode): |
paul@1 | 166 | |
paul@2 | 167 | """ |
paul@2 | 168 | For the given 'mode', initialise the... |
paul@1 | 169 | |
paul@2 | 170 | * width in pixels |
paul@2 | 171 | * colour depth in bits per pixel |
paul@2 | 172 | * number of character rows |
paul@2 | 173 | * character row size in bytes |
paul@2 | 174 | * screen size in bytes |
paul@2 | 175 | * default screen start address |
paul@2 | 176 | * horizontal pixel scaling factor |
paul@2 | 177 | * line spacing in pixels |
paul@2 | 178 | * number of entries in the pixel buffer |
paul@31 | 179 | |
paul@31 | 180 | The ULA should be reset after a mode switch in order to cleanly display |
paul@31 | 181 | a full screen. |
paul@2 | 182 | """ |
paul@1 | 183 | |
paul@3 | 184 | self.width, self.depth, rows = self.modes[mode] |
paul@3 | 185 | |
paul@31 | 186 | columns = (self.width * self.depth) / 8 # bits read -> bytes read |
paul@40 | 187 | self.access_frequency = 80 / columns # cycle frequency for reading bytes |
paul@31 | 188 | row_size = columns * LINES_PER_ROW |
paul@2 | 189 | |
paul@3 | 190 | # Memory access configuration. |
paul@4 | 191 | # Note the limitation on positioning the screen start. |
paul@3 | 192 | |
paul@4 | 193 | screen_size = row_size * rows |
paul@4 | 194 | self.screen_start = (SCREEN_LIMIT - screen_size) & 0xff00 |
paul@4 | 195 | self.screen_size = SCREEN_LIMIT - self.screen_start |
paul@3 | 196 | |
paul@3 | 197 | # Scanline configuration. |
paul@1 | 198 | |
paul@22 | 199 | self.xscale = MAX_WIDTH / self.width # pixel width in display pixels |
paul@3 | 200 | self.spacing = MAX_HEIGHT / rows - LINES_PER_ROW # pixels between rows |
paul@3 | 201 | |
paul@3 | 202 | # Start of unused region. |
paul@3 | 203 | |
paul@3 | 204 | self.footer = rows * LINES_PER_ROW |
paul@22 | 205 | self.margin = MAX_SCANLINE - rows * (LINES_PER_ROW + self.spacing) + self.spacing |
paul@3 | 206 | |
paul@3 | 207 | # Internal pixel buffer size. |
paul@3 | 208 | |
paul@2 | 209 | self.buffer_limit = 8 / self.depth |
paul@1 | 210 | |
paul@40 | 211 | def vsync(self, value=0): |
paul@40 | 212 | |
paul@40 | 213 | "Signal the start of a frame." |
paul@40 | 214 | |
paul@40 | 215 | self.csync = value |
paul@40 | 216 | self.video.set_csync(value) |
paul@40 | 217 | |
paul@40 | 218 | def hsync(self, value=0): |
paul@40 | 219 | |
paul@40 | 220 | "Signal the end of a scanline." |
paul@40 | 221 | |
paul@40 | 222 | self.hs = value |
paul@40 | 223 | self.video.set_hs(value) |
paul@40 | 224 | |
paul@40 | 225 | def reset_vertical(self): |
paul@2 | 226 | |
paul@2 | 227 | "Signal the start of a frame." |
paul@1 | 228 | |
paul@2 | 229 | self.line_start = self.address = self.screen_start |
paul@5 | 230 | self.line = self.line_start % LINES_PER_ROW |
paul@3 | 231 | self.ssub = 0 |
paul@31 | 232 | self.y = 0 |
paul@40 | 233 | self.x = 0 |
paul@2 | 234 | |
paul@40 | 235 | def reset_horizontal(self): |
paul@1 | 236 | |
paul@40 | 237 | "Reset horizontal state within the active region of the frame." |
paul@31 | 238 | |
paul@31 | 239 | self.y += 1 |
paul@40 | 240 | self.x = 0 |
paul@40 | 241 | |
paul@40 | 242 | if not self.inside_frame(): |
paul@40 | 243 | return |
paul@2 | 244 | |
paul@3 | 245 | # Support spacing between character rows. |
paul@3 | 246 | |
paul@3 | 247 | if self.ssub: |
paul@3 | 248 | self.ssub -= 1 |
paul@3 | 249 | return |
paul@3 | 250 | |
paul@2 | 251 | self.line += 1 |
paul@2 | 252 | |
paul@3 | 253 | # If not on a row boundary, move to the next line. |
paul@3 | 254 | |
paul@3 | 255 | if self.line % LINES_PER_ROW: |
paul@2 | 256 | self.address = self.line_start + 1 |
paul@2 | 257 | self.wrap_address() |
paul@2 | 258 | |
paul@2 | 259 | # After the end of the last line in a row, the address should already |
paul@2 | 260 | # have been positioned on the last line of the next column. |
paul@1 | 261 | |
paul@2 | 262 | else: |
paul@2 | 263 | self.address -= LINES_PER_ROW - 1 |
paul@2 | 264 | self.wrap_address() |
paul@1 | 265 | |
paul@3 | 266 | # Test for the footer region. |
paul@3 | 267 | |
paul@3 | 268 | if self.spacing and self.line == self.footer: |
paul@22 | 269 | self.ssub = self.margin |
paul@3 | 270 | return |
paul@1 | 271 | |
paul@3 | 272 | # Support spacing between character rows. |
paul@2 | 273 | |
paul@22 | 274 | self.ssub = self.spacing |
paul@3 | 275 | |
paul@3 | 276 | self.line_start = self.address |
paul@1 | 277 | |
paul@40 | 278 | def in_frame(self): return MIN_PIXELLINE <= self.y < MAX_PIXELLINE |
paul@40 | 279 | def inside_frame(self): return MIN_PIXELLINE < self.y < MAX_PIXELLINE |
paul@40 | 280 | def read_pixels(self): return MIN_PIXELPOS - 8 <= self.x < MAX_PIXELPOS - 8 and self.in_frame() |
paul@40 | 281 | def make_pixels(self): return MIN_PIXELPOS <= self.x < MAX_PIXELPOS and self.in_frame() |
paul@31 | 282 | |
paul@31 | 283 | def update(self): |
paul@1 | 284 | |
paul@2 | 285 | """ |
paul@40 | 286 | Update the state of the ULA for each clock cycle. This involves updating |
paul@40 | 287 | the pixel colour by reading from the pixel buffer. |
paul@2 | 288 | """ |
paul@2 | 289 | |
paul@40 | 290 | # Detect the end of the scanline. |
paul@40 | 291 | |
paul@40 | 292 | if self.x == MAX_SCANPOS: |
paul@40 | 293 | self.reset_horizontal() |
paul@40 | 294 | |
paul@40 | 295 | # Detect the end of the frame. |
paul@40 | 296 | |
paul@40 | 297 | if self.y == MAX_SCANLINE: |
paul@40 | 298 | self.reset_vertical() |
paul@40 | 299 | |
paul@40 | 300 | |
paul@40 | 301 | |
paul@40 | 302 | # Clock management. |
paul@40 | 303 | |
paul@40 | 304 | access_ram = self.access == 0 and self.read_pixels() and not self.ssub |
paul@40 | 305 | |
paul@40 | 306 | # Set row address (for ULA access only). |
paul@40 | 307 | |
paul@40 | 308 | if self.cycle == 0: |
paul@40 | 309 | |
paul@40 | 310 | # NOTE: Propagate CPU address here. |
paul@40 | 311 | |
paul@40 | 312 | if access_ram: |
paul@40 | 313 | self.ram_address = (self.address & 0xff80) >> 7 |
paul@40 | 314 | |
paul@40 | 315 | # Latch row address, set column address (for ULA access only). |
paul@40 | 316 | |
paul@40 | 317 | elif self.cycle == 1: |
paul@40 | 318 | |
paul@40 | 319 | # NOTE: Permit CPU access here. |
paul@31 | 320 | |
paul@40 | 321 | if access_ram: |
paul@40 | 322 | self.ram.row_select(self.ram_address) |
paul@40 | 323 | |
paul@40 | 324 | # NOTE: Propagate CPU address here. |
paul@40 | 325 | |
paul@40 | 326 | if access_ram: |
paul@40 | 327 | self.ram_address = (self.address & 0x7f) << 1 |
paul@40 | 328 | |
paul@40 | 329 | # Latch column address. |
paul@40 | 330 | |
paul@40 | 331 | elif self.cycle == 2: |
paul@40 | 332 | |
paul@40 | 333 | # NOTE: Permit CPU access here. |
paul@31 | 334 | |
paul@40 | 335 | if access_ram: |
paul@40 | 336 | self.ram.column_select(self.ram_address) |
paul@40 | 337 | |
paul@40 | 338 | # Read 4 bits (for ULA access only). |
paul@40 | 339 | # NOTE: Perhaps map alternate bits, not half-bytes. |
paul@40 | 340 | |
paul@40 | 341 | elif self.cycle == 3: |
paul@40 | 342 | |
paul@40 | 343 | # NOTE: Propagate CPU data here. |
paul@40 | 344 | |
paul@40 | 345 | if access_ram: |
paul@40 | 346 | self.data = self.ram.data << 4 |
paul@40 | 347 | |
paul@40 | 348 | # Set column address (for ULA access only). |
paul@40 | 349 | |
paul@40 | 350 | elif self.cycle == 4: |
paul@40 | 351 | self.ram.column_deselect() |
paul@31 | 352 | |
paul@40 | 353 | # NOTE: Propagate CPU address here. |
paul@40 | 354 | |
paul@40 | 355 | if access_ram: |
paul@40 | 356 | self.ram_address = (self.address & 0x7f) << 1 | 0x1 |
paul@40 | 357 | |
paul@40 | 358 | # Latch column address. |
paul@40 | 359 | |
paul@40 | 360 | elif self.cycle == 5: |
paul@40 | 361 | |
paul@40 | 362 | # NOTE: Permit CPU access here. |
paul@40 | 363 | |
paul@40 | 364 | if access_ram: |
paul@40 | 365 | self.ram.column_select(self.ram_address) |
paul@31 | 366 | |
paul@40 | 367 | # Read 4 bits (for ULA access only). |
paul@40 | 368 | # NOTE: Perhaps map alternate bits, not half-bytes. |
paul@40 | 369 | |
paul@40 | 370 | elif self.cycle == 6: |
paul@40 | 371 | |
paul@40 | 372 | # NOTE: Propagate CPU data here. |
paul@40 | 373 | |
paul@40 | 374 | if access_ram: |
paul@40 | 375 | self.data = self.data | self.ram.data |
paul@40 | 376 | |
paul@40 | 377 | # Advance to the next column. |
paul@40 | 378 | |
paul@40 | 379 | self.address += LINES_PER_ROW |
paul@40 | 380 | self.wrap_address() |
paul@40 | 381 | |
paul@40 | 382 | # Reset addresses. |
paul@31 | 383 | |
paul@40 | 384 | elif self.cycle == 7: |
paul@40 | 385 | self.ram.column_deselect() |
paul@40 | 386 | self.ram.row_deselect() |
paul@40 | 387 | |
paul@40 | 388 | # Update the RAM access controller. |
paul@40 | 389 | |
paul@40 | 390 | self.access = (self.access + 1) % self.access_frequency |
paul@40 | 391 | |
paul@40 | 392 | self.cycle = (self.cycle + 1) % 8 |
paul@40 | 393 | |
paul@40 | 394 | |
paul@40 | 395 | |
paul@40 | 396 | # Video signalling. |
paul@40 | 397 | |
paul@40 | 398 | # Detect any sync conditions. |
paul@31 | 399 | |
paul@40 | 400 | if self.x == 0: |
paul@40 | 401 | self.hsync() |
paul@40 | 402 | if self.y == 0: |
paul@40 | 403 | self.vsync() |
paul@40 | 404 | |
paul@40 | 405 | # Detect the end of hsync. |
paul@31 | 406 | |
paul@40 | 407 | elif self.x == MAX_HSYNC: |
paul@40 | 408 | self.hsync(1) |
paul@40 | 409 | |
paul@40 | 410 | # Detect the end of vsync. |
paul@40 | 411 | |
paul@40 | 412 | elif self.y == MAX_CSYNC and self.x == MAX_SCANPOS / 2: |
paul@40 | 413 | self.vsync(1) |
paul@40 | 414 | |
paul@40 | 415 | |
paul@40 | 416 | |
paul@40 | 417 | # Pixel production. |
paul@31 | 418 | |
paul@3 | 419 | # Detect spacing between character rows. |
paul@3 | 420 | |
paul@40 | 421 | if not self.make_pixels() or self.ssub: |
paul@31 | 422 | self.video.colour = BLANK |
paul@3 | 423 | |
paul@31 | 424 | # For pixels within the frame, obtain and output the value. |
paul@31 | 425 | |
paul@31 | 426 | else: |
paul@40 | 427 | # Detect the start of the pixel generation. |
paul@40 | 428 | |
paul@40 | 429 | if self.x == MIN_PIXELPOS: |
paul@40 | 430 | self.xcounter = self.xscale |
paul@40 | 431 | self.buffer_index = 0 |
paul@40 | 432 | self.fill_pixel_buffer() |
paul@1 | 433 | |
paul@31 | 434 | # Scale pixels horizontally, only accessing the next pixel value |
paul@31 | 435 | # after the required number of scan positions. |
paul@22 | 436 | |
paul@40 | 437 | elif self.xcounter == 0: |
paul@40 | 438 | self.xcounter = self.xscale |
paul@31 | 439 | self.buffer_index += 1 |
paul@31 | 440 | |
paul@40 | 441 | # Fill the pixel buffer, assuming that data is available. |
paul@22 | 442 | |
paul@40 | 443 | if self.buffer_index >= self.buffer_limit: |
paul@40 | 444 | self.buffer_index = 0 |
paul@40 | 445 | self.fill_pixel_buffer() |
paul@22 | 446 | |
paul@40 | 447 | self.xcounter -= 1 |
paul@31 | 448 | self.video.colour = self.buffer[self.buffer_index] |
paul@2 | 449 | |
paul@31 | 450 | self.x += 1 |
paul@2 | 451 | |
paul@2 | 452 | def fill_pixel_buffer(self): |
paul@1 | 453 | |
paul@2 | 454 | """ |
paul@2 | 455 | Fill the pixel buffer by translating memory content for the current |
paul@2 | 456 | mode. |
paul@2 | 457 | """ |
paul@1 | 458 | |
paul@40 | 459 | byte_value = self.data # which should have been read automatically |
paul@1 | 460 | |
paul@2 | 461 | i = 0 |
paul@2 | 462 | for colour in decode(byte_value, self.depth): |
paul@2 | 463 | self.buffer[i] = get_physical_colour(self.palette[colour]) |
paul@2 | 464 | i += 1 |
paul@2 | 465 | |
paul@2 | 466 | def wrap_address(self): |
paul@2 | 467 | if self.address >= SCREEN_LIMIT: |
paul@2 | 468 | self.address -= self.screen_size |
paul@1 | 469 | |
paul@1 | 470 | def get_physical_colour(value): |
paul@1 | 471 | |
paul@1 | 472 | """ |
paul@1 | 473 | Return the physical colour as an RGB triple for the given 'value'. |
paul@1 | 474 | """ |
paul@1 | 475 | |
paul@1 | 476 | return value & 1, value >> 1 & 1, value >> 2 & 1 |
paul@1 | 477 | |
paul@1 | 478 | def decode(value, depth): |
paul@1 | 479 | |
paul@1 | 480 | """ |
paul@1 | 481 | Decode the given byte 'value' according to the 'depth' in bits per pixel, |
paul@1 | 482 | returning a sequence of pixel values. |
paul@1 | 483 | """ |
paul@1 | 484 | |
paul@1 | 485 | if depth == 1: |
paul@1 | 486 | return (value >> 7, value >> 6 & 1, value >> 5 & 1, value >> 4 & 1, |
paul@1 | 487 | value >> 3 & 1, value >> 2 & 1, value >> 1 & 1, value & 1) |
paul@1 | 488 | elif depth == 2: |
paul@1 | 489 | return (value >> 6 & 2 | value >> 3 & 1, value >> 5 & 2 | value >> 2 & 1, |
paul@1 | 490 | value >> 4 & 2 | value >> 1 & 1, value >> 3 & 2 | value & 1) |
paul@1 | 491 | elif depth == 4: |
paul@1 | 492 | return (value >> 4 & 8 | value >> 3 & 4 | value >> 2 & 2 | value >> 1 & 1, |
paul@1 | 493 | value >> 3 & 8 | value >> 2 & 4 | value >> 1 & 2 | value & 1) |
paul@1 | 494 | else: |
paul@1 | 495 | raise ValueError, "Only depths of 1, 2 and 4 are supported, not %d." % depth |
paul@1 | 496 | |
paul@1 | 497 | # Convenience functions. |
paul@1 | 498 | |
paul@1 | 499 | def encode(values, depth): |
paul@1 | 500 | |
paul@1 | 501 | """ |
paul@1 | 502 | Encode the given 'values' according to the 'depth' in bits per pixel, |
paul@1 | 503 | returning a byte value for the pixels. |
paul@1 | 504 | """ |
paul@1 | 505 | |
paul@1 | 506 | result = 0 |
paul@1 | 507 | |
paul@1 | 508 | if depth == 1: |
paul@1 | 509 | for value in values: |
paul@1 | 510 | result = result << 1 | (value & 1) |
paul@1 | 511 | elif depth == 2: |
paul@1 | 512 | for value in values: |
paul@1 | 513 | result = result << 1 | (value & 2) << 3 | (value & 1) |
paul@1 | 514 | elif depth == 4: |
paul@1 | 515 | for value in values: |
paul@1 | 516 | result = result << 1 | (value & 8) << 3 | (value & 4) << 2 | (value & 2) << 1 | (value & 1) |
paul@1 | 517 | else: |
paul@1 | 518 | raise ValueError, "Only depths of 1, 2 and 4 are supported, not %d." % depth |
paul@1 | 519 | |
paul@1 | 520 | return result |
paul@1 | 521 | |
paul@11 | 522 | def get_ula(): |
paul@11 | 523 | |
paul@31 | 524 | "Return a ULA initialised with a memory array and video." |
paul@31 | 525 | |
paul@40 | 526 | return ULA(get_ram(), get_video()) |
paul@11 | 527 | |
paul@31 | 528 | def get_video(): |
paul@31 | 529 | |
paul@31 | 530 | "Return a video circuit." |
paul@31 | 531 | |
paul@31 | 532 | return Video() |
paul@11 | 533 | |
paul@40 | 534 | def get_ram(): |
paul@10 | 535 | |
paul@40 | 536 | "Return an instance representing the computer's RAM hardware." |
paul@7 | 537 | |
paul@40 | 538 | return RAM() |
paul@1 | 539 | |
paul@7 | 540 | # Test program providing coverage (necessary for compilers like Shedskin). |
paul@7 | 541 | |
paul@7 | 542 | if __name__ == "__main__": |
paul@11 | 543 | ula = get_ula() |
paul@7 | 544 | ula.set_mode(2) |
paul@40 | 545 | ula.reset() |
paul@40 | 546 | ula.ram.fill(0x5800 - 320, 0x8000, encode((2, 7), 4)) |
paul@7 | 547 | |
paul@7 | 548 | # Make a simple two-dimensional array of tuples (three-dimensional in pygame |
paul@7 | 549 | # terminology). |
paul@7 | 550 | |
paul@29 | 551 | a = update(ula) |
paul@7 | 552 | |
paul@1 | 553 | # vim: tabstop=4 expandtab shiftwidth=4 |