Friday 3 December 2010

Electronics for Dogs 5 - some loose ends

So in the first post I told you there were some things to not worry about. Some things to trust me on. Hopefully now I'll complete the picture. This should be a very short post.

Let's assume that our idiot is running a washing machine. We're going to make things a bit easier for him but in this case he, as always, has no idea what he's doing. He's just following instructions and doing as he's told.
I'll make it easy and have a number of lights on the wall labelled with numbers. In actual fact these will be connected up to sensors on the washing machine. One light will be the door open latch, another will be the half full indicator, another will be the full indicator, another will be the overflow. One will be the on switch.
There'll be another indicator that as far as he knows gives a number from 0 to 7. In fact that will be the program number. There'll also be a number of switches for him to throw. One will be connected to a pump to pump water in, one to pump water out, one to turn on the motor that runs the drum etc. But he won't know, it's just a bunch of switches to him.

So he sits there and his first instruction says wait for light 1 to switch on. When it does look at the number on indicator 1 and use that to determine which set of instructions to use next.
Having picked instruction sheet number 3 he sees that his first job is to see if light number 4 is turned on (is the door closed) if it is then turn on switch number 6.
And on and on...

So far so good, but what does this have to do with a processor? Did you notice how all of that up there was to do with numbers? If light number 3 is on could very easily be is value x 1 or 0. Throw switch number 6 could be set value y to 1.

Time for a new concept. Let's make this idiot's job even more basic and simple. Let's get rid of the lights and switches Let's just give him a wall of numbers. Think of it as a wall of pigeon holes if you like, so that instead of a light coming on the number in box x changes from being 0 to being 1. Instead of throwing a switch he puts a value into that pigeon hole.
This makes things simpler for him because now everything is a number, if he's solving the budget or operating a washing machine or running the phone exchange all he has is a wall of numbers and a set of instructions. In the real world getting things like the status of a door latch into a number in a box on the wall would be very difficult. For the computer however it's easy!

I said previously that everything in a computer is a switch. Numbers are stored by the state of a number of switches. The difference between a switch that stores the last value you told it, and one that tells you the state of the washing machine door really isn't that much. Going back to the pigeon hole analogy instead of numbers on a slip of paper let's say we had 8 switches. Each switch had a light associated with it. You'll remember from the last post that 8 switches means that you can story a value between 0 and 255. So that's what our idiot is doing, you tell him to store the number 4 and actually he's not even aware he's doing that (I did tell you he was very stupid didn't I) he's setting the 8 switches to 00000100. That is just flick on the switch of bit 2 (we start numbering at 0) and make sure all the others are off.
Sometimes these switches will be just connected to the light associated with it. Sometimes they'll go and do other things. Sometimes the light associated with the switch won't be connected to the switch at all and will be connected to (for example) a door latch. It doesn't matter he doesn't care that's a separate problem.

Believe it or not that is what your keyboard and mouse are doing. When you push a button it closes a switch. This switch closure is detected and presented as a number to the computer. But if it's a USB keyboard or mouse then the only way your computer knows about this is that somewhere in its memory a value has changed and it has to occasionally go and look at it to see if it has.
Having said that I'll immediately retract it because USB doesn't work quite like that. In most implementations it's more a case of the processor will regularly check memory and be presented with a list of devices that have had updates, then it will have to go and look at those updates to see what they are; however the principle is still valid that the only way the processor knows that something has happened is because it goes and looks at some memory and sees what value it is then goes and does something else based upon that value. Once again it's all just numbers at the end of the day.
Oh and if anyone is old enough here to chirp in and start talking about I/O address space and the like then yes, well done, have a cookie, but often it's not done that way anymore for good reasons, stop trying to confuse the issue, that's my job!

So next time we'll take our last detailed look at our idiot making him the most stupid ignorant individual you'll probably meet. But that's okay because the dumbed down version is easy to copy.

No comments: