Verilog 半减器和全减器

系列目录

Verilog 半加器和全加器

Verilog 半减器和全减器

串行加法器

11位全加器verilog设计


前言

大二的数字逻辑课程和考试已经结束了,栽在了没细致研究的状态机上,但是对于一名计科学生来说,FPGA的领域仍然是广阔的,所以我将会把我之前学习所写的资料进行一个整理。

一、什么是半减器和全减器

半减器

半减器原理:两个二进制数相减叫做半减,实现半减器操作的电路称为半减器,半减器用于计算两bit x和y 的减法,输出结果diff和减法借位标志输出cin

半减器只考虑当前两位二进制数相减,输出为差以及是否向高位借位,而全减器还要考虑当前位的低位是否曾有借位。它们的真值表如下:

被减数减数表示本位最终运算结果表示本位是否向高位借位
xydiffcin
0000
0111
1010
1100

diff = x \oplus y

cin = \sim x\And y

diff = x ^ y;  cin = (~x) & y;

全减器

要理解真值表,可以用举列子的方法得到:

被减数减数表示低位是否向本位借位表示本位最终运算结果表示本位是否向高位借位
xycoutdiffcin
0000

0

00111
01011
01101
10010
10100
11000
11111

比如4'b1000 - 4b'0001,

则第一位对应0 1 0 1 1第二位对应的是0 0 1 1 1

 从真值表中,可以得到

diff = x ^ y ^ cout, cin = (~x & (y ^ cout))|(y & cout)

注意: +和|都表示或。 

推导过程:

diff = \overline{x} \overline{y} cout + \overline{x}y\overline{cout} +x\overline{y}\overline{cout} + xycout=\overline{x}(\overline{y}cout + y\overline{cout})+ x(\overline{y}\overline{cout} + ycout) = \overline{x}(y\oplus cout) + x\overline{(y \oplus cout)} = x\oplus y \oplus cout;

cin = \overline{x} \overline{y}cout + \overline{x}y\overline{cout} + \overline{x}ycout + xycout =\overline{x}(\overline{y}cout + \overline{x} \overline{cout})+(\overline{x} + x)ycout=\overline{x}(y \oplus cout) + ycout

二、半减器的verilog代码和testbench代码如下:

代码如下(示例):

module halfsub(x,y,d,cin);

  input x;
  input y;

  output d;
  output cin;

  assign d = x ^ y;
  assign cin = (~x) & y;

endmodule

testbench:

`timescale 1ns/1ns
`define clock_period 20

module halfsub_tb;
  reg  x,y;

  wire cin; //carryover
  wire d;
  reg clk;

  halfsub halfsub_0(
            .x(x),
            .y(y),
            .d(d),
            .cin(cin)
                  );

  initial clk = 0;
  always #(`clock_period/2) clk = ~clk;

  initial begin
     x = 0;
     repeat(20)
      #(`clock_period) x = $random;

  end

  initial begin
     y = 0;
     repeat(20)
      #(`clock_period) y = $random;

  end


  initial begin
     #(`clock_period*20)
    $stop;
  end
  
endmodule

三、全减器的verilog代码和testbench代码如下:

module fullsub(cout,x,y,d,cin);

  input cout; // carry out bit, borrowed by its next low bit
  input x;
  input y;

  output d;
  output cin;

  assign d = x ^ y ^ cout;
  assign cin = (~x & (y ^ cout)) | (y & cout);


endmodule

`timescale 1ns/1ns
`define clock_period 20

module fullsub_tb;
  reg  x,y,cout;

  wire cin; //carryover
  wire d;
  reg clk;

  fullsub fullsub_0(
                  .cout(cout),
            .x(x),
            .y(y),
            .d(d),
            .cin(cin)
                  );

  initial clk = 0;
  always #(`clock_period/2) clk = ~clk;

  initial begin
     x = 0;
     repeat(20)
      #(`clock_period) x = $random;

  end

  initial begin
     y = 0;
     repeat(20)
      #(`clock_period) y = $random;

  end

   initial begin
     cout = 0;
     repeat(20)
      #(`clock_period) cout = $random;

  end

  initial begin
     #(`clock_period*20)
    $stop;
  end


endmodule


 四、全减器仿真结果:

RTL:

 波形图:

  • 11
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YOLOKY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值