一、诺瓦星云的一道笔试题
输入两个数据,分别为InData0和InData1,数据都为10bit,Weight=InData0/nData1,Weight的范围为0-1,需要定点化为3bit,1bit整数,2bit小数。根据特点,为了考虑资源优化不用除法器(考虑用加、减、乘及移位等方式实现),请实现Weight计算。
二、个人实现代码
WeightCalculator.v
module WeightCalculator(
input [9:0] InData0,
input [9:0] InData1,
output [2:0] Outweight
);
reg [2:0] r_outweight;
always @(*)begin
r_outweight[2] = 0;
r_outweight[1] = ({InData0,1'b0} > InData1) ? 1 : 0;
if (r_outweight[1])
r_outweight[0] = ({{InData0,1'b0} - InData1, 1'b0} > InData1) ? 1 : 0;
else
r_outweight[0] = ({InData0,2'b0}>InData1) ? 1 : 0;
end
assign Outweight = (InData0==InData1) ? 3'b100 : r_outweight;
endmodule
测试文件–WeightCalculator_tb.v
`timescale 1ns/1ps
module WeightCalculator_tb;
reg [9:0] a = 0;
reg [9:0] b = 0;
WeightCalculator WeightCalculator_inst(
.InData0 (a),
.InData1 (b)
);
initial begin
a=0;
b=0;
#20;
a=1;
b=99;
#20;
a=222;
b=456;
#20;
a=21;
b=22;
#20;
a=270;
b=500;
#20;
$stop;
end
endmodule
仿真结果