Friday 3 December 2010

Electronics for Dogs 6 - Our final idiot

So let's have a recap of our idiot. What can he do?
Well he has a whiteboard with boxes A to H on them.
He has a set of pigeon holes each with 8 switches and lights on them. Normally a switch will turn on the corresponding light, (but not always). Trust me when I say that it turns out to be very cheap to make these pigeon holes. Having billions of "holes" with 8 switches and lights in each hole is not a problem. Because of this we'll give each hole a number, or better yet an address.
He also has a set of instructions that we have given him. However these instructions have to be very simple. Let's have another look at these instructions. How simple can we make them? Let's take it to the limit:

Add the number in box A to the number in box B place the result in box C. If anything odd happens record the oddities with numbers in box D
Copy the value from box x to box y replacing the value in there with the new one (substitute any box designation in here)
Take a value from the pigeon hole number listed in Box D and put that value in Box A
Take the value from Box A and put it in the pigeon hole number listed in Box D
Set box A to value specified.
Test box A if the bit number as specified in Box B is a 1 then do not do the next instruction.
Stop following this instruction stream and start using a different one
And, XOR and NOT instructions. (more on these later)

And there you have it, less than a dozen instructions and capable of solving any problem you can throw at a computer.
Don't believe me? Everything I've discussed so far can be solved with those instructions. now a nice processor will make life easier for you by having more complex instructions, but let's get the basics done first.

One thing to say is with just a dozen instructions then why not have the instructions as a set of numbers? Rather than say "add this to this" then "move this to this" just say 12, 4.
When the instructions are just numbers then then the instructions can be placed into the pidgeon holes too. When our idiot turns up for work in the morning he doesn't have to think. He just looks at address 0 in the pigeon holes and sees the number 5 which tells him that he needs to start by writing 5 into box A then he looks at address 1 and sees he needs to...

Hang on though, how do you work out what numbers to put in there? That's the programming we've discussed but let me give you a final recap before we start getting into the electronics:

User Interface designer says:
We need a box that says Publish Post in it

Programmer Thinks:
First I need to draw a box, then set it's colour, then put the text publish post in it

Programmer writes - in a language I just made up, but not a million miles off:
draw_box(top_left_coordinate, top_right_coordinate, bottom_left_coordinate, bottom_right_coordinate)
Set_colour(orange)
Insert_text("Publish Post")

The Compiler (A piece of software, another program) looks at draw_box and finds the code associated with that. That code includes such things as:
create_triangle(top_left_coordinate, top_right_coordinate, bottom_left_coordinate)
create_triangle(top_right_coordinate, bottom_left_coordinate, bottom_right_coordinate)
so it then looks up create_triangle which includes code such as
vertex_x = coordinate_x + (offset*depth)

Which it translates into:
set A = offset
set B = depth
jump to multiply function
set A = coordinate_x
set B = C
add A to B and place in C
set address vertex_x to C

Which then gets passed to the assembler which can then rattle off a stream of numbers corresponding to the above instructions. e.g. set A to offset is saying set A to the value at memory address (wherever we chose to put offset in memory). so it's just a lookup that it has to do.

Now at this point I'm going to leave computers because I'm not sure more detail is necessary. If you want more detail on any of the above shout and I can always write more. However I'm not currently interested in detailing how for example the 3D rendering engine works no matter how interesting you may find that or otherwise, I just want the concepts to be clear as to what sort of processes you go through to get from a publish post button on a web form to a sequence of numbers in memory.

So next we move onto some electronics. To the point where you might be able to build one of these processors. Might...

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.

Electronics for Dogs 4 - On the nature of numbers

We all know what numbers are. If I want to write 10 I write 10 on a piece of paper. I can keep writing forever as big a number as I fancy - or at least as big as will fit on the paper.
Computers aren't like that. Not only do they have limited amount of memory - the size of the notepad as it were, but the size of the number they can represent is limited. Before I say why this is the case I want to say why it normally isn't a problem.

