OpenCores
URL https://opencores.org/ocsvn/risc5x/risc5x/trunk

Subversion Repositories risc5x

[/] [risc5x/] [trunk/] [add_sub.vhd] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 mikej
--
2
-- Risc5x
3
-- www.OpenCores.Org - November 2001
4
--
5
--
6
-- This library is free software; you can distribute it and/or modify it
7
-- under the terms of the GNU Lesser General Public License as published
8
-- by the Free Software Foundation; either version 2.1 of the License, or
9
-- (at your option) any later version.
10
--
11
-- This library is distributed in the hope that it will be useful, but
12
-- WITHOUT ANY WARRANTY; without even the implied warranty of
13
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
-- See the GNU Lesser General Public License for more details.
15
--
16
-- A RISC CPU core.
17
--
18
-- (c) Mike Johnson 2001. All Rights Reserved.
19
-- mikej@opencores.org for support or any other issues.
20
--
21
-- Revision list
22
--
23
-- version 1.0 initial opencores release
24
--
25
library ieee;
26
  use ieee.std_logic_1164.all;
27
  use ieee.std_logic_arith.all;
28
  use ieee.std_logic_unsigned.all;
29
 
30
--
31
-- op <= A +/- B or A
32
--
33
entity ADD_SUB is
34
  generic (
35
    WIDTH         : in  natural := 8
36
    );
37
  port (
38
    A             : in  std_logic_vector(WIDTH-1 downto 0);
39
    B             : in  std_logic_vector(WIDTH-1 downto 0);
40
 
41
    ADD_OR_SUB    : in  std_logic; -- high for DOUT <= A +/- B, low for DOUT <= A
42
    DO_SUB        : in  std_logic; -- high for DOUT <= A   - B, low for DOUT <= A + B
43
 
44
    CARRY_OUT     : out std_logic_vector(WIDTH-1 downto 0);
45
    DOUT          : out std_logic_vector(WIDTH-1 downto 0)
46
    );
47
end;
48
 
49
use work.pkg_xilinx_prims.all;
50
library ieee;
51
  use ieee.std_logic_1164.all;
52
  use ieee.std_logic_arith.all;
53
  use ieee.std_logic_unsigned.all;
54
 
55
architecture VIRTEX of ADD_SUB is
56
 
57
    signal lut_op       : std_logic_vector(WIDTH-1 downto 0);
58
    signal mult_and_op  : std_logic_vector(WIDTH-1 downto 0);
59
    signal carry        : std_logic_vector(WIDTH   downto 0);
60
    signal op_int       : std_logic_vector(WIDTH-1 downto 0);
61
 
62
    function loc(i : integer) return integer is
63
    begin
64
      return (((WIDTH+1)/2)-1) - i/2;
65
    end loc;
66
 
67
begin
68
  carry(0) <= DO_SUB;
69
  INST : for i in 0 to WIDTH-1 generate
70
    attribute RLOC of u_lut  : label is "R" & integer'image(loc(i)) & "C0.S1";
71
    attribute RLOC of u_1    : label is "R" & integer'image(loc(i)) & "C0.S1";
72
    attribute RLOC of u_2    : label is "R" & integer'image(loc(i)) & "C0.S1";
73
    attribute RLOC of u_3    : label is "R" & integer'image(loc(i)) & "C0.S1";
74
    attribute INIT of u_lut  : label is "C66C";
75
    begin
76
      u_lut :  LUT4
77
      --pragma translate_off
78
      generic map (
79
        INIT => str2slv(u_lut'INIT)
80
        )
81
      --pragma translate_on
82
      port map (
83
        I0 => ADD_OR_SUB,
84
        I1 => A(i),
85
        I2 => B(i),
86
        I3 => DO_SUB,
87
        O  => lut_op(i)
88
        );
89
 
90
      u_1 : MULT_AND
91
      port map (
92
        I0 => ADD_OR_SUB,
93
        I1 => A(i),
94
        LO => mult_and_op(i)
95
        );
96
 
97
      u_2 : MUXCY
98
      port map (
99
        DI => mult_and_op(i),
100
        CI => carry(i),
101
        S  => lut_op(i),
102
        O  => carry(i+1)
103
        );
104
 
105
      u_3 : XORCY
106
      port map (
107
        LI => lut_op(i),
108
        CI => carry(i),
109
        O  => op_int(i)
110
        );
111
 
112
  end generate;
113
  CARRY_OUT <= carry(WIDTH downto 1);
114
  DOUT <= op_int;
115
end Virtex;
116
 
117
--pragma translate_off
118
 
119
library ieee;
120
  use ieee.std_logic_1164.all;
121
  use ieee.std_logic_arith.all;
122
  use ieee.std_logic_unsigned.all;
123
 
124
architecture RTL of ADD_SUB is
125
  signal a_plus_b   : std_logic_vector(9 downto 0) := (others => '0');
126
  signal a_minus_b  : std_logic_vector(9 downto 0) := (others => '0');
127
begin -- architecture
128
 
129
  p_addsub_comb : process(A,B,a_plus_b,a_minus_b)
130
  begin
131
    a_plus_b(4 downto 0)  <= ('0' & A(3 downto 0)) + ('0' & B(3 downto 0));
132
    a_plus_b(9 downto 5)  <= ('0' & A(7 downto 4)) + ('0' & B(7 downto 4)) + ("0000" & a_plus_b(4));
133
    a_minus_b(4 downto 0) <= ('0' & A(3 downto 0)) - ('0' & B(3 downto 0));
134
    a_minus_b(9 downto 5) <= ('0' & A(7 downto 4)) - ('0' & B(7 downto 4)) - ("0000" & a_minus_b(4));
135
  end process;
136
 
137
  p_add_sub_comb : process(A,B,ADD_OR_SUB,DO_SUB,a_minus_b,a_plus_b)
138
  begin
139
    DOUT <= A;
140
    CARRY_OUT <= (others => '0');
141
    if (ADD_OR_SUB = '1') then
142
      if (DO_SUB = '1') then
143
        DOUT <= a_minus_b(8 downto 5) & a_minus_b(3 downto 0);
144
        CARRY_OUT(7) <= not a_minus_b(9);
145
        CARRY_OUT(3) <= not a_minus_b(4);
146
      else
147
        DOUT <= a_plus_b(8 downto 5) & a_plus_b(3 downto 0);
148
        CARRY_OUT(7) <=     a_plus_b(9);
149
        CARRY_OUT(3) <=     a_plus_b(4);
150
      end if;
151
    end if;
152
  end process;
153
 
154
end RTL;
155
 
156
--pragma translate_on
157
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.