Advertisement
tsounakis

ex1_thirdlab

Nov 20th, 2020
1,661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. module FA (a,b,cin,cout,sum);
  3.     input a,b, cin;
  4.     output reg cout, sum;
  5.  
  6.     always @ (*)
  7.         begin
  8.             {cout, sum} = a + b + cin;
  9.         end
  10.  
  11. endmodule
  12. */
  13.  
  14. module MUX2to1NB (s, i, f);
  15.     parameter n = 4;
  16.     input               s;
  17.     input [n-1:0]       i;
  18.     output reg [n-1:0]  f;
  19.    
  20.     always @ (*)
  21.         begin
  22.             f   = i[s +: n];
  23.         end
  24. endmodule
  25.  
  26. module CLA4bit  (a, b, cin, cout, s);
  27.     parameter n = 4;
  28.  
  29.     input   [n-1:0] a, b;
  30.     input           cin;
  31.     output  [n-1:0] s;
  32.     output          cout;
  33.  
  34.     wire    [n:0]   c;
  35.     wire    [n-1:0] g, p, sum;
  36.    
  37.     assign c[0]     = cin;
  38.    
  39.     genvar  j;
  40.     generate
  41.         for (j=0; j<n; j=j+1)
  42.             begin
  43.                 assign g[j]     = a[j] & b[j];
  44.                 assign p[j]     = a[j] ^ b[j];
  45.                 assign sum[j]   = p[j] ^ c[j];
  46.                 assign c[j+1]   = g[j] | p[j] & c[j];
  47.             end
  48.     endgenerate
  49.    
  50.     assign cout = c[n];
  51.     assign s = sum;
  52.    
  53. endmodule
  54.  
  55. module Adder8bit    (a, b, cin, cout, s);
  56.     input   [7:0]   a, b;
  57.     input           cin;
  58.    
  59.     output          cout;
  60.     output  [7:0]   s;
  61.    
  62.     wire            w_c0, w_c1, w_c2, w_cin1, w_cin2;
  63.     wire    [3:0]   w_s1, w_s2;
  64.    
  65.     assign w_cin1 =     0;
  66.     assign w_cin2 =     1;
  67.    
  68.     CLA4bit#(.n(4)) CLA0 (.a(a[3:0]), .b(b[3:0]), .cin(cin), .cout(w_c0), .s(s[3:0]));
  69.     CLA4bit#(.n(4)) CLA1 (.a(a[7:4]), .b(b[7:4]), .cin(w_cin1), .cout(w_c1), .s(w_s1));
  70.     CLA4bit#(.n(4)) CLA2 (.a(a[7:4]), .b(b[7:4]), .cin(w_cin2), .cout(w_c2), .s(w_s2));
  71.    
  72.     MUX2to1NB#(.n(4)) MUX1 (.s(w_c0), .i({w_s2, w_s1}), .f(s[7:4]));
  73.     MUX2to1NB#(.n(1)) MUX2 (.s(w_c0), .i({w_c2, w_c1}), .f(cout));
  74.    
  75. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement