zdebel

zdebel / vsync.md

Last active 1 day ago

Like 0

revised this gist 1 day ago · 07d7923

1 file changed, 16 insertions, 13 deletions

vsync.md
@@ -27,7 +27,7 @@ Pico timer, every 16.667 ms
27 27 -> if (F and R1 bit 5): Pico drives /IRQ low (through the I2C port expander)
28 28 -> 65C02 finishes its instruction, enters your IRQ handler
29 29 -> handler reads $8003: F (and 5S, C) cleared
30 - -> Pico notices the cleared flag and releases /IRQ (about 80-90 us later)
30 + -> Pico notices the cleared flag and releases /IRQ (about 150-170 us later)
31 31 ```
32 32
33 33 Numbers, 65C02 at 2.000 MHz:
@@ -36,9 +36,9 @@ Numbers, 65C02 at 2.000 MHz:
36 36 |---|---|
37 37 | frame period | 16.667 ms = **33 333 CPU cycles** |
38 38 | F set -> /IRQ low | up to ~100 us (estimate: one I2C write, possibly queued behind another) |
39 - | status read -> /IRQ high | ~80-90 us (measured indirectly, see 4) |
40 - | extra empty handler entries per frame | 3-5 (measured on the board) |
41 - | cost of those | ~4 x 21 us = ~85 us per frame, **about 0.5 % of the CPU** |
39 + | status read -> /IRQ high | ~150-170 us (measured indirectly on the board, see 4) |
40 + | extra empty handler entries per frame | **7-8** (measured on the board with the full firmware: LCD streaming and keyboard scan running) |
41 + | cost of those | ~8 x 21 us = ~170 us per frame, **about 1 % of the CPU** |
42 42
43 43 Interrupt timing therefore has a few tens of microseconds of jitter. Do not
44 44 use it for cycle-exact effects.
@@ -166,14 +166,17 @@ tell how many frames went by. Remember that this read also clears 5S and C.
166 166
167 167 ## 5. What VSYNC is not
168 168
169 - * The LCD is updated by the other Pico core at its own speed, limited by the
170 - SPI link (one full frame is ~98 KB, about 16 ms of SPI time at 50 MHz before
171 - any rendering), so it is not locked to the timer and cannot be counted on to
172 - reach 60 fps. The renderer
173 - reads VRAM line by line while your program keeps running, so a half-finished
174 - update can show up on screen. Write related changes (e.g. all sprite
175 - attributes) together and quickly, right after the tick, and expect rare
176 - tearing rather than a guaranteed clean frame.
169 + * The LCD is updated by the other Pico core, streamed to the panel by DMA.
170 + The SPI link limits it: one full frame is ~98 KB, **15.73 ms** at 50 MHz, so
171 + the ceiling is ~63 fps. When a frame finishes shortly before the next 60 Hz
172 + tick the renderer waits for the tick and starts the next frame on it, so the
173 + LCD normally runs at exactly 60 fps in step with VSYNC; when frames run slow
174 + it simply runs free. (Added later than the rest of this document and not yet
175 + measured on the board.) The renderer reads VRAM line by line while your
176 + program keeps running, so a half-finished update can show up on screen.
177 + Write related changes (e.g. all sprite attributes) together and quickly,
178 + right after the tick, and expect rare tearing rather than a guaranteed clean
179 + frame.
177 180 * There is no raster position and no per-line interrupt.
178 181 * The frame flag keeps ticking with the display blanked (R1 bit 6 = 0).
179 182
@@ -203,7 +206,7 @@ The stock ROM itself never enables the interrupt (it writes R1 = `$D0` /
203 206 PySide6 and numpy, see `emulator/README.md`). The debugger shows the IRQ pin
204 207 and the VDP interrupt-enable bit. **Machine -> VDP interrupt: IRQ release
205 208 delay** switches between an ideal interrupt (0 us), the board-like delay
206 - (~70 us, the default) and a slow one (150 us). Test your handler with the
209 + (the default, ~7-8 empty entries per frame like the board), and slower ones. Test your handler with the
207 210 board-like setting - with 0 us the empty entries never happen and a handler
208 211 that forgets to test bit 7 would look fine.
209 212 * **Example:** `asm_examples/vsync.a65` (+ `vsync.bin`): the three LEDs count

revised this gist 2 days ago · 4d95950

1 file changed, 228 insertions

vsync.md (file created)

Diff is too large to be shown