1 |
2 |
csantifort |
//////////////////////////////////////////////////////////////////
|
2 |
|
|
// //
|
3 |
|
|
// Arithmetic Logic Unit (ALU) for Amber 2 Core //
|
4 |
|
|
// //
|
5 |
|
|
// This file is part of the Amber project //
|
6 |
|
|
// http://www.opencores.org/project,amber //
|
7 |
|
|
// //
|
8 |
|
|
// Description //
|
9 |
|
|
// Supported functions: 32-bit add and subtract, AND, OR, //
|
10 |
|
|
// XOR, NOT, Zero extent 8-bit numbers //
|
11 |
|
|
// //
|
12 |
|
|
// Author(s): //
|
13 |
|
|
// - Conor Santifort, csantifort.amber@gmail.com //
|
14 |
|
|
// //
|
15 |
|
|
//////////////////////////////////////////////////////////////////
|
16 |
|
|
// //
|
17 |
|
|
// Copyright (C) 2010 Authors and OPENCORES.ORG //
|
18 |
|
|
// //
|
19 |
|
|
// This source file may be used and distributed without //
|
20 |
|
|
// restriction provided that this copyright statement is not //
|
21 |
|
|
// removed from the file and that any derivative work contains //
|
22 |
|
|
// the original copyright notice and the associated disclaimer. //
|
23 |
|
|
// //
|
24 |
|
|
// This source file is free software; you can redistribute it //
|
25 |
|
|
// and/or modify it under the terms of the GNU Lesser General //
|
26 |
|
|
// Public License as published by the Free Software Foundation; //
|
27 |
|
|
// either version 2.1 of the License, or (at your option) any //
|
28 |
|
|
// later version. //
|
29 |
|
|
// //
|
30 |
|
|
// This source is distributed in the hope that it will be //
|
31 |
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied //
|
32 |
|
|
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
|
33 |
|
|
// PURPOSE. See the GNU Lesser General Public License for more //
|
34 |
|
|
// details. //
|
35 |
|
|
// //
|
36 |
|
|
// You should have received a copy of the GNU Lesser General //
|
37 |
|
|
// Public License along with this source; if not, download it //
|
38 |
|
|
// from http://www.opencores.org/lgpl.shtml //
|
39 |
|
|
// //
|
40 |
|
|
//////////////////////////////////////////////////////////////////
|
41 |
|
|
|
42 |
|
|
|
43 |
15 |
csantifort |
module a23_alu (
|
44 |
2 |
csantifort |
|
45 |
|
|
input [31:0] i_a_in,
|
46 |
|
|
input [31:0] i_b_in,
|
47 |
|
|
input i_barrel_shift_carry,
|
48 |
|
|
input i_status_bits_carry,
|
49 |
|
|
input [8:0] i_function,
|
50 |
|
|
|
51 |
|
|
output [31:0] o_out,
|
52 |
83 |
csantifort |
output [3:0] o_flags // negative, zero, carry, overflow
|
53 |
2 |
csantifort |
);
|
54 |
|
|
|
55 |
|
|
wire [31:0] a, b, b_not;
|
56 |
|
|
wire [31:0] and_out, or_out, xor_out;
|
57 |
|
|
wire [31:0] sign_ex8_out, sign_ex_16_out;
|
58 |
|
|
wire [31:0] zero_ex8_out, zero_ex_16_out;
|
59 |
|
|
wire [32:0] fadder_out;
|
60 |
|
|
wire swap_sel;
|
61 |
|
|
wire not_sel;
|
62 |
|
|
wire [1:0] cin_sel;
|
63 |
|
|
wire cout_sel;
|
64 |
|
|
wire [3:0] out_sel;
|
65 |
|
|
wire carry_in;
|
66 |
|
|
wire carry_out;
|
67 |
|
|
wire overflow_out;
|
68 |
|
|
wire fadder_carry_out;
|
69 |
|
|
|
70 |
|
|
assign { swap_sel, not_sel, cin_sel, cout_sel, out_sel } = i_function;
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
// ========================================================
|
74 |
|
|
// A Select
|
75 |
|
|
// ========================================================
|
76 |
|
|
assign a = (swap_sel ) ? i_b_in : i_a_in ;
|
77 |
|
|
|
78 |
|
|
// ========================================================
|
79 |
|
|
// B Select
|
80 |
|
|
// ========================================================
|
81 |
|
|
assign b = (swap_sel ) ? i_a_in : i_b_in ;
|
82 |
|
|
|
83 |
|
|
// ========================================================
|
84 |
|
|
// Not Select
|
85 |
|
|
// ========================================================
|
86 |
|
|
assign b_not = (not_sel ) ? ~b : b ;
|
87 |
|
|
|
88 |
|
|
// ========================================================
|
89 |
|
|
// Cin Select
|
90 |
|
|
// ========================================================
|
91 |
|
|
assign carry_in = (cin_sel==2'd0 ) ? 1'd0 :
|
92 |
|
|
(cin_sel==2'd1 ) ? 1'd1 :
|
93 |
|
|
i_status_bits_carry ; // add with carry
|
94 |
|
|
|
95 |
|
|
// ========================================================
|
96 |
|
|
// Cout Select
|
97 |
|
|
// ========================================================
|
98 |
|
|
assign carry_out = (cout_sel==1'd0 ) ? fadder_carry_out :
|
99 |
|
|
i_barrel_shift_carry ;
|
100 |
|
|
|
101 |
|
|
// For non-addition/subtractions that incorporate a shift
|
102 |
|
|
// operation, C is set to the last bit
|
103 |
|
|
// shifted out of the value by the shifter.
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
// ========================================================
|
107 |
|
|
// Overflow out
|
108 |
|
|
// ========================================================
|
109 |
|
|
// Only assert the overflow flag when using the adder
|
110 |
|
|
assign overflow_out = out_sel == 4'd1 &&
|
111 |
|
|
// overflow if adding two positive numbers and get a negative number
|
112 |
|
|
( (!a[31] && !b_not[31] && fadder_out[31]) ||
|
113 |
|
|
// or adding two negative numbers and get a positive number
|
114 |
|
|
(a[31] && b_not[31] && !fadder_out[31]) );
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
// ========================================================
|
118 |
|
|
// ALU Operations
|
119 |
|
|
// ========================================================
|
120 |
|
|
|
121 |
|
|
`ifdef XILINX_FPGA
|
122 |
|
|
|
123 |
|
|
// XIlinx Spartan 6 DSP module
|
124 |
|
|
`ifdef XILINX_SPARTAN6_FPGA
|
125 |
|
|
xs6_addsub_n #(.WIDTH(33))
|
126 |
|
|
`endif
|
127 |
|
|
`ifdef XILINX_VIRTEX6_FPGA
|
128 |
|
|
xv6_addsub_n #(.WIDTH(33))
|
129 |
|
|
`endif
|
130 |
|
|
u_xx_addsub_33(
|
131 |
|
|
.i_a ( {1'd0,a} ),
|
132 |
|
|
.i_b ( {1'd0,b_not} ),
|
133 |
|
|
.i_cin ( carry_in ),
|
134 |
|
|
.i_sub ( 1'd0 ),
|
135 |
|
|
.o_sum ( fadder_out ),
|
136 |
|
|
.o_co ( )
|
137 |
|
|
);
|
138 |
|
|
|
139 |
|
|
`else
|
140 |
|
|
assign fadder_out = { 1'd0,a} + {1'd0,b_not} + {32'd0,carry_in};
|
141 |
|
|
`endif
|
142 |
|
|
|
143 |
|
|
assign fadder_carry_out = fadder_out[32];
|
144 |
|
|
assign and_out = a & b_not;
|
145 |
|
|
assign or_out = a | b_not;
|
146 |
|
|
assign xor_out = a ^ b_not;
|
147 |
|
|
assign zero_ex8_out = {24'd0, b_not[7:0]};
|
148 |
|
|
assign zero_ex_16_out = {16'd0, b_not[15:0]};
|
149 |
|
|
assign sign_ex8_out = {{24{b_not[7]}}, b_not[7:0]};
|
150 |
|
|
assign sign_ex_16_out = {{16{b_not[15]}}, b_not[15:0]};
|
151 |
|
|
|
152 |
|
|
// ========================================================
|
153 |
|
|
// Out Select
|
154 |
|
|
// ========================================================
|
155 |
|
|
assign o_out = out_sel == 4'd0 ? b_not :
|
156 |
|
|
out_sel == 4'd1 ? fadder_out[31:0] :
|
157 |
|
|
out_sel == 4'd2 ? zero_ex_16_out :
|
158 |
|
|
out_sel == 4'd3 ? zero_ex8_out :
|
159 |
|
|
out_sel == 4'd4 ? sign_ex_16_out :
|
160 |
|
|
out_sel == 4'd5 ? sign_ex8_out :
|
161 |
|
|
out_sel == 4'd6 ? xor_out :
|
162 |
|
|
out_sel == 4'd7 ? or_out :
|
163 |
|
|
and_out ;
|
164 |
|
|
|
165 |
|
|
assign o_flags = { o_out[31], // negative
|
166 |
|
|
|o_out == 1'd0, // zero
|
167 |
|
|
carry_out, // carry
|
168 |
|
|
overflow_out // overflow
|
169 |
|
|
};
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
endmodule
|
173 |
|
|
|
174 |
|
|
|