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