Tagged: C64 Toggle Comment Threads | Keyboard Shortcuts

  • Jay Versluis 11:43 am on March 14, 2018 Permalink | Reply
    Tags: , C64, , VIC-20   

    Categories: Commodore ( 26 )

    String Operations on Commodore Computers 

    Commodore BASIC has some interesting and simple string functions built in. Three of them are self explanatory: LEN, LEFT$ and RIGHT$. But others, like the mysterious MID$ and INSTR functions, are a little tricker, and I can never remember how they works.

    So here’s a quick recap on how they all work.

    LEN (A$)

    Returns the length of any given string. For example,

    a$=”the cake is a lie”

    print len (a$)

    returns 17, which is the number of characters in our string.

    LEFT$ (A$,X)

    The LEFT$ function takes the x left characters from a given string. Here’s an example:

    print left$(a$,3)

    We get “one”, because those are the 3 leftmost characters in our string a$.

    RIGHT$ (A$,X)

    Likewise, RIGHT$ takes the x right characters from any given string:

    print right$(a$,5)

    Here we get “three”, because those are the 5 right characters of a$.

    MID$ (A$,X,Y)

    MID$ is a little more complex. It takes x characters from a given string, starting at position y. Let’s look at our earlier example again:

    print mid$(a$,5,3)

    We get “two”, because those are the 3 characters, starting at position 5. The first position in all these string operations counts as one rather than zero.

    But did you know that MID$ can also be used to assign and replace different characters in a string? Consider this:

    print a$

    Now we’ve replaced the 3 characters in our string with another string, starting at position 5.

    I had no idea it cold do that! All these string operations work in all variations of the Commodore BASIC, except for the MID$ assignment which only works on the Plus/4 and the C128.


    INSTR (A$, B$)

    On the Plus/4 and C128, we can even check if one string is contained in another and at which position this occurs. Consider this:

    a$="the cake is a lie"
    print instr(a$,b$)

    In our example, INSTR returns 5 because “cake” has been found at position 5 of “the cake is a lie”.

    We can also specify a position from which the search shall be started like this:

    print instr(a$,b$,6)

    Now INSTR returns 0 because “cake” has not been found beyond position 6 of our input string.

  • Jay Versluis 5:16 pm on January 13, 2016 Permalink | Reply
    Tags: , C64,   

    Categories: Commodore ( 26 )

    How to use direct block access commands in Commodore DOS 

    Commodore-Logo-PaddedWe can access each sector’s raw data on Commodore disk drives with direct block access commands. Supported drives include the 1541, 1571, the VICE emulator as well as the SD2IEC card reader (for the most part).

    Each single sided floppy contains 35 sectors, while a double sided 1571 formatted disk contains 70 sectors. Each track contains between 17 and 21 sectors depending on how far inside or outside they are. Each sector contains 255 bytes we can read or write.

    Sectors are the same as blocks: only the directory refers to them as “blocks” and shows us how many we have available.

    We’ll need to open two channels to our disk drives: a command channel and a data channel. Here’s how to do it:
    (More …)

  • Jay Versluis 9:33 am on June 15, 2015 Permalink | Reply
    Tags: , C64,   

    Categories: Commodore ( 26 )

    How to split a long string into separate words in Commodore BASIC 

    Here’s a quick word splitter routine for CBM BASIC. It takes phrase and “explodes” all words into an array, removing spaces. Feel free to adopt it for your own needs.

    10 rem word splitter
    20 rem splits a long phrase into words at a space
    30 input "tell me something";a$
    40 rem clear current array of words
    50 for i=0 to 10: wd$(i)="": next: wd=1
    60 rem detect spaces
    70 for i=1 to len(a$)
    80 wd$(0)=mid$(a$,i,1)
    90 if wd$(0)=" " then wd=wd+1: next
    100 wd$(wd)=wd$(wd)+wd$(0): next
    110 print
    120 rem print all words
    130 for i=1 to 10
    140 if wd$(i)="" then 160
    150 print wd$(i)
    160 next

    Line 50 clears an array of 10 words called WD$(n). We also setup a word counter called WD. So in this example we can only detect a maximum of ten words. If you need more, you must DIM the array first.

    Lines 70 loops through all characters in our phrase. We make use of the first element in our array WD$(0) to store each single character. If it’s a space, we’ll increase the word counter and move on to the next character. If it’s not (line 80), then we’ll add this character to the current word which is stored in WD$(WD).

    The rest of the code simply prints each word on a new line to see our handy work.

  • Jay Versluis 4:27 pm on September 1, 2014 Permalink | Reply
    Tags: , C64,   

    Categories: Commodore ( 26 )

    The evolution of Flow Control in Commodore BASIC 

    Commodore-Logo-PaddedWhen I had a C64 back in the days, the only IF/THEN statement we could create in BASIC v2 was just that: IF and THEN.

    There was no ELSE, and everything had to be on a single line. It was the only flow control we had back in those days, and it was a severe limitation compared to what we can do with higher level languages today.

    Here’s an example:

    10 input x
    20 if x=1 then print"x is one" : goto 100

    Such constructs weren’t very convenient when you wanted to react to anything other than the tested value. Instead you had to write an IF statement for every possible other outcome which could make the code very cluttered with several GOTO statements.

    When the Plus/4 came out Commodore added the ELSE statement. It still had to be on the same line as the IF and THEN, but at least we had ELSE as an option:

    10 input x
    20 if x=1 then print "x is one" : else print "x is not one"

    ELSE needs to be on the same line as the IF/THEN statement and separated by a colon.

    ELSE was a very handy addition to the BASIC arsenal, but it still didn’t solve the problem that your evaluation had to be cramped into a single line. This was improved on the C128 with the introduction of BEGIN/BEND.

    BEGIN and BEND allowed you to execute as many lines of code as you wanted between those statements when a condition was true (or false). Used together with IF, THEN and ELSE it was a very useful thing to have:

    10 input x
    20 if x=1 then begin
    30 print "x is one"
    40 print "which is like totally awesome"
    50 rem more code here
    60 bend : else begin
    70 print "x was obviously not one"
    80 print "which is a shame"
    90 print "but that's life"
    100 bend

    BEGIN means that everything starting from now until BEND is met will be executed as part of the IF/THEN statement – as many lines of code you like.

    When BEND is met the code continues as normal – but we can also use ELSE after a colon on the same line, indicating that this is still part of the first IF/THEN statement. Here we can use BEGIN again for as many lines as we like, until we finish with BEND again.

    It was very very cool!



    sys 32800,123,45,6 to show these credits (thanks, team!)

  • Jay Versluis 1:42 pm on August 30, 2014 Permalink | Reply
    Tags: , C64, , sd2iec   

    Categories: Commodore ( 26 )

    How to use SD2IEC: a quick command reference 

    Photo 28-08-2014 19 30 57
    I’ve ordered an SD2IEC a few weeks ago from Nic over at http://sd2iec.co.uk. It’s an SD card reader for Commodre computers and emulates many features of the 1541 Floppy Disk Drive.

    I went for the Limited Edition made from authentic recycled C64 plastic – so this little critter used to be a real C64! This has to be one of the coolest gadgets for any Commodore fan in the 21st century. And we know many people like gadgets of any kinds, to do many different things, that’s why we have lists of Electronics & Gadgets – Top9Rated.

    Nic and several other sellers on eBay build the hardware, while the software was developed by Ingo Korb with contributions from others. In this article I’ll explain how you can get the most out of the SD2IEC.

    Reading and Writing Files

    The SD2IEC works out of the box with standard disk commands (such as LOAD and SAVE). This will save a BASIC programme as PRG just like your Commodore would save it to floppy disk.

    You can create, switch into and remove directories on the SD card with your SD2IEC (or your “contemporary” computer of course). You can also read from and write to D64/D71/D81 image files and I’ll explain how this works further down.

    When you’re in a subdirectory (or a disk image) all read/write operations are performed there until you switch images or directories. We can do this by communicating with the SD2IEC via the command channel, as if we’re speaking to a 1541.

    Looks like the SD card is hot-swappable without the need to “safe-eject” as long as none of the lights are on, indicating read/write access.

    Speaking to your SD2IEC

    To issue commands on your SD2IEC we must open that trusty old command channel on the device (number 15), PRINT# the command and then optionally close the channel again. Here’s how to do that:

    OPEN 15,8,15
    PRINT#15,”your command here”
    CLOSE 15

    You can also write this on a single line:

    OPEN 15,8,15,”your command”:CLOSE 15

    You can also leave channel 15 open and issue more commands. Notice though that if you use any other disk command after opening channel 15 (such as LOAD or SAVE) all channels including 15 are closed automatically. Commands such as DIRECTORY do not close the channel.

    I find it helpful to add these things to one my my programmable keys (C128 and Plus/4) via the KEY command:

    // open command with trailing quotes
    KEY 1,"OPEN 15,8,15,"+CHR$(34)

    Hit F1, then type one of the commands below. If the channel is already open, simply issue the command via PRINT#15,”command”.


    The CD command lets you navigate the directory structure much like you would in Linux and Windows from the command line.

    // change into the root directory
    // enter directory “mydir” (relative to where you are)
    // enter directory “mydir” (absolute from root directory)
    // navigate up one level (like the Linux cd .. command)

    You can also create directories to stay organised without the need for other tools. That’s where the MD command comes in handy:

    // create directory in the current location
    // create directory inside another directory
    // create a directory absolute to root

    When you’re done with a directory you can delete it with RD. Note that only empty directories can be deleted, otherwise you’ll get a FILE EXISTS error:

    // remove in current directory
    // remove directory absolute to root

    Mounting Disk Images

    The CD command can also be used to mount D64/D71/D81 image files, just as if they were standard directories. The same syntax applies as with switching directories:

    // mount “myimage.d64” in current directory
    // mount “myimage.d64” in subdirectory /mydir (relative)
    // or absolute
    // unmount a disk image (go back SD card directory in which the image resides)

    You don’t have to mount disk images of course and can instead use a FAT or FAT32 formatted SD card just like it was a floppy disk. However, CBM DOS can only address a maximum of 144 files in a directory which means that a cheap 4GB SD card will be exhausted by this limitation before it’s anywhere near full.

    Changing the Device Address

    By default the SD2IEC is set to be drive number 8. But like its vintage counterpart you can change this to 9,10 or 11 using the following command:

    // change from drive 8 to drive 9
    OPEN 15,8,15
    CLOSE 15

    Replace the CHR$ value with the desired drive number. Just like a real 1541 drive, the SD2IEC will not remember this change upon reset. You can however save the above to its internal EEPROM which will survive subsequent reboots:

    // save drive number to EEPROM
    OPEN 15,9,15
    CLOSE 15

    Replace 9 with your actual drive number.


    If you encounter flashing lights on your device, then the SD2IEC is trying to tell you something. On the C128 and Plus/4 you can read out the disk error channel by looking at the DS$ system variable:

    // print disk error channel on C128 and Plus/4

    This will show you what went wrong (FILE EXISTS, FILE NOT FOUND, etc). On the C64 this is a little harder and requires you to write a small programme to read those values out. This is necessary because the INPUT# command can not be used in direct mode:

    // read disk error channel on C64
    10 open 15,8,15
    20 input#15,a,b$,c,d
    30 print a;b$,c,d
    40 close 15

    There’s a lot more you can do with this device – check out the full documentation in the README file at Ingo Korb’s website:

  • Jay Versluis 9:01 am on July 25, 2014 Permalink | Reply
    Tags: , C64,   

    Categories: Commodore ( 26 )

    How to merge BASIC programmes on your Commodore C64, C128 and Plus/4 

    Commodore LogoYou can merge / combine / concatenate BASIC programmes on a Commodore computer. It’s often useful to develop shorter chunks of code and put them together for a larger app.

    Sadly there’s no built-in command that lets you do this, even though the C128 user guide hints that the CONCAT command can do this (if you’ve ever tried you know that this is not the case and only works for sequential data files).

    With a few peeks and pokes we can accomplish what we need. Here’s how:

    • after loading the fist programme, we’ll set the start of BASIC to the end of programme 1
    • then we’ll load the second programme
    • and finally return the start of BASIC to the first one

    We’re left with both programmes in memory – without any changes in line numbers. Depending on how you write the code a quick RENUMBER command may be necessary here. Even though you can run a programme when the line numbers are out of sequence, GOSUB and GOTO routines may not work as expected.

    The commands are slightly different for each machine.

    On the C64

    The start of BASIC is stored in 43/44, and ends just before the variables begin – which is stored in 45/46. Load your first programme, then type this:

    poke 44,n/256
    poke 43,n-256*peek(46)

    Load the second part, then reset the BASIC pointers to where the first part resides:

    poke 43,1
    poke 44,8

    On the Plus/4

    The Plus/4 works almost identical to the C64 in regards to storing the BASIC pointers, however the start of BASIC is at a different address. Load your first programme, then type this (just like on the C64):

    poke 44,n/256
    poke 43,n-256*peek(46)

    Load the second part, then reset the BASIC pointers:

    poke 43,1
    poke 44,16

    On the C128

    The C128 stores variables in a different memory bank. Therefore it has a dedicated pointer to the end of BASIC which is missing on the other computers. The memory map is slightly different, and the start of BASIC is stored in 45/46. Load your first programme, then type this:

    poke 46,n/256
    poke 45,n-256*peek(46)

    Load the second part, then reset the BASIC pointers:

    poke 45,1
    poke 46,28

    And there you have it! Run any of these routines right after a reset or restart.

    Thanks to Rick Kephart for this tip. You may also be interested in another (more complex) solution to this puzzle by Jim Butterfield in which he reads each byte and merges them back on disk.

  • Jay Versluis 8:29 pm on July 6, 2014 Permalink | Reply
    Tags: , C64,   

    Categories: Commodore ( 26 )

    How to set the time on your Commodore C64 / C128 

    Commodore LogoDid you know that the Commodore computers had a built-in clock? It wasn’t an exact science, and it didn’t show the date, but it was able to display a 24hr clock.

    It did this by counting how many seconds your machine was switched on, accessible in two variables TI and TI$ (short for TIME).

    TI$ is a string value in the format HHMMss, counting seconds. 112347 would mean 11:23am and 47 seconds. TI$ will be 000000 when you switch on the system. You can set it just like any other variable to set the time:

    TI$ = "094500"

    TI counts the number of 60th seconds since the system was switched on (don’t ask):

    print int(ti/60);" seconds since startup"

    Both values reset when you reset the system. Like all internal computer clocks it is relatively accurate, but does drift a second or two per day (we don’t notice this on modern systems because those talk to an NTP server once every day and sync themselves).

    TI and TI$ are present on the C64, C128, Plus/4 and many other Commodore variants.

  • Jay Versluis 10:54 am on June 20, 2014 Permalink | Reply
    Tags: , C64,   

    Categories: Commodore ( 26 )

    How to load a .PRG file in VICE 

    Commodore LogoUsually you’d attach a disk image to the VICE emulator and load one of several files from your virtual disk. Occasionally though you’ll come across single files ending in .PRG – those are the files that are contained on a disk image.

    Remember those weird three letter abbreviations on the right hand side when you list your floppy directory?


    Those are the extensions of single files (there’s PRG, SEQ, USR and REL), and the actual file name appears on the left.

    VICE can attach disk images, but it can also display lose files we may have stored in a directory. All we need to do is tell VICE where that directory is and it will make it accessible to the virtual machine.

    To do this, start your favourite emulator and head over to Settings – Peripheral Settings. Next pick the drive you’d like to attach the directory to (say Drive 8) and tick the box that reads “Use IEC Device”. Now pick the desired directory and you’re all set!

    This will turn the entire directory into a disk image on the fly. Now you can list whatever is in said directory either with DIRECTORY or LOAD”$”,8 followed by LIST. Load and run your .PRG as you usually would.


    This works fine on the C64 and C128 emulators. The Plus/4 seems to have a problem with this approach as of VICE version 2.4.

  • Jay Versluis 3:57 pm on June 15, 2014 Permalink | Reply
    Tags: C64   

    Categories: Commodore ( 26 )

    How to switch between upper/lower case and PETSCII fonts on a C64 

    Commodore LogoThe C64 can display two sets of fonts: either a combination of upper case and lower case letters (good for any kind of text), or a combination of upper case letters and graphics (default), known as PETSCII characters.

    In direct mode you can switch between the two by pressing CBM+SHIFT (i.e. The Commodore Key and SHIFT together).

    But that’s no good when you’re app starts – you can’t expect the user to press that combination to see the correct letters on the screen (or that horrible combination of text and weird characters). The good news is that you can switch between those fonts in code too:


    While your app is running users would still be able to use the CBM+SHIFT combination to switch the fonts – which is ugly to say the least. But we can manually disable and enable this for users:


    Here are a couple of examples of what this looks like:



    The “Commodore 64 Programmer’s Reference” explains all this and more. I wish I knew this book existed back in the eighties! You can still grab a copy from Ian’s amazing site commodore.ca (in the Manuals section):

  • Jay Versluis 3:23 pm on June 15, 2014 Permalink | Reply
    Tags: C64   

    Categories: Commodore ( 26 )

    How to set the text and background colour on a C64 

    Commodore LogoTo set the background colour on your beloved old C64 (and C128 in 40 column mode) you have to POKE the colour value into two registers, one for the background and one for the border. The poor little guy only had 16 colour values, 0 for black, 1 for white, etc.

    To change the background to black:

    • poke 53280,0 (for the background)
    • poke 53281,0 (for the border)

    Likewise you can read the current background colour out using PEEK:

    • bg = peek (53280)
    • br = peek (53281)

    The text colour has to be set before any text is written. You can do this either by PRINTing the PETSCII character which will come up automatically once you type the first quote (and stop appearing when you type the second) – much like you can print cursor commands. It’s tacky, but it works.

    Another (perhaps better) way is to print a colour value using PRINT CHR$(x), where x is the value of the colour you’d like to write in.

    Yet another way would be to set the colour for each existing character on the screen, starting at 55296 via POKE (you’d have to do this 1024 times, once for every character).

    Refer to this handy table for all C64 colour values – courtesy of the C64 Wiki:

compose new post
next post/next comment
previous post/previous comment
show/hide comments
go to top
go to login
show/hide help
shift + esc