2 //----------------------------------------------------------------------------
3 // Copyright (C) 2001 Authors
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
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.
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.
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
24 //----------------------------------------------------------------------------
26 // *File Name: omsp_multiplier.v
28 // *Module Description:
29 // 16x16 Hardware multiplier.
32 // - Olivier Girard, olgirard@gmail.com
34 //----------------------------------------------------------------------------
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"
42 module omsp_multiplier (
45 per_dout, // Peripheral data output
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
58 output [15:0] per_dout; // Peripheral data output
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
70 //=============================================================================
71 // 1) PARAMETER/REGISTERS & WIRE DECLARATION
72 //=============================================================================
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;
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);
96 // Wire pre-declarations
102 //============================================================================
103 // 2) REGISTER DECODER
104 //============================================================================
106 // Register address decode
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}};
122 wire reg_write = |per_wen & per_en;
123 wire reg_read = ~|per_wen & per_en;
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}};
130 //============================================================================
132 //============================================================================
138 wire op1_wr = reg_wr[OP1_MPY] |
143 always @ (posedge mclk or posedge puc)
144 if (puc) op1 <= 16'h0000;
145 else if (op1_wr) op1 <= per_din;
147 wire [15:0] op1_rd = op1;
154 wire op2_wr = reg_wr[OP2];
156 always @ (posedge mclk or posedge puc)
157 if (puc) op2 <= 16'h0000;
158 else if (op2_wr) op2 <= per_din;
160 wire [15:0] op2_rd = op2;
167 wire [15:0] reslo_nxt;
168 wire reslo_wr = reg_wr[RESLO];
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;
176 wire [15:0] reslo_rd = early_read ? reslo_nxt : reslo;
183 wire [15:0] reshi_nxt;
184 wire reshi_wr = reg_wr[RESHI];
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;
192 wire [15:0] reshi_rd = early_read ? reshi_nxt : reshi;
199 wire [1:0] sumext_s_nxt;
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;
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;
211 //============================================================================
212 // 4) DATA OUTPUT GENERATION
213 //============================================================================
216 wire [15:0] op1_mux = op1_rd & {16{reg_rd[OP1_MPY] |
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]}};
225 wire [15:0] per_dout = op1_mux |
232 //============================================================================
233 // 5) HARDWARE MULTIPLIER FUNCTIONAL LOGIC
234 //============================================================================
236 // Multiplier configuration
237 //--------------------------
239 // Detect signed mode
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];
246 // Detect accumulate mode
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];
253 // Detect whenever the RESHI and RESLO registers should be cleared
254 assign result_clr = op2_wr & ~acc_sel;
256 // Combine RESHI & RESLO
257 wire [31:0] result = {reshi, reslo};
260 // 16x16 Multiplier (result computed in 1 clock cycle)
261 //-----------------------------------------------------
264 // Detect start of a multiplication
266 always @ (posedge mclk or posedge puc)
267 if (puc) cycle <= 1'b0;
268 else cycle <= op2_wr;
270 assign result_wr = cycle;
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};
277 // 17x17 signed multiplication
278 wire signed [33:0] product = op1_xp * op2_xp;
281 wire [32:0] result_nxt = {1'b0, result} + {1'b0, product[31:0]};
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]};
291 // Since the MAC is completed within 1 clock cycle,
292 // an early read can't happen.
293 assign early_read = 1'b0;
296 // 16x8 Multiplier (result computed in 2 clock cycles)
297 //-----------------------------------------------------
300 // Detect start of a multiplication
302 always @ (posedge mclk or posedge puc)
303 if (puc) cycle <= 2'b00;
304 else cycle <= {cycle[0], op2_wr};
306 assign result_wr = |cycle;
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;
316 // 17x9 signed multiplication
317 wire signed [25:0] product = op1_xp * op2_xp;
319 wire [31:0] product_xp = cycle[0] ? {product[23:0], 8'h00} :
320 {{8{sign_sel & product[23]}}, product[23:0]};
323 wire [32:0] result_nxt = {1'b0, result} + {1'b0, product_xp[31:0]};
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]};
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];
339 endmodule // omsp_multiplier
341 `include "openMSP430_undefines.v"