Friday, 6 September 2013

Arduino 80x25 TV Video Output Library

This is an old post, preserved for reference.
The products and services mentioned within are no longer available.

[Update: Please see the new version which supports multiple fonts and 'medium resolution' graphics]

Arduino TV Out has been around for a while, but you can't get many characters on the screen and uses a lot of the Arduino's resource. This is a library which will let you generate an 80x25 text screen on a TV or monitor from an Ardiuno. Well I say from an Arduino, it's more like two Arduinos, or to be precise, an Arduino and a second ATMega328P as a video processor, and one additional TTL chip.
The video processing work is all done by the second ATMega328P, the video processor. This leaves the main processor (the ATMega328P on the Arduino) to do whatever you like. There is a simple 8 bit data transfer whenever a new character is to be added to the display, in a similar way to driving an LCD display. The concept comes from Grant Searles Monitor and Keyboard interface, itself an update of code by Daryl Rictor (40x25 display using an ATmega8). The code in the second chip is unmodified from Grant's (download from his page above). I've completely rewritten the Arduino code, now in the form of a library to make it easier to use.

           Shift Register --> Char Data
               /                   \ 
Arduino <==> Video Processor        + ==> Video
               \                   /
                   Composite Sync

The idea is basically this, the Arduino (the first ATMega328P) has an 8 bit output port and two control lines. This interfaces to the video processor (the second ATMega328P). This has an 8 bit output port and 1 control line which is fed to a 74LS165 or 74HCT166 shift register (the pinouts are different, but they can both provide the necessary functionality, you may have more luck finding the 165) [Update: the 166 is preferable, and the HCT version should be used if possible]. The output of the shift register is the character video output data. There is also a composite sync is output from video processor. The composite sync and video data are combined via two resistors and this forms the composite video output. This composite video output is suitable to drive a modern LCD TV or an old CRT TV or monitor.
OK, so how do you do it? Well first you need to program the second microcontroller. This is written in assembler and does not use the Arduino bootloader, so needs to be uploaded via a programmer. There are many options, use an external programmer, use something like the USBTiny ISP to program in circuit, or use the Ardunio as an ISP.
If using an external programmer, such as the MiniPro, select ATMega328P, set the fuses to E6 D9 FF and select Grant's SBCVideo.hex file.
Alternatively, since there is already going to be an Arduino and a second ATMega328P, Arduino as ISP can be used. Wire up the circuit as per the Arduino as ISP demo (leave some space for the 74LS165). Here is an excellent tutorial on Arduino as ISP, and more info on avrdude.
Once built up, you can test it in the Arduino environment by selecting Arduino As ISP from the Programmer menu, As a test, load up the 'blink' example and use 'Upload using Programmer' to upload it. If all is well, the LED on digital pin 13 will blink. This is running from the second ATMega328P (their pin 13's are connected together). You can easily test this if you remove the wire and see it stop flashing. If it keeps flashing, you've reporgrammed the Arduino instead, try again. The fuses need to be set, and a hex file uploaded. This cannot be done from the Arduino environment, so you need to use avrdude on the command line to program the device. The commands required are as follows (change the com port as necessary)

  1. avrdude -P COM3 -b 19200 -c avrisp -p m328p -n
  2. avrdude -P COM3 -b 19200 -c avrisp -p m328p -U lfuse:w:0xe6:m
  3. avrdude -P COM3 -b 19200 -c avrisp -p m328p -U hfuse:w:0xd9:m
  4. avrdude -P COM3 -b 19200 -c avrisp -p m328p -U efuse:w:0xff:m
  5. avrdude -P COM3 -b 19200 -c avrisp -p m328p -U flash:w:SBCVideo.hex

Once the video processor is programmed, wire it up as per the circuit diagram. The pins on the video processor should remain the same (unless you want to modify the source). The pins on the Arduino end can be any. I chose the nearest ones. The only exception was choosing digital pin 13 as the Ack pin. This just shows activity on the LED on the Arduino for diagnostic purposes. The pin connections are:

  • Video Data In 0: Arduino D12 - ATMega328P pin 15
  • Video Data In 1: Arduino D11 - ATMega328P pin 16
  • Video Data In 2: Arduino D10 - ATMega328P pin 17
  • Video Data In 3: Arduino D9 - ATMega328P pin 18
  • Video Data In 4: Arduino D8 - ATMega328P pin 19 
  • Video Data In 5: Arduino D7 - ATMega328P pin 23 
  • Video Data In 6: Arduino D6 - ATMega328P pin 24 
  • Video Data In 7: Arduino D5 - ATMega328P pin 25
  • Video Available: Arduino D4 - ATMega328P pin 26 
  • Video Ack: Arduino D13 - ATMega328P pin 27
  • Reset: Arduino Reset - ATMega328P pin 1
  • 16 MHz Crystal to ATMega328P pins 9 and 10
  • 22pf capacitors from ATMega328P pins 9 and 10 to GND
  • Video Data Out 0: ATMega328P pin 2 - 74LS165 pin 11
  • Video Data Out 1: ATMega328P pin 3 - 74LS165 pin 12
  • Video Data Out 2: ATMega328P pin 4 - 74LS165 pin 13
  • Video Data Out 3: ATMega328P pin 5 - 74LS165 pin 14
  • Video Data Out 4: ATMega328P pin 6 - 74LS165 pin 3
  • Video Data Out 5: ATMega328P pin 11 - 74LS165 pin 4
  • Video Data Out 6: ATMega328P pin 12 - 74LS165 pin 5
  • Video Data Out 7: ATMega328P pin 13 - 74LS165 pin 6
  • Shift Register Load: ATMega328P pin 28 - 74LS165 pin 1
  • Shift Register Clock 1: ATMega328P pin 10 - 74LS165 pin 2
  • Shift Register Clock 2: GND - Shift Register pin 15
  • Shift Register Serial Load: GND - Shift Register 10
  • Composite Sync Out: ATMega328P pin 14 via 1K to Composite Video Out
  • Video Out: 74LS165 pin 9 via 470R resistor to Composite Video Out

The breadboard wiring is as follows:
It should probably look neater than this.
Once that is built, you can get on with the coding. Download the Terminal library, unzip it and copy the 'terminal' folder into the library folder in your Arduino workspace. You should then be able to include the terminal.h file in your projects.
There are a number of examples provided, these show using the library to generate things like a BBC Micro startup screen.
Or from a Commodore Pet  (notice different cursor options are available):
Since the work is now being done by the video processor, it leaves most of the Arduino free to do something more useful. For example, with a slight modification to Mike Field's Tiny BASIC, it can use the terminal as an output.
The input is from the serial terminal. It is left as an exercise for the reader to provide a keyboard of some sort. The I2C, and half of the input pins are still available, and SPI could be freed up so there are lots of choices.
Since the sync and video are available separately, it can even drive something odd like an Amstrad GT 65 green screen monitor which has separate sync and video inputs
The sync and video are wired to the monitor inputs directly. The the resistors can be removed if the composite video is not required.
The whole reason for doing all this is to drive a Commodore Pet monitor, as part of my Pet Project.
However, that turned out to be a lot more complicated. More on that later....

Wednesday, 4 September 2013

Commodore PET Project Part 1 - History

This is an old post, preserved for reference.
The products and services mentioned within are no longer available.

This is the first in a series of articles covering a project involving a Commodore PET. This PET has quite a life story, this first part covers how I what I did with it 10-12 years ago. The next articles will cover recent developments. Here it is, as it was in 2009 (ignore what's on the screen, more on that later).
The unit started life in the late 1970's as a 4032-32N. So 40 column display, 32K memory. 1MHz 6502 Processor. This one has a 12" green screen monochrome CRT monitor. Earlier models had 9" displays, certainly the 40 column versions. I think the 80 column version (8032) always had 12" monitors.
I'm calling it a PET (Personal Electronic Transactor), but I think that only refers to the original 2001 with the chiclet keyboard and built in tape player. The label on the back just says 4032-32N. The label on the front says CBM, but I have always and will probably continue to refer to it as a PET or Pet. It's probably more correctly a CBM - Commodore Business Machine(S), but I think I'll stick with PET.
The black name place has been replaced with a custom one from the original suppliers (Currie and Maughan of Gateshead), but unfortunately this has become damaged (the Tux badge is hiding where it is damaged). There is still a Maughan Microcomputers in the Metro Centre, I wonder if they are related? The normal 4032 has a black label. I may print a normal black CBM 4032 label and stick it over.
Anyway, at some point in the 90's, it died. The screen filled up with random characters - a common fault in PETs (the screen below is a 'reconstruction' of this sort of problem). At the time, a friend blamed the power supply (probably incorrectly) and 'fixed' it by removing the massive transformer and fitting a switch mode supply. That didn't fix the problem, so they took the board away to investigate further. Unfortunately, I lost touch and I never saw the board again.
So at this point (the late 1990s), I was left with the PET, with the keyboard and monitor intact, but without any of the internals. I tried to source a PET motherboard at the time, but failed. I wanted to keep it, but an empty shell wasn't much use. I decided to use what I had available, and installed a PC motherboard. There was plenty of space for a full size AT motherboard - no, I didn't forget the X, this wasn't ATX, but the older AT standard, before the standard rectangular cutout with all the connectors on. No USB, no onboard sound or video, not even a mouse port! However, it was a functional PC, but there were two main problems to solve, the display and the keyboard.
Firstly, the display. The PET display is a 12" green screen monitor with separate horizontal sync, vertical sync and video input. These are all at 5V logic levels. I'm not sure how I managed as I didn't have a scope at the time, but I found a PC video card capable of driving a monitor at a suitable resolution (it was the old Hercules spec) and built a little board to invert one or both of the sync signals to drive the display.
The video card is the one at the back, and the board on the right has the 74LS86 that inverts and buffers the sync signals. That actually worked quite well, apart from the 'enegry star' logo on the top right, it sort of works, but was sufficient for the purpose.
Next, the keyboard. The PET has an 8x10 keyboard matrix, and the chosen motherboard had the old 5 pin DIN keyboard connector (AT style). I adapted an old PC keyboard to drive this matrix, with an adapter board to interface between the 20 pin Pet keyboard and the PC Keyboard controller board. It was a partial success, as the matrix was different, so I needed to remap them in software at the PC side. Some weren't mapped by the controller at the keyboard end, so weren't sent through, but most of the keys worked. 
I fitted what would have been about a 5 year old motherboard at the time, a Pentium 120, with 32MB RAM, two network cards and a floppy drive. As it wasn't doing much, it ran fanless, underclocked to 75MHz. Note the wonderful original car bonet style prop for holding the case open!
The task it had was running some software called 'FreeSCO', (Free Cisco). This was a tiny linux distribution which booted from a floppy disk and basically does what you average home router does these days. It took the network from my cable modem on one network card and made this available to a number of PCs plugged into the other network. At the time, you could only have one PC plugged in at a time and needed to reset the router to change to a different PC. If I remember correctly, I had 128kbit broadband at the time, so this was more that capable. The floppy drive was accessible via a slot on the side intended for access to memory upgrades. I added a switch to turn off the screen most of the time and there it sat as a router for many years.
It was only when I moved back to Tynemouth in 2004 that I got ADSL broadband and replaced the PET encased router with a considerably smaller Netgear DG834. The PET then sat patiently waiting a new task. I had tried on a number of occasions to give it a new purpose in life, but the monitor was always the problem. The only thing I had that would drive it was an ISA video card, and any modern PC I put in there wouldn't support it. I looked around for quite a while for a 12" colour monitor that I could fit in the top casing, but couldn't find one.
I did spend quite a while trying to get it running from a BBC Micro motherboard, I got a model B with a tatty marked case and thought it would be a good match, The BBC and the PET use the same 6845 video controller, so I presumed it would drive it fine, however I just couldn't get it to sync up. I now know this is due to the standard video signal that the BBC outputs not being in the tight range the Pet monitor accepts. I also realised how lucky I'd been with the PC video card.
The CMOS battery had gone bad on the motherboard, so I removed the whole thing as I couldn't see it being used again. The Pet was once more empty. So there it sat, waiting for a PET motherboard or a new purpose in life.
Read about it's exciting revival in next week's thrilling instalment of The Pet Project!

Update: This Pet has now been fully restored

2022 Update: This PET still sits on my desk, although it now has a Mini PET 40/80D board installed. 

Saturday, 31 August 2013

Acorn Archimedes A3000 Repair

This is an old post, preserved for reference.
The products and services mentioned within are no longer available.

A recent video by Dave Jones on his EEVBlog showed a tear down of an Acorn A3000 (I think they had officially dropped the Archimedes name at this point, but it is still sometimes referred to as an Archimedes 3000 or Archimedes A3000). The one Dave had suffered from what is sadly a common fault on this range of machines (and things like the Commodore Amiga A501), that of battery leaking and damaging the boards. Knowing this often wrote off the boards, I had been looking for a cheap A3000 that looked good externally, but where the main board was shot. My plan was to 'upgrade' the ARM2 processor to the ARM11 of a Raspberry Pi. I was going to convert the keyboard (like my other USB keyboards) and run RISC OS on it, so externally, it would look like an A3000, just inside would be the pi rather than the A3000.
Looks ok, doesn't it? Many of these had school names etched into the top of them. This is also the A3000 which is the last model to have BBC style red function keys. The later A3010 and A3020 had green function keys and no reference to the BBC heritage.
Again, the inside loosk OK. The expansion cover is missing, there is no econet module or serial upgrade. There is however a 3MB upgrade (taking the memory to 4MB in total). The horrors lurk under the keyboard - those of a nervous disposition look away now...
Over the 20-25 years since these machines were manufactured, the batteries have leaked an alkali solution over the area of the board around them. In the case of the A3000, that is the RTC / CMOS RAM and the 4 ROM chips (RISO OS runs from RAM, so no need for an OS disk like the Amiga).
The alkali can eat away at the copper, and indeed both the legs of the 32.768KHz crystal had gone, as had one leg of a capacitor. The tracks all seemed intact, bar one connection from the RTC chip to the crystal which I patched up.
As horrid as it looks, a wash in a vinegar solution and a clean with IPA seems to have brought it up nicely. Originally, the battery was a 1.2V rechargeable, and the user guide recommended having the machine switched on at least 1 hour a week to keep it topped up. I didn't fancy that, so I went for a standard CR2032 3V battery, as used on almost all PC motherboards these days. The PCF8583P RTC chip claims to work from 1.0 to 6.0V, so 3V should do nicely. I added a diode in series to stop it trying to change the battery and powered it on.
I didn't get any photos of the screen at this point, but it wasn't a great start, the settings were wrong (it defaults to all the ROMs being virtually 'unplugged', like the BBC Master), so was just showing 'Supervisor'. However, I couldn't get a stable picture. I didn't want to reset the config until I could see what was going on. The LCD TV wouldn't lock on to the sync, so I tried the composite video on a Commodore 1901 monitor. Same thing. The 1901 has an RGB input, so I made up a lead for that and tried. Same again.Testing it with the scope showed the sync was around 31Khz, not the 15.4KHz I'd expect - it seems the machine was in high res mode. So I went for the factory reset (hold down delete whilst powering it up). I got the screen with a red border which synced up fine, and then up came the loading screen.
It looks to have been upgraded to RISC OS 3.11, which is the last version supported on this hardware. And there we have the RISC desktop.
There is still the issue of a mouse. I didn't get one with this machine, and they are tricky to find (round 9 pin connector, discrete wired buttons and quadrature outputs).
For the moment, pressing F12 allows you to type. All the keys and the floppy disk seem to work fine. Acorn always seem to be on the ball when it comes to thinking about expansion, and I found places on the board where sockets could be fitted for alternate mouse and keyboard connections. They are both controlled by an 8051 based microcontroller which converts key presses and mouse moves to serial data, presumably using the same protocol as the controller in the external keyboards on the earlier A400 series machines. There is even a jumper to interrupt that serial data and inject new traffic. So in theory, if I can work out the protocol, I could add a microcontroller with a mouse and convert it's output to serial and inject it into the system as if it had come form the 8051.
Having fixed it, that put paid to my plan to install the Raspberry Pi, I think I need to look for one a bit more broken - or try less hard to fix the next one.

Monday, 26 August 2013

ZX81 Clone Part 2 - ZX80 Clone

This is an old post, preserved for reference.
The products and services mentioned within are no longer available.

Following on from the previous article where I built a ZX81 on breadboard using the original chips and then modern equivalents of all but the irreplaceable ULA, I moved onto the next step, which was to replace the ULA. There are many alternative circuits on the next (I've already admitted I'm reinventing a wheel that's already been reinvented), however, the simplest circuit and a good starting point is actually the ZX80, so I started with that. There are rather a lot of chips required, so first I'm going to need a bigger breadboard. The one I had been using for many years seemed quite good, so I got another one of those to double the area. However, I took the opportunity took make a small improvement. The previous builds have taken power from the posts via wires to the top rails, and then via wires down the sides, and via smaller jumpers, across the top. Having all the rails separate does give increased flexibility, but pretty much everything I had built on there had been using the same 5V rails all wired together.
This had worked fine, but was a little untidy and limited the usable space a bit. So before I started, I took the new one apart to see if I could wire up all the rails up behind the board. Firstly the left and right sides of each rail were wired together.
Next each rail was soldered to a wire at one end.
These were there threaded through holes, glued in place and bolted to the posts.
This gave me a clean board with power to all 6 rails.
So no more delaying tactics, time to build a ZX80 clone. I followed the ZX80 circuit more or less. I made a couple of changes for layout and practicality, I kept with the 16K RAM and 74LS04 for the crystal oscillator that I'd used on the ZX81 and used a single 74LS245 instead of a pair of 74LS05's for the NOP generator (I ran out of space!).
I also jumped the gun on the next modification and added a couple of gates to generate the back porch and make the video signal suitable for a modern TV (the 'white level restoration' section from Grant Searle's ZX80 page).
So here is the ZX80 clone, next to the original I was using to compare signals. The top of the left hand board has the Z80, RAM and EPROM, below that, the things which use the address and data buses: the NOP generator, the input port at 0xFE, the character latch, address generation and video shift register. The top right is the rest of the glue logic, and the bottom right is the additional video tidying up and output buffer.
Next, I added the remainder of the NMI generation circuit as per Grant's page, which should in theory make it fully compatible with a ZX81. That has filled up the right hand side of the breadboard as well and it is looking rather messy now.
Unfortunately, somewhere there is at least one of these wires in the wrong hole - it may take some time to find out which.

To be continued......

UPDATE - I did just about get this working, but the breadboard version had some intermittent connections, so I didn't get much further with that. I have now revisited this and built a PCB version, a ZX80 clone I have called Minstrel.

2022 Update - I did go on to release ZX80 and a ZX81 compatible kits. There are currently a limited stocks of these left, available from TFW8b.com.

Wednesday, 21 August 2013

ZX81 Clone Part 1

This is an old post, preserved for reference.
The products and services mentioned within are no longer available.

For many years, I planned to build a ZX80 clone. It's all standard parts (no custom ULA like the ZX81 or ZX Spectrum), and it's a well known and well documented circuit. However, I never quite got around to it. And then, I got my hands on an actual ZX80 (read about the repair of the ZX80), so the idea lost momentum.
Recently I've come across a few more ZX81's with dead ULA's, which is great for turning them into ZX81 USB keyboards, but not great if you play 3D Monster Maze. So back to the idea, but this time, make a ZX81 Clone. OK, it's reinventing a wheel that's already been reinvented, but it should be an interesting project, and I plan to make some modifications along the way.
The idea is to build something along the lines of a ZX81, using standard components i.e. without using the custom ULA chip. I've talked about that a few times before, it's basically all the chips on the ZX80 that weren't the ROM, RAM or CPU rolled into one. The ZX81 ULA also added some extra functionality over the ZX80, so it's even more chips to replace. It was a marvel at the time, only problem is they ran hot and eventually failed, and you can't easily get replacements. The inner working are now well understood and well documented, thanks to the work of various Sinclair fans. The following sites have provided excellent points of reference, and are well worth a read:


It seemed the fist stage was just to try to build up the ZX81 circuit away from a ZX81 board, but using the original ZX81 chips. This was just a test of how practical it would be. It worked, not particularly well due to clock issues, but sufficient to move onto the next stage.
This used parts borrowed from an issue 1 ZX81, a Z80 A CPU, Mostek MK36809N (8K mask ROM) and Mostek MK4118N (1K SRAM), and the Feranti 2C184E ULA is the one at the bottom with the heatsink on. The next step was to replace everything but the ULA with modern equivalents. I used a modern Z80 CPU, the Z84C0006PEG (which is limited to 6MHz, I need to order a faster one if I want to enable turbo mode). The mask ROM was replaced with a 27C64 (8K EPROM), and the RAM with a 62256 (32K SRAM). Although it is a 32K chip, without extra logic, only the first 16K can be used. Even though this is more RAM, the supply current is almost half with the modern chips.
I had a few problems with V1 above with the clock, and the same here on V2. I ended up building an Arduino based frequency counter to keep track of the frequency fluctuations. Since the video signal is directly tied to this frequency, if this drifts, the TV signal goes out of range. I tried both a 6.5MHz ceramic resonator on the ULA, and also a 6.5MHz crystal oscillator made with a 74LS04. I eventually traced the issues to the transistor which buffers (and inverts) the clock between the ULA and the Z80 CPU. I tried an assortment of different transistors, including a ZTX313 from a ZX81. I finally changed to using a gate from a 74LS04 inverter, and the clock issues all went away, I got a stable 6.5MHz main clock and a stable 3.25MHz CPU clock. And that lead to a stable TV display.
The video output isn't bad considering this is constructed with long wires on breadboard, with a simple transistor amplifier on the video out, and it is being displayed on a modern LCD TV. Just a word at this point about ULA versions. There were two, the original 2C184E, like the one I am using here, and the later 2C210E. The main difference between them was an improvement to the video signal, adding the missing 'back porch' signal to properly set the black level. So that's about as good as I'd expect from a lashed together circuit with the older ULA.
So now that's working it's onto the next stage....

Update:

And only 5 and a bit years later, it's here, the Minstrel ZX81 compatible computer kit.


2022 Update - I did go on to release ZX80 and a ZX81 compatible kits. There are currently a limited stocks of these left, available from TFW8b.com.