Let's say for the sake of argument that the largest number you could represent was 256. Actually no, let's say 100 because we're still thinking in decimal at the moment. What would you do if you needed to represent the fact that you owned 101 sheep? Simple you'd say you had 100 sheep plus 1 sheep.
Hang on a minute, that's exactly what our decimal system does. We only have 10 numbers 0,1,2,3,4,5,6,7,8 & 9. If you want to represent 10 then you say you have 1 lot of 10 plus 0 lots of 1. 23 is 2 lots of ten plus 3 lots of 1. 101 is 1 lot of 100 plus 1 lot of 1. We call this the base system, that adding numbers to the left represents increments of the base. Most of us deal in base 10. Computers deal in base 2. Why this is and what it means we'll get to later, but don't forget that we tell the time in base 12. Imperial measures are in whatever base they fancy at that particular time. You're used to different bases whether you realise it or not.
But let's say that even with this base system, this system of adding numbers to the left to represent ever larger numbers you still only have limited space. Let's say you're a bureaucrat making a form and you want to make it easy for yourself. So you make a form where the person has to put each letter of their name in a different box. You're not doing this to be annoying as such, you're just trying to stop people from using cursive handwriting to make sure you can get their names right. Of course as soon as someone tries to fill out your form who has 3 middle names then your system breaks, but hey that's not your fault is it?
Anyway this is exactly the sort of problem we deal with all the time in computing and electronics. because you physically have to build something you have to chose how big to make it in the first place, you can't just add a few extra bits on at the end because there is no space for them because you haven't built that bit of the computer.

So what do you do? First you make things big, normally you make them way too big. Second you don't just have fixed space allowance for things. Let me once again explain by analogy,
Imagine an authoritarian government states that every family shall have a home. Every family shall have at most three children. All houses are identical. Every family shall have an address of city number and house number and occupant number. Their name shall be formed by sticking all three together.
Sounds great/terrible but what happens when the supreme leader wants 4 children. He's not allowed to break the rules as such, but he really wants that extra child. No problem he thinks I'll create a new house number that the extra children will stay in. Child number 3 will be reassigned to the new house along with child 4 and the child 3 designation at the original house will be a pointer, a reference to this new house. Using this system of continually pointing to further houses he can have as many children as he wants without breaking the system.
Yes it's a mess, yes it's often confusing, yes there's a long trail of investigation to find what you need to know but this way he could have as many children as he wanted yet staying within the scheme.

This is the sort of problem we have all the time in electronics. Many processors in everyday devices, the one that probably runs your washing machine or burgler alarm, your calculator even are 8 bit devices. This means that they can't represent any number larger than 256. Ask it to add 3 to 255 and it will tell you the answer is 2.
Wait a minute you say, I just told my calculator to add 255 to 3 and it said 258 clear as day! Well yes, that's because it cheats, or perhaps the person who was designing it knew about this problem and worked around it. So in the case of the washing machine, it has 8 programs, has a delay timer of up to 120 minutes, has 3 sensors reporting nothing more complex than door open or closed so it doesn't need anything more sophisticated.
In the case of the calculator it's be a good question to ask, well why not make it bigger? Why not make it understand numbers larger than 256 rather than always working around the problem.
Well this comes back to my very fast idiot again. To get an idiot that can deal with bigger numbers is a little bit harder. And even if I got an idiot that could deal with the concept of 4 billion, what if I needed to deal with 5 billion? No first of all I need to come up with a set of instructions that can cope with anything I throw at the idiot/calculator then go from there.
So back to the problem, the calculator is trying to add 255 to 3 and telling me the answer is 2. How do I work around that? Well fortunately when it does the add it remembers that it ran out of space so while it tells you the answer is 2 you can then go and say "Did you run out of space" in this case it would answer yes. most of the time it would answer no. You as the programmer writing the instructions for this idiot can then put something in to keep track of this overflow and deal with it appropriatly. The advantage is if you write these instructions correctly the user of the calculator will never notice this problem.
Except that that's not entirely true either. On the average calculator try the calculation (9999999999 *100000) +1 - (9999999999 *100000) and you'll get an answer of 0 rather than 1, but hey nothing's perfect.

