Page 1 of 1

DMA Function

Posted: Mon Oct 21, 2024 9:30 pm
by Sarasaworks
Am in the process of adapting VERA into my own custom C/CC65 library and came up with this DMA function. Thought I'd leave it here for thoughts and critique. If you like it, feel free to use it.

Code: Select all

void DirectMemoryAccess(unsigned char DataBus, unsigned int destination, unsigned char stride, volatile unsigned char* Source, unsigned char ByteCount) {
    ADDRx_L = destination & 0xFF;                              // Low Byte of destination sent to $9f20
    ADDRx_M = (destination >> 8) & 0xFF;                       // High Byte of destination sent to $9f21
    ADDRx_H = ((destination >> 16) & 0x01) | (stride << 4);    // destination overflow bit sent to bit zero of $9f22. Stride sent to high nibble.

    switch(DataBus) {
        case 0:
            CTRL = CTRL & ~0x01;                                // Bit 0 of $9f25 set to zero for DataBus 0
            for(unsigned char i = 0; i < ByteCount; i++) {
                DATA_0 = *(Source + i);                         // Copy Data from RAM to VRAM.
            }
            break;
        case 1:
            CTRL = (CTRL & ~0x01) | 0x01;                       // Bit zero of $9f5 set to one for DataBus 1.
            for(unsigned char i = 0; i < ByteCount; i++) {
                DATA_1 = *(Source + i);                         // Copy Data from RAM to VRAM.
            }
            break;
    }
}

Re: DMA Function

Posted: Tue Oct 22, 2024 4:03 pm
by mgkaiser
"unsigned int" is only 16 bits long and you're trying to use 17 bits of it. And this is less "DMA" and more "memcpy" to VRAM. Still Really useful though. Really good candidate for doing in assembly instead of C. Really good candidate for at least partially unwinding the loops.

Re: DMA Function

Posted: Tue Oct 29, 2024 4:53 pm
by Sarasaworks
I didn't even consider that an int would be treated as 16bit in this instance. Thanks for reminding me. Only reason I'm using C versus assembly is to see just how much I can do without headers like x16.h and stdlib.h or even inline assembly. Bouncing back and forth between the Famicom and the X16 whenever I get stuck and adapting whatever I can. Using the CC65 suite for compiling and linking. It's a fun challenge and I'm learning a lot in the process.