<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: amuletofyendor </title>
    <description>The latest articles on Forem by amuletofyendor  (@ianwitham).</description>
    <link>https://forem.com/ianwitham</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1004288%2F0a6e8a1f-9be1-4e80-b627-2e1e1cab9de1.png</url>
      <title>Forem: amuletofyendor </title>
      <link>https://forem.com/ianwitham</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ianwitham"/>
    <language>en</language>
    <item>
      <title>Optical Illusion tutorial with the Commodore 64 and DurexForth</title>
      <dc:creator>amuletofyendor </dc:creator>
      <pubDate>Wed, 25 Jan 2023 02:31:50 +0000</pubDate>
      <link>https://forem.com/ianwitham/optical-illusion-tutorial-with-the-commodore-64-and-durexforth-4hbm</link>
      <guid>https://forem.com/ianwitham/optical-illusion-tutorial-with-the-commodore-64-and-durexforth-4hbm</guid>
      <description>&lt;p&gt;Hello again. &lt;a href="https://dev.to/ianwitham/a-brief-introduction-to-durexforth-for-the-commodore-64-1c99"&gt;Start here&lt;/a&gt; if you don't know what DurexForth is, or perhaps even what a Commodore 64 is.&lt;/p&gt;

&lt;p&gt;Let's expand on my previous post with something a bit more fun than yet-another-fizzbuzz.&lt;/p&gt;

&lt;p&gt;In this post, we'll implement the &lt;a href="https://en.wikipedia.org/wiki/Stepping_feet_illusion" rel="noopener noreferrer"&gt;"stepping feet" illusion&lt;/a&gt;. Here's an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhf0mm3eo5wcafso9y75b.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhf0mm3eo5wcafso9y75b.gif" alt="Stepping Feet illusion by Rinuraeni/Wikimedia Commons" width="482" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's think about how to implement this. We'll need the black and white vertical bars, and a way to quickly switch them off to show a plain gray background (thus revealing the illusion). Commodore 64 character graphics seem just about ideal for this.&lt;/p&gt;

&lt;p&gt;The coloured "feet" can be hardware sprites moving across this background. Two big rectangles must be just about the simplest sprites we could imagine!&lt;/p&gt;

&lt;p&gt;But first, the alternating bars. I want to implement the colours in colour memory, and then just fill the screen with "reversed" spaces to display the coloured bars. That way I can turn off the bars by simply filling the screen with "non-reversed" spaces (which are invisible), leaving the alternating colours intact in the colour memory but allowing the background "screen colour" to show through. I'll make this gray just like in the example.&lt;/p&gt;

&lt;p&gt;Let's begin by starting up the DurexForth 'v' editor, and entering some useful constants.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$0400 constant SCREENMEM
$d800 constant COLORMEM
53281 constant SCREENCOLOR
32 constant SPACE-CHAR
160 constant REVERSE-CHAR
12 constant GRAY2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how easily numbers can be entered in either hexadecimal or decimal. You can also enter numbers as binary by prefixing with '%', e.g. &lt;code&gt;%10000011&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now I'll make a helper word (i.e. a function) to fill the screen with a given character. Although DurexForth is &lt;em&gt;much&lt;/em&gt; faster than Basic, using a loop to fill in the screen memory is still noticeably slower than if you were to do it in assembly. Fortunately, there is a better way. Let's take a look at the &lt;code&gt;fill&lt;/code&gt; word from the DurexForth manual:&lt;/p&gt;

&lt;pre&gt;
fill ( addr len char — )
Fill range [addr, len + addr) with char.
&lt;/pre&gt;

&lt;p&gt;Note the stack comment, &lt;code&gt;( addr len char — )&lt;/code&gt;. This indicates what values the word will expect to take off the stack (before the dash), and what values it may leave on the stack (after the dash). So, the &lt;code&gt;fill&lt;/code&gt; word will consume three parameters from the stack and add nothing back onto the stack. It's good practice to leave comments like this on your own words too, as it helps you to prevent stack overflow or underflow, which will crash your program.&lt;/p&gt;

