FIXING C64 BASIC V2 IN 750 BYTES!   3 comments


TransActorV404The front cover of the magazine which published this amazing program!

I’ve recently been amazed by some more little known information I’ve found in a small magazine called “The Transactor”, published by Commodore Canada and available for download on . This magazine was so good that Commodore decided to cancel it, to be replaced by its US magazine, but then “The Transactor” managed to continue after getting sponsorship from one of its advertisers.

In their Volume 4, Issue 4 edition, dated July 1983, there was an article with a type in listing called “Graphics Utility”, written by a well known Commodore PET programmer called Brad Templeton. Obviously, this program was written only a few months after the Commodore 64 came out, so there was no way anyone could have learnt how to program many things on it by that time, if it had been their first computer.

Transfix64The output of a demo program using these routines

An Internet search reveals that Brad Templeton worked for the software house Visicorp (creators of Visicalc), which at one time was bigger than Microsoft, and he programmed commercial software such as “Time Trek” for the PET, “Checker King” for the PET, Apple ][, and Atari 400/800, the PAL Assembler for the PET and C64, as well as the utility package Power for the PET and Power 64 for the C64. All of these computers speak 6502 Machine Code. He also programmed most of Visiplot for the IBM PC, used Arpanet starting in 1979, and Usenet starting in 1981, but I’m not sure what computers this was on. As they were text based, I think most computers at the time with the right software and a modem could handle this. He’s done lots of Internet related things since then. To sum up, my point is that before programming the Commodore 64 he had programmed the PET and used computers for at least THREE YEARS before this amazing program “Graphics Utility” was published.

The program “Graphics Utility” is a short, quick fix for Commodore 64 BASIC V2, adding Machine Code routines for turning on and clearing the lores multicolour graphics screen, setting up its palette, plotting a point, drawing a line, detecting the colour of a pixel, as well as returning to the text screen, which were all things that the Atari 400/800 and Apple ][ computers could already do in their dialects of BASIC. These are the bare essentials, but there seems to be no way to set the other 12 colours without clearing the screen again, use the hires graphics screen, or to specify vertical Y as well as horizontal X coordinates before printing a character on the text screen. I should also mention at this point that the Commodore BASIC command SYS could have various parameters following it after the RAM address, but Commodore often failed to mention this! Of course, most dialects of BASIC at the time didn’t have a command to fill or paint enclosed shapes.

The most important thing about this article is that it makes the point that extended BASICs are no good for writing “transportable programs” which will run on someone else’s Commodore 64. From this and the general tone of this article, we can conclude that these routines were made to be redistributed without paying any royalty fees! This means that Brad Templeton and The Transactor magazine actually more or less fixed Commodore 64 BASIC V2 in mid 1983 in a magazine published by Commodore!!!!

TransFixCodeA section early on in the code showing some occurences of ??? indicating that the C128 MONITOR can’t disassemble it

I tried disassembling this code in the C128 built in MONITOR, to try and work it out, but I found that a lot of the code was opcodes I haven’t yet studied and some of it wouldn’t disassemble at all, just listing ??? instead of a disassembly. I’ve recently found that Jim Butterfield’s book about Machine Language for Commodore computers has become very hard going, so now I’ll have to study a 6502 book not about Commodore computers at all.

I think that what was desperately needed at this point was for Commodore to include a cassette of this amazing software, as well as a copy of the original article accompanying it, with every single Commodore 64 sold anywhere in the World from that point in time! Not long after this, they could have incorporated these routines into their BASIC ROM and actually given them names, such as LRG, DRAW, PLOT, POINT, and TEXT. Don’t forget that this Machine Code program only took up 750 bytes. The Commodore BASIC V2 ROM on the C64 was 8K, but it was only one of FOUR ROMs in the C64, so don’t bother telling me this couldn’t have been done! I rest my case.

Posted April 9, 2014 by C64hater in Uncategorized

3 responses to “FIXING C64 BASIC V2 IN 750 BYTES!

Subscribe to comments with RSS.

  1. My dear friend, you are partially wrong about C64 SYS command and Commodore.

    You state that Commodore failed to “menton SYS [command in C64 BASIC] could have various parameters following it after the RAM address”, but this is incorrect.

    If you take Commodore 128 System Guide (this computer was my 1st real computer), you can read on chapter 17.95 how is SYS command on ecah system:

    Call and execute a machine language subroutine at the specified address.
    SYS address (C64 mode)
    SYS address [, [a] [, [x] [, [y] [, [s] ] ] ] ] (C128 mode)

    As you can see in C128 mode SYS command can take arguments, but it can´t in C64 mode.

    In C128 mode you can run the command SYS 8192,20,21,22,23 an it is ok, even if the routine at 8192 take that arguments or not. That is becauses that SYS command is designed to take arguments and BASIC take care of that.

    But in C64 mode -or in a normal C64- SYS command is note designed to take arguments in that way. The official way to take (and get) arguments in C64 is by pokeing them. You can poke/peek arguments address you reserve to do that, or even you can take official address for that:
    SYS “loads” the accumulator, the X and the Y index register, and the status register with the bytes stored at addresses 780–783/$030C–030F. When the routine returns control to BASIC (via an RTS instruction), SYS saves the contents of the accumulator, the X and Y index registers and the status register, back into the 780–783 memory range.
    I.e. address $030C–030F functions exactly as the [, [a] [, [x] [, [y] [, [s] ] ] ] ] parameters in C128.

    On other side yoy would be right to accuse Commodore of not state the information about address $030C–030F in SYS command chapetr. To find that information on C64 Programmer’s Reference Guide you must go to page 315 where it says “SAREG 030C 780 Storage for 6502 .A Register”/etc, but withot mention to SYS command, so this is a fault. That information had to appear also in SYS command description.

    Why I can say that officially C64 BASIC isn´t designed to take arguments following RAM address in SYS command? Because it isn´t. When you do it as described in [1][2], you aren´t taking thouse arguments in BASIC but in machine code in your own routine. That is: is your program in ML, not BASIC that take care of that.

    The trick used here is that Commodore BASIC process BASIC statements in partiall order, and then when a SYS is processed -inmediatly afeter SYS addres and a separator (“,” or “:” or line end)- execution is on ML, not in BASIC, and is that ML program that use that trick.

    If I have a routine at address ADDR, I can call it in C128 as “SYS ADDR,20,30,40” and there is no error even if my routine doesn´t use the data transmited in 20,30,40. In this case BASIC take all that process completely as it is desgined for. But in C64 mode if a run “SYS ADDR,20,30,40” and my machine code doesn´t use the trick to take thouse arguments (or if they are no exactly in expected format for my ML progam) we take a SYNTAX ERROR… simply because C64 BASIC emits that error when BASIC encounters more data afeter SYS ADDR separated by “,”.

    Please, run this code in C64:
    poke a,96
    sys a

    We write a small ML language program in 49152: it does nothing, it only returns to BASIC and all is fine.
    But if we run this:
    sys a,12
    then we get a ¨?SYNTAX ERROR”. Why BASIC gives us a SYNTAX ERROR: simply because SYS only takes address parameter and no more.

    In C128 we could use SYS addr,12 without any error because it CANS take more arguments, not only address like in C64.

    If you run this program:
    10 input “number”;a,
    you get this anser “NUMERO? “, and you can enter a number, but afeter you enter the number you get a “?SYNTAX ERROR IN 10” message. With this I want to demostrate that Commodore BASIC encounters errors when it arrives to them, not in advance; in this case when it arrives to the final “,”. This is the same that for SYS command: if you use “SYS add,x” the error if found afeter excecuting SYS addr, when it reads “,x”.

    [1]: In order to pass parameters from BASIC to the machine code routine, one can also exploit the “mechanisms” in BASIC ROM that handles parameters in other BASIC commands, using routines like e.g.

    CHKCOM (at 44797/$AEFD) which checks that the next character in the command is a comma
    GETBYT (at 47006/$B79E) which reads an unsigned byte parameter
    GETPAR (at 47083/$B7EB) which reads a 16-bit unsigned integer parameter followed by a comma followed by an 8-bit unsigned integer parameter.

    SYS command can also take parameters, as in SYS 49152,20,A%,6942 – the first one is the address of the routine, the rest are left for the subroutine to process.

    In order to read these parameters, you need to first JSR $AEFD – this will check that the SYS command is followed by a comma, and if it isn’t, the program execution will end with ?SYNTAX ERROR. This comma check is also provided by some other subroutines:

    Read two parameters, separated by comma. First parameter is a word, stored to $14 and $15; the other is a byte, stored to X register.
    Read a byte (and store to X), check for a following comma.
    $B79B ¿¿or $B79E??
    Read a byte (and store to X).
    Read a word, store to $14 and $15. Datatype check routine at $AD8A must be called beforehand.
    Read a variable pointer. Address of the variable is stored to $47 and $48, name of the variable is stored to $45 and $46.

    If both variable name bytes have bit 7 set, it’s an integer (%), stored as highbyte + lowbyte.
    If only the second one has bit 7 set, it’s a string, stored as string length + address of the string as Lowbyte+Highbyte.
    If neither has bit 7 set, it’s an untyped (float?) variable. Floats are stored as exponent + 4 byte mantissa.

    • A better way to show how C64 BASIC finds errors on the run is:
      poke 53280,5,9
      It is evident that this code has an error (the last “,9”), but If you run it you can see how it change screen border to green before arriving at “,9” and getiing “?SYNTAX ERROR” 🙂

      It is the same that in “SYS addr,12”: you get an error (although you use tricks in your ML program) when it arrives at “,12” beacuse SYS isn´t designed to take tha last argument.

      Of course, I think passing parameters after SYS in C64, using that trick, is very nice, but I can´t blame Commodore for not saying it as it isn´t designed in that way originally.

  2. I’m not sure I should have repied to these comments above from David! It seems I automatically approved them just by replying! There’s one very important thing to note from these comments, though. David’s first computer wasn’t a Commodore 64. It was a Commodore 128. I think this proves my point that the best way to learn how to program the Commodore 64 is to learn on another computer which is somehow compatible, such as the Commodore 128, or Atari 8 bit computers!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: