【FPGA】Verilog 实践:半加器与全加器 | 半减器与全减器 | Code Converter

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

写在前面本章主要理解加法器和减法器的概念并了解 Code converter 的概念。使用 Verilog 实现多种加法器、减法器和代码转换器通过 FPGA 验证 Verilog 实现的电路的行为。


Ⅰ. 前置知识

0x00 半加器与全加器

① 半加器 (\textrm{HA}) 有两个输入和输出

  • 输入由 2 个 1-bit (A,B) 数组成输出由 Sum(S) 和 \textrm{Carry(C)} 组成。
  • 当两个 1-bit 数相加大于可以用 1-bit 表示的数时会生成进位Carry。

S = \bar{A}B+A\bar{B} = A\oplus B
C=AB

② 全加器\textrm{FA}是 Carry 也是一个可加的加法器用作实际的基础运算电路。

S= (A\oplus B)\oplus C_{in}
C_{out} = C_{in}(A\oplus B)+AB

0x01 半减器与全减器

减法器与加法器相反是用于 1-bit 数减法的逻辑电路。

半减器 (\textrm{HS}) 由 \textrm{Difference(D)} 和 \textrm{Borrow(B)} 组成分别表示两个 1-bit 输入  A,B 和输出 A-B 的结果。

如果要进行比一个数字更大的减法则从前面的数字中获取借位Borrow。

D = \bar{A}\cdot B+A\cdot \bar{B}=A\oplus B
b=\bar{A}\cdot B

全减器 (\textrm{FS}) 将 Borrow 也作为输入具有完整的 Subtractor 功能

 D_n = A_n\oplus B_n\oplus b_{n-1}
b_n = \overline{(A_n\oplus B_n)}\cdot b_{n-1}+\bar{A_n}\cdot B_n

0x02 逻辑电路设计程序

设计程序

  1. 考虑电路的结构和动作用真值表进行设计。
  2. 将真值表的内容转换为卡诺图K-map。
  3. 通过编写的卡诺图和的 Minimization (POS, SOP) 编写出最小化形式的布尔函数。
  4. 尽量使用 \textrm{NAND}  或 \textrm{NOR}门进行配置。
  5. 验证配置和实验结果是否与真值表相同。

Code Converter一种逻辑电路它将输入的特定代码转换成指定的代码输出。

Ⅱ.  练习Assignment

练习1Half Adder

按照下列结构写出 Verilog 代码得到 Verilog 的 Simulation 结果。

💬 Design source

`timescale 1ns / 1ps

/* Half_Adder */
module Half_Adder (
    input a, b,
    output s, c
    );

    assign s = a ^ b;
    assign c = a & b;
    
endmodule

💬 Testbench

`timescale 1ns / 1ps

/* Half_Adder Table Bench */
module Half_Adder_tb;
reg aa, bb;
wire s, c;

Half_Adder u_Half_Adder(
    .a(aa),
    .b(bb),
    .s(s),
    .c(c)
    );

initial aa = 1'b0;
initial bb = 1'b0;

always aa = #100 ~aa;
always bb = #200 ~bb;
    
initial begin
    #1000
    $finish;
end

endmodule

🚩 运行结果如下

练习2Full Adder

按照下列结构写出 Verilog 代码得到 Verilog 的 Simulation 结果。

💬 Design source

`timescale 1ns / 1ps

/* Full_Adder */
module Full_Adder (
    input a, b, cinput,
    output s, coutput
    );
    
    assign s = (a ^ b) ^ cinput;
    assign coutput = (a & b) | ((a ^ b) & cinput);

endmodule

💬 Testbench

`timescale 1ns / 1ps

/* Full_Adder Sim */
module Full_Adder_tb;
reg aa, bb, ccinput;
wire s, coutput;

Full_Adder u_Full_Adder (
   .a(aa),
   .b(bb),
   .cinput(ccinput),
   .s(s),
   .coutput(coutput)
   );
   
initial aa = 1'b0;
initial bb = 1'b0;
initial ccinput = 1'b0;
    
