-
Notifications
You must be signed in to change notification settings - Fork 42
/
arithmetic.vhdl
293 lines (239 loc) · 8.33 KB
/
arithmetic.vhdl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
--------------------------------------------------------------------
-- _ __ __ __ ____ __ = --
-- | | / // / / // __ \ / / = --
-- | | / // /_/ // / / // / = .__ |/ _/_ .__ .__ __ --
-- | |/ // __ // /_/ // /___ = /___) | / / ) / ) (_ ` --
-- |___//_/ /_//_____//_____/ = (___ /| (_ / (___(_ (__) --
-- ===== / --
-- === --
----------------------------- = ----------------------------------
--# arithmetic.vhdl - Pipelined arithmetic operations
--# Freely available from VHDL-extras (http://github.com/kevinpt/vhdl-extras)
--#
--# Copyright © 2017 Kevin Thibedeau
--# (kevin 'period' thibedeau 'at' gmail 'punto' com)
--#
--# Permission is hereby granted, free of charge, to any person obtaining a
--# copy of this software and associated documentation files (the "Software"),
--# to deal in the Software without restriction, including without limitation
--# the rights to use, copy, modify, merge, publish, distribute, sublicense,
--# and/or sell copies of the Software, and to permit persons to whom the
--# Software is furnished to do so, subject to the following conditions:
--#
--# The above copyright notice and this permission notice shall be included in
--# all copies or substantial portions of the Software.
--#
--# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
--# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
--# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
--# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
--# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
--# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
--# DEALINGS IN THE SOFTWARE.
--#
--# DEPENDENCIES: pipelining
--#
--# DESCRIPTION:
--# This is an implementation of general purpose pipelined adder. It can be configured
--# for any number of stages and bit widths. The adder is divided into a number of slices
--# each of which is a conventional adder. The maximum carry chain length is reduced to
--# ceil(bit-length / slices).
--#
--# --[]-[]-[Slice]--> Sum_3
--# --[]-[Slice]-[]--> Sum_2
--# --[Slice]-[]-[]--> Sum_1
--#
--# EXAMPLE USAGE:
--# -- 16-bit adder partitioned into 4 4-bit slices
--# signal A, B, Sum : unsigned(15 downto 0);
--#
--# pa: pipelined_adder
--# generic map (
--# SLICES => 4,
--# CONST_B_INPUT => false,
--# RESET_ACTIVE_LEVEL => RESET_ACTIVE_LEVEL
--# )
--# port map (
--# Clock => clock,
--# Reset => reset,
--#
--# A => A,
--# B => B,
--#
--# Sum => Sum
--# );
--------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
package arithmetic is
--## Variable size pipelined adder.
component pipelined_adder is
generic (
SLICES : positive; --# Number of pipeline stages
CONST_B_INPUT : boolean := false; --# Optimize when the B input is constant
RESET_ACTIVE_LEVEL : std_ulogic := '1' --# Asynch. reset control level
);
port (
--# {{clocks|}}
Clock : in std_ulogic; --# System clock
Reset : in std_ulogic; --# Asynchronous reset
--# {{data|}}
A : in unsigned; --# Addend A
B : in unsigned; --# Addend B
Sum : out unsigned --# Result sum of A and B
);
end component;
end package;
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
library extras;
use extras.pipelining.fixed_delay_line_unsigned;
-- Single slice of a pipelined adder
entity adder_slice is
generic (
SLICES : positive; --# Number of slices
SLICE_IX : natural; --# Index of this slice
CONST_B_INPUT : boolean := false; --# Optimize when the B input is constant
RESET_ACTIVE_LEVEL : std_ulogic := '1' --# Asynch. reset control level
);
port (
Clock : in std_ulogic;
Reset : in std_ulogic;
A : in unsigned;
B : in unsigned;
Carry_in : in std_ulogic;
Sum : out unsigned;
Carry_out : out std_ulogic
);
end entity;
architecture rtl of adder_slice is
signal A_dly : unsigned(A'range);
signal A_expanded : unsigned(A'length downto 0);
signal B_dly : unsigned(B'range);
signal B_expanded : unsigned(A'length downto 0);
signal cin : unsigned(0 downto 0);
signal cin_expanded : unsigned(A'length downto 0);
signal sum_loc : unsigned(A'length downto 0); -- Extra bit for carry out
signal sum_dly : unsigned(A'length-1 downto 0);
begin
dly_A: fixed_delay_line_unsigned
generic map (
STAGES => SLICE_IX
)
port map (
Clock => Clock,
Enable => '1',
Data_in => A,
Data_out => A_dly
);
-- Expand by one bit so we can capture the carry out
A_expanded <= resize(A_dly, A_expanded'length);
-- We skip the delay on the B input if it is treated as a constant
bd: if CONST_B_INPUT = false generate
dly_B: fixed_delay_line_unsigned
generic map (
STAGES => SLICE_IX
)
port map (
Clock => Clock,
Enable => '1',
Data_in => B,
Data_out => B_dly
);
end generate;
-- Expand by one bit so we can capture the carry out
with CONST_B_INPUT select
B_expanded <= resize(B_dly, B_expanded'length) when false,
resize(B, B_expanded'length) when others;
cin(0) <= Carry_in;
cin_expanded <= resize(cin, cin_expanded'length);
sum_loc <= A_expanded + B_expanded + cin_expanded;
-- Register the carry bit
carry: process(Clock, Reset) is
begin
if Reset = RESET_ACTIVE_LEVEL then
Carry_out <= '0';
elsif rising_edge(Clock) then
Carry_out <= sum_loc(sum_loc'high);
end if;
end process;
-- Add pipeline delays to sum
dly_out: fixed_delay_line_unsigned
generic map (
STAGES => SLICES - SLICE_IX
)
port map (
Clock => Clock,
Enable => '1',
Data_in => sum_loc(sum_loc'high-1 downto 0),
Data_out => sum_dly
);
Sum <= sum_dly;
end architecture;
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity pipelined_adder is
generic (
SLICES : positive;
CONST_B_INPUT : boolean := false;
RESET_ACTIVE_LEVEL : std_ulogic := '1'
);
port (
Clock : in std_ulogic;
Reset : in std_ulogic;
A : in unsigned;
B : in unsigned;
Sum : out unsigned
);
end entity;
architecture rtl of pipelined_adder is
component adder_slice is
generic (
SLICES : positive;
SLICE_IX : natural;
CONST_B_INPUT : boolean := false;
RESET_ACTIVE_LEVEL : std_ulogic := '1'
);
port (
Clock : in std_ulogic;
Reset : in std_ulogic;
A : in unsigned;
B : in unsigned;
Carry_in : in std_ulogic;
Sum : out unsigned;
Carry_out : out std_ulogic
);
end component;
constant SLICE_SIZE : positive := (A'length + SLICES - 1) / SLICES;
signal A_exp : unsigned(SLICE_SIZE*SLICES-1 downto 0);
signal B_exp : unsigned(SLICE_SIZE*SLICES-1 downto 0);
signal Sum_exp : unsigned(SLICE_SIZE*SLICES-1 downto 0);
signal slice_carry : std_ulogic_vector(0 to SLICES);
signal sum_loc : unsigned(SLICE_SIZE*SLICES-1 downto 0);
begin
A_exp <= resize(A, A_exp'length);
B_exp <= resize(B, B_exp'length);
slice_carry(0) <= '0';
gs: for SLICE_IX in 0 to SLICES-1 generate
slc: adder_slice
generic map (
SLICES => SLICES,
SLICE_IX => SLICE_IX,
CONST_B_INPUT => CONST_B_INPUT,
RESET_ACTIVE_LEVEL => RESET_ACTIVE_LEVEL
)
port map (
Clock => Clock,
Reset => Reset,
A => A_exp(SLICE_SIZE * (SLICE_IX + 1) - 1 downto SLICE_SIZE * SLICE_IX),
B => B_exp(SLICE_SIZE * (SLICE_IX + 1) - 1 downto SLICE_SIZE * SLICE_IX),
Carry_in => slice_carry(SLICE_IX),
Sum => sum_loc(SLICE_SIZE * (SLICE_IX + 1) - 1 downto SLICE_SIZE * SLICE_IX),
Carry_out => slice_carry(SLICE_IX+1)
);
end generate;
Sum <= slice_carry(slice_carry'high) & sum_loc(Sum'length-2 downto 0);
end architecture;