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!

Posted in Coding, DrACo/Z80, Drexel | 4 Comments

9 MHz!

More progress on the Z80 computer:

* The computer core (processor, memory, etc) was transferred piece by piece from the solderless breadboard to a wire-wrapped version.
* Termination of the data and address busses was added (1K resistors to ground)
* A 555-based internal clock was added; this will allow execution at speeds up to 1MHz.
* The virtual-control-panel circuit was pared down; it now consists of a PIC16F877A, a MAX232 chip, and a few resistors and capacitors. It’s still on solderless breadboard at this point; a more permanent control panel is planned, using three microcontrollers and having a lot more functionality.

After verifying that it all still worked, I decided to see how fast it would go (the idea being that if it ran well at speed, it should be very reliable at the slow speeds we’ll be using in class next term.) I connected it up to a signal generator and a mixed-signal Agilent scope. It proved to be stable at up to slightly more than 9MHz clock speed (18MHz into the J/K flip-flop).

Here’s a picture of the system in action.

Here is a trace of the system running a "Fibonacci" program. Execution starts at address 0x000A, with a JMP 0x0003 instruction. (The analog trace at the top is address line 12 — which goes high whenever the Z80 writes to memory location 0x1234. The reason that the data for this location is ambiguous is that, unlike the other steps in the program, this data is constantly changing.)

Posted in Digital, DrACo/Z80, Drexel | Leave a comment

It works!

After a bit of debugging (some PIC code corrections, some VB code corrections, and finally tracking down a missing direction-pin wire), the Z80 computer is executing code! I wrote a short test program to compute Fibonacci numbers, and it ran correctly.

It still could use quite a few features — such as an internal clear-all-memory routine, program save and load functionality, program trace output, etc. But for now, it’s a working Z80 computer, accessed via RS232.

Posted in Digital, DrACo/Z80, Drexel | Leave a comment

Plus ça change…

…plus ça ne change pas. Or so they say, anyway — but the design of this computer has certainly changed. The changes are for the better, though: the core (which will be constructed by the students) has been greatly simplified, with as much functionality as possible having been collected into a control panel unit.

Here is the schematic for the “core.” Some more minor changes may yet happen (perhaps another 74LS245 for the control lines etc), but the core design is essentially finalized. The idea is that the core system can run as a “headless” unit, without a control panel: with the addition of 24 LEDs to show the status of the address and data lines, it should look like a real “Hollywood” computer, complete with blinking lights! I’m still skeptical about putting LEDs directly onto the busses, but we’ll see how that works out. If nothing else, they can be driven by three more ‘245s.

The control panel (still under construction) will include:

  • Rotary hex switches to enter addresses and data;
  • An LCD panel to read addresses and data (and perhaps other information);
  • Run/Stop, Single-Step, Manual/Auto, Reset, and Write switches;
  • An SD card slot and Load/Save switches to back up programs to an SD card; and
  • Three PIC microcontrollers to run all of this.

Right now, I’m creating a set of inter-MPU commands, to keep everything in sync. Complete details will be available here once it’s all finished.

Posted in Digital, DrACo/Z80, Drexel, PIC Microcontrollers | Leave a comment