DMA Function

All aspects of programming on the Commander X16.
Post Reply
User avatar
Sarasaworks
Posts: 16
Joined: Fri Jul 05, 2024 9:47 am

General - CX16 DMA Function

Post 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;
    }
}
"I mean... You'd be pretty disappointed too if you came all this way and the princess was in another castle... All those turtles and whatnot!"
--Scout Sarasa
mgkaiser
Posts: 60
Joined: Sat Dec 02, 2023 6:49 pm

Re: DMA Function

Post 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.
User avatar
Sarasaworks
Posts: 16
Joined: Fri Jul 05, 2024 9:47 am

Re: DMA Function

Post 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.
"I mean... You'd be pretty disappointed too if you came all this way and the princess was in another castle... All those turtles and whatnot!"
--Scout Sarasa
Post Reply