blog:commodore_c65_dtv_programming

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
blog:commodore_c65_dtv_programming [2023/01/25 15:07] – [Basic DTV Functions] johnblog:commodore_c65_dtv_programming [2023/01/25 17:21] (current) – [Extended DTV Functions] john
Line 1: Line 1:
 ====== DTV (and C64) Programming - Using the CC65 toolchain ====== ====== DTV (and C64) Programming - Using the CC65 toolchain ======
  
 +~~TOC_HERE 2-5~~ 
 +
 +From [[https://en.wikipedia.org/wiki/C64_Direct-to-TV|Wikipedia]]:
 +
 +> The C64 Direct-to-TV, called C64DTV for short, is a single-chip implementation of the Commodore 64 computer, contained in a joystick (modeled after the mid-1980s Competition Pro
 +> joystick), with 30 built-in games. The design is similar to the Atari Classics 10-in-1 TV Game. The circuitry of the C64DTV was designed by Jeri Ellsworth, a computer chip designer who
 +> had previously designed the C-One.
 +
 +These are my notes on programming for the C64 DTV and making use of some of the extended functionality of the hardware from within C, rather than 6502 assembly. I hope you find these useful - whilst I came to the DTV more than a decade after its popularity peaked, I've found it a very capable little system!
 +
 +{{:blog:c64:commodore64_dtv_mugshot-x600.jpg?200|}}
 ===== Basic use of CC65 ===== ===== Basic use of CC65 =====
  
Line 205: Line 216:
  
 ---- ----
 +
 +==== Extended DTV Functions ====
 +
 +These functions raise the DTV above a simple C64 clone and add substantially improved functionality to the system: increased colours, digital sound and a dedicated image blitter.
  
 === Enabling 320x200 Linear Framebuffer === === Enabling 320x200 Linear Framebuffer ===
Line 460: Line 475:
 } }
 </code> </code>
 +
 +== Bugs ==
 +
 +I've seemingly found a bug which manifests when making sequential DMA calls in a tight loop and the transfer length is 182 bytes or greater. 
 +
 +You can successfully DMA copy quite a large region in a single operation and there are no side-effects, however in my testing I have found that if you have a tight loop where you are DMA transferring line by line, I get corrupted transfers with any size over 181 bytes. See the example below:
 +
 +{{:blog:c64:vice_dtv_256_speed2x.png?400|}}
 +
 +The colour bars in the image should be continuous for the entire screen width, but they glitch at two points.
 +The above example was generated by the following pseudocode:
 +
 +<code>
 +screen = FRAMEBUFFER_LOCATION;
 +for (line = 0; line < SCREEN_HEIGHT; line++){
 +    memset(line_buffer, colour, SCREEN_WIDTH);
 +    dtv_DMA(&line_buffer, screen, SCREEN_WIDTH);
 +    colour++;
 +    screen += SCREEN_WIDTH;
 +}
 +</code>
 +
 +If you reduce the transfer size to 181 bytes you do not get the glitching, and if you have a //reasonable-number-of-cycles// between DMA operations you also don't get the glitching... but I don't know what that //reasonable-number-of-cycles// value is. It's clearly a timing issue between DMA operations - due to the time it takes for transfers over a centre number of bytes, but all I know is that the documentation says that checking bit 0 of 0xD31F should indicate whether the transfer has finished or not. It's possible (though unlikely) that this is an emulation bug - until I have a working, physical DTV, I won't be able to confirm.
 +
 +----
 +
 +=== Blitter Operation ===
 +
 +One of the big features of the DTV2 & 3 is the addition of [[https://en.wikipedia.org/wiki/Blitter|blitter hardware]]. This dramatically speeds up transfer of memory and can be used to achieve very high speed image/screen manipulation.
 +
 +The Blitter documentation is extensive, with dozens of different registers to set and configure. However, in most circumstances you will want to copy one solid block of pixels to another area (either on-screen or off - the blitter works over the entire 0-2048kb memory region, but //not// ROM).
 +
 +Here I show a variation of the blitter command to transfer w x h pixels from src to dest.
 +
 +   * **src**: address of the upper-left pixel of a rectangular block of source pixels
 +   * **dst**: address of the upper-left pixel of the destination area.
 +   * **w**: width of the source area to copy in pixels
 +   * **h**: number of lines of the source area to copy
 +
 +The source area is OR-ed with the destination and transparency in the source area is honoured (source pixels with value //0// will //not// overwrite pixels in the destination).
 +
 +**dtv_Blit()**
 +
 +<code C>
 +#include <stdint.h>
 +#include <accelerator.h>
 +#include "dtv.h"
 +#include "dtv_reg.h"
 +
 +void dtv_Blit(uint32_t src, uint32_t dst, uint16_t w, unsigned char h){
 + // Blit 'width * height' bytes of data from 'src' to 'dst' in high memory
 + // using the blitter engine.
 + //
 + // 'src' is the address to copy from - it is NOT pointers
 + // 'dst' is the destination address - it is not a pointer
 + // 'w' is the width of the source rectangle, in pixels/bytes
 + // 'h' is the height of the source rectangle, in pixels/bytes
 +
 + uint16_t total_bytes = w * h;
 + uint16_t modulus = SCREEN_WIDTH - w;
 +
 + // ==================================
 + // Source address
 + // ==================================
 + POKE(BLITTER_SOURCE_A_LOW, src);
 + POKE(BLITTER_SOURCE_A_MED, src >> 8);
 + POKE(BLITTER_SOURCE_A_HI, src >> 16);
 +
 + // ==================================
 + // Destination address
 + // ==================================
 + POKE(BLITTER_DEST_LOW, dst);
 + POKE(BLITTER_DEST_MED, dst >> 8);
 + POKE(BLITTER_DEST_HI, dst >> 16);
 +
 + // ==================================
 + // Set line length
 + // ==================================
 + POKE(BLITTER_SOURCE_A_LENGTH_LOW, w);
 + POKE(BLITTER_SOURCE_A_LENGTH_HI, w >> 8);
 + POKE(BLITTER_DEST_LENGTH_LOW, w);
 + POKE(BLITTER_DEST_LENGTH_HI, w >> 8);
 +
 + // ==================================
 + // Set modulus/wraparound
 + // ==================================
 + POKE(BLITTER_SOURCE_A_MODULO_LOW, modulus); 
 + POKE(BLITTER_SOURCE_A_MODULO_HI, modulus >> 8);
 + POKE(BLITTER_DEST_MODULO_LOW, modulus); 
 + POKE(BLITTER_DEST_MODULO_HI, modulus >> 8);
 +
 + // ==================================
 + // Step sizes
 + // ==================================
 + POKE(BLITTER_SOURCE_A_STEP, 0x10);
 + POKE(BLITTER_DEST_STEP, 0x10);
 +
 + // ==================================
 + // Set total transfer size
 + // ==================================
 + POKE(BLITTER_SIZE_LOW, total_bytes);
 + POKE(BLITTER_SIZE_HI, total_bytes >> 8);
 +
 + // ==================================
 + // Configure blitter mode and start blit
 + // ==================================
 + POKE(BLITTER_CFG, 0x04); // Enable transparency, if supported by blitter
 + POKE(BLITTER_MINTERM_CFG, 0x18); // OR the source with destination
 + POKE(BLITTER_START, 0x0B);
 +}
 +</code>
 +
 +Here's an example of the blitter function in operation, copying a **100x100** rectangle of pixels from the source position of **1,1** to the destination at **219,100**:
 +
 +This is the source image - a basic set of colour bars:
 +
 +{{:blog:c64:c64_dtv_blitter_example_source.png?400|}}
 +
 +Using dtv_Blit() we copy from 1,1 to 219,100 a 100x100 rectangle in OR mode:
 +
 +{{:blog:c64:c64_dtv_blitter_example_dtv3.png?400|}}
 +
 +Notice that the transparent (aka //black//) pixels in the source region are preserved when copying to the destination, and that the original content of the destination is preserved for those areas. Where the source is not transparent, those pixels then overwrite the destination. The mode of operation can be customised - see the [[https://www.c64-wiki.com/wiki/C64DTV_Programming_Guide#BLITTER_DATAPATH|DTV Programming Guide]] for more details; specifically the ALU mode set for register **0xD33E**.
 +
 +== Bugs ==
 +
 +However, there is a problem. On the **DTV2** systems (the initial PAL models), the blitter is partially bugged and transparent copies result int a vertical banding effect:
 +
 +{{:blog:c64:c64_dtv_blitter_example_dtv2.png?400|}}
 +
 +Therefore, //without mitigation//, only **DTV3/Hummer** systems fully support transparent blits. If you are //not// bothered about transparency, then the blitter functions fine on DTV2, but to support transparency you need to do some type of workaround.
  • blog/commodore_c65_dtv_programming.1674659273.txt.gz
  • Last modified: 2023/01/25 15:07
  • by john