Monday, June 18, 2012

Hello 6510!

The way I see it, modern-day C=64 hacking is the epitome of "autotelic computing" (a fancy new word I just learned, which seemed to capture so perfectly one of my deepest intellectual ideals, I couldn't resist renaming my blog).

Like so many programmers of the current generation, I've been a long time C=64 hacker, very autotelic to boot. As soon as I realized that BASIC V2 was not very appropriate for game programming, I had to learn 6510 assembly. But for a French-speaking ~12 year-old, this is no easy task. With the help of a forgotten library book I was very lucky to find, I was able achieve a few proofs of concept, but not much more (sadly no Super Mario, King's Quest, U.N. Squadron or Power Drift clone, contrary to my wildest dreams).

Nevertheless, I've been following with interest the many aspects of the nostalgia-fueled C=64 subculture that has been thriving exponentially with the internet in the last two decades: numerous emulators, game repositories, compilers, SID-based chiptune revival, etc. Fascinating stuff, driven mostly by the unflinching love of this old machine!

Recently I got an idea for an autotelic project, that I won't disclose now, because it's not very likely to ever materialize (or should I say logicalize?). But to gently push it towards reality anyway, I thought I'd share and document my very first Hello World attempt with modern C=64 tools.

Why would you torture yourself with a bare-metal (or emulated) assembler when you can use such a powerful tool as the aptly named Kick Assembler? Java-based, command-line driven, sporting some augmented syntax and parsing capabilities, a higher-level scripting engine, etc. In fact this fine piece of engineering made me realize my own "shopping habits" when contemplating the adoption of a new software package: whenever I can find, within the first five pages of the manual, how to achieve the initial task I have in mind, I'm instantly sold. And that's precisely what happened with this one, as this assembly code snippet (adapted very slightly from Jim Butterfield's excellent book Learning Machine Code Programming on the Commodore 64) shows (that book, and better English courses perhaps, could have possibly stirred my game programming career otherwise):

:BasicUpstart2(start) // autostart macro

start:  ldx #$00      // put 0 in X register
loop:   lda $4000,x   // put X-indexed char of string (stored at $4000) in acc
        jsr $ffd2     // call CHROUT kernal routine to print single char in acc
        inx           // increment X
        cpx #12       // did we reach the 12th char?
        bne loop      // if not, loop again
        rts           // if yes, terminate

.pc = $4000 "data"
        .text "HELLO WORLD!"

which is very easy to pipe directly in an emulator (I use x64 on Ubuntu):

$ java -jar KickAss.jar hello.asm -execute x64

yielding the nostalgic and familiar (but feeling so cramped nowadays, don't you find?):


This post may (or may not..) be followed by posts in the same vein.

Update: my original idea was way more ambitious, but I ended up writing this Tetris clone, to learn C=64 assembly. Without help from the Lemon64 community, things would have been undeniably harder. If someone is interested, I could consider writing about it, tutorial-style.