So anyway back to the whole point of this. I've been trying to hold this off in these articles but I can wait no longer. Binary and Hexadecimal. Bases and all their glory.
The building blocks of everything we've talked about is a switch, for the purposes of discussion almost all electronics is digital these days. The analogue stuff is I would argue more interesting, but the digital stuff is really what gets most of the work done. Why that's the case is not important right now, but it is simpler to understand so let's run with that. The switch can be on or off. Unless you have dimmer switches this is no surprise. How can you express numbers then. If you have only light bulbs then how do you represent 7? Exactly the same way we represent 15, or 23. 0 is 0 and 1 is 1. So it hopefully follows that 10 is 2 and 11 is 3. 100 then must be 4 which means 7 is 111.
Here comes the old joke that there are 10 types of people in the world, those who understand binary and those who don't. Now you can count yourself amongst the type that does.

This was where the 256 came from earlier. If you have 8 switches then you can have 256 values. This is why I say that while most people can count to 10 on their hands an engineer can count to over 1000, 1023 to be precise. Why? Because I still want a value for zero and with 1024 states that means I can count to 1023. How do I calculate 1024 for 10 fingers? Easy. With 1 finger I have 0 or 1. With two fingers I have 0, 1, 2 & 3. So I have 2*2 states. With 3 fingers 2*2*2. etc. Also known as 2^3, that is 2 raised to the power of 3. so 10 fingers gives you 2^10, 2 times 2 ten times, 2 to the power of 10 or however else you want to phrase it.

And that I promise you is the most complex maths you'll need for this series. Well unless I get as far as digital coding theory, but that's just long division. Oh and analogue electronics is full of calculus, but I can't remember much of that anymore so you should be fine. Control theory is much worse but I'll be glossing over that because I never really understood it anyway; it's a great case of Someone Else's Problem; and unless you're interested in switched mode power supplies (the thing that powers your computer) then you're fine.

Right so by convention we call each of those individual switches a bit. If we have 8 of them that is a byte. Normally if we need more of them we tend to use multiple bytes to give us 16, 32 or 64 bits. While 24 bit processors do exist they are not common for most domestic purposes. I have used 10 and 14 bit processors in my time, but they were really specialist so let's not worry about those for the moment.

All this is well and good provided you just want to deal with whole numbers or integers as they are often known. What if i want to represent 1.2 or 34.382476235076?
As always there are many ways you can solve this problem, but the one commonly used is known as floating point. Again if you're interested in why other systems aren't used then let me know, but otherwise trust me floating point solves most problems well enough. This is not how it actually works in most implementations - look here if you want to know some specifics, as usual this is about the concepts.
Because you want a fixed storage space for a number that could take a very large range you break the problem up.
Take the number 32.76256
in floating point form we store 3.276256 and 1. That is 3.276256 * 10. We could also have 327.6256 as 3.276256 and 2.
What's the advantage here? Simply put it's that often we only care about the number of significant digits. That is, to use an earlier example if we have (9999999999 *100000) then we often don't care about that last 1 that is added to it. Or to put it another way we'd often rather sacrifice the precision of an answer in order to be able to deal with the full range of the problem. That is it's better to lose that 1 in that calculation than to not be able to perform the calculation at all. If you have gone through life not knowing about this problem then that just shows how true it is. If you have experienced it before then you know how to deal with it I'm sure ;-)

Now one last word on these numbers before we go. 32 bits gives you just over 4 billion separate values. That's an awful lot you say. Indeed for most things we need to do it's an excessively large number. However with more than that many people on earth it's clearly not enough to assign a separate number to each device attached to the global network. This is exactly the problem the internet will face soon, but that's a whole other issue...

Next time there's a few loose ends to tie up. I mentioned how things get into and out of a processor and I need to deal with that first before we can then go on and look at some of how we would build a processor.
Feedback needed as always by the way, what is obvious and what is still a blur please do comment...

Electronics for Dogs 3 - everything is a number

So I mentioned last time how everything is a number. Many people might get this straight away, for others just how the web page they are currently reading isn't actually letters as you normally think of them but in fact an awful lot of numbers is a bit of a strange concept.

