paul@1 | 1 | #!/usr/bin/env python |
paul@1 | 2 | |
paul@1 | 3 | """ |
paul@1 | 4 | Acorn Electron ULA simulation. |
paul@71 | 5 | |
paul@84 | 6 | Copyright (C) 2011, 2012, 2013, 2014, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@71 | 7 | |
paul@71 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@71 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@71 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@71 | 11 | version. |
paul@71 | 12 | |
paul@71 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@71 | 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@71 | 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@71 | 16 | |
paul@71 | 17 | You should have received a copy of the GNU General Public License along |
paul@71 | 18 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@1 | 19 | """ |
paul@1 | 20 | |
paul@29 | 21 | from array import array |
paul@29 | 22 | from itertools import repeat |
paul@29 | 23 | |
paul@22 | 24 | LINES_PER_ROW = 8 # the number of pixel lines per character row |
paul@22 | 25 | MAX_HEIGHT = 256 # the height of the screen in pixels |
paul@40 | 26 | MAX_WIDTH = 640 # the width of the screen in pixels |
paul@40 | 27 | |
paul@40 | 28 | MAX_CSYNC = 2 # the scanline during which vsync ends |
paul@84 | 29 | MIN_PIXELLINE = 28 # the first scanline involving pixel generation |
paul@22 | 30 | MAX_SCANLINE = 312 # the number of scanlines in each frame |
paul@40 | 31 | |
paul@40 | 32 | MAX_PIXELLINE = MIN_PIXELLINE + MAX_HEIGHT |
paul@40 | 33 | |
paul@84 | 34 | MAX_HSYNC = 64 # the number of cycles in each hsync period |
paul@42 | 35 | MIN_PIXELPOS = 256 # the first cycle involving pixel generation |
paul@40 | 36 | MAX_SCANPOS = 1024 # the number of cycles in each scanline |
paul@40 | 37 | |
paul@40 | 38 | MAX_PIXELPOS = MIN_PIXELPOS + MAX_WIDTH |
paul@40 | 39 | |
paul@22 | 40 | SCREEN_LIMIT = 0x8000 # the first address after the screen memory |
paul@22 | 41 | MAX_MEMORY = 0x10000 # the number of addressable memory locations |
paul@40 | 42 | MAX_RAM = 0x10000 # the number of addressable RAM locations (64Kb in each IC) |
paul@3 | 43 | BLANK = (0, 0, 0) |
paul@1 | 44 | |
paul@29 | 45 | def update(ula): |
paul@1 | 46 | |
paul@1 | 47 | """ |
paul@31 | 48 | Update the 'ula' for one frame. Return the resulting screen. |
paul@31 | 49 | """ |
paul@31 | 50 | |
paul@31 | 51 | video = ula.video |
paul@31 | 52 | |
paul@31 | 53 | i = 0 |
paul@31 | 54 | limit = MAX_SCANLINE * MAX_SCANPOS |
paul@31 | 55 | while i < limit: |
paul@95 | 56 | ula.posedge() |
paul@31 | 57 | video.update() |
paul@95 | 58 | ula.negedge() |
paul@31 | 59 | i += 1 |
paul@40 | 60 | |
paul@31 | 61 | return video.screen |
paul@31 | 62 | |
paul@31 | 63 | class Video: |
paul@31 | 64 | |
paul@31 | 65 | """ |
paul@31 | 66 | A class representing the video circuitry. |
paul@1 | 67 | """ |
paul@1 | 68 | |
paul@31 | 69 | def __init__(self): |
paul@31 | 70 | self.screen = array("B", repeat(0, MAX_WIDTH * 3 * MAX_HEIGHT)) |
paul@31 | 71 | self.colour = BLANK |
paul@31 | 72 | self.csync = 1 |
paul@31 | 73 | self.hs = 1 |
paul@40 | 74 | self.x = 0 |
paul@40 | 75 | self.y = 0 |
paul@1 | 76 | |
paul@40 | 77 | def set_csync(self, value): |
paul@40 | 78 | if self.csync and not value: |
paul@40 | 79 | self.y = 0 |
paul@40 | 80 | self.pos = 0 |
paul@40 | 81 | self.csync = value |
paul@40 | 82 | |
paul@40 | 83 | def set_hs(self, value): |
paul@40 | 84 | if self.hs and not value: |
paul@40 | 85 | self.x = 0 |
paul@40 | 86 | self.y += 1 |
paul@40 | 87 | self.hs = value |
paul@31 | 88 | |
paul@31 | 89 | def update(self): |
paul@40 | 90 | if MIN_PIXELLINE <= self.y < MAX_PIXELLINE: |
paul@42 | 91 | if MIN_PIXELPOS + 8 <= self.x < MAX_PIXELPOS + 8: |
paul@31 | 92 | self.screen[self.pos] = self.colour[0]; self.pos += 1 |
paul@31 | 93 | self.screen[self.pos] = self.colour[1]; self.pos += 1 |
paul@31 | 94 | self.screen[self.pos] = self.colour[2]; self.pos += 1 |
paul@40 | 95 | self.x += 1 |
paul@40 | 96 | |
paul@40 | 97 | class RAM: |
paul@40 | 98 | |
paul@40 | 99 | """ |
paul@40 | 100 | A class representing the RAM circuits (IC4 to IC7). Each circuit |
paul@48 | 101 | traditionally holds 64 kilobits, with each access obtaining 1 bit from each |
paul@48 | 102 | IC, and thus two accesses being required to obtain a whole byte. Here, we |
paul@48 | 103 | model the circuits with a list of 65536 half-bytes with each bit in a |
paul@48 | 104 | half-byte representing a bit stored on a separate IC. |
paul@40 | 105 | """ |
paul@40 | 106 | |
paul@40 | 107 | def __init__(self): |
paul@40 | 108 | |
paul@40 | 109 | "Initialise the RAM circuits." |
paul@40 | 110 | |
paul@40 | 111 | self.memory = [0] * MAX_RAM |
paul@40 | 112 | self.row_address = 0 |
paul@40 | 113 | self.column_address = 0 |
paul@40 | 114 | self.data = 0 |
paul@40 | 115 | |
paul@40 | 116 | def row_select(self, address): |
paul@59 | 117 | |
paul@59 | 118 | "The operation of asserting a row 'address' via RA0...RA7." |
paul@59 | 119 | |
paul@40 | 120 | self.row_address = address |
paul@40 | 121 | |
paul@40 | 122 | def row_deselect(self): |
paul@40 | 123 | pass |
paul@40 | 124 | |
paul@40 | 125 | def column_select(self, address): |
paul@59 | 126 | |
paul@59 | 127 | "The operation of asserting a column 'address' via RA0...RA7." |
paul@59 | 128 | |
paul@40 | 129 | self.column_address = address |
paul@40 | 130 | |
paul@40 | 131 | # Read the data. |
paul@40 | 132 | |
paul@40 | 133 | self.data = self.memory[self.row_address << 8 | self.column_address] |
paul@40 | 134 | |
paul@40 | 135 | def column_deselect(self): |
paul@40 | 136 | pass |
paul@40 | 137 | |
paul@40 | 138 | # Convenience methods. |
paul@40 | 139 | |
paul@40 | 140 | def fill(self, start, end, value): |
paul@40 | 141 | for i in xrange(start, end): |
paul@40 | 142 | self.memory[i << 1] = value >> 4 |
paul@40 | 143 | self.memory[i << 1 | 0x1] = value & 0xf |
paul@29 | 144 | |
paul@2 | 145 | class ULA: |
paul@2 | 146 | |
paul@31 | 147 | """ |
paul@31 | 148 | A class providing the ULA functionality. Instances of this class refer to |
paul@31 | 149 | the system memory, maintain internal state (such as information about the |
paul@31 | 150 | current screen mode), and provide outputs (such as the current pixel |
paul@31 | 151 | colour). |
paul@31 | 152 | """ |
paul@1 | 153 | |
paul@2 | 154 | modes = [ |
paul@2 | 155 | (640, 1, 32), (320, 2, 32), (160, 4, 32), # (width, depth, rows) |
paul@3 | 156 | (640, 1, 25), (320, 1, 32), (160, 2, 32), |
paul@3 | 157 | (320, 1, 25) |
paul@2 | 158 | ] |
paul@2 | 159 | |
paul@40 | 160 | def __init__(self, ram, video): |
paul@1 | 161 | |
paul@40 | 162 | "Initialise the ULA with the given 'ram' and 'video' instances." |
paul@2 | 163 | |
paul@40 | 164 | self.ram = ram |
paul@31 | 165 | self.video = video |
paul@2 | 166 | self.set_mode(6) |
paul@91 | 167 | self.palette = map(get_physical_colour, range(0, 8) * 2) |
paul@1 | 168 | |
paul@31 | 169 | self.reset() |
paul@31 | 170 | |
paul@31 | 171 | def reset(self): |
paul@31 | 172 | |
paul@31 | 173 | "Reset the ULA." |
paul@31 | 174 | |
paul@43 | 175 | # General state. |
paul@43 | 176 | |
paul@43 | 177 | self.nmi = 0 # no NMI asserted initially |
paul@43 | 178 | self.irq_vsync = 0 # no IRQ asserted initially |
paul@43 | 179 | |
paul@59 | 180 | # Communication. |
paul@59 | 181 | |
paul@59 | 182 | self.ram_address = 0 # address given to the RAM via RA0...RA7 |
paul@59 | 183 | self.data = 0 # data read from the RAM via RAM0...RAM3 |
paul@59 | 184 | self.cpu_address = 0 # address selected by the CPU via A0...A15 |
paul@59 | 185 | self.cpu_read = 0 # data read/write by the CPU selected using R/W |
paul@59 | 186 | |
paul@40 | 187 | # Internal state. |
paul@40 | 188 | |
paul@40 | 189 | self.access = 0 # counter used to determine whether a byte needs reading |
paul@42 | 190 | self.have_pixels = 0 # whether pixel data has been read |
paul@93 | 191 | self.pdata = 0 # decoded RAM data for pixel output |
paul@93 | 192 | self.cycle = 1 # 8-state counter within each 2MHz period |
paul@93 | 193 | self.pcycle = 0 # 8/4/2-state pixel output counter |
paul@50 | 194 | |
paul@87 | 195 | self.next_frame() |
paul@31 | 196 | |
paul@2 | 197 | def set_mode(self, mode): |
paul@1 | 198 | |
paul@2 | 199 | """ |
paul@2 | 200 | For the given 'mode', initialise the... |
paul@1 | 201 | |
paul@2 | 202 | * width in pixels |
paul@2 | 203 | * colour depth in bits per pixel |
paul@2 | 204 | * number of character rows |
paul@2 | 205 | * character row size in bytes |
paul@2 | 206 | * screen size in bytes |
paul@2 | 207 | * default screen start address |
paul@2 | 208 | * horizontal pixel scaling factor |
paul@88 | 209 | * row height in pixels |
paul@88 | 210 | * display height in pixels |
paul@31 | 211 | |
paul@31 | 212 | The ULA should be reset after a mode switch in order to cleanly display |
paul@31 | 213 | a full screen. |
paul@2 | 214 | """ |
paul@1 | 215 | |
paul@3 | 216 | self.width, self.depth, rows = self.modes[mode] |
paul@3 | 217 | |
paul@31 | 218 | columns = (self.width * self.depth) / 8 # bits read -> bytes read |
paul@40 | 219 | self.access_frequency = 80 / columns # cycle frequency for reading bytes |
paul@31 | 220 | row_size = columns * LINES_PER_ROW |
paul@2 | 221 | |
paul@3 | 222 | # Memory access configuration. |
paul@4 | 223 | # Note the limitation on positioning the screen start. |
paul@3 | 224 | |
paul@4 | 225 | screen_size = row_size * rows |
paul@4 | 226 | self.screen_start = (SCREEN_LIMIT - screen_size) & 0xff00 |
paul@4 | 227 | self.screen_size = SCREEN_LIMIT - self.screen_start |
paul@3 | 228 | |
paul@3 | 229 | # Scanline configuration. |
paul@1 | 230 | |
paul@22 | 231 | self.xscale = MAX_WIDTH / self.width # pixel width in display pixels |
paul@88 | 232 | self.row_height = MAX_HEIGHT / rows # row height in display pixels |
paul@88 | 233 | self.display_height = rows * self.row_height # display height in pixels |
paul@3 | 234 | |
paul@40 | 235 | def vsync(self, value=0): |
paul@40 | 236 | |
paul@40 | 237 | "Signal the start of a frame." |
paul@40 | 238 | |
paul@40 | 239 | self.csync = value |
paul@40 | 240 | self.video.set_csync(value) |
paul@40 | 241 | |
paul@40 | 242 | def hsync(self, value=0): |
paul@40 | 243 | |
paul@40 | 244 | "Signal the end of a scanline." |
paul@40 | 245 | |
paul@40 | 246 | self.hs = value |
paul@40 | 247 | self.video.set_hs(value) |
paul@40 | 248 | |
paul@87 | 249 | def next_frame(self): |
paul@2 | 250 | |
paul@2 | 251 | "Signal the start of a frame." |
paul@1 | 252 | |
paul@2 | 253 | self.line_start = self.address = self.screen_start |
paul@5 | 254 | self.line = self.line_start % LINES_PER_ROW |
paul@31 | 255 | self.y = 0 |
paul@40 | 256 | self.x = 0 |
paul@2 | 257 | |
paul@87 | 258 | def next_horizontal(self): |
paul@87 | 259 | |
paul@87 | 260 | "Visit the next horizontal position." |
paul@87 | 261 | |
paul@87 | 262 | self.address += LINES_PER_ROW |
paul@87 | 263 | self.wrap_address() |
paul@87 | 264 | |
paul@87 | 265 | def next_vertical(self): |
paul@1 | 266 | |
paul@40 | 267 | "Reset horizontal state within the active region of the frame." |
paul@31 | 268 | |
paul@31 | 269 | self.y += 1 |
paul@40 | 270 | self.x = 0 |
paul@40 | 271 | |
paul@96 | 272 | if self.inside_frame(): |
paul@96 | 273 | self.line += 1 |
paul@2 | 274 | |
paul@96 | 275 | # At the end of a row... |
paul@96 | 276 | |
paul@96 | 277 | if self.line == self.row_height: |
paul@2 | 278 | |
paul@96 | 279 | # After the end of the last line in a row, the address should already |
paul@96 | 280 | # have been positioned on the last line of the next column. |
paul@1 | 281 | |
paul@96 | 282 | self.address -= LINES_PER_ROW - 1 |
paul@96 | 283 | self.wrap_address() |
paul@96 | 284 | self.line = 0 |
paul@96 | 285 | |
paul@96 | 286 | # Record the position of the start of the pixel row. |
paul@88 | 287 | |
paul@96 | 288 | self.line_start = self.address |
paul@88 | 289 | |
paul@96 | 290 | # Before any spacing between character rows... |
paul@88 | 291 | |
paul@96 | 292 | elif self.in_line(): |
paul@88 | 293 | |
paul@96 | 294 | # If not on a row boundary, move to the next line. Here, the address |
paul@96 | 295 | # needs bringing back to the previous character row. |
paul@1 | 296 | |
paul@96 | 297 | self.address = self.line_start + 1 |
paul@96 | 298 | self.wrap_address() |
paul@3 | 299 | |
paul@96 | 300 | # Record the position of the start of the pixel row. |
paul@96 | 301 | |
paul@96 | 302 | self.line_start = self.address |
paul@1 | 303 | |
paul@88 | 304 | def in_line(self): return self.line < LINES_PER_ROW |
paul@88 | 305 | def in_frame(self): return MIN_PIXELLINE <= self.y < (MIN_PIXELLINE + self.display_height) |
paul@88 | 306 | def inside_frame(self): return MIN_PIXELLINE < self.y < (MIN_PIXELLINE + self.display_height) |
paul@42 | 307 | def read_pixels(self): return MIN_PIXELPOS <= self.x < MAX_PIXELPOS and self.in_frame() |
paul@94 | 308 | def write_pixels(self): return self.pcycle != 0 |
paul@94 | 309 | def next_pixel(self): return self.xscale == 1 or (self.xscale == 2 and self.cycle & 0b10101010) or (self.xscale == 4 and self.cycle & 0b10001000) |
paul@31 | 310 | |
paul@95 | 311 | def posedge(self): |
paul@1 | 312 | |
paul@2 | 313 | """ |
paul@40 | 314 | Update the state of the ULA for each clock cycle. This involves updating |
paul@40 | 315 | the pixel colour by reading from the pixel buffer. |
paul@2 | 316 | """ |
paul@2 | 317 | |
paul@95 | 318 | # Video signalling. |
paul@95 | 319 | |
paul@40 | 320 | # Detect the end of the scanline. |
paul@40 | 321 | |
paul@40 | 322 | if self.x == MAX_SCANPOS: |
paul@87 | 323 | self.next_vertical() |
paul@40 | 324 | |
paul@40 | 325 | # Detect the end of the frame. |
paul@40 | 326 | |
paul@40 | 327 | if self.y == MAX_SCANLINE: |
paul@87 | 328 | self.next_frame() |
paul@40 | 329 | |
paul@40 | 330 | |
paul@40 | 331 | |
paul@95 | 332 | # Detect any sync conditions. |
paul@95 | 333 | |
paul@95 | 334 | if self.x == 0: |
paul@95 | 335 | self.hsync() |
paul@95 | 336 | if self.y == 0: |
paul@95 | 337 | self.vsync() |
paul@95 | 338 | self.irq_vsync = 0 |
paul@95 | 339 | elif self.y == MAX_PIXELLINE: |
paul@95 | 340 | self.irq_vsync = 1 |
paul@95 | 341 | |
paul@95 | 342 | # Detect the end of hsync. |
paul@95 | 343 | |
paul@95 | 344 | elif self.x == MAX_HSYNC: |
paul@95 | 345 | self.hsync(1) |
paul@95 | 346 | |
paul@95 | 347 | # Detect the end of vsync. |
paul@95 | 348 | |
paul@95 | 349 | elif self.y == MAX_CSYNC and self.x == MAX_SCANPOS / 2: |
paul@95 | 350 | self.vsync(1) |
paul@95 | 351 | |
paul@95 | 352 | |
paul@95 | 353 | |
paul@40 | 354 | # Clock management. |
paul@40 | 355 | |
paul@88 | 356 | would_access_ram = self.access == 0 and self.read_pixels() and self.in_line() |
paul@86 | 357 | access_ram = not self.nmi and would_access_ram |
paul@40 | 358 | |
paul@40 | 359 | # Set row address (for ULA access only). |
paul@40 | 360 | |
paul@93 | 361 | if self.cycle == 1: |
paul@40 | 362 | |
paul@59 | 363 | # Either assert a required address or propagate the CPU address. |
paul@40 | 364 | |
paul@40 | 365 | if access_ram: |
paul@59 | 366 | self.init_row_address(self.address) |
paul@59 | 367 | else: |
paul@59 | 368 | self.init_row_address(self.cpu_address) |
paul@40 | 369 | |
paul@40 | 370 | # Latch row address, set column address (for ULA access only). |
paul@40 | 371 | |
paul@93 | 372 | elif self.cycle == 2: |
paul@40 | 373 | |
paul@59 | 374 | # Select an address needed by the ULA or CPU. |
paul@59 | 375 | |
paul@59 | 376 | self.ram.row_select(self.ram_address) |
paul@59 | 377 | |
paul@59 | 378 | # Either assert a required address or propagate the CPU address. |
paul@31 | 379 | |
paul@40 | 380 | if access_ram: |
paul@59 | 381 | self.init_column_address(self.address, 0) |
paul@59 | 382 | else: |
paul@59 | 383 | self.init_column_address(self.cpu_address, 0) |
paul@40 | 384 | |
paul@40 | 385 | # Latch column address. |
paul@40 | 386 | |
paul@93 | 387 | elif self.cycle == 4: |
paul@40 | 388 | |
paul@59 | 389 | # Select an address needed by the ULA or CPU. |
paul@31 | 390 | |
paul@59 | 391 | self.ram.column_select(self.ram_address) |
paul@40 | 392 | |
paul@40 | 393 | # Read 4 bits (for ULA access only). |
paul@40 | 394 | |
paul@93 | 395 | elif self.cycle == 8: |
paul@40 | 396 | |
paul@59 | 397 | # Either read from a required address or transfer CPU data. |
paul@40 | 398 | |
paul@40 | 399 | if access_ram: |
paul@40 | 400 | self.data = self.ram.data << 4 |
paul@59 | 401 | else: |
paul@59 | 402 | self.cpu_transfer_high() |
paul@40 | 403 | |
paul@40 | 404 | # Set column address (for ULA access only). |
paul@40 | 405 | |
paul@93 | 406 | elif self.cycle == 16: |
paul@40 | 407 | self.ram.column_deselect() |
paul@31 | 408 | |
paul@59 | 409 | # Either assert a required address or propagate the CPU address. |
paul@40 | 410 | |
paul@40 | 411 | if access_ram: |
paul@59 | 412 | self.init_column_address(self.address, 1) |
paul@59 | 413 | else: |
paul@59 | 414 | self.init_column_address(self.cpu_address, 1) |
paul@40 | 415 | |
paul@40 | 416 | # Latch column address. |
paul@40 | 417 | |
paul@93 | 418 | elif self.cycle == 32: |
paul@40 | 419 | |
paul@59 | 420 | # Select an address needed by the ULA or CPU. |
paul@40 | 421 | |
paul@59 | 422 | self.ram.column_select(self.ram_address) |
paul@31 | 423 | |
paul@40 | 424 | # Read 4 bits (for ULA access only). |
paul@40 | 425 | |
paul@93 | 426 | elif self.cycle == 64: |
paul@40 | 427 | |
paul@59 | 428 | # Either read from a required address or transfer CPU data. |
paul@40 | 429 | |
paul@40 | 430 | if access_ram: |
paul@40 | 431 | self.data = self.data | self.ram.data |
paul@42 | 432 | self.have_pixels = 1 |
paul@86 | 433 | else: |
paul@86 | 434 | self.cpu_transfer_low() |
paul@40 | 435 | |
paul@86 | 436 | # Advance to the next column even if an NMI is asserted. |
paul@40 | 437 | |
paul@86 | 438 | if would_access_ram: |
paul@87 | 439 | self.next_horizontal() |
paul@40 | 440 | |
paul@40 | 441 | # Reset addresses. |
paul@31 | 442 | |
paul@93 | 443 | elif self.cycle == 128: |
paul@40 | 444 | self.ram.column_deselect() |
paul@40 | 445 | self.ram.row_deselect() |
paul@40 | 446 | |
paul@40 | 447 | # Update the RAM access controller. |
paul@40 | 448 | |
paul@40 | 449 | self.access = (self.access + 1) % self.access_frequency |
paul@40 | 450 | |
paul@40 | 451 | |
paul@93 | 452 | |
paul@40 | 453 | # Pixel production. |
paul@31 | 454 | |
paul@31 | 455 | # For pixels within the frame, obtain and output the value. |
paul@31 | 456 | |
paul@95 | 457 | if self.write_pixels(): |
paul@92 | 458 | self.output_colour_value() |
paul@1 | 459 | |
paul@31 | 460 | # Scale pixels horizontally, only accessing the next pixel value |
paul@31 | 461 | # after the required number of scan positions. |
paul@22 | 462 | |
paul@93 | 463 | if self.next_pixel(): |
paul@92 | 464 | self.next_pixel_value() |
paul@31 | 465 | |
paul@95 | 466 | # Detect spacing between character rows. |
paul@95 | 467 | |
paul@95 | 468 | else: |
paul@95 | 469 | self.video.colour = BLANK |
paul@95 | 470 | |
paul@95 | 471 | def negedge(self): |
paul@95 | 472 | |
paul@95 | 473 | "Update the state of the device." |
paul@22 | 474 | |
paul@95 | 475 | # Initialise the pixel buffer if appropriate. Output starts after |
paul@95 | 476 | # this cycle. |
paul@93 | 477 | |
paul@95 | 478 | if self.cycle == 128 and self.have_pixels: |
paul@95 | 479 | self.pdata = decode(self.data, self.depth) |
paul@95 | 480 | self.pcycle = 1 |
paul@95 | 481 | self.have_pixels = 0 |
paul@93 | 482 | |
paul@95 | 483 | # Start a new cycle. |
paul@95 | 484 | |
paul@95 | 485 | self.cycle = rotate(self.cycle, 1) |
paul@95 | 486 | self.x += 1 |
paul@2 | 487 | |
paul@92 | 488 | def output_colour_value(self): |
paul@1 | 489 | |
paul@2 | 490 | """ |
paul@92 | 491 | Output the colour value for the current pixel by translating memory |
paul@92 | 492 | content for the current mode. |
paul@2 | 493 | """ |
paul@1 | 494 | |
paul@92 | 495 | value = value_of_bits(self.pdata, self.depth) |
paul@92 | 496 | self.video.colour = self.palette[value] |
paul@92 | 497 | |
paul@92 | 498 | def next_pixel_value(self): |
paul@92 | 499 | self.pdata = rotate(self.pdata, self.depth) |
paul@95 | 500 | self.pcycle = rotate(self.pcycle, self.depth, zero=True) |
paul@2 | 501 | |
paul@2 | 502 | def wrap_address(self): |
paul@2 | 503 | if self.address >= SCREEN_LIMIT: |
paul@2 | 504 | self.address -= self.screen_size |
paul@1 | 505 | |
paul@59 | 506 | def init_row_address(self, address): |
paul@59 | 507 | self.ram_address = (address & 0xff80) >> 7 |
paul@59 | 508 | |
paul@59 | 509 | def init_column_address(self, address, offset): |
paul@59 | 510 | self.ram_address = (address & 0x7f) << 1 | offset |
paul@59 | 511 | |
paul@59 | 512 | def cpu_transfer_high(self): |
paul@59 | 513 | if self.cpu_read: |
paul@59 | 514 | self.cpu_data = self.ram.data << 4 |
paul@59 | 515 | |
paul@59 | 516 | def cpu_transfer_low(self): |
paul@59 | 517 | if self.cpu_read: |
paul@59 | 518 | self.cpu_data = self.data | self.ram.data |
paul@59 | 519 | |
paul@95 | 520 | def rotate(value, depth, width=8, zero=False): |
paul@89 | 521 | |
paul@93 | 522 | """ |
paul@95 | 523 | Return 'value' rotated left by the number of bits given by 'depth', doing so |
paul@95 | 524 | within a value 'width' given in bits. If 'zero' is true, rotate zero bits |
paul@95 | 525 | into the lower bits when rotating. |
paul@93 | 526 | """ |
paul@93 | 527 | |
paul@93 | 528 | field = width - depth |
paul@92 | 529 | top = value >> field |
paul@93 | 530 | mask = 2 ** (width - depth) - 1 |
paul@92 | 531 | rest = value & mask |
paul@95 | 532 | return (rest << depth) | (not zero and top or 0) |
paul@92 | 533 | |
paul@92 | 534 | def value_of_bits(value, depth): |
paul@92 | 535 | |
paul@92 | 536 | """ |
paul@92 | 537 | Convert the upper bits of 'value' to a result, using 'depth' to indicate the |
paul@92 | 538 | number of bits involved. |
paul@92 | 539 | """ |
paul@92 | 540 | |
paul@92 | 541 | return value >> (8 - depth) |
paul@89 | 542 | |
paul@1 | 543 | def get_physical_colour(value): |
paul@1 | 544 | |
paul@1 | 545 | """ |
paul@1 | 546 | Return the physical colour as an RGB triple for the given 'value'. |
paul@1 | 547 | """ |
paul@1 | 548 | |
paul@1 | 549 | return value & 1, value >> 1 & 1, value >> 2 & 1 |
paul@1 | 550 | |
paul@1 | 551 | def decode(value, depth): |
paul@1 | 552 | |
paul@1 | 553 | """ |
paul@1 | 554 | Decode the given byte 'value' according to the 'depth' in bits per pixel, |
paul@1 | 555 | returning a sequence of pixel values. |
paul@1 | 556 | """ |
paul@1 | 557 | |
paul@1 | 558 | if depth == 1: |
paul@92 | 559 | return value |
paul@1 | 560 | elif depth == 2: |
paul@92 | 561 | return ((value & 128) | ((value & 8) << 3) | ((value & 64) >> 1) | ((value & 4) << 2) | |
paul@92 | 562 | ((value & 32) >> 2) | ((value & 2) << 1) | ((value & 16) >> 3) | (value & 1)) |
paul@1 | 563 | elif depth == 4: |
paul@92 | 564 | return ((value & 128) | ((value & 32) << 1) | ((value & 8) << 2) | ((value & 2) << 3) | |
paul@92 | 565 | ((value & 64) >> 3) | ((value & 16) >> 2) | ((value & 4) >> 1) | (value & 1)) |
paul@1 | 566 | else: |
paul@1 | 567 | raise ValueError, "Only depths of 1, 2 and 4 are supported, not %d." % depth |
paul@1 | 568 | |
paul@1 | 569 | # Convenience functions. |
paul@1 | 570 | |
paul@1 | 571 | def encode(values, depth): |
paul@1 | 572 | |
paul@1 | 573 | """ |
paul@1 | 574 | Encode the given 'values' according to the 'depth' in bits per pixel, |
paul@1 | 575 | returning a byte value for the pixels. |
paul@1 | 576 | """ |
paul@1 | 577 | |
paul@1 | 578 | result = 0 |
paul@1 | 579 | |
paul@1 | 580 | if depth == 1: |
paul@1 | 581 | for value in values: |
paul@1 | 582 | result = result << 1 | (value & 1) |
paul@1 | 583 | elif depth == 2: |
paul@1 | 584 | for value in values: |
paul@1 | 585 | result = result << 1 | (value & 2) << 3 | (value & 1) |
paul@1 | 586 | elif depth == 4: |
paul@1 | 587 | for value in values: |
paul@1 | 588 | result = result << 1 | (value & 8) << 3 | (value & 4) << 2 | (value & 2) << 1 | (value & 1) |
paul@1 | 589 | else: |
paul@1 | 590 | raise ValueError, "Only depths of 1, 2 and 4 are supported, not %d." % depth |
paul@1 | 591 | |
paul@1 | 592 | return result |
paul@1 | 593 | |
paul@11 | 594 | def get_ula(): |
paul@11 | 595 | |
paul@31 | 596 | "Return a ULA initialised with a memory array and video." |
paul@31 | 597 | |
paul@40 | 598 | return ULA(get_ram(), get_video()) |
paul@11 | 599 | |
paul@31 | 600 | def get_video(): |
paul@31 | 601 | |
paul@31 | 602 | "Return a video circuit." |
paul@31 | 603 | |
paul@31 | 604 | return Video() |
paul@11 | 605 | |
paul@40 | 606 | def get_ram(): |
paul@10 | 607 | |
paul@40 | 608 | "Return an instance representing the computer's RAM hardware." |
paul@7 | 609 | |
paul@40 | 610 | return RAM() |
paul@1 | 611 | |
paul@7 | 612 | # Test program providing coverage (necessary for compilers like Shedskin). |
paul@7 | 613 | |
paul@7 | 614 | if __name__ == "__main__": |
paul@11 | 615 | ula = get_ula() |
paul@7 | 616 | ula.set_mode(2) |
paul@40 | 617 | ula.reset() |
paul@40 | 618 | ula.ram.fill(0x5800 - 320, 0x8000, encode((2, 7), 4)) |
paul@7 | 619 | |
paul@7 | 620 | # Make a simple two-dimensional array of tuples (three-dimensional in pygame |
paul@7 | 621 | # terminology). |
paul@7 | 622 | |
paul@29 | 623 | a = update(ula) |
paul@7 | 624 | |
paul@1 | 625 | # vim: tabstop=4 expandtab shiftwidth=4 |