Commodore 64 Fast Bitmap Scrolling Techniques: HSP/DMA Delay and Line Crunching Explained with Debugging
Martin Piper
Summary:
This video details advanced bitmap scrolling techniques on the Commodore 64, breaking down how complex demo effects are achieved without solely relying on CPU memory manipulation. It explores techniques like Horizontal Screen Positioning (HSP) or DMA delay, which involves precisely timed updates to the VIC chip's screen control register (D011) to trigger a "bad line" state mid-raster line, forcing the VIC chip to fetch character data earlier and shift the display horizontally. It also covers "line crunching" for vertical scrolling, where the VIC chip is tricked into prematurely fetching new character rows, effectively "crunching" 8 pixel lines into one. These methods are often combined, as demonstrated by a dynamic, free-form bitmap scrolling effect, with debugging insights showing real-time register changes and CPU pauses.
Introduction to Commodore 64 Bitmap Scrolling [00:00:00]
The video explores techniques for fast bitmap scrolling on the Commodore 64 (C64), which is challenging due to the large memory footprint (10,000 bytes per screen) that would overburden the CPU if scrolled conventionally.
- The demo shows horizontal, vertical, and bouncing bitmap screens.
- Debugging tools (C64 Debugger) are used to analyze raster and instruction timing to understand these effects.
Horizontal Bitmap Scrolling: HSP/DMA Delay Technique [00:02:43]
This technique, also known as Horizontal Screen Positioning (HSP) or DMA delay, is demonstrated with a "fist" graphic scrolling horizontally.
- Observation: The bitmap data itself does not scroll in memory; only the display position changes. Sprites are layered on top for scrolling messages.
- The effect of the scrolling is visually demonstrated by editing the color memory, revealing the repeating nature of the bitmap data as it wraps around the screen.
- Mechanism (Triggering "Bad Line"):
- The C64's VIC (Video Interface Chip) registers are updated (specifically D011, the screen control register) at precise cycle timings.
- Writing to D011 causes the VIC to enter a "bad line" state in the middle of a raster line.
- When in a bad line state, the VIC chip prioritizes fetching character definitions (or bitmap data), effectively pausing or "stunning" the CPU.
- The debugger shows the
Bad line state changing from no to yes and the CPU (PC) pausing as the VIC accesses memory.
- Effect on Display:
- Because the VIC starts fetching data mid-line, it reads fewer than the usual 40 bytes for that row.
- This results in the
VC base (video matrix base) offset changing, causing the display to start rendering the bitmap from a horizontally shifted position.
- By repeatedly adjusting this offset, the entire screen can be scrolled horizontally (0 to 39 bytes), creating a wrapping effect as data on one side reappears on the other.
Vertical Bitmap Scrolling: Line Crunching Technique [00:13:57]
This technique, often called "line crunching," enables very fast vertical scrolling of bitmap screens.
- Demonstration: Shown using a demo with a King Tut mask that scrolls vertically at high speed. The memory is not shifted, but the view scrolls.
- Hiding Artifacts:
- Often, an "illegal text mode" (where extended color and multicolor bits are simultaneously enabled, an invalid state) is used to turn off video output circuitry and render black.
- This masks the "messy" pixels or unwanted lines that appear due to the aggressive line crunching, ensuring a clean visual effect. The debugger shows the
Illegal text mode being entered.
- Mechanism:
- The technique involves making the VIC chip believe it has rendered an entire 8-pixel high character row within a single raster line.
- This is achieved by updating the VIC's D011 register at a specific cycle position within a raster line, effectively forcing the VIC to prematurely update its
VC base pointer by 40 bytes (or 28 hex), which is the size of a full character row.
- This "crunches" 8 lines into 1, making the screen advance vertically much faster. The debugger clearly shows the
VC base value updating rapidly.
- Tiling Effect: Because the VIC base memory pages (1024 bytes) are not perfectly divisible by 40 bytes (the character row size), the screen content will wrap around and appear horizontally offset after several "crunched" rows.
Combined Horizontal and Vertical Scrolling (Any Given Screen Position - AGSP / Variable Screen Position - VSP) [00:28:46]
This section showcases a demo (the "Bones" logo) that combines both the HSP/DMA delay and line crunching techniques to achieve dynamic, multi-directional bitmap scrolling.
- Visual Impact: By manipulating the
VC base pointer using both techniques, the entire screen, including its tiled repetitions, appears to swing and move freely.
- Masking in Combined Effects: The demo clears areas around the logo with black characters to hide the natural tiling effect. However, if these areas were filled with content, a repeating, horizontally/vertically offset pattern of the logo would be visible, potentially creating an even more impressive visual if designed correctly.
- Debugging Combined Effects: The debugger confirms that both the line crunch (updating
VC base by 40 bytes for vertical movement) and the DMA delay (shifting the horizontal raster position) are being executed in sequence within each frame, allowing for precise control over the bitmap's position.
- The debugger also illustrates how the
Raster X position shifts, indicating the horizontal adjustment of the screen.
Example Code and Resources [00:42:00]
The video concludes by providing a practical example of the combined techniques in C64 assembly code.
- Code Availability: The example source code (rastertest2.a) is available on the presenter's GitHub repository.
- Interactive Demo: The provided code allows controlling horizontal (HSP/DMA delay) and vertical (line crunch) scrolling via a joystick. When debugging is enabled, the masking (illegal text mode) is turned off, revealing the raw effect of line crunching, which produces black lines.
- Flexibility: The techniques are applicable to both text screen and bitmap screen modes, as the VIC chip uses the
VC base offset fundamentally for fetching display data.
- Additional Resources: An article on Codebase64 about AGSP (Any Given Screen Position) is also mentioned for further learning.