Page 1 of 2

CX16 Programming and Coding in C Guide/Tutorial

Posted: Thu May 25, 2023 2:49 pm
by mwiedmann
I've been working on a CX16 programming guide and C programming tutorial/guide. I'll be adding more but it's ready enough to share. Even if you don't want to code in C its a good general CX16 programming guide/tutorial. It has C code examples for everything discussed. It uses cc65 but I explain how to do everything "from scratch" before leveraging any libraries that cc65 has to offer so you _should_ be able to use another compiler. I learned a lot putting this together so hopefully someone will find this helpful.

https://github.com/mwiedmann/cx16CodingInC

Here are the current chapters just to give you an idea.
Chapter01-Installation
Chapter02-First-Program-Setup
Chapter03-Numbers
Chapter04-CX16-Overview
Chapter05-Video-Memory
Chapter06-Tiles
Chapter07-MapBase
Chapter08-Palette
Chapter09-Color-Depth-Modes
Chapter10-Tile-Sizes
Chapter11-Bitmap-Mode
Chapter12-Layers
Chapter13-GameLoop-Wait
Chapter14-Controls-and-Input
Chapter15-Sprites
Chapter16-Animations
Chapter17-Collisions

Coming soon - Files and some "Put it all together" game examples.

Re: CX16 Programming and Coding in C Guide/Tutorial

Posted: Fri May 26, 2023 8:56 am
by Manannan
This is a great idea.

I had a lot of trouble figuring things out when I started building my Sierra AGI interpreter.

Here is what I wish was documented when I started:
- File Seeking
- Executing C code from banks
- Integration between C and assembler
- Writing performant code in C. (I found that calling lots of C methods from my interpreter loop caused a significant performance penalty)
- When assembler should be used instead of C due to performance or other reasons

I asked some of these questions in forms post which have since become inaccessable for some reason. See here: https://www.commanderx16.com/forum/view ... hp?p=25434

I am not sure what is going on.

However if you don't already have examples. I can provide some if that would be helpful

Good job.

Re: CX16 Programming and Coding in C Guide/Tutorial

Posted: Fri May 26, 2023 2:09 pm
by mwiedmann
Those are all great topics. I think they would go well with some "Advanced Topics" chapters.

Right now its more geared towards someone who wants to make a game for the CX16 in C (because Assembly Language is a bit too much) and needs help with CX16 basics and some examples in C to show how. I'll keep working on it though and yes, if you want to connect on these more advanced topics, I'm in!

Re: CX16 Programming and Coding in C Guide/Tutorial

Posted: Mon May 29, 2023 12:56 pm
by Manannan
Thanks. I will be in touch soon.

I hope there is something I can add from my experience.

Re: CX16 Programming and Coding in C Guide/Tutorial

Posted: Mon Jun 26, 2023 3:36 am
by 10?CHR$(147)
Great tutorials, thanks for putting this together. I found the pages on setup and banked memory helpful.

I have a game that I have working on the C64 and the .prg is 37,210 bytes which is well under the 40Kb limit of the X16 even with the 1K loss to kernal functions and others. The C128 version is 37,221.

However, when I compile it get the following:
cl65 -t cx16 -g -Oirs src/*.c -o bin/main.cx16.prg ld65: Error: /home/me/github/cc65/cfg/cx16.cfg:15: Size of memory area 'BSS' is negative: -140

Aside from breaking my .prg up into multiple files to load pieces into banked RAM, do I have other options for configuring CC65 to free up more space?

Re: CX16 Programming and Coding in C Guide/Tutorial

Posted: Thu Jun 29, 2023 12:41 am
by mwiedmann
Good question. As far as compiler options, I'm not sure (I'm not an expert on that part). Your program will use more RAM than just its size (there is a stack and other things like any global data you have). I usually run into issues when I get over even 32k. You mentioned this, but look at Chapter 20 on Banked RAM. Its REALLY easy to put some of your code into Banked RAM.

Another thing to try is to load any data from a file at runtime rather than hardcoding in your code. For instance, if you have something like:

Code: Select all

unsigned char spriteData[] = { bunch of numbers };
You should load it into Banked RAM from a file at runtime. That will reduce code and data size.

Re: CX16 Programming and Coding in C Guide/Tutorial

Posted: Fri Jun 30, 2023 2:19 am
by 10?CHR$(147)
Thanks I think I'll try and work my game down to under 32kb and move more of it into resource files.

Re: CX16 Programming and Coding in C Guide/Tutorial

Posted: Fri Jun 30, 2023 9:17 pm
by wizardmountain
Thank for this! I was trying my hand at ASM but getting frustrated. Bit of a steep learning curve. Hoping C will give me less trouble.

Re: CX16 Programming and Coding in C Guide/Tutorial

Posted: Wed Jul 05, 2023 4:27 pm
by mwiedmann
Quick update:
I've added some chapters around Scrolling, Files, and Banked RAM (thanks to Manannan for the section on how to run code from banked RAM). There is a Sound chapter but its just a reference to the ZSound library.

I'm basically done with this (for now). Please feel free to open PRs to fix things or add new chapters!

Chapter01-Installation
Chapter02-First-Program-Setup
Chapter03-Numbers
Chapter04-CX16-Overview
Chapter05-Video-Memory
Chapter06-Tiles
Chapter07-MapBase
Chapter08-Palette
Chapter09-Color-Depth-Modes
Chapter10-Tile-Sizes
Chapter11-Bitmap-Mode
Chapter12-Layers
Chapter13-GameLoop-Wait
Chapter14-Controls-and-Input
Chapter15-Sprites
Chapter16-Animations
Chapter17-Collisions
Chapter18-Scrolling
Chapter19-Files
Chapter20-Banked-RAM
Chapter21-Sound

Re: CX16 Programming and Coding in C Guide/Tutorial

Posted: Wed Jul 05, 2023 6:03 pm
by desertfish
Wow this must have been a lot of work!