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

Subversion Repositories amber

[/] [amber/] [trunk/] [hw/] [vlog/] [amber23/] [a23_multiply.v] - Blame information for rev 72

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

Line No. Rev Author Line
1 2 csantifort
//////////////////////////////////////////////////////////////////
2
//                                                              //
3
//  Multiplication Module for Amber 2 Core                      //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  64-bit Booth signed or unsigned multiply and                //
10
//  multiply-accumulate supported. It takes about 38 clock      //
11
//  cycles to complete an operation.                            //
12
//                                                              //
13
//  Author(s):                                                  //
14
//      - Conor Santifort, csantifort.amber@gmail.com           //
15
//                                                              //
16
//////////////////////////////////////////////////////////////////
17
//                                                              //
18
// Copyright (C) 2010 Authors and OPENCORES.ORG                 //
19
//                                                              //
20
// This source file may be used and distributed without         //
21
// restriction provided that this copyright statement is not    //
22
// removed from the file and that any derivative work contains  //
23
// the original copyright notice and the associated disclaimer. //
24
//                                                              //
25
// This source file is free software; you can redistribute it   //
26
// and/or modify it under the terms of the GNU Lesser General   //
27
// Public License as published by the Free Software Foundation; //
28
// either version 2.1 of the License, or (at your option) any   //
29
// later version.                                               //
30
//                                                              //
31
// This source is distributed in the hope that it will be       //
32
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
33
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
34
// PURPOSE.  See the GNU Lesser General Public License for more //
35
// details.                                                     //
36
//                                                              //
37
// You should have received a copy of the GNU Lesser General    //
38
// Public License along with this source; if not, download it   //
39
// from http://www.opencores.org/lgpl.shtml                     //
40
//                                                              //
41
//////////////////////////////////////////////////////////////////
42
 
43
 
44
 
45
// bit 0 go, bit 1 accumulate
46
// Command:
47
//  4'b01 :  MUL   - 32 bit multiplication
48
//  4'b11 :  MLA   - 32 bit multiply and accumulate
49
//
50
//  34-bit Booth adder
51
//  The adder needs to be 34 bit to deal with signed and unsigned 32-bit
52
//  multiplication inputs. This adds 1 extra bit. Then to deal with the
53
//  case of two max negative numbers another bit is required.
54
//
55
 
56 15 csantifort
module a23_multiply (
57 2 csantifort
input                       i_clk,
58
input                       i_fetch_stall,
59
 
60
input       [31:0]          i_a_in,         // Rds
61
input       [31:0]          i_b_in,         // Rm
62
input       [1:0]           i_function,
63
input                       i_execute,
64
 
65
output      [31:0]          o_out,
66
output      [1:0]           o_flags,        // [1] = N, [0] = Z
67
output reg                  o_done = 'd0    // goes high 2 cycles before completion                                          
68
);
69
 
70
 
71
wire        enable;
72
wire        accumulate;
73
wire [33:0] multiplier;
74
wire [33:0] multiplier_bar;
75
wire [33:0] sum;
76
wire [33:0] sum34_b;
77
 
78
reg  [5:0]  count = 'd0;
79
reg  [5:0]  count_nxt;
80
reg  [67:0] product = 'd0;
81
reg  [67:0] product_nxt;
82
reg  [1:0]  flags_nxt;
83
wire [32:0] sum_acc1;           // the MSB is the carry out for the upper 32 bit addition
84
 
85
 
86
assign enable         = i_function[0];
87
assign accumulate     = i_function[1];
88
 
89
assign multiplier     =  { 2'd0, i_a_in} ;
90
assign multiplier_bar = ~{ 2'd0, i_a_in} + 34'd1 ;
91
 
92
assign sum34_b        =  product[1:0] == 2'b01 ? multiplier     :
93
                         product[1:0] == 2'b10 ? multiplier_bar :
94
                                                 34'd0          ;
95
 
96
 
97
// Use DSP modules from Xilinx Spartan6 FPGA devices
98
`ifdef XILINX_FPGA
99
    // -----------------------------------
100
    // 34-bit adder - booth multiplication
101
    // -----------------------------------
102
    `ifdef XILINX_SPARTAN6_FPGA
103
        xs6_addsub_n #(.WIDTH(34))
104
    `endif
105
    `ifdef XILINX_VIRTEX6_FPGA
106
        xv6_addsub_n #(.WIDTH(34))
107
    `endif
108
 
109
        u_xx_addsub_34_sum (
110
        .i_a    ( product[67:34]        ),
111
        .i_b    ( sum34_b               ),
112
        .i_cin  ( 1'd0                  ),
113
        .i_sub  ( 1'd0                  ),
114
        .o_sum  ( sum                   ),
115
        .o_co   (                       )
116
    );
117
 
118
    // ------------------------------------
119
    // 33-bit adder - accumulate operations
120
    // ------------------------------------
121
    `ifdef XILINX_SPARTAN6_FPGA
122
        xs6_addsub_n #(.WIDTH(33))
123
    `endif
124
    `ifdef XILINX_VIRTEX6_FPGA
125
        xv6_addsub_n #(.WIDTH(33))
126
    `endif
127
        u_xx_addsub_33_acc1 (
128
        .i_a    ( {1'd0, product[32:1]} ),
129
        .i_b    ( {1'd0, i_a_in}        ),
130
        .i_cin  ( 1'd0                  ),
131
        .i_sub  ( 1'd0                  ),
132
        .o_sum  ( sum_acc1              ),
133
        .o_co   (                       )
134
    );
135
 
136
`else
137
 
138
    // -----------------------------------
139
    // 34-bit adder - booth multiplication
140
    // -----------------------------------
141
    assign sum =  product[67:34] + sum34_b;
142
 
143
    // ------------------------------------
144
    // 33-bit adder - accumulate operations
145
    // ------------------------------------
146
    assign sum_acc1 = {1'd0, product[32:1]} + {1'd0, i_a_in};
147
 
148
`endif
149
 
150
 
151
always @*
152
    begin
153
    // Defaults
154
    count_nxt           = count;
155
    product_nxt         = product;
156
 
157
    // update Negative and Zero flags
158
    // Use registered value of product so this adds an extra cycle
159
    // but this avoids having the 64-bit zero comparator on the
160
    // main adder path
161
    flags_nxt   = { product[32], product[32:1] == 32'd0 };
162
 
163
 
164
    if ( count == 6'd0 )
165
        product_nxt = {33'd0, 1'd0, i_b_in, 1'd0 } ;
166
    else if ( count <= 6'd33 )
167
        product_nxt = { sum[33], sum, product[33:1]} ;
168
    else if ( count == 6'd34 && accumulate )
169
        begin
170
        // Note that bit 0 is not part of the product. It is used during the booth
171
        // multiplication algorithm
172
        product_nxt         = { product[64:33], sum_acc1[31:0], 1'd0}; // Accumulate
173
        end
174
 
175
    // Multiplication state counter
176
    if (count == 6'd0)  // start
177
        count_nxt   = enable ? 6'd1 : 6'd0;
178
    else if ((count == 6'd34 && !accumulate) ||  // MUL
179
             (count == 6'd35 &&  accumulate)  )  // MLA
180
        count_nxt   = 6'd0;
181
    else
182
        count_nxt   = count + 1'd1;
183
    end
184
 
185
 
186
always @ ( posedge i_clk )
187
    if ( !i_fetch_stall )
188
        begin
189
        count           <= i_execute ? count_nxt          : count;
190
        product         <= i_execute ? product_nxt        : product;
191
        o_done          <= i_execute ? count == 6'd31     : o_done;
192
        end
193
 
194
// Outputs
195
assign o_out   = product[32:1];
196
assign o_flags = flags_nxt;
197
 
198
endmodule
199
 
200
 

powered by: WebSVN 2.1.0

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