Tagged: BASIC Toggle Comment Threads | Keyboard Shortcuts

  • Jay Versluis 3:42 pm on April 17, 2018 Permalink | Reply
    Tags: BASIC, , ,   

    Categories: Commodore ( 41 )   

    Programmatic Loops in Commodore BASIC 

    In this screencast I’ll demonstrate how to use programmatic loops in Commodore BASIC.

    I’ll show you how to use the FOR/NEXT loop (available in all versions of Commodore BASIC), as well as the DO/WHILE loops (available on the Plus/4 and C128).

    Enjoy!





     
  • Jay Versluis 3:29 pm on April 16, 2018 Permalink | Reply
    Tags: BASIC, , ,   

    Categories: Commodore ( 41 )   

    Flow Control in Commodore BASIC 

    In this screencast I’ll explain the concept of Flow Control in Commodore BASIC. It’s kind of a video update of a post I did a while ago.

    In essence, it means that we can tell the programme to take a different route in the code depending on a condition that’s met. We’ll explore the IF/THEN and ON… GOTO/GOSUB statements (available on all versions of Commodore BASIC), as well as the expanded IF/THEN/ELSE version (available on the C128 and Plus/4 only).

    In addition, I’ll also show you how to use the BEGIN and BEND clauses that were introduced with the C128.





     
  • Jay Versluis 3:26 pm on April 16, 2018 Permalink | Reply
    Tags: BASIC   

    Categories: Commodore ( 41 ), Linux ( 101 ), macOS ( 37 ), Screencast ( 87 )   

    How to run Commodore BASIC as a Scripting Language on macOS 

    Did you know you can run Commodore BASIC v2 on your Mac and Linux systems as a scripting language? It’s true – thanks to the marvellous efforts of Michael Steil and James Abbatiello. They’ve adapted the original BASIC v2 as featured on the VIC-20 and C64 with additional routines so that it works natively on modern machines. It’s ingenious!

    You can get the full source code on GitHub – it works a treat!

    For those who don’t quite know what to do with it, here are some instructions that’ll help you get CBM BASIC up and running on macOS.

    Download

    GitHub provides a convenient way to either clone a repository on your local machine if you have GitHub for Desktop installed, or you can download a ZIP file and unZIP it somewhere on your system. Once done, you should see a directory structure that looks exactly like the repo on GitHub.

    Compile

    You need a few utilities installed your your Mac to be able to compile files. Downloading Xcode will ptovide you with an excellent (and free) IDE as well as the command line tools needed to do that (gcc, make and several other goodies). You might be able to bring those components in using the Homebrew package manager.

    Using your Terminal app, navigate to your unZIPped folder. It includes a MAKEFILE so there’s no need to mess with any parameters. Enter “make” at the command prompt, and after a few moments you should have a file called “cbmbasic” without an extension in the same directory. That’s the binary for your Mac.

    To make it executable, we’ll have to tweak the file permissions – otherwise our operating system won’t be able to run it. We’ll do it using the chmod command:

    chmod +x ./cbmbasic

    You can test if it works by calling the binary without any parameters like this:

    ./cbmbasic

    If all goes well you should see something like this:

    For easy access, copy the binary over to your /usr/bin directory. That’s where common command line utilities are installed on Mac and Linux systems. The added benefit is that the path to that folder is already in your local $PATH variable, and as such you can simply type “cbmbasic” from any location when you’re on the command line.

    Here’s how to copy the binary over (this will ask for your administrator password):

    sudo cp ./cbmbasic /usr/bin

    Using Direct Mode

    When you run the binary it will bring up something like the standard BASIC command prompt we’re used to seeing on the Commodore 64. There are however a few important differences between a C64 emulator and this implementation:

    • this is NOT an emulator
    • cursor keys DO NOT work
    • all commands typed in must be CAPITALISED

    Other than that, you can start typing like on a real machine. Be careful with certain POKE commands though as those may call system routines that might not be implemented.

    LOAD and SAVE commands have been tweaked to reference the command line structure. They work just fine, but will load and save files in Commodore file format (which is not clear text ASCII – more on that later). For example:

    LOAD"$"
    
    LIST
    
    0 "/Users/versluis/Desktop/cbmbasic" 00 2A
    4    "."                PRG  
    2    ".."               PRG  
    2    ".git"             PRG  
    567  "cbmbasic"         PRG  
    7350 "cbmbasic.c"       PRG  
    593  "cbmbasic.o"       PRG  
    19   "cbmbasic.vcproj"  PRG  
    20   "console.c"        PRG  
    3    "console.h"        PRG  
    8    "console.o"        PRG  
    4    "glue.h"           PRG   
    1    "Makefile"         PRG  
    2    "Makefile.sun32"   PRG  
    3    "Makefile.win32"   PRG  
    27   "plugin.c"         PRG  
    
    ...
    
    READY.

    The above displays the directory of the current folder, much like it was an attached floppy drive. How cool is that? You can reference folders using both LOAD and SAVE just as if you were on the command line.

    You can also type in programmes and run them – however the cursor keys won’t work, so there’s no screen editing options. Programmes SAVEd from direct mode cannot be loaded from the command line, only from direct mode (i.e. when launching the binary without parameters).

    To quit direct mode, hit CTRL+C.

    Running Programmes from the Command Line

    The real power of this implementation can be seen when we run files from the command line. Those files must not be SAVEd from direct mode, but instead are simple clear text files. The extension doesn’t matter, but for good practice, let’s store them as .BAS files (much like shell scripts are stored with the.SH extension, or PHP files would be stored with the .PHP extension… you get the idea).

    You can write standard BASIC programmes use your favourite text editor (like vi from the command line), or try one from the “test” directory that’s provided with the repository.

    Imagine we had a file called “hello.bas” that we’ve created with vi, looking like this:

    10 PRINT "Hello World!"

    To run our file, we can simply define it behind the binary like this:

    cbmbasic hello.bas

    This will greet us with the “Hello World” message on the command line.

    Running Script Files

    Alternatively, we can specify the full path to cbmbasic at the beginning of a file (she-bang notation) and run it just like any other script file. Observe please:

    #!/usr/bin/cbmbasic
    10 PRINT “Hello World”
    20 PRINT “from CBM BASIC :-)”

    If the file was called “hello”, we’d need to change the permissions again so that the file can be executed:

    chmod +x ./hello

    Now we can run it like this:

     ./hello
    
    Hello World
    from CBM BASIC :-)

    Sweet – but I can’t work out how to compile this on macOS…

    Fear not – I’ve got a macOS binary that was compiled on 10.12 “Sierra”. You can find it in my fork of the project. Check out the “binaries” folder.

    Does it work on Windows? Or on Linux?

    I’ve tested compiling and running this puppy on CentOS 6 and 7 with roaring success. The above steps worked fine for it, so I’m assuming it’ll work on other Linux flavours just as well (the beauty of portable C code).

    According to the author, it works fine on Windows too – however I have no idea how to compile source code on Windows, so you’ll have to figure that out for yourself. I hear good things about Visual Studio  – if I work out how to do it, I’ll add it to the “binaries” folder on my GitHub Fork.

    Can I write my own extensions to BASIC?

    Apparently so – check our Michael’s site and repo for details:

    Right now, if you run SYS 1 from direct mode first, you can use the SYSTEM command (followed by anything you’d like to execute on the command line in “double quotes”) as well as LOCATE (followed by an x and y coordinate to place the text cursor) and WAIT.

    Have fun hacking BASIC and letting it run with the blistering speed of modern CPUs 🙂





     
  • Jay Versluis 12:10 am on April 2, 2018 Permalink | Reply
    Tags: BASIC, , ,   

    Categories: Commodore ( 41 )   

    How to print Numbers as Columns in Commodore BASIC 

    In this video I’m demonstrating how to print numbers in evenly spaced columns in Commodore BASIC.

    On the C128 and the Plus/4 we can use a nifty little function called PRINT USING for this, with which we can format the output of any printed text or variable.

    On the C64 and VIC-20 that function doesn’t exist, so we’ll have to convert a numeric value into a string (using the STR$ function), and then determine how long our string is. Following that we’ll have to manually pad our string value with as many spaces as are required. (More …)





     
  • Jay Versluis 9:45 am on March 29, 2018 Permalink | Reply
    Tags: BASIC,   

    Categories: Commodore ( 41 ), Screencast ( 87 )   

    How to build a Word Splitter on the C64 in Commodore BASIC 

    In this video I’m demonstrating how to build a word splitter on the Commodore 64. We’ll use string functions to parse a sentence and split each word off into an array of words so that they can be analysed later (for example, as part of an adventure game).

    Here’s the code I’m building:

    20 input a$
    30 gosub 100
    40 print:print wd;" words:"
    50 for i=1 to wd
    60 print wd$(i)
    70 next
    99 end
    100 rem word splitter
    110 lt$="":wd=1
    120 for i=1 to len(a$)
    130 lt$=mid$(a$,i,1)
    140 if lt$=" " then wd=wd+1:next
    150 wd$(wd)=wd$(wd)+lt$
    160 next
    199 return

    The interesting part starts in line 100 and onwards, where I’m building a subroutine that deals with the string functions. In line 110 I’m resetting/initialising two of the three important variables: LT$ holds a single letter from the phrase we’re getting in A$, while WD is counting each word we’re splitting out.

    The FOR loop in line 120 parses each letter of the phrase, and if it finds a space character (line 140), the word count is increased. If the letter is not a space, then it’s added to the current word held in WD$(WD). The current word is assembled character by character.

    Apologies for the audio quality, I did this on my laptop while sitting on the balcony, hence sea planes flying overhead can be heard (as well as the neighbours dog and kids).

    Happy hacking 🙂





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

    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: BASIC,   

    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





     
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