Is that Logic There? Or are You Just Glad to See Me?
by, 05-05-2009 at 12:00 AM (1993 Views)
Well, as promised, we are doing a little detour into the realm of the known. Yes, my little sackpeople, we are going to go over the basics of logic, why it is important, and why everyone seems to be doing it these days.
I mean, it MUST be important if everyone is doing it, right?
So lets start off with a little problem.
Hi, I'm Joe Sackperson and I'm building my very own, wow, gosh, SUPER level that everyone is going to love and and I'll get a billion zillion hearts for.
Except I have this ONE itsy bitsy, teeny, tiny, "oh, it's nothing", problem -- I have a puzzle that I can't figure out how to do the logic for (and if you think THAT is grammatically questionable, you haven't been reading these blogs!).
The Problem: I have two slots. If someone puts key A in Slot A OR key B in Slot B then I want to give them BUNCHES of prize bubbles (HOORAY!). BUT if they put BOTH key A in slot A AND key B in Slot B then I want a large brick to fall on them.
Didn't say it would be a good puzzle.
The OR Gate
Looking back at our "Problem" we can pick out some key words (you might have noticed that they had BOLD PRINT). This is your first hint what you need.
The OR gate is a wonderful little device. When N00BS complain "why can't I hook up more than one wire to a piston?" well, guess what? With an OR gate you can!
Here is the logic diagram for an OR gate:
Imagine that A and B are switch inputs. Output goes to your piston. Now, if you turn on A, the piston will extend. If you turn on B, the piston will extend (turn on).Code:A B Output 0 0 0 0 1 1 1 0 1 1 1 1
Wow! That makes things a lot easier!
Look at our problem. See where it has "If they put key A in Slot A OR key B in Slot B then I want to give them BUNCHES of prize bubbles"? To do this in LBP hardware we would have something like the following:
If Switch A or Switch B is turned on, then the prize emitter is turned on. If both are turned off, then the prize emitter is turned off.
The key thing to remember about the OR gate is that you use it when you want one or more things to turn something on.
Great! We are almost halfway there, right?
The AND Gate
Looking back at our problem we notice that we also have ANOTHER word in bold (convenient that they do that, eh?). That is our hint that we PROBABLY want to use an AND gate.
AND gates are picky little devices. They won't output a signal until ALL inputs have a signal. Much like a good driver won't start the car until all the doors are closed, seat belts are buckled, and someone has a map just in case the driver gets lost.
Here is the logic diagram for an AND gate:
Notice how NOTHING is output until all inputs have a one? That's the brilliance of an AND gate.Code:A B Output 0 0 0 0 1 0 1 0 0 1 1 1
Back to our problem, remember we want to drop a giant brick on the unsuspecting sack person's head if key A is in slot A and key B is in slot B? This is how it would look with LBP hardware:
So beware, you turn A on AND B on and that brick is falling on your head!
Okay, Let's Solve the Problem
Great, now we have the two pieces of the puzzle that we need.
- They get prize bubbles if they put either key A OR key B in the appropriate slot
- Or if they put BOTH keys in they get a brick on the noggin'
So much for "E for Everyone", eh?
But, lets move on and draw this up as a proper little "circuit".
A True Logic Circuit
So now we have the basic pieces all we have to do is jam 'em together, right? Well, actually for this simple case, yes.
There we are: our first bit of logic that does something ... interesting.
Now some of you will go "Hey, if A is turned on then B is turned on, you will get prize bubbles and then get squished!"
Oh, dear, our first bug!
Fortunately, we can solve this by making sure our gates move slowly (a second or two). "But surely that's a hack?!?!" you might wonder. It all depends. Personally, I would argue that a small delay is cheaper in thermo than slapping on more logic.
So there you go: AND and OR gates!
What About XOR?
Ah, my friend, that is the most ignored gate of all. The XOR is a WONDERFUL device. Imagine you want something to move left to right when a button isn't pressed, but right to left when the button IS pressed. This is easy to do with an XOR (exclusive or) gate.
Here is the logic diagram for an XOR gate:
Notice that the output is one IF and ONLY IF there is only ONE input set high. Otherwise it is zero.Code:A B Output 0 0 0 0 1 1 1 0 1 1 1 0
Now here is the cool part. Imagine that A is a switch and B is the output from some device. When A is zero and B is zero, the output is zero. BUT if A is a 1 and B is zero, then the output is a 1. You can now change the direction of your signal using a pushbutton!
How cool is that?
But let's take another example: imagine a puzzle where if A is turned on the platform explodes. If B is turned on, the platform explodes. BUT if A AND B are turned on simultaneously, the platform does nothing. You can't do this with an AND gate (notice: NO BOLDFACE).
Here would be the diagram:
Notice that the logic diagram says the bomb won't go off if A and B remain where they are OR if A and B are removed at the same time. If only ONE is removed, KABOOOM!!! And sackboy turns into glowing red bits (hey, how is that "E for Everyone?!?").
We are Done?!?!
Yup. That's a very short tutorial.
Print it off. Read it multiple times. Put it under your pillow at night (although it won't help and must make it hard to sleep).
EVENTUALLY we we move on to the monostable multivibrator (oooooooo, it sounds so dirty!). Or as engineers call them, one-shots.
But I think it would be nice to review the RS Latch and how it can be used in place of the P-switch or permanent switch to give your logic a nice, resettable feel. Always good when sack boy bites the dust!
And if he plays in MY little scenario as above, he is going to get SQUISHED or KABOOM'ed!