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-06 15:17:56 +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-06 15:17:56 +00:00
|
|
|
|
output reg[`RegBus] result_o, // 除法结果
|
2020-04-18 12:14:37 +00:00
|
|
|
|
output reg ready_o, // 运算结束信号
|
2020-09-06 15:17:56 +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-06 15:17:56 +00:00
|
|
|
|
localparam STATE_IDLE = 4'b0001;
|
|
|
|
|
localparam STATE_START = 4'b0010;
|
|
|
|
|
localparam STATE_INVERT = 4'b0100;
|
|
|
|
|
localparam STATE_END = 4'b1000;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
|
|
|
|
|
reg[`RegBus] dividend_temp;
|
|
|
|
|
reg[`RegBus] divisor_temp;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
reg[3:0] state;
|
|
|
|
|
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-06 15:17:56 +00:00
|
|
|
|
reg inst_div;
|
|
|
|
|
reg inst_divu;
|
2020-03-29 15:19:14 +00:00
|
|
|
|
|
2020-09-06 15:17:56 +00:00
|
|
|
|
wire[31:0] dividend_invert = -dividend_i;
|
|
|
|
|
wire[31:0] divisor_invert = -divisor_i;
|
|
|
|
|
wire[31:0] minuend_sub_res = minuend - divisor_temp;
|
|
|
|
|
wire minuend_ge_divisor = minuend >= divisor_temp;
|
2020-03-29 15:19:14 +00:00
|
|
|
|
|
2020-09-06 15:17:56 +00:00
|
|
|
|
wire op_div = (op_i == `INST_DIV);
|
|
|
|
|
wire op_divu = (op_i == `INST_DIVU);
|
|
|
|
|
wire op_rem = (op_i == `INST_REM);
|
|
|
|
|
wire op_remu = (op_i == `INST_REMU);
|
2020-01-13 00:26:41 +00:00
|
|
|
|
|
2020-04-18 12:14:37 +00:00
|
|
|
|
// 状态机实现
|
2020-01-13 00:26:41 +00:00
|
|
|
|
always @ (posedge clk) begin
|
|
|
|
|
if (rst == `RstEnable) begin
|
|
|
|
|
state <= STATE_IDLE;
|
|
|
|
|
ready_o <= `DivResultNotReady;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
result_o <= `ZeroWord;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
div_result <= `ZeroWord;
|
|
|
|
|
div_remain <= `ZeroWord;
|
2020-03-29 15:19:14 +00:00
|
|
|
|
reg_waddr_o <= `ZeroWord;
|
|
|
|
|
dividend_temp <= `ZeroWord;
|
|
|
|
|
divisor_temp <= `ZeroWord;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
minuend <= `ZeroWord;
|
|
|
|
|
count <= `ZeroWord;
|
2020-03-29 15:19:14 +00:00
|
|
|
|
invert_result <= 1'b0;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
busy_o <= `False;
|
|
|
|
|
inst_div <= 1'b0;
|
|
|
|
|
inst_divu <= 1'b0;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end else begin
|
|
|
|
|
case (state)
|
|
|
|
|
STATE_IDLE: begin
|
2020-09-06 15:17:56 +00:00
|
|
|
|
busy_o <= `False;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
if (start_i == `DivStart) begin
|
2020-03-29 15:19:14 +00:00
|
|
|
|
reg_waddr_o <= reg_waddr_i;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
inst_div <= op_div;
|
|
|
|
|
inst_divu <= op_divu;
|
2020-03-29 15:19:14 +00:00
|
|
|
|
|
2020-04-18 12:14:37 +00:00
|
|
|
|
// 除数为0
|
2020-01-13 00:26:41 +00:00
|
|
|
|
if (divisor_i == `ZeroWord) begin
|
|
|
|
|
ready_o <= `DivResultReady;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
if (op_div | op_divu) begin
|
|
|
|
|
result_o <= 32'hffffffff;
|
|
|
|
|
end else begin
|
|
|
|
|
result_o <= dividend_i;
|
|
|
|
|
end
|
2020-04-18 12:14:37 +00:00
|
|
|
|
// 除数不为0
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end else begin
|
2020-09-06 15:17:56 +00:00
|
|
|
|
count <= 32'h80000000;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
state <= STATE_START;
|
2020-03-29 15:19:14 +00:00
|
|
|
|
|
2020-04-18 12:14:37 +00:00
|
|
|
|
// DIV和REM这两条指令是有符号数运算
|
2020-09-06 15:17:56 +00:00
|
|
|
|
if ((op_div) || (op_rem)) begin
|
2020-04-18 12:14:37 +00:00
|
|
|
|
// 被除数求补码
|
2020-03-29 15:19:14 +00:00
|
|
|
|
if (dividend_i[31] == 1'b1) begin
|
2020-09-06 15:17:56 +00:00
|
|
|
|
dividend_temp <= dividend_invert;
|
|
|
|
|
minuend <= dividend_invert[31];
|
2020-03-29 15:19:14 +00:00
|
|
|
|
end else begin
|
|
|
|
|
dividend_temp <= dividend_i;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
minuend <= dividend_i[31];
|
2020-03-29 15:19:14 +00:00
|
|
|
|
end
|
2020-04-18 12:14:37 +00:00
|
|
|
|
// 除数求补码
|
2020-03-29 15:19:14 +00:00
|
|
|
|
if (divisor_i[31] == 1'b1) begin
|
2020-09-06 15:17:56 +00:00
|
|
|
|
divisor_temp <= divisor_invert;
|
2020-03-29 15:19:14 +00:00
|
|
|
|
end else begin
|
|
|
|
|
divisor_temp <= divisor_i;
|
|
|
|
|
end
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end else begin
|
|
|
|
|
dividend_temp <= dividend_i;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
minuend <= dividend_i[31];
|
2020-03-29 15:19:14 +00:00
|
|
|
|
divisor_temp <= divisor_i;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end
|
2020-03-29 15:19:14 +00:00
|
|
|
|
|
2020-04-18 12:14:37 +00:00
|
|
|
|
// 运算结束后是否要对结果取补码
|
2020-09-06 15:17:56 +00:00
|
|
|
|
if (((op_div) && (dividend_i[31] ^ divisor_i[31] == 1'b1))
|
|
|
|
|
|| ((op_rem) && (dividend_i[31] == 1'b1))) begin
|
2020-03-29 15:19:14 +00:00
|
|
|
|
invert_result <= 1'b1;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end else begin
|
2020-03-29 15:19:14 +00:00
|
|
|
|
invert_result <= 1'b0;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end else begin
|
|
|
|
|
ready_o <= `DivResultNotReady;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
result_o <= `ZeroWord;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
STATE_START: begin
|
2020-09-06 15:17:56 +00:00
|
|
|
|
busy_o <= `True;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
if (start_i == `DivStart) begin
|
2020-09-06 15:17:56 +00:00
|
|
|
|
div_result <= {div_result[30:0], minuend_ge_divisor};
|
|
|
|
|
if (|count) begin
|
|
|
|
|
if (minuend_ge_divisor) begin
|
|
|
|
|
minuend <= {minuend_sub_res[30:0], dividend_temp[31]};
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end else begin
|
2020-09-06 15:17:56 +00:00
|
|
|
|
minuend <= {minuend[30:0], dividend_temp[31]};
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end
|
2020-09-06 15:17:56 +00:00
|
|
|
|
count <= {1'b0, count[31:1]};
|
|
|
|
|
dividend_temp <= {dividend_temp[30:0], 1'b0};
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end else begin
|
2020-03-29 15:19:14 +00:00
|
|
|
|
state <= STATE_INVERT;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
if (minuend_ge_divisor) begin
|
|
|
|
|
div_remain <= minuend_sub_res;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end else begin
|
|
|
|
|
div_remain <= minuend;
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end else begin
|
2020-04-25 09:04:44 +00:00
|
|
|
|
ready_o <= `DivResultNotReady;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
result_o <= `ZeroWord;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
state <= STATE_IDLE;
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-03-29 15:19:14 +00:00
|
|
|
|
STATE_INVERT: begin
|
2020-09-06 15:17:56 +00:00
|
|
|
|
busy_o <= `True;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
if (start_i == `DivStart) begin
|
2020-03-29 15:19:14 +00:00
|
|
|
|
if (invert_result == 1'b1) begin
|
2020-09-06 15:17:56 +00:00
|
|
|
|
div_result <= -div_result;
|
|
|
|
|
div_remain <= -div_remain;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end
|
|
|
|
|
state <= STATE_END;
|
|
|
|
|
end else begin
|
2020-04-25 09:04:44 +00:00
|
|
|
|
ready_o <= `DivResultNotReady;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
result_o <= `ZeroWord;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
state <= STATE_IDLE;
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
STATE_END: begin
|
2020-09-06 15:17:56 +00:00
|
|
|
|
busy_o <= `False;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
if (start_i == `DivStart) begin
|
|
|
|
|
ready_o <= `DivResultReady;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
if (inst_div | inst_divu) begin
|
|
|
|
|
result_o <= div_result;
|
|
|
|
|
end else begin
|
|
|
|
|
result_o <= div_remain;
|
|
|
|
|
end
|
2020-02-22 12:38:16 +00:00
|
|
|
|
state <= STATE_IDLE;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
end else begin
|
|
|
|
|
state <= STATE_IDLE;
|
2020-09-06 15:17:56 +00:00
|
|
|
|
result_o <= `ZeroWord;
|
2020-01-13 00:26:41 +00:00
|
|
|
|
ready_o <= `DivResultNotReady;
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
endcase
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
endmodule
|