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

Subversion Repositories openmsp430

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

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

Line No. Rev Author Line
1 67 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 103 olivier.gi
`ifdef OMSP_NO_INCLUDE
40
`else
41 67 olivier.gi
`include "openMSP430_defines.v"
42 103 olivier.gi
`endif
43 67 olivier.gi
 
44
module  omsp_multiplier (
45
 
46
// OUTPUTs
47
    per_dout,                       // Peripheral data output
48
 
49
// INPUTs
50
    mclk,                           // Main system clock
51
    per_addr,                       // Peripheral address
52
    per_din,                        // Peripheral data input
53
    per_en,                         // Peripheral enable (high active)
54
    per_wen,                        // Peripheral write enable (high active)
55
    puc                             // Main system reset
56
);
57
 
58
// OUTPUTs
59
//=========
60
output       [15:0] per_dout;       // Peripheral data output
61
 
62
// INPUTs
63
//=========
64
input               mclk;           // Main system clock
65
input         [7:0] per_addr;       // Peripheral address
66
input        [15:0] per_din;        // Peripheral data input
67
input               per_en;         // Peripheral enable (high active)
68
input         [1:0] per_wen;        // Peripheral write enable (high active)
69
input               puc;            // Main system reset
70
 
71
 
72
//=============================================================================
73
// 1)  PARAMETER/REGISTERS & WIRE DECLARATION
74
//=============================================================================
75
 
76
// Register addresses
77
parameter           OP1_MPY    = 9'h130;
78
parameter           OP1_MPYS   = 9'h132;
79
parameter           OP1_MAC    = 9'h134;
80
parameter           OP1_MACS   = 9'h136;
81
parameter           OP2        = 9'h138;
82
parameter           RESLO      = 9'h13A;
83
parameter           RESHI      = 9'h13C;
84
parameter           SUMEXT     = 9'h13E;
85
 
86
 
