1 |
2 |
alfik |
/*
|
2 |
|
|
* Copyright (c) 2014, Aleksander Osman
|
3 |
|
|
* All rights reserved.
|
4 |
|
|
*
|
5 |
|
|
* Redistribution and use in source and binary forms, with or without
|
6 |
|
|
* modification, are permitted provided that the following conditions are met:
|
7 |
|
|
*
|
8 |
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
9 |
|
|
* list of conditions and the following disclaimer.
|
10 |
|
|
*
|
11 |
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
12 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
13 |
|
|
* and/or other materials provided with the distribution.
|
14 |
|
|
*
|
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18 |
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
21 |
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
22 |
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
23 |
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
24 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
`include "defines.v"
|
28 |
|
|
|
29 |
|
|
module execute_multiply(
|
30 |
|
|
input clk,
|
31 |
|
|
input rst_n,
|
32 |
|
|
|
33 |
|
|
input exe_reset,
|
34 |
|
|
|
35 |
|
|
input [6:0] exe_cmd,
|
36 |
|
|
input exe_is_8bit,
|
37 |
|
|
input exe_operand_16bit,
|
38 |
|
|
input exe_operand_32bit,
|
39 |
|
|
|
40 |
|
|
input [31:0] src,
|
41 |
|
|
input [31:0] dst,
|
42 |
|
|
|
43 |
|
|
//
|
44 |
|
|
output [65:0] mult_result,
|
45 |
|
|
output mult_busy,
|
46 |
|
|
|
47 |
|
|
output exe_mult_overflow
|
48 |
|
|
);
|
49 |
|
|
|
50 |
|
|
//------------------------------------------------------------------------------ MUL, IMUL, AAD
|
51 |
|
|
|
52 |
|
|
wire mult_start;
|
53 |
|
|
|
54 |
|
|
reg [1:0] mult_counter;
|
55 |
|
|
|
56 |
|
|
wire [32:0] mult_a;
|
57 |
|
|
wire [32:0] mult_b;
|
58 |
|
|
|
59 |
|
|
//------------------------------------------------------------------------------
|
60 |
|
|
|
61 |
|
|
assign mult_start = mult_counter == 2'd0 && (exe_cmd == `CMD_IMUL || exe_cmd == `CMD_MUL || exe_cmd == `CMD_AAD);
|
62 |
|
|
assign mult_busy = mult_counter != 2'd1;
|
63 |
|
|
//mult_end condition: mult_counter == 2'd1
|
64 |
|
|
|
65 |
|
|
always @(posedge clk or negedge rst_n) begin
|
66 |
|
|
if(rst_n == 1'b0) mult_counter <= 2'd0;
|
67 |
|
|
else if(exe_reset) mult_counter <= 2'd0;
|
68 |
|
|
else if(mult_start) mult_counter <= 2'd2;
|
69 |
|
|
else if(mult_counter != 2'd0) mult_counter <= mult_counter - 2'd1;
|
70 |
|
|
end
|
71 |
|
|
|
72 |
|
|
assign mult_a =
|
73 |
|
|
(exe_is_8bit)? { {25{(exe_cmd == `CMD_IMUL) & src[7]}}, src[7:0] } :
|
74 |
|
|
(exe_operand_16bit)? { {17{(exe_cmd == `CMD_IMUL) & src[15]}}, src[15:0] } :
|
75 |
|
|
{ ((exe_cmd == `CMD_IMUL) & src[31]), src };
|
76 |
|
|
|
77 |
|
|
assign mult_b =
|
78 |
|
|
(exe_cmd == `CMD_AAD)? { 25'd0, dst[15:8] } :
|
79 |
|
|
(exe_is_8bit)? { {25{(exe_cmd == `CMD_IMUL) & dst[7]}}, dst[7:0] } :
|
80 |
|
|
(exe_operand_16bit)? { {17{(exe_cmd == `CMD_IMUL) & dst[15]}}, dst[15:0] } :
|
81 |
|
|
{ ((exe_cmd == `CMD_IMUL) & dst[31]), dst };
|
82 |
|
|
|
83 |
|
|
simple_mult
|
84 |
|
|
#(
|
85 |
|
|
.widtha (33),
|
86 |
|
|
.widthb (33),
|
87 |
|
|
.widthp (66)
|
88 |
|
|
)
|
89 |
|
|
mult_inst(
|
90 |
|
|
.clk (clk),
|
91 |
|
|
.a (mult_a),
|
92 |
|
|
.b (mult_b),
|
93 |
|
|
.out (mult_result)
|
94 |
|
|
);
|
95 |
|
|
|
96 |
|
|
assign exe_mult_overflow =
|
97 |
|
|
(exe_is_8bit && mult_result[65:8] != {58{(exe_cmd == `CMD_IMUL) & mult_result[7]}}) ||
|
98 |
|
|
(exe_operand_16bit && mult_result[65:16] != {50{(exe_cmd == `CMD_IMUL) & mult_result[15]}}) ||
|
99 |
|
|
(exe_operand_32bit && mult_result[65:32] != {34{(exe_cmd == `CMD_IMUL) & mult_result[31]}});
|
100 |
|
|
|
101 |
|
|
//------------------------------------------------------------------------------
|
102 |
|
|
|
103 |
|
|
endmodule
|