0

In Verilog code

case ({Q[0], Q_1})
  2'b0_1 :begin 
    A<=sum[7];        Q<=sum;        Q_1<=Q;
  end
  2'b1_0 : begin 
    A<=difference[7]; Q<=difference; Q_1<=Q;
  end
  default: begin 
    A<=A[7];          Q<=A;          Q_1<=Q;
  end
endcase

is above code is same as below code

case ({Q[0], Q_1})
  2'b0_1 : {A, Q, Q_1} <= {sum[7], sum, Q};
  2'b1_0 : {A, Q, Q_1} <= {difference[7], difference, Q};
  default: {A, Q, Q_1} <= {A[7], A, Q};
endcase

If yes then why i am getting different result?

Edit:-A, Q, sum and difference are all 8-bit values and Q_1 is a 1-bit value.

Morgan
  • 19,934
  • 8
  • 58
  • 84
Mukesh Gupta
  • 1,373
  • 3
  • 17
  • 42
  • As an aside - never write code like `A<=sum[7]`, or `Q_1<=Q` - write defensively, say exactly what you expect. What results did you actually want? Do you understand resizing and sign-propagation? Verilog provides no compile-time checks for statements like this (and they're illegal in VHDL). – EML Nov 09 '15 at 12:50

2 Answers2

2

No, these are not the same. The concatenation operator ({ ... }) allows you to create vectors from several different signals, allowing you to both use these vectors and assign to these vectors, resulting in the assignment of the component signals will the appropriate bits from the result. From your previous question (Please Explain these verilog code?), I see that A, Q, sum and difference are all 8-bit values and Q_1 is a 1-bit value. Lets examine the first assignment (noting that the other three work the same way):

{A, Q, Q_1} <= {sum[7], sum, Q};

If we look at the right-hand side, we can see that the result of the concatenation is a 17-bit vector, as sum[7] is 1 bit (the MSb of sum), sum is 8 bits, and Q is 8 bits (1 + 8 + 8 = 17). Lets say sum = 8'b10100101 and Q = 8'b00110110, what would {sum[7], sum, Q} look like? Well, its the concatenation of the values from sum and Q so it would be 17'b1_10100101_00110110, the first bit coming from sum[7], the next 8 bits from sum and the final 8 bits from Q.

Now we have to assign this 17-bit value to the left hand side. On the left, we have {A, Q, Q_1}, which is also 17 bits (A is 8 bits, Q is 8 bits and Q_1 is 1 bit). However, we have to assign the bits from our 17-bit value we got above to the proper signals that make up this new 17-bit vector, that means the 8 most significant bits go into A, the next 8 bits go into Q and the least significant bit go into Q_1. So, if we take our value from above (17'b1_10100101_00110110), and split it up this way (17'b11010010_10011011_0), we see A = 8'b11010010, Q = 8'b10011011 and Q_1 = 1'b0. Thus, this is not the same as assigning A = sum[7], Q = sum and Q_1 = Q (this would result in A = 8'b00000001, Q = 8'b10100101, Q_1 = 1'b0, with many bits of Q being lost and A having 7 extra bits).

However, this doesnt mean we cant split up the left-hand side concatenation, it would just look like this:

A <= {sum[7], sum[7:1]};
Q <= {sum[0], Q[7:1]};
Q_1 <= Q[0];
Community
  • 1
  • 1
Unn
  • 4,775
  • 18
  • 30
  • @Unn. In the last part. I think it must be like `A <= {A[7:1], sum[7]}` and `Q <= sum`. Actually I didn't get that part fully. Please correct me if I am wrong. – sharvil111 Nov 07 '15 at 02:40
  • @sharvil111 `A` doesnt appear in the rhs of the assignment, so `A` doesnt take on any part of `A`, note also that the concat operator has MSb first, so `sum[7]` is the MSb in the example I use while `Q[0]` (last bit of `Q`) is the LSb. So, Im pretty sure my split is correct. – Unn Nov 07 '15 at 22:14
  • @Unn okay, now I got. You are talking about the second snippet in the OP's code, while I meant about the first snippet. – sharvil111 Nov 08 '15 at 01:22
0

Yes, they are same. For example try this small code and check, the output is same :

module test;

wire A,B,C;

reg p,q,r;

initial 
begin
 p=1; q=1; r=0;
end

assign  {A,B,C} = {p,q,r};

initial   #1 $display("%b %b %b",A,B,C); 

endmodule

In general if you want to understand concatenation operator, you can refer here

Edit : I have assumed A and p , B and q, C and r of same length.

Community
  • 1
  • 1
ssgr
  • 393
  • 7
  • 21
  • Note that they are not the same as `sum`, `difference` and `A` are at least 8 bits long, see my answer for a complete explanation. – Unn Nov 06 '15 at 10:01
  • Actually I have assumed that they are of same length. I have updated my assumption in the answer. – ssgr Nov 06 '15 at 10:07