1 |
71 |
olivier.gi |
|
2 |
|
|
//----------------------------------------------------------------------------
|
3 |
|
|
// Copyright (C) 2001 Authors
|
4 |
|
|
//
|
5 |
|
|
// This source file may be used and distributed without restriction provided
|
6 |
|
|
// that this copyright statement is not removed from the file and that any
|
7 |
|
|
// derivative work contains the original copyright notice and the associated
|
8 |
|
|
// disclaimer.
|
9 |
|
|
//
|
10 |
|
|
// This source file is free software; you can redistribute it and/or modify
|
11 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
12 |
|
|
// by the Free Software Foundation; either version 2.1 of the License, or
|
13 |
|
|
// (at your option) any later version.
|
14 |
|
|
//
|
15 |
|
|
// This source is distributed in the hope that it will be useful, but WITHOUT
|
16 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
17 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
18 |
|
|
// License for more details.
|
19 |
|
|
//
|
20 |
|
|
// You should have received a copy of the GNU Lesser General Public License
|
21 |
|
|
// along with this source; if not, write to the Free Software Foundation,
|
22 |
|
|
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
23 |
|
|
//
|
24 |
|
|
//----------------------------------------------------------------------------
|
25 |
|
|
//
|
26 |
|
|
// *File Name: omsp_multiplier.v
|
27 |
|
|
//
|
28 |
|
|
// *Module Description:
|
29 |
|
|
// 16x16 Hardware multiplier.
|
30 |
|
|
//
|
31 |
|
|
// *Author(s):
|
32 |
|
|
// - Olivier Girard, olgirard@gmail.com
|
33 |
|
|
//
|
34 |
|
|
//----------------------------------------------------------------------------
|
35 |
|
|
// $Rev: 23 $
|
36 |
|
|
// $LastChangedBy: olivier.girard $
|
37 |
|
|
// $LastChangedDate: 2009-08-30 18:39:26 +0200 (Sun, 30 Aug 2009) $
|
38 |
|
|
//----------------------------------------------------------------------------
|
39 |
|
|
`include "timescale.v"
|
40 |
|
|
`include "openMSP430_defines.v"
|
41 |
|
|
|
42 |
|
|
module omsp_multiplier (
|
43 |
|
|
|
44 |
|
|
// OUTPUTs
|
45 |
|
|
per_dout, // Peripheral data output
|
46 |
|
|
|
47 |
|
|
// INPUTs
|
48 |
|
|
mclk, // Main system clock
|
49 |
|
|
per_addr, // Peripheral address
|
50 |
|
|
per_din, // Peripheral data input
|
51 |
|
|
per_en, // Peripheral enable (high active)
|
52 |
|
|
per_wen, // Peripheral write enable (high active)
|
53 |
|
|
puc // Main system reset
|
54 |
|
|
);
|
55 |
|
|
|
56 |
|
|
// OUTPUTs
|
57 |
|
|
//=========
|
58 |
|
|
output [15:0] per_dout; // Peripheral data output
|
59 |
|
|
|
60 |
|
|
// INPUTs
|
61 |
|
|
//=========
|
62 |
|
|
input mclk; // Main system clock
|
63 |
|
|
input [7:0] per_addr; // Peripheral address
|
64 |
|
|
input [15:0] per_din; // Peripheral data input
|
65 |
|
|
input per_en; // Peripheral enable (high active)
|
66 |
|
|
input [1:0] per_wen; // Peripheral write enable (high active)
|
67 |
|
|
input puc; // Main system reset
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
//=============================================================================
|
71 |
|
|
// 1) PARAMETER/REGISTERS & WIRE DECLARATION
|
72 |
|
|
//=============================================================================
|
73 |
|
|
|
74 |
|
|
// Register addresses
|
75 |
|
|
parameter OP1_MPY = 9'h130;
|
76 |
|
|
parameter OP1_MPYS = 9'h132;
|
77 |
|
|
parameter OP1_MAC = 9'h134;
|
78 |
|
|
parameter OP1_MACS = 9'h136;
|
79 |
|
|
parameter OP2 = 9'h138;
|
80 |
|
|
parameter RESLO = 9'h13A;
|
81 |
|
|
parameter RESHI = 9'h13C;
|
82 |
|
|
parameter SUMEXT = 9'h13E;
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
// Register one-hot decoder
|
86 |
|
|
parameter OP1_MPY_D = (512'h1 << OP1_MPY);
|
87 |
|
|
parameter OP1_MPYS_D = (512'h1 << OP1_MPYS);
|
88 |
|
|
parameter OP1_MAC_D = (512'h1 << OP1_MAC);
|
89 |
|
|
parameter OP1_MACS_D = (512'h1 << OP1_MACS);
|
90 |
|
|
parameter OP2_D = (512'h1 << OP2);
|
91 |
|
|
parameter RESLO_D = (512'h1 << RESLO);
|
92 |
|
|
parameter RESHI_D = (512'h1 << RESHI);
|
93 |
|
|
parameter SUMEXT_D = (512'h1 << SUMEXT);
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
// Wire pre-declarations
|
97 |
|
|
wire result_wr;
|
98 |
|
|
wire result_clr;
|
99 |
|
|
wire early_read;
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
//============================================================================
|
103 |
|
|
// 2) REGISTER DECODER
|
104 |
|
|
//============================================================================
|
105 |
|
|
|
106 |
|
|
// Register address decode
|
107 |
|
|
reg [511:0] reg_dec;
|
108 |
|
|
always @(per_addr)
|
109 |
|
|
case ({per_addr,1'b0})
|
110 |
|
|
OP1_MPY : reg_dec = OP1_MPY_D;
|
111 |
|
|
OP1_MPYS : reg_dec = OP1_MPYS_D;
|
112 |
|
|
OP1_MAC : reg_dec = OP1_MAC_D;
|
113 |
|
|
OP1_MACS : reg_dec = OP1_MACS_D;
|
114 |
|
|
OP2 : reg_dec = OP2_D;
|
115 |
|
|
RESLO : reg_dec = RESLO_D;
|
116 |
|
|
RESHI : reg_dec = RESHI_D;
|
117 |
|
|
SUMEXT : reg_dec = SUMEXT_D;
|
118 |
|
|
default : reg_dec = {512{1'b0}};
|
119 |
|
|
endcase
|
120 |
|
|
|
121 |
|
|
// Read/Write probes
|
122 |
|
|
wire reg_write = |per_wen & per_en;
|
123 |
|
|
wire reg_read = ~|per_wen & per_en;
|
124 |
|
|
|
125 |
|
|
// Read/Write vectors
|
126 |
|
|
wire [511:0] reg_wr = reg_dec & {512{reg_write}};
|
127 |
|
|
wire [511:0] reg_rd = reg_dec & {512{reg_read}};
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
//============================================================================
|
131 |
|
|
// 3) REGISTERS
|
132 |
|
|
//============================================================================
|
133 |
|
|
|
134 |
|
|
// OP1 Register
|
135 |
|
|
//-----------------
|
136 |
|
|
reg [15:0] op1;
|
137 |
|
|
|
138 |
|
|
wire op1_wr = reg_wr[OP1_MPY] |
|
139 |
|
|
reg_wr[OP1_MPYS] |
|
140 |
|
|
reg_wr[OP1_MAC] |
|
141 |
|
|
reg_wr[OP1_MACS];
|
142 |
|
|
|
143 |
|
|
always @ (posedge mclk or posedge puc)
|
144 |
|
|
if (puc) op1 <= 16'h0000;
|
145 |
|
|
else if (op1_wr) op1 <= per_din;
|
146 |
|
|
|
147 |
|
|
wire [15:0] op1_rd = op1;
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
// OP2 Register
|
151 |
|
|
//-----------------
|
152 |
|
|
reg [15:0] op2;
|
153 |
|
|
|
154 |
|
|
wire op2_wr = reg_wr[OP2];
|
155 |
|
|
|
156 |
|
|
always @ (posedge mclk or posedge puc)
|
157 |
|
|
if (puc) op2 <= 16'h0000;
|
158 |
|
|
else if (op2_wr) op2 <= per_din;
|
159 |
|
|
|
160 |
|
|
wire [15:0] op2_rd = op2;
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
// RESLO Register
|
164 |
|
|
//-----------------
|
165 |
|
|
reg [15:0] reslo;
|
166 |
|
|
|
167 |
|
|
wire [15:0] reslo_nxt;
|
168 |
|
|
wire reslo_wr = reg_wr[RESLO];
|
169 |
|
|
|
170 |
|
|
always @ (posedge mclk or posedge puc)
|
171 |
|
|
if (puc) reslo <= 16'h0000;
|
172 |
|
|
else if (reslo_wr) reslo <= per_din;
|
173 |
|
|
else if (result_clr) reslo <= 16'h0000;
|
174 |
|
|
else if (result_wr) reslo <= reslo_nxt;
|
175 |
|
|
|
176 |
|
|
wire [15:0] reslo_rd = early_read ? reslo_nxt : reslo;
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
// RESHI Register
|
180 |
|
|
//-----------------
|
181 |
|
|
reg [15:0] reshi;
|
182 |
|
|
|
183 |
|
|
wire [15:0] reshi_nxt;
|
184 |
|
|
wire reshi_wr = reg_wr[RESHI];
|
185 |
|
|
|
186 |
|
|
always @ (posedge mclk or posedge puc)
|
187 |
|
|
if (puc) reshi <= 16'h0000;
|
188 |
|
|
else if (reshi_wr) reshi <= per_din;
|
189 |
|
|
else if (result_clr) reshi <= 16'h0000;
|
190 |
|
|
else if (result_wr) reshi <= reshi_nxt;
|
191 |
|
|
|
192 |
|
|
wire [15:0] reshi_rd = early_read ? reshi_nxt : reshi;
|
193 |
|
|
|
194 |
|
|
|
195 |
|
|
// SUMEXT Register
|
196 |
|
|
//-----------------
|
197 |
|
|
reg [1:0] sumext_s;
|
198 |
|
|
|
199 |
|
|
wire [1:0] sumext_s_nxt;
|
200 |
|
|
|
201 |
|
|
always @ (posedge mclk or posedge puc)
|
202 |
|
|
if (puc) sumext_s <= 2'b00;
|
203 |
|
|
else if (op2_wr) sumext_s <= 2'b00;
|
204 |
|
|
else if (result_wr) sumext_s <= sumext_s_nxt;
|
205 |
|
|
|
206 |
|
|
wire [15:0] sumext_nxt = {{14{sumext_s_nxt[1]}}, sumext_s_nxt};
|
207 |
|
|
wire [15:0] sumext = {{14{sumext_s[1]}}, sumext_s};
|
208 |
|
|
wire [15:0] sumext_rd = early_read ? sumext_nxt : sumext;
|
209 |
|
|
|
210 |
|
|
|
211 |
|
|
//============================================================================
|
212 |
|
|
// 4) DATA OUTPUT GENERATION
|
213 |
|
|
//============================================================================
|
214 |
|
|
|
215 |
|
|
// Data output mux
|
216 |
|
|
wire [15:0] op1_mux = op1_rd & {16{reg_rd[OP1_MPY] |
|
217 |
|
|
reg_rd[OP1_MPYS] |
|
218 |
|
|
reg_rd[OP1_MAC] |
|
219 |
|
|
reg_rd[OP1_MACS]}};
|
220 |
|
|
wire [15:0] op2_mux = op2_rd & {16{reg_rd[OP2]}};
|
221 |
|
|
wire [15:0] reslo_mux = reslo_rd & {16{reg_rd[RESLO]}};
|
222 |
|
|
wire [15:0] reshi_mux = reshi_rd & {16{reg_rd[RESHI]}};
|
223 |
|
|
wire [15:0] sumext_mux = sumext_rd & {16{reg_rd[SUMEXT]}};
|
224 |
|
|
|
225 |
|
|
wire [15:0] per_dout = op1_mux |
|
226 |
|
|
op2_mux |
|
227 |
|
|
reslo_mux |
|
228 |
|
|
reshi_mux |
|
229 |
|
|
sumext_mux;
|
230 |
|
|
|
231 |
|
|
|
232 |
|
|
//============================================================================
|
233 |
|
|
// 5) HARDWARE MULTIPLIER FUNCTIONAL LOGIC
|
234 |
|
|
//============================================================================
|
235 |
|
|
|
236 |
|
|
// Multiplier configuration
|
237 |
|
|
//--------------------------
|
238 |
|
|
|
239 |
|
|
// Detect signed mode
|
240 |
|
|
reg sign_sel;
|
241 |
|
|
always @ (posedge mclk or posedge puc)
|
242 |
|
|
if (puc) sign_sel <= 1'b0;
|
243 |
|
|
else if (op1_wr) sign_sel <= reg_wr[OP1_MPYS] | reg_wr[OP1_MACS];
|
244 |
|
|
|
245 |
|
|
|
246 |
|
|
// Detect accumulate mode
|
247 |
|
|
reg acc_sel;
|
248 |
|
|
always @ (posedge mclk or posedge puc)
|
249 |
|
|
if (puc) acc_sel <= 1'b0;
|
250 |
|
|
else if (op1_wr) acc_sel <= reg_wr[OP1_MAC] | reg_wr[OP1_MACS];
|
251 |
|
|
|
252 |
|
|
|
253 |
|
|
// Detect whenever the RESHI and RESLO registers should be cleared
|
254 |
|
|
assign result_clr = op2_wr & ~acc_sel;
|
255 |
|
|
|
256 |
|
|
// Combine RESHI & RESLO
|
257 |
|
|
wire [31:0] result = {reshi, reslo};
|
258 |
|
|
|
259 |
|
|
|
260 |
|
|
// 16x16 Multiplier (result computed in 1 clock cycle)
|
261 |
|
|
//-----------------------------------------------------
|
262 |
|
|
`ifdef MPY_16x16
|
263 |
|
|
|
264 |
|
|
// Detect start of a multiplication
|
265 |
|
|
reg cycle;
|
266 |
|
|
always @ (posedge mclk or posedge puc)
|
267 |
|
|
if (puc) cycle <= 1'b0;
|
268 |
|
|
else cycle <= op2_wr;
|
269 |
|
|
|
270 |
|
|
assign result_wr = cycle;
|
271 |
|
|
|
272 |
|
|
// Expand the operands to support signed & unsigned operations
|
273 |
|
|
wire signed [16:0] op1_xp = {sign_sel & op1[15], op1};
|
274 |
|
|
wire signed [16:0] op2_xp = {sign_sel & op2[15], op2};
|
275 |
|
|
|
276 |
|
|
|
277 |
|
|
// 17x17 signed multiplication
|
278 |
|
|
wire signed [33:0] product = op1_xp * op2_xp;
|
279 |
|
|
|
280 |
|
|
// Accumulate
|
281 |
|
|
wire [32:0] result_nxt = {1'b0, result} + {1'b0, product[31:0]};
|
282 |
|
|
|
283 |
|
|
|
284 |
|
|
// Next register values
|
285 |
|
|
assign reslo_nxt = result_nxt[15:0];
|
286 |
|
|
assign reshi_nxt = result_nxt[31:16];
|
287 |
|
|
assign sumext_s_nxt = sign_sel ? {2{result_nxt[31]}} :
|
288 |
|
|
{1'b0, result_nxt[32]};
|
289 |
|
|
|
290 |
|
|
|
291 |
|
|
// Since the MAC is completed within 1 clock cycle,
|
292 |
|
|
// an early read can't happen.
|
293 |
|
|
assign early_read = 1'b0;
|
294 |
|
|
|
295 |
|
|
|
296 |
|
|
// 16x8 Multiplier (result computed in 2 clock cycles)
|
297 |
|
|
//-----------------------------------------------------
|
298 |
|
|
`else
|
299 |
|
|
|
300 |
|
|
// Detect start of a multiplication
|
301 |
|
|
reg [1:0] cycle;
|
302 |
|
|
always @ (posedge mclk or posedge puc)
|
303 |
|
|
if (puc) cycle <= 2'b00;
|
304 |
|
|
else cycle <= {cycle[0], op2_wr};
|
305 |
|
|
|
306 |
|
|
assign result_wr = |cycle;
|
307 |
|
|
|
308 |
|
|
|
309 |
|
|
// Expand the operands to support signed & unsigned operations
|
310 |
|
|
wire signed [16:0] op1_xp = {sign_sel & op1[15], op1};
|
311 |
|
|
wire signed [8:0] op2_hi_xp = {sign_sel & op2[15], op2[15:8]};
|
312 |
|
|
wire signed [8:0] op2_lo_xp = { 1'b0, op2[7:0]};
|
313 |
|
|
wire signed [8:0] op2_xp = cycle[0] ? op2_hi_xp : op2_lo_xp;
|
314 |
|
|
|
315 |
|
|
|
316 |
|
|
// 17x9 signed multiplication
|
317 |
|
|
wire signed [25:0] product = op1_xp * op2_xp;
|
318 |
|
|
|
319 |
|
|
wire [31:0] product_xp = cycle[0] ? {product[23:0], 8'h00} :
|
320 |
|
|
{{8{sign_sel & product[23]}}, product[23:0]};
|
321 |
|
|
|
322 |
|
|
// Accumulate
|
323 |
|
|
wire [32:0] result_nxt = {1'b0, result} + {1'b0, product_xp[31:0]};
|
324 |
|
|
|
325 |
|
|
|
326 |
|
|
// Next register values
|
327 |
|
|
assign reslo_nxt = result_nxt[15:0];
|
328 |
|
|
assign reshi_nxt = result_nxt[31:16];
|
329 |
|
|
assign sumext_s_nxt = sign_sel ? {2{result_nxt[31]}} :
|
330 |
|
|
{1'b0, result_nxt[32] | sumext_s[0]};
|
331 |
|
|
|
332 |
|
|
// Since the MAC is completed within 2 clock cycle,
|
333 |
|
|
// an early read can happen during the second cycle.
|
334 |
|
|
assign early_read = cycle[1];
|
335 |
|
|
|
336 |
|
|
`endif
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
endmodule // omsp_multiplier
|
340 |
|
|
|
341 |
|
|
`include "openMSP430_undefines.v"
|