Advertisement
jack96013

ex4_55

Jun 7th, 2020
1,984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //BCD Add Sub
  2. module BCD_Add_Sub(output[3:0] sum_diff,output carry_borrow,input[3:0] a,b,input Mode);
  3.     wire[3:0] b_comp;
  4.     wire[3:0] mux_output;
  5.    
  6.     Nines_Complement m0(b_comp,b);
  7.     quad_mux21 m1(mux_output,b,b_comp,Mode);
  8.     BCD_Adder m2(sum_diff,carry_borrow,a,mux_output,Mode);
  9. endmodule
  10.  
  11. //4_54 9's
  12. module Nines_Complement(output reg[3:0]comp,input[3:0]bcd);
  13.     always @(bcd)
  14.         case (bcd)
  15.             4'd0:comp=4'd9;
  16.             4'd1:comp=4'd8;
  17.             4'd2:comp=4'd7;
  18.             4'd3:comp=4'd6;
  19.             4'd4:comp=4'd5;
  20.             4'd5:comp=4'd4;
  21.             4'd6:comp=4'd3;
  22.             4'd7:comp=4'd2;
  23.             4'd8:comp=4'd1;
  24.             4'd9:comp=4'd0;
  25.             default: comp=4'b1111;
  26.         endcase
  27. endmodule
  28.  
  29. //4.38
  30. module quad_mux21(output[3:0]Y,input[3:0] A,B,input Select);
  31.     //多工
  32.     assign Y=Select ? B:A;
  33. endmodule
  34.  
  35. module BCD_Adder(output[3:0] sum,output carry_out,input[3:0]a,b,input c_in);
  36.     wire[3:0] z;
  37.     wire c_out1,c_out2,w1,w2;
  38.     wire[3:0] y;
  39.  
  40.     supply0 gnd;
  41.     //For fix (when value is over 1001)
  42.     //CarryOut=cout+z3z2+z3z1;
  43.     and g0(w1,z[3],z[2]);
  44.     and g1(w2,z[3],z[1]);
  45.     or g2(carry_out,c_out1,w1,w2);
  46.    
  47.     assign y={1'b0,carry_out,carry_out,1'b0}; //Detect i f the value need fix
  48.  
  49.     //可用cla_4bit;    
  50.     Add_4bit m0(z,c_out1,a,b,c_in);
  51.  
  52.     Add_4bit m1(sum,c_out2,z,y,gnd); //For fix
  53. endmodule
  54.  
  55. module Add_4bit(output[3:0]s,output c4,input[3:0] a,b,input c0);
  56.    assign {c4,s}=a+b+c0;
  57. endmodule
  58.  
  59. //前瞻
  60. module cla_4bit(output[3:0]s,output c4,input[3:0]a,b,input c0);
  61.     wire[3:0]p,g;
  62.     HalfAdder M0(p[0],g[0],a[0],b[0]);
  63.     HalfAdder M1(p[1],g[1],a[1],b[1]);
  64.     HalfAdder M2(p[2],g[2],a[2],b[2]);
  65.     HalfAdder M3(p[3],g[3],a[3],b[3]);
  66.  
  67.     assign c1=g[0]|(p[0]&c0);
  68.     assign c2=g[1]|(p[1]&g[0])|(p[1]&p[0]&c0);
  69.     assign c3=g[2]|(p[2]&g[1])|(p[2]&p[1]&g[0])|(p[2]&p[1]&p[0]&c0);
  70.     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);
  71.    
  72.     assign s[3]=c3^p[3];
  73.     assign s[2]=c2^p[2];
  74.     assign s[1]=c1^p[1];
  75.     assign s[0]=c0^p[0];
  76.  
  77. endmodule
  78.  
  79.  
  80. module HalfAdder(output s,c,input a,b);
  81.     xor G0(s,a,b);
  82.     and G1(c,a,b);
  83. endmodule
  84.  
  85. module FullAdder(output s,co,input a,b,ci);
  86.     wire s1,c1,c2;
  87.     HalfAdder M0(s1,c1,a,b);
  88.     HalfAdder M1(s,c2,s1,ci);
  89.     or G0(co,c1,c2);
  90. endmodule
  91.  
  92. //TestBench;
  93. module t_BCD_Add_Sub();
  94.     reg[3:0] a,b;
  95.     reg Mode;
  96.     wire[3:0] sum;
  97.     wire c_out;
  98.  
  99.     BCD_Add_Sub m0(sum,c_out,a,b,Mode);
  100.     //BCD_Adder m0(sum,c_out,a,b,Mode);
  101.     initial fork
  102.         Mode=0;
  103.         a=4'b1001;
  104.         b=4'd0101;
  105.         #20
  106.           a=4'b0100;
  107.           #40
  108.           a=4'b0011;
  109.           Mode=1;
  110.           #60
  111.           a=4'b0111;
  112.         #80 $finish;
  113.     join
  114. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement