Orwell keyboard input and Microsoft Basic.

October 2nd, 2013

Well, things are progressing. After a bit of fiddling I got the keyboard to work. A few wiring issues (bloody breadboards) then a false start on the key debounce code and then I got it right.

Basically one port of the VIA is used to drive the 4051 analogue multiplexer which is used to pull the rows low one at a time. I then read the VIA input port that is connected to the columns and check if any keys are currently pressed. The columns are connected to the port via 4k7 pull up resistors. Pressing a key will pull the appropriate column low. Since we know which row is currently selected we can determine which key in the matrix is pressed.

Since the shift, function and control keys are just special keys on the matrix (as is the alpha lock) I read those first. I then check the remaining rows in turn. If more than one key (apart from those mentioned) is pressed I don’t return a valid key-press. Orwell only allows one key to be active at a time. A limitation but one keeping with the 80s design.  If a single key is pressed I then look up the ASCII value in a table. I change the offset into the table based on if any of the shift, function or control keys are pressed.

Reading the matrix itself was not too hard. What took me a bit of mucking about was code to then present what key was pressed. My test code simply sat in a loop polling for key presses then sending that key to the screen. But you need to have code in there to 1. debounce the key and 2. stop the same key triggering repeatedly.

Debouncing is necessary since any buttons contacts don’t open or close immediately. They ‘bounce’ which is where the contacts is made and broken very quickly until the key remains in a fixed state. This happens over a short time (I measured about 50uS on my keyboard) but when you’re polling it with a computer very fast if you look at the key as the bouncing is happening you can get false readings.

Oddly enough I didn’t have that problem because it seems my scanning is done in such a way (slowly!) that I was missing the bouncing but I didn’t feel comfortable leaving it to chance like that so I implemented a simple debounce routine.

It works like this.

That number was determined by experiment and is basically there to cause a delay as we waitfor the right number of consistent readings. The value above gives me a debounce time of about 40mS which is more than enough.

The second problem is how to make it so that the key is only registered as pressed once until it is released. Initially when I pressed a key the computer would spit that to the serial port as fast as it could as long as the key was held down. To get around this I  use my own status variable with a bit set as a flag to indicate there is new data available. When a key is pressed I store the ASCII value of the key into a variable and I set the data available flag in the status variable (this status variable also shows if the shift, control, function and alpha are pressed too).

The flag is reset again when the a new key (or no key) is being pressed and has been debounced.

This all seems to work really well. I basically got the code to the point where I could type on the keyboard and have the characters echo to the screen. And as my video device supports terminal like commands I can even move the cursor around and so on.

20130922_101746 Orwell as a simple terminal.

With basic I/O working I had to work out what to do next. The plan was always to use this computer to enter and run all the old Usborne book programs. So the obvious thing was to get some kind of Basic running on it. This sounds like a really daunting task but it turns out it’s already been done a number of times before. There are several versions of Basic interpreters about that can be used on a home made machine like mine.

Hunting about thought I came across this page at Pagetable.com: Create your own version of Microsoft Basic for 6502

Perfect! Microsoft Basic (yes, Bill Gates was involved!) is the version of basic used by many of the old 80s home computers. Since that’s what I am trying to build it is the perfect version of Basic to be running. The Pagetable site explains it fairly well but it’s not a ready to run solution. It needs some fiddling. All the code is there as are the tools needed to assemble it. It uses the cc65 tool chain which is actually a macro assembler as well as a C compiler and other tools.

I am not familiar with that tool and it seems rather complicated to use and all the source is presented as separate files all linked together and built using config files and bat files. It was very hard to follow what was going on with it to be honest.

Luckily someone else has already done exactly what I want to do. A chap by the name of Grant Searle has a site where he also built a minimal 6502 machine and he managed to get Microsoft Basic running on it. Grant’s site is well worth a look at if you are building your own machine. He took the Microsoft code and extracted out the OSI version of Microsoft Basic, added his own IO routines (he is using the serial port) and got it running. I believe he picked the OSI version (from 1977) because this is the version with the least reliance on the actual hardware present.

I took Grant’s code and added in my own IO routines I had working now. I did need to fiddle a bit with the config files and I am not sure I have it all right yet but I have actually managed to assemble it and burn a ROM. And on the very first try I was presented with this on the screen:

IMG_4244_1 Getting warm!

And that is the start up message with this version of Basic. So I knew I had the output part working fine. When I pressed a key though I found it repeated which was due to a bug in my IO routine for the keyboard. With that fixed I am able to continue further:

IMG_4243_1 Past the first option!

But now I get stuck. Basically I just get an error now no matter what I press and it also seem my keyboard isn’t returning some characters. I also noticed my IO port is showing some LEDs lit when nothing should be output there.

I am not entirely certain what is happening but the fact that my IO LEDs are lit shows something is writing into the IO section of the address space. That section should not be touched of course. I think somehow I am corrupting memory which would explain the weird behaviour on my keyboard to (like being able to press ‘a’ but not ‘A’ even though ‘b’ and ‘B’ work fine). I haven’t worked out yet how you specify what the upper bounds on your RAM actually is.

I did, after a bit of hunting (as searches for Microsoft Basic and OSI just return Visual Basic and OSI data model links), find a manual for this version of Basic here: http://osiweb.org/osiweb/manuals/OSI_8K_Basic_in_ROM.pdf

That tells me that SN error means syntax error (anyone familiar with Basic will recognise that!). So, good progress so far but more work to be done. I now just need to figure out how the hell this code works!

I am progressing on other fronts of the project too. I ditched the circuit drawing/board layout tool I was using and switched to Eagle which is much more widely used. It has libraries for all the parts I am using and there are a ton of tutorials on line about how to use it (which you do need since it is a bit, errr, quirky). I am busy learning that and drawing the schematic and will soon lay out my main board. it does have a size limit on the board you can generate with the free version but I should be able to work in that since I want the main board to be small and have additional board will be added later and joined by ribbons.

I am also doing well in my quest for Usborne books. I keep discovering new ones to get. So far I have these ones:


That’s 17 so far. I have another  8 on the way. And there are 3 left to find. One of those left is ridiculously expensive for some reason (How to make computer model controllers). I do have a PDF of it but that’s in Spanish. ¡Ole!

Not all of the books contain programs but they are all from the same series of books more or less. I did email Usborne in the UK to ask if they might have a complete list but I never got a reply.

I should start reviewing these soon.

Comments are closed.