Can't understand Verilog arithmetics

Guest #4493336
Hi,

I am trying to understand some Verilog source code, but I am confused 
about the following
1
reg   [29-PSR-1: 0] kp_reg        ;
2
wire  [    29-1: 0] kp_mult       ;
3

4
always @(posedge clk_i) begin
5
   if (rstn_i == 1'b0) begin
6
      kp_reg  <= {29-PSR{1'b0}};
7
   end
8
   else begin
9
      kp_reg <= kp_mult[29-1:PSR] ;
10
   end
11
end
12

13
assign kp_mult = $signed(error) * $signed(set_kp_i);
 where PSR is a paramter equal = 12.

So, kp_mult keeps the value of the product. But then it is truncated and 
only MSB are assigned to kp_reg. I am wondering what this is happened? 
My guess is that it is related to the fixed-point arithmetics and PSR 
defines the position of the binary point but I am not sure.
Guest #4493790
You are right with your assumption!

Multiplications tends to blow up the bit widths of your signals because 
the resulting bit width is always the sum of both input bit widths. 
That's why you often have to truncate the result after a multiplication. 
And that's exactly what happens here.

When you consider a number as fixed point you can easily cut off some of 
the fractional bits when the required accuracy is not so high.
Of course rounding would be better than truncating, but this is more 
complicated and requires more logic resources.
Guest #4493839
Hi Andi,

I would agree with you if kp_mult is used for the accumulation. However, 
it just a single multiplication. Consider for example that kp_mult 
equals to 3 in decimal, kp_mult = 0000 0000 0000 0000 0000 0000 0000 11. 
If we move only 17 MSB to kp_reg it will equal to zero. And it will be 
zero until really big number (2^11). This is why, I am not sure that 
just truncation is a reasonable explanation.

Apart from it, error and set_kp_i are integer values.

Reply

A question of your own? This thread is over 10 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [vhdl]VHDL-Code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.