&lt;p&gt;So, using this word to fill the first 100 characters of screen memory with the 'A' character would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$0400 100 1 fill
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;code&gt;$0400&lt;/code&gt; is the start address of the fill operation, &lt;code&gt;100&lt;/code&gt; is the number of bytes to fill, and &lt;code&gt;1&lt;/code&gt; is the byte you want to fill that memory area with (i.e. the character code for the letter 'A').&lt;/p&gt;

&lt;p&gt;We want to fill screen memory, so the first two parameters will &lt;em&gt;always&lt;/em&gt; be $0400 and 1000. Here's how the helper word looks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;: fillscreen ( char -- )
 SCREENMEM 1000 rot fill ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;first, take a look at the stack comment. This word expects the character code to already be on the stack. Then the word itself will push the SCREENMEM constant onto the stack ($0400), followed by size in bytes of screen memory (1000).&lt;/p&gt;

&lt;p&gt;The stack will now contain &lt;code&gt;char addr len&lt;/code&gt;. Referring back to the documentation for &lt;code&gt;fill&lt;/code&gt; we see that it requires those values in this order: &lt;code&gt;addr len char&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's take a look at the documentation for the built-in stack-manipulation word, &lt;code&gt;rot&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;
rot ( a b c — b c a )
Rotate the third item to the top.
&lt;/pre&gt;

&lt;p&gt;Substitute &lt;code&gt;a b c&lt;/code&gt; for &lt;code&gt;char addr len&lt;/code&gt;, and you can see this will give us just what we need. There are many such stack-manipulation words for getting parameters into the required order.&lt;/p&gt;

&lt;p&gt;Now we have our &lt;code&gt;fillscreen&lt;/code&gt; helper word which we can use to fill the screen with any character we give it. For example, using our predefined character constants:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REVERSE-CHAR fillscreen  \ show the black and white bars
SPACE-CHAR fillscreen    \ hide them again
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we'll implement the colours. We can put this into a setup word, as once we've defined these colours, we don't need to touch them again. Also, as this is a one-off setup routine, I don't mind using a slightly slow loop construct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;: coloursetup
GRAY2 SCREENCOLOR c!
1000 0 do
 i 1 and COLORMEM i + c!
loop ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The snippet &lt;code&gt;i 1 and&lt;/code&gt; will give us a zero for even-numbered iterations, and a one for odd-numbered iterations. Fortuitously, these are the colour codes for black and white respectively. &lt;code&gt;c!&lt;/code&gt; simply stores a byte at an address. It's POKE with a less silly name.&lt;/p&gt;

&lt;p&gt;Let's add a couple of words to test what we have so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;: mainloop
 REVERSE-CHAR fillscreen
 key drop
 SPACE-CHAR fillscreen
 key drop
recurse ;

: main
 screen-setup
 mainloop ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;key&lt;/code&gt; word gets one character from input and leaves it on the stack. We don't want to use that character for anything, we only want to check if a key has been pressed, so we can discard that value from the stack with the &lt;code&gt;drop&lt;/code&gt; word.&lt;/p&gt;

&lt;p&gt;Let's try it out. If you're in the 'v' editor, hit &lt;code&gt;F7&lt;/code&gt; to compile and return to the immediate prompt, and then run the &lt;code&gt;main&lt;/code&gt; word. Tapping a key will toggle the black and white bars on or off. Hit RESTORE to exit the program ("Page up" on my Vice key mapping... YMMV).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7xrpzgng45uguejw0whd.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7xrpzgng45uguejw0whd.gif" alt="Image description" width="720" height="576"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's move on to the sprites. First, let's add some more useful constants to the top of the program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;53280 constant VIC
14 constant SPRITEPTR
4 constant BLUE
7 constant YELLOW
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that it is common in BASIC to store a base address for the VIC-II registers as &lt;code&gt;V=53280&lt;/code&gt;, and then reference the various registers by their offset, e.g. &lt;code&gt;POKE V+21, 3&lt;/code&gt;. While I am going to do the same in my program, I'm using the name VIC, so as not to collide with the command to open the 'v' text editor. Despite my convention of using ALL CAPS for constant names, these names are case insensitive so naming this constant 'V' would be a problem.&lt;/p&gt;

