CX16 Programming and Coding in C Guide/Tutorial

All aspects of programming on the Commander X16.
mwiedmann
Posts: 70
Joined: Fri Jan 20, 2023 2:21 am
Location: New York City

CX16 Programming and Coding in C Guide/Tutorial

Post 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.
Manannan
Posts: 67
Joined: Fri Oct 14, 2022 7:23 am

Re: CX16 Programming and Coding in C Guide/Tutorial

Post 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.
mwiedmann
Posts: 70
Joined: Fri Jan 20, 2023 2:21 am
Location: New York City

Re: CX16 Programming and Coding in C Guide/Tutorial

Post 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!
Manannan
Posts: 67
Joined: Fri Oct 14, 2022 7:23 am

Re: CX16 Programming and Coding in C Guide/Tutorial

Post by Manannan »

Thanks. I will be in touch soon.

I hope there is something I can add from my experience.
10?CHR$(147)
Posts: 3
Joined: Mon Jun 26, 2023 3:12 am

Re: CX16 Programming and Coding in C Guide/Tutorial

Post 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?
mwiedmann
Posts: 70
Joined: Fri Jan 20, 2023 2:21 am
Location: New York City

Re: CX16 Programming and Coding in C Guide/Tutorial

Post 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.
10?CHR$(147)
Posts: 3
Joined: Mon Jun 26, 2023 3:12 am

Re: CX16 Programming and Coding in C Guide/Tutorial

Post 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.
wizardmountain
Posts: 4
Joined: Tue May 02, 2023 2:03 pm

Re: CX16 Programming and Coding in C Guide/Tutorial

Post 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.
mwiedmann
Posts: 70
Joined: Fri Jan 20, 2023 2:21 am
Location: New York City

Re: CX16 Programming and Coding in C Guide/Tutorial

Post 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
User avatar
desertfish
Posts: 1075
Joined: Tue Aug 25, 2020 8:27 pm
Location: Netherlands

Re: CX16 Programming and Coding in C Guide/Tutorial

Post by desertfish »

Wow this must have been a lot of work!
Post Reply