Jim Lawless' Blog


Changing the C64 Text Color in C

Originally published on: Sun, 30 Aug 2009 14:08:10 +0000

I now realize it was 25 years ago that I bought my first Commodore 64. Programming it was an obsession. Unfortunately, there were few tolerable compilers for high-level languages back in the day. I had a couple of Pascal compilers and one C compiler, but they were all bound by the C64/1541 serial disk I/O. For the most part, my programming consisted primarily of BASIC and assembly-language ( or ML as it was referred to in the popular magazines of the day. )

In about 1986, I began to attempt to learn C. I had bought one of the Abacus C compilers, but it was just not too friendly. I was mainly learning the C programming language via the venerable K&R book and by poring over code in magazines like Byte and Dr. Dobbs Journal.

My drive to learn C ultimately led me to newer platforms of the day ( an Amiga and an MS-DOS machine. ) That time-frame marked the beginning of the end of my obsession with C64 coding.

Several years ago, I'd heard about the the CC65 C cross-compiler ( see http://www.cc65.org ) : a small C compiler that outputs 6502-family code.

I've seen some posts by people who have done some impressive things with the cross-compiler specifically for the Commodore 64, so I decided to exercise my C knowledge and see how much of my old C64 programming skills have atrophied.

My first CC65 program ( hopefully of many ) is a simple program that will iterate over the 1000 bytes of VIC-II color memory and will increment the values by 1. This 1000-byte change will occur 16 times.

Let's take a look at the code and then a tiny screenshot of what one should see when the code is executed.

colorchange.c


// colorchange
// A Commodore 64 program that will cycle through 16
// color transitions of the screen where color memory
// begins at $d800
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include <stdlib.h>

int main(void)
{
   int i,j;
   char *colors;

   colors=(char *)0xd800;
   for(j=0;j<16;j++) {
      for(i=0;i<1000;i++) {
         colors[i]=colors[i]+1;
      }
   }
}

I compiled this program under a Windows command-prompt using the following command-line: cl65 -O -t c64 colorchange.c

The resulting output file colorchange was left in the same directory.

Then, I used VICE to bring up a C64 emulator where I entered the commands: load "colorchange",8 run The effect should look like that depicted in the following tiny picture:

Let's take a deeper look at some of code. char *colors;

colors=(char *)0xd800; The above code declares a variable named colors as a pointer to a character. The second line assigns an address to that pointer by using the (char *) typecast to turn the hexadecimal integer 0xd800 into a pointer to a character.

So, colors then points to $d800 ... the default VIC-II color memory location. for(i=0;i<1000;i++) { colors[i]=colors[i]+1; } The above inner-loop iterates 1000 times. The interior line colors[i]=colors[i]+1 first resolves the colors[i] reference on the right side of the equals sign ... effectively PEEKing at the byte at $d800+i. The program then adds one to that value and effectively POKE's it back into the same spot.

So, the loop reads the current color byte, increments it by one, then stores it in the same spot and moves on to the next byte of color memory.

The source and C64 executable files for colorchange can be found here.

http://www.mailsend-online.com/wp/colorchange.zip

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: A Simple Media Control Interface Script Processor
Next post:A Scrolling Banner using Canvas and JavaScript


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Book Review : Using Google App Engine

An Interview with Game Developer James Hague

Blogoversary

WSH2EXE - The Final Chapter

Computers I Have Known

Understanding TRS-80 CMD Files

Hiding Batch File Console Windows

Open Source Licenses

Obfuscated C

Stacking Images with PerlMagick


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service