&lt;p&gt;Here's the wall of POKEs required to initialise the two sprites:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;: init-sprites
 SPRITEPTR 2040 c! \ sprite 0
 SPRITEPTR 2041 c! \ sprite 1
 SPRITEPTR 64 * 64 $ff fill \ data
 YELLOW VIC 39 + c!
 BLUE VIC 40 + c!
 \ sprite x expand
 3 VIC 29 + c!
 \ sprite initial position
 50 VIC c!       \ sprite 0 xpos
 100 VIC 1 + c!  \ sprite 0 ypos
 50 VIC 2 + c!   \ sprite 1 xpos
 180 VIC 3 + c!  \ sprite 1 ypos
;

: show-sprites
 3 VIC 21 + c! ;

: hide-sprites
 0 VIC 21 + c! ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is straightforward sprite boilerplate. As I mentioned, I'm just poking values into various VIC registers to initialise the sprites, their size, their colour and their positions. I use the &lt;code&gt;fill&lt;/code&gt; word again to initialise the sprite data because we just want to turn on every pixel... i.e. a plain rectangle of pixels. There a many resources online which explain the various registers and how to set up your sprites. &lt;a href="https://retro64.altervista.org/blog/programming-sprites-the-commodore-64-simple-tutorial-using-basic-v2/" rel="noopener noreferrer"&gt;Here's a pretty good example&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;At this point I want to add a value to track whether the black and white bars are currently hidden. This can go near the top of the program with the constants:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 value hidebars
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax for using a value is nice and simple. &lt;code&gt;hidebars&lt;/code&gt; will push the current value of hidebars to the stack, and &lt;code&gt;val to hidebars&lt;/code&gt; will set hidebars with a new value. In our case we want to toggle the value between true and false, so we'll use &lt;code&gt;hidebars invert to hidebars&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now all that is left is to modify our &lt;code&gt;main&lt;/code&gt; and &lt;code&gt;mainloop&lt;/code&gt; words to incorporate the sprites.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;: mainloop
 begin
 [ VIC inc, VIC 2 + inc, ]
 100 0 do
  key? if
   key drop
   hidebars invert to hidebars
   hidebars if
    SPACE-CHAR fillscreen
   else
    RVS-CHAR fillscreen
   then
  then
 loop
 again ;

: main
 screen-setup
 init-sprites
 RVS-CHAR fillscreen
 show-sprites
 mainloop ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that I've used some inline assembly to increment the x-position of the two sprites. This is a rare case where I feel assembly code provides nicer syntax.&lt;/p&gt;

&lt;p&gt;The alternative in forth would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;VIC dup c@ 1+ swap c!
VIC 2 + dup c@ 1+ swap c!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another change is that I'm now using &lt;code&gt;key?&lt;/code&gt; to check whether there is a character available for &lt;code&gt;key&lt;/code&gt;. This prevents the program from waiting for input when there is none, which would pause the animation.&lt;/p&gt;

&lt;p&gt;Let's take a look at the end result:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/aL8ZStWd4NE"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Here's the full program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$0400 constant SCREENMEM
$d800 constant COLORMEM
53281 constant SCREENCOLOR
32 constant SPACE-CHAR
160 constant RVS-CHAR
12 constant GRAY2
53248 constant VIC
14 constant SPRITEPTR
6 constant BLUE
7 constant YELLOW
0 value hidebars

: screen-setup  ( -- )
 GRAY2 SCREENCOLOR c!
 1000 0 do
  i 1 and COLORMEM i + c!
 loop ;

: fillscreen ( char -- )
SCREENMEM 1000 rot fill ;

: init-sprites
 SPRITEPTR 2040 c! \ sprite 0
 SPRITEPTR 2041 c! \ sprite 1
 SPRITEPTR 64 * 64 $ff fill \ data
 YELLOW VIC 39 + c!
 BLUE VIC 40 + c!
 \ sprite x expand
 3 VIC 29 + c!
 \ sprite initial position
 50 VIC c!       \ sprite 0 xpos
 100 VIC 1 + c!  \ sprite 0 ypos
 50 VIC 2 + c!   \ sprite 1 xpos
 180 VIC 3 + c!  \ sprite 1 ypos
;

: show-sprites
 3 VIC 21 + c! ;

: hide-sprites
 0 VIC 21 + c! ;

