bit-wise negation in systemVerilog [duplicate] - verilog

This question already has an answer here:
What happened to this simple verilog ~^ operator?
(1 answer)
Closed 2 years ago.
In the code below, I was expecting "b" to be 2'b00 since "a" has fewer bits than "b" and they are unsigned wire. However, in the simulating result, "b" is 2'b10.
wire a;
wire [1:0]b;
assign a = 1'b1;
assign b = ~a;
I have tried to set "b" to width 3, and this time "b" is 3'b110.
wire a;
wire [2:0]b;
assign a = 1'b1;
assign b = ~a;
At my understanding, if RHS has fewer bits than LHS, the unused bits of the LHS should be 0s. Can anyone please explain why this is happening?

In the 1st case, a is 1 bit wide, but the LHS (b) is 2 bits wide. The assignment to b forces all values to be the maximum width (2). Before the bitwise negation, a is left-extended with 0's, becoming 2'b01. And, ~(2'b01) is 2'b10.
The same is true when b is 3 bits wide.
Refer to IEEE Std 1800-2017, section 11.6.2 Example of expression bit-length problem.

Related

2 consecutive nonblocking assignments

Can someone explain what it means to assign two consecutive nonblocking assignments in a sequential always block?
For example:
always #(posedge clk) begin
a <= b <= c;
end
That can be more clearly coded as:
a <= (c >= b);
a is assigned the value of the expression "c is greater than or equal to b".
The 1st <= is the nonblocking assignment operator, whereas, the 2nd is the comparison operator.
That is not two consecutive non-blocking assignment.
That is a <= (b<=c);
b<=c evaluates to true (1) or false (0) and that value is assigned to a.
two consecutive non-blocking assignments is not a legal Verilog syntax

signedness vs bit size

Given example below, with the simulators I've tried, the value of d2 is created by sign extending 4'sd2 - i1. From, my reading of the standard (1800-2012) I expect 4'sd2 - i1 to be treated as unsigned and zero extended in the same way as for d1.
What am I missing? Does the unsignedness of i1 somehow change as it is being widened from 2 to 4 bits? Thanks.
module sign_test(input clk, input /*unsigned*/ [1:0] i1,
output reg [7:0] d1, output reg [7:0] d2);
always #(posedge clk) begin
d1 = $unsigned(4'sd2 - i1); // this gets zero extended; the only possible values for 'd1' are 0, 1, 2 and 0x0f
d2 = 4'sd2 - i1; // this gets sign extended; the only possible values for 'd2' are 0, 1, 2 and 0xff
// but 11.8.1 says "4'sd2 - i1" should be unsigned:
// "-- For non-self-determined operands, the following rules apply:
// [...]
// -- If any operand is unsigned, the result is unsigned,
// regardless of the operator."
// and 'i1' is unsigned by default (adding "unsigned"
// explicitly doesn't change the behaviour).
end
endmodule
I think the reason for the difference is not to do with whether each expression is signed or unsigned, but instead to do with the number of bits Verilog uses to do the calculation.
This is a context-determined expression:
d2 = 4'sd2 - i1;
That means Verilog uses the maximum of all the widths involved (the widths of d2, 4'sd2 and i1, which is 8 (the width of d2). As you say unsigned arithmetic is done. With a value of 2'b11 for i1:
d2 = 00000010 - 00000011
which is
11111111
With
d1 = $unsigned(4'sd2 - i1);
however, the width of d1 is not taken into account, because of the call to the $unsigned system function. This makes the expression 4'sd2 - i1 a self-determined expression and so the bit width used for the calculation is 4 - the maximum of the widths of 4'sd2 and i1. So
0010 - 0011
is
1111
This is then zero-padded to
00001111
https://www.edaplayground.com/x/duY

Verilog 8 bit comparator from 2 bit comparators Where do I start?

Hello I am new to the programming world. I have a project for my ece 171 class that I have been trying to figure out on my own but cannot. I had a project before where I needed to design a 2 bit comparator which I did design successfully. Now I am supposed to take that 2 bit comparator and alter it as well as write some more verilog code that will compare 8 bits with each other using my expanded 2 bit program. My problem is that I don't know where to even start. My professor said always start with a black box and then truth table. My initial instinct is to create a 16 bit truth table which I know is wrong. But I don't even know where to start? Any suggestions? I feel like if I could just be shown where the path starts that it would start coming to me.
My 2-bit comparator code below.
//two 2-bit input Values Comparer Behavioral Dataflow
//Defining Module and Parameters
module BcompareA(A, B, GT, EQU, LT);
input [1:0] A;
input [1:0] B;
output GT;//when a is greater than b
output EQU;//when a is equal to b
output LT;//when a is less than b
//Boolean Output Descriptions
assign GT = A[1]&~B[1]
| A[1]&A[0]&~B[0]
| A[0]&~B[1]&~B[0]
assign EQU = ~A[1]&~A[0]&~B[1]&~B[0]
| ~A[1]&A[0]&~B[1]&B[0]
| A[1]&A[0]&B[1]&B[0]
| A[1]&~A[0]&B[1]&~B[0]
assign LT = ~A[1]&B[1]
| ~A[1]&~A[0]&B[0]
| ~A[0]&B[1]&B[0]
endmodule
You should use yours 2 bit comparator to compare pairs of bits [7:6], [5,4],...
Lets start with EQU output - it is the easiest. A and B would be equal if all its pairs would be equal. So logic is simple.
Make a notice that A would be greater if its highest bits are greater (GT_76 == 1 ) or highest bits are equal( EQU_76 == 1 ) and next pair would be greater ( GT_54 = 1 ) or etc.
Finally A would be less if it is not greater and not equal.

Verilog: Aligning valid and invalid bytes from a dynamic input

I'm trying to design a system that takes an 8 byte data input and an 8 bit valid input every clock cycle where each bit on the valid input either validates or invalidates a data byte.
input wire [63:0] d_in;
input wire [7:0] v_in;
The program should process d_in aligning all the valid bytes as follows.
(where B is a valid byte and X is an invalid byte)
Instance 1:
d_in = B1 B2 X X B3 X B4 B5
d_out = B1 B2 B3 B4 B5 X X X
Instance 2:
d_in = X B1 B2 B3 X B4 B5 B6
d_out = B1 B2 B3 B4 B5 B6 X X
I've mainly worked with algorithms before where all bit manipulation was the same every iteration e.g.assign d_out [7:0] = d_in [15:8]; but the fact that the quantity and order of valid bytes can change with every data input means this strategy cannot be used.
My question:
Is there a way to realise this function in using Verilog or VHDL? If so can someone point me towards a high level solution or some relevant reading so I can understand this concept better. I think if I understood at a high level then I'd be able to take a stab a coding it but currently I'm not even sure what I need to be coding.
Thanks
Zach
Since you asked for high level I will give a pseudocode example of something that might work, or at least get you going.
d_out = '0; //Assuming that the X bytes can be set to zero in the output.
bytes = 0;
for i in range(8)
if v_in[i]
d_out[bytes*8 +: 8] = d_in[i*8 +: 8] //Note the +: notation which is not pseudo, but verilog.
bytes++
Now perform this sequential code in an always block and you should be set.
Note: How the synthesized result from this will look is not entierly clear to me, but i suspect it will generate quite a bit of hardware.
I have something similar but not quite.
Input data into a FIFO, pre-calculate a byte-enable with the FIFO entries.
On the output side, read the the byte enable portions and use it to shift out bytes. So there are only eight conditions to satisfy for the byte enable...
1 byte, byteEn(0 downto 1) = "10", shift left 1 byte
2 bytes, byteEn(0 downto 2) = "110", shift left 2 bytes
3 bytes, byteEn(0 downto 3) = "1110", shift left 3 bytes
...and so on...
As you shift, read in the next word using the FIFOs read enable.
Note you will need to take care of when the FIFO is empty but not halt the pipeline so data already present continues to be shifted out.
Not sure how complicated it will be as I have glossed over it a bit.

Verilog Example Wrong? Arbiter Code MSB Finder

In my book as an example it has:
wire [n-1:0] c = {1'b1,(~r[n-1:1] & c[n-1:1])};
If n=4 then c is 4 bits but the concatenation however makes 5 bits! 0.o
)r is there something I don't understand about Verilog here, that maybe the case.
The concatenation is infact just 4 bits. r[n-1:1] is an n-1 bit value which in this case is 3. Note that r[n-1:1] means the bits starting from 2nd bit (index = 1) to nth bit ( index = n-1). So a 3 bit value concatenated with 1b'1 gives a 4 bit value.

Resources