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