Verilog

Friday, June 23, 2006

吊橋的程式

這次的吊橋,自己有嘗試去寫過,可是卻漏洞百出,慘不忍睹阿,只好參考同學所作出來的程式,經過一番了解,與跟人探討後,無形中亦學到了很多東西!雖然這個版本的程式,每個後面都有註解,但還是有些東西是字面上所得不到的,一定要自己思考過,重新寫過一次,這樣東西才會變成自己的!

===================================================================

module bridge();
real fL,fR,w,dx,ans,t1,t2,t3,a;
real change,app,dff,max;
integer i; //正整數I
parameter nodes=11; //節點我看就不要改好ㄌ
reg[0:100]x[0:nodes]; //有N個點每一點都有100個位元
reg[0:100]a1,a2,a3,temp;
initial //initial區塊
begin
dff=1.0; //為分兩次的值
fL=10.0; //吊橋最左邊低高度
fR=20.0; //吊橋最右邊低高度
w=10.0; //吊橋低寬度
a=(fR-fL-0.5*dff*w*w)/w; //為解析法中X項的的係數0.5*dff*i*dx*i*dx+"a"*i*dx+fL
dx=w/(nodes-1); //節點之間低距離
max=1.0; //最大誤差起始值
x[0]=$realtobits(fL); //將FL轉成2進制存到X[0]裡面
x[nodes-1]=$realtobits(fR); //將FR轉成2進制存到x[nodes-1]裡面
for(i=1;i<=nodes-2;i=i+1) //起始化但是保留最左邊根最右邊的值 x[i]=0.0; //將X[1]到X[9]起始值都設為0 while (max>0.001)//最大誤差要是比0.001大或是-0.001大就進入回圈
begin
max=0.0;
for(i=1;i<=nodes-2;i=i+1)//此FOR迴圈開始就是疊代 begin //此三數為連續如456那a1=4 a2=5 a3=6 a1=x[i-1];//左邊的點 a2=x[i];//中間的點 a3=x[i+1];//右邊的點 t1=$bitstoreal(a1);//必須轉回10進制才能運算 t2=$bitstoreal(a2);//必須轉回10進制才能運算 t3=$bitstoreal(a3);//必須轉回10進制才能運算 ans=0.5*(t1+t3-dff*dx*dx);//此行老師為必考推導過程下面附注 change=ans-t2;//把後面算出來的減掉前一次所算出的答案 x[i]=$realtobits(ans);//把ANS轉成2進制存到X[i] if(change<0) change="change*-1.0;//">max)//change要是比MAX大MAX換成change
max=change;//
end
end
for(i=0;i begin
temp=x[i];//將上一個迴圈的X[i]抓下來存到temp
app=$bitstoreal(temp);//再把temp轉成10進制存到app
$display(" nodes %d\n 差分 %f 解析 %f\n"
,i,app,0.5*dff*i*dx*i*dx+a*i*dx+fL);
//0.5*dff*i*dx*i*dx+a*i*dx+fL此行為解析法就是對dff做兩次積分所得到的]
end
end
endmodule
//ANS推導
//d^2w/dx^2={a1-2(a2)+a3}/dx^2=dff
//{a1-2(a2)+a3}/dx^2=dff把此式做移項
//a1-2a2+a3=(dff)(dx^2)
//2a2=(dff)(dx^2)-a1-a3
//a2={(dff)(dx^2)-a1-a3}/2
//參考老師C語言P195頁
//a=(fR-fL-0.5*dff*w*w)/w;推導
//對d^2w/dx^2=dff做兩次積分
//dw/dx=dff*x+a第一次積分
//w(x)=1/2(dff*x^2)+a*x+b
//將邊界條件帶入
//當x=0(即是吊橋最左邊=fl)w(0)=fl
//1/2(dff*0^2)+a*0+b=fl所以a跟dff項為0所以fl=b
//所以原式改寫成
//w(x)=1/2(dff*x^2)+a*x+fl
//當x=10(即是吊橋最左邊=fr)w(10)=fr
//1/2(dff*w^2)+a*w+fl=fr
//求出a
//a*w=fr-fl-1/2(dff*w^2)
//a={fr-fl-1/2(dff*w^2)}/w
//搞定


Friday, June 09, 2006

test_Add_rca_4

感覺老師給的課堂作業比較難了,原本以為只是把課本的東西打一打,然後再加個top上去,就可以跑出來了,可是sum、c_out那邊都跑不出來。經由老師的開導,跟參考同學的程式碼,原來是要在內部加訊號就可,而不用加top送訊息進去,真是不經一事不長一智阿!
===================================================================
module test_Add_rca_4();
reg [3:0] a,b;
reg c_in;
wire [3:0] sum;
wire c_out;

initial
begin
a=1;
b=1;
c_in=1;
#40
$display ($time,,"sum=%b c_out=%b C_in4=%b c_in3=%b c_in2=%b c_in=%b",sum,c_out,M1.c_in4,M1.c_in3,M1.c_in2,c_in);
end

initial
begin
//stomulus patterns for data paths go here
end

Add_rca_4 M1 (sum,c_out,a,b,c_in); //module declaration
endmodule

module Add_rca_4(sum,c_out,a,b,c_in);
output [3:0] sum;
output c_out;
input [3:0] a,b;
input c_in;
wire c_out,c_in4,c_in3,c_in2;
Add_full G1(sum[0],c_in2,a[0],b[0],c_in);
Add_full G2(sum[1],c_in3,a[1],b[1],c_in2);
Add_full G3(sum[2],c_in4,a[2],b[2],c_in3);
Add_full G4(sum[3],c_out,a[3],b[3],c_in4);

endmodule
module Add_full(sum,c_out,a,b,c_in);
input a,b,c_in;
output c_out,sum;
wire w1,w2,w3;
half_adder m1(w1,w2,a,b);
half_adder m2(sum,w3,c_in,w1);
or (c_out,w2,w3);
endmodule

module half_adder(sum,c_out,a,b);
input a,b;
output sum,c_out;
wire c_out_bar;
xor (sum,a,b);
nand (c_out_bar,a,b);
not (c_out,c_out_bar);
endmodule

Friday, June 02, 2006

本次上課心得

這次上課,聽老師教的內容,讓我有種當頭棒喝的感覺,真是一語驚醒夢中人,有如打通任督二脈,一舉成為武林高手!原來reg跟wire的差別只需短短的幾句話就能詮釋,老師就是老師,行家一出手,果然不同凡響!

Friday, May 26, 2006

Nand_Latch simulation之心得

今天老師教的東西,有些不太懂,所給的作業(88頁的)有一個做不出來,完全照著課本打,但總覺得好像怪怪的,一直跑不出想要的圖形,改天自己一定要好好鑽研一下,使之充實自我,不白費光陰!

Nand_Latch simulation with delay

module test_Nand_Latch_1;
reg preset,clear;
wire q,qbar;
Nand_Latch_1 M1(q,qbar,preset,clear);

initial
begin
$monitor($time,"preset=%b clear=%b q=%b qbar=%b",preset,clear,q,qbar);
end

initial
begin
#10 preset=0; clear=1;
#10 preset=1;
#10 clear=0;
#10 clear=1;
#10 preset=0;
end
initial
#60 $finish;
endmodule
module Nand_Latch_1(q,qbar,preset,clear);
input preset,clear;
output q,qbar;
nand #1 g1(q,preset,qbar);
nand #1 g2(qbar,clear,q);
endmodule


Nand_Latch_1

module test_Nand_Latch_1;
reg preset,clear;
wire q,qbar;
Nand_Latch_1 M1(q,qbar,preset,clear);

initial
begin
$monitor($time,"preset=%b clear=%b q=%b qbar=%b",preset,clear,q,qbar);
end

initial
begin
#10 preset=0; clear=1;
#10 preset=1;
#10 clear=0;
#10 clear=1;
#10 preset=0;
end
initial
#60 $finish;
endmodule
module Nand_Latch_1(q,qbar,preset,clear);
input preset,clear;
output q,qbar;
nand g1(q,preset,qbar),
g2(qbar,clear,q);
endmodule


Friday, May 19, 2006

Data structures and event propagation delay

今天老師教delay,讓我覺得蠻實用的,很多的電路都會用當delay這個地方!在作的過程中,不知道要怎麼把訊息c顯示出來,後來想一想才知道用兩個副程式寫就好了!
==================================================================

module top;
reg a,b;
wire c,d;
event_driven m1(c,a,b);
no m1(d,c);
initial
begin
a=1;
b=0;
#2000 $finish;
end
always #30 a=~a;
always #20 b=~b;
endmodule

module event_driven(c,a,b);
input a,b;
output c;
and #3(c,a,b);
endmodule
module no(d,c);
input c;
output d;
not #2(d,c);
endmodule

Friday, May 12, 2006

2位元比較器之compare_2_algo

module top;
reg A,B;
wire A_lt_B,A_gt_B,A_eq_B;
compare_2_algo m1(A_lt_B,A_gt_B,A_eq_B,A,B);
initial
begin
A=0;
B=0;
#2000 $finish;
end
always #100 A=~A;
always #50 B=~B;
endmodule

module compare_2_algo(A_lt_B,A_gt_B,A_eq_B,A,B);
input [1:0] A,B;
output A_lt_B,A_gt_B,A_eq_B;
reg A_lt_B,A_gt_B,A_eq_B;

always @ (A or B)
begin
A_lt_B=0;
A_gt_B=0;
A_eq_B=0;
if(A==B)A_eq_B=1;
else if (A>B)A_gt_B=1;
else A_lt_B=1;
end
endmodule


2位元比較器之心得

這次上課,因為有分工合作的關係,一人打一個程式,然後再自己輸入訊號,感覺蠻有效率的,希望下次上課時,也都能像這次上課一樣順利!

2位元比較器之compare_2b

module top;
reg A1,A0,B1,B0;
wire A_lt_B,A_gt_B,A_eq_B;
compare_2b m1(A_lt_B,A_gt_B,A_eq_B,A1,A0,B1,B0);
initial
begin
A1=0;
A0=0;
B1=0;
B0=0;

#2000 $finish;
end
always
#30 A1=~A1;
always
#40 A0=~A0;
always
#50 B1=~B1;
always
#60 B0=~B0;

endmodule

module compare_2b(A_lt_B,A_gt_B,A_eq_B,A1,A0,B1,B0);
input A1,A0,B1,B0;
output A_lt_B,A_gt_B,A_eq_B;

assign A_lt_B=({A1,A0}<{B1,B0}); assign A_gt_B=({A1,A0}>{B1,B0});
assign A_eq_B=({A1,A0}=={B1,B0});
endmodule

2位元比較器之compare_2a

module top;
reg A1,A0,B1,B0;
wire A_lt_B,A_gt_B,A_eq_B;
compare_2a m1(A_lt_B,A_gt_B,A_eq_B,A1,A0,B1,B0);
initial
begin
A1=0;
A0=0;
B1=0;
B0=0;

#2000 $finish;
end
always
#30 A1=~A1;
always
#40 A0=~A0;
always
#50 B1=~B1;
always
#60 B0=~B0;

endmodule


module compare_2a(A_lt_B,A_gt_B,A_eq_B,A1,A0,B1,B0);
input A1,A0,B1,B0;
output A_lt_B,A_gt_B,A_eq_B;
assign A_lt_B=(~A1)&B1(~A1)&amp;amp;(~A0)&B0(~A0)&B1&B0;
assign A_gt_B=A1&(~B1)A0&amp;amp;(~B1)&(~B0)A1&A0&(~B0);
assign A_eq_B=(~A1)&(~A0)&amp;amp;(~B1)&(~B0)(~A1)&A0&(~B1)&B0A1&A0&B1&B0A1&(~A0)&B1&(~B0);
endmodule


2位元比較器之compare_2_str

module top;
reg A1,A0,B1,B0;
wire A_lt_B,A_gt_B,A_eq_B;
compare_2_str m1(A_lt_B,A_gt_B,A_eq_B,A1,A0,B1,B0);
initial
begin
A1=0;
A0=0;
B1=0;
B0=0;

#2000 $finish;
end
always
#30 A1=~A1;
always
#40 A0=~A0;
always
#50 B1=~B1;
always
#60 B0=~B0;

endmodule

module compare_2_str(A_lt_B,A_gt_B,A_eq_B,A0,A1,B0,B1);
input A0,A1,B0,B1;
output A_lt_B,A_gt_B,A_eq_B;
wire w1,w2,w3,w4,w5,w6,w7;

or (A_lt_B,w1,w2,w3);
nor (A_gt_B,A_lt_B,A_eq_B);
and (A_eq_B,w4,w5);
and (w1,w6,B1);
and (w2,w6,w7,B0);
and (w3,w7,B0,B1);
not (w6,A1);
not (w7,A0);
xnor (w4,A1,B0);
xnor (w5,A0,B0);
endmodule


Friday, May 05, 2006

16-bit ripple-carry adder

感覺這次教的比較難,用了好久才跑出來,若不是老師的教導,我想我應該也做不出來吧,難度提高的同時,當寫出來的成就感,是無法用言語形容的!
==================================================================

module top;
reg [15:0] a,b;
reg c_in;
wire [15:0] sum;
wire c_out;
Add_rca_16 m1(sum,c_out,a,b,c_in);
initial
begin
a=0;
b=0;
c_in=0;

#2000 $finish;
end
always
#100 a[0]=~a[0];
always
#150 b[1]=~b[1];
always
#200 c_in=~c_in;

endmodule

module Add_rca_16 (sum,c_out,a,b,c_in);
output [15:0] sum;
output c_out;
input [15:0] a,b;
input c_in;
wire c_in,c_in4,c_in8,c_in12,c_out;
Add_rca_4 M1(sum[3:0], c_in4, a[3:0], b[3:0], c_in);
Add_rca_4 M2(sum[7:4], c_in8, a[7:4], b[7:4], c_in4);
Add_rca_4 M3(sum[11:8], c_in12, a[11:8], b[11:8], c_in8);
Add_rca_4 M4(sum[15:12], c_out, a[15:12], b[15:12], c_in12);
endmodule

module Add_rca_4(sum,c_out,a,b,c_in);
output [3:0] sum;
output c_out;
input [3:0] a,b;
input c_in;
wire c_in4,c_in3,c_in2;
Add_full G1 (sum[0],c_in2,a[0],b[0],c_in);
Add_full G2 (sum[1],c_in3,a[1],b[1],c_in2);
Add_full G3 (sum[2],c_in4,a[2],b[2],c_in3);
Add_full G4 (sum[3],c_out,a[3],b[3],c_in4);
endmodule

module Add_full(s,c_out,a,b,c_in);
output s,c_out;
input a,b,c_in;
wire s1,c1,c2,c_out_bar;
Add_half G1(s1,c1,a,b);
Add_half G2(s,c2,s1,c_in);
norf201 G3(c_out_bar,c1,c2);
invf101 G4(c_out,c_out_bar);
endmodule

module Add_half(s,c,a,b);
output s,c;
input a,b;
wire c_bar;
xorf201 G1(s,a,b);
nanf201 G2(c_bar,a,b);
invf101 G3(c,c_bar);
endmodule

module xorf201(O,A1,B1);
input A1,B1;
output O;
xor(O,A1,B1);
specify
specparam
Tpd_0_1=1.13:3.09:7.75,
Tpd_1_0=0.93:2.50:7.34;
(A1=>O)=(Tpd_0_1,Tpd_1_0);
(B1=>O)=(Tpd_0_1,Tpd_1_0);
endspecify
endmodule

module nanf201(O,A1,B1);
input A1,B1;
output O;
nand(O,A1,B1);
specify
specparam
Tpd_0_1=1.13:3.09:7.75,
Tpd_1_0=0.93:2.50:7.34;
(A1=>O)=(Tpd_0_1,Tpd_1_0);
(B1=>O)=(Tpd_0_1,Tpd_1_0);
endspecify
endmodule

module invf101(O,A1);
input A1;
output O;
not(O,A1);
specify
specparam
Tpd_0_1=1.53:4.09:6.75,
Tpd_1_0=0.53:4.50:6.34;
(A1=>O)=(Tpd_0_1,Tpd_1_0);
endspecify
endmodule

module norf201(O,A1,B1);
input A1,B1;
output O;
nor(O,A1,B1);
specify
specparam
Tpd_0_1=1.13:3.09:7.75,
Tpd_1_0=0.93:2.50:7.34;
(A1=>O)=(Tpd_0_1,Tpd_1_0);
(B1=>O)=(Tpd_0_1,Tpd_1_0);
endspecify
endmodule




Friday, April 28, 2006

Add_half_structural

這禮拜老師教的,讓我受益很多,對於verilog的這門課,開始有了更濃厚的興趣,使用程式上,也有著很大的進步。寫verilog時,一定要按步驟走,先把基礎打好,這樣以後才能更靈活的運用它!
==================================================================

module top;
reg a,b;
wire sum,c_out;
Add_half_structural m1(sum,c_out,a,b);
initial
begin
a=0;
b=0;
#2000 $finish;
end
always
#100 a=~a;
always
#50 b=~b;
endmodule

module Add_half_structural(sum,c_out,a,b);
input a,b;
output sum,c_out;
wire c_out_bar;
xorf201 G1(sum,a,b);
nandf201 G2(c_out_bar, a,b);
invf101 G3(c_out,c_out_bar);
endmodule

module nandf201(O,A1,B1);
input A1,B1;
output O;
nand(O,A1,B1);
specify
specparam
Tpd_0_1=1.13:3.09:7.75,
Tpd_1_0=0.93:2.50:7.34;
(A1=>O)=(Tpd_0_1,Tpd_1_0);
(B1=>O)=(Tpd_0_1,Tpd_1_0);
endspecify
endmodule

module xorf201(O,A1,B1);
input A1,B1;
output O;
xor(O,A1,B1);
specify
specparam
Tpd_0_1=1.13:3.09:7.75,
Tpd_1_0=0.93:2.50:7.34;
(A1=>O)=(Tpd_0_1,Tpd_1_0);
(B1=>O)=(Tpd_0_1,Tpd_1_0);
endspecify
endmodule

module invf101(O,A1);
input A1;
output O;
not(O,A1);
specify
specparam
Tpd_0_1=1.53:4.09:6.75,
Tpd_1_0=0.53:4.50:6.34;
(A1=>O)=(Tpd_0_1,Tpd_1_0);
endspecify
endmodule



Friday, April 14, 2006

全加法器之模擬

這次上課,讓我對verilog有些陌生,單單兩個禮拜沒碰,就令我產生如此生疏,所以,要認真學一樣東西,一定要經驗去接觸它,幸好我這次只有兩個禮拜,還能找回感覺,於是,我覺得應該在我有空的時候,要多多模擬一下。
===================================================================


module top;
reg a,b,c_in;
wire sum,c_out;
all_full m1(sum,c_out,a,b,c_in);
initial
begin
a=0;
b=0;
c_in=0;
#2000 $finish;
end
always#10
a=~a;
always#20
b=~b;
always#40
c_in=~c_in;
endmodule

module all_full(sum,c_out,a,b,c_in);
input a,b,c_in;
output c_out,sum;
wire w1,w2,w3;
half_adder m1(w1,w2,a,b);
half_adder m2(sum,w3,c_in,w1);
or (c_out,w2,w3);
endmodule

module half_adder(sum,c_out,a,b);
input a,b;
output sum,c_out;
wire c_out_bar;
xor (sum,a,b);
nand (c_out_bar,a,b);
not (c_out,c_out_bar);
endmodule


Friday, March 31, 2006

C++之吊橋參考

經過老師的這次講解,讓我之前學的C++語言有更深入的了解,也使我對期末的報告有新的一番體悟,吊橋的原理其實不會太難,只要認真的去思想,很多難題都可以迎刃而解,勢如破竹!
其實最大的功勞,是老師教的太好了,簡直是學術界的教學楷模!帥啦
===============================================
http://www.isr.umd.edu/~austin/book.d/code.d/c.d/prog_cable.c

Friday, March 24, 2006

Flip_flop

這週老師所給予的作業,依究不太清楚,還是參考上禮拜而做出來的,不過相信,到後面會越來越熟悉這門課。
=========================================================
module top;
reg data_in,clk,rst;
wire q;
Flip_flop m1(q,data_in,clk,rst);
initial
begin
data_in=0;
clk=0;
rst=0;
#2000 $finish;
end
always#20
data_in=~data_in;
always#30
clk=~clk;
always#100
rst=~rst;
endmodule
module Flip_flop(q,data_in,clk,rst);
input data_in,clk,rst;
output q;
reg q;
always @ (posedge clk)
begin
if (rst==1) q=0;
else q=data_in;
endendmodule



Friday, March 17, 2006

延用上週的課程加入半加法器

本次上課,我對verilog這門課,有進一步的了解,但很多基本的語言,都不能夠理解,大多都是參考別人而作出來的。所以有空我打算去借一本中文書,把基礎打好,使自己能更為融入這門學問。

==============================


module top;
reg a,b;
wire sum,c_out;
half_adder m1(c_out,sum,a,b);
initial
begin
a=0;
b=0;
#2000 $finish;
end
always
#50 a=~a;
always
#100 b=~b;
endmodule

module half_adder(c_out,sum,a,b);
input a,b;
output sum,c_out;
wire c_out_bar;
xor (sum,a,b);
nand (c_out_bar,a,b);
not (c_out,c_out_bar);
endmodule



用and取代assign

module top;
reg a,b;
wire c;
and(c,a,b); // 原本為 assign c=a&b;
initial
begin
a=0;
b=0;
#2000 $finish;
end
always
#50 a=~a;
always
#100 b=~b;
endmodule

Friday, March 10, 2006

雙CPU設計的測試

單CPU設計的測試

課堂初試

Friday, March 03, 2006

網路的範例

Verilog

Verilog 是目前最普遍與廣泛使用的硬體模述語言,可說是硬體設計中的C語言。Verilog語法分成可合成與不可合成兩部份。Verilog是數位IC設計之基本功之一,需要具備基本邏輯設計概念,藉由學習Verilog,可幫助自己打穩IC設計的基礎。