2015年12月16日 星期三

期末考



001

module top; 

wire a, A, b, B, c, C, d, D, F, F1, F2, F3, F4;
system_clock #800 clock1(A); 
system_clock #400 clock2(B);
system_clock #200 clock3(C);
system_clock #100 clock4(D);

nand S1(a, A, B);
nand S2(b, B, B);
nand S3(c, C, C);
nand S4(d, D, D);
nand C1(F4, A ,c , d);
nand C2(F2, a, b, D);
nand C3(F3, B, c, d);
nand C4(F1, C, D); 
nand o1(F, F4, F2, F3, F1);

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 

2015年12月2日 星期三

1202


module top; 

wire a, A, b, B, c, C, d, D, F, F1, F2, F3, F4;
system_clock #800 clock1(A); 
system_clock #400 clock2(B);
system_clock #200 clock3(C);
system_clock #100 clock4(D);

not S1(a, A);
not S2(b, B);
not S3(c, C);
not S4(d, D);
and C1(F4, A ,c , d);
and C2(F2, a, b, D);
and C3(F3, B, c, d);
and C4(F1, C, D); 
or o1(F, F4, F2, F3, F1);

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 

2015年11月25日 星期三

三位元加法器- 行為模式

module fulladder (sum, c_out, a, b, c_in);
wire s1, c1, c2;
output sum;
output c_out;
input a, b, c_in;

assign{c_out,sum}=a+b+c_in;
endmodule

module adder3(sum, c_out, a, b, c_in);
wire [2:0] c; 
output [2:0] sum;
output c_out;
input [2:0] a;
input [2:0] b;
input c_in;
fulladder fa1(sum[0], c[1], a[0], b[0], c_in) ;
fulladder fa2(sum[1], c[2], a[1], b[1], c[1]) ;
fulladder fa3(sum[2], c_out, a[2], b[2], c[2]) ;


endmodule

module main;
reg [2:0] a;
reg [2:0] b;
wire [2:0] sum;
wire c_out;

adder3 DUT (sum, c_out, a, b, 1'b0);

initial
begin
  a = 4'b0101;
  b = 4'b0000;
end

always #50 begin
  b=b+1;
  $monitor("%dns monitor: a=%d b=%d sum=%d", $stime, a, b, sum);
end

initial #2000 $finish;

endmodule

1125

二位元加法器

module top;
wire [1:0]Sum, A, B;
wire Cout, Cin;
system_clock #400 clock(Cin);
system_clock #200 clock(A[0]);
system_clock #100 clock(B[1]);
system_clock #200 clock(B[0]);
system_clock #100 clock(A[1]);

adder2 M2(Cout, Sum, A, B, Cin);
endmodule

module adder2(Cout, Sum, A, B, Cin);
output Cout;
output [1:0]Sum;
input [1:0]A,B;
input Cin;
adder1 lo(C0,Sum[0], A[0], B[0], Cin);
adder1 hi(Cout, Sum[1], A[1], B[1],C0);
endmodule



//一位元加法器
module adder1(Cout, Sum, A, B, Cin);
output Cout, Sum;
input A,B,Cin;
not I1 (Anot, A);
not I2 (Bnot, B);
not I3 (Cinnot, Cin);
and I4 (L1, A, Cin);
and I5 (L2, Cin, B);
and I6 (L3, A, B);
and I7 (L4, Cinnot, A, Bnot);
and I8 (L5, Cin, A, B);
and I9 (L6, Cinnot, Anot, B);
and I10 (L7, Cin, Anot, Bnot);
or I11 (Cout, L1, L2, L3);
or I12 (Sum, L4, L5, L6, L7);
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
三位元加法器-結構模式
module fulladder (sum, c_out, a, b, c_in);
wire s1, c1, c2;
output sum;
output c_out;
input a, b, c_in;
xor g1(s1, a, b);
xor g2(sum, s1, c_in);
and g3(c1, a,b);
and g4(c2, s1, c_in) ;
xor g5(c_out, c2, c1) ;

endmodule

module adder3(sum, c_out, a, b, c_in);
wire [2:0] c;
output [2:0] sum;
output c_out;
input [2:0] a;
input [2:0] b;
input c_in;
fulladder fa1(sum[0], c[1], a[0], b[0], c_in) ;
fulladder fa2(sum[1], c[2], a[1], b[1], c[1]) ;
fulladder fa3(sum[2], c_out, a[2], b[2], c[2]) ;


endmodule

module main;
reg [2:0] a;
reg [2:0] b;
wire [2:0] sum;
wire c_out;

adder3 DUT (sum, c_out, a, b, 1'b0);

initial
begin
  a = 4'b0101;
  b = 4'b0000;
end

always #50 begin
  b=b+1;
  $monitor("%dns monitor: a=%d b=%d sum=%d", $stime, a, b, sum);
end

initial #2000 $finish;

endmodule

2015年11月18日 星期三

1118


module test_adder1;

 reg a,b;
 reg carry_in ; 
 wire sum;
 wire carry_out;

 adder1_behavorial A1(carry_out, sum, a, b, carry_in);

 initial 
  begin

    carry_in = 0; a = 0; b = 0; 
    # 100 if ( carry_out != 0 | sum !== 0) 
                $display(" 0+0+0=00 sum is WRONG!");
              else
                $display(" 0+0+0=00 sum is RIGHT!");
    carry_in = 0; a = 0; b = 1; 
    # 100 if ( carry_out != 0 | sum !== 1) 
               $display(" 0+0+1=01 sum is WRONG!");
              else
               $display(" 0+0+1=01 sum is RIGHT!");
    carry_in = 0; a = 1; b = 0; 
    # 100 if ( carry_out != 0 | sum !== 1) 
               $display(" 0+1+0=01 sum is WRONG!");
              else
               $display(" 0+1+0=01 sum is RIGHT!");
    carry_in = 0; a = 1; b = 1; 
    # 100 if ( carry_out != 1 | sum !== 0) 
                $display(" 0+1+1=10 sum is WRONG!");
              else
                $display(" 0+1+1=10 sum is RIGHT!");
    carry_in = 1; a = 0; b = 0; 
    # 100 if ( carry_out != 0 | sum !== 1) 
               $display(" 1+0+0=01 sum is WRONG!");
              else
               $display(" 1+0+0=01 sum is RIGHT!");
     carry_in = 1; a = 0; b = 1; 
    # 100 if ( carry_out != 1 | sum !== 0) 
                $display(" 1+0+1=10 sum is WRONG!");
              else
                $display(" 1+0+1=10 sum is RIGHT!");
    carry_in = 1; a = 1; b = 0; 
    # 100 if ( carry_out != 1 | sum !== 0) 
               $display(" 1+1+0=10 sum is WRONG!");
              else
               $display(" 1+1+0=10 sum is RIGHT!");
    carry_in = 1; a = 1; b = 1; 
    # 100 if ( carry_out != 1 | sum !== 1) 
               $display(" 1+1+1=11 sum is WRONG!");
              else
               $display(" 1+1+1=11 sum is RIGHT!");
    $finish;
  end
endmodule



module adder1_behavorial (carry_out, sum, a, b, carry_in);
 input a, b, carry_in;
 output carry_out, sum;
  assign sum = (~a&b&~carry_in)|(~carry_in&a&~b)|(a&b&carry_in)|(~a&~b&carry_in); 
  assign carry_out = a&carry_in|a&b|b&carry_in; 
endmodule

module top;
wire Cout, Sum, A, B, Cin;
system_clock #400 clock1(Cin); 
system_clock #200 clock2(A);
system_clock #100 clock3(B);
adder1 M1(Cout, Sum, A, B, Cin);
endmodule

module adder1(Cout, Sum, A, B, Cin);
output Cout,Sum;
input A,B,Cin;
and I1 (AandB, A, B);
xor I2 (AxorB, A, B);
and I3 (And1, AxorB, Cin);
or I4 (Cout, AandB, And1);
xor I5 (Sum, AxorB, 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 



2015年9月23日 星期三

2015年9月16日 星期三

學電機Verilog不會怎麼行

CIC 數位IC認證

「數位 IC 設計能力鑑定」,目的在於可評量各產官學研界提供之各數位電路設計 課程其考生的學習成效,以具備獨立完成數位電路邏輯設計流程與驗證之基本能力為主要目 的;更可進一步推廣數位電路實作,提升現有數位電路設計工程師之專業能力,提供產業界優 秀人才,降低企業訓練成本。



以上來源網址: http://www.cic.narl.org.tw/announce/HotNews_detail.jsp?newsid=54



Verilog 的兩種主要模式

1. 結構模式 (Structural Modeling) : 描述網路連線 (netlist) 的方式,元件和元件之間如何連接起來。
  • 模組 (Model):透過輸出入 (I/O) 與其他模組連接起來,模組內可以包含元件 (instance)、子模組、訊號 (signal) 等。
    • 模組是靜態 (static) 且平行執行 (concurrent) 的單位。
2. 行為模式 (Behavioral Modeling) : 有順序關係 (sequencing),更加彈性,同時可用來寫電路與測試程式 (testbench)
  • 平行:Initial , Always — 事件驅動模式 (Concurrent, event-triggered processes)
  • 控制:Assignment, if else, case — 進行順序控制,可加上延遲一段時間 #time 的概念。
Verilog 的兩種主要資料型態
1. 線路 (Nets) : 代表連線,不能儲存內容,代表閘或模組之間的連線,不可以被指定 (assign)
範例:wireinputoutput
2. 暫存 (Reg) :代表存儲空間,就像暫存器一樣,儲存某值,直到下次被指定 (assign) 為止。
範例:reginput regoutput reg(可以用來代表正反器 latchflip-flop)
注意:Reg 不可與某個元件連結 (Never connected to something)


以上來源網址 : http://ccckmit.wikidot.com/ve:basic