EmbDev.net

Forum: FPGA, VHDL & Verilog Arithmetic operator (Sub/Add/+1/-1) N bits Cascaded


von YouseKalack (Guest)


Attached files:

Rate this post
useful
not useful
Hello i've got a matter with my simulation, the result of the sub 
operation is not the result expected.

My code :

I) The dataflow code to describe cell.
1
 
2
library ieee;
3
use ieee.std_logic_1164.all;
4
use ieee.std_logic_unsigned.all;
5
use ieee.numeric_std.all;
6
7
entity cell is 
8
port(Ai,Bi,Ci : in std_logic;
9
     S : in std_logic_vector (1 downto 0);
10
     Co,Go : out std_logic);
11
end cell;
12
13
architecture dflow of cell is
14
signal oper : std_logic_vector(1 downto 0);
15
signal tabc : std_logic_vector(2 downto 0);
16
begin
17
with tabc select oper <=
18
                          '0' & Ai + Bi  when "001",
19
                          '0' & Ai + Bi when "010" ,
20
                          '0' & Ai - '1' when "011" ,
21
                    '0' & Ai + '1' when "100" ,
22
                          '0' & Ai - Bi when "101" ,
23
                          '0' & Ai + Bi + '1' when "110",
24
                          '0' & Ai when others;
25
26
Co <= oper(1);
27
Go <= oper(0);
28
tabc <= Ci&S;
29
end architecture;

II) The structural code to link 4 cells to implement the arithmetic 
operator
1
 
2
library ieee;
3
use ieee.std_logic_1164.all,ieee.std_logic_unsigned.all;
4
5
6
entity operator is
7
port(A,B : in std_logic_vector(3 downto 0);
8
     S : in std_logic_vector(1 downto 0);
9
     cin : in std_logic;
10
     G : out std_logic_vector(3 downto 0);
11
     cout : out std_logic);
12
end entity;
13
14
architecture rtl of operator is
15
component cell is 
16
port(Ai,Bi,Ci : in std_logic;
17
     S : in std_logic_vector (1 downto 0);
18
     Co,Go : out std_logic);
19
end component;
20
signal C : std_logic_vector(3 downto 1);
21
begin
22
cell0 : cell port map(Ai=>A(0),Bi=>B(0),Ci=>cin,S=>S,Co =>C(1),Go => G(0));
23
cell1 : cell port map(Ai=>A(1),Bi=>B(1),Ci=>C(1),S=>S,Co=>C(2),Go => G(1));
24
cell2 : cell port map(Ai=>A(2),Bi=>B(2),Ci=>C(2),S=>S,Co=>C(3),Go => G(2));
25
cell3 : cell port map(Ai=>A(3),Bi=>B(3),Ci=>C(3),S=>S,Co =>cout,Go =>G(3));
26
end architecture;

: Edited by Moderator
von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
YouseKalack wrote:
> the result of the sub operation is not the result expected.
What do you expect and wht do you get instead?

> My code
Pls use the [vhdl] tags further on to wrap your code!
See the "Formatting options" above each edit box...

> use ieee.std_logic_unsigned.all;
> use ieee.numeric_std.all;
Never ever both together!
Otherwise you will encounter strange problems due to multiple type 
definitions.

: Edited by Moderator
von YouseKalack (Guest)


Attached files:

Rate this post
useful
not useful
The result expect would be "8" when S = "11" but the result is "2" as 
you can see in the simulation ....

von Yousekalack (Guest)


Rate this post
useful
not useful
My code :

I) The dataflow code to describe cell.
1
library ieee;
2
use ieee.std_logic_1164.all;
3
use ieee.std_logic_unsigned.all;
4
use ieee.numeric_std.all;
5
6
entity cell is 
7
port(Ai,Bi,Ci : in std_logic;
8
     S : in std_logic_vector (1 downto 0);
9
     Co,Go : out std_logic);
10
end cell;
11
12
architecture dflow of cell is
13
signal oper : std_logic_vector(1 downto 0);
14
signal tabc : std_logic_vector(2 downto 0);
15
begin
16
with tabc select oper <=
17
                          '0' & Ai + Bi  when "001",
18
                          '0' & Ai + Bi when "010" ,
19
                          '0' & Ai - '1' when "011" ,
20
                    '0' & Ai + '1' when "100" ,
21
                          '0' & Ai - Bi when "101" ,
22
                          '0' & Ai + Bi + '1' when "110",
23
                          '0' & Ai when others;
24
25
Co <= oper(1);
26
Go <= oper(0);
27
tabc <= Ci&S;
28
end architecture;
II) The structural code to link 4 cells to implement the arithmetic
operator
1
library ieee;
2
use ieee.std_logic_1164.all,ieee.std_logic_unsigned.all;
3
4
5
entity operator is
6
port(A,B : in std_logic_vector(3 downto 0);
7
     S : in std_logic_vector(1 downto 0);
8
     cin : in std_logic;
9
     G : out std_logic_vector(3 downto 0);
10
     cout : out std_logic);
11
end entity;
12
13
architecture rtl of operator is
14
component cell is 
15
port(Ai,Bi,Ci : in std_logic;
16
     S : in std_logic_vector (1 downto 0);
17
     Co,Go : out std_logic);
18
end component;
19
signal C : std_logic_vector(3 downto 1);
20
begin
21
cell0 : cell port map(Ai=>A(0),Bi=>B(0),Ci=>cin,S=>S,Co =>C(1),Go => G(0));
22
cell1 : cell port map(Ai=>A(1),Bi=>B(1),Ci=>C(1),S=>S,Co=>C(2),Go => G(1));
23
cell2 : cell port map(Ai=>A(2),Bi=>B(2),Ci=>C(2),S=>S,Co=>C(3),Go => G(2));
24
cell3 : cell port map(Ai=>A(3),Bi=>B(3),Ci=>C(3),S=>S,Co =>cout,Go =>G(3));
25
end architecture;

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
YouseKalack wrote:
> as you can see in the simulation ....
So setup a second test bench for your "cell" and have a look wether its 
working as desired.

> I) The dataflow code to describe cell.
By looking at the picture bottom left corner a thought pops up in my 
mind: shouldn't that "cell" also be described as a logic/structural 
expression instead as a functional data flow description? And shouldn't 
the CI be used for any operation in the "cell"?

To clear the view: the top left hand table is not related to the "cell" 
driectly. That table shows the expected behaviour of the top level 
module "operator".

: Edited by Moderator
von Achim S. (Guest)


Rate this post
useful
not useful
in your description of "cell" you never set the Carry-output. Oper(1) is 
set to 0 for all possible combinations of input signals. Your arithmetic 
won't work without correct Carry-Signals.

von Yousekalack (Guest)


Rate this post
useful
not useful
Yes the test work for one cell but only sub is not working good.. i 
think the problem is when A = '0' and A - 1 = ???

von Achim S. (Guest)


Rate this post
useful
not useful
Yousekalack wrote:
> Yes the test work for one cell but only sub is not working good.. i
> think the problem is when A = '0' and A - 1 = ???

No, for a single cell your code is also not working correctly.

Perform a simulation for a single cell. What are the correct outputs for 
A=0, Cin=0 and opeartion A-1? What does your simulation show instead?

von Huynh J. (yousekalack)


Rate this post
useful
not useful
This is the test for one cell with Ai = 0, Ci = 0 and S = '11'

von Huynh J. (yousekalack)


Attached files:

Rate this post
useful
not useful
Oper(1) is the carry out from all operations, i put'0' & but the '0' 
will be replace if there is a carry

von Achim S. (Guest)


Rate this post
useful
not useful
Huynh J. wrote:
> Oper(1) is the carry out from all operations, i put'0' & but the '0'
> will be replace if there is a carry

ok you're right, my mistake. "&" and "-" have identical priority and are 
evaluted from left to right. So

   '0' & Ai - '1'

turns into

  ('0' & Ai) - '1'

and then  the higer bit (i.e. the carry) can indeed be set after the 
operation

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.