: mainloop
 begin
 [ VIC inc, VIC 2 + inc, ]
 100 0 do
  key? if
   key drop
   hidebars invert to hidebars
   hidebars if
    SPACE-CHAR fillscreen
   else
    RVS-CHAR fillscreen
   then
  then
 loop
 again ;

: main
 screen-setup
 init-sprites
 RVS-CHAR fillscreen
 show-sprites
 mainloop ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>A Brief Introduction to DurexForth for the Commodore 64</title>
      <dc:creator>amuletofyendor </dc:creator>
      <pubDate>Mon, 09 Jan 2023 11:51:10 +0000</pubDate>
      <link>https://forem.com/ianwitham/a-brief-introduction-to-durexforth-for-the-commodore-64-1c99</link>
      <guid>https://forem.com/ianwitham/a-brief-introduction-to-durexforth-for-the-commodore-64-1c99</guid>
      <description>&lt;p&gt;Forth is pretty great. In an alternate timeline, the C64 might have had Forth loaded into ROM instead of BASIC (indeed, some other 8-bit computers of the era did just that).&lt;/p&gt;

&lt;p&gt;My goal is to keep this post brief and informative, so in that spirit, let's get a few things out of the way.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Commodore 64?
&lt;/h3&gt;

&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Commodore_64" rel="noopener noreferrer"&gt;40-year-old 8-bit computer&lt;/a&gt; that impressed users both then and now with its &lt;a href="https://www.youtube.com/watch?v=q56-23D7omY" rel="noopener noreferrer"&gt;graphics and sound capabilities&lt;/a&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is Forth?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Forth_(programming_language)" rel="noopener noreferrer"&gt;A procedural, stack-based programming language&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is DurexForth?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/jkotlinski/durexforth" rel="noopener noreferrer"&gt;An actively developed version of Forth for the Commodore 64, based on the Forth 2012 core standard&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why would I want to use Forth instead of Basic?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It's much faster&lt;/li&gt;
&lt;li&gt;It allows structured programming (essentially, this means subroutines with parameters and return values)&lt;/li&gt;
&lt;li&gt;It's extendable&lt;/li&gt;
&lt;li&gt;It's as high level as you want (build as many new capabilities into the language as you want), or as low level (drop into assembly code whenever you need to go even faster).&lt;/li&gt;
&lt;li&gt;You might discover you love it&lt;/li&gt;
&lt;li&gt;You might even find it's still a handy language for programming small microcontrollers, or &lt;a href="https://en.wikipedia.org/wiki/Philae_(spacecraft)" rel="noopener noreferrer"&gt;deep space exploration probes&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Some people even prefer it as a scripting language over the likes of Lua or Python&lt;/li&gt;
&lt;li&gt;Because just try to do &lt;em&gt;this&lt;/em&gt; in Basic:
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/pE4tUkBrWV8"&gt;
&lt;/iframe&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What does it look like?
&lt;/h3&gt;

&lt;p&gt;To the uninitiated, perhaps a little odd. Let's take as an example the famous &lt;a href="https://en.wikipedia.org/wiki/Fizz_buzz" rel="noopener noreferrer"&gt;FizzBuzz&lt;/a&gt; program. So you'll have a point of reference, I'll first show the algorithm in both a modern c-like language and in Commodore Basic 2.0.&lt;/p&gt;

&lt;p&gt;In a modern language, it would perhaps look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;%&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;==&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;%&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;==&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"fizzbuzz"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;%&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;==&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"fizz"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;%&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;==&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"buzz"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Commodore Basic 2.0 (the Commodore 64's "built-in" language), it could look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10 I=0
20 I=I+1 : IF I &amp;gt; 100 THEN END
30 IF INT(I/3)=I/3 AND INT(I/5)=I/5 THEN PRINT "FIZZBUZZ"
35 GOTO 20
40 IF INT(I/3)=I/3 THEN PRINT "FIZZ"
45 GOTO 20
50 IF INT(I/5)=I/5 THEN PRINT "BUZZ"
55 GOTO 20
60 PRINT I
65 GOTO 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A couple of things to note here. First, there is no modulo operator in Basic 2.0, so I'm performing an inline test to check whether a number divides evenly by either 3 or 5.&lt;/p&gt;

&lt;p&gt;If we wanted to factor out the modulo operation (sort of), we could do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10 DEF FN D3(X) = INT(I/3)=I/3
20 DEF FN D5(X) = INT(I/5)=I/5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note, however, that only one parameter is allowed, so we couldn't do something like &lt;code&gt;DEF FN DIVIDESBY(I,D) = INT(I/D)=I/D&lt;/code&gt;. Additionally, variable names in Basic only regard the first two characters as significant, which is why I've gone for &lt;code&gt;D3&lt;/code&gt; and &lt;code&gt;D5&lt;/code&gt; over the more descriptive names &lt;code&gt;DIVIDES_BY_3&lt;/code&gt; and &lt;code&gt;DIVIDES_BY_5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Secondly, I'd like to point out that there is no IF, ELSE IF, ELSE structure in basic, forcing the programmer to implement the desired behaviour with the dreaded GOTO statement.&lt;/p&gt;

&lt;p&gt;Finally, let's take a look at some Forth&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;: fizzbuzz
  101 1 do
    i 3 mod 0= i 5 mod 0= and if
      ." fizzbuzz" cr
    else i 3 mod 0= if
      ." fizz" cr
    else i 5 mod 0= if
      ." buzz" cr
    else i . cr
    then then then
  loop ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a quick guide to all the moving parts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;: fizzbuzz
...
;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are compiling a new word into Forth, &lt;code&gt;fizzbuzz&lt;/code&gt;. "Word" is just Forth-speak for a subroutine. You make a program by extending Forth with your own words. In fact there are two "core" words hidden in that snippet, &lt;code&gt;:&lt;/code&gt; to define a word and enter the so-called compilation state, and &lt;code&gt;;&lt;/code&gt; to end the current definition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;101 1 do
...
loop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we push two values to the parameter stack, &lt;code&gt;101&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt;. The stack is a first-in last-out data structure which is key to how Forth programs work. It acts as a sort of universal mechanism for passing parameters and returning results.&lt;/p&gt;

&lt;p&gt;Now we call the core word &lt;code&gt;do&lt;/code&gt;. This consumes the parameters that were placed on the stack and sets up some loop controls variables (on another stack... the return stack. but that's not important right now -- forget I mentioned it).&lt;/p&gt;

&lt;p&gt;As you can probably guess, &lt;code&gt;loop&lt;/code&gt; is a word marking the end of the loop. In addition to &lt;code&gt;do ... loop&lt;/code&gt;, Forth supports &lt;code&gt;begin ... again&lt;/code&gt; for endless loops and both &lt;code&gt;begin ... until&lt;/code&gt; and &lt;code&gt;begin ... while ... repeat&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i 3 mod 0=
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This places two parameters on the stack, &lt;code&gt;i&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt; and calls the &lt;code&gt;mod&lt;/code&gt; word, which is a proper built-in modulo operation. &lt;code&gt;mod&lt;/code&gt; consumes the two parameters from the stack, and replaces them with the result of the modulo operation.&lt;/p&gt;

&lt;p&gt;But what is &lt;code&gt;i&lt;/code&gt;? It's a little bit magic, but not really. &lt;code&gt;i&lt;/code&gt; is a word that copies the current iteration value from the return stack to the top of the parameter stack. So for the first iteration we'll have &lt;code&gt;1 3 mod 0=&lt;/code&gt;, the next iteration we'll have &lt;code&gt;2 3 mod 0=&lt;/code&gt; and so on.&lt;/p&gt;

&lt;p&gt;Next, &lt;code&gt;0=&lt;/code&gt; checks whether the value at the top of the stack is zero, replacing it on the stack with a boolean result. The actual boolean will be represented by either 0 (false) or -1 (true), just like in Basic.&lt;/p&gt;

&lt;p&gt;By the way, why 0 and -1 and not 0 and 1? Because the byte representation of -1 is 11111111, which is about as "true" as you can get, and 0 is 00000000, which is pretty darn false. Actually, the stack holds 16-bit numbers, but I digress further from this digression.&lt;/p&gt;

&lt;p&gt;Now take a look at this snippet and try to imagine what's happening to the parameter stack at each step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i 3 mod 0= i 5 mod 0= and
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's step through it. For the purposes of this demonstration, we'll assume &lt;code&gt;i&lt;/code&gt; to be &lt;code&gt;10&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;executing...&lt;/th&gt;
&lt;th&gt;leaves this on the stack...&lt;/th&gt;
&lt;th&gt;note&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;i&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;10&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;push a value to the stack&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;3&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;10 3&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;...and another.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;mod&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;the result of 10 % 3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;...is not equal to zero.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;i&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0 10&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;our previous result is safe on the stack.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;5&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0 10 5&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;mod&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0 0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0 -1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;we have "false, true" on the stack&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;and&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;em&gt;false&lt;/em&gt; and &lt;em&gt;true&lt;/em&gt; = &lt;em&gt;false&lt;/em&gt;. 10 is not a "fizzbuzz" number&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you're familiar with &lt;a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation" rel="noopener noreferrer"&gt;Reverse Polish Notation&lt;/a&gt;, you'll probably see what's going on here. If not, you'll need to familiarise yourself with the concept before you can have much fun with Forth, as it goes hand-in-glove with stack-based programming.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;... if ... else ... then
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well we know what this is, but in the spirit of RPN, things are a little backwards.&lt;/p&gt;

&lt;p&gt;Let's fill in the gaps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[predicate] if [true part] else [false part] then
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the idiomatic role of the word "then". In Basic, you'd read it as "&lt;em&gt;if&lt;/em&gt; this predicate is true, &lt;em&gt;then&lt;/em&gt; do this". In some languages, e.g. Visual Basic, you'd close it out with "End If".&lt;/p&gt;

&lt;p&gt;In Forth, &lt;em&gt;then&lt;/em&gt; marks the end of the conditional structure, much like an "end if". So I imagine it means something more like "here's a predicate, &lt;em&gt;if&lt;/em&gt; it's true I'll do some stuff here, &lt;em&gt;then&lt;/em&gt; I'll carry on and do this other stuff I was going to do anyway."&lt;/p&gt;

&lt;p&gt;The three &lt;em&gt;then&lt;/em&gt;'s near the end of the program make a lot more sense when you think of them as three "end-ifs" at the end of some nested if statements, or even three closing curly brackets if that's more your style.&lt;/p&gt;

&lt;p&gt;The final piece of the puzzle is some console IO:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;." fizzbuzz" cr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;." fizzbuzz"&lt;/code&gt; prints some text to the screen, equivalent to &lt;code&gt;PRINT "fizzbuzz";&lt;/code&gt;. Note, &lt;code&gt;."&lt;/code&gt; is it's own core word, so the space between that and the text to print is required.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cr&lt;/code&gt; is another core word which prints a carriage return, so &lt;code&gt;." fizzbuzz" cr&lt;/code&gt; is simply equivalent to &lt;code&gt;PRINT "fizzbuzz"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This code does not affect the stack in any way.&lt;/p&gt;

&lt;p&gt;Finally, the last piece of the puzzle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i . cr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;i&lt;/code&gt; pushes the value of &lt;code&gt;i&lt;/code&gt; to the top of the stack, then &lt;code&gt;.&lt;/code&gt; prints the top item on the stack to the screen (removing it from the stack in the process), and &lt;code&gt;cr&lt;/code&gt; as mentioned will print a carriage return.&lt;/p&gt;

&lt;p&gt;And that's the end of this gentle introduction. I'll most probably do a few more posts showing some fun stuff that can be done on the Commodore 64 in particular. For now, I didn't want to assume too much prior knowledge. Hopefully you, the reader, now have a feel for what Forth is, and what it looks like. Next time we can really take it for a spin.&lt;/p&gt;

&lt;p&gt;Some links for the curious:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jkotlinski.github.io/durexforth/" rel="noopener noreferrer"&gt;DurexForth's Operators Manual&lt;/a&gt;&lt;br&gt;
&lt;a href="https://youtu.be/TXIDqptXmiM" rel="noopener noreferrer"&gt;Jeff from Hey Birt! Installs DurexForth&lt;/a&gt;&lt;br&gt;
&lt;a href="https://youtu.be/1XdgUK1NbpI" rel="noopener noreferrer"&gt;Robin from 8-Bit Show and Tell explores 64Forth, an older version of Forth for the C64&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vibecoding</category>
    </item>
  </channel>
</rss>
