VIC-20: Sky-Ape-Er code dissection – part 1

See also: part 1, part 2, part 3, part 4 or part 5 (with more coming).

NOTE: This article was originally started on February 29, 2016, shortly after I discovered my cigar box of VIC-20 cassette. When I say “recently,” that now means “four years ago…”


Welcome to VIC-20 Tuesday!

I have slowly been importing my old VIC-20 programs in to the VICE emulator. This has been a tricky process. The tapes are over 30 years old and I no longer posses any real hardware to read them on. (I let my dad take my VIC-20 in exchange for getting me a Radio Shack TRS-80 Color Computer.) I played them on a higher end late 1990s Radio Shack dual cassette deck, and record them at 96khz stereo in Audacity on my Mac. The original tapes were mono, but some are stronger in one channel so I have been preserving both tracks as stereo audio files. I then use wav-prg to convert them to image files I can load in the VICE Commodore emulator.

My Sky-Ape-Er game has caught my attention. On the tape was a program called INSRTUCTIONS (with that typo I apparently never noticed) that will display instructions, wait for a keypress, then load the game as a second program called SKY-APE-ER. There were actually three copies of the main program, and at least two were different versions (oh, if only I had added version numbers to the file name). I also found several other copies of the main game program on other tapes in various forms of completion. I am hoping that this one is the “final” complete version.

The loading process looks like this:

vicCode-skyapeerload1
VIC-20 loading a program from tape.

…then you would type “RUN” (assuming you didn’t use the shortcut “RUN” key, which would do the “LOAD” and “RUN” automatically):

vicSkyApeEr-instructions
VIC-20 Sky-Ape-Er loader (instructions).

Then you press any key…

vicCode-skyapeerload2
VIC-20 Sky-Ape-Er loader, loading second part.

…and the program will have you “PLEASE WAIT” for a moment, then will erase itself with “NEW” and the “LOAD” the actual game.

I scanned the code to see how it did this, but I saw nothing obvious. No “NEW” or “LOAD” commands or anything. How did it work? Thanks to the Internet, I believe I figured it out.

First, let’s look at the source code of the program:

Sky-Ape-Er loader, lines -25.
Sky-Ape-Er loader, lines -25.
  • Lines 5-6 – REMarks (comments).
  • Line 10 – POKE stores a value at a memory location. But what is 36879, 808 and 775? A quick search lead me to some answers. 36879 ($900f) is “Screen color / Reverse mode / Border color”. In the VIC-20 manual, Appendix E shows that 26 would be red border, and white screen. Another search led me to a Compute magazine article showing that  808 ($328) is used to disable STOP, RESTORE and LIST (to prevent the user from BREAKing out of the program). 775  ($307) showed up in a correction article from the same magazine, stating that it is what disabled LIST. (Not sure about 808, then. Maybe there were two POKEs required and the first article had a typo or left the second one out by mistake.)
  • Line 15-25 – The Commodores allowed embedded special control characters, and the inverted heart is clear screen. The others are likely color codes and cursor movements, to position the text where I wanted it to be.
Sky-Ape-Er loader, lines 25-55.
Sky-Ape-Er loader, lines 25-55.
  • Line 25-50 – More things that print the text on the screen.
  • Line 55 – Gets a keypress then, if the keypress is nothing (“”), go back and get another keypress. This causes the program to spin in a loop until a key is pressed on the keyboard.
Sky-Ape-Er loader, lines 60-105.
Sky-Ape-Er loader, lines 60-105.
  • Line 60 – Please wait … for what? It looks like what happens next may take a few seconds.
  • Line 65 – This is clearing out memory locations 7424 to 7432. But why? All I can tell so far is this is some memory location in RAM with other places saying it’s in the User BASIC area. A helpful forum post gave me a hint. This is actually clearing out the 8 bytes that will make up the “space” character so once I customize the character set with game graphics, that character will still be a space. More on this next…
  • Line 70 – This walks through memory locations 7168 to 7303 and POKEs them with the values read from DATA statements below. This is storing values in that range of memory. But what is 7168? According to this page, that is the start of “half RAM, half ROM of upper case normal characters since the 14-bit address space of the VIC wraps around”. I believe this is the font/character set, and the DATA statements are the custom graphics for the Sky-Ape-Er program (such as the bricks for the levels, the pieces that make up the ape, the chimps, and the player).
  • Line 80 – This is the magic! According to this page, memory location 198 is the number of characters in the keyboard buffer. The VIC-20 has a keyboard buffer from 631-640 (9 bytes?). I am stuffing the buffer with 78 (“N”), 69 (“E”), 87 (“W”), 13 (“ENTER”) and then 131 (the “RUN” key). Thus, it’s like I typed “NEW” and pressed ENTER (clearing out the BASIC memory), followed by hitting the “RUN” key, which does a “LOAD/RUN” sequence, loading the next file from tape and running it. Huzzah! The autostart with done by stuffing the keyboard buffer as if the user typed commands. This tells me just putting in “LOAD:RUN” in a line of BASIC wouldn’t have worked (probably because it would overwrite the program, and you could never get to the RUN part). Cool
Sky-Ape-Er loader, lines 110-END.
Sky-Ape-Er loader, lines 110-END.
  • Line 110-END – The rest of the code are the DATA statements that make up the custom character set. I will have to dissect them so I can see what the bitmaps look like.

