Glaas2

LAC adder

Aug 4th, 2022
1,583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //https://verilogcodes.blogspot.com/2017/11/verilog-code-for-carry-look-ahead-adder.html
  2. //https://verilogcodes.blogspot.com/2017/11/verilog-code-for-carry-select-adder.html
  3. //http://techmasterplus.com/programs/verilog/claadder.php?i=1
  4. ////////////////////////////////cla_adder.v
  5. module cla_adder
  6.         (   input [3:0] A,B,
  7.             input cin,
  8.             output [3:0] S,
  9.             output cout
  10.             );
  11.            
  12. wire [3:0] P,G;
  13. wire [4:0] C;  
  14.    
  15. //first level
  16. assign P = A ^ B;
  17. assign G = A & B;
  18.  
  19. //second level
  20. cla_block gen_c(P,G,cin,C);
  21.  
  22. //third level
  23. assign S = P ^ C[3:0];
  24. assign cout = C[4];
  25.  
  26. endmodule
  27.  
  28.  
  29. //////////////////////////////////////////////////cla_block.v
  30. module cla_block
  31.         (   input [3:0] P,G,
  32.             input cin,
  33.             output [4:0] C
  34.             );
  35.            
  36. assign C[0] = cin;
  37. assign C[1] = G[0] | (P[0] & cin);
  38. assign C[2] = G[1] | (P[1] & G[0]) | (P[1] & P[0] & cin);
  39. assign C[3] = G[2] | (P[2] & G[1]) | (P[2] & P[1] & G[0]) | (P[2] & P[1] & P[0] & cin);
  40. assign C[4] = G[3] | (P[3] & G[2]) | (P[3] & P[2] & G[1]) |
  41.             (P[3] & P[2] & P[1] & G[0]) | (P[3] & P[2] & P[1] & P[0] & cin);
  42.  
  43. endmodule
  44.  
  45.  
  46. /////////////////////////////////////////////////////////////// otro
  47.  
  48. ///////////////////////////////////////////////////////////////////////////
  49. // Company: TMP
  50. // Create Date: 08:15:45 01/12/2015
  51. // Module Name: CLA_Adder
  52. // Project Name: Carry Look Ahead Adder
  53. ///////////////////////////////////////////////////////////////////////////
  54. ///////////////////////////////////////////////////////////////////////////
  55. // Company: TMP
  56. // Create Date: 08:15:45 01/12/2015
  57. // Module Name: CLA_Adder
  58. // Project Name: Carry Look Ahead Adder
  59. ///////////////////////////////////////////////////////////////////////////
  60. module CLA_Adder(a,b,cin,sum,cout);
  61. input[3:0] a,b;
  62. input cin;
  63. output [3:0] sum;
  64. output cout;
  65. wire p0,p1,p2,p3,g0,g1,g2,g3,c1,c2,c3,c4;
  66. assign p0=(a[0]^b[0]),
  67. p1=(a[1]^b[1]),
  68. p2=(a[2]^b[2]),
  69. p3=(a[3]^b[3]);
  70. assign g0=(a[0]&b[0]),
  71. g1=(a[1]&b[1]),
  72. g2=(a[2]&b[2]),
  73. g3=(a[3]&b[3]);
  74. assign c0=cin,
  75. c1=g0|(p0&cin),
  76. c2=g1|(p1&g0)|(p1&p0&cin),
  77. c3=g2|(p2&g1)|(p2&p1&g0)|(p1&p1&p0&cin),
  78. c4=g3|(p3&g2)|(p3&p2&g1)|(p3&p2&p1&g0)|(p3&p2&p1&p0&cin);
  79. assign sum[0]=p0^c0,
  80. sum[1]=p1^c1,
  81. sum[2]=p2^c2,
  82. sum[3]=p3^c3;
  83. assign cout=c4;
  84. endmodule
  85. //////////////// testbe
  86. module TestModule;
  87. // Inputs
  88. reg [3:0] a;
  89. reg [3:0] b;
  90. reg cin;
  91.  
  92. // Outputs
  93. wire [3:0] sum;
  94. wire cout;
  95.  
  96. // Instantiate the Unit Under Test (UUT)
  97. CLA_Adder uut (
  98. .a(a),
  99. .b(b),
  100. .cin(cin),
  101. .sum(sum),
  102. .cout(cout)
  103. );
  104. initial begin
  105. // Initialize Inputs
  106. a = 0;
  107. b = 0;
  108. cin = 0;
  109. // Wait 100 ns for global reset to finish
  110. #100;
  111. a = 5;
  112. b = 6;
  113. cin = 1;
  114. // Wait 100 ns for global reset to finish
  115. #100;
  116. end
  117. endmodule
  118.  
Advertisement
Add Comment
Please, Sign In to add comment