Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch01-2 Verilog語法 資料流(DataFlow)設計 行為(Behavior)設計

Similar presentations


Presentation on theme: "Ch01-2 Verilog語法 資料流(DataFlow)設計 行為(Behavior)設計"— Presentation transcript:

1 Ch01-2 Verilog語法 資料流(DataFlow)設計 行為(Behavior)設計
數位積體電路雛型製作 Ch01-2 Verilog語法 資料流(DataFlow)設計 行為(Behavior)設計

2 資料流設計(Dataflow level)
說明資料如何在暫存器中儲存和傳送,和資料處理的方式。使用具有關鍵字assign之連續指定敘述。

3 持續指定(continuous assignment)
在資料處理層次中對於一條線指定給其值的描述。 當其輸出是經過邏輯線路時,使用資料處理可以較為簡潔。 電路的設計重點在於說明資料如何在電路中的傳送過程。 ex. wire out; assign out = i n1 & in2; //正規式的持續指定 wire out = in1 & in2 ; //隱藏式的持續指定,與上面同效果

4 延遲(delay) wire out; assign #10 out = in1 & in2;

5 布林表示式1 assign D=(A&B)|(~C) assign E= ~C

6 布林表示式 module test( E, F, A, B, C, D); output E, F; input A, B, C, D;
assign E = A | (B & C) | (~B & D); assign F = (~B & C) | (B & ~C & ~D); endmodule

7 1 bit 半加器 module half_add(s, cout, a, b);
output s, cout; // sum and carry out input a, b; assign s = a^ b; assign cout = a & b; endmodule

8 全加器設計 module full_adder_df (Cin, A, B, S, Cout); input Cin, A, B;
output S, Cout; assign S = A ^ B ^ Cin; assign Cout = A & B | A & Cin | B & Cin; endmodule

9 8 bit 偶同位/全零檢查電路 利用精簡運算子
module even_parity(ev_parity, all_zeros, din); output ev_parity, all_zeros; input [7:0] din; assign ev_parity = ~^ din; // ev_parity = din[0]⊙din[1]⊙din[2]⊙din[3]⊙din[4]⊙din[5]⊙din[6]⊙din[7] XNOR assign all_zeros = ~| din; // all_zeros = NOT(din[0] + din[1] + din[2] + din[3] + din[4] + din[5] + din[6] + din[7] ) NOR endmodule

10 除8電路 利用移位運算子 module divid_8(quot, div); output [7:0] quot; //quotient
input [7:0] div; //dividend parameter sh_bit = 3; //define the number of bits for shifting assign quot = div >> sh_bit; // >>右移 endmodule

11 2對1多工器 module mux2x1_df (I0,I1,S,Y); input I0,I1; input S; output Y;
// 2-to-1 MUX body assign Y = ~S ? I0 : I1; endmodule

12 4 bit 2對1多工器 利用條件運算子 module mul2_1_4bits(y, sel, a, b);
output [3:0] y; input [3:0] a, b; // 4-bit input data input sel; // selection line assign y = (sel) ? a : b; //條件運算子 endmodule

13 1對4解多工器 module demux_1_4( input i, input [1:0] s, output [3:0] f );
assign f[0] = i&(~s[1])&(~s[0]); assign f[1] = i&(~s[1])&( s[0]); assign f[2] = i&( s[1])&(~ s[0]); assign f[3] = i&( s[1])&( s[0]); endmodule

