Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Life would be so much easier if we could just look at the source code. -- Dave Olson


devel / comp.lang.vhdl / Understanding Verilog Code

SubjectAuthor
* Understanding Verilog CodeRupinder Goyal
`- Re: Understanding Verilog CodeMotaz

1
Understanding Verilog Code

<c56d4927-f148-4e5a-981c-f73e5d5dc125n@googlegroups.com>

  copy mid

https://rocksolidbbs.com/devel/article-flat.php?id=26&group=comp.lang.vhdl#26

  copy link   Newsgroups: comp.lang.vhdl
X-Received: by 2002:ac8:111:: with SMTP id e17mr1923229qtg.34.1632808342286;
Mon, 27 Sep 2021 22:52:22 -0700 (PDT)
X-Received: by 2002:a05:6902:1546:: with SMTP id r6mr5376336ybu.268.1632808341782;
Mon, 27 Sep 2021 22:52:21 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.vhdl
Date: Mon, 27 Sep 2021 22:52:21 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=2402:8100:22e0:e917:d16a:88b3:c15e:3e79;
posting-account=HkGJVQoAAAB6xWKS794pjj885gJWKopL
NNTP-Posting-Host: 2402:8100:22e0:e917:d16a:88b3:c15e:3e79
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <c56d4927-f148-4e5a-981c-f73e5d5dc125n@googlegroups.com>
Subject: Understanding Verilog Code
From: rupinderg00@gmail.com (Rupinder Goyal)
Injection-Date: Tue, 28 Sep 2021 05:52:22 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Rupinder Goyal - Tue, 28 Sep 2021 05:52 UTC

Hi, I am new to verilog. I have written a code but the output is not as expected. I want to know how to interpret the code so that I can debug it? Can someone help me out?
The module will get the input bit by bit ( from LSB side) and output a single bit everytime. Output till any particular moment is the 2's complement form of the input read till now.
What I want to do is: Outputbit and nextstate change according to inputbit and prestate and then prestate gets the value of nextstate.
What actually happening is: The code calculates the nextstate , assigns it to prestate and then outputbit is calculated according to the prestate.

Please help me understand the code and how to correct it.

`timescale 1ns / 1ps

module twoComplement(inputBit, clk, reset, outputBit);
input inputBit, clk, reset;
output reg outputBit;
reg preState, nextState;
parameter Carry1 = 1, Carry0 = 0;

always @ (posedge clk)
begin
if (reset)
preState <= Carry1;
else
preState <= nextState;
end

always @(*)
case (preState)
Carry1: begin
outputBit = inputBit ? 1 : 0;
nextState = inputBit ? Carry0 : Carry1;
end
Carry0: begin
outputBit = inputBit ? 0 : 1;
nextState = Carry0;
end
default: begin
outputBit = 0;
nextState = Carry1;
end
endcase
endmodule

TestBench

`timescale 1ns / 1ps

`include "Q1.v"

module testbenchFortwoComplement;
reg inp, reset, clock;
wire out;

twoComplement a(inp, clock, reset, out);

initial begin
inp = 0;
clock = 1;
reset = 1;

#5 reset = 0;
#5 inp = 0;
#5 $display("%b", out);
#5 inp = 0;
#5 $display("%b", out);
#5 inp = 1;
#5 $display("%b", out);
#5 inp = 1;
#5 $display("%b", out);
#5 inp = 1;
#5 $display("%b", out);
#5 inp = 0;
#5 $display("%b", out);
#5 inp = 1;
#5 $display("%b", out);

#10 $finish;
end

always begin
#5 clock = ~clock;
end
endmodule

Re: Understanding Verilog Code

<a3daa536-aafc-4f52-9027-43aa96a904ddn@googlegroups.com>

  copy mid

https://rocksolidbbs.com/devel/article-flat.php?id=27&group=comp.lang.vhdl#27

  copy link   Newsgroups: comp.lang.vhdl
X-Received: by 2002:a37:b045:: with SMTP id z66mr4974681qke.271.1632920092583;
Wed, 29 Sep 2021 05:54:52 -0700 (PDT)
X-Received: by 2002:a5b:f03:: with SMTP id x3mr13710031ybr.546.1632920092204;
Wed, 29 Sep 2021 05:54:52 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.vhdl
Date: Wed, 29 Sep 2021 05:54:51 -0700 (PDT)
In-Reply-To: <c56d4927-f148-4e5a-981c-f73e5d5dc125n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=82.194.215.7; posting-account=jIJeYwoAAABTTnDz1ZV_9Dy052PoBoBM
NNTP-Posting-Host: 82.194.215.7
References: <c56d4927-f148-4e5a-981c-f73e5d5dc125n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a3daa536-aafc-4f52-9027-43aa96a904ddn@googlegroups.com>
Subject: Re: Understanding Verilog Code
From: motaz.theab@gmail.com (Motaz)
Injection-Date: Wed, 29 Sep 2021 12:54:52 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 208
 by: Motaz - Wed, 29 Sep 2021 12:54 UTC

في الثلاثاء، 28 سبتمبر 2021 في تمام الساعة 7:52:24 ص UTC+2، كتب rupin...@gmail.com رسالة نصها:
> Hi, I am new to verilog. I have written a code but the output is not as expected. I want to know how to interpret the code so that I can debug it? Can someone help me out?
> The module will get the input bit by bit ( from LSB side) and output a single bit everytime. Output till any particular moment is the 2's complement form of the input read till now.
> What I want to do is: Outputbit and nextstate change according to inputbit and prestate and then prestate gets the value of nextstate.
> What actually happening is: The code calculates the nextstate , assigns it to prestate and then outputbit is calculated according to the prestate.
>
> Please help me understand the code and how to correct it.
>
>
> `timescale 1ns / 1ps
>
> module twoComplement(inputBit, clk, reset, outputBit);
> input inputBit, clk, reset;
> output reg outputBit;
> reg preState, nextState;
> parameter Carry1 = 1, Carry0 = 0;
>
> always @ (posedge clk)
> begin
> if (reset)
> preState <= Carry1;
> else
> preState <= nextState;
> end
>
> always @(*)
> case (preState)
> Carry1: begin
> outputBit = inputBit ? 1 : 0;
> nextState = inputBit ? Carry0 : Carry1;
> end
> Carry0: begin
> outputBit = inputBit ? 0 : 1;
> nextState = Carry0;
> end
> default: begin
> outputBit = 0;
> nextState = Carry1;
> end
> endcase
> endmodule
>
>
>
> TestBench
>
> `timescale 1ns / 1ps
>
> `include "Q1.v"
>
> module testbenchFortwoComplement;
> reg inp, reset, clock;
> wire out;
>
> twoComplement a(inp, clock, reset, out);
>
> initial begin
> inp = 0;
> clock = 1;
> reset = 1;
>
> #5 reset = 0;
> #5 inp = 0;
> #5 $display("%b", out);
> #5 inp = 0;
> #5 $display("%b", out);
> #5 inp = 1;
> #5 $display("%b", out);
> #5 inp = 1;
> #5 $display("%b", out);
> #5 inp = 1;
> #5 $display("%b", out);
> #5 inp = 0;
> #5 $display("%b", out);
> #5 inp = 1;
> #5 $display("%b", out);
>
>
> #10 $finish;
> end
>
> always begin
> #5 clock = ~clock;
> end
> endmodule
On Tuesday, September 28, 2021 at 7:52:24 AM UTC+2, rupin...@gmail.com wrote:
> Hi, I am new to verilog. I have written a code but the output is not as expected. I want to know how to interpret the code so that I can debug it? Can someone help me out?
> The module will get the input bit by bit ( from LSB side) and output a single bit everytime. Output till any particular moment is the 2's complement form of the input read till now.
> What I want to do is: Outputbit and nextstate change according to inputbit and prestate and then prestate gets the value of nextstate.
> What actually happening is: The code calculates the nextstate , assigns it to prestate and then outputbit is calculated according to the prestate.
>
> Please help me understand the code and how to correct it.
>
>
> `timescale 1ns / 1ps
>
> module twoComplement(inputBit, clk, reset, outputBit);
> input inputBit, clk, reset;
> output reg outputBit;
> reg preState, nextState;
> parameter Carry1 = 1, Carry0 = 0;
>
> always @ (posedge clk)
> begin
> if (reset)
> preState <= Carry1;
> else
> preState <= nextState;
> end
>
> always @(*)
> case (preState)
> Carry1: begin
> outputBit = inputBit ? 1 : 0;
> nextState = inputBit ? Carry0 : Carry1;
> end
> Carry0: begin
> outputBit = inputBit ? 0 : 1;
> nextState = Carry0;
> end
> default: begin
> outputBit = 0;
> nextState = Carry1;
> end
> endcase
> endmodule
>
>
>
> TestBench
>
> `timescale 1ns / 1ps
>
> `include "Q1.v"
>
> module testbenchFortwoComplement;
> reg inp, reset, clock;
> wire out;
>
> twoComplement a(inp, clock, reset, out);
>
> initial begin
> inp = 0;
> clock = 1;
> reset = 1;
>
> #5 reset = 0;
> #5 inp = 0;
> #5 $display("%b", out);
> #5 inp = 0;
> #5 $display("%b", out);
> #5 inp = 1;
> #5 $display("%b", out);
> #5 inp = 1;
> #5 $display("%b", out);
> #5 inp = 1;
> #5 $display("%b", out);
> #5 inp = 0;
> #5 $display("%b", out);
> #5 inp = 1;
> #5 $display("%b", out);
>
>
> #10 $finish;
> end
>
> always begin
> #5 clock = ~clock;
> end
> endmodule

Hi,
I think the issue is caused by the process of updating the preState.
It depends on the clk to update the preState, while the FSM that calculates the outputBit and nextState updates its values each time ANY signal inside of it is modified. Meaning that the calculation process updates its values multiple times before the the preState gets assigned a new value.

always @ (posedge clk)
begin
if (reset)
preState <= Carry1;
else
preState <= nextState;
end

I would recommend update the preState reg in the same process as you calculate the outputs of the design .

- Motaz


devel / comp.lang.vhdl / Understanding Verilog Code

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor