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

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [core/] [rtl/] [verilog/] [omsp_multiplier.v] - Blame information for rev 134

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 67 olivier.gi
//----------------------------------------------------------------------------
2 117 olivier.gi
// Copyright (C) 2009 , Olivier Girard
3 67 olivier.gi
//
4 117 olivier.gi
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions
6
// are met:
7
//     * Redistributions of source code must retain the above copyright
8
//       notice, this list of conditions and the following disclaimer.
9
//     * Redistributions in binary form must reproduce the above copyright
10
//       notice, this list of conditions and the following disclaimer in the
11
//       documentation and/or other materials provided with the distribution.
12
//     * Neither the name of the authors nor the names of its contributors
13
//       may be used to endorse or promote products derived from this software
14
//       without specific prior written permission.
15 67 olivier.gi
//
16 117 olivier.gi
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26
// THE POSSIBILITY OF SUCH DAMAGE
27 67 olivier.gi
//
28
//----------------------------------------------------------------------------
29
//
30
// *File Name: omsp_multiplier.v
31
// 
32
// *Module Description:
33
//                       16x16 Hardware multiplier.
34
//
35
// *Author(s):
36
//              - Olivier Girard,    olgirard@gmail.com
37
//
38
//----------------------------------------------------------------------------
39
// $Rev: 23 $
40
// $LastChangedBy: olivier.girard $
41
// $LastChangedDate: 2009-08-30 18:39:26 +0200 (Sun, 30 Aug 2009) $
42
//----------------------------------------------------------------------------
43 103 olivier.gi
`ifdef OMSP_NO_INCLUDE
44
`else
45 67 olivier.gi
`include "openMSP430_defines.v"
46 103 olivier.gi
`endif
47 67 olivier.gi
 
48
module  omsp_multiplier (
49
 
50
// OUTPUTs
51
    per_dout,                       // Peripheral data output
52
 
53
// INPUTs
54
    mclk,                           // Main system clock
55
    per_addr,                       // Peripheral address
56
    per_din,                        // Peripheral data input
57
    per_en,                         // Peripheral enable (high active)
58 106 olivier.gi
    per_we,                         // Peripheral write enable (high active)
59 134 olivier.gi
    puc_rst,                        // Main system reset
60
    scan_enable                     // Scan enable (active during scan shifting)
61 67 olivier.gi
);
62
 
63
// OUTPUTs
64
//=========
65
output       [15:0] per_dout;       // Peripheral data output
66
 
67
// INPUTs
68
//=========
69
input               mclk;           // Main system clock
70 111 olivier.gi
input        [13:0] per_addr;       // Peripheral address
71 67 olivier.gi
input        [15:0] per_din;        // Peripheral data input
72
input               per_en;         // Peripheral enable (high active)
73 106 olivier.gi
input         [1:0] per_we;         // Peripheral write enable (high active)
74 111 olivier.gi
input               puc_rst;        // Main system reset
75 134 olivier.gi
input               scan_enable;    // Scan enable (active during scan shifting)
76 67 olivier.gi
 
77
 
78
//=============================================================================
79
// 1)  PARAMETER/REGISTERS & WIRE DECLARATION
80
//=============================================================================
81
 
82 111 olivier.gi
// Register base address (must be aligned to decoder bit width)
83
parameter       [14:0] BASE_ADDR   = 15'h0130;
84 67 olivier.gi
 
85 111 olivier.gi
// Decoder bit width (defines how many bits are considered for address decoding)
86
parameter              DEC_WD      =  4;
87 67 olivier.gi
 
88 111 olivier.gi
// Register addresses offset
89
parameter [DEC_WD-1:0] OP1_MPY     = 'h0,
90
                       OP1_MPYS    = 'h2,
91
                       OP1_MAC     = 'h4,
92
                       OP1_MACS    = 'h6,
93
                       OP2         = 'h8,
94
                       RESLO       = 'hA,
95
                       RESHI       = 'hC,
96
                       SUMEXT      = 'hE;
97
 
98
// Register one-hot decoder utilities
99 134 olivier.gi
parameter              DEC_SZ      =  (1 << DEC_WD);
100 111 olivier.gi
parameter [DEC_SZ-1:0] BASE_REG    =  {{DEC_SZ-1{1'b0}}, 1'b1};
101
 
102 67 olivier.gi
// Register one-hot decoder
103 111 olivier.gi
parameter [DEC_SZ-1:0] OP1_MPY_D   = (BASE_REG << OP1_MPY),
104
                       OP1_MPYS_D  = (BASE_REG << OP1_MPYS),
105
                       OP1_MAC_D   = (BASE_REG << OP1_MAC),
106
                       OP1_MACS_D  = (BASE_REG << OP1_MACS),
107
                       OP2_D       = (BASE_REG << OP2),
108
                       RESLO_D     = (BASE_REG << RESLO),
109
                       RESHI_D     = (BASE_REG << RESHI),
110
                       SUMEXT_D    = (BASE_REG << SUMEXT);
111 67 olivier.gi
 
112
 
113
// Wire pre-declarations
114
wire  result_wr;
115
wire  result_clr;
116
wire  early_read;
117
 
118
 
119
//============================================================================
120
// 2)  REGISTER DECODER
121
//============================================================================
122
 
123 111 olivier.gi
// Local register selection
124
wire              reg_sel   =  per_en & (per_addr[13:DEC_WD-1]==BASE_ADDR[14:DEC_WD]);
125
 
126
// Register local address
127
wire [DEC_WD-1:0] reg_addr  =  {per_addr[DEC_WD-2:0], 1'b0};
128
 
129 67 olivier.gi
// Register address decode
130 111 olivier.gi
wire [DEC_SZ-1:0] reg_dec   =  (OP1_MPY_D   &  {DEC_SZ{(reg_addr == OP1_MPY  )}})  |
131
                               (OP1_MPYS_D  &  {DEC_SZ{(reg_addr == OP1_MPYS )}})  |
132
                               (OP1_MAC_D   &  {DEC_SZ{(reg_addr == OP1_MAC  )}})  |
133
                               (OP1_MACS_D  &  {DEC_SZ{(reg_addr == OP1_MACS )}})  |
134
                               (OP2_D       &  {DEC_SZ{(reg_addr == OP2      )}})  |
135
                               (RESLO_D     &  {DEC_SZ{(reg_addr == RESLO    )}})  |
136
                               (RESHI_D     &  {DEC_SZ{(reg_addr == RESHI    )}})  |
137
                               (SUMEXT_D    &  {DEC_SZ{(reg_addr == SUMEXT   )}});
138
 
139 67 olivier.gi
// Read/Write probes
140 111 olivier.gi
wire              reg_write =  |per_we & reg_sel;
141
wire              reg_read  = ~|per_we & reg_sel;
142 67 olivier.gi
 
143
// Read/Write vectors
144 111 olivier.gi
wire [DEC_SZ-1:0] reg_wr    = reg_dec & {DEC_SZ{reg_write}};
145
wire [DEC_SZ-1:0] reg_rd    = reg_dec & {DEC_SZ{reg_read}};
146 67 olivier.gi
 
147
 
148
//============================================================================
149
// 3) REGISTERS
150
//============================================================================
151
 
152
// OP1 Register
153
//-----------------   
154
reg  [15:0] op1;
155
 
156
wire        op1_wr = reg_wr[OP1_MPY]  |
157
                     reg_wr[OP1_MPYS] |
158
                     reg_wr[OP1_MAC]  |
159
                     reg_wr[OP1_MACS];
160
 
161 134 olivier.gi
`ifdef CLOCK_GATING
162
wire        mclk_op1;
163
omsp_clock_gate clock_gate_op1 (.gclk(mclk_op1),
164
                                .clk (mclk), .enable(op1_wr), .scan_enable(scan_enable));
165
`else
166
wire        mclk_op1 = mclk;
167
`endif
168
 
169
always @ (posedge mclk_op1 or posedge puc_rst)
170 111 olivier.gi
  if (puc_rst)      op1 <=  16'h0000;
171 134 olivier.gi
`ifdef CLOCK_GATING
172
  else              op1 <=  per_din;
173
`else
174 67 olivier.gi
  else if (op1_wr)  op1 <=  per_din;
175 134 olivier.gi
`endif
176
 
177 67 olivier.gi
wire [15:0] op1_rd  = op1;
178
 
179
 
180
// OP2 Register
181
//-----------------   
182
reg  [15:0] op2;
183
 
184
wire        op2_wr = reg_wr[OP2];
185
 
186 134 olivier.gi
`ifdef CLOCK_GATING
187
wire        mclk_op2;
188
omsp_clock_gate clock_gate_op2 (.gclk(mclk_op2),
189
                                .clk (mclk), .enable(op2_wr), .scan_enable(scan_enable));
190
`else
191
wire        mclk_op2 = mclk;
192
`endif
193
 
194
always @ (posedge mclk_op2 or posedge puc_rst)
195 111 olivier.gi
  if (puc_rst)      op2 <=  16'h0000;
196 134 olivier.gi
`ifdef CLOCK_GATING
197
  else              op2 <=  per_din;
198
`else
199 67 olivier.gi
  else if (op2_wr)  op2 <=  per_din;
200 134 olivier.gi
`endif
201 67 olivier.gi
 
202
wire [15:0] op2_rd  = op2;
203
 
204
 
205
// RESLO Register
206
//-----------------   
207
reg  [15:0] reslo;
208
 
209
wire [15:0] reslo_nxt;
210
wire        reslo_wr = reg_wr[RESLO];
211
 
212 134 olivier.gi
`ifdef CLOCK_GATING
213
wire        reslo_en = reslo_wr | result_clr | result_wr;
214
wire        mclk_reslo;
215
omsp_clock_gate clock_gate_reslo (.gclk(mclk_reslo),
216
                                  .clk (mclk), .enable(reslo_en), .scan_enable(scan_enable));
217
`else
218
wire        mclk_reslo = mclk;
219
`endif
220
 
221
always @ (posedge mclk_reslo or posedge puc_rst)
222 111 olivier.gi
  if (puc_rst)         reslo <=  16'h0000;
223 67 olivier.gi
  else if (reslo_wr)   reslo <=  per_din;
224
  else if (result_clr) reslo <=  16'h0000;
225 134 olivier.gi
`ifdef CLOCK_GATING
226
  else                 reslo <=  reslo_nxt;
227
`else
228 67 olivier.gi
  else if (result_wr)  reslo <=  reslo_nxt;
229 134 olivier.gi
`endif
230 67 olivier.gi
 
231
wire [15:0] reslo_rd = early_read ? reslo_nxt : reslo;
232
 
233
 
234
// RESHI Register
235
//-----------------   
236
reg  [15:0] reshi;
237
 
238
wire [15:0] reshi_nxt;
239
wire        reshi_wr = reg_wr[RESHI];
240
 
241 134 olivier.gi
`ifdef CLOCK_GATING
242
wire        reshi_en = reshi_wr | result_clr | result_wr;
243
wire        mclk_reshi;
244
omsp_clock_gate clock_gate_reshi (.gclk(mclk_reshi),
245
                                  .clk (mclk), .enable(reshi_en), .scan_enable(scan_enable));
246
`else
247
wire        mclk_reshi = mclk;
248
`endif
249
 
250
always @ (posedge mclk_reshi or posedge puc_rst)
251 111 olivier.gi
  if (puc_rst)         reshi <=  16'h0000;
252 67 olivier.gi
  else if (reshi_wr)   reshi <=  per_din;
253
  else if (result_clr) reshi <=  16'h0000;
254 134 olivier.gi
`ifdef CLOCK_GATING
255
  else                 reshi <=  reshi_nxt;
256
`else
257 67 olivier.gi
  else if (result_wr)  reshi <=  reshi_nxt;
258 134 olivier.gi
`endif
259 67 olivier.gi
 
260
wire [15:0] reshi_rd = early_read ? reshi_nxt  : reshi;
261
 
262
 
263
// SUMEXT Register
264
//-----------------   
265
reg  [1:0] sumext_s;
266
 
267
wire [1:0] sumext_s_nxt;
268
 
269 111 olivier.gi
always @ (posedge mclk or posedge puc_rst)
270
  if (puc_rst)         sumext_s <=  2'b00;
271 67 olivier.gi
  else if (op2_wr)     sumext_s <=  2'b00;
272
  else if (result_wr)  sumext_s <=  sumext_s_nxt;
273
 
274
wire [15:0] sumext_nxt = {{14{sumext_s_nxt[1]}}, sumext_s_nxt};
275
wire [15:0] sumext     = {{14{sumext_s[1]}},     sumext_s};
276
wire [15:0] sumext_rd  = early_read ? sumext_nxt : sumext;
277
 
278
 
279
//============================================================================
280
// 4) DATA OUTPUT GENERATION
281
//============================================================================
282
 
283
// Data output mux
284
wire [15:0] op1_mux    = op1_rd     & {16{reg_rd[OP1_MPY]  |
285
                                          reg_rd[OP1_MPYS] |
286
                                          reg_rd[OP1_MAC]  |
287
                                          reg_rd[OP1_MACS]}};
288
wire [15:0] op2_mux    = op2_rd     & {16{reg_rd[OP2]}};
289
wire [15:0] reslo_mux  = reslo_rd   & {16{reg_rd[RESLO]}};
290
wire [15:0] reshi_mux  = reshi_rd   & {16{reg_rd[RESHI]}};
291
wire [15:0] sumext_mux = sumext_rd  & {16{reg_rd[SUMEXT]}};
292
 
293
wire [15:0] per_dout   = op1_mux    |
294
                         op2_mux    |
295
                         reslo_mux  |
296
                         reshi_mux  |
297
                         sumext_mux;
298
 
299
 
300
//============================================================================
301
// 5) HARDWARE MULTIPLIER FUNCTIONAL LOGIC
302
//============================================================================
303
 
304
// Multiplier configuration
305
//--------------------------
306
 
307
// Detect signed mode
308
reg sign_sel;
309 134 olivier.gi
always @ (posedge mclk_op1 or posedge puc_rst)
310 111 olivier.gi
  if (puc_rst)     sign_sel <=  1'b0;
311 134 olivier.gi
`ifdef CLOCK_GATING
312
  else             sign_sel <=  reg_wr[OP1_MPYS] | reg_wr[OP1_MACS];
313
`else
314 67 olivier.gi
  else if (op1_wr) sign_sel <=  reg_wr[OP1_MPYS] | reg_wr[OP1_MACS];
315 134 olivier.gi
`endif
316 67 olivier.gi
 
317
 
318
// Detect accumulate mode
319
reg acc_sel;
320 134 olivier.gi
always @ (posedge mclk_op1 or posedge puc_rst)
321 111 olivier.gi
  if (puc_rst)     acc_sel  <=  1'b0;
322 134 olivier.gi
`ifdef CLOCK_GATING
323
  else             acc_sel  <=  reg_wr[OP1_MAC]  | reg_wr[OP1_MACS];
324
`else
325 67 olivier.gi
  else if (op1_wr) acc_sel  <=  reg_wr[OP1_MAC]  | reg_wr[OP1_MACS];
326 134 olivier.gi
`endif
327 67 olivier.gi
 
328
 
329
// Detect whenever the RESHI and RESLO registers should be cleared
330
assign      result_clr = op2_wr & ~acc_sel;
331
 
332
// Combine RESHI & RESLO 
333
wire [31:0] result     = {reshi, reslo};
334
 
335
 
336
// 16x16 Multiplier (result computed in 1 clock cycle)
337
//-----------------------------------------------------
338
`ifdef MPY_16x16
339
 
340
// Detect start of a multiplication
341
reg cycle;
342 111 olivier.gi
always @ (posedge mclk or posedge puc_rst)
343
  if (puc_rst) cycle <=  1'b0;
344
  else         cycle <=  op2_wr;
345 67 olivier.gi
 
346
assign result_wr = cycle;
347
 
348
// Expand the operands to support signed & unsigned operations
349
wire signed [16:0] op1_xp = {sign_sel & op1[15], op1};
350
wire signed [16:0] op2_xp = {sign_sel & op2[15], op2};
351
 
352
 
353
// 17x17 signed multiplication
354
wire signed [33:0] product = op1_xp * op2_xp;
355
 
356
// Accumulate
357
wire [32:0] result_nxt = {1'b0, result} + {1'b0, product[31:0]};
358
 
359
 
360
// Next register values
361
assign reslo_nxt    = result_nxt[15:0];
362
assign reshi_nxt    = result_nxt[31:16];
363
assign sumext_s_nxt =  sign_sel ? {2{result_nxt[31]}} :
364
                                  {1'b0, result_nxt[32]};
365
 
366
 
367
// Since the MAC is completed within 1 clock cycle,
368
// an early read can't happen.
369
assign early_read   = 1'b0;
370
 
371
 
372
// 16x8 Multiplier (result computed in 2 clock cycles)
373
//-----------------------------------------------------
374
`else
375
 
376
// Detect start of a multiplication
377
reg [1:0] cycle;
378 111 olivier.gi
always @ (posedge mclk or posedge puc_rst)
379
  if (puc_rst) cycle <=  2'b00;
380
  else         cycle <=  {cycle[0], op2_wr};
381 67 olivier.gi
 
382
assign result_wr = |cycle;
383
 
384
 
385
// Expand the operands to support signed & unsigned operations
386
wire signed [16:0] op1_xp    = {sign_sel & op1[15], op1};
387
wire signed  [8:0] op2_hi_xp = {sign_sel & op2[15], op2[15:8]};
388
wire signed  [8:0] op2_lo_xp = {              1'b0, op2[7:0]};
389
wire signed  [8:0] op2_xp    = cycle[0] ? op2_hi_xp : op2_lo_xp;
390
 
391
 
392
// 17x9 signed multiplication
393
wire signed [25:0] product    = op1_xp * op2_xp;
394
 
395
wire        [31:0] product_xp = cycle[0] ? {product[23:0], 8'h00} :
396
                                           {{8{sign_sel & product[23]}}, product[23:0]};
397
 
398
// Accumulate
399
wire [32:0] result_nxt  = {1'b0, result} + {1'b0, product_xp[31:0]};
400
 
401
 
402
// Next register values
403
assign reslo_nxt    = result_nxt[15:0];
404
assign reshi_nxt    = result_nxt[31:16];
405
assign sumext_s_nxt =  sign_sel ? {2{result_nxt[31]}} :
406
                                  {1'b0, result_nxt[32] | sumext_s[0]};
407
 
408
// Since the MAC is completed within 2 clock cycle,
409
// an early read can happen during the second cycle.
410
assign early_read   = cycle[1];
411
 
412
`endif
413
 
414
 
415
endmodule // omsp_multiplier
416
 
417 103 olivier.gi
`ifdef OMSP_NO_INCLUDE
418
`else
419 67 olivier.gi
`include "openMSP430_undefines.v"
420 103 olivier.gi
`endif

powered by: WebSVN 2.1.0

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