• 【数电】【verilog】加法器


    1.2输入1bit半加器

    半加器的电路如下图所示:

     

    1. module half adder(
    2. input wire A,
    3. input wire B,
    4. output wire C,
    5. output wire sum
    6. );
    7. //assign sum = (A == B) ? 0 : 1; //这两种方式都可以实现
    8. assign sum = A^B;
    9. assign C= A&B;
    10. endmodule

    2.2输入1bit全加器

     真值表:

     电路图(有很多不同的电路形式):

    第一种,利用连续赋值语句实现: 

    1. module full_add2
    2. (
    3. input a, //加数
    4. input b, //被加数
    5. input cin, //进位输入
    6. output sum, //结果输出
    7. output cout //进位输出
    8. );
    9. assign sum = a^b^cin;
    10. assign cout = (a&b)|((a^b)&cin);
    11. endmodule

    第二种,利用行为描述方式实现:

    1. module full_add2
    2. (
    3. input a, //加数
    4. input b, //被加数
    5. input cin, //进位输入
    6. output sum, //结果输出
    7. output cout //进位输出
    8. );
    9. assign {cout,sum} = a+b+cin; //利用拼接的方式实现全加器
    10. endmodule

    第三种,利用两个一位半加器并联实现一个一位全加器,sum = a^b^cin,cout = (a&b)|((a^b)&cin):

    1. module add_half(
    2. input A ,
    3. input B ,
    4. output wire S ,
    5. output wire C
    6. );
    7. assign S = A ^ B;
    8. assign C = A & B;
    9. endmodule
    10. /***************************************************************/
    11. module add_full(
    12. input A ,
    13. input B ,
    14. input Ci ,
    15. output wire S ,
    16. output wire Co
    17. );
    18. wire [1:0] s, c;
    19. add_half m1 (
    20. .A(A),
    21. .B(B),
    22. .S(s[0]),
    23. .C(c[0]));
    24. add_half m2 (
    25. .A(s[0]),
    26. .B(Ci),
    27. .S(s[1]),
    28. .C(c[1]));
    29. assign S = s[1];
    30. assign Co = c[0] | c[1];
    31. endmodule

    3.4位串行加法器(全加器)

    在这里插入图片描述

    第一种,利用半加器生成全加器,再用全加器生成串行全加器:

    1. `timescale 1ns/1ns
    2. module add_half(
    3. input A ,
    4. input B ,
    5. output wire S ,
    6. output wire C
    7. );
    8. assign S = A ^ B;
    9. assign C = A & B;
    10. endmodule
    11. /***************************************************************/
    12. module add_full(
    13. input A ,
    14. input B ,
    15. input Ci ,
    16. output wire S ,
    17. output wire Co
    18. );
    19. wire c_1;
    20. wire c_2;
    21. wire sum_1;
    22. add_half add_half_1(
    23. .A (A),
    24. .B (B),
    25. .S (sum_1),
    26. .C (c_1)
    27. );
    28. add_half add_half_2(
    29. .A (sum_1),
    30. .B (Ci),
    31. .S (S),
    32. .C (c_2)
    33. );
    34. assign Co = c_1 | c_2;
    35. endmodule
    36. /******************************************************************/
    37. module add_4(
    38. input [3:0] A ,
    39. input [3:0] B ,
    40. input Ci ,
    41. output wire [3:0] S ,
    42. output wire Co
    43. );
    44. wire [3:0] C;
    45. add_full u1(
    46. .A (A[0]),
    47. .B (B[0]),
    48. .Ci (Ci),
    49. .S (S[0]),
    50. .Co (C[0])
    51. );
    52. add_full u2(
    53. .A (A[1]),
    54. .B (B[1]),
    55. .Ci (C[0]),
    56. .S (S[1]),
    57. .Co (C[1])
    58. );
    59. add_full u3(
    60. .A (A[2]),
    61. .B (B[2]),
    62. .Ci (C[1]),
    63. .S (S[2]),
    64. .Co (C[2])
    65. );
    66. add_full u4(
    67. .A (A[3]),
    68. .B (B[3]),
    69. .Ci (C[2]),
    70. .S (S[3]),
    71. .Co (C[3])
    72. );
    73. assign Co = C[3];
    74. endmodule

    第二种,利用拼接的方式来搭建电路:

    1. //4位全加器,有低位向本位进位(即有输入进位)
    2. module four_bits_full_add(
    3. input [3:0]a,b,
    4. input cin,
    5. output cout,
    6. output [3:0]sum
    7. );
    8. assign {cout,sum} = a + b + cin;
    9. endmodule

    串行全加器是上面这样,但是这种结构的缺点是,必须要等到上一片的结果算出来之后下一片才能进行工作,当级数很高的时候计算的时间将是每一片时间的n倍,会出现组合逻辑延时过长的问题。此时另一种进位方法——超前进位加法器就可以解决这一延时过高的问题。

    4.4位超前进位加法器

    根据上面的讲解,我们可以得到:

     然后根据下面公式:

     带入到上面,得到:

     我们定义下面两个式子分别为进位传输函数和进位产生函数:

    代入可以得到:

     最后,4位的超前进位加法器可以得到下面这些式子:

    这里实在看不懂可以翻一番数电的超前进位加法器的内容。

     verilog代码如下:0

    1. module four_bits_fast_adder(cout,sum,a,b,cin);
    2. output [3:0]sum; //数据累加和本位
    3. output cout; //输出进位
    4. input [3:0]a,b; //需要相加的数
    5. input cin; //输入进位
    6. wire [4:0]g,p,c; //分别对应Gi、Pi和Ci
    7. assign c[0] = cin; //最低进位为输入进位
    8. assign P = a | b; // Pi = Ai·Bi
    9. assign g = a & b; // Gi = Ai+Bi
    10. assign c[1] = g[0] | (p[0]&c[0]);
    11. assign c[2] = g[1] | ( p[1]&(g[0] | (p[0]&c[0]) );
    12. assign c[3] = g[2] | (p[2]&(g[1]|(p[1]&(g[0]|(p[0]&c[0])))));
    13. assign c[4] = g[3] | (p[3]&(g[2]|(p[2]&(g[1]|(p[1]&(g[0]|(p[0]&c[0])))))));
    14. assign sum = p ^ c[3:0];
    15. assign cout = c[4];
    16. endmodule

     超前进位加法器就是用电路的复杂度来换时间。

  • 相关阅读:
    单图像超分辨率重建总结
    使用x64dbg手动脱UPX壳(UPX4.1.0)
    启动Docker Desktop报 “Docker Desktop - Unexpected WSL error”
    扫雷(C语言)
    MFC Windows 程序设计[340]之特效按钮的实现(附源码)
    【限定词习题】another / other / others
    asp.net core之HttpClient
    SpringSecurity-基于Web的认证与权限访问
    2023年电工杯 | 2023年电工杯数学建模数学建模竞赛思路(A题、B题)
    2022年下半年 软件设计师 上午试卷(41题—75题)
  • 原文地址:https://blog.csdn.net/qq_22774445/article/details/126257315