240 likes | 251 Views
Discover the concept of Finite State Machines (FSMs) in computer science including sequential circuits, combinational circuits, state transitions, Moore and Mealy Machines. Learn how to implement FSMs, study examples, counters, generators, controllers, and their practical applications in multiplication and division. Unpack the relationships between inputs, states, and outputs through tables and state diagrams. Explore the possibilities of FSMs for problem-solving, decomposition, and software development.
E N D
FSMs,Multiplication, and Division CS/COE 0447 Jarrett Billingsley
Class announcements • fhghhbhlhbll CS447
Finite State Machines CS447
The circle of life • when you combine combinational and sequential circuits… • fun things happen. …sequential circuits let us remember the results… combinational circuits let us do any computation… 74 …and connecting them in a loop lets us do multi-step computations. you might call those "programs." CS447
What's an FSM? (animated) • a Finite State Machine is a machine with a finite number of states. • :^) lol jkif you put this answer on the exam I will fail you • it's a way of thinking about a sequential process where: the system has a state(memory) there are inputs which can change the state there are outputs based on the state (and maybe on the inputs) should panic? bears seen: 0 bears seen: 1 bears seen: 2 bears seen: 3 yeah ok nah CS447
A very simple example • what states can a ceiling fan be in? • high, medium, low, and off • what are the input and output? • the chain and the motor • when you pull the chain, it changes state pull pull pull off high med low pull CS447
Missing some arrows • at any point in time, which of 2 choices can the input (chain) be? • pulled or not-pulled • when you don't pull the chain, what happens? pull pull pull off high med low no pull no pull no pull no pull pull this is the full state transition diagram every state should be able to handle every input! CS447
Table-a-fying it • we can represent this diagram with a table: off hi pull pull pull off high med low hi no pull no pull no pull med for the input, let's say: 0 means not pulling 1 means pulling med no pull pull low low off CS447
What about the outputs? • our fan controller has to control the motor after all • we can make a table showing the output(s) for each state note that the input isn't needed at all in this case. this is a Moore Machine. Mealy Machineslet the output depend on the state and inputs. the outputs only depend on the current state! CS447
Making a circuit out of it • this is a sequential circuit –the state changes over time • but the state transition and output tables are combinational • here's the general organization of any Moore FSM circuit: state feeds back into transition logic D Q outputs based on state outputs inputs outputlogic transitionlogic statememory Mealy machines just have this extra wire. CS447
Are flip-flops FSMs? • sure. (people always ask this.) • they're kinda like the simplest possible FSMs • they are to sequential logic what gates are to combinational logic: • the tiniest piece that you can build anything else out of. CS447
Kinds of FSMs CS447
Counters • counters just count how many of something they've "seen" • usually the states form a ring (like the ceiling fan) • sometimes they make a line: 0 bears 1 bear 2 bears ≥3 bears bear or cat in this machine, there's no way to "get back" to an earlier state. bear bear bear cat cat cat there are also as many states as numbers you want to represent… CS447
Generators • sometimes you have no inputs • other than some signal to tell you to go to the next step (what's that signal called?) • these are useful for generating a sequence of values. 0 1 2 3 4 5 6 7 8 9 this is kind of a generator-counter combo. I've used these for sequencing steps in a more complex process (another FSM!). CS447
Controllers • let's say you want to make a simple vending machine • it accepts nickels and dimes and lets you buy 25¢ items nickel nickel nickel nickel nickel dime dime dime dime dime 0¢ 5¢ 10¢ 15¢ 20¢ 25¢ 30¢ dispense give nickel these arrows on the right are unconditional transitions CS447
What's the limit? • you can make some pretty complex things with FSMs! • and you can embed FSMs inside other FSMs, or chain them, or… • they can be a helpful tool for decomposing larger problems into smaller, simpler ones that are easy to characterize and build. • FSMs come up in software, too! • you can do a lot of stuff with them, but not anything. • FSMs are not Turing-complete. (take CS1511) • but if you add more memory… • well. you'll see. ;)))))))))))))))))))))) CS447
Remember this? • we used this algorithm to do multiplication: how might we make a circuit to count the steps? what do we use to make a decision? what do we use to store values? for(n bits in multiplier) { if((multiplier & 1) != 0) product += multiplicand multiplicand <<= 1 multiplier >>= 1 } CS447
A very common pattern • have a look at simple_counter.circ this could really be anything though. like… shifting? this is a very simple FSM. every clock tick, it adds 1 to the value in the register. D Q + this is the hardware equivalent of "x = x + 1". 1 but we can reuse this pattern to come up with several parts of our multiplier. CS447
So… what are we gonna need • we've got 3 variables (+1 for the loop counter maybe?) • we're doing 2 shifts and (maybe) an add on each loop kindaaa…? D D D D Q Q Q Q how do we decide when to add to the product? + << >> + product 1 1 1 multiplicand how do we know when to start and stop? multiplier loop step CS447
Fast multiplication as a circuit • remember there were two phases: • calculating the partial products • summing them • calculating the partial products is easy • it's just shifts and ANDs • in hardware, shifting can be done by just rerouting wires! • summing the partial products is easy • we use a binary tree of adders! • the product just pops out the end. • of course, this uses a lot of silicon… • have a look at fast_mult_4x4.circ Fun Stuff + + + + Partial Products + + + CS447
Deja vu • We used this division algorithm: divisor <<= n (bits in dividend) remainder = dividend quotient = 0 for(n bits in dividend) { divisor >>= 1 if(divisor > remainder) { append 0 to quotient } else { remainder -= divisor append 1 to quotient } } another for loop… another if-else… subtraction and shifting… CS447
Feels pretty familiar • we've got 3 variables (+1 for the loop counter maybe?) • we're doing 2 shifts and (maybe) a subtract on each loop kindaaa…? D D D D Q Q Q Q how do we "append a 0 or 1" to the quotient? + >> << - remainder 1 1 1 divisor how do we know when to start and stop? quotient loop step CS447
Their similarity is no coincidence • in MIPS, both mul and div use the HI and LO registers • this is because in the original design, both algorithms used the same registers, just with different controllers • they do some fancy stuff to save bits • check out the book – it goes into way too much detail on this, but it's kinda neat • your lab will do something similar! • hohohoho! CS447