0
\$\begingroup\$

I am a beginner in verilog and came across this question-

Given the finite state machine circuit as shown, assume that the D flip-flops are initially reset to zero before the machine begins.

Build this circuit. enter image description here

My Code is -

module top_module (
input clk,
input x,
output z
); 
reg q,q1,q2;
always @(posedge clk)
 begin
 q<= q^x;
 q1<= ~q1 && x;
 q2<= ~q2 || x;
 z=~(q | q1 | q2);
 end 
 endmodule

Suggest me where i am going wrong!

asked Jan 24, 2020 at 9:27
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome! You haven't said what's failed, but try moving z=... outside the always block and change it to assign z = ~(q | q1 | q2); \$\endgroup\$ Commented Jan 24, 2020 at 9:54
  • \$\begingroup\$ It previously showed 43 mismatches but now its working fine. Could you please tell me the reason behind this? \$\endgroup\$ Commented Jan 24, 2020 at 9:57
  • \$\begingroup\$ Briefly, you were mixing combinatorial (continuous) assignment (z=...) and sequential logic (q<=...) in an edge sensitive always block. Continous assignments are either done using assign, or in an always @* (Verilog) or always_comb (SystemVerilog) construct. \$\endgroup\$ Commented Jan 24, 2020 at 12:04

1 Answer 1

2
\$\begingroup\$

From the diagram, z is driven by a combinational logic. In the code, you are trying to drive z using sequential logic inside clockedge. You have to either use z as a wire and drive it using assign statement. Or use z as a reg and drive it inside always@* block.

General coding guideline is to not mix blocking and non-blocking assignments in the same always block.

answered Jan 24, 2020 at 12:39
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.