Just from inspecting the logic diagram, I don't see how the original FSM can ever get out of its initial state {Y,Q1,Q0}=3'b000
, regardless of its inputs. Are you sure 3'b000 is the correct initial/reset state?
When Q1
is 0
, the Ain
input is effectively disabled, because anything AND 0 is 0.
Similarly, when Q0==0
the Bin
input is disabled, and when {Q1,Q0}==2'b00
the Cin input is disabled. So when the initial state sets Y, Q1, and Q0 all 0, the circuit becomes insensitive to all three of its inputs.
One other thing: the test bench would be more useful to you, if it tested various combinations of the inputs.
Your problem lies in that you are describing an asynchronous circuit which requires its previous state.
// output
always @ (*) begin
if (in) number = cur_state;
end
When in
is high, all is well - number
is assigned to the value of cur_state
. However, what happens when in
is low?
When in
is low, number
does not have a new value specified (i.e. via else
) which means that you are inferring that it must hold its value. Whenever a combinational circuit is asked to hold its value, you get a latch.
The way to prevent latches then is to ensure that in every combinationally inferred logic, you fully define the assigned value to never require itself. You can do this in one of two ways.
First, if you don't care about the value when in
is low, then you can assign some constant:
// output
always @ (*) begin
if (in) begin
number = cur_state; //If in is high, output the current state
end else begin
number = 4'b0000; //If in is low, output is don't care so avoid latch by assigning value
end
end
Second, if you need the output to hold its state, then you need to make it a clocked process:
// output
always @ (posedge clock) begin
if (in) number = cur_state;
end
Now that it is synchronous, you can have the output hold its state because you are now inferring a flip-flop. The down side to this is you have a 1 cycle latency from when you change the in
signal to when the number
value updates.
Best Answer
Since you tagged your question with verilog, I'll assume that you're really looking for an HDL solution targeted at an FPGA. In this case, using a single programmable divider makes a whole lot more sense than having multiple dividers followed by a multiplexer. Something like this:
Define
X
,Y
,Z
, etc. to give you the clock division ratios you're looking for, keeping in mind that the output clock is further divided by 2 to produce a square wave.This solution will never produce a bad clock pulse, even if the
select
input changes in the middle of an output cycle.Addressing your secondary problem of controlling the select lines, the key here is to bring the two debounced pushbutton signals into the same clock domain as the rest of the logic. Something like this:
Connect the
count
output of this module to theselect
input of the previous module.