CX16 Programming and Coding in C Guide/Tutorial

Tutorials and help articles.

(Posts require approval. See pinned post.)
Forum rules
Post guides, tutorials, and other instructional content here.

This topic area requires approval, so please be patient while we review content to make sure it fits the expectations for this topic area.

Tech support questions should be asked in Hardware or Software support.
Post Reply
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 »

Cross post from the Programming section of the forums (I think belongs here too).

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.
User avatar
StephenHorn
Posts: 565
Joined: Tue Apr 28, 2020 12:00 am
Contact:

Re: CX16 Programming and Coding in C Guide/Tutorial

Post by StephenHorn »

Only had a chance to take a quick glance at it, but this looks pretty good so far! This should be a huge help for C programmers wanting to get started with the X16!
Developer for Box16, the other X16 emulator. (Box16 on GitHub)
I also accept pull requests for x16emu, the official X16 emulator. (x16-emulator on GitHub)
User avatar
Dr.J
Posts: 13
Joined: Sun Dec 24, 2023 12:16 am
Location: United States
Contact:

Re: CX16 Programming and Coding in C Guide/Tutorial

Post by Dr.J »

Hey, just wanted to leave a note that I'm using this tutorial. I've worked through the first few chapters and this is enormously helpful so far. This is a very well-written tutorial and it is saving me many hours of having to figure these things out "the hard way." Thanks so much for taking the time to write this!

Dr.J
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 »

Glad it is helping! I've learned a few things since I wrote this so I'll hopefully make a few updates soon. Everything in there is still good to go but some the joystick section needs an update and the (lack of) sound chapter needs a major update (plus a few other minor things). MooingLemur released the fantastic "ZSMKit" and I learned how to use it in C. This part is huge and makes it WAY easier to do music/sound now. Thanks again for checking this out though!
Bill Leue
Posts: 20
Joined: Thu Sep 08, 2022 5:52 pm

Re: CX16 Programming and Coding in C Guide/Tutorial

Post by Bill Leue »

Very nice! I plan to learn 6502 assembler coding, but programming in C is a good first step, and I wrote a ton of C code during my long career as a programmer, so I expect, with the help of your tutorial, I will get this step done quickly.
-Bill
Ringoid
Posts: 3
Joined: Mon Jan 22, 2024 1:09 am

Re: CX16 Programming and Coding in C Guide/Tutorial

Post by Ringoid »

Thanks for providing this guide — I can confirm that the Linux steps you've given so far (in the first couple videos) work just fine on macOS, too. Same use of "make", same convenience putting cl65 in the PATH (I edited the one in my .zshrc, but people using the default Bash shell should put it in .bash_profile).

One question, though, and I don't know if you're covering it in a later video. How do we access Kernal routines from C? There doesn't seem to be a lot of documentation on how the cx16.h library works outside that big "vera" struct, but what if I want to use asm routines for speed in drawing character-cell graphics (like the old Below the Root game)?
User avatar
ahenry3068
Posts: 1134
Joined: Tue Apr 04, 2023 9:57 pm

Re: CX16 Programming and Coding in C Guide/Tutorial

Post by ahenry3068 »

Ringoid wrote: Tue Feb 06, 2024 8:19 am Thanks for providing this guide — I can confirm that the Linux steps you've given so far (in the first couple videos) work just fine on macOS, too. Same use of "make", same convenience putting cl65 in the PATH (I edited the one in my .zshrc, but people using the default Bash shell should put it in .bash_profile).

One question, though, and I don't know if you're covering it in a later video. How do we access Kernal routines from C? There doesn't seem to be a lot of documentation on how the cx16.h library works outside that big "vera" struct, but what if I want to use asm routines for speed in drawing character-cell graphics (like the old Below the Root game)?
I've only played around with the C compiler a little but I believe there are two ways to do that. You can either use inline assembly (Not sure of the keyword, but I think there is one) or "link" in an external library written in assembly.
User avatar
JimmyDansbo
Posts: 476
Joined: Sun Apr 26, 2020 8:10 pm
Location: Denmark
Contact:

Re: CX16 Programming and Coding in C Guide/Tutorial

Post by JimmyDansbo »

You may find this useful for combining C and assembler:
https://techblog.dansbo.dk/?p=62

For short inline assembly, you use the __ASM__ keyword, see:
https://www.cc65.org/doc/cc65-9.html
Visit my Github repo
or my personal site with CX16/C64/6502 related information.
Feel free to contact me regarding any of my projects or even about meeting up somewhere near Denmark
jaydg
Posts: 10
Joined: Tue Sep 24, 2024 10:23 pm

Re: CX16 Programming and Coding in C Guide/Tutorial

Post by jaydg »

I'm a little more than halfway through and this has been an enormous help! Thank you for putting this together. If you turned this into a book I would buy it just to support your efforts. The writing is articulate, easy to read with small examples easily digested, but meaningful enough to show the power they represent. I am planning to use a hybrid C/asm approach and this documentation and samples have been a enormous springboard to get me up and running. Nice work!
User avatar
desertfish
Posts: 1095
Joined: Tue Aug 25, 2020 8:27 pm
Location: Netherlands

Re: CX16 Programming and Coding in C Guide/Tutorial

Post by desertfish »

I hadn't looked at this before but I browsed through it now. It's really some superb work ! I'm jealous!
Post Reply