# 數位系統實驗報告Lab14 :::info 學號: B093040044 系級: 資工113 姓名: 蔡明軒 ::: ``` 實驗日期: 12/20 ``` ## 實驗一 ### 內容 - 設計一個可以在input的bit string中偵測連續大於等於三個 1 的序列偵測器 - State diagram 及 state table 附於 Fig. 5.27 及 Table 5.11 - 利用 **D flip-flops** 設計這個序列偵測器 - 用**Behavioral model**,撰寫 state diagram 的 Verilog HDL description - 用**Structural model**,撰寫 logic circuit diagram 的 Verilog HDL description - 撰寫input順序為 **0111110110** 的 Testbench,驗證兩種 HDL description 結果是否相同(第一次運作的時候把所有的 flip-flops reset為 0 - Fig. 5.27 ![](https://i.imgur.com/Y34weW9.png) - Table 5.11 ![](https://i.imgur.com/CLxdnWS.png) ### 過程 - **Behavioral model**: 觀察 Fig. 5.27 ,用 state diagram 的方式撰寫 Verilog HDL description ```verilog= module Behavioral_Model ( output y, input x,Clk,rst ); reg[1:0] state; parameter S0 = 2'b00, S1 = 2'b01,S2 = 2'b10,S3 = 2'b11; always @(posedge Clk, negedge rst) if(rst==0)state<=S0; else case(state) S0:if(x == 1'b0) state<=S0;else state<=S1; S1:if(x == 1'b0) state<=S0;else state<=S2; S2:if(x == 1'b0) state<=S0;else state<=S3; S3:if(x == 1'b0) state<=S0;else state<=S3; endcase assign y = (state == S3); endmodule ``` - **Structural model**: 分析各個 **D flip-flop** 的 input equation 及 output equation,再用 logic circuit diagram 的方式撰寫 Verilog HDL description 1. 觀察 Table 5.11,畫出 Next state 的 Karnaugh maps,並得到 **input equation** - $A(t+1)=\Sigma(3,5,7)=Ax+Bx$ ![](https://i.imgur.com/cnNvyld.png) - $B(t+1)=\Sigma(1,5,7)=Ax+B'x$ ![](https://i.imgur.com/Gbo22Iq.png) 2. 觀察 Table 5.11,畫出 output 的 Karnaugh map,並得到 **output equation** - $y=\Sigma(6,7)=AB$ ![](https://i.imgur.com/IQX33Yg.png) - 根據上面所得到的 equation ,撰寫 Verilog HDL description ```verilog= module D_flip_flop (Q, Q_b, D, Clk, rst); output Q, Q_b; input D, Clk, rst; reg Q; assign Q_b = ~Q; always @ (posedge Clk, negedge rst) if (rst == 0) Q <= 1'b0; else Q <= D; endmodule module Stuctural_Model ( output y, input x,Clk,rst ); wire w1,w2,w3,w4,w5,A,B,A_b,B_b; and(w1,A,x); and(w2,B,x); and(w3,~B,x); or(w4,w1,w2); or(w5,w1,w3); D_flip_flop FF1(A,A_b,w4,Clk,rst),FF2(B,B_b,w5,Clk,rst); and(y,A,B); endmodule ``` - 撰寫testbench,input順序為 **0111110110**,其中y_Behavioral 為 Behavioral model 的輸出,y_Stuctural 則為 Structural model 的輸出 ```verilog= `timescale 1ns / 1ps module tb; wire y_Behavioral, y_Stuctural; reg x, Clk, rst; Behavioral_Model M1(y_Behavioral, x, Clk, rst); Stuctural_Model M2 (y_Stuctural, x, Clk, rst); initial #100 $finish; initial begin Clk = 0; forever #5 Clk = ~Clk; end initial fork rst = 0; #2 rst = 1; x=0; #10 x=1; #20 x=1; #30 x=1; #40 x=1; #50 x=1; #60 x=0; #70 x=1; #80 x=1; #90 x=0; join endmodule ``` ### 結果 - State Diagram ![](https://i.imgur.com/Y34weW9.png) - 根據上面的 Diagram ,推測出預期結果 | tick | x | y | |:----:|:---:|:---:| | #5 | 0 | 0 | | #15 | 1 | 0 | | #25 | 1 | 0 | | #35 | 1 | 1 | | #45 | 1 | 1 | | #55 | 1 | 1 | | #65 | 0 | 0 | | #75 | 1 | 0 | | #85 | 1 | 0 | | #95 | 0 | 0 | - Behavioral 及 Structural 的輸出皆與預期結果相同 ![](https://i.imgur.com/SgqI0UN.png) ## 實驗二 ### 內容 - 設計一個可以在input的bit sting中偵測連續大於等於三個 1 的序列偵測器 - State diagram 及 state table 附於 Fig. 5.27 及 Table 5.11 - 利用 **T flip-flops** 設計這個序列偵測器 - 用**Behavioral model**,撰寫 state diagram 的 Verilog HDL description - 用**Structural model**,撰寫 logic circuit diagram 的 Verilog HDL description - 撰寫input順序為 **0111110110** 的 Testbench,驗證兩種 HDL description 結果是否相同(第一次運作的時候把所有的 flip-flops reset為 0 ### 過程 - **Behavioral model**: 觀察 Fig. 5.27 ,用 state diagram 的方式撰寫 Verilog HDL description ```verilog= module Behavioral_Model ( output y, input x,Clk,rst ); reg[1:0] state; parameter S0 = 2'b00, S1 = 2'b01,S2 = 2'b10,S3 = 2'b11; always @(posedge Clk, negedge rst) if(rst==0)state<=S0; else case(state) S0:if(x == 1'b0) state<=S0;else state<=S1; S1:if(x == 1'b0) state<=S0;else state<=S2; S2:if(x == 1'b0) state<=S0;else state<=S3; S3:if(x == 1'b0) state<=S0;else state<=S3; endcase assign y = (state == S3); endmodule ``` - **Structural model**: 分析各個 **T flip-flop** 的 input equation 及 output equation,再用 logic circuit diagram 的方式撰寫 Verilog HDL description 1. 利用 T flip-flop 的 excitation table,畫出有 T flip-flop output 的 state table - Excitation Table of T flip-flop ![](https://i.imgur.com/i7hkJor.png) - State table with T flip-flop outputs ![](https://i.imgur.com/L3oJjFU.png) 2. 觀察步驟1的 Table,畫出 Next state 的 Karnaugh maps,並得到 input eqution - $T_A(t+1)=\Sigma(3,4,6)=Ax'+A'Bx$ ![](https://i.imgur.com/HMEsZhm.png) - $T_B(t+1)=\Sigma(1,2,3,5,6)=A'x+B'x+Bx'$ ![](https://i.imgur.com/SJVFgUH.png) 3. 觀察 Table 5.11,畫出 output 的 Karnaugh map,並得到 output equation - $y=\Sigma(6,7)=AB$ ![](https://i.imgur.com/IQX33Yg.png) - 根據上面所得到的 equation ,撰寫 Verilog HDL description ```verilog= module T_flip_flop (Q, Q_b, T, Clk, rst); output Q, Q_b; input T, Clk, rst; reg Q; assign Q_b = ~Q; always @ (posedge Clk, negedge rst) if (rst == 0) Q <= 1'b0; else Q <= Q^T; endmodule module Stuctural_Model ( output y, input x,Clk,rst ); wire w1,w2,w3,w4,w5,w6,w7,A,B,A_b,B_b; and(w1,A,~x); and(w2,~A,B,x); and(w3,~B,x); and(w4,B,~x); and(w5,~A,x); or(w6,w1,w2); or(w7,w3,w4,w5); T_flip_flop FF1(A,A_b,w6,Clk,rst),FF2(B,B_b,w7,Clk,rst); and(y,A,B); endmodule ``` - 撰寫testbench,input順序為 **0111110110**,其中y_Behavioral 為 Behavioral model 的輸出,y_Stuctural 則為 Structural model 的輸出 ```verilog= `timescale 1ns / 1ps module tb; wire y_Behavioral, y_Stuctural; reg x, Clk, rst; Behavioral_Model M1(y_Behavioral, x, Clk, rst); Stuctural_Model M2 (y_Stuctural, x, Clk, rst); initial #100 $finish; initial begin Clk = 0; forever #5 Clk = ~Clk; end initial fork rst = 0; #2 rst = 1; x=0; #10 x=1; #20 x=1; #30 x=1; #40 x=1; #50 x=1; #60 x=0; #70 x=1; #80 x=1; #90 x=0; join endmodule ``` ### 結果 - State Diagram ![](https://i.imgur.com/Y34weW9.png) - 根據上面的 Diagram ,推測出預期結果 | tick | x | y | |:----:|:---:|:---:| | #5 | 0 | 0 | | #15 | 1 | 0 | | #25 | 1 | 0 | | #35 | 1 | 1 | | #45 | 1 | 1 | | #55 | 1 | 1 | | #65 | 0 | 0 | | #75 | 1 | 0 | | #85 | 1 | 0 | | #95 | 0 | 0 | - Behavioral 及 Structural 的輸出皆與預期結果相同 ![](https://i.imgur.com/qK2CWnx.png) ## 實驗心得 >這次的實驗為同步序向電路設計,實驗一和實驗二內容是差不多的,都是設計一個偵測連續三個一的序列偵測器,只是前者是用 D Flip-flops 設計,後者則是利用 T Flip-flops。從這次的實驗可以發現,使用不同的 Flop-flop 來設計邏輯電路,最大的差別就是得到 input equation 的時候,因為 D Flip-flop的輸入和輸出相同 直接用 Present state 和 input 對 Next state 的每個 bit 畫 Karnaugh map 即可直接求得每個 flip-flop 的 input function,若是使用 T flip-flop 或是 JK flip-flop 設計,就必須使用激勵表,比對每個 present state 和 next state,得到各個 flip-flop的 input,才可以去做 Karnaugh map 化簡。 >從這個偵測連續三個一的序列偵測器來看,用 D flip-flop來設計是比 T flip-flop 用到的邏輯閘更少,但是課堂上教的 Counter 是 T flip-flop 用到的邏輯閘較少,因此在設計一個同步序向電路時,或許用D flip-flop設計過程較簡單,但不一定使用的邏輯閘是最少的。