Advertisement
jack96013

ex4_37

Jun 7th, 2020
2,169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //必考!(第一題) 給圖,跑出一樣的Waveform
  2. module Add_Sub_4bit(output [3:0] s,output c4,input[3:0]a,b,input M);
  3.     wire[3:0] b_bar;
  4.     xor G0(b_bar[0],b[0],M);
  5.     xor G1(b_bar[1],b[1],M);
  6.     xor G2(b_bar[2],b[2],M);
  7.     xor G3(b_bar[3],b[3],M);
  8.     wire c1,c2,c3;
  9.  
  10.     //Ripple Adder
  11.    
  12.      FullAdder M0(s[0],c1,a[0],b_bar[0],M);
  13.     FullAdder M1(s[1],c2,a[1],b_bar[1],c1);
  14.     FullAdder M2(s[2],c3,a[2],b_bar[2],c2);
  15.     FullAdder M3(s[3],c4,a[3],b_bar[3],c3);
  16.      
  17.     //前瞻
  18.     //cla_4bit M0(s,c4,a,b_bar,M);
  19.    
  20. endmodule
  21.  
  22. //前瞻
  23. module cla_4bit(output[3:0]s,output c4,input[3:0]a,b,input c0);
  24.     wire[3:0] p,g;
  25.     HalfAdder M0 (p[0],g[0],a[0],b[0]);
  26.     HalfAdder M1 (p[1],g[1],a[1],b[1]);
  27.     HalfAdder M2 (p[2],g[2],a[2],b[2]);
  28.     HalfAdder M3 (p[3],g[3],a[3],b[3]);
  29.  
  30.     assign c1=g[0]|(p[0]&c0);
  31.     assign c2=g[1]|(p[1]&g[0])|(p[1]&p[0]&c0);
  32.     assign c3=g[2]|(p[2]&g[1])|(p[2]&p[1]&g[0])|(p[2]&p[1]&p[0]&c0);
  33.     assign c4=g[3]|(p[3]&g[2])|(p[3]&p[2]&g[1])|(p[3]&p[2]&p[1]&g[0]) | (p[3]&p[2]&g[1]&g[0]&c0);
  34.    
  35.     assign s[3]=c3^p[3];
  36.     assign s[2]=c2^p[2];
  37.     assign s[1]=c1^p[1];
  38.     assign s[0]=c0^p[0];
  39.  
  40. endmodule
  41.  
  42.  
  43. module HalfAdder(output s,c,input a,b);
  44.     xor G0(s,a,b);
  45.     and G1(c,a,b);
  46. endmodule
  47.  
  48. module FullAdder(output s,co,input a,b,ci);
  49.     wire s1,c1,c2;
  50.     HalfAdder M0(s1,c1,a,b);
  51.     HalfAdder M1(s,c2,s1,ci);
  52.     or G0(co,c1,c2);
  53. endmodule
  54.  
  55.  
  56. module t_Add_Sub_4bit();
  57.     wire[3:0] s;
  58.     wire c;
  59.     reg[3:0] a;
  60.     reg[3:0] b;
  61.     reg M;
  62.  
  63.     Add_Sub_4bit M0(s,c,a,b,M);
  64.     initial begin
  65.             #20
  66.             a=4'b1011;
  67.             b=4'b0101;
  68.             M=0;
  69.             #20
  70.             a=4'b0100;
  71.             #20
  72.             M=1;
  73.             #20
  74.             a=4'b0011;
  75.             #20 $finish;
  76.     end
  77. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement