Introduction to C64 demo coding

ozone
.C:2101   A9 3A      LDA #$3A
.C:2103   CD 12 D0   CMP $D012
.C:2106   D0 FB      BNE $2103
.C:2108   A2 09      LDX #$09
.C:210a   CA         DEX
.C:210b   D0 FD      BNE $210A
.C:210d   A2 00      LDX #$00
.C:210f   BD 00 09   LDA $0900,X
.C:2112   8D 21 D0   STA $D021
.C:2115   8D 20 D0   STA $D020
.C:2118   BC 00 0A   LDY $0A00,X
.C:211b   88         DEY
.C:211c   10 FD      BPL $211B
.C:211e   E8         INX
.C:211f   E0 65      CPX #$65
.C:2121   D0 EC      BNE $210F
.C:2123   A2 01      LDX #$01
.C:2125   CA         DEX
.C:2126   D0 FD      BNE $2125
.C:2128   A9 00      LDA #$00
.C:212a   8D 20 D0   STA $D020
.C:212d   AD 21 D0   LDA $D021

So, who knows what’s happening above? Come on, it’ll come back to you if you look at the screenshot! I found a great tutorial on C64 demo coding. Unfortunately it’s a 404 now, but Google cached it and I downloaded it here for safe keeping: intro-to-programming-c64-demos.html
Look for the part on $d012

$d012 might be the most important address of them all, when it comes to demo programming on the C-64. $d012 has two different functions:

* When read, it returns the number of the current raster line.
* When written, it is used to set the number of the line where the next raster interrupt will occur.

We’ll get back to raster interrupts later. You need to know about $d012 to understand them, so pay attention to the stuff in this section! The first item above is interesting, but it may not be obvious why it is interesting.

The current raster line is the line that is currently being redrawn on your screen. The whole screen is redrawn 50 times per second. Each time it is redrawn from top to bottom, from the left to the right. So, if you want something to happen 50 times per second, all you have to do is to check the current value of $d012, and when it reaches a certain value, call the routine that performs the desired task. When finished, go back to checking $d012.

That blew me away when I found out about that. You could literally change the background colour of the screen halfway down the screen, have multiple text and graphics modes, use more than the default 8 hardware sprites. The PC was disappointing in comparison.

That code above is for Justin who reminded me that Vice has an ASM monitor!


You might also like

If you like this post then please subscribe to my full RSS feed. You can also click here to subscribe by email. There are also my fabulous photos to explore too!

"Introduction to C64 demo coding", 5 out of 5 based on 1 ratings.

7 thoughts on “Introduction to C64 demo coding


  1. Pffft, where’s the self-modifying code?

    Thanks for the post, though – brought back a lot of memories.

    When I was in secondary school a bunch of us were writing games and wound up coding everything around the raster interrupt.

    It meant there was no actual program “running” per se, so you could be editing a new routine in your ASM monitor while the game was “running” in the interrupt (sprite and sound layers only – no char-based backgrounds) then splice the new routine into the raster interrupt loop and hope it didn’t crash the machine (or exceed the allocated time and start dropping frames).

    The C64 was a hacker’s dream.


  2. But where’s the double IRQ stabilisation?! Brings back a lot of old memories, an awareness of reality and some other bits and bobs!


  3. Hi, I’ve just discovered VICE and CCS64 emulators, as I don’t have a real monitor for my old C64, but I understand 6502/6510 assembly without problems.

    I’m trying to run this program you list. I can write it in the VICE monitor, but I’m afraid I just obtain the typical blue Run/Stop+Restore screen (and a totally black screen if I use CCS64). Only if I run it step by step, I manage to obtain the top black bar with the immediately below white thin bar. But then the program exits.

    Are you sure this program works? Does it have to show all these horizontal colour bars with that logo in the middle? What am I doing wrong? I would appreciate your help, thanks.


  4. Angelo – unfortunately that snippet of code isn’t complete. It will create raster bars, but the timing information is in another 2 blocks at $0900 and $0A00. The code to draw the logo is elsewhere and tbh, I’ve completely forgotten how to do that, although it’s easy enough.

Leave a Reply

Loading Facebook Comments ...