Verilog编程题:不使用除法器,得出除法运算结果,结果为定点形式

一、诺瓦星云的一道笔试题

  输入两个数据,分别为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 

  仿真结果
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值