飞道的博客

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

489人阅读  评论(0)

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

     本篇博客全站热榜排名:12


Ⅰ. 前置知识

0x00 半加器与全加器

① 半加器 () 有两个输入和输出:

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


② 全加器)是 Carry 也是一个可加的加法器,用作实际的基础运算电路。


0x01 半减器与全减器

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

半减器 () 由  和  组成,分别表示两个 1-bit 输入   和输出  的结果。

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


全减器 () 将 Borrow 也作为输入,具有完整的 Subtractor 功能:

 

0x02 逻辑电路设计程序

设计程序:

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

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

Ⅱ.  练习(Assignment)

练习1:Half Adder

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

💬 Design source:


  
  1. `timescale 1ns / 1ps
  2. /* Half_Adder */
  3. module Half_Adder (
  4. input a, b,
  5. output s, c
  6. );
  7. assign s = a ^ b;
  8. assign c = a & b;
  9. endmodule

💬 Testbench:


  
  1. `timescale 1ns / 1ps
  2. /* Half_Adder Table Bench */
  3. module Half_Adder_tb;
  4. reg aa, bb;
  5. wire s, c;
  6. Half_Adder u_Half_Adder(
  7. .a(aa),
  8. .b(bb),
  9. .s(s),
  10. .c(c)
  11. );
  12. initial aa = 1'b0;
  13. initial bb = 1'b0;
  14. always aa = # 100 ~aa;
  15. always bb = # 200 ~bb;
  16. initial begin
  17. # 1000
  18. $finish;
  19. end
  20. endmodule

🚩 运行结果如下:

练习2:Full Adder

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

💬 Design source:


  
  1. `timescale 1ns / 1ps
  2. /* Full_Adder */
  3. module Full_Adder (
  4. input a, b, cinput,
  5. output s, coutput
  6. );
  7. assign s = (a ^ b) ^ cinput;
  8. assign coutput = (a & b) | ((a ^ b) & cinput);
  9. endmodule

💬 Testbench:


  
  1. `timescale 1ns / 1ps
  2. /* Full_Adder Sim */
  3. module Full_Adder_tb;
  4. reg aa, bb, ccinput;
  5. wire s, coutput;
  6. Full_Adder u_Full_Adder (
  7. .a(aa),
  8. .b(bb),
  9. .cinput(ccinput),
  10. .s(s),
  11. .coutput(coutput)
  12. );
  13. initial aa = 1'b0;
  14. initial bb = 1'b0;
  15. initial ccinput = 1'b0;
  16. always aa = # 100 ~aa;
  17. always bb = # 200 ~bb;
  18. always ccinput = # 400 ~ccinput;
  19. initial begin
  20. # 1000
  21. $finish;
  22. end
  23. endmodule

🚩 运行结果如下:

练习3:Half Subtractor

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

💬 Design source:


  
  1. `timescale 1ns / 1 ps
  2. module Half_Subtractor (
  3. input A,B,
  4. output b,D
  5. );
  6. assign D = A ^ B;
  7. assign b = (~A) & B;
  8. endmodule

💬 Testbench:


  
  1. `timescale 1ns / 1ps
  2. /* Half_Subtractor Sim */
  3. module Half_Subtractor_tb;
  4. reg AA, BB;
  5. wire b, D;
  6. Half_Subtractor u_Half_Subtractor(
  7. .A(AA),
  8. .B(BB),
  9. .b(b),
  10. .D(D)
  11. );
  12. initial AA = 1'b0;
  13. initial BB = 1'b0;
  14. always AA = # 100 ~AA;
  15. always BB = # 200 ~BB;
  16. initial begin
  17. # 1000
  18. $finish;
  19. end
  20. endmodule

🚩 运行结果如下:

练习4:Full Subtractor

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

💬 Design source:


  
  1. `timescale 1ns / 1ps
  2. /* Full_ Subtractor */
  3. module Full_Subtractor (
  4. input a, b, c,
  5. output d, e
  6. );
  7. assign d = (a ^ b) ^ c;
  8. assign e = (~(a ^ b) & c) | (b & ~a);
  9. endmodule

💬 Testbench:


  
  1. `timescale 1ns / 1ps
  2. /* Full_Subtractor Sim */
  3. module Full_Subtractor_tb;
  4. reg aa, bb, cc;
  5. wire d, e;
  6. Full_Subtractor u_Full_Subtractor(
  7. .a(aa),
  8. .b(bb),
  9. .c(cc),
  10. .d(d),
  11. .e(e)
  12. );
  13. initial aa = 1'b0;
  14. initial bb = 1'b0;
  15. initial cc = 1'b0;
  16. always aa = # 100 ~aa;
  17. always bb = # 200 ~bb;
  18. always cc = # 400 ~cc;
  19. initial begin
  20. # 1000
  21. $finish;
  22. end
  23. endmodule

🚩 运行结果如下:

练习4:Code Converter

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

💬 Design source:


  
  1. `timescale 1ns / 1ps
  2. /* Code_Converter */
  3. module Code_Converter(
  4. input a, b, c, d,
  5. output e, f, g, h
  6. );
  7. assign e = a || (b && c) || (b && d);
  8. assign f = a || (b && (~d)) || (b && c);
  9. assign g = a || ((~b) && c) || (b && (~c) && d);
  10. assign h = ~(~d);
  11. endmodule

💬 Testbench:


  
  1. `timescale 1ns / 1ps
  2. /* Code_Converter Sim */
  3. module Code_Converter_tb;
  4. reg aa, bb, cc, dd;
  5. wire e, f, g, h;
  6. Code_Converter u_Code_Converter(
  7. .a(aa),
  8. .b(bb),
  9. .c(cc),
  10. .d(dd),
  11. .e(e),
  12. .f(f),
  13. .g(g),
  14. .h(h)
  15. );
  16. initial aa = 1'b0;
  17. initial bb = 1'b0;
  18. initial cc = 1'b0;
  19. initial dd = 1'b0;
  20. always aa = # 100 ~aa;
  21. always bb = # 200 ~bb;
  22. always cc = # 400 ~cc;
  23. always dd = # 800 ~dd;
  24. initial begin
  25. # 1000
  26. $finish;
  27. end
  28. endmodule

🚩 运行结果如下:


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

📜 参考资料 

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

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

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


转载:https://blog.csdn.net/weixin_50502862/article/details/128834735
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场