Tagged: C64 Toggle Comment Threads | Keyboard Shortcuts

  • Jay Versluis 9:58 am on March 28, 2018 Permalink | Reply
    Tags: , C64   

    Categories: Commodore ( 41 ), Screencast ( 87 )   

    How to build a time of day clock on the Commodore 64 

    In this video I’ll demonstrate how to build a simple clock on the C64. We’ll go through this process step by step, including the built-in TI and TI$ variables, string formatting with LEFT$, RIGHT$ and MID$, as well as screen formatting.

    Here’s the code I’m writing – works in Commodore BASIC v2 and above:

    5 input "qwhat is the current time (hhmm
    ss) ";ti$
    10 print chr$(147):print chr$(5)
    20 a$ = left$(ti$,2)
    25 a$ = a$ +":"
    30 a$ = a$ + mid$(ti$,3,2)
    35 a$ = a$ +":"
    40 a$ = a$ +right$(ti$,2)
    50 gosub 200
    60 print chr$(19)
    70 print "qqqqqqqqqqq]]]]]]]]]]]]]]curre
    nt time"
    80 print "]]]]]]]]]]]]]]]]";
    90 print a$
    100 goto 20
    200 rem print a box
    210 print chr$(19)
    220 print "qqqqqqqqqq]]]]]]]]]]]]]UCCCCC
    CCCCCCCI"
    230 print "]]]]]]]]]]]]]B]]]]]]]]]]]]B"
    240 print "]]]]]]]]]]]]]B]]]]]]]]]]]]B"
    250 print "]]]]]]]]]]]]]JCCCCCCCCCCCCK"
    299 return

    Many of the characters that appear in this listing are cursor control characters and appear in reverse in the video. They either position the cursor or print PETSCII graphics.

    Inspired by David’s video, in which he connects an LCD screen to his C64’s User Port: https://www.youtube.com/watch?v=vV8FbwobrKY





     
  • Jay Versluis 9:51 am on March 27, 2018 Permalink | Reply
    Tags: , C64   

    Categories: Commodore ( 41 ), Screencast ( 87 )   

    How to create random YouTube URLs in Commodore BASIC v2 

    In this episode I’ll demonstrate how to create those seemingly random YouTube Video IDs using a Commodore 64.

    Here’s the code I’m writing – works in BASIC v2 and above:

    10 print chr$(14)
    20 gosub 100:x=rnd(-ti):cn=1
    30 a$="https://youtu.be/"
    40 for i=1 to 11
    50 rn=int(rnd(0)*62)+1
    60 a$=a$+yt$(rn)
    70 next
    80 print:print cn;" : ";a$
    85 cn=cn+1
    90 goto 30
    85 cn=cn+1
    90 goto 30
    
    100 rem populate array
    110 dim yt$(62)
    120 i=1
    130 for j=65 to 90
    140 yt$(i)=chr$(j)
    150 i=i+1
    160 next j
    170 for j=193 to 218
    180 yt$(i)=chr$(j)
    190 i=i+1
    200 next j
    210 for j=48 to 57
    220 yt$(i)=chr$(j)
    230 i=i+1
    240 next j
    299 return

    The first line switches to lower case letters (I forgot to show that in the video).

    NOTE: In addition to the upper case and lower case alphabet, and the numbers 0-9, YouTube also use two special characters that I forgot to mention in the video. One is the standard minus sign (-), and the other one is the underscore (_). The Commodore machines cannot produce the latter. For simplicity’s sake, I’ve left both of those out (just though I’d mention it here).

    Inspired by Tom Scott’s video “Will YouTube ever run out of Video IDs” – watch it here: https://www.youtube.com/watch?v=gocwRvLhDf8





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

    Categories: Commodore ( 41 )   

    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$)
    17

    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:

    a$="one-two-three"
    
    print left$(a$,3)
    one

    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:

    a$="one-two-three"
    
    print right$(a$,5)
    three

    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:

    a$="one-two-three"
    
    print mid$(a$,5,3)
    two

    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:

    mid$(a$,5,3)="ten"
    
    print a$
    one-ten-three

    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"
    
    b$="cake"
    
    print instr(a$,b$)
     5

    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)
     0

    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 ( 41 )   

    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 ( 41 )   

    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 ( 41 )   

    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!

     

    Herdware

    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 ( 41 )   

    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”.

    Directories

    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
    CD//
    
    // enter directory “mydir” (relative to where you are)
    CD/mydir/
    
    // enter directory “mydir” (absolute from root directory)
    CD//mydir/
    
    // navigate up one level (like the Linux cd .. command)
    CD:←
    

    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
    MD:mydir
    
    // create directory inside another directory
    MD/mydir/:otherdir
    
    // create a directory absolute to root
    MD//mydir/:otherdir
    

    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
    RD:mydir
    
    // remove directory absolute to root
    RD//mydir/:otherdir
    

    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
    CD:myimage.d64
    
    // mount “myimage.d64” in subdirectory /mydir (relative)
    CD/mydir/myimage.d64
    
    // or absolute
    CD//mydir/myimage.d64
    
    // unmount a disk image (go back SD card directory in which the image resides)
    CD:←
    

    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
    PRINT#15,“U0>"+CHR$(9)
    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
    PRINT#15,“XW"
    CLOSE 15
    

    Replace 9 with your actual drive number.

    Troubleshooting

    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
    PRINT DS$
    

    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 ( 41 )   

    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:

    n=(peek(45)+256*peek(46))-2
    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
    clr
    

    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):

    n=(peek(45)+256*peek(46))-2
    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
    clr
    

    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:

    n=peek(174)+256*peek(175)-2
    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
    clr
    

    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 ( 41 )   

    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 ( 41 )   

    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?

    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.

    Capture

    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.





     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel