shaiko
Advanced Member level 5
- Joined
- Aug 20, 2011
- Messages
- 2,644
- Helped
- 303
- Reputation
- 608
- Reaction score
- 297
- Trophy points
- 1,363
- Activity points
- 18,302
signal a : signed ( 7 downto 0 ) := "00000000" ;
signal b : signed ( 7 downto 0 ) := "00000001" ;
signal c : signed ( 7 downto 0 ) ;
c <= a - b ;
Hello,
signals a,b,c are defined as follows:
Code:signal a : signed ( 7 downto 0 ) := "00000000" ; signal b : signed ( 7 downto 0 ) := "00000001" ; signal c : signed ( 7 downto 0 ) ;
in my code I write:
Because I'm using signed numbers, I expect the result to beCode:c <= a - b ;
minus 1. Which in signed binary is: 10000001
However the result is: 11111111
What did I do wrong?
Like Tricky said the output is 2s complement. All you need to do is binary addition of 32 consecutive output samples to compute the algebraic sum.My FPGA is communicating with an ADC over a serial bus. The ADC supports negative voltage readings.
https://www.analog.com/static/imported-files/data_sheets/AD7687.pdf
Figure 26 at page 14 suggests that the device uses the leftmost bit as the sign bit.
I'm required to calculate the algebraic sum of 32 consecutive readings.
As you explained - VHDL signed uses 2's complement instead of the signed bit method...
Any suggestions as to how approach the problem?
Thanks,That looks like standard 2s compliment to me
-5V = 8000
+5V = 7FFF
Please give an example of how to do the sign extension...Oh, and don't forget to sign extend the samples, when you perform the accumulation.
That's why I mentioned you need to sign extend the input samples and run the accumulator with more bits.shaiko said:What will happen in the event of an overflow ?
You should do the following:shaiko said:Please give an example of how to do the sign extension...
accum <= accum + (samp[15] & samp[15] & samp[15] & samp[15] & samp[15] & samp[15 downto 0]);
This suggests that if the leftmost bit is '1' then the number is negative...check if the number is negative or positive by looking at the sign bit
you forgot to sign extend. The resize function does sign extension:
my_12bit_output <= resize(some_8bit_0, 12) + resize(some_8bit_1, 12);
100:
(-1)*2^2 + (0)*2^1 + (0)*2^0 = -4 + 0 + 0 = -4
now sign extend
1100:
(-1)*2^3 + (1)*2^2 + (0)*2^1 + (0)*2^0 = -8 + 4 + 0 + 0= -4
sign extending the value doesn't affect the result
An overflow implies you have a design error that should not exist. You know the range of the input and the number of inputs, the range of that summation is a computable constant.What will happen in the event of an overflow ?
Personally, I would convert the sample to an integer and add the sample to the accumulated sum like thisPlease give an example of how to do the sign extension...
signal Accum: integer -32*32768 to +32*32767;
...
Accum <= Accum + to_integer(signed(Sample_Input));
Accum_slv <= std_logic_vector(to_signed(Accum, Accum_slv'length));
You pointed me to the correct direction...How about nbit = ceil(log2)?
signal signed_a : signed ( 7 downto 0 ) := "00001111" ;
signal unsigned_a : unsigned ( 7 downto 0 ) := "00001111" ;
signal signed_b : signed ( 7 downto 0 ) := "10001111" ;
signal unsigned_b : unsigned ( 7 downto 0 ) := "10001111" ;
signal signed_c : signed ( 7 downto 0 ) ;
signal unsigned_c : unsigned ( 7 downto 0 ) ;
signed_c <= signed_a + signed_b ;
unsigned_c <= unsigned_a + unsigned_b ;
You pointed me to the correct direction...
But if n is defined as the number of additions (the number of times you sent the '+' sign) then the formula should be nbit = ceil(log2(n+1)).
Correct?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?