2015年11月25日 星期三

3位元全加法器

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

1118 2


1118


2015年11月18日 星期三

一位元全加法器

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_in != 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_in != 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_in != 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_in != 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_in != 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_in != 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_in != 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_in != 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);
  assign carry_out = a&carry_in|a&b|b&carry_in;
endmodule

2015年10月28日 星期三

1028 四位元

module top;

wire [3:0]OUT, A, B;
wire SEL;
system_clock #12800 clock1(A[3]);
system_clock #6400 clock2(A[2]);
system_clock #3200 clock3(A[1]);
system_clock #1600 clock4(A[0]);
system_clock #800 clock5(B[3]);
system_clock #400 clock6(B[2]);
system_clock #200 clock7(B[1]);
system_clock #100 clock8(B[0]);
system_clock #25600 clock7(SEL);
mux2 m1(OUT[1:0],A[1:0],B[1:0],SEL);
mux2 m2(OUT[3:2],A[3:2],B[3:2],SEL);
endmodule

module mux(OUT, A, B, SEL);
output OUT;
input A,B,SEL;
not I5 (sel_n, SEL) ;
and I6 (sel_a, A, SEL);
and I7 (sel_b, sel_n, B);
or I4 (OUT, sel_a, sel_b);
endmodule
module mux2(OUT, A, B, SEL);
output [1:0] OUT;
input [1:0] A,B;
input SEL;
mux hi (OUT[1], A[1], B[1], SEL);
mux lo (OUT[0], A[0], B[0], SEL);
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>25600)$stop;
endmodule

1021


2015年10月14日 星期三

10.14

module top;
wire OUT, OUT1, A1, B1, SEL, A0, B0;
system_clock #1600 clock1(A1);
system_clock #800 clock2(A0);
system_clock #400 clock3(SEL);
system_clock #200 clock4(B1);
system_clock #100 clock5(B0);

mux M1(OUT1, A1, B1, SEL);
mux M2(OUT0, A0, B0, SEL);

endmodule

module mux(OUT, A, B, SEL);

output OUT;

input A,B,SEL;

and a1(sel_1, A, SEL);

not n1(sel_n, SEL);

and a2(sel_2, B, sel_n);

or OUT(OUT, sel_1, sel_2);

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>5000)$stop;

endmodule

10.14 1


2015年10月7日 星期三


module top;

wire A,B,C,D,E,OUT1,OUT2,OUT3,OUT4,OUT5,F;

system_clock#1600 clock(A);
system_clock#800 clock(B);
system_clock#400 clock(C);
system_clock#200 clock(D);
system_clock#100 clock(E);

not a1(OUT1,A);
and a2(OUT2,OUT1,B);
or a3(OUT3,C,D);
not a4(OUT4,OUT3);
and a5(OUT5,OUT4,E);
or a6(F,OUT2,OUT5);

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月30日 星期三

一開始聽不太懂但是朋友
在跟我講一次之後就懂了


module top; 

wire A, B, C, OUT1, OUT2;
system_clock #400 clock1(A); 
system_clock #200 clock2(B);
system_clock #100 clock3(C);

and a1(OUT1, A, B);
and a2(OUT2, OUT1, C);

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日 星期三

and

and and andand and andand and andand and andand and andand and andand and andand and andand and andand and and

2015年9月16日 星期三

Verilog 電路設計

Verilog 基礎
module <name> // 模組名稱
 parameter ... // 參數宣告
 port ... // 腳位宣告
 wire ... // 線宣告
 reg  ... // 暫存器宣告

 initial begin // 初始化設定區塊 
 end

 assign ... // 資料處理層級之描述

 ... // 引用較低階模組別名

 always begin // 行為層級之描述區塊
   // 資料處理與指定等描述
   // task與function的使用
 end

 function  // 函數宣告
 task       // 作業宣告

endmodule
Verilog 電路設計

Verilog 與 VHDL 都是用來設計數位電路的硬體描述語言,但 VHDL 在1983年被提出後,1987 年被美國國防部和IEEE確定為標準的硬體描述語言。
Verilog 是由 Gateway Design Automation 公司於 1984 年開始發展的, Cadence Design Systems 公司於1990年購併了 Gateway 公司,Cadence 隨後將 Verilog 提交到 Open Verilog International 成為開放公用標準,1995 年 Verilog 被 IEEE 認可成為 IEEE 1364-1995 標準,簡稱為 Verilog-95。此一標準於 2001 年更新後成為 Verilog-2001。
相較於 VHDL 而言,Verilog 的語法較為簡潔,因此經常被專業的數位電路設計者採用,而 VHDL 的使用族群則有較多的初學者。當我們想學習數位電路設計時,經常會難以選擇要用哪一種語言,因為 VHDL 的書籍與教材似乎比 Verilog 多一些,但是 Verilog 的高階設計電路(像是開放原始碼 CPU 等)則比 VHDL 多很多。
筆者是為了要設計 CPU 而學習數位電路設計的,因此決定學習 Verilog 語言,而非 VHDL 語言。雖然筆者也學過 VHDL 語言,但後來發現 Verilog 相當好,相對而言語法簡潔了許多,因此筆者比較偏好 Verilog 語言。
筆者的專長是軟體程式設計,因此熟悉 C, C#, Java, R, JavaScript 等語言,但由於 Verilog 或 VHDL 都是設計數位電路硬體用的語言,因此與那些軟體語言有某種程度上的不同,其中最重要的幾個特性如下:
  • Concurrent v.s. Sequential
  • Blocking v.s. Nonblocking
  • Delayed evaluations v.s. Delayed assignments
這些特性是在學習 Verilog 語言時,必須要特別注意的,一但能清楚的分辨這些特性之後,您就可以從「軟體設計領域」跨入「硬體設計領域」了,對於筆者而言,這是一件相當令人興奮的事情。