Advertisement
SMF

ECE282Fig2

SMF
Dec 9th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns / 1ps
  2. //////////////////////////////////////////////////////////////////////////////////
  3. // Company:
  4. // Engineer:
  5. //
  6. // Create Date:    15:24:12 12/07/2015
  7. // Design Name:
  8. // Module Name:    lab8fig2
  9. // Project Name:
  10. // Target Devices:
  11. // Tool versions:
  12. // Description:
  13. //
  14. // Dependencies:
  15. //
  16. // Revision:
  17. // Revision 0.01 - File Created
  18. // Additional Comments:
  19. //
  20. //////////////////////////////////////////////////////////////////////////////////
  21. /*module test_circuit;
  22.   reg [3:0] A;
  23.   reg [3:0] B;
  24.   reg [2:0] M;
  25.   //reg [3:0] _V;
  26.   wire [3:0] Sout;
  27.   //wire [3:0] sub_S;
  28.   wire V;
  29.  
  30.   _4bit_adder add(Sout, V, A, B, M);
  31.   //lab8fig2(Ei_out, S0_in, S1_in, Ai_in, Bi_in);
  32.  
  33.   initial begin
  34.   M = 3'b0;
  35.   A = 4'b0111;
  36.   B = 4'b0110;
  37.   #50
  38.   A = 4'b1000;
  39.   B = 4'b1001;
  40.   #50
  41.   M = 3'b1;
  42.   A = 4'b1100;
  43.   B = 4'b1000;
  44.   #50
  45.   A = 4'b0101;
  46.   B = 4'b1010;
  47.   #50
  48.   A = 4'b0000;
  49.   B = 4'b0001;
  50.   B = 4'b0000;
  51.   #50 M = 3'b010; //increment
  52.   #50 M = 3'b011; //decrement
  53.   #50 M = 3'b100; //xfer
  54.  
  55.   end
  56. endmodule*/
  57.  
  58. //Description of 4-bit adder (see Fig 4-9)
  59. module _4bit_adder(Sout, V, _A, _B, C0);
  60.    input [3:0] _A, _B;
  61.    input C0; //C0 is M
  62.    output [3:0] Sout;
  63.    output V; //overflow
  64.    wire C1, C2, C3;
  65.  
  66.      
  67.                  
  68.    fulladdersub  FA0 (Sout[0],C1,_A[0],_B[0],C0,C0),
  69.               FA1 (Sout[1],C2,_A[1],_B[1],C1,C0),
  70.               FA2 (Sout[2],C3,_A[2],_B[2],C2,C0),
  71.               FA3 (Sout[3],C4,_A[3],_B[3],C3,C0);
  72.     xor (V,C3,C4);           
  73. endmodule
  74.  
  75. module fulladdersub (Sout, Cout, A, B, Cin, M);
  76.     output Sout, Cout;
  77.     input Cin, M, A, B;
  78.     wire w1;
  79. //  xor (w1,b,m);
  80.      Multiplexor mux(w1,M,B|((~B) << 1)|(1 << 3));
  81.     fulladder fa(Sout,Cout,A,w1,Cin);
  82.    
  83. endmodule
  84.  
  85.  
  86. module Multiplexor(O,S,I);
  87. parameter S00 = 3'b0000,S01 = 3'b0001,S10 = 3'b0010,S11=3'b0011;
  88.     output reg O;
  89.     input [2:0] S;
  90.     input [3:0] I;
  91.     always @ (S,I)
  92.         case(S)
  93.             S00:O<=I[0];
  94.             S01:O<=I[1];
  95.             S10:O<=I[2];
  96.             S11:O<=I[3];
  97.             //default: O <= O;
  98.             default O<=3'b000;
  99.         endcase
  100.  
  101. endmodule
  102.  
  103. //4x1 MUX
  104. module mux41_bh(MUXout, i0, i1, i2, i3, s1, s0);
  105.  
  106.   //port declarations
  107.   input i0, i1, i2, i3;
  108.   input s1, s0;
  109.   output reg MUXout;
  110.  
  111.   //using basic and, or, not logic operators
  112.   always @(i0 or i1 or i2 or i3 or s1 or s0)
  113.     MUXout = (~s1 & ~s0 & i0) |
  114.              (~s1 & s0 & i1) |
  115.              (s1 & ~ s0 & i2) |
  116.              (s1 & s0 & i3);
  117.  
  118. endmodule
  119.  
  120. //Description of full adder (see Fig 4-8)
  121. module fulladder (Sout, Cout, x_in, y_in, z);
  122.    input x_in, y_in, z;
  123.    output Sout, Cout;
  124.    wire S1, D1, D2; //Outputs of first XOR and two AND gates
  125. //Instantiate the halfadder
  126.     halfadder HA1 (S1, D1, x_in, y_in),
  127.               HA2 (Sout, D2, S1, z);
  128.     or g1(Cout, D2, D1);
  129. endmodule
  130.  
  131. module halfadder (S_out, C_out, x_in, y_in);
  132.    input x_in, y_in;
  133.    output S_out, C_out;
  134. //Instantiate primitive gates
  135.    xor g5(S_out, x_in, y_in);
  136.    and g6(C_out, x_in, y_in);
  137. endmodule
  138.  
  139. module lab8fig2(Ei_out, S0_in, S1_in, Ai_in, Bi_in);
  140.     input S0_in, S1_in, Ai_in, Bi_in;
  141.     output Ei_out;
  142.    
  143.     and g1(w1, Ai_in, Bi_in);
  144.     or g2 (w2, Ai_in, Bi_in);
  145.     xor g3 (w3, Ai_in, Bi_in);
  146.     not g4 (w4, Ai_in, Bi_in);
  147.    
  148.     //use one mux
  149.     mux41_bh mux1 (Ei_out, S0_in, S1_in, w1, w2, w3, w4);
  150.    
  151.  
  152. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement