Monday 30 September 2013

Z80 Single Board Computer (PET Project Part 3)

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

This is a further use of the Arduino TV Out Library I previous posted. It is effectively a Z80 single board computer.
OK, it's made on more that one board, but you get the point. Based on Grant's 7-chip Z80 computer, it uses his modified version of the NASCOM computer version of Microsoft BASIC running from an 8K ROM. The remainder of the Z80's 64K address space is taken up with 56K of RAM. The board on the left contains the Z80 side of the system and the right hand side controls all the input and output, leaving the Z80 free to run code all of the time rather than having to bother about screen display or keyboard scanning.
The keyboard is borrowed from a Commodore Pet as this will end up as part of my 'Pet Project', a plan to fill the gap inside an old Commodore Pet where the mainboard was removed 20 odd years ago. The box on the bottom right is an in circuit programmer, the USBtiny ISP (see my review of the USBtiny ISP), to program the chips from the Arduino IDE.
On the left there is the Z80 side of the system. The EPROM, RAM and the Z80 CPU, are all under the nest of wires on the top row of the left breadboard. On the bottom of the left board is the clock, running at 7.3728MHz. A slightly odd frequency, this is used as it is exactly 64 times 115200, which is the BAUD rate used by the serial communications within the system. Those serial comms being handled by a 6850 UART on the bottom right on that first board. There are three connections across to the second board, RX, TX and reset (there is now and additional RTS line to ensure the serial buffer doesn't overflow).
On the right are two ATMega328P microcontrollers. The left hand one is connected to the serial RX and TX from the Z80 SBC. Also connected to that are the 8 column lines of the Commodore Pet keyboard. There aren't enough pins spare for the 10 row lines, so I borrowed the idea from the Commodore Pet itself of driving those via a 74LS145 BCD to decimal converter. That means it uses just 4 lines to drives one of 10 outputs. The columns are read as inputs, pulled high internally. The keyboard is scanned and any key presses sent over serial. Any data coming back over the serial is send out of the I2C port to the second ATMega 328P, using the Arduino TV Out Library. That is basically the same thing which is on the shield version, an ATMega 328P and a 74HCT166 shift register.
Indeed, I initially build this up on the shield version, but fell foul of the fact the hardware serial port on the Arduino is used for serial programming, and so cannot be used for other purposes. I thought about switching to the Leonardo, which does have two serial ports, but the I2C pins are in a different location, so the shield wouldn't have worked. So I went straight to breadboard for it all. The components of the shield are duplicated, and together, they generates a composite video signal which is sent to the TV. On power up, the keyboard / serial processor holds down the reset line of the Z80 until it is ready, then releases it, so there is always a clean start.
I'm planning to build a series of boards inside the Pet, this will be the first, a simple Z80 SBC running BASIC. I've build up a slightly different version using a 63B50 (a CMOS version of the 6850), a modern CMOS Z80 CPU, 74HC chips and 32K of RAM.
This version uses less than 20mA, so can easily be powered via a USB to Serial adapter such as the FTDI clone Arduino USB2Serial Light, and run from a serial terminal. For reference the left hand breadboard version uses around 150mA, with 50mA for the keyboard / display side.

Saturday 28 September 2013

Arduino TV Out Terminal Library

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

This is an update to the library I have written for the Arduino which makes use of Grant Searle's Video Processor. Grant has recently updated his video processor code to support 40 character mode as well as the original 80 character mode, added a second font and a double line mode. There is now also a 160x100 pixel graphics mode.
The principal is this, you have an Arduino with your code in and this library, and connected to that you have a second ATMega328P (the same chip as is used in the Arduino) and a 74HCT166 shift register. Together they do all the work involved in generating a composite video signal suitable to connect to a TV or video monitor. You can build up these additional components on breadboard, or I have build a shield which fits onto the Arduino with all the required components on. The DIP switches select the default font and communication settings.
Interfacing between the Arduino and the video processor can be done in three ways. The original way was an 8 wire parallel bus with 2 handshaking control lines. A second mode was later added which allowed this to be reduced to 4 wires and 2 handshaking lines. The third method uses 2 wires in TWI or I2C mode, which was originally a fork of the code as part of an earlier version of this library, but has now been rolled into the main code. (Note: the diagram below shows shift/load connected to the ATmega in the way I had originally connected it. Since this post, Grant has changed the code to use PC3 (A3) for shift load).
The pin assignment on the video processor has been updated so that the two TWI pins will always be connected (they are used for handshaking in 4-bit and 8-bit modes). The library and shield support all three, just make sure the library and the wiring match.  The 8 bit is up to twice as fast as TWI mode (30,000 characters / second vs 18,000), but for most applications, either should be sufficiently fast. The 8 and 4 bit versions can use any pins that are spare, the TWI version needs to use A4 and A5. So unless you need to have A4 and A5 for some other use, or you really need speed, it's probably easiest to use TWI mode. All the demos are coded for TWI, but can easily be adapted to 4-bit or 8-bit mode by changing only the terminal constructor line. See the comments in the code for further information. I also connect the reset lines together, so the video processor gets reset when the Arduino is reprogrammed, so you get a clean start each time you change your code. Otherwise, tie the reset line high.
The source and hex file for the video processor are unmodified from Grant's original and are included in the library with permission. The fuses need to be set to E6 D9 FF. You can program an ATMega328P using an EPROM programmer (such as the mini pro shown above) or Arduino as ISP (see the earlier article for further information on this and the avrdude command line).
The library can be downloaded here. If you are using Arduino 1.0.5, you can import this be going to the 'Sketch' menu, selecting 'Import Library' and then 'Add Library'. Browse to the downloaded zip file and add it. With older versions, just unzip the 'Terminal' folder and add it to the 'libraries' folder in your Arduino directory.
As previously, I've had fun writing a few samples. The mock up BBC start screen now looks better in 40 character mode which more closely matches mode 7 (the previous demo was more like mode 0). The homage to Look Around You also works better in 40 character mode.
I've updated the Hello World samples to show the character set in a more useful way, and added spinning characters in the corners.
The new version of the video processor supports multiple different styles, on a per line basis, there is now a demo showing off the various options.
The new version also brings graphics mode. The ability to address the display as 160x100 pixels with set pixel and clear pixel commands. I've extended this functionality in the library and added functions to draw lines, boxes and circles.
When testing the line drawing, it reminded me of the old Missile Command games, so I mocked up a demo of that. This is just a simulation, it just fires at the bases and loops around after firing 5 shots. I don't know if there is enough to be able to make that a playable game, maybe.

Monday 23 September 2013

Vintage IBM 5160 XT Teardown - Part 2: A Brief History of Disk Drives

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

This started as a look at the drives that came with the IBM5160 I recently acquired and took apart in Part 1. However, when I started looking at when should have been fitted, I ended up with so many different types of drives on the bench at one time, it seems to have grown into a brief history of the disk drive.
The original IBM PC (the 5150) had two of these. They are Tandon TM100-2A drives, 5.25" floppy disks 40 tracks, DS DD (Double Sided, Double Density), giving a total of 360KBytes per disk. For reference, that's about the same size as each of the photos on this page. These are 'full height' drives, equivalent to two of today's standard 'half height' drives (i.e. it's the same size as two DVD-ROM drives). The IBM 5160 that I'm rebuilding would have originally be fitted with one of these and one full height hard drive.
When I got it, both had been replaced with 4 half height drives, 2 hard drives (on the left) and 2 floppy drives on the right. The replacement drives were (at least) half the size, but they were twice the capacity, as is the way of these things.
I don't have any 8" disk drives, but the disk on the left is an 8 inch floppy, with a capacity of 1Megabyte. The disk with the blue IBM label is a DS DD 360K. Next to that is a progression, same size but 4 times the capacity, a DS HD (Double Sided, High Densitry) 1.2MB 5.25" floppy disk. Below are the 3.5" inch disks that superceeded them. Firstly the grey disk is DS DD and gives 720K, and finally the largest capacity is the 3.5" DS HD floppy at 1.44MB. On the 3.5" disks, an extra hole was added on the right to indicate high density. The one of the left and the slot on the right on the 5.25" disks being a write protect mechanism.
The drives have shrunk as well. The 1.2MB drive is 'half height', and the 3.5" drives are even smaller. The 5160 cannot support the two HD formats, so that makes it easier to chose what to put back in. I suspect I will go with the 360K DS DD 5.25" drive and one of the 720K DS DD 3.5" drives that came with it. That would leave space for one hard drive.
Not relevant to the 5160, but there was another drive size on the market at the time, Amstrad had a 3" disk, the CF2. This had a capacity of 360K in total, split into two 180K sides, so it needed to be turned over to get to the other side. This made it cheaper as it only needed one read/write head rather than two as fitted to all the double sided drives. Amstrad used these 3" drives on their CPC 6128 and various word processors such as the PCW 8256. These were also used in the Specturm +3 (which was build by Amstrad). They later relented and moved to 3.5" DS DD.
With hard drives again, capacity grows and size is reduced. The drive on the left is a full height 5.25" hard drive, an ST4038, with a capacity of 30MB.  Tiny by today's standards, but that's 100 floppies worth.This is the soft of thing which would originally graced the 5160. The next two are the ones that were in the 5160 when I got it. The first is 5.25" half height, an NEC D5126 at 20MB, and a half height 3.5" wide drive in a frame to fit half height 5.25", a Tandon TM262, also 20MB. For comparison, the ones at the end are modern standard 3.5" and 2.5" hard drives, both 160GB (yes, 8000 times larger capacity). And the little spec at the front is a 4GB micro SD card.
Here is the platter from a 5.25" hard drive I took apart when it died in the early nineties. It was obsolete then at around 5MB I think. Next to it is the disk removed from a 5,25" floppy (probably around the same time). The small pin hole on the left is used to indicate where track 0 is, a light shines though a hole in the cover and when it is lined up with the hole, it shines on a sensor and the drive knows it's at the right position. That's what generally happens when you insert a disk, it spins it around until it detects track 0. Next to that is a 3.5" drive with the cover removed. There you can see the disk itself and the head which floats over it. On the end is a 2.5" laptop drive.
I'd like to go back to one full height floppy and one full height hard drive, but the full height hard drive I have doesn't have a front panel. So I'm going for one full height floppy, as it would have had when new, and a half height floppy and half height hard drive, both of which were in it when I got it. I've tested the floppy drives and all seem to work. The full height drive is out of alignment. It can format disks and read them back, but can't read other disks, and it's disks won't read in other drives, so I need to realign that.
I can't test the hard drives at the moment as I still haven't found a keyboard and it isn't compatible with the later AT style or PS/2 style keyboards, so I'm going to need to make some kind of interface. It's currently waiting for me to press F1........
On to Part 3.

Sunday 22 September 2013

Arduino 80x25 Text TV Out Shield and 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]

I've update the library used to generate 80x25 characters of text output to a TV or video monitor on and Arduino. This now uses the latest update to Grant Searles code which incorporates his 4-bit and 8-bit modes and the I2C version I had previously forked the library to add has now been rolled into the main library.
I've also build a shield to make using this even easier. This is currently a prototype, if there is any interest in this, I could put together a PCB version.
Grant is currently expanding the capabilities and adding as many character options as we can think of, soon it will support 80 character or 40 character text, single or double line height and a choice of two fonts. That's what all the DIP switches are for, to select the initial state of the display. Once the video processor is on the shield, That leaves you free to do what you want with the rest of the Arduino.

Monday 16 September 2013

Arduino 80x25 TV Video Output Library I2C Mode

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]

I know I only updated the Arduino 80x25 text tv / video output library yesterday, but whilst I was doing that, a further improvement occurred to me. The original interface was 8 bit parallel with two handshaking lines. This is about as fast as it is going to get, but tricky if you don't have 10 I/O lines available. The recent update was to add 4 bit mode. This effectively halved the speed by splitting the data into two nibbles, but reduced the required I/O pins to 6 (4 bits plus two handshaking). Well. I thought I could take that a step further. It is again fewer pins, now only two are required, but it is slower than the parallel version.
My initial thought was to use serial, but the hardware UART on the is on two pins of port D, and the full 8 bits of port D are required to drive the shift register. Writing the character data to the shift register has to be accomplished in 8 processor instructions, so only a write to a complete 8 bit port is possible. The UART is also interrupt driven, and the timer interrupts need to be maintained for the display updates. The beauty of the 8 bit interface was that it could be polled in the deadspace between timer interrupts.
So what else is available? SPI is an option, but is also interrupt driven. The final thought was the I2C. The ATMega328P TWI (Two Wire Interface) is fully compatible with the Phillips I2C (I Squared C or Inter-Integrated Circuit) bus, and the terms TWI and I2C will be used interchangeably. It allows simple communication with up to 127 devices with only two pins. It can also be polled rather than interrupt driven for exactly the purpose of slow devices, or devices which are busy doing something else.  The two pins required are PC4 and PC5, analogue pins 4 and 5. One of these was used on the parallel version as the shift register load pin. That has been moved to PB1, but means there need to be two different versions of the video processor. Grant Searle's original with 4bit or 8bit parallel interface, and the new I2C only version.
The documentation on implementing an I2C slave was a bit minimal, most of the examples covering only master mode. But though it took quite a while to get it working, the actual code changes to the video processor boiled down to be quite minimal. Now it initialises itself as an I2C slave as address 1 (this can be changed in the code if required). In the main loop, instead of looking for new parallel data, it checks the I2C interface and reads new data if there is any. The changes at the Arduino end are a bit more complicated. The Wire library is used to setup the Arduino as a master device and data is sent though to the terminal. The wire library has a limit of 32 byte packets, so I split larger strings up before sending. I also found problems with packets of only a few bytes. This could be an issue in my code or the wire library, I'm not sure. I had to add a small (50uS) delay after sending small packets or it can loose every other packet sometimes. Below is a screenshot from a debug version which just printed the value of the character it received, it was being sent a sequence 00, 01, 02, 03 etc. up to FF and back to 00. As you can see, it starts ok, but where is 75, 77, 79 etc.? The delay seems to fix this and doesn't slow it down too much. 50uS may be a lot in terms of 16MHz processor clocks, but if all you're doing is echoing characters being typed, it's perfectly acceptable. As with the 4-bit mods, it can still update the screen much faster than it can be read.
The library has been updated to support I2C mode, and there is now a Hello World I2C sample with the character set in I2C mode. This was a good test as the characters and the spaces between are all sent individually, where as the two 80 character lines are sent as single text lines, and there are various control codes to position the slashes at the corners.
The new library can be downloaded here. This zip file includes the source and hex files and source for both versions of the video processor, and an updated version of the library which supports I2C mode.
It is possible to use the Video Processor in I2C mode without the library, just using the wire library.
  #include <Wire.h>

  void setup()
  {
    Wire.begin();
  
    Wire.beginTransmission(0x01);
    Wire.write("Hello, World!");
    Wire.endTransmission();
  }

  void loop()
  {
    // your code
  }
If you go for just the Wire library, you will need to deal with the issues of the 32 byte Wire buffer and small packets.

Sunday 15 September 2013

Arduino 80x25 TV Video Output Library V0.2

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]

This is an update to my previous version of the 80x25 tv video output library for Arduino. Grant Searle has updated his video processor code (upon which this is based), to support 4 bit mode. This reduces the number of I/O pins required from 10 down to 6 (4 bits and 2 control lines). See his page for further information on that.
As before, all you need to do is download my 80x25 TV Video Output Library for Arduino library, and extract it into the libraries folder inside you arduino projects folder. The choice of the 6 pins is up to you, here is how it is wired in the code examples in the library.
There are two 10K resistors which control the mode of the video processor. R 4bit is there to set 4 bit mode. R NTSC can be used to set NTSC output mode. Omit R NTSC for PAL mode.
Note, this time I am following Grant's advice and using a 74HCT166. Although the 165 I used last time works, the 166 is preferable due to it's fully synchronous output. The HCT version is also preferable to the LS version. Compare the gaps between the zeroes. Firstly, a 74LS165:
Then a 74HC165
And finally a 74LS166
As you can see, the sharpness is improved. The variation in brightness is due to poor photographic skills on my part.
Although 4 bit mode is half as fast as 8 bit mode, it can still update the display considerably faster that you can read. One of the examples in the library I have provided reads the 6 analogue inputs and displays a binary representation of their value. It does this as fast as it can and continues to send data to the display, which scrolls up as the data appears. This is a good example of using this sort of output for debugging purposes.

Wednesday 11 September 2013

Commodore PET Project Part 2 - Screen and Keyboard

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

Following on from Part 1 of my Commodore PET Project, which looked at the history of this particular Commodore Pet 4032, the question is 'What are you going to do with it now?'. Well, I expect the biggest problem is going to be driving the display, so I concentrated on that first. The Arduino 80x25 TV out library I recently published was a starting point, but that was designed to output standard PAL / NTSC video signals. That is vertical sync of 50 or 60 Hz, and a horizontal sync of 15.625 KHz. The dot clock (the rate the pixels are sent out) is around 16MHz.
The PET needs separate horizontal and vertical sync pulese. I tried various methods to generate this, from simple RC filters to TTL counters, however, whatever I feed to the PET monitor, it wouldn't display it. Checking the output from a working pet, it appears to be more like 50Hz and 20KHz, and the pulses were considerable longer. The VSync was 50Hz (20mS) with a 720uS pulse, and HSync was 20KHz (50uS) with a 14.6uS pulse  I've updated the video processor code to run on an ATMega664. This gave me some additional I/O to allow H sync and V sync to be generated separately, and allowed me to increase the dot clock to 20MHz. This allows the system to use 1000 clock pulses per line (1000 clocks at 20MHz = 50uS = 20KHz). Allowing 292 pulses to generate the 14.6uS sync pulse (to match the original pet), and 640 pulses for 80 characters leaves 68 pulses at the end (3.4uS).
The video output is generated from a 74LS165 shift register, as before. This time, I'm using a Leonardo to drive the video processor, although it will be an 238P in the final build. I then tweaked the lengths of the sync pulses to match those generated by a real PET and now the display is working fine.
It seemed only right to use the Commodore Pet bootup text demo to test it. Here it is with an 8032-SK for comparison (yes, I know I forgot the space before the memory free line). The 8032-SK has the same 12" monitor and probably the same universal pet board as the 4032 would originally have had.
Next came the keyboard. It's an 8x10 matrix, and I have used many similar keyboards in my USB Keyboards, so it was just a case of adapting that code.
Rather than have lots of wires, I used two I2C I/O expanders (an MCP23008 and an MCP23016) on a small board at the keyboard end, so just needed 4 wires (VCC, SCK, SDA, GND) to connect from the board to the video processor chip and the interface microcontroller. So keyboard and monitor working. I/O handled via I2C, display handled by the display processor.
The first test was to simply send the key presses from the keyboard, suitably remapped, to the display. So for the first time in many years, pressing a key on the pet keyboard display the appropriate character on the pet screen.
Next, I tried Tiny BASIC, as I had in the previous tests of the 80x25 text out testing, and it was a basic computer, albeit with 1K free memory. Now, it was possible to type in a program an run it, again for the first time in many years.
To make it more powerful, the Arduino becomes just an I/O processor, taking key presses in and sending them over the serial port, and reading back the serial port to display it on the screen. Add a MAX232 and you've got an RS232 serial terminal - I'll be doing that later.

Add something more powerful and you've got a usable computer. In the next article, I'll be doing just that.

Update: This PET has now been fully restored