14 3 bit 多數表決電路 `timescale 1ns / 1ps module majority_3_data(
input [2:0] i, output f ); assign f = ( i[1] & i[0] ) +( i[2] & ( i[0] | i[1] ) ); //卡諾圖化簡後結果 endmodule

15 4 bit 多數表決電路

16 有enable的2對4解碼器 module decoder_2_to_4 (x, E, Y); input [1:0] x;
input E; output [3:0] Y; assign Y[0] = ~E & ~x[1] & ~x[0]; assign Y[1] = ~E & ~x[1] & x[0]; assign Y[2] = ~E & x[1] & ~x[0]; assign Y[3] = ~E & x[1] & x[0]; endmodule

17 3對8低態輸出解碼器 module decoder3_8( input [2:0] d, output [7:0] y );
assign y[0] = ~((~d[2]) & (~d[1]) & (~d[0])) , y[1] = ~((~d[2]) & (~d[1]) & (d[0])) , y[2] = ~((~d[2]) & (d[1]) & (~d[0])) , y[3] = ~((~d[2]) & (d[1]) & (d[0])) , y[4] = ~((d[2]) & (~d[1]) & (~d[0])) , y[5] = ~((d[2]) & (~d[1]) & (d[0])) , y[6] = ~((d[2]) & (d[1]) & (~d[0])) , y[7] = ~((d[2]) & (d[1]) & (d[0])) ; endmodule

18 4對2高態輸出編碼器 module encoder4_2_data( input i3, input i2, input i1,
output [1:0] y ); // assign y[1] = ((((~i3) & i2) | (i3 & (~i2))) & (~i1) & (~i0)), // y[0] = ( (~i2)&(~i0)&( ((~i3) & i1) |( i3 & (~i1)) ) ); assign y[1] = ((((~i3) & i2) | (i3 & (~i2))) & (~i1) & (~i0)); assign y[0] = ( (~i2)&(~i0)&( ((~i3) & i1) |( i3 & (~i1)) ) ); endmodule

19 4 bit加法器 module adder_4bit( input [3:0] a, input [3:0] b, input cin,
output [3:0] sum, output cout ); assign { cout,sum } = a + b + cin; endmodule

20 1 bit 全減器 module subtractor_1bit( input x, input y, input bin,
output dif, output bout ); assign dif = ((~x)&(~y)& bin) |( (~x)&(y)&(~bin)) |(x&(~y)&(~bin))|( x & y & bin); assign bout = ((~x)&y)|((~x)& bin)|(y & bin); endmodule

21 4 bit 比較器 module comparator_4bit (A, B, IALTB, IAEQB, IAGTB, OAGTB, OAEQB, OALTB); input [3:0] A; input [3:0] B; input IALTB,IAEQB,IAGTB; output OAGTB,OAEQB,OALTB ; // -- comparator body-- // assign OALTB = (A < B)||(A==B)&& IALTB; assign OAGTB = (A > B)||(A==B)&& IAGTB; assign OAEQB = (A==B)&& IAEQB; endmodule

22 8bit偶同位產生器 module even_parity_generator(x,f); input [7:0] x; output f;
assign f = ^x; // compute the even parity of x endmodule

23 行為描述(Behavioral level )
只考慮模組中的功能和函數,不必考慮硬體方面的詳細電路。使用具有關鍵字always之程序指定敘述。

24 結構化程序 在Verilog中”initial”和”always”是行為模型中最基本的描述,Verilog是一並行程式語言,須用硬體的角度來思考所需的設計。 “initial”:一個initial的區塊開始的時間為時間零(t=0),當區塊中有很多的敘述時,需用begin…end上下包起來。Initial的動作是不可合成的指令,使用於模擬測試自己的設計是否正確。 “always”:用來描述重複工作的數位邏輯電路

25 程序指定(Procedural assignments)
程序指定用來更新暫存器(reg)的值,變數上被指定的值會維持到新的程序指定更新為止。 限定指定: (blocking assignment ) 依照在循序區塊的位置,依序執行。一般用於模擬測試(testbench)中。使用符號為“=”。 無限定指定:(nonblocking assignment): 不受敘述位置的影響,用在設計當中較能符合設計的要求,使用符號為”<=”。

26 事件基礎時間控制 一個訊號產生觸發區塊開始動作,以達到其設計功能。 @(clock) q=d; @(posedge clock) q=d;
//clock正緣觸發,當clock訊號變化為1的時,q=d被執行。 @(negedge clock) q=d; //clock負緣觸發,當clock訊號變化為0的時,q=d被執行。 clock) d; //d立刻被執行,等到clock正緣觸發時再指定至q。

27 if (條件敘述) a. if (xxx) … else …. b. if (xxx)… else if (xxx) … else…

28 case(多路徑分支) case (判斷訊號名稱) 條件1:…..//條件1成立時,所要做的動作
條件2:.….//條件2成立時,所要做的動作 ….. default:…. //最後最好加上 。 endcase

29 4 bit 暫存器 區塊程序指定 ( blocking procedure assignment )
module reg(clk,reset,din,qout); input clk,reset; input din; output [3:0] qout; reg [3:0] qout; clk or posedge reset) if ( reset ) qout = 4'b0000; else begin qout[0] = din; qout[1] = qout[0]; qout[2] = qout[1]; qout[3] = qout[2]; end endmodule

30 4 bit 暫存器 非區塊程序指定 ( non_blocking procedure assignment )
module reg_bpa(clk,reset,din,qout); input clk,reset; input din; output [3:0] qout; reg [3:0] qout; clk or posedge reset) if ( reset ) qout <= 4'b0000; else begin qout[0] <= din; qout[1] <= qout[0]; qout[2] <= qout[1]; qout[3] <= qout[2]; end endmodule

31 2對1多工器 module mux2x1_bh (I0,I1,S,Y); input I0,I1; input S; output Y;
reg Y; or I1 or S) if (S == 0) Y = I0; else Y = I1; endmodule

32 4 bit 2對1多工器 module mux2_1_4bit(a,b,s,f); input [3:0] a;
input [3:0] b; input s; output [3:0] f; reg [3:0] f; s or a or b ) begin if ( s ) f = b; else f = a; end endmodule

33 4對1多工器(if) module mux4_1_bhv(i,s,f); input [3:0] i; input [1:0] s;
output f; reg f; s, i ) begin if ( s == 2'b00 ) f = i[0]; else if ( s == 2'b01 ) f = i[1]; else if (s == 2'b10 ) f = i[2]; else f = i[3]; end endmodule

34 4對1多工器(case) module mux_4_1_bhv_case(i,s,f); input [3:0] i;
input [1:0] s; output f; reg f; i or s ) begin case ( s ) 2'b00 : f = i[0]; 2'b01 : f = i[1]; 2'b10 : f = i[2]; default: f = i[3]; endcase end endmodule

35 4對1多工器(case)2 module mux4_1 (I0,I1,I2,I3,S,Y); input I0,I1,I2,I3;
input [1:0] S; output Y; reg Y; or I1 or I2 or I3 or S) case (S) 2'b00: Y =I0; 2'b01: Y =I1; 2'b10: Y =I2; 2'b11: Y =I3; endcase endmodule

36 BCD對7段共陽解碼器

37 BCD對7段共陽解碼器 module bcd2seg_case(hex, seg); input [3:0] hex;
output [7:0] seg; reg [7:0] seg; hex ) begin case ( hex) 4'b0001 : seg = 8'b1111_1001; // 1= F9H (共陽,a段在bit0) 4'b0010 : seg = 8'b1010_0100; // 2= A4H 4'b0011 : seg = 8'b1011_0000; // 3= B0H 4'b0100 : seg = 8'b1001_1001; // 4= 99H 4'b0101 : seg = 8'b1001_0010; // 5= 92H 4'b0110 : seg = 8'b1000_0010; // 6= 82H 4'b0111 : seg = 8'b1111_1000; // 7= F8H 4'b1000 : seg = 8'b1000_0000; // 8= 80H 4'b1001 : seg = 8'b1001_0000; // 9= 90H 4'b1010 : seg = 8'b1000_1000; // A= 88H 4'b1011 : seg = 8'b1000_0011; // b= 83H 4'b1100 : seg = 8'b1100_0110; // c= C6H 4'b1101 : seg = 8'b1010_0001; // d= A1H 4'b1110 : seg = 8'b1000_0110; // E= 86H 4'b1111 : seg = 8'b1000_1110; // F= 8EH default : seg = 8'b1100_0000; // 0= C0H endcase end endmodule

38 有enable的2對4解碼器-if module decode2_4( input wire en, input wire [1:0] a,
output reg [3:0] d ); begin if ( en == 1'b0 ) //也可寫成 ~en d = 4'b0000; else if ( a == 2'b00 ) d = 4'b0001; else if ( a == 2'b01 ) d = 4'b0010; else if ( a == 2'b10 ) d = 4'b0100; else d = 4'b1000; end endmodule

39 有enable的2對4解碼器-case module decode2_4( input wire [1:0] a,
input wire en, output reg [3:0] d ); begin case ( { en , a } ) 3'b000, 3'b001, 3'b010, 3'b011 : d = 4'b0000; 3'b100 : d = 4'b0001; 3'b101 : d = 4'b0010; 3'b110 : d = 4'b0100; 3'b111 : d = 4'b1000; endcase end endmodule

40 有enable的3對8低態輸出解碼器1 module Decoder_Behavioral_3x8_2( input [2:0] i,
input en, output reg [7:0] y ); begin case( { en , i } ) 4'b1000: y = 8'hfe; 4'b1001: y = 8'hfd; 4'b1010: y = 8'hfb; 4'b1011: y = 8'hf7; 4'b1100: y = 8'hef; 4'b1101: y = 8'hdf; 4'b1110: y = 8'hbf; 4'b1111: y = 8'h7f; default: y = 8'hff; endcase end endmodule

41 有enable的3對8低態輸出解碼器2 module Decoder_Behavioral_3x8_1( input [2:0] i,
input en, output [7:0] y ); reg [7:0] x; begin x = 8'b0000_0001; x = x << {~en,i}; end assign y = ~x; endmodule

42 4對16解碼器 利用有enable 的2對4解碼器

43 1對4解多工器 module demux_1_4_bhv_if(i,s,f); input i; input [1:0] s;
output [3:0] f; reg [3:0] f; i or s ) begin if ( s == 2'b00 ) f = { 3'b000, i }; else if ( s == 2'b01 ) f = { 2'b00, i, 1'b0 }; else if ( s == 2'b10 ) f = { 1'b0, i, 2'b00 }; else f = {i, 3'b000 }; end endmodule

44 4 bit 比較器-1 module compare1(comout, a, b);
output [2:0] comout; //comout[2]:great than, //comout[1]:equal comout[0]:less than input [3:0] a, b; //input data reg [2:0] tmp_dat; or b) begin if (a>b) tmp_dat <= 3'b100; //GT EQ LT else if (a==b) tmp_dat <= 3'b010; else tmp_dat <= 3'b001; end assign comout = tmp_dat; endmodule

45 4 bit 比較器-2 module comparator_4bit_bhv_if(a,b,gt,eq,lt);
input[3:0] a,b; output gt,eq,lt; reg gt,eq,lt; a or b ) begin if ( a == b ) eq = 1'b1; gt = 1'b0; lt = 1'b0; end else if ( a > b ) eq = 1'b0; gt = 1'b1; lt = 1'b0; eq = 1'b0; gt = 1'b0; lt = 1'b1; endmodule

46 4 bit 栓鎖器(latch) module latch_4bit(din,load,f); input [3:0] din;
input load; output [3:0] f; reg[3:0] f; load ) begin if ( load ) f = din; end endmodule

47 計數器 不規則計數的計數器 規則計數 上數計數器 下數計數器 可上、下數計數器 可載入計數器

48 4bit上數計數器(0->9往上數) module count_9( clkm, reset, bcd );
input clkm,reset; output [3:0] bcd; reg [3:0] counter; posedge clkm or negedge reset ) begin if ( ! reset ) counter <= 4'h0; else if ( counter == 4'h9 ) counter <= 4'h0; counter <= counter + 1; end assign bcd = counter; endmodule

49 非同步重置8 bit (0→255)上數計數器 module counter_8bit(clk,reset,count);
input clk; input reset; output [7:0] count; reg [7:0] count; clk or posedge reset) begin if ( reset ) count <= 8'b0; else count <= count + 1; end endmodule

50 下數4bit計數器 0→F →E →D →..... →1 →0 module Counter_Fto0_1( input clk,
input reset, output [3:0] y ); reg [3:0] counter; posedge clk or negedge reset ) begin if ( ! reset ) counter <= 4'h0; else if ( counter == 4'h0 ) counter <= 4'hf; counter <= counter - 1; end assign y = counter; endmodule

51 可上、下數4bit計數器 module Counter_up_down_1( input clk, input up_down,
input reset, output [3:0] Q ); reg [3:0] counter; clk or negedge reset) if(!reset) counter = 4'h0; else case(up_down) 1: if (counter == 4'h9) counter = 4'h0; else counter = counter + 1; default: if (counter == 4'h0) counter = 4'h9; else counter = counter - 1; endcase assign Q = counter; endmodule

52 可載入上、下數4bit計數器 module Counter_load_up_down_1( input clk, input load,
input up_down, input reset, input [3:0] Din, output [3:0] Q ); reg [3:0] counter; clk or negedge reset) if ( ! rese t) counter = 4'h0; else if ( load ) counter = Din; else case ( up_down ) 1: if ( counter == 4'h9 ) counter = 4'h0; else counter = counter + 1; default: if (counter == 4'h0) counter = 4'h9; else counter = counter - 1; endcase assign Q = counter; endmodule

53 00 →99上數計數器

54 0~9 Up Counter module Counter_up_0to9 ( input clk,en,clr,
output reg tc, output [3:0] out ); reg [3:0] counter; clk or posedge clr) if( clr ) counter = 4'h0; else if( en ) begin if( counter == 4'h9 ) tc = 1; end else tc = 0; counter = counter + 1; assign out = counter; endmodule

55 00 →99 main module Counter_up_00to99_1( input clk,reset,
output [3:0] one_out, output [3:0] ten_out ); wire tc; Counter_up_0to9 count1( .clk( clk ) , .en( 1 ) , .clr( ~reset ) , .tc( tc ) , .out( one_out ) ) ; Counter_up_0to9 count2( .clk( clk ) , .en( tc ) , .clr( ~reset ) , .out( ten_out ) ) ; endmodule

56 左右移1bit暫存器 利用連結運算子 module sht_l_r( sht_out, din, l_r );
output [7:0] sht_out; input [7:0] din; input l_r; //l_r : selection of shift left or right reg [7:0] cnt; or din) begin cnt = din; if ( l_r ) cnt = { cnt[6:0], 1'b0 }; //Shift left 大括號:連結運算子 else cnt = { 1'b0, cnt[7:1] }; //Shift right end assign sht_out = cnt; endmodule


Download ppt "Ch01-2 Verilog語法 資料流(DataFlow)設計 行為(Behavior)設計"

Similar presentations


Ads by Google