87
// Register one-hot decoder
88
parameter           OP1_MPY_D  = (512'h1 << OP1_MPY);
89
parameter           OP1_MPYS_D = (512'h1 << OP1_MPYS);
90
parameter           OP1_MAC_D  = (512'h1 << OP1_MAC);
91
parameter           OP1_MACS_D = (512'h1 << OP1_MACS);
92
parameter           OP2_D      = (512'h1 << OP2);
93
parameter           RESLO_D    = (512'h1 << RESLO);
94
parameter           RESHI_D    = (512'h1 << RESHI);
95
parameter           SUMEXT_D   = (512'h1 << SUMEXT);
96
 
97
 
98
// Wire pre-declarations
99
wire  result_wr;
100
wire  result_clr;
101
wire  early_read;
102
 
103
 
104
//============================================================================
105
// 2)  REGISTER DECODER
106
//============================================================================
107
 
108
// Register address decode
109
reg  [511:0]  reg_dec;
110
always @(per_addr)
111
  case ({per_addr,1'b0})
112
    OP1_MPY  :  reg_dec  =  OP1_MPY_D;
113
    OP1_MPYS :  reg_dec  =  OP1_MPYS_D;
114
    OP1_MAC  :  reg_dec  =  OP1_MAC_D;
115
    OP1_MACS :  reg_dec  =  OP1_MACS_D;
116
    OP2      :  reg_dec  =  OP2_D;
117
    RESLO    :  reg_dec  =  RESLO_D;
118
    RESHI    :  reg_dec  =  RESHI_D;
119
    SUMEXT   :  reg_dec  =  SUMEXT_D;
120
    default  :  reg_dec  =  {512{1'b0}};
121
  endcase
122
 
123
// Read/Write probes
124
wire         reg_write =  |per_wen   & per_en;
125
wire         reg_read  = ~|per_wen   & per_en;
126
 
127
// Read/Write vectors
128
wire [511:0] reg_wr    = reg_dec & {512{reg_write}};
129
wire [511:0] reg_rd    = reg_dec & {512{reg_read}};
130
 
131
 
132
//============================================================================
133
// 3) REGISTERS
134
//============================================================================
135
 
136
// OP1 Register
137
//-----------------   
138
reg  [15:0] op1;
139
 
140
wire        op1_wr = reg_wr[OP1_MPY]  |
141
                     reg_wr[OP1_MPYS] |
142
                     reg_wr[OP1_MAC]  |
143
                     reg_wr[OP1_MACS];
144
 
145
always @ (posedge mclk or posedge puc)
146
  if (puc)          op1 <=  16'h0000;
147
  else if (op1_wr)  op1 <=  per_din;
148
 
149
wire [15:0] op1_rd  = op1;
150
 
151
 
152
// OP2 Register
153
//-----------------   
154
reg  [15:0] op2;
155
 
156
wire        op2_wr = reg_wr[OP2];
157
 
158
always @ (posedge mclk or posedge puc)
159
  if (puc)          op2 <=  16'h0000;
160
  else if (op2_wr)  op2 <=  per_din;
161
 
162
wire [15:0] op2_rd  = op2;
163
 
164
 
165
// RESLO Register
166
//-----------------   
167
reg  [15:0] reslo;
168
 
169
wire [15:0] reslo_nxt;
170
wire        reslo_wr = reg_wr[RESLO];
171
 
172
always @ (posedge mclk or posedge puc)
173
  if (puc)             reslo <=  16'h0000;
174
  else if (reslo_wr)   reslo <=  per_din;
175
  else if (result_clr) reslo <=  16'h0000;
176
  else if (result_wr)  reslo <=  reslo_nxt;
177
 
178
wire [15:0] reslo_rd = early_read ? reslo_nxt : reslo;
179
 
180
 
181
// RESHI Register
182
//-----------------   
183
reg  [15:0] reshi;
184
 
185
wire [15:0] reshi_nxt;
186
wire        reshi_wr = reg_wr[RESHI];
187
 
188
always @ (posedge mclk or posedge puc)
189
  if (puc)             reshi <=  16'h0000;
190
  else if (reshi_wr)   reshi <=  per_din;
191
  else if (result_clr) reshi <=  16'h0000;
192
  else if (result_wr)  reshi <=  reshi_nxt;
193
 
194
wire [15:0] reshi_rd = early_read ? reshi_nxt  : reshi;
195
 
196
 
197
// SUMEXT Register
198
//-----------------   
199
reg  [1:0] sumext_s;
200
 
201
wire [1:0] sumext_s_nxt;
202
 
203
always @ (posedge mclk or posedge puc)
204
  if (puc)             sumext_s <=  2'b00;
205
  else if (op2_wr)     sumext_s <=  2'b00;
206
  else if (result_wr)  sumext_s <=  sumext_s_nxt;
207
 
208
wire [15:0] sumext_nxt = {{14{sumext_s_nxt[1]}}, sumext_s_nxt};
209
wire [15:0] sumext     = {{14{sumext_s[1]}},     sumext_s};
210
wire [15:0] sumext_rd  = early_read ? sumext_nxt : sumext;
211
 
212
 
213
//============================================================================
214
// 4) DATA OUTPUT GENERATION
215
//============================================================================
216
 
217
// Data output mux
218
wire [15:0] op1_mux    = op1_rd     & {16{reg_rd[OP1_MPY]  |
219
                                          reg_rd[OP1_MPYS] |
220
                                          reg_rd[OP1_MAC]  |
221
                                          reg_rd[OP1_MACS]}};
222
wire [15:0] op2_mux    = op2_rd     & {16{reg_rd[OP2]}};
223
wire [15:0] reslo_mux  = reslo_rd   & {16{reg_rd[RESLO]}};
224
wire [15:0] reshi_mux  = reshi_rd   & {16{reg_rd[RESHI]}};
225
wire [15:0] sumext_mux = sumext_rd  & {16{reg_rd[SUMEXT]}};
226
 
227
wire [15:0] per_dout   = op1_mux    |
228
                         op2_mux    |
229
                         reslo_mux  |
230
                         reshi_mux  |
231
                         sumext_mux;
232
 
233
 
234
//============================================================================
235
// 5) HARDWARE MULTIPLIER FUNCTIONAL LOGIC
236
//============================================================================
237
 
238
// Multiplier configuration
239
//--------------------------
240
 
241
// Detect signed mode
242
reg sign_sel;
243
always @ (posedge mclk or posedge puc)
244
  if (puc)         sign_sel <=  1'b0;
245
  else if (op1_wr) sign_sel <=  reg_wr[OP1_MPYS] | reg_wr[OP1_MACS];
246
 
247
 
248
// Detect accumulate mode
249
reg acc_sel;
250
always @ (posedge mclk or posedge puc)
251
  if (puc)         acc_sel  <=  1'b0;
252
  else if (op1_wr) acc_sel  <=  reg_wr[OP1_MAC]  | reg_wr[OP1_MACS];
253
 
254
 
255
// Detect whenever the RESHI and RESLO registers should be cleared
256
assign      result_clr = op2_wr & ~acc_sel;
257
 
258
// Combine RESHI & RESLO 
259
wire [31:0] result     = {reshi, reslo};
260
 
261
 
262
// 16x16 Multiplier (result computed in 1 clock cycle)
263
//-----------------------------------------------------
264
`ifdef MPY_16x16
265
 
266
// Detect start of a multiplication
267
reg cycle;
268
always @ (posedge mclk or posedge puc)
269
  if (puc) cycle <=  1'b0;
270
  else     cycle <=  op2_wr;
271
 
272
assign result_wr = cycle;
273
 
274
// Expand the operands to support signed & unsigned operations
275
wire signed [16:0] op1_xp = {sign_sel & op1[15], op1};
276
wire signed [16:0] op2_xp = {sign_sel & op2[15], op2};
277
 
278
 
279
// 17x17 signed multiplication
280
wire signed [33:0] product = op1_xp * op2_xp;
281
 
282
// Accumulate
283
wire [32:0] result_nxt = {1'b0, result} + {1'b0, product[31:0]};
284
 
285
 
286
// Next register values
287
assign reslo_nxt    = result_nxt[15:0];
288
assign reshi_nxt    = result_nxt[31:16];
289
assign sumext_s_nxt =  sign_sel ? {2{result_nxt[31]}} :
290
                                  {1'b0, result_nxt[32]};
291
 
292
 
293
// Since the MAC is completed within 1 clock cycle,
294
// an early read can't happen.
295
assign early_read   = 1'b0;
296
 
297
 
298
// 16x8 Multiplier (result computed in 2 clock cycles)
299
//-----------------------------------------------------
300
`else
301
 
302
// Detect start of a multiplication
303
reg [1:0] cycle;
304
always @ (posedge mclk or posedge puc)
305
  if (puc) cycle <=  2'b00;
306
  else     cycle <=  {cycle[0], op2_wr};
307
 
308
assign result_wr = |cycle;
309
 
310
 
311
// Expand the operands to support signed & unsigned operations
312
wire signed [16:0] op1_xp    = {sign_sel & op1[15], op1};
313
wire signed  [8:0] op2_hi_xp = {sign_sel & op2[15], op2[15:8]};
314
wire signed  [8:0] op2_lo_xp = {              1'b0, op2[7:0]};
315
wire signed  [8:0] op2_xp    = cycle[0] ? op2_hi_xp : op2_lo_xp;
316
 
317
 
318
// 17x9 signed multiplication
319
wire signed [25:0] product    = op1_xp * op2_xp;
320
 
321
wire        [31:0] product_xp = cycle[0] ? {product[23:0], 8'h00} :
322
                                           {{8{sign_sel & product[23]}}, product[23:0]};
323
 
324
// Accumulate
325
wire [32:0] result_nxt  = {1'b0, result} + {1'b0, product_xp[31:0]};
326
 
327
 
328
// Next register values
329
assign reslo_nxt    = result_nxt[15:0];
330
assign reshi_nxt    = result_nxt[31:16];
331
assign sumext_s_nxt =  sign_sel ? {2{result_nxt[31]}} :
332
                                  {1'b0, result_nxt[32] | sumext_s[0]};
333
 
334
// Since the MAC is completed within 2 clock cycle,
335
// an early read can happen during the second cycle.
336
assign early_read   = cycle[1];
337
 
338
`endif
339
 
340
 
341
endmodule // omsp_multiplier
342
 
343 103 olivier.gi
`ifdef OMSP_NO_INCLUDE
344
`else
345 67 olivier.gi
`include "openMSP430_undefines.v"
346 103 olivier.gi
`endif

powered by: WebSVN 2.1.0

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