| 1 | // Commander X16 Emulator — VERA VRAM bandwidth accounting. |
| 2 | // |
| 3 | // THE PROBLEM: src/perf_budget.h answers "how much of my frame is my CODE |
| 4 | // using". It says nothing about VERA, and VERA is the other half of the |
| 5 | // question -- a program can have cycles to spare and still tear, drop sprites |
| 6 | // or run out of time to push pixels through the data port. This module |
| 7 | // measures the VRAM bus: what the layers fetch, what the CPU pushes through |
| 8 | // $9F23/$9F24, and what VERA FX multiplies that into. |
| 9 | // |
| 10 | // ─── What the bus actually is ─────────────────────────────────────────────── |
| 11 | // |
| 12 | // Quoted from the RTL, which for an FPGA is not a description of the hardware |
| 13 | // but IS the hardware. Pinned to X16Community/vera-module tag v47.0.2, the |
| 14 | // same commit tests/fetch_vera_rtl.py fetches; run it to check these. |
| 15 | // |
| 16 | // * ONE VRAM bus, 32 bits wide, one access per clock. main_ram.v:7-10 takes |
| 17 | // a 15-bit word address and 32 bits of data, so AN ACCESS IS FOUR BYTES. |
| 18 | // * A scanline is 800 clocks. video_vga.v:27-31 -- 640 active + 16 front |
| 19 | // porch + 96 sync + 48 back porch. 525 lines a frame (video_vga.v:33-37). |
| 20 | // * FIXED PRIORITY, not round-robin: vram_if.v:142-157 is a combinatorial |
| 21 | // if/else-if chain, if0 > if1 > if2 > if3. top.v:680-713 wires those to |
| 22 | // the CPU-and-FX port ("Interface 0 - 8-bit (highest priority)"), layer 0, |
| 23 | // layer 1 and the sprite renderer, in that order. SPRITES COME LAST. |
| 24 | // * An access costs TWO clocks: the FETCH state raises the strobe and the |
| 25 | // WAIT_FETCH state takes the ack one clock later |
| 26 | // (layer_renderer.v:250-271). |
| 27 | // |
| 28 | // WHY THE HEADLINE IS PER-SCANLINE AND NOT "PERCENT OF PEAK": 25 MHz x 4 bytes |
| 29 | // is 100 MB/s, and a real program uses perhaps a fifth of it. Reporting that |
| 30 | // would produce a number that is always low and always useless -- the same |
| 31 | // mistake as reporting raw cycles-per-frame, which is always 100%. The 800 |
| 32 | // clocks of ONE SCANLINE is the window that can genuinely run out, and at full |
| 33 | // load it exactly does: |
| 34 | // |
| 35 | // layer 0, 8bpp bitmap 640 160 fetches x 2 = 320 clocks |
| 36 | // layer 1, 8bpp bitmap 640 160 fetches x 2 = 320 clocks |
| 37 | // sprites, 4bpp, full line = 160 clocks |
| 38 | // ---- |
| 39 | // 800 = H_TOTAL |
| 40 | // |
| 41 | // ─── Why this models the registers instead of counting reads ──────────────── |
| 42 | // |
| 43 | // The obvious implementation -- count video_space_read() calls in the render |
| 44 | // path -- measures the wrong machine. The emulator's renderer is a PIXEL loop: |
| 45 | // it walks SCREEN_WIDTH columns and reads a byte whenever it crosses a byte |
| 46 | // boundary, so at 320-wide it reads the same byte repeatedly, and it never |
| 47 | // fetches 32 bits at once the way the hardware does. Those call counts are an |
| 48 | // artefact of the emulator's inner loop, not VERA's bus traffic. |
| 49 | // |
| 50 | // So the fetch count is derived instead: a closed form over the layer's mode, |
| 51 | // colour depth and tile geometry that reproduces the RTL's own decode tables |
| 52 | // (layer_renderer.v:42-51 pixels_per_word_minus1, :68-77 |
| 53 | // words_per_line_minus1). It is both more faithful and cheaper than |
| 54 | // instrumenting every read. |
| 55 | // |
| 56 | // ─── WHERE THIS MODEL IS WRONG ────────────────────────────────────────────── |
| 57 | // |
| 58 | // Read this part before trusting a number out of it. |
| 59 | // |
| 60 | // 1. FETCHES ARE MODELLED, NOT OBSERVED. If the emulator's renderer and the |
| 61 | // RTL disagree about what a mode fetches, this follows the RTL. That is |
| 62 | // the intent -- the question is "what would VERA do", not "what did this |
| 63 | // emulator's convenience code do" -- but it means a bug in the emulator's |
| 64 | // renderer will NOT show up here. |
| 65 | // |
| 66 | // 2. CLOCKS ARE A LOWER BOUND; ACCESSES ARE EXACT. Two clocks per access is |
| 67 | // the uncontended cost. Under real contention a lower-priority client |
| 68 | // holds its strobe and waits, so it burns MORE clocks than this charges. |
| 69 | // Trust the access counts; treat the clock totals as "at least this bad". |
| 70 | // |
| 71 | // 3. THE SCROLL TILE IS AN ASSUMPTION. A layer scrolled mid-tile needs one |
| 72 | // more tile than the arithmetic gives. This charges that extra tile |
| 73 | // whenever hscroll is not tile-aligned. The RTL's exact edge behaviour at |
| 74 | // the last partially-visible tile was not pinned down, so the count can |
| 75 | // be one tile high or low at the margin. |
| 76 | // |
| 77 | // 4. A LINE IS FETCHED DURING THE LINE BEFORE IT. The layer line buffer is |
| 78 | // double-buffered (top.v active_line_buf_r, layer_line_buffer.v) and |
| 79 | // composer.v starts the render for line N+1 as line N begins. This module |
| 80 | // reports a line's fetches against the line they PAINT, not the line they |
| 81 | // happen on, because that is where a developer looks for them. |
| 82 | // |
| 83 | // 5. SPRITE HEADROOM IS NOT COMPUTED HERE. This module deliberately knows |
| 84 | // nothing about sprite_trace.c. It reports what the layers and the CPU |
| 85 | // took, and leaves "so how much was left for sprites" to the consumer, |
| 86 | // which is what keeps this file dependency-free. The Performance panel |
| 87 | // crosses the two. |
| 88 | // |
| 89 | // 6. FX IS COVERED ONLY WHERE THE EMULATOR IMPLEMENTS IT. addr_data.v is |
| 90 | // 59 KB of pipeline; this charges the paths video.c actually has -- the |
| 91 | // four-byte cache write and the affine prefetch's second fetch -- and |
| 92 | // does not claim to be a whole FX performance model. |
| 93 | // |
| 94 | // WHY THIS FILE HAS NO EMULATOR DEPENDENCIES: it is unit-tested on its own, |
| 95 | // with no CPU, no memory, no video and no SDL -- see |
| 96 | // tests/test_vera_bandwidth.c. Everything it needs arrives as a function |
| 97 | // argument or through vera_bandwidth_configure(), exactly as perf_budget.c, |
| 98 | // io_trace.c and cpu/irq_ctx.c do, and for the same reason. |
| 99 | // |
| 100 | // AND NO <math.h>: lrintf and fabsf are intrinsics on MSVC but real libm |
| 101 | // symbols on glibc, which broke the Linux link for every target that pulls in |
| 102 | // memory.c. Everything here is integer arithmetic. |
| 103 | // |
| 104 | // COST WHEN OFF: one load and one predictable branch, from the inline guards |
| 105 | // below. The guards test the flag BEFORE evaluating anything, because C |
| 106 | // evaluates arguments before the callee can decline them. |
| 107 | // |
| 108 | // THREADING: same rule as perf_budget.h -- one thread steps the CPU, polls DAP |
| 109 | // and draws the debugger, so none of this locks. Do not call it from the SDL |
| 110 | // audio callback. |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | extern "C" |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | __cplusplus |
| 346 | } |
| 347 | |
| 348 | |
| 349 | // _VERA_BANDWIDTH_H_ |