My First Kernel

We're going to jump right in, now that we know what a kernal needs to do. Seen below, and in the attached file, is the source code for a working '2600 kernel. It displays the image you see here. Not bad for just a few lines of code. Over the next few sessions we'll learn how to modify this code, and assemble it - and, of course, what all those strange words mean.

But first, here's a screenshot of our first ROM file in action. A pretty (and very simple to program) rainbow screen.
Kernel1
Here's the full source-code...

; Kernel #1 - pretty rainbow

    processor 6502
    include "vcs.h"
    include "macro.h"


    SEG
    ORG $F000

Reset

    ; set TIA to known state (clear to 0)

            lda #0
            ldx #0
.zapTIA     sta 0,x
            inx
            cpx #$40
            bne .zapTIA


newFrame

  ; Start of vertical blank processing
            
            lda #0
            sta VBLANK

            sta COLUBK              ; background colour to black

    ; 3 scanlines of vertical sync signal

            lda #%00000010
            sta VSYNC               ; turn ON VSYNC bit 1

            sta WSYNC               ; wait a scanline
            sta WSYNC               ; another
            sta WSYNC               ; another = 3 lines total

            lda #0
            sta VSYNC               ; turn OFF VSYNC bit 1


    ; 37 scanlines of vertical blank

            ldx #0
vBlank      sta WSYNC
            inx
            cpx #37
            bne vBlank


    ; 192 scanlines of picture

            ldx #0
doPicture   stx COLUBK              ; put a colour in the background
            sta WSYNC               ; wait to the start of the next scanline
            inx
            cpx #192
            bne doPicture

            lda #0
            sta COLUBK              ; background colour to black


    ; 30 lines of overscan

            ldx #0
doOverscan  sta WSYNC               ; wait a scanline
            inx
            cpx #30
            bne doOverscan


            jmp newFrame

;-----------------------------------------------------------------------------------
; the CPU reset vectors

    ORG $FFFA

    .word Reset          ; NMI
    .word Reset          ; RESET
    .word Reset          ; IRQ

    END


The source code and ROM binary are attached at the bottom of this session. You can load the ".bin" file into Stella and see it in action.

Load up the source file (Kernel1.asm) into your IDE. For now, have a look at the structure of the code and note how closely it relates to the structure of the TV frame diagram in the earlier sessions. Don't expect to understand everything - we'll walk through every line soon. For now, all you need to know is that the "sta WSYNC" is where the 6502 is telling the TIA to halt the 6502 until the start of the next horizontal blank period (which is at the start of the next scanline, at TIA colour clock 0). So each of those lines is where one complete scanline has been sent to the TV by the TIA. Have a close look at those lines, and see how there are 3, followed by 37 (vertical blank period), followed by 192 (picture) followed by 30 (overscan) - and how this exactly matches our TV frame diagram, above.

Yes, this is a complete kernel. It's not that difficult!

Of course there are strange 6502 things in there that aren't explained yet - but hopefully the basic structure of the code will be evident, and with an understanding of this you're well on the way to writing '2600 games. Next session we'll have a look at how to actually assemble this code using dasm, and how to make modifications so you can play with it and test it on the emulator to see what effect your changes have.

see Programming for Newbies
previous The TV and our Kernel