2014年6月11日 星期三

考試

module top;

wire a,b,c,d;
wire out;

 system_clock #800 clock1(a);
 system_clock #400 clock2(b);
 system_clock #200 clock3(c);
 system_clock #100 clock4(d);

not n1(nota,a);
not n2(notb,b);
not n2(notc,c);
not n4(notd,d);
and a1(f1,nota,b,notc);
and a2(f2,a,notc,d);
and a3(f3,a,c,notd);
and a4(f4,notb,c,notd);
or  o0(out,f1,f2,f3,f4);

endmodule

module system_clock(clk);
parameter PERIOD=100;
output clk;
reg clk;
initial clk=0;
always
 begin
#(PERIOD/2) clk=~clk;
 end
always@(posedge clk)
 if($time>2000)$stop;
endmodule

2014年4月30日 星期三

1位元加法器--結構

module top;
wire A, B, A0 , B0 , C0 , Cin , Sum, Cout;
system_clock #400 clock1(A);
system_clock #200 clock2(B);
system_clock #100 clock3(Cin);
and a1(A0, A,B);
xor x1(B0, A,B);
and a2(C0, B0,Cin);
or  o1(Cout, A0, C0);
xor x2(Sum, B0 , Cin);
endmodule
module system_clock(clk);
parameter PERIOD=100;
output clk;
reg clk;
initial clk=0;
always
 begin
#(PERIOD/2) clk=~clk;
 end
always@(posedge clk)
 if($time>1000)$stop;
endmodule

1位元加法器--行為

module top;
  integer A0,B0,Cin0;
  reg  A,B,Cin;
  wire Cout,Sum;
  mux_behavioral mux1(Cout,Sum,A,B,Cin);
  initial
    begin
      for (A0=0; A0<=1; A0 = A0+1)
        begin
          A = A0;
          for (B0=0; B0<=1; B0 = B0+1)
            begin
              B = B0;
               for (Cin0=0; Cin0<=1; Cin0 = Cin0+1)
                 begin
                   Cin = Cin0;
                 #1 $display("A=%d B=%d Cin=%d ",A,B,Cin,Cout,Sum);
                 end
             end
         end
    end
endmodule
module mux_behavioral(Cout,Sum,A,B,Cin);
 output Cout,Sum;
 input A,B,Cin;
 wire  A,B,Cin;
 reg   Cout,Sum;
always @(A or B or Cin)
 begin
   Cout = (Cin & (A^B)) | (A&B);
   Sum =  (Cin ^(A^B));
 end
endmodule

2014年3月27日 星期四

2位元多工器--階層模式

module top;
  integer is;
  integer ia[1:0],ib[1:0];
  reg [1:0]a,b;
  reg s;
  wire [1:0]out;

  mux_behavioral mux2(out,a,b,s);

  initial
    begin
      for (ia[0]=0; ia[0]<=1; ia[0] = ia[0]+1)
        begin
          a[0]= ia[0];
          for (ia[1]=0; ia[1]<=1; ia[1] = ia[1]+ 1)
            begin
              a[1] = ia[1];
               for (ib[0]=0; ib[0]<=1; ib[0] = ib[0]+1)
                 begin
                   b[0] = ib[0];
                    for (ib[1]=0; ib[1]<=1; ib[1] = ib[1]+ 1)
                     begin
                      b[1] = ib[1];
                        for (is=0; is<=1; is = is + 1)
                       begin
                        s = is;
                 #1 $display("a[0]=%d a[1]=%d b[0]=%d b[1]=%d s=%d out[0]%d out[1]%d",a[0],a[1],b[0],b[1],s,out[0],out[1]);
                      end
                    end
                  end
              end
         end
    end
endmodule

module mux_behavioral(OUT,A,B,SEL);
 output [1:0]OUT;
 input [1:0] A,B;
 input SEL;

mux1 X1(OUT[0],A[0],B[0],SEL);
mux1 X2(OUT[1],A[1],B[1],SEL);

endmodule

module mux1(OUT, A, B, SEL);
 output OUT;
 input A,B,SEL;

 not n1(NOT_SEL, SEL);
 and a1 (X, A, NOT_SEL);
 and a2 (Y, SEL, B);
 or  o1 (OUT, X, Y);

endmodule

2014年3月20日 星期四

二位元多工器---行為模式



module top;
  integer ia0,ia1,ib0,ib1,is;
  reg  a0,a1,b0,b1,s;
  wire out1,out2;

  mux_behavioral mux1(out1,out2,a0,a1,b0,b1,s);

  initial
    begin
      for (ia0=0; ia0<=1; ia0 = ia0+1)
        begin
          a0 = ia0;
          for (ia1=0; ia1<=1; ia1 = ia1+ 1)
            begin
              a1 = ia1;
               for (ib0=0; ib0<=1; ib0 = ib0+1)
                 begin
                   b0 = ib0;
                   for (ib1=0; ib1<=1; ib1 = ib1+ 1)
                    begin
                     b1 = ib1;
                      for (is=0; is<=1; is = is + 1)
                       begin
                        s = is;
                 #100 $display("a0=%d a1=%d b0=%d b1=%d s=%d out1=%d out2=%d",a0,a1,b0,b1,s,out1,out2);
                       end
                    end
                  end
              end
         end
   end
endmodule

module mux_behavioral(OUT1,OUT2,A0,A1,B0,B1,SEL);
 output OUT1,OUT2;
 input A0,A1,B0,B1,SEL;
 wire  A0,A1,B0,B1,SEL;
 reg   OUT1,OUT2;

always @(A0 or A1 or B0 or B1 or SEL)
 begin
   OUT1 = (A0 & SEL)|(B0 & ~SEL );
   OUT2 = (A1 & SEL)|(B1 & ~SEL );
 end
endmodule

一位元多工器--結構模式