EmbDev.net

Forum: FPGA, VHDL & Verilog 8-Bit ALU Model using VHDL


von John Clark (Guest)


Rate this post
useful
not useful
Hey everyone,

It has been a while since I programmed using VHDL, plus I don't have 
much experience. I am trying to create a simple, structural, 8-bit ALU 
model that is capable of doing two's complement arithmetic (addition and 
subtraction) and that can do the logic functions NAND and NOR.

I started out creating an Add_Sub and Mux in the megawizard. After that, 
I am pretty much lost. I believe it's something like having a top-level 
entity that comprises the components and lays out the basic 
architecture.

If anyone can point me in the right direction, or what I'm missing, it 
would be greatly appreciated.

ADD_SUB Code:
1
LIBRARY ieee;
2
USE ieee.std_logic_1164.all;
3
4
LIBRARY lpm;
5
USE lpm.all;
6
7
ENTITY Add_Sub IS
8
  PORT
9
  (
10
    add_sub  : IN STD_LOGIC ;
11
    cin      : IN STD_LOGIC ;
12
    dataa    : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
13
    datab    : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
14
    cout     : OUT STD_LOGIC ;
15
    overflow : OUT STD_LOGIC ;
16
    result   : OUT STD_LOGIC_VECTOR (7 DOWNTO 0)
17
  );
18
END Add_Sub;
19
20
21
ARCHITECTURE SYN OF add_sub IS
22
23
  SIGNAL sub_wire0  : STD_LOGIC ;
24
  SIGNAL sub_wire1  : STD_LOGIC ;
25
  SIGNAL sub_wire2  : STD_LOGIC_VECTOR (7 DOWNTO 0);
26
27
28
29
  COMPONENT lpm_add_sub
30
  GENERIC (
31
    lpm_direction       : STRING;
32
    lpm_hint            : STRING;
33
    lpm_representation  : STRING;
34
    lpm_type            : STRING;
35
    lpm_width           : NATURAL
36
  );
37
  PORT (
38
      add_sub  : IN STD_LOGIC ;
39
      cin      : IN STD_LOGIC ;
40
      datab    : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
41
      overflow : OUT STD_LOGIC ;
42
      cout     : OUT STD_LOGIC ;
43
      dataa    : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
44
      result   : OUT STD_LOGIC_VECTOR (7 DOWNTO 0)
45
  );
46
  END COMPONENT;
47
48
BEGIN
49
  overflow <= sub_wire0;
50
  cout     <= sub_wire1;
51
  result   <= sub_wire2(7 DOWNTO 0);
52
53
  LPM_ADD_SUB_component : LPM_ADD_SUB
54
  GENERIC MAP (
55
    lpm_direction      => "UNUSED",
56
    lpm_hint           => "ONE_INPUT_IS_CONSTANT=NO,CIN_USED=YES",
57
    lpm_representation => "SIGNED",
58
    lpm_type           => "LPM_ADD_SUB",
59
    lpm_width          => 8
60
  )
61
  PORT MAP (
62
    add_sub  => add_sub,
63
    cin      => cin,
64
    datab    => datab,
65
    dataa    => dataa,
66
    overflow => sub_wire0,
67
    cout     => sub_wire1,
68
    result   => sub_wire2
69
  );
70
71
72
73
END SYN;


MUX Code:
1
 LIBRARY ieee;
2
USE ieee.std_logic_1164.all;
3
4
LIBRARY lpm;
5
USE lpm.lpm_components.all;
6
7
ENTITY MUX2X8 IS
8
  PORT
9
  (
10
    data0x  : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
11
    data1x  : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
12
    sel     : IN STD_LOGIC ;
13
    result  : OUT STD_LOGIC_VECTOR (7 DOWNTO 0)
14
  );
15
END MUX2X8;
16
17
18
ARCHITECTURE SYN OF mux2x8 IS
19
20
--  type STD_LOGIC_2D is array (NATURAL RANGE <>, NATURAL RANGE <>) of STD_LOGIC;
21
22
  SIGNAL sub_wire0  : STD_LOGIC_VECTOR (7 DOWNTO 0);
23
  SIGNAL sub_wire1  : STD_LOGIC_VECTOR (7 DOWNTO 0);
24
  SIGNAL sub_wire2  : STD_LOGIC_2D (1 DOWNTO 0, 7 DOWNTO 0);
25
  SIGNAL sub_wire3  : STD_LOGIC_VECTOR (7 DOWNTO 0);
26
  SIGNAL sub_wire4  : STD_LOGIC ;
27
  SIGNAL sub_wire5  : STD_LOGIC_VECTOR (0 DOWNTO 0);
28
29
BEGIN
30
  sub_wire3          <= data0x(7 DOWNTO 0);
31
  result             <= sub_wire0(7 DOWNTO 0);
32
  sub_wire1          <= data1x(7 DOWNTO 0);
33
  sub_wire2(1, 0)    <= sub_wire1(0);
34
  sub_wire2(1, 1)    <= sub_wire1(1);
35
  sub_wire2(1, 2)    <= sub_wire1(2);
36
  sub_wire2(1, 3)    <= sub_wire1(3);
37
  sub_wire2(1, 4)    <= sub_wire1(4);
38
  sub_wire2(1, 5)    <= sub_wire1(5);
39
  sub_wire2(1, 6)    <= sub_wire1(6);
40
  sub_wire2(1, 7)    <= sub_wire1(7);
41
  sub_wire2(0, 0)    <= sub_wire3(0);
42
  sub_wire2(0, 1)    <= sub_wire3(1);
43
  sub_wire2(0, 2)    <= sub_wire3(2);
44
  sub_wire2(0, 3)    <= sub_wire3(3);
45
  sub_wire2(0, 4)    <= sub_wire3(4);
46
  sub_wire2(0, 5)    <= sub_wire3(5);
47
  sub_wire2(0, 6)    <= sub_wire3(6);
48
  sub_wire2(0, 7)    <= sub_wire3(7);
49
  sub_wire4          <= sel;
50
  sub_wire5(0)       <= sub_wire4;
51
52
  LPM_MUX_component : LPM_MUX
53
  GENERIC MAP (
54
    lpm_size   => 2,
55
    lpm_type   => "LPM_MUX",
56
    lpm_width  => 8,
57
    lpm_widths => 1
58
  )
59
  PORT MAP (
60
    data   => sub_wire2,
61
    sel    => sub_wire5,
62
    result => sub_wire0
63
  );
64
65
66
67
END SYN;

von Schlumpf (Guest)


Rate this post
useful
not useful
It´s something like:
"I am not experienced in cars but I bought a tyre and a camshaft and I 
want to build a car. Can anybody point me in the right direction?"

von John Clark (Guest)


Rate this post
useful
not useful
*Tire

Dude, don't be an idiot. And by the way, I'm sure if someone wanted to 
know how to build a car, there would be someone intelligent enough to 
tell a person how. If you're not, then just stay out of the 
conversation.

People learn by example, by mistakes, or by instructions. In this case, 
I'm trying to learn by instruction. All I asked for was some help, so 
next time use a little introspection before you cast arrogant remarks in 
an open forum.

von Martin (Guest)


Rate this post
useful
not useful
Why  don't  you  simply  say

if(command) c=a+b ; else  c=a-b;

?

Nobody pays  you  for doing digital
design  on gate level.

von Schlumpf (Guest)


Rate this post
useful
not useful
Dude, don´t be an idiot!

what you posted is only a small fragmet of what is needed for an alu.
And now you ask the people to help you to make an alu out of this.
I just wanted to show the really bad quality of your question. But 
probably you didn´t undertood this.
If you ask a proper question you´ll get a proper answer!

I think you have no glue of what you´re doing here.
But you want to learn things by instruction. So why don´t you start with 
something more easy. You don´t know what a top level is and you don´t 
know how to connect certain componentes. So start with a small example. 
E.g. two AND-Gates.

Words you can google are:
COMPONENT, PORT MAP, etc..

If you are familiar with this than you can try to make a project. And if 
you stuck then, you are able to ask sensible questions.

Anyway, maybe you find a teacher here giving you a general VHDL-course.

btw. I wonder why you don´t use Verilog. Because if you write "tire" you 
must be from cotton-picker-country, where Verilog is the HDL that 
"rocks" :-)

von Schlumpf (Guest)


Rate this post
useful
not useful
>>Why  don't  you  simply  say
>>if(command) c=a+b ; else  c=a-b;

maybe because Megawizard doesn´t have a click-and-ready solution for 
this statement ;-)

A simple ALU may be "described" like this (you don´t "program" with 
VHDL)

case COMMAND is
  when "00" => C <= A + B;
  when "01" => C <= A - B;
  when "10" => C <= not (A and B);
  when "11" => C <= not (A or B);
  when others => NULL;
end case;

I hope this helps you.

von chris (Guest)


Rate this post
useful
not useful
Ha John you must be in Eisenbarth's class too.

I'm working with the same prompt.

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


Rate this post
useful
not useful
> Ha John you must be in Eisenbarth's class too.
Looks like he is. Greeting to Texas from Germany... ;-)

@ John
Are you are allowed to use third party cores in your homework? Do you 
have to do the (really senseless) job and just wire some components 
together? Aren't you allowed to use behavioral description (like all the 
others around the wordl)?

A mux is a fairly simple design, even in a structural description you 
should be allowed to use this:
1
ENTITY MUX2X8 IS
2
  PORT (
3
    data0x  : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
4
    data1x  : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
5
    sel     : IN STD_LOGIC ;
6
    result  : OUT STD_LOGIC_VECTOR (7 DOWNTO 0)
7
  );
8
END MUX2X8;
9
10
ARCHITECTURE SYN OF mux2x8 IS
11
BEGIN
12
  result  <= data0x when sel='1' else data0x;
13
END SYN;
The "upper level wiring" then can be done structural (even if that makes 
no further sense to me...).

von Klaus (Guest)


Rate this post
useful
not useful
it have been difficult times, when we all did our homework on our own...

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.