數位邏輯

Download Report

Transcript 數位邏輯

數位邏輯設計與實習
Ch07 Verilog語法
CAD –電腦輔助設計




數百萬個電晶體與邏輯閘
支援電路之電腦基本陳述的軟體程式
利用自動化設計程序輔助數位硬體電路的開發
設計入門
 電路圖抓取 (schematic capture) 或電路圖繪製
(schematic entry)
 硬體描述語言 (hardware description language,
HDL) Verilog, VHDL
 模擬
 實體實現
 ASIC, FPGA, PLD
硬體描述語言(HDL)
 一種以計算機為基礎而用文字的形式來描述
數位系統硬體電路的語言:
 硬體結構
 功能/行為
 時序
 VHDL 與 Verilog HDL
A Top-Down Design Flow
簡介





Verilog HDL 由 Gateway 公司所提出。
用來描述硬體設計,從硬體的概念出發。
語法與 C 語言類似,容易學習。
RTL( Register Transfer Language)
Verilog HDL: IEEE Standard (IEEE
1364 - 1995 ~ IEEE 1364 - 2001)
識別字(Identifiers)
 在 Verilog 電路描述中,識別字可用於定義
變數名稱、函數名稱、模組名稱與物件實例
(instance) 名稱。
 識別字的命名規則:
 第一個字元必須是英文字母。
 第二個之後的字元可以是英文字母、數字、底
線 ( _ ) 或是錢字號 ($)。
 識別字的長度沒有限制。
 識別字有區分英文大小寫。
關鍵字(Keywords)


關鍵字是用來描述Verilog的電路架構。
Ex
 input adder_in; //”input”是關鍵字, ”adder_in”
是識別字
 wire adder_out; //”wire”是關鍵字,”adder_out”
是識別字
 所有的關鍵字必須使用英文小寫字母來表示。
 常見的關鍵字有
always negedge
posedge
begin
end
assign
wire
integer
function
endfunction module
endmodule for
if
else
inout
input output
and
buf
nand nor
not
or
xnor
xor
註解(Comments)
 單行註解
 使用「//」作為開始符號。
 結束符號為換行符號 (end_of_line)。
 多行註解
 使用「/*」作為開始符號。
 使用「*/」作為結束符號。
接線(Nets)
 wire a;
//宣告有一條接線叫做a
 wire [15:0] data_bus;
// 宣告data_bus為16 bit的連接線
暫存器(registers)
 reg R;
//宣告一個變數R為暫存器
 reg [7:0] r0; //宣告一個寬度為8位元
的r0暫存器
 有記憶功能的線
數字(number)
 integer
 real
 time
integer count;
real avg;
count = 0;
avg =1.23;
參數
 parameter value1=9;
 parameter wordsize=16;
reg [wordsize-1:0] data_bus;
reg [15:0] data_bus; //同上
陣列與記憶體





<資料型態><長度><變數名><陣列大小>
reg datareg[7:0];
integer [7:0] outint[15:0];
reg[7:0] mem256[255:0];
reg[15:0] mem_1024[1023:0];
三態
 inout[3:0] dbus;
module tribuf(dbus,enable,value1);
inout[3:0] dbus;
input enable;
input[3:0] value1;
assign
dbus =(enable==1) ? value1 : 4’bz;
endmodule
數字表示規格
 一樣的數值以不同的進制表示
 8'b10100101;
示
 8'ha5;
六進位表示
 8'd165;
表示
 8'o245;
//binary 8位元的二進位表
//hexadecimal 8位元的十
//decimal 8位元的十進位
//octal 8位元的八進位表示
負數
 -8’d3;
 4’d-2;
//8bit以二補數法表示(-3)
//錯誤的寫法
數值
 12’h13z; //一個z在十六進制代表四位
元的高阻抗
 12’h12x; //一個x在十六進制代表四位
元的不確定值
 8’b0011_1010; //加上”_”可以方便
閱讀,並不會影響數值
 //但第一位元不能加低線
運算元(Operators)
 單元(Unary)運算子:放在運算元前面。
ex. assign a=~b;
//"~",1位元反向。
 二元(Binary)運算元:放在兩個運算元之間。
ex. assign a=b&c; //"&",1位元寬度的AND動
作。
 三元(Ternary)運算子:條件運算子
ex. assign out=select ? a : b;
//當select為1(true)時,out=a;
//當select為0(false)時,out=b;
運算子種類
運算子優先順序
模組結構
模組的基本架構
module test(A, B, C, D, E);
outputD, E;
input A, B, C;
wire w1;
reg
r;
...............
..............
endmodule
模組的基本架構new
module test(output D, E, input A,B,C);
wire w1;
reg
r;
...............
..............
endmodule
注意:分號有無
對映方式
 by order
 by name
module dff(din,clock,q)
input din,clock;
output q;
reg q;
always@(posedge clock)
q = din;
endmodule
2_1多工器
module mux2_1(ma,mb,s,mout)
input ma,mb,s;
output mout;
assign mout = s? ma:mb;
endmodule
正反器輸出選擇—by order
module dff_sef(da,db,sel,clk,q)
input da,db,sel,clk;
output q;
wire qa,qb;
dff dff1(da,clk,qa);
dff dff2(db,clk,qb);
mux2_1 mux(qa,qb,sel,q)
endmodule
正反器輸出選擇—by name
module dff_sef(da,db,sel,clk,q)
input da,db,sel,clk;
output q;
wire qa , qb;
dff dff1(.din(da),.clock(clk),.q(qa));
dff dff2(.din(db),.clock(clk),.q(qb));
mux2_1 mux(qa,qb,sel,q)
endmodule
模組範例1
module half_adder (output S, C, input x, y);
xor (S, x, y);
and (C, x, y);
endmodule
module full_adder (output S, C, input x, y, z);
wire S1, C1, C2;
half_adder
HA1 (S1, C1, x, y);
half_adder
HA2 (S, C2, S1, z);
or
G1 (C, C2, C1);
endmodule
module ripple_carry_4_bit_adder ( output [3: 0] Sum, output C4,
input [3:0] A, B, input C0);
wire
C1, C2, C3;
full_adder
endmodule
,
FA1 (Sum[1], C2, A[1], B[1], C1) ,
FA2 (Sum[2], C3, A[2], B[2], C2) ,
FA3 (Sum[3], C4, A[3], B[3], C3) ;
FA0 (Sum[0], C1, A[0], B[0], C0)
模組範例2
module half_adder (output S, C, input x, y);
xor (S, x, y);
and (C, x, y);
endmodule
module full_adder (output S, C, input x, y, z);
wire S1, C1, C2;
half_adder HA1 (S1, C1, x, y);
half_adder HA2 (S, C2, S1, z);
or
G1 (C, C2, C1);
endmodule
module ripple_carry_4_bit_adder ( output [3: 0] Sum, output C4,
input [3:0] A, B, input C0);
wire
C1, C2, C3;
full_adder
full_adder
full_adder
full_adder
endmodule
;
FA1 (Sum[1], C2, A[1], B[1], C1) ;
FA2 (Sum[2], C3, A[2], B[2], C2) ;
FA3 (Sum[3], C4, A[3], B[3], C3) ;
FA0 (Sum[0], C1, A[0], B[0], C0)
四種描述層次
 Switch level
最低層次,設計者需知道電晶體的元件特性。
 Gate level
模組是由Logic gates所構成的,使用原始閘以及
使用者定義模組的實例(instantiation)。
 Dataflow level
說明資料如何在暫存器中儲存和傳送,和資料處理
的方式。使用具有關鍵字assign之連續指定敘述。
 Behavioral level
只考慮模組中的功能和函數,不必考慮硬體方面的
詳細電路,如同是在寫C語言一樣。使用具有關鍵
字always之程序指定敘述。
Gate level
 and/nand/or/nor/xor/xnor
 buffer、not
 bufif1, bufif0, notif1, notif0
基本邏輯閘 1
 and/nand/or/nor/xor/xnor
nand g1(out,in1,in2,in3) ;//別名
and (out,in1,in2) ;
//沒有別名
第一腳輸出,其餘當輸入
基本邏輯閘 2
 buffer、not 閘的使用
buf b1(out, in) ;
not n1(out, in) ;
 bufif1, bufif0, notif1, notif0
bufif1 (outb, inb, ctrlb)
notif0 element0(outn, inn,ctrln)
三態閘
•gate name (output, input, control);
• bufif1, bufif0, notif1, notif0
bufif1 (outb, inb, ctrlb)
notif0 element0(outn, inn,ctrln)
簡單範例
module Simple_Circuit (A, B, C, D, E);
output
D, E;
input
A, B, C;
wire
w1;
and
not
or
endmodule
G1 (w1, A, B);
G2 (E, C);
G3 (D, w1, E);
// instance name可有可無
練習
上機模擬練習7-1
閘延遲
 timescale directive (編譯指引)
 ‘timescale 1 ns/100ps(時間單位/解析度)
 例:
‘timescale 10ns/1ns
nand #10 g1(f,x,y);
10*10=100ns 則此nand 閘傳播延遲
100ns
閘延遲
module Simple_Circuit_prop_delay (A, B,
C, D, E);
output
D, E;
input
A, B, C;
wire
w1;
and
#(30) G1 (w1, A, B);
not
#(10) G2 (E, C);
or
#(20) G3 (D, w1, E);
endmodule
閘延遲範例
‘timescale 1 ns/1ns
module prog_ex(x, y, z, f);
input x, y, z;
output f;
// internal declaration
wire a, b, c;
// internal net
// logic circuit body
and #5 a1 (b,x,y); //AND p_delay
not #10 n1 (a,x);
and #5 a2 (c,a,z);
or #5 o2 (f,b,c);
endmodule
上機模擬練習7-2
測試平台
`timescale 1 ns / 100 ps
module t_Simple_Circuit_prop_delay;
wire D, E;
reg
A, B, C;
Simple_Circuit_prop_delay M1 (A, B, C, D, E); // Instance name
required
initial
begin
A = 1'bx; B = 1'bx; C = 1'bx;
A = 1'b0; B = 1'b0; C = 1'b0;
#100 A = 1'b1; B = 1'b1; C = 1'b1;
#100 $finish;
end
initial $monitor($time,,"A = 5b B= %b C = %b w1 = %b D = %b
E = %b", A, B, C, D, M1.w1, E);
endmodule
2對4高態輸出解碼器
`timescale 1ns / 1ps
module decoder2_4(
input a,
//a MSB
input b,
output [3:0] y
);
wire an,bn;
not(an,a);
not(bn,b);
and(y[0],an,bn);
and(y[1],an,b);
and(y[2],a,bn);
and(y[3],a,b);
endmodule
1 bit 比較器
`timescale 1ns / 1ps
module comparator_1bit(
input a,
input b,
output eq,
output gt,
output lt
);
wire an,bn,a0b0,a1b1;
not(an,a);
not(bn,b);
and(a1b1,a,b);
nor(a0b0,a,b);
or(eq,a0b0,a1b1);
and(gt,a,bn);
and(lt,an,b);
endmodule
//NOR gate
2 bit 比較器方塊圖
2 bit 比較器
`timescale 1ns / 1ps
module comparator_2bit( input [1:0] a,
eq2, output gt2, output lt2 );
wire equal_1,large_1,small_1;
wire equal_2,large_2,small_2;
wire gla,lla;
input [1:0] b,
output
and( gla, large_1, equal_2 );
and( lla, small_1, equal_2 );
comparator_1bit
element1(.eq(equal_1), .gt(large_1), .lt(small_1), .a(a[0]), .b(
b[0]));
comparator_1bit
element2(.eq(equal_2), .gt(large_2), .lt(small_2), .a(a[1]), .b(
b[1]));
and( eq2, equal_1, equal_2 );
or( gt2, gla, large_2 );
or( lt2, lla, small_2 );
endmodule
上機模擬練習7-3
 多模組編輯
上機模擬練習7-4
 一程式有許多模組(不建議此方式)
上機模擬練習7-5
 HDL+電路圖方式
 利用HDL製作基本元件
 主程式以繪圖方式達成系統功能
2對1多工器
`timescale 1ns / 1ps
module mux_2_1_gate(
input a,
input b,
input s,
output y
);
wire s0,sa,sb;
not( s0, s );
and( sa, a, s0 );
and( sb, b, s );
or( y, sa, sb );
endmodule
4對1多工器-1
`timescale 1ns / 1ps
module mux_4_1_gate(
input [3:0] i,
input [1:0] s,
output f
);
wire f0,f1;
// by inoder mapping
mux_2_1_gate element1( i[0], i[1], s[0], f0 );
mux_2_1_gate element2( i[2], i[3], s[0], f1 );
mux_2_1_gate element3( f0, f1, s[1], f );
endmodule
4對1多工器-2
 改成 by name mapping
 mux_2_1_gate element1( i[0] , i[1], s[0], f0 );
mux_2_1_gate
element1(.a(i[0] ),.b(i[1]),.s(s[0]),.y(f0));
4對1多工器-3
 以bufif0,bufif1
module mux4_1b_g(y, s, i);
output y; //output y
input [1:0] s;
input [3:0] i;
wire y0, y1;
// Instantiates buffers
bufif0 (y0, i[0], s[0]);
bufif1 (y0, i[1], s[0]);
bufif0 (y1, i[2], s[0]);
bufif1 (y1, i[3], s[0]);
bufif0 (y, y0, s[1]);
bufif1 (y, y1, s[1]);
endmodule
具有enable2對4低態輸出解碼器
半加器設計
全加器設計
3 bit 多數表決電路設計
UDP範例(使用者定義的基本閘)
使用者定義的基本閘
 使用關鍵字primitive作宣告,後面接著是名稱以
及埠名單。
 只可以有一個輸出,且此輸出必須列在埠名單的第
一個,還有必須用關鍵字output來宣告。
 輸入數目不限制,至於它們在input宣告中的順序
則必須與它們在下面表中所給值的順序相同。
 真值表必須在關鍵字table及endtable之間。
 輸入值依順序列出,用冒號 (:) 代表結束。輸出
通常是每一列的最後一個記錄,後面跟著是一個分
號 (;)。
 最後用關鍵字endprimitive做為UDP宣告的結束。
User-defined Primitive
primitive my_UDP (D, A, B, C);
output D;
input A, B, C;
// Truth table for D = f (A, B, C) = m(0, 2, 4, 6, 7);
table
// A B
C
:
D
// Column header comment
0 0
0
:
1;
0 0
1
:
0;
0 1
0
:
1;
0 1
1
:
0;
1 0
0
:
1;
1 0
1
:
0;
1 1
0
:
1;
1 1
1
:
1;
endtable
endprimitive
UDP範例程式
module Circuit_with_UDP(e, f, a, b, c, d);
output
e, f;
input
a, b, c, d;
my_UDP
and
endmodule
M0 (e, a, b, c);
(f, e, d);
//省略 gate instance name
上機模擬練習7-6
Gate Level Design練習







以Gate Level Design方式設計,4對1多工器,並附模
擬圖
以Gate Level Design方式設計,利用2對1多工器,設
計4對1多工器,用By name mapping,並附模擬圖
利用2對1多工器做成元件,以HDL及電路圖方式,設計4
對1多工器,並附模擬圖
以Gate Level Design方式設計,具有enable2對4低
態輸出解碼器,並附模擬圖
以Gate Level Design方式設計,半加器設計,並附模
擬圖
以Gate Level Design方式設計,全加器設計,並附模
擬圖
以Gate Level Design方式設計,3 bit 多數表決電路
設計,並附模擬圖