verilog code correction???

Status
Not open for further replies.

sotomie

Newbie level 6
Joined
Apr 5, 2013
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,376
can anyone please tell me if there is any logical error is this always block,its for fibonacci series ,i'm getting my results right but i was just curious if i've done something which isn't correct like using two 'if's?

Code Verilog - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
always@(posedge clock)
  begin
      if(n==0)
      current=1;
      else
      begin
      temp=current;
      current=current+previous;
      previous=temp;
      end
      if(current<previous)
      begin
            previous=0;
            n=0;
            current=1;
      end
    n=n+1;
   end   
endmodule

 
Last edited by a moderator:

No logical error, but bad style. Synchronous always blocks should use non-blocking statements for variable assignments.
 
Code:
always@(posedge clock)
  begin
      if(n==0)
      current<=1;
      else
      begin
      temp=current;
      current=current+previous;
      previous=temp;
      end
      if(current<previous)
      begin
            previous<=0;
            n<=0;
            current<=1;
      end
    n<=n+1;
   end   
endmodule
better?
 

No, now you have mixed blocking and non-blocking statements for the same variables, which isn't allowed.
 

can you modify it ? I'm new to verilog.And if i am changing all the blocking assignments to non-blocking results are not right because of this part i think
Code:
temp=current;
      current=current+previous;
      previous=temp;
.Thankyou
 

Yes, the code must be differently arranged when using non-blocking statements.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…