I have no recollection of how this works, so I assume these POKE values were things I found in the Commodore VIC-20 manual or read in magazines such as Compute’s Gazette. There was very little information around back then. In fact, when I got this computer, I knew of only one store in all of Houston that sold software for it (other than Commodore cartridges at some places that sold the computer). I had my grandmother drive me across town to go there once, and that is the store where I bought the Krazy Kong game that inspired me to write Sky-Ape-Er.

There was a Commodore Houston’s User’s Group (CHUG) that I attended a meeting of, but that was the only time I ever met other VIC-20 users (other than going to meet the publishers of the FOX-20 cassette magazine at their house). It was a whole different world back then, and it’s amazing that I can go from from “what does this do?” to writing this article in just a few web searches.

Up next: A look at the actual Sky-Ape-Er game itself, including how it uses this custom character set for game graphics.

Converted Source Code

Here is the INSTRUCTIONS program converted from PETASCII to ASCII. I found a utility that does this thanks to this blog post. The utility is found here:

https://www.commodoreserver.com/Downloads.asp

It does a nice job of translating the PETASCII characters to {words} so I can better understand what was supposed to be there.

5 REM  ************          *SKY-APE-ER*        *******BY*******
6 REM*ALLEN  HUFFMAN*      ****************{$cc}
10 POKE36879,26:POKE808,100:POKE775,200
15 PRINT"{clear}{black}{right:2}-=<<SKY-{red}APE{black}-ER>>=-{right:5}BY:{blue}ALLEN HUFFMAN"
20 PRINT"{purple}{$a4:22}{$a5}    INSTRUCTIONS    {$a7}{$a3:22}"
25 PRINT"{black}{down}CLIMB THE BUILDINGS TO CAPTURE THE MAD APE!"
30 PRINT" JUMP OVER CHIMPS AND UP STEPS TO GET TO THETOP. YOU MUST FACE THE";
35 PRINT"  DIRECTION TO JUMP."
40 PRINT"{down}{blue}      CONTROLS :{down}"
45 PRINT"{red}<LEFT>='A'{right}<RIGHT>='S'{right:4}<JUMP>='F1 KEY'"
50 PRINT"{down}{black}{rvrs on}    PRESS ANY KEY.    "
55 GETA$:IFA$=""THEN55
60 PRINT"{clear}{right:4}PLEASE WAIT..."
65 FORA=7424TO7432:POKEA,0:NEXTA
70 FORA=7168TO7303:READB:POKEA,B:NEXTA
80 POKE198,6:POKE631,78:POKE632,69:POKE633,87:POKE634,13:POKE635,131
100 DATA223,223,223,0,253,253,253,0,0,0,0,0,1,3,3,7,0,60,126,219,129,165,165,129
105 DATA0,0,0,0,128,192,192,224,31,63,127,255,252,254,127,63,0,126,126,0,255,0,231,231
110 DATA248,252,254,255,63,127,254,252,15,7,8,30,31,31,31,31,231,219,60,255,126,126
115 DATA189,195,240,224,16,120,248,248,248,248,31,31,31,15,15,63,127,0,231,129,0,0,129
120 DATA129,129,0,248,248,248,240,240,252,254,0,12,12,24,47,8,15,82,36,48,48,24,244,16
125 DATA240,74,36,28,8,28,42,8,20,20,54,12,76,40,47,40,159,82,36

I normally just use the WordPress “preformatted text” box, but it did not like this listing. Hopefully this shows up.

Until next time…

5 thoughts on “VIC-20: Sky-Ape-Er code dissection – part 1

  1. Shaun Bebbington

    Did you know that empty entries in Commodore BASIC DATA statements are read as a zero, meaning you may save typing and probably some BASIC bytes. You may therefore replace lines 100 – 125 as follows:

    100 DATA223,223,223,,253,253,253,,,,,,1,3,3,7,,60,126,219,129,165,165,129
    105 DATA,,,,128,192,192,224,31,63,127,255,252,254,127,63,,126,126,,255,,231,231
    110 DATA248,252,254,255,63,127,254,252,15,7,8,30,31,31,31,31,231,219,60,255,126,126
    115 DATA189,195,240,224,16,120,248,248,248,248,31,31,31,15,15,63,127,,231,129,,,129
    120 DATA129,129,,248,248,248,240,240,252,254,,12,12,24,47,8,15,82,36,48,48,24,244,16
    125 DATA240,74,36,28,8,28,42,8,20,20,54,12,76,40,47,40,159,82,36

    Reply
    1. Allen Huffman Post author

      That’s a great tip! I knew this about my CoCo’s Color BASIC, but if I knew it in my VIC days, I had long forgotten.

      The VIC doesn’t seem to have ELSE (IF/THEN/ELSE). Does it support Integer variables like the later Commodores, or HEX values?

      Reply
      1. Shaun Bebbington

        You can mis-use the ON statement with conditional values as an if/else type structure.

        As for integers, the most efficient way is to use an array, like:

        DIM X%(8)

        Each value i the X% array will be stored as two-byte signed, and is a zero-indexed array, so that would be nine signed integers; however, using this is slower as a general rule of thumb.

        Reply
  2. Pingback: What ELSE can you do when you don’t have ELSE? | Sub-Etha Software

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.