Orwell REALLY running Basic.

October 28th, 2013

Finally, after weeks of painful debugging, I worked out why Basic wasn’t running. It seems in the OSI code there is a bug in the control-c handler. If you look in the original PageTable.com code for MS Basic you’ll see it in the osi_iscntc.s file you see:

.segment “CODE”
lsr a
bcc RET2
cmp #$03
;!!! *used*to* run into “STOP”

The important bit is at the end. That’s exactly what was happening in my code. It checked for control-c but then ran into the stop code no matter if it was pressed or not!

So what was happening was as soon as I types LIST or RUN it checked for control-c and immediately stopped.

Now if I had seen that original code earlier it would have saved me a lot of time. I started with the code from another site though and I haven’t quite worked out how it is working. I changed mine to make it much easier to see what’s happening and now it works fine.

So I was finally able to run my first Basic programs!

IMG_4345_1 Basic running.

So I am now slowly modifying the code to work specifically for my machine. Since my keyboard matrix scanning code is making use of a VIA but it isn’t using all of port B on it I am using the free pin 7 on that port to drive a piezo speaker. Pin 7 is connected internally to a timer running in the VIA. The timer is controlled by writing to two bytes (upper and lower for a 16 bit value) into a latch on the VIA. This value is what the timer will count down from. You can set the VIA up so that it will toggle the output on pin 7 when the timer times out. It then automatically starts counting down again.

Basically what this means is you can create a periodic signal on this pin just by loading values into the latches for the timer. With a simple piezo on this pin you can make (annoying) noises!

To do this I just POKE values directly into memory locations 20484 and 20485 to set the value. I was having great fun last night writing Basic programs to make silly noises and flash the IO LEDs. I remember when I was ten years old one of the first things I did on my VIC 20 computer when I got it was getting it to make sounds typing in the code that came in the manual.

It was really a relief to finally work out what was wrong but it took me a long time to get there. The journey wasn’t without merit though as I now know a lot more about how Microsoft Basic works. I compared my code to various other version of the code about (including Applesoft Basic). I found these two documents that also helped:



I also used an OSI machine emulator so I could see what happens in memory and eventually wrote my own routine for doing a hex dump of various locations in RAM and the input buffer. The way the code parses each line is interesting. The line is read into the input buffer. It then converts the commands into tokens. It also adds in a pointer to the next instruction. All this is done in the input buffer which is then inserted into the appropriate place in system memory based on line number.

Along the way trying to learn this stuff I have been commenting the code as I go. The AppleSoft Basic disassembly here helped greatly.

I also fixed the garbage collector bug that OSI Basic is famous for. It had an issue cleaning up after strings and eventually it would lock the machine up. On osiweb.org I found a fixed version of the ROM with the garbage collector bug fixed. I took the Fixed BASIC3 ROM and patched in the listing 4 version of the fix into my own code. I had to add back in all the variable names so it matched the original source.

To test it you run the following Basic program:

10 A$=”+”
20 A$=”-“+A$+”-”
30 A$=”+”+A$+”+”
50 GOTO 20

In the broken original version this runs for a bit then the machine locks up requiring a reset. The patched version seems to just run forever (well, as long as I left it).

I would also like to add in some screen commands if possible. So backspace (actually they use _ ) really does remove characters off the screen for example. With my display supporting terminal commands that should actually be possible.

I also need to work on the Eagle board layout. I am sure when it gets to the stage of actually getting the board made it will take weeks and I’ll be wishing I had done it sooner!

Finally I can start reviewing my 1980s Usborne books. I now have quite a collection as you can see.

IMG_4357_1 1980s Usborne books.

I have three more in the post and three left to get. For some reason these three, ‘Computer spy games’, ‘Island of secrets’ and ‘How to make computer model controllers’ are expensive so I am waiting to find cheaper copies.

Two of the books shows above are actually compilations of  other books. ‘The beginners programming handbook (in Basic and machine code)’ is ‘Machine code for beginners’, ‘ Practice you Basic’ and ‘Guide to better Basic’ combined.

And ”Practical Micro Book’ is ‘Practical things to do with a microcomputer’, Experiments with your computer’ and ‘Expanding your micro’ combined.

Not all the books have Basic programs in them but most do. Now I have a working machine I can start my reviews! Some even have electronics projects and I actually based my first robot (controlled by an Apple 2e) on one of the robots in these books back when I was 15.

Finally here is a small film I made very late at night when I finally got things all working.


Comments are closed.