always aa = #100 ~aa;
always bb = #200 ~bb;
always ccinput = #400 ~ccinput;
    
initial begin
     #1000
     $finish;
end

endmodule

🚩 运行结果如下

练习3Half Subtractor

按照下列结构写出 Verilog 代码得到 Verilog 的 Simulation 结果。

💬 Design source

`timescale 1ns / 1ps

module Half_Subtractor (
   input A,B,
   output b,D
   );
   
   assign D = A ^ B;
   assign b = (~A) & B;

endmodule

💬 Testbench

`timescale 1ns / 1ps

/* Half_Subtractor Sim */
module Half_Subtractor_tb;
reg AA, BB;
wire b, D;

Half_Subtractor u_Half_Subtractor(
    .A(AA),
    .B(BB),
    .b(b),
    .D(D)
    );

initial AA = 1'b0;
initial BB = 1'b0;

always AA = #100 ~AA;
always BB = #200 ~BB;

initial begin
    #1000
    $finish;
end
   
endmodule

🚩 运行结果如下

练习4Full Subtractor

按照下列结构写出 Verilog 代码得到 Verilog 的 Simulation 结果。

💬 Design source

`timescale 1ns / 1ps

/* Full_ Subtractor */
module Full_Subtractor (
    input a, b, c,
    output d, e
    );
    
    assign d = (a ^ b) ^ c;
    assign e = (~(a ^ b) & c) | (b & ~a);

endmodule

💬 Testbench

`timescale 1ns / 1ps

/*  Full_Subtractor Sim */
module Full_Subtractor_tb;
reg aa, bb, cc;
wire d, e;

Full_Subtractor u_Full_Subtractor(
   .a(aa),
   .b(bb),
   .c(cc),
   .d(d),
   .e(e)
   );

initial aa = 1'b0;
initial bb = 1'b0;
initial cc = 1'b0;

always aa = #100 ~aa;
always bb = #200 ~bb;
always cc = #400 ~cc;

initial begin
   #1000
   $finish;
end

endmodule

🚩 运行结果如下

练习4Code Converter

 8421(BCD)-2421 Code converter使用上图表创建真值表画出卡诺图使用卡诺图 minimazation 创建布尔函数SOP form、POS form分别使用 NAND 和NOR 配置使用 Verilog 实现 NAND 形式的 8421-2421 converter使用 FPGA 验证模拟和操作。

💬 Design source

`timescale 1ns / 1ps

/* Code_Converter */
module Code_Converter(
   input a, b, c, d,
   output e, f, g, h
   );
   
   assign e = a || (b && c) || (b && d);      
   assign f = a || (b && (~d)) || (b && c);   
   assign g = a || ((~b) && c) || (b && (~c) && d);   
   assign h = ~(~d);

endmodule

💬 Testbench

`timescale 1ns / 1ps

/* Code_Converter Sim */
module Code_Converter_tb;
reg aa, bb, cc, dd;
wire e, f, g, h;

Code_Converter u_Code_Converter(
   .a(aa),
   .b(bb),
   .c(cc),
   .d(dd),
   .e(e),
   .f(f),
   .g(g),
   .h(h)
   );

initial aa = 1'b0;
initial bb = 1'b0;
initial cc = 1'b0;
initial dd = 1'b0;

always aa = #100 ~aa;
always bb = #200 ~bb;
always cc = #400 ~cc;
always dd = #800 ~dd;

initial begin
   #1000
   $finish;
end

endmodule

🚩 运行结果如下

📌 [ 笔者 ]   王亦优
📃 [ 更新 ]   2022.10.5
❌ [ 勘误 ]   /* 暂无 */
📜 [ 声明 ]   由于作者水平有限本文有错误和不准确之处在所难免
              本人也很想知道这些错误恳望读者批评指正

📜 参考资料 

Introduction to Logic and Computer Design, Alan Marcovitz, McGrawHill, 2008

Microsoft. MSDN(Microsoft Developer Network)[EB/OL]. []. .

百度百科[EB/OL]. []. https://baike.baidu.com/.

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6