1 |
4 |
tomburkeii |
`timescale 1ns / 1ps
|
2 |
|
|
//////////////////////////////////////////////////////////////////////////////////
|
3 |
|
|
// Company:
|
4 |
|
|
// Engineer:
|
5 |
|
|
//
|
6 |
|
|
// Create Date: 13:33:36 01/02/2014
|
7 |
|
|
// Design Name:
|
8 |
|
|
// Module Name: qmults
|
9 |
|
|
// Project Name:
|
10 |
|
|
// Target Devices:
|
11 |
|
|
// Tool versions:
|
12 |
|
|
// Description:
|
13 |
|
|
//
|
14 |
|
|
// Dependencies:
|
15 |
|
|
//
|
16 |
|
|
// Revision:
|
17 |
|
|
// Revision 0.01 - File Created
|
18 |
|
|
// Additional Comments:
|
19 |
|
|
//
|
20 |
|
|
//////////////////////////////////////////////////////////////////////////////////
|
21 |
|
|
module qmults#(
|
22 |
|
|
//Parameterized values
|
23 |
|
|
parameter Q = 15,
|
24 |
|
|
parameter N = 32
|
25 |
|
|
)
|
26 |
|
|
(
|
27 |
|
|
input [N-1:0] i_multiplicand,
|
28 |
|
|
input [N-1:0] i_multiplier,
|
29 |
|
|
input i_start,
|
30 |
|
|
input i_clk,
|
31 |
|
|
output [N-1:0] o_result_out,
|
32 |
|
|
output o_complete,
|
33 |
|
|
output o_overflow
|
34 |
|
|
);
|
35 |
|
|
|
36 |
|
|
reg [2*N-2:0] reg_working_result; // a place to accumulate our result
|
37 |
|
|
reg [2*N-2:0] reg_multiplier_temp; // a working copy of the multiplier
|
38 |
|
|
reg [N-1:0] reg_multiplicand_temp; // a working copy of the umultiplicand
|
39 |
|
|
|
40 |
7 |
tomburkeii |
reg [N-1:0] reg_count; // This is obviously a lot bigger than it needs to be, as we only need
|
41 |
4 |
tomburkeii |
// count to N, but computing that number of bits requires a
|
42 |
|
|
// logarithm (base 2), and I don't know how to do that in a
|
43 |
|
|
// way that will work for every possibility
|
44 |
|
|
|
45 |
|
|
reg reg_done; // Computation completed flag
|
46 |
|
|
reg reg_sign; // The result's sign bit
|
47 |
|
|
reg reg_overflow; // Overflow flag
|
48 |
|
|
|
49 |
|
|
initial reg_done = 1'b1; // Initial state is to not be doing anything
|
50 |
|
|
initial reg_overflow = 1'b0; // And there should be no woverflow present
|
51 |
|
|
initial reg_sign = 1'b0; // And the sign should be positive
|
52 |
|
|
|
53 |
|
|
assign o_result_out[N-2:0] = reg_working_result[N-2+Q:Q]; // The multiplication results
|
54 |
|
|
assign o_result_out[N-1] = reg_sign; // The sign of the result
|
55 |
|
|
assign o_complete = reg_done; // "Done" flag
|
56 |
|
|
assign o_overflow = reg_overflow; // Overflow flag
|
57 |
|
|
|
58 |
|
|
always @( posedge i_clk ) begin
|
59 |
|
|
if( reg_done && i_start ) begin // This is our startup condition
|
60 |
|
|
reg_done <= 1'b0; // We're not done
|
61 |
|
|
reg_count <= 0; // Reset the count
|
62 |
|
|
reg_working_result <= 0; // Clear out the result register
|
63 |
|
|
reg_multiplier_temp <= 0; // Clear out the multiplier register
|
64 |
|
|
reg_multiplicand_temp <= 0; // Clear out the multiplicand register
|
65 |
|
|
reg_overflow <= 1'b0; // Clear the overflow register
|
66 |
|
|
|
67 |
|
|
reg_multiplicand_temp <= i_multiplicand[N-2:0]; // Load the multiplicand in its working register and lose the sign bit
|
68 |
|
|
reg_multiplier_temp <= i_multiplier[N-2:0]; // Load the multiplier into its working register and lose the sign bit
|
69 |
|
|
|
70 |
|
|
reg_sign <= i_multiplicand[N-1] ^ i_multiplier[N-1]; // Set the sign bit
|
71 |
|
|
end
|
72 |
|
|
|
73 |
|
|
else if (!reg_done) begin
|
74 |
|
|
if (reg_multiplicand_temp[reg_count] == 1'b1) // if the appropriate multiplicand bit is 1
|
75 |
|
|
reg_working_result <= reg_working_result + reg_multiplier_temp; // then add the temp multiplier
|
76 |
|
|
|
77 |
|
|
reg_multiplier_temp <= reg_multiplier_temp << 1; // Do a left-shift on the multiplier
|
78 |
|
|
reg_count <= reg_count + 1; // Increment the count
|
79 |
|
|
|
80 |
|
|
//stop condition
|
81 |
|
|
if(reg_count == N) begin
|
82 |
|
|
reg_done <= 1'b1; // If we're done, it's time to tell the calling process
|
83 |
|
|
if (reg_working_result[2*N-2:N-1+Q] > 0) // Check for an overflow
|
84 |
|
|
reg_overflow <= 1'b1;
|
85 |
|
|
// else
|
86 |
|
|
// reg_count <= reg_count + 1; // Increment the count
|
87 |
|
|
end
|
88 |
|
|
end
|
89 |
|
|
end
|
90 |
|
|
endmodule
|