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