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

Subversion Repositories openmsp430

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

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 186 olivier.gi
wire              reg_sel     =  per_en & (per_addr[13:DEC_WD-1]==BASE_ADDR[14:DEC_WD]);
125 111 olivier.gi
 
126
// Register local address
127 186 olivier.gi
wire [DEC_WD-1:0] reg_addr    =  {per_addr[DEC_WD-2:0], 1'b0};
128 111 olivier.gi
 
129 67 olivier.gi
// Register address decode
130 186 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 111 olivier.gi
 
139 67 olivier.gi
// Read/Write probes
140 186 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 186 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 186 olivier.gi
// Masked input data for byte access
148
wire       [15:0] per_din_msk =  per_din & {{8{per_we[1]}}, 8'hff};
149 67 olivier.gi
 
150
//============================================================================
151
// 3) REGISTERS
152
//============================================================================
153
 
154
// OP1 Register
155
//-----------------   
156
reg  [15:0] op1;
157
 
158
wire        op1_wr = reg_wr[OP1_MPY]  |
159
                     reg_wr[OP1_MPYS] |
160
                     reg_wr[OP1_MAC]  |
161
                     reg_wr[OP1_MACS];
162
 
163 134 olivier.gi
`ifdef CLOCK_GATING
164
wire        mclk_op1;
165
omsp_clock_gate clock_gate_op1 (.gclk(mclk_op1),
166
                                .clk (mclk), .enable(op1_wr), .scan_enable(scan_enable));
167
`else
168
wire        mclk_op1 = mclk;
169
`endif
170
 
171
always @ (posedge mclk_op1 or posedge puc_rst)
172 111 olivier.gi
  if (puc_rst)      op1 <=  16'h0000;
173 134 olivier.gi
`ifdef CLOCK_GATING
174 186 olivier.gi
  else              op1 <=  per_din_msk;
175 134 olivier.gi
`else
176 186 olivier.gi
  else if (op1_wr)  op1 <=  per_din_msk;
177 134 olivier.gi
`endif
178
 
179 67 olivier.gi
wire [15:0] op1_rd  = op1;
180
 
181
 
182
// OP2 Register
183
//-----------------   
184
reg  [15:0] op2;
185
 
186
wire        op2_wr = reg_wr[OP2];
187
 
188 134 olivier.gi
`ifdef CLOCK_GATING
189
wire        mclk_op2;
190
omsp_clock_gate clock_gate_op2 (.gclk(mclk_op2),
191
                                .clk (mclk), .enable(op2_wr), .scan_enable(scan_enable));
192
`else
193
wire        mclk_op2 = mclk;
194
`endif
195
 
196
always @ (posedge mclk_op2 or posedge puc_rst)
197 111 olivier.gi
  if (puc_rst)      op2 <=  16'h0000;
198 134 olivier.gi
`ifdef CLOCK_GATING
199 186 olivier.gi
  else              op2 <=  per_din_msk;
200 134 olivier.gi
`else
201 186 olivier.gi
  else if (op2_wr)  op2 <=  per_din_msk;
202 134 olivier.gi
`endif
203 67 olivier.gi
 
204
wire [15:0] op2_rd  = op2;
205
 
206
 
207
// RESLO Register
208
//-----------------   
209
reg  [15:0] reslo;
210
 
211
wire [15:0] reslo_nxt;
212
wire        reslo_wr = reg_wr[RESLO];
213
 
214 134 olivier.gi
`ifdef CLOCK_GATING
215
wire        reslo_en = reslo_wr | result_clr | result_wr;
216
wire        mclk_reslo;
217
omsp_clock_gate clock_gate_reslo (.gclk(mclk_reslo),
218
                                  .clk (mclk), .enable(reslo_en), .scan_enable(scan_enable));
219
`else
220
wire        mclk_reslo = mclk;
221
`endif
222
 
223
always @ (posedge mclk_reslo or posedge puc_rst)
224 111 olivier.gi
  if (puc_rst)         reslo <=  16'h0000;
225 186 olivier.gi
  else if (reslo_wr)   reslo <=  per_din_msk;
226 67 olivier.gi
  else if (result_clr) reslo <=  16'h0000;
227 134 olivier.gi
`ifdef CLOCK_GATING
228
  else                 reslo <=  reslo_nxt;
229
`else
230 67 olivier.gi
  else if (result_wr)  reslo <=  reslo_nxt;
231 134 olivier.gi
`endif
232 67 olivier.gi
 
233
wire [15:0] reslo_rd = early_read ? reslo_nxt : reslo;
234
 
235
 
236
// RESHI Register
237
//-----------------   
238
reg  [15:0] reshi;
239
 
240
wire [15:0] reshi_nxt;
241
wire        reshi_wr = reg_wr[RESHI];
242
 
243 134 olivier.gi
`ifdef CLOCK_GATING
244
wire        reshi_en = reshi_wr | result_clr | result_wr;
245
wire        mclk_reshi;
246
omsp_clock_gate clock_gate_reshi (.gclk(mclk_reshi),
247
                                  .clk (mclk), .enable(reshi_en), .scan_enable(scan_enable));
248
`else
249
wire        mclk_reshi = mclk;
250
`endif
251
 
252
always @ (posedge mclk_reshi or posedge puc_rst)
253 111 olivier.gi
  if (puc_rst)         reshi <=  16'h0000;
254 186 olivier.gi
  else if (reshi_wr)   reshi <=  per_din_msk;
255 67 olivier.gi
  else if (result_clr) reshi <=  16'h0000;
256 134 olivier.gi
`ifdef CLOCK_GATING
257
  else                 reshi <=  reshi_nxt;
258
`else
259 67 olivier.gi
  else if (result_wr)  reshi <=  reshi_nxt;
260 134 olivier.gi
`endif
261 67 olivier.gi
 
262
wire [15:0] reshi_rd = early_read ? reshi_nxt  : reshi;
263
 
264
 
265
// SUMEXT Register
266
//-----------------   
267
reg  [1:0] sumext_s;
268
 
269
wire [1:0] sumext_s_nxt;
270
 
271 111 olivier.gi
always @ (posedge mclk or posedge puc_rst)
272
  if (puc_rst)         sumext_s <=  2'b00;
273 67 olivier.gi
  else if (op2_wr)     sumext_s <=  2'b00;
274
  else if (result_wr)  sumext_s <=  sumext_s_nxt;
275
 
276
wire [15:0] sumext_nxt = {{14{sumext_s_nxt[1]}}, sumext_s_nxt};
277
wire [15:0] sumext     = {{14{sumext_s[1]}},     sumext_s};
278
wire [15:0] sumext_rd  = early_read ? sumext_nxt : sumext;
279
 
280
 
281
//============================================================================
282
// 4) DATA OUTPUT GENERATION
283
//============================================================================
284
 
285
// Data output mux
286
wire [15:0] op1_mux    = op1_rd     & {16{reg_rd[OP1_MPY]  |
287
                                          reg_rd[OP1_MPYS] |
288
                                          reg_rd[OP1_MAC]  |
289
                                          reg_rd[OP1_MACS]}};
290
wire [15:0] op2_mux    = op2_rd     & {16{reg_rd[OP2]}};
291
wire [15:0] reslo_mux  = reslo_rd   & {16{reg_rd[RESLO]}};
292
wire [15:0] reshi_mux  = reshi_rd   & {16{reg_rd[RESHI]}};
293
wire [15:0] sumext_mux = sumext_rd  & {16{reg_rd[SUMEXT]}};
294
 
295
wire [15:0] per_dout   = op1_mux    |
296
                         op2_mux    |
297
                         reslo_mux  |
298
                         reshi_mux  |
299
                         sumext_mux;
300
 
301
 
302
//============================================================================
303
// 5) HARDWARE MULTIPLIER FUNCTIONAL LOGIC
304
//============================================================================
305
 
306
// Multiplier configuration
307
//--------------------------
308
 
309
// Detect signed mode
310
reg sign_sel;
311 134 olivier.gi
always @ (posedge mclk_op1 or posedge puc_rst)
312 111 olivier.gi
  if (puc_rst)     sign_sel <=  1'b0;
313 134 olivier.gi
`ifdef CLOCK_GATING
314
  else             sign_sel <=  reg_wr[OP1_MPYS] | reg_wr[OP1_MACS];
315
`else
316 67 olivier.gi
  else if (op1_wr) sign_sel <=  reg_wr[OP1_MPYS] | reg_wr[OP1_MACS];
317 134 olivier.gi
`endif
318 67 olivier.gi
 
319
 
320
// Detect accumulate mode
321
reg acc_sel;
322 134 olivier.gi
always @ (posedge mclk_op1 or posedge puc_rst)
323 111 olivier.gi
  if (puc_rst)     acc_sel  <=  1'b0;
324 134 olivier.gi
`ifdef CLOCK_GATING
325
  else             acc_sel  <=  reg_wr[OP1_MAC]  | reg_wr[OP1_MACS];
326
`else
327 67 olivier.gi
  else if (op1_wr) acc_sel  <=  reg_wr[OP1_MAC]  | reg_wr[OP1_MACS];
328 134 olivier.gi
`endif
329 67 olivier.gi
 
330
 
331
// Detect whenever the RESHI and RESLO registers should be cleared
332
assign      result_clr = op2_wr & ~acc_sel;
333
 
334
// Combine RESHI & RESLO 
335
wire [31:0] result     = {reshi, reslo};
336
 
337
 
338
// 16x16 Multiplier (result computed in 1 clock cycle)
339
//-----------------------------------------------------
340
`ifdef MPY_16x16
341
 
342
// Detect start of a multiplication
343
reg cycle;
344 111 olivier.gi
always @ (posedge mclk or posedge puc_rst)
345
  if (puc_rst) cycle <=  1'b0;
346
  else         cycle <=  op2_wr;
347 67 olivier.gi
 
348
assign result_wr = cycle;
349
 
350
// Expand the operands to support signed & unsigned operations
351
wire signed [16:0] op1_xp = {sign_sel & op1[15], op1};
352
wire signed [16:0] op2_xp = {sign_sel & op2[15], op2};
353
 
354
 
355
// 17x17 signed multiplication
356
wire signed [33:0] product = op1_xp * op2_xp;
357
 
358
// Accumulate
359
wire [32:0] result_nxt = {1'b0, result} + {1'b0, product[31:0]};
360
 
361
 
362
// Next register values
363
assign reslo_nxt    = result_nxt[15:0];
364
assign reshi_nxt    = result_nxt[31:16];
365
assign sumext_s_nxt =  sign_sel ? {2{result_nxt[31]}} :
366
                                  {1'b0, result_nxt[32]};
367
 
368
 
369
// Since the MAC is completed within 1 clock cycle,
370
// an early read can't happen.
371
assign early_read   = 1'b0;
372
 
373
 
374
// 16x8 Multiplier (result computed in 2 clock cycles)
375
//-----------------------------------------------------
376
`else
377
 
378
// Detect start of a multiplication
379
reg [1:0] cycle;
380 111 olivier.gi
always @ (posedge mclk or posedge puc_rst)
381
  if (puc_rst) cycle <=  2'b00;
382
  else         cycle <=  {cycle[0], op2_wr};
383 67 olivier.gi
 
384
assign result_wr = |cycle;
385
 
386
 
387
// Expand the operands to support signed & unsigned operations
388
wire signed [16:0] op1_xp    = {sign_sel & op1[15], op1};
389
wire signed  [8:0] op2_hi_xp = {sign_sel & op2[15], op2[15:8]};
390
wire signed  [8:0] op2_lo_xp = {              1'b0, op2[7:0]};
391
wire signed  [8:0] op2_xp    = cycle[0] ? op2_hi_xp : op2_lo_xp;
392
 
393
 
394
// 17x9 signed multiplication
395
wire signed [25:0] product    = op1_xp * op2_xp;
396
 
397
wire        [31:0] product_xp = cycle[0] ? {product[23:0], 8'h00} :
398
                                           {{8{sign_sel & product[23]}}, product[23:0]};
399
 
400
// Accumulate
401
wire [32:0] result_nxt  = {1'b0, result} + {1'b0, product_xp[31:0]};
402
 
403
 
404
// Next register values
405
assign reslo_nxt    = result_nxt[15:0];
406
assign reshi_nxt    = result_nxt[31:16];
407
assign sumext_s_nxt =  sign_sel ? {2{result_nxt[31]}} :
408
                                  {1'b0, result_nxt[32] | sumext_s[0]};
409
 
410
// Since the MAC is completed within 2 clock cycle,
411
// an early read can happen during the second cycle.
412
assign early_read   = cycle[1];
413
 
414
`endif
415
 
416
 
417
endmodule // omsp_multiplier
418
 
419 103 olivier.gi
`ifdef OMSP_NO_INCLUDE
420
`else
421 67 olivier.gi
`include "openMSP430_undefines.v"
422 103 olivier.gi
`endif

powered by: WebSVN 2.1.0

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