2020-02-23 09:01:45 +00:00
|
|
|
|
/*
|
|
|
|
|
Copyright 2019 Blue Liang, liangkangnan@163.com
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-01-13 00:26:41 +00:00
|
|
|
|
`include "defines.v"
|
|
|
|
|
|
2020-04-18 12:14:37 +00:00
|
|
|
|
// 除法模块
|
|
|
|
|
// 试商法实现32位整数除法
|
2020-09-14 14:22:42 +00:00
|
|
|
|
// 每次除法运算至少需要33个时钟周期才能完成
|
2020-03-29 15:19:14 +00:00
|
|
|
|
module div(
|
2020-01-13 00:26:41 +00:00
|
|
|
|
|
|
|
|
|
input wire clk,
|
|
|
|
|
input wire rst,
|
|
|
|
|
|
2020-04-18 12:14:37 +00:00
|
|
|
|
// from ex
|
|
|
|
|
input wire[`RegBus] dividend_i, // 被除数
|
|
|
|
|
input wire[`RegBus] divisor_i, // 除数
|
|
|
|
|
input wire start_i, // 开始信号,运算期间这个信号需要一直保持有效
|
|
|
|
|
input wire[2:0] op_i, // 具体是哪一条指令
|
|
|
|
|
input wire[`RegAddrBus] reg_waddr_i, // 运算结束后需要写的寄存器
|
2020-01-13 00:26:41 +00:00
|
|
|
|
|
2020-04-18 12:14:37 +00:00
|
|
|
|
// to ex
|
2020-09-14 14:22:42 +00:00
|
|
|
|
output reg[`RegBus] result_o, // 除法结果,高32位是余数,低32位是商
|
2020-04-18 12:14:37 +00:00
|
|
|
|
output reg ready_o, // 运算结束信号
|
2020-09-14 14:22:42 +00:00
|
|
|
|
output reg busy_o, // 正在运算信号
|
2020-04-18 12:14:37 +00:00
|
|
|
|
output reg[`RegAddrBus] reg_waddr_o // 运算结束后需要写的寄存器
|
2020-01-13 00:26:41 +00:00
|
|
|
|
|
2020-02-23 09:01:45 +00:00
|
|
|
|
);
|
2020-01-13 00:26:41 +00:00
|
|
|
|
|
2020-04-18 12:14:37 +00:00
|
|
|
|
// 状态定义
|
2020-09-14 14:22:42 +00:00
|
|
|
|
localparam STATE_IDLE = 4'b0001;
|
|
|
|
|
localparam STATE_START = 4'b0010;
|
|
|
|
|
localparam STATE_CALC = 4'b0100;
|
|
|
|
|
localparam STATE_END = 4'b1000;
|
2020-09-12 06:17:34 +00:00
|
|
|
|
|
|
|
|
|
reg[`RegBus] dividend_r;
|
|
|
|
|
reg[`RegBus] divisor_r;
|
|
|
|
|
reg[2:0] op_r;
|
2020-09-14 14:22:42 +00:00
|
|
|
|
reg[3:0] state;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
reg[31:0] count;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
reg[`RegBus] div_result;
|
|
|
|
|
reg[`RegBus] div_remain;
|
|
|
|
|
reg[`RegBus] minuend;
|
2020-03-29 15:19:14 +00:00
|
|
|
|
reg invert_result;
|
|
|
|
|
|
2020-09-12 06:17:34 +00:00
|
|
|
|
wire op_div = (op_r == `INST_DIV);
|
|
|
|
|
wire op_divu = (op_r == `INST_DIVU);
|
|
|
|
|
wire op_rem = (op_r == `INST_REM);
|
|
|
|
|
wire op_remu = (op_r == `INST_REMU);
|
2020-03-29 15:19:14 +00:00
|
|
|
|
|
2020-09-14 14:22:42 +00:00
|
|
|
|
wire[31:0] dividend_invert = (-dividend_r);
|
|
|
|
|
wire[31:0] divisor_invert = (-divisor_r);
|
|
|
|
|
wire minuend_ge_divisor = minuend >= divisor_r;
|
|
|
|
|
wire[31:0] minuend_sub_res = minuend - divisor_r;
|
|
|
|
|
wire[31:0] div_result_tmp = minuend_ge_divisor? ({div_result[30:0], 1'b1}): ({div_result[30:0], 1'b0});
|
|
|
|
|
wire[31:0] minuend_tmp = minuend_ge_divisor? minuend_sub_res[30:0]: minuend[30:0];
|
2020-01-13 00:26:41 +00:00
|
|
|
|
|
2020-09-14 14:22:42 +00:00
|
|
|
|
// 状态机实现
|
2020-01-13 00:26:41 +00:00
|
|
|
|
always @ (posedge clk) begin
|
|
|
|
|
if (rst == `RstEnable) begin
|
|
|
|
|
state <= STATE_IDLE;
|
2020-09-14 14:22:42 +00:00
|
|
|
|
ready_o <= `DivResultNotReady;
|
|
|
|
|
result_o <= `ZeroWord;
|
|
|
|
|
div_result <= `ZeroWord;
|
|
|
|
|
div_remain <= `ZeroWord;
|
2020-09-12 06:17:34 +00:00
|
|
|
|
op_r <= 3'h0;
|
2020-09-14 14:22:42 +00:00
|
|
|
|
reg_waddr_o <= `ZeroWord;
|
2020-09-12 06:17:34 +00:00
|
|
|
|
dividend_r <= `ZeroWord;
|
|
|
|
|
divisor_r <= `ZeroWord;
|
2020-09-14 14:22:42 +00:00
|
|
|
|
minuend <= `ZeroWord;
|
|
|
|
|
invert_result <= 1'b0;
|
|
|
|
|
busy_o <= `False;
|
|
|
|
|
count <= `ZeroWord;
|
2020-09-12 06:17:34 +00:00
|
|
|
|
end else begin
|
|
|
|
|
case (state)
|
|
|
|
|
STATE_IDLE: begin
|
|
|
|
|
if (start_i == `DivStart) begin
|
2020-09-14 14:22:42 +00:00
|
|
|
|
op_r <= op_i;
|
|
|
|
|
dividend_r <= dividend_i;
|
2020-09-12 06:17:34 +00:00
|
|
|
|
divisor_r <= divisor_i;
|
2020-09-14 14:22:42 +00:00
|
|
|
|
reg_waddr_o <= reg_waddr_i;
|
|
|
|
|
state <= STATE_START;
|
|
|
|
|
busy_o <= `True;
|
|
|
|
|
end else begin
|
|
|
|
|
op_r <= 3'h0;
|
|
|
|
|
reg_waddr_o <= `ZeroWord;
|
|
|
|
|
dividend_r <= `ZeroWord;
|
|
|
|
|
divisor_r <= `ZeroWord;
|
|
|
|
|
ready_o <= `DivResultNotReady;
|
|
|
|
|
result_o <= `ZeroWord;
|
|
|
|
|
busy_o <= `False;
|
2020-09-12 06:17:34 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
STATE_START: begin
|
2020-09-14 14:22:42 +00:00
|
|
|
|
if (start_i == `DivStart) begin
|
|
|
|
|
// 除数为0
|
|
|
|
|
if (divisor_r == `ZeroWord) begin
|
|
|
|
|
if (op_div | op_divu) begin
|
|
|
|
|
result_o <= 32'hffffffff;
|
|
|
|
|
end else begin
|
|
|
|
|
result_o <= dividend_r;
|
|
|
|
|
end
|
|
|
|
|
ready_o <= `DivResultReady;
|
|
|
|
|
state <= STATE_IDLE;
|
|
|
|
|
busy_o <= `False;
|
|
|
|
|
// 除数不为0
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end else begin
|
2020-09-14 14:22:42 +00:00
|
|
|
|
busy_o <= `True;
|
|
|
|
|
count <= 32'h40000000;
|
|
|
|
|
state <= STATE_CALC;
|
|
|
|
|
div_result <= `ZeroWord;
|
|
|
|
|
div_remain <= `ZeroWord;
|
|
|
|
|
|
|
|
|
|
// DIV和REM这两条指令是有符号数运算指令
|
|
|
|
|
if (op_div | op_rem) begin
|
|
|
|
|
// 被除数求补码
|
|
|
|
|
if (dividend_r[31] == 1'b1) begin
|
|
|
|
|
dividend_r <= dividend_invert;
|
|
|
|
|
minuend <= dividend_invert[31];
|
|
|
|
|
end else begin
|
|
|
|
|
minuend <= dividend_r[31];
|
|
|
|
|
end
|
|
|
|
|
// 除数求补码
|
|
|
|
|
if (divisor_r[31] == 1'b1) begin
|
|
|
|
|
divisor_r <= divisor_invert;
|
|
|
|
|
end
|
|
|
|
|
end else begin
|
|
|
|
|
minuend <= dividend_r[31];
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
// 运算结束后是否要对结果取补码
|
|
|
|
|
if ((op_div && (dividend_r[31] ^ divisor_r[31] == 1'b1))
|
|
|
|
|
|| (op_rem && (dividend_r[31] == 1'b1))) begin
|
|
|
|
|
invert_result <= 1'b1;
|
|
|
|
|
end else begin
|
|
|
|
|
invert_result <= 1'b0;
|
|
|
|
|
end
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end
|
|
|
|
|
end else begin
|
2020-09-14 14:22:42 +00:00
|
|
|
|
state <= STATE_IDLE;
|
|
|
|
|
result_o <= `ZeroWord;
|
|
|
|
|
ready_o <= `DivResultNotReady;
|
|
|
|
|
busy_o <= `False;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-09-12 06:17:34 +00:00
|
|
|
|
STATE_CALC: begin
|
2020-09-14 14:22:42 +00:00
|
|
|
|
if (start_i == `DivStart) begin
|
|
|
|
|
dividend_r <= {dividend_r[30:0], 1'b0};
|
|
|
|
|
div_result <= div_result_tmp;
|
2020-09-12 06:17:34 +00:00
|
|
|
|
count <= {1'b0, count[31:1]};
|
2020-09-14 14:22:42 +00:00
|
|
|
|
if (|count) begin
|
|
|
|
|
minuend <= {minuend_tmp[30:0], dividend_r[30]};
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end else begin
|
2020-09-14 14:22:42 +00:00
|
|
|
|
state <= STATE_END;
|
|
|
|
|
if (minuend_ge_divisor) begin
|
|
|
|
|
div_remain <= minuend_sub_res;
|
|
|
|
|
end else begin
|
|
|
|
|
div_remain <= minuend;
|
|
|
|
|
end
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end
|
2020-09-14 14:22:42 +00:00
|
|
|
|
end else begin
|
|
|
|
|
state <= STATE_IDLE;
|
|
|
|
|
result_o <= `ZeroWord;
|
|
|
|
|
ready_o <= `DivResultNotReady;
|
|
|
|
|
busy_o <= `False;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
2020-09-12 06:17:34 +00:00
|
|
|
|
|
2020-09-14 14:22:42 +00:00
|
|
|
|
STATE_END: begin
|
|
|
|
|
if (start_i == `DivStart) begin
|
|
|
|
|
ready_o <= `DivResultReady;
|
|
|
|
|
state <= STATE_IDLE;
|
|
|
|
|
busy_o <= `False;
|
|
|
|
|
if (op_div | op_divu) begin
|
|
|
|
|
if (invert_result) begin
|
|
|
|
|
result_o <= (-div_result);
|
|
|
|
|
end else begin
|
|
|
|
|
result_o <= div_result;
|
|
|
|
|
end
|
2020-09-06 15:17:56 +00:00
|
|
|
|
end else begin
|
2020-09-14 14:22:42 +00:00
|
|
|
|
if (invert_result) begin
|
|
|
|
|
result_o <= (-div_remain);
|
|
|
|
|
end else begin
|
|
|
|
|
result_o <= div_remain;
|
|
|
|
|
end
|
2020-09-06 15:17:56 +00:00
|
|
|
|
end
|
2020-09-14 14:22:42 +00:00
|
|
|
|
end else begin
|
|
|
|
|
state <= STATE_IDLE;
|
|
|
|
|
result_o <= `ZeroWord;
|
|
|
|
|
ready_o <= `DivResultNotReady;
|
|
|
|
|
busy_o <= `False;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
endcase
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
endmodule
|