So let me start with it's all about context. The numbers 62, 76, 111 are equally good as letters on a page, values of sound level on an answer phone message or polygon locations on Buzz lightyears helmet; like everything in life it's what you do with them.
Again let's take a simple example. Text that I'm typing now. If you only have numbers then what do you do? Well you could start by saying 0=a, 1=b, 2=c. As it happens the most common standard is known as ASCII and a is represented by the number 97, b by 98 and so on. Lower values being used for UPPER CASE characters, punctuation, and numbers themselves. Wait, what? I hear you say, numbers for numbers?
Yes in an ASCII text file the number 12 is represented by the number 49 then the number 50. The space character is the number 32 and so on. If you want the full list have a look here.

So what about things like Microsoft Word. That allows you to do lots of things with text such as BOLD and italics, different colours and so on. Wait a minute I just did that on the web too. How does that work? Are there further numbers used for bold, maybe a bold letter a is stored as the number 165342...
The short answer is it is up to the writer of the program what they do but the web chooses a very interesting solution. (Disclaimer in the modern days of CSS and Java and the like this is far from the full story, but it'll do for now.)
Imagine a program that read in the text file as before, but if I typed some text then the brackets would be taken as an instruction to display some text. So a file containing only text (that was composed of numbers) would be displayed as something much richer.
Again don't worry too much here is a good starting place if the details interest you.

What else? Well the MP3s you listen to on your music device of choice? I'm sure you're aware that music and sound is just the movements of air. A speaker moves itself back and forth to move air back and forth. Why not represent the movement of the speaker, it's position from millisecond to millisecond with a lot of numbers? You may have heard of 20 kHz 40khz or 44 or even 48kHz mp3s. This is exactly it. In a 48kHz mp3 there are 48000 numbers used per second to represent the position of each of the speakers. Sure that's a lot of numbers but I did mention that these computers were quite fast...

What about one of the wonders of the modern age where reality and computers mix? CGI, watching Beowulf it can be hard to believe it's not real never mind that it's all a lot of numbers.
Let's start with something much simpler. Remember Terminator 2? Actually no let's go even simpler. Luxo Jr. The first feature pixar did and still a classic. How on earth do you describe that as a lot of numbers?
I assume you're happy with the concept of Cartesian co-ordinates. You know that thing from maths where a line starts at 3 across 4 up and continues to 7 across 3 up.
Well that's what you're doing with 3d graphics, and instead of line you have triangles. Corner 1 of the tiangle is at 3 across 4 up 2 deep. Corner 2 is at 7 across 3 up 24 deep. Corner 3 is at etc.
Fine you say, that describes a single triangle. That movie had spheres and cones. It had light sources and the ball had patterns on it. The short answer is again that if you use enough triangles then like you can make a football out of hexagons then you can make a sphere out of triangles. If you use enough then you can make any shape. As for the patterns on the ball, well then you just give each triangle a number to represent its colour. Or you have a picture that the rendering engine (the thing that takes this list of triangles and produces a picture) paints around the shape.
Yes it's an awful lot of numbers, yes it's a lot of work to build up a character's face from a collection of spheres cones boxes etc, but now you might have an inkling as to why CGI is so very expensive.

So now you know. Your keyboard is just sending a sequence of numbers to your computer. Your mouse is just sending numbers saying how fast and in what direction it is moving. The webpage is just a whole bunch of numbers saying the letter to use and how it should be displayed. Your webcam is just sending a stream of numbers saying what colour each pixel it sees should be at. That youTube video you watched, again more numbers saying how the pixels on your screen should change. It's all numbers and that's all the computer does; and it's all you need.
Next though we need to look at the numbers themselves because it's never as simple as you'd first hope it to be...

Electronics for Dogs 2 - The very fast idiot

So moving on after those initial thoughts lets jump into what that processor at the heart of every computer is. I mentioned it before, the very fast idiot.
So imagine you have an employee working for you. He's as thick as two short planks but he's eager. Let's assume that he always follows instructions given to him, but don't ask him to make any judgement calls himself because he'll normally get it wrong. So what do you do, this guy is so stupid he can't even use a calculator to add up numbers and struggles to answer the phone correctly.
Never mind you are a thoughtful employer so you decide to write out long lists of instructions for him. It goes something like:

if the phone rings then lift the receiver. Then place the bit with the cord coming out of it next to your mouth and the other end of the receiver next to your right ear.

Ask the person at the other end who they are and who they want to speak to. Write this down on a piece of paper you find here using a pen here. Then replace the paper there and the pen there so you'll be able to find them again. Next consult this diagram for where you might find that that person. Follow the instructions on that diagram then give it to that person.
If that person is nor available consult this diagram provided here to work out who else to ask about it. Once you have done all that wait for the phone to ring again and start from the beginning.

This may sound over the top, but your processor at the heart of your computer is even more stupid than this. Hearing something and writing it down is still after decades of work still a problem for computers. Why? Because they are monumentally stupid (in the traditional numan sense) but they are very fast and very very reliable. It's often said that the only time a computer genuinely makes a mistake is because someone has told it to make that mistake.
So let me strip this down further the average processor can do the following things:

Add one number to another
Copy a value from one place to another.
Test if one value is equal, greater than or smaller to another number. Then "jump" to a different place in the instructions depending on the result.
And, Xor, Or two numbers together (for those of you thinking in decimal this doesn't make much sense. For the following discussion lets assume that it can do a number select that is if I asked for the 3rd digit in 5467 I would report the number 4)
Bit shifts (basically multiply or divide by the base. So in decimal terms the ability to multiply or divide by 10. i.e. take the number and add a zero to it or remove the first digit. Wow 56 * 10 is 560 - clever computer...)

And that's about it. Everything your computer does can be accomplished with those few basic abilities.
Let's go back to our very reliable idiot. You give him a whiteboard with 8 boxes on it labelled A to H. You've given up trying to teach him how to multiply numbers so instead you're going to write him some instructions. You're going to teach him long multiplication, but not tell him you're doing that.
The instructions might go something like this:

When writing a number in a box always replace the value there with the new one.
Write 0 into box H
Write the first number in box A
Write the second number in box B
Write the number 1 in box C
Write the number 1 in box D
* Using the number in box C look at the value in box A and select the number correlating to that
Put this value in box E
Using the number in box D look at the value in box B and select the number correlating to that
Put this value in box F
In the Table go across E rows and down F rows Select that number and place it in box G
add one less than the number of zeroes indicated in box C (this is the bit shift we talked about before)
add this to the number in box H and place the result in box H
Is the number in box C the same as the number of digits (Yes a computer can't do this, but it's a small cheat.)
If it is then you're finished
If it isn't then
add 1 to the number in box C
add 1 to the number in box D
Go back to *

Ok there's a small cheat there but you get the idea of how to build something like multiplication up from smaller steps. If we go back to the first lesson you'll see the opposite of this where we tried to break down something into simple steps. At some point we meet in the middle and where we have to have things like displays and keyboards and mice. But that is a subject for the next post.
What I'm trying to get at here is that if your tame idiot was fast enough he'd be able to multiply perfectly fine just using very simple operations.

Let me just sneak in an extra concept here while you're not looking. Now that I have instructions on how to multiply two numbers I never have to tell the idiot how to do it again. If for example later on I was writing instructions on how to do a balance sheet then I'd just say multiply these two numbers using these instruction. To use computing parlance that would be a subroutine or a function call. But that's a bit unwieldy I'd rather say something like multiply number A by number B. If I was getting really generic and lazy I might prefer to write multiply(A, B). A suitably instructed idiot would know to look for a set of instructions labeled multiply and then apply those instructions to A and B. This way of expression has the advantage that it works for square root, sqrt(A); divide (A, B), or anything else I might want to do with two numbers, print (A); perhaps I'm writing a computer game and I need to draw a circle of diameter A of colour B at location on the screen X and Y, draw_circle (A, B, X, Y)

But that really is a topic for the next post. I keep saying all a computer knows is numbers but how can everything a computer does be just numbers? Another one of those things that's really simple once you get it, but if you don't then it all seems so impenetrable. So lets get that out of the way next time.