TweetFollow Us on Twitter

basic Cons BASIC

Volume Number: 20 (2004)
Issue Number: 6
Column Tag: Programming

Programming Techniques

by F.C. Kuechmann

basic Cons BASIC

Using a Macintosh to program little control computers in BASIC

History Lesson

Back in the prehistoric days of desktop computerdom, the late 1970s and early 1980s, the BASIC interpreter seemed ubiquitous. The original Apple ][ came with a fast integer BASIC, which was displaced by the Microsoft variant, Applesoft. Commodore and Atari also shipped their 6502-based computers with Microsoft interpreter variants. On the IBM-Intel-Microsoft side of the tracks, MS-DOS shipped with GW-BASIC (eventually replaced by the QB interpreter, which copied its core technology from BASIC09 by Microware of Des Moines). Although differing in detail, these early interpreters had many things in common - including line numbers and all-global variables.

Although this type of language has long since been superceded for desktop programming, there is a significant market in small embedded controllers running BASIC11 (Motorola M68HC11 processors) and BASIC-52 (Intel 8052 family). Initial purchase costs are low and development rapid. . Controllers are available in a variety of configurations from companies like Micromint and New Micros. Using a USB-to-serial adapter or serial port PCI card, these boards are easy to program and run with modern Macintoshes under OS-X and offer an easy route to hardware fiddling.

While some folks may think interpreted BASIC is a toy language with no serious applications, that's far from the case. Micromint, for example, has been selling interpreted BASIC controllers in industrial markets for nearly two decades. Several years ago I implemented the control section of a gate and barrier system used in fish hatcheries -- based on Micromint 8052 boards programmed with ~25k of interpreted BASIC-52 code.

Figure 1 shows a typical small controller that runs BASIC, the Motorola M68HC11-based Micromint RTC-HC11. Figure 2 shows a member of the New Micros NMIT/X-020 family. It too runs the BASIC11 interpreter. Both boards are shown "bare", without the stacked I/O cards included in a typical installation. Details of these boards as well as the companies' other BASIC offerings can be obtained by contacting the companies at the addresses at the end of this article.

Now, before you check the cover to be sure you're reading a Macintosh magazine,, we'll get to where the Mac comes in shortly.

Short programs are easy to create in unstructured BASIC, but problems appear when line numbers and all-global variables are combined with large programs. In order to create maintainable programs I adopted some assembly language techniques in writing a desktop computer program to convert unnumbered source text to numbered BASIC. In the source text, variable names are managed via an equate table, making it easy to avoid global variable problems, and branch destinations are labels. The source text can use meaningful, i.e. long, variable names, while the final code uses one and two characters.


Figure 1. Micromint RTC-HC11

My first conversion program was written in structured Power BASIC under DOS in the 1980s. It was later translated to Borland Pascal before being ported to Mac Pascal. The most recent versions are in CodeWarrior Pascal and REALbasic 5.5, both carbonized and running under OS-X. In this article I'll focus on the REALbasic variant, hence the name basic Cons BASIC.


Figure 2. New Micros NMIT/X-020


Figure 3. Screenshot following conversion

Figure 3 is a screenshot taken following conversion of a short program called blinky, which will be discussed further below. Three black rectangles upper right are REALbasic EditField Controls that display the time conversion started in 24-hour format, the time it finished, and the elapsed time between, in this case three seconds. Below that are EditField Controls showing lines in and out for the most recent pass through the files. Since the final pass checks the bas file for unconverted labels, the out figure is zero.

The big black rectangle with the scrollbar is a REALbasic ListBox Control holding the output of basicConsBASIC's three read-write passes starting with the source file. From a source file named blinky.src, basicConsBASIC creates the files blinky.par, blinky.num, blinky.bas and blinky.dmp. The par and num files are scratch files, bas is the file you load into your controller, and the dmp file is the saved contents of the list box.

At the bottom is sixth EditField Control that functions as a message window.

A short example that follows illustrates the programming approach. Complete sourcefile instructions can be found in the file basicConsBASIC sourcefiles.doc.

The following program toggles the onboard L.E.D. on a Micromint RTC-HC11 once each second for an hour. Listing 1 shows the contents of the source file, created with an ordinary ASCII editor such as BBEdit or the CodeWarrior IDE's editor. The underlying syntax is that of BASIC11, and the hardware addresses specific to the RTC-HC11.

blinky.src
|rem
|rem blinky program
|rem
%bit5 .equ $20
%index .equ x
%limit .equ lm
%blinkaddr .equ $b009
poke(%blinkaddr,%bit5)
%limit=3600   : 'set to blink 3600 secs [1 hr]
%index=0
gosub &blinkit
end
|rem
|rem
      ' skip numbering to line 500
500:
|rem
         ' toggle the state of the l.e.d.
         ' each second using the time function
blinkit:
while %index<%limit
 portd=portd.eor.%bit5 : ' exclusive-or to toggle
 time=0
 while time<1
 endwh
 %index=%index+1
endwh
            ' skip to 900 to rts
900:
return
|rem
|rem

Listing 1. blinky.src

Listing 2 shows the contents of the file blinky.bas that is created from blinky.src by the program basicConsBASIC

blinky.bas
100 rem
110 rem blinky program
120 rem
130 poke($b009,$20)
140 lm=3600     
150 x=0
160 gosub 510
170 end
180 rem
190 rem
500 rem
510 while x<lm
520  portd=portd.eor.$20
530  time=0
540  while time<1
550  endwh
560  x=x+1
570 endwh
900 return
910 rem
920 rem

Listing 2. blinky.bas

The converted program is loaded into the controller using a terminal program like ZTerm. After testing and debugging , the BASIC program can, in many instances, be easily stored in EPROM or EEPROM or non-volatile (battery-backed) RAM. Micromint's BCC-52 series controllers can burn programs into EPROM, while some New Micros M68HC11 boards are easily fitted with EEPROM storage.

The Macintosh program

The program basicConsBASIC is a relatively simple text parser that uses the BASIC language's excellent string manipulation capabilities. On the initial pass through the source file the contents of the equate table are saved in two global string arrays. Array gEQlabel() holds the labels and gEQsub() holds the replacement strings. For branches (GOTO and GOSUB), the array gBranchLabl() holds the labels and gLineNumStr() stores the line numbers that replace the labels. On the first pass, through the src file, the appropriate strings are assigned to the arrays; on the next pass (par file) the BASIC program lines are searched for strings in the gEQlabel() and gBranchLabl () arrays. Any that are found are replaced by the corresponding (same array index) strings in gEQsub() and gLineNumStr().

Listing 3 shows the method that processes the equate table.

Listing 3.

ProcessEqu
Sub ProcessEqu(byref textline as string)
  dim tempProgLine, equLabel, equsub As string
  dim squotePos, equPos, howlong As integer
  
  squotePos = instr(textline, gkSquote)
  if squotePos > 0 then
      // chop off comment
    tempProgLine = Left(textline, squotePos - 1) 
  else
    tempProgLine = textline
  end if
  equPos = instr(tempProgLine, ".EQU") 
  Dec (equPos)
   // get left part of .equ line
  equLabel = Left(tempProgLine, equPos)  
  StripCon (equLabel)
  gEQlabel (gEqIx) = Trim (equLabel)
   // then get assignment text
  howlong = Len(tempProgLine)     
  howlong =  howlong - (equPos + 4)
  equsub = Right(tempProgLine, howlong)
  // strip control chars
  StripCon(equsub)            
   // assign to swap array
  gEQsub(gEqIx) = Trim (equsub) 
  Inc (gEqIx)
   // make line unnumbered
  textline = "~" + " " + textline 
End Sub

basicConsBASIC is derived from an earlier Pascal program called, oddly enough, PascalConsBASIC. The languages are syntactically and structurally so similar that converting Pascal to REALbasic consisted mainly of pasting the code from each Pascal procedure or function into a REALbasic method, then making relatively minor changes, most of which can be accomplished with global search and destroy sequences. All instances of ":=" change to "=". "end;" becomes "end", and "begin" is simply removed. "Case x of" mutates to "Select case x".

One of the biggest differences between the initial Pascal program and the REALbasic version lies in the main program loop. In Pascal the loop is visible and explicitly created by the programmer; in REALbasic the loop is implicit and hidden. To illustrate let's assume we're making a program whose actions are controlled by buttons labeled This, That and Quit. A Pascal main loop might book something like Listing 4.

Listing 4.

Pascal main loop
Repeat
   If ThisButtonWasClicked() then
      DoThis()
   Else if ThatButtonWasClicked() then
      DoThat()
   Else if QuitButtonWasClicked() then
      Exitflag:=true;
Until exitflag = true;

In REALbasic, the Action methods of the three buttons call the DoThis, DoThat and Quit methods, respectively. Like the Wizard of Oz, the little man behind the curtain keeps an eye on the buttons so the programmer doesn't have to. See Listings 5 through 7.

Listing 5

ThisButton.Action
Sub ThisButton.Action()
   DoThis()
End Sub

Listing 6

ThatButton.Action
Sub ThatButton.Action()
   DoThat()
End Sub

Listing 7

QuitButton.Action
Sub QuitButton.Action()
   Quit ()
End Sub

The program basicConsBASIC Convert button's Action method calls the method Main in Listing 8.

Listing 8

Main
Sub Main ()
   dim sourcefile As folderItem
  
   DoInit ()
  sourcefile = GetOpenFolderItem("TEXT")
  if sourcefile <> nil then
    ProcessSource (sourcefile)
  end if
end sub

If the folderItem returned by REALbasic's GetOpenFolderItem method is valid, the ProcessSource method is called.

Listing 9

wBASIC.ProcessSource
Sub ProcessSource(sourcefile As folderItem)
  dim fname, conbaspath, outpath, ext As string
  dim timeOut, dateStr, timeStr, lapstr As string
  dim btimeOut,bdateStr, btimeStr, mess As string
  dim infile, outfile, conbasFolder as FolderItem
  dim flag1, flag2 As boolean
  dim instrm As TextInputStream
  dim outstrm As TextOutputStream
  dim index, begsecs, finsecs, lapsecs As integer
  
  // where are  we? get current dir 
  conbasFolder = GetFolderItem("") 
  conbaspath = conbasFolder.AbsolutePath
  fname = sourcefile.Name
  //  get date/time
  btimeStr = CurrentTimeString( )
  bdateStr = CurrentDateString( )
  mess = "Converting sourcefile :  " + fname
  fname = StriptExtention(fname)
  // open the dmp file
   outpath = conbaspath + fname + ".dmp"
  flag1 = DumpFileOpen (outpath)
  flag1 = false
  index = 1 
  do
    select case index
    case 1
      infile = sourcefile
      outfile = nil
      instrm = infile.OpenAsTextFile
      if instrm <> nil then
        outpath = conbaspath + fname 
        ext = ".par"
        flag2 = MakeOutFile (outpath,ext,outstrm)
        if flag2 then
          // handle err
          exit
        end if
        flag1 = MakeParFile(instrm, outstrm)
        flag2 = CloseFiles (instrm, outstrm)
      else
        // handle err
        flag2 = true
      end if
    case 2
      infile = GetFolderItem (outpath + ext)
      if infile = nil then
        exit
      end if
      instrm = infile.OpenAsTextFile
      if instrm <> nil then
        outpath = conbaspath + fname 
        ext = ".num"
        flag2 = MakeOutFile (outpath,ext,outstrm)
        if flag2 then
          // handle err
          exit
        end if
        flag1 = MakeNumFile(instrm, outstrm)
        flag2 = CloseFiles (instrm, outstrm)
      else
        // handle err
        flag2 = true
      end if
    case 3
      infile = GetFolderItem (outpath + ext)
      if infile = nil then
        exit
      end if
      instrm = infile.OpenAsTextFile
      if instrm <> nil then
        outpath = conbaspath + fname 
        ext = ".bas"
        flag1 = MakeOutFile(outpath,ext,outstrm)
        if flag1 then
          exit
        end if
        flag1 = MakeBasFile(instrm, outstrm)
        flag2 = CloseFiles (instrm, outstrm)
      else
        flag2 = true
      end if
    case 4
      infile = GetFolderItem (outpath + ext)
      if infile = nil then
        exit
      end if
      instrm = infile.OpenAsTextFile
      if instrm <> nil then
        flag1 = CheckBasFile (instrm)
        instrm.Close
      end if
    end // select
    Inc (index)
  loop until (index  > 4) or flag1 or flag2
  
  
  timeStr = CurrentTimeString( )
  UpdateBegTime (btimestr)
  UpdateFinTime  (timeStr)
  begsecs = TimeStringToSecs (btimestr)
  finsecs = TimeStringToSecs (timeStr)
  lapsecs = finsecs - begsecs
  lapstr = SecsToTimeString (lapsecs)
  UpdateLapsedTime (lapstr)
  
  if gDumpFileOpenFlag then
    dateStr = CurrentDateString( )
    WriteLineToDumpFile (mess)
    mess = "Beg : " + bdateStr + " " + btimestr
    WriteLineToDumpFile (mess)
    mess = "Fin : " + datestr + " " + timestr
    WriteLineToDumpFile (mess)
    WriteLineToDumpFile ("")
    WriteLineToDumpFile ("")
    mess = "Elapsed Time :  " + lapstr
    WriteLineToDumpFile (mess)
    WriteLineToDumpFile ("")
    WriteLineToDumpFile ("")
    WriteLineToDumpFile ("")
    WriteLineToDumpFile ("")
    gDumpStream.close
    gDumpFileOpenFlag = false
  end if

End Sub

Listing 9 shows the case statement inside a four-pass do loop that creates a new file in each pass but the fourth, in each case but the first taking input from the output file of the previous pass.

Hooking up

Once your program is written, you need to get it into the controller. With modern Macs that means a serial port must be added either with a USB-to-serial converter or a PCI bus card with serial ports. Keyspan is one source for both types. The internet auction sites such as ebay commonly offer adapters from various sources. Additional suitable cables and/or adapters and software drivers may also be required, exact needs determined by the controller being used. The recent article by Tom Djajadiningrat in MacTech 20:2 has a very good discussion of Mac serial issues.

If you're using a Keyspan USA-28X USB-to-serial adapter or a Keyspan SX Pro PCI serial card and your controller is a Micromint BCC-52, you'll need an old style Mac modem cable with a DB-25 male connector at one end and a mini-DIN8 male at the other. TrippLite is one source for this type of cable. If your controller is a New Micros 68HC11-based model supplied with an RS-232 cable designed for the WinTel set, a bit of custom adapting may be needed. RS-232 interfacing is something of a black art whose practitioners require familiarity with breakout boxes, logic probes, soldering irons and strong coffee.

The shareware program ZTerm is probably the simplest software to use to download a converted program to the controller. Set the parms to 8N1 and 9600 Baud, then select local from the Dial menu. Hit "Enter" and you should get a prompt. BASIC11 uses a "splat" (#) prompt; BASIC-52 uses ">". Use the Settings : Text Pacing menu to set character and line delays and the BASIC prompt for smooth transfers. To initiate the transfer, select Send text from ZTerm's File menu, then choose the file, e.g. blinky.bas in the navigation dialog.

Sources

ZTerm - http://homepage.mac.com/dalverson/zterm/

New Micros Incorporated - http://www.newmicros.com/

Micromint - http://www.micromint.com/


F.C. Kuechmann is a programmer, hardware designer and coffee drinker who once lived in Chicago and has lived in Vancouver-NOT-CANADA Washington for many years. He can be contacted at bosedenage@earthlink.net

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.