Advertisement
ptrawt

CPE212-[Lab8.1] q2

Sep 6th, 2013
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module ALU4Bit(y,o,a,b,ci,c);
  2.     input [3:0]a;
  3.     input [3:0]b;
  4.     input [1:0]c;
  5.     input ci;
  6.     output [3:0]y;
  7.     output o;
  8.     wire [3:0]z;
  9.     wire zor;
  10.     reg x;
  11.    
  12.     ALU1Bit alu0(y[0],z[0],a[0],b[0],ci,c);
  13.     ALU1Bit alu1(y[1],z[1],a[1],b[1],z[0],c);
  14.     ALU1Bit alu2(y[2],z[2],a[2],b[2],z[1],c);
  15.     ALU1Bit alu3(y[3],z[3],a[3],b[3],z[2],c);
  16.    
  17.     or or1(zor,z);
  18.     Mux4to1 mux1(o,zor,x,x,z[3],c);
  19.    
  20. endmodule
  21.  
  22. module ALU1Bit(y,z,a,b,ci,c);
  23.     input a,b,ci;
  24.     input [1:0]c;
  25.     output y,z;
  26.     wire s,co;          //y0,0
  27.     wire anda;          //y0,1
  28.     wire nota;          //y1,0
  29.  
  30.     wire xorab;         //z1,1
  31.    
  32.     reg x;
  33.  
  34.     FullAdder fa1(s,co,a,b,ci);     //y0,0
  35.     and and1(anda,a,b);         //y0,1
  36.     not not1(nota,a);           //y1,0
  37.  
  38.     xor xor1(xorab,a,b);        //z1,1
  39.    
  40.     Mux4to1 mux1(y,s,anda,nota,1'b0,c);
  41.     Mux4to1 mux2(z,co,x,x,xorab,c);
  42.  
  43. endmodule
  44.  
  45. module FullAdder(s,co,a,b,ci);
  46.     input a,b,ci;
  47.     output s,co;
  48.     wire xorab,andaci,andbci;
  49.    
  50.     xor xor1(xorab,a,b);
  51.     xor xor2(s,xorab,ci);
  52.  
  53.     and and1(andaci,xorab,ci);
  54.     and and2(andbci,b,a);
  55.  
  56.     or or1(co,andaci,andbci);
  57. endmodule
  58.  
  59. module Mux4to1(o,i1,i2,i3,i4,c);
  60.     input i1,i2,i3,i4;
  61.     input [1:0]c;
  62.     output o;
  63.  
  64.     wire andi1,andi2,andi3,andi4;
  65.    
  66.     input [1:0]nc;
  67.    
  68.     not not1(nc[0],c[0]);
  69.     not not2(nc[1],c[1]);
  70.  
  71.     and and1(andi1,i1,nc[1],nc[0]);
  72.     and and2(andi2,i2,nc[1],c[0]);
  73.     and and3(andi3,i3,c[1],nc[0]);
  74.     and and4(andi4,i4,c[1],c[0]);
  75.  
  76.     or or1(o,andi1,andi2,andi3,andi4);
  77. endmodule
  78.  
  79. module stimulus;
  80.     reg ci;
  81.     reg [3:0]a;
  82.     reg [3:0]b;
  83.     reg [1:0]c;
  84.     wire [3:0]y;
  85.     wire o;
  86.  
  87.     ALU4Bit alu4bit(y,o,a,b,ci,c);
  88.  
  89.     initial
  90.         begin
  91.             a = 1'b0;
  92.             b = 1'b0;
  93.             ci = 1'b0;
  94.             c = 1'b0;
  95.         end
  96.        
  97.     always #1 ci = ~ci;
  98.     always #2 a[0] = ~a[0];
  99.     always #4 a[1] = ~a[1];
  100.     always #8 a[2] = ~a[2];
  101.     always #16 a[3] = ~a[3];
  102.     always #32 b[0] = ~b[0];
  103.     always #64 b[1] = ~b[1];
  104.     always #128 b[2] = ~b[2];
  105.     always #256 b[3] = ~b[3];
  106.     always #512 c[0] = ~c[0];
  107.     always #1024 c[1] = ~c[1];
  108.  
  109.     initial #2047 $finish;
  110.  
  111.     initial
  112.         $monitor($time,"\tc = %b\ta = %b\tb = %b\tci = %d\ty = %b\tz = %d",c,a,b,ci,y,o);  
  113.    
  114. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement