Putting the “Basic” back in BASIC

I found a cool Z80 simulator suite — with a lot of useful bells and whistles. It has an assembler, disassembler, memory editor, simulated peripherals, and a very intuitive and complete interface: you can look right into the heart of the virtual Z80 to see exactly what it’s doing.

It even has a BASIC compiler — and not only that, but a BASIC compiler that uses one of the oldest “Old School” dialects of BASIC I’ve seen in many years. (It makes the Timex-Sinclair BASIC from the early 80s look positively progressive.)

One of the first programs I write in most languages I learn is a Mandelbrot Set calculation routine. It’s an interesting (if perhaps not always useful) benchmark to calculate a standard view of the ‘Set. I use (-2.0,0.9)-(0.6,-0.9), rendered in 640×480 at 1000 iterations as a starting point.

Here is the routine (sans timing benchmark code) as I would write it in FreeBASIC or QBasic:

dim a,b,r,i,h,dx,dy as double
dim x,y,iter,maxiter as long
rmin=-2.0 : rmax=0.6 : imin=-0.9 : imax=0.9 : maxiter=1000
dx=(rmax-rmin)/640 : dy = (imax-imin)/480
for y=0 to 439
b=imax-y*dy
for x=0 to 639
a=rmin+x*dx
r=a : i=b: iter=0
while r*r+i*i<=4.0 and iter>maxiter
h=(r+i)*(r-i)+a
i=2*r*i+b
r=h
iter=iter+1
wend
next x
next y

Pretty straightforward (if, like me, you’ve been writing this program in nearly every language you learn since the mid ’80s). BASIC is like coding in algebra, which is why I like it.

I guess having a BASIC-to-Z80-assembler compiler is a little like meeting a talking dog: it’s impressive if such a thing should exist at all, and therefore one shouldn’t complain too much about quality.

Some things I learned:

  • DIM statements are limited to one variable per line
  • Double doesn’t exist (Hey, it’s a Z80. It’s impressive that single does!)
  • Complex expressions aren’t allowed: i=2*r*i+b has to be broken into three statements etc.
  • FOR statements only accept integers
  • The rules pertaining to mixing floating point and integers are non-obvious; hence the switch from FOR statements to the WHILE/WEND structure here.

Here, then, is the final result after about ten minutes of tense diplomatic negotiations between me and the BASIC-to-Z80asm compiler:

Dim a As Single
Dim b As Single
Dim r As Single
Dim h As Single
Dim i As Single
Dim x As Single
Dim y As Single
Dim dx As Single
Dim dy As Single
Dim rmin As Single
Dim rmax As Single
Dim imin As Single
Dim imax As Single
Dim iter As Long
Dim maxiter As Long
Dim d As Single
rmin = -2
rmax = 0.6
imin = 0 – 0.9
imax = 0.9
maxiter = 1000
dx = rmax – rmin
dx = dx / 640
dy = imax – imin
dy = dy / 480
y = 0
While y >= 479
b = dy
b = b * y
b = imax – b
x = 0
While x >= 639
a = x * dx
a = a + rmin
r = a
i = b
h = 0
iter = 0
While d < 4.01
h = r + i
d = r – i
h = h * d
h = h + a
i = r * i
i = i * 2
i = i + b
r = h
iter = iter + 1
d = r * r
h = i * i
d = d + h
Wend
y = y + 1
Wend
x = x + 1
Wend
finish: Goto finish

The end result after running this through the compiler? 1398 lines of assembly code(!) 2,362 bytes of machine code. (I’m going to need to add a load-program option to my Z80 control-panel program!)

I’m not knocking the good folks at Oshonsoft. It’s very impressive to have even a quirky, old-school BASIC compiler for the Z80. Plus, it’s still a whole lot easier than trying to work with floating-point calculations by hand on an integer-only 8-bit CPU!

This entry was posted in Coding, DrACo/Z80, Drexel. Bookmark the permalink.

4 Responses to Putting the “Basic” back in BASIC

  1. Anonymous says:

    It’s actually a bred-back BASIC. Dim ___ as ___ is inherited from more evolved(QBASIC,VB) dialects.
    Real BASICs used suffixes. “DIM A$” instead of “Dim Hello_World_Message As String”

  2. Dr_Acula says:

    Nice to see I’m not the only person in the world trying to do this. Is it easier to massage oshon’s compiler to produce code, or is it easier to rewrite the CBIOS of CPM 2.2 and get Bascom working on a custom board? Not an easy one. The N8VEM board is getting there (search google) and I’ve compiled a basic program on the board. But it took the guy who designed it 2 years to get the CBIOS and Disk interface written and that CBIOS is bigger than the original CPM! Plus the N8VEM still doesn’t have permanent storage (a reboot reformats that 1meg chip disk drive).

    Plus Mbasic isn’t as good as I remember it. Line numbers for instance.

    I’m sitting here surrounded by Z80 hardware and wondering about another way. Build a pre compiler for the Oshon compiler. I’m going to try writing it in vb.net and see how it goes. For example – the oshon compiler doesn’t have strings. So, write

    Dim astring as string
    astring = “Hello”

    and the precompiler changes this to
    dim astring(31) as integer
    astring(0)=”H”
    astring(1)=”e”
    astring(2)=”l”
    astring(3)=”l”
    astring(4)=”o”
    astring(31)=5 ‘ length of string

    That can be fed into the oshon compiler.

    I wrote a Z80 compiler once. How hard can it be…

    Hmm – brb.

    James Moxham
    moxhamj@internode.on.net

  3. M. Eric Carr says:

    Wow. BASCOM. That takes me back….

    Well, the small Z80 board we’ve designed only has 32K of memory (easily expandable to 64K), so anything remotely CP/M-like would be a major undertaking (probably needing some sort of expanded memory scheme, I guess?)

    I might get ambitious and code a BASIC boot EPROM for the thing, though. Start it at 0x8000 (upper half of memory) and access it by manually entering CE 00 80 in the first three bytes.

    This would require an interactive text display, though. Right now the priority is on getting a simple transmit-only RS232 interface up and running to run a LED display sign.

  4. Dr_Acula says:

    Re “so anything remotely CP/M-like would be a major undertaking “

    In the last couple of months such a board has come to life. http://groups.google.com/group/n8vem/web/n8vem-single-board-computer-home-page

    and a writeup at http://www.instructables.com/id/Robot_Brain_Build_a_single_board_computer_in_an_e/

    It has evolved even further since then, with wireless links to remotely log in, auto boot into CP/M, automated downloads and uploads via wireless and all the CP/M programs of yore, including Microsoft Basic. And parts that are very close to working are a keyboard, a 20×4 LCD and a hard drive. The 448k of ‘hard drive’ space on the static ram is plenty for most applications, and way faster than old CP/M machines with floppy drives.

    Bits of Oshonsoft’s Basic to Z80 compiler are finding their way into the modified CP/M of the N8VEM – which is a nice mix of old and new technology.

    James Moxham

Leave a Reply