Flashing LEDs – Z80 Style

good morning all and a Happy New Year 2017 hmm so what are we going to do well flash some LEDs obviously but today let’s do it slightly differently let’s do it using a z80 CPU and I’ve got lots of z80 CPUs because I was heavily into the z80 back in the 80s it’s kind of where my youth got Miss spent but I’ve got a quite a collection here covered in dust some of them because I’m seeing the light of day for 20 years but yeah lots of Zen 80s now this looks like quite an intimidating chip I mean 40 pins seriously how easy is it going to be to get this to flash some LEDs well it turns out it’s not that hard stick it in a breadboard put a few LEDs on some pins and some resistors and we got ourselves some flashing lights an orange one there and a green one and on the blue light you can see it’s counting up in a binary sequence so yeah not too hard but let’s go right back to the beginning and see how this is done now of course you can’t just expect to plug a zhe CPU into a breadboard and it just works we do need to do some research what we need is some data and I just happened to have this fantastic original zilog z80 CPU manual and in here we’ve got the z80 CPU architecture register summary first we got our pin outs now that’s going to be very important we need to know how to connect the various pins what all the pins do and timing diagrams and waveforms we’re going to need these two so what I’ve done is I’ve plunked a zilog z80 CPU now this is actually a z80 not a za ta so this is the original 2.5 mega Hertz product I plunked it on one of these MB 102 bread boards I’m really liking these bread boards because you’ve got the positive and negative on both sides linked those across with a couple of wires and I’ve built a clock because the z80 CPU needs to be clocked now this has a maximum of two point five megahertz that’s not going to bother me too much today because I’m going to be driving it at about five or six Hertz this is just a 555 timer in a stable mode so freerunning multivibrator oscillator mode resistor between seven and eight resistor between seven and six and I’ve implemented that as a pot because it’s just easier it spans those pins very neatly and it allows you a little bit of flexibility to tweak the frequency although if you turn this you do find that the mark space ratio there is a bit I’ve got a capacitor there between pins 1 & 2 so that’s setting the clock speed and then there’s a link jumping from pin 6 across to pin 2 so classic clock mode multivibrator mode LED on pin 3 of the 5 v 5 so that’s just showing the output oscillating switching on and off ok so first things first z80 cpu we’re going to need some power so 5 volts goes to pin 11 and ground goes to pin 29 right so we’re going to count now at 1 2 3 4 5 6 7 8 9 10 11 right pin 11 just happens to be marked 11 even though my numbering is going other way so what was it 5 volts to pin 11 and this is where this MB 102 breadboard is quite handy because I’ve got a positive down on this lower side which you wouldn’t have on a breadboard that has the single fast line so 5 volts to pin 11 that looks like it fits there right ground pin 29 so there are 20 pins on each side so that’s a 1 because there’s the ident marker you can just see that in the light so that’s pin 20 this is pin 21 21 22 23 24 25 26 27 28 29 so it’s just above where it has a PS right we have this strange phi symbol on pin six and phi says single-phase TTL level o’clock which requires only a 330 ohm pull-up resistor to plus 5 volts to meet all clock requirements hmm interesting I didn’t bother with the 330 ohm pull-up resistor and it seemed to work fine right so we want the clock output from the 555 to go to the Z 80s pin six that’s one two three four five six so let’s put that in there and stick that on the output of the clock so that’s it the CPU is powered up and it’s

being clocked by my five or six Hertz clock hmm now how are we going to see what’s going on well one of the signals that we could look at is this m1 signal on pin 27 and down here it says m1 is machine cycle 1 it’s an output it’s active low m1 indicates that the current machine cycle is the opcode fetch cycle of an instruction execution so I’ve made up some LEDs which have a little resistor soldered to the cathode so that would be the most negative end so I’m going to use this yellow LED to take a look at that m1 signal so let’s plug that one in so it goes on to pin 27 that’s to back from the ground pin the zero volts which is pin 29 now I’m going to put the resistor which is the anode actually to the m1 signal that’s the cathode sorry and the anode I’m going to put to positive because I want the LED to come on when the m1 signal goes low because it’s active low in other words when it goes low we are in machine cycle one and that’s what I want to see so let’s put that in positive and as I say the resistor the cathode end which is the little flat on the LED goes to that m1 and let’s see what happens switch on oh it’s flashing so we’ve made a star we do have a flashing LED but that only flashed a few times and then it stopped so what’s going on has the CPU crashed in fact do we know what this thing is doing at all well we don’t really because there’s lots of other signals here that we need to deal with so let’s take a look at some of those what we really need to sort out are these inputs because they’re going to actually affect the way the CPU runs so weight for example tells the CPU to just do nothing for a while to extend some of its pulse widths so we need to make sure it’s not waiting this is an active low signal so we need to pull that high now the CPU uses n MOS which is sort of half CMOS but it does use MOSFETs and their gates float so what we need to do is make sure that we don’t leave them just open circuit we need to tie them either high or low so to deactivate this weight signal I’m going to tie it high now I’m going to use 1k resistors it’s probably not a good idea to tie anything directly to 5 volts or even directly to zero volts but a 1k resistor will pull it fairly hard up and fairly hard down if you connect it to zero volts it’s just a safe option really so a pin 24 so that’s 1 2 3 4 to positive let’s put that in there so that means that the CPU is never going to execute a wait state 16 and 17 are interrupt and non-maskable interrupt respectively we don’t really want to invoke the interrupt system of the CPU because that gets quite complicated and what we’re doing here is anything but complicated so I’m going to tie both 16 and 17 to 5 volts using 1k resistors so there they are pins 16 and 17 both pulled up to positive another reason why this mba 102 prototyping board is rather good because I can pull pins this side to positive or this side to positive it’s really good okay so now what else have we got reset a now we certainly don’t want that floating about so 26 needs to be pulled high because again that’s an active low signal that’s what the bar running across the top means and bus requests now this is an interesting one if you pull bus request low the z80 says oh okay you can have all my buses the 16-bit address bus the 8-bit data bus and a whole bunch of these control signals will all go tri-state and the CPU says I’ll just take over I’m I’m out of the equation this is for DMA direct memory access so this is for when other peripherals for example a floppy disk controller would just take over the bus the CPU would go quiet and the floppy disk controller would write directly to ram it’s just a quicker way of doing it than passing every bite through the CPU so reset 26 bus requests 25 both pulled to five volts so that’s dealt with power and the clock input and all these control inputs which I’ve just pulled all hi now everything else is either an output these are all outputs the address bus are all outputs or the data bus which is bi-directional but it will only be an input if the CPU is doing a read so let’s try that now and see what happens it’s switch it on oh just the one flash that’s not very impressive is it so in some ways I’ve made it worse but we are getting to the

point where the CPU is going to do something intelligent now one critically important difference between a microprocessor like the z80 and a microcontroller is that the micro processors don’t have any program memory so the z80 has no program to execute now back in the days of the z80 the most popular option for program memory was the EEPROM the erasable programmable read-only memory now generally speaking this is a read-only memory you can’t write to it unless you go through a special sequence and apply a special programming voltage and you can see that this one actually has it written on there VPP the programming voltage is 12 point 5 volts so if you put 12 point 5 volts on one of these pins I can’t remember for the life of me which one it was and then go through a special sequence of toggling the control lines you can write data into this thing only once and then this becomes programmed and read-only then the CPU will read its program instructions from this memory the only way you can erase those program instructions and put new ones in is to shine ultraviolet light through this little glass window but this is all going to be a bit of a hassle because we need to connect I think it’s 15 of these address lines to the EEPROM all of the eight data bus lines half-a-dozen control signals that’s just going to create a spaghetti of wires and I don’t really want to go down that route so I’m going to build a sudo ROM so I’m going to build something that behaves like this ROM but just with these 8 resistors now if I programmed this ROM so that every single memory location had the same data in it I could fake that by just putting resistors into the breadboard so that every time the CPU reads an instruction it just reads the same one so the question is which instruction am i going to program into my ROM well I’m going to do this one no op no operation and the data for that is very simple it’s 0 0 so I want the CPU to read in on its data bus 0 0 well that’s actually very easy I just tie all eight of these data bus lines down to ground I’m going to do it using 1k resistors because the database on is 880 is bi-directional it can on occasion right out of that data bus and I don’t want it to be writing directly into links to ground so 1k resistors in all of these eight data lines they are these eight connections here four to the left of VCC and four to the right let’s get those resistors in now so there are the resistors there’s my sudo ROM and now the CPU is reading the data from that ROM and it’s running completely reliably the em1 light is flashing on and off it’s actually flashing on an offer a quarter of the speed of the incoming clock but that doesn’t really tell us a lot what we need to look at now are these address lines the address bus we don’t need to look at many of them I’ll say put about four LEDs in on a 0 a 1 a 2 and a 3 let’s do that right now and now we can see that there’s a binary counting sequence on those address lines so what the CPU thinks it’s doing is stepping through the ROM picking out an instruction from each different location actually we’re fooling the CPU we’re giving it the same instruction for every location in the memory so our memory can completely ignore these address lines because it just contains 0 0 at every address now these 4 LEDs look like they’re counting up from 0 to 15 whereas 15 there’s 15 and then resetting back to 0 that’s not actually happening because when it resets back to 0 the next most significant address line goes high so let’s get some more of these blue LEDs in right now I’ve got 7 LEDs on the lower 7 address lines that’s a 0 to a 6 and we can see now that it’s counting up in binary from 0 all the way up to and it’s going to get there any minute now a hundred and twenty seven so that’s seven lots of ones all high it’s very nearly there and there it is so all the LEDs are on as one hundred and twenty seven and then again it resets back to zero but we know that actually it’s

going to count up to the full 16 bit address count which is 65,536 right I’m going to now add a couple more LEDs I’m gonna put LEDs on Reed which I’ll make green and right which I’ll make red and here they are on pins 21 and 22 and you can see that the Reed light the red line the green one is pulsing pretty much in sync with the m1 LED but the right LEDs coming on at all so the CPU is not doing any writes on the data bus and that’s because it’s just reading instructions from the pseudo ROM it’s reading a whole sequence of no ops and it’s counting up through the memory it thinks it’s reading sequential instructions from the ROM actually it’s reading the same instruction over and over again right time to look at a timing diagram and this one is the opcode fetch so every time the CPU is reading this pseudo rom reading the zero zero opcode which is just a no operation it has to toggle certain lines and you can see here that the read line goes low for slightly less time than the m1 line goes low we can actually see that happening there the m1 lights up a little bit ahead of read and so we can see that this is actually working so m1 the yellow light tells us that we’re doing an opcode fetch read the green light would known be routed round to the ROM to actually enable its outputs and the address would be also routed round to the ROM to select the address location for the data we want to read right time to add another LED and that’s this one memory request now look at this one this one actually goes low twice once where it puts the contents of the program counter on to the address bus in order to read the ROM and another time when it puts a refresh address on the address bus and that’s for dynamic Ram it is all a bit complicated but let’s plug this one in now this needs to be active low so it goes pin 19 to positive and we can actually see that double pulse the read line only pulses once so that’s there the memory request line pulses twice so certainly what we’re seeing ties up with this waveform diagram now I’ve just put in an eighth blue LED so this is on address line a seven and now something seems to be going a bit wrong because that address line is flashing on and off so no longer do we have this nice neat counting sequence because one of the address lines seems to have gone a bit mad what’s actually happening here is that the CPU is alternately putting out memory address locations with refresh address locations for dynamic RAM so by going beyond seven LEDs things start to get very muddled so it’s probably better if I take that LED back out and continue without it now remember I said that the m1 signal is flashing four times slower than the clock input well there’s a reason for that well that’s because the no operation code there it is no op there’s the binary value it’s all zeros that’s where I tied all my data lines down to zero this no operation code takes 40 states that’s four clock cycles to execute so why don’t we redo the ROM so that it’s a different instruction that takes a different number of T states right I’ve now reprogrammed my ROM well all I’ve done is move a couple of resistors so that it’s not reading no ops anymore it’s actually reading a hexadecimal zero nine now these data lines are all mixed up so it doesn’t look like 0 9 but that’s what it is and that’s this instruction add to the HL register another register I’m using BC but the important thing is that this is 11 T States long so now it’s all a bit slower the m1 light is only coming on for every 11 pulses on the clock everything else is the same and one and read both come on almost together memory request is pulsing twice same as before the address bus is counting up and that’s because this instruction add HL comma BC is just one bite it just takes longer to execute now there are more than a hundred different instructions in this z80 that I could program into my pseudo ROM and get different combinations of flashing lights but I think the point has been made the z80 can be powered up on a breadboard you

can simulate a ROM so that can read instructions in it’s currently reading that instruction zero nine eleven T State so eleven flashes of the input clock to every flash of the m1 signal and it’s counting up on the memory address bus or you can have them much faster and actually more pretty in terms of flashing LEDs no op with all zeros here so that the m1 light lights every four clock pulses and how about this one this one is the instruction load an external memory address pointed to by the H L register with an immediate bit of data and that’s instruction 36 I put 36 on the database because that means that the data is also going to be 36 but let’s not worry too much about that so that does two reads you can see that the read light flashes twice but then it does a write and it writes to a memory address pointed to by the HL register now I have no idea what the HL address is pointing to so of course the address bus just looked like it’s going completely crazy but this is the first instruction that I’ve actually executed where the right light comes on so it is writing to this data bus so that’s probably a good thing we’ve got 1k resistors in there now rather than wires pulling directly down to ground so yes you can use a set a TCP you to flash LEDs it’s quite complicated you do need to do a fair bit of reading through the data sheets but I have to say that’s the most fun I’ve had with a z80 CPU probably in the last 20 years now interestingly this said 80 is getting very very warm interestingly in the z80 datasheet DC characteristics there’s really very little here about how much current you can drive from each of the outputs and there’s just this rather bland statement note all outputs are rated at one standard TTL load well I’ve looked up a standard TTL load and it’s 1.6 milliamps and I know that I’m drawing one heck of a lot more than 1.6 million for each of these LEDs I mean the resistors I sold it to the LED legs are only a hundred and fifty ohms they were the only ones I could find in this tiny eighth watt format so I think I’m massively overdriving the outputs of the zilog z80 cpu unlike a microcontroller where all the i/o is designed to drive reasonably heavy loads these pins were never designed to drive LEDs it was never meant that a Zed 80 would have LEDs on all its address and are and control lines so there it is flashing LEDs z80 style and actually if you want a quick and dirty seven bit binary counter why not use a set eighty cheerio