# 數位系統實驗報告Lab13
:::info
學號: B093040044
系級: 資工113
姓名: 蔡明軒
:::
```
實驗日期: 2021/12/13
```
## 實驗一
### 內容
- Fig. 5.17(a)

- 得到Fig. 5.17(a)序向電路的 input equation 及 state equation
- 得到Fig. 5.17(a)序向電路的 state table 及 state diagram
- 用Behavioral model,撰寫 state diagram 的 Verilog HDL description
- 用Structural model,撰寫 logic circuit diagram 的 Verilog HDL description
- 撰寫input順序為==00, 01, 11, 10==的Testbench檔案,驗證兩種 HDL description 結果是否相同(第一次運作的時候把 A reset為 0)
### 過程
- Input equation
$D = A\oplus x\oplus y$
- State equation
- D flip-flop輸出即輸入
$A(t+1) = A\oplus x\oplus y$
- State table
| Present State | x | y | Next state |
|:-------------:|:---:|:---:|:----------:|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 1 |
- 觀察State table,畫出State Diagram

- Flip-flop 採用 PPT 的版本
```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
```
- 根據剛剛完成的 State Diagram,撰寫 Behavioral 版本的 Verilog HDL description
```verilog=
module Diagram (
output A,
input x,y,Clock,reset
);
reg state;
parameter S0 = 1'b0, S1 = 1'b1;
always @(posedge Clock, negedge reset)
if(reset==0)state<=S0;
else
case(state)
S0:if(x == y) state<=S0;else state<=S1;
S1:if(x == y) state<=S1;else state<=S0;
endcase
assign A = state;
endmodule
```
- 根據上面的電路圖,撰寫 Structural 版本的 Verilog HDL description
```verilog=
module Stuctural_Model (
output A,
input x,y,Clock,reset
);
wire w1,w2,tmp;
xor(w1,x,y);
xor(w2,A,w1);
D_flip_flop D1(A,tmp,w2,Clock,reset);
endmodule
```
- 撰寫testbench,input順序如下
| x | y |
| - | - |
| 0 | 0 |
| 1 | 1 |
| 0 | 1 |
| 1 | 0 |
```verilog=
`timescale 1ns / 1ps
module tb;
wire A_Diagram, A_Stuctural;
reg x,y, Clock, reset;
Diagram M1 (A_Diagram, x, y, Clock, reset);
Stuctural_Model M2 (A_Stuctural, x, y, Clock, reset);
initial #40 $finish;
initial begin Clock = 0; forever #5 Clock = ~Clock; end
initial fork
reset = 0;
#2 reset = 1;
x=0; y=0;
#10 x=0;#10 y=1;
#20 x=1;#20 y=1;
#30 x=1;#30 y=0;
join
endmodule
```
### 結果
- state diagram

- 預期結果
| Time | x | y | State | Next State |
|:----: |:---:|:---:|:-----:|:----------:|
| 5ns | 0 | 0 | 0 | 0 |
| 15ns | 0 | 1 | 0 | 1 |
| 25ns | 1 | 1 | 1 | 1 |
| 35ns | 1 | 0 | 1 | 0 |
- Diagram(Behavioral) 和 Structural 的輸出結果都與上面的預期結果相同

## 實驗二
### 內容
- Fig. 5.18

- 得到Fig. 5.18序向電路的 input equation 及 state equation
- 得到Fig. 5.18序向電路的 state table 及 state diagram
- 用Behavioral model,撰寫 state diagram 的 Verilog HDL description
- 用Structural model,撰寫 logic circuit diagram 的 Verilog HDL description
- 撰寫input順序為==0, 1, 0, 0==的Testbench檔案,驗證兩種 HDL description 結果是否相同(第一次運作的時候把 A 和 B reset為 0)
### 過程
- Input equation
$J_A=B,K_A=Bx'$
$J_B=x',K_B=x\oplus A$
- State equation
**利用JK Flip-Flop 的 Characteristic equations
$Q(t+1) = JQ'+K'Q$,得到兩個Flipflop的輸出**
- $A(t+1) = BA'+(Bx')'A=BA'+B'A+Ax$
- $B(t+1) = x'B'+(x\oplus A)'B=x'B'+xAB+x'A'B$
- State table
| A(t) | B(t) | x | A(t+1) | B(t+1) |
|:----:|:----:|:---:|:------:|:------:|
| 0 | 0 | 0 | 0 | 1 |
| 0 | 0 | 1 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 0 | 1 | 1 | 0 |
| 1 | 1 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 | 1 |
- 觀察State table,畫出State Diagram

- 參考JK Flip-flop的characteristic equations
$Q(t+1) = JQ'+K'Q$,將 PPT 提供的D Flip-flop修改成JK Flip-flop
```verilog=
module JK_flip_flop (Q, Q_b, J,K , Clk, rst);
output Q, Q_b;
input J,K, Clk, rst;
reg Q;
assign Q_b = ~Q;
always @ (posedge Clk, negedge rst)
if (rst == 0) Q <= 1'b0;
else Q <= (J&Q_b | ~K&Q);
endmodule
```
- 根據剛剛完成的 State Diagram,撰寫 Behavioral 版本的 Verilog HDL description
```verilog=
module Diagram (
output A,B,
input x,Clock,reset
);
reg[1:0] state;
parameter S0 = 2'b00, S1 = 2'b01,S2=2'b10,S3=2'b11;
always @(posedge Clock, negedge reset)
if(reset==0)state<=S0;
else
case(state)
S0:if(x) state<=S0;else state<=S1;
S1:if(x) state<=S2;else state<=S3;
S2:if(x) state<=S2;else state<=S3;
S3:if(x) state<=S3;else state<=S0;
endcase
assign A = state[1],B = state[0];
endmodule
```
- 根據上面的電路圖,撰寫 Structural 版本的 Verilog HDL description
```verilog=
module Stuctural_Model (
output A,B,
input x,Clock,reset
);
wire nA,nB,nx,K_a,K_b;
not(nx,x);
xor(K_b,A,x);
and(K_a,nx,B);
JK_flip_flop F_A(A,nA,B,K_a,Clock,reset),F_B(B,nB,nx,K_b,Clock,reset);
endmodule
```
- 撰寫testbench,input順序為 0,1,0,0
```verilog=
`timescale 1ns / 1ps
module tb;
wire A_Diagram,B_Diagram, A_Structural,B_Structural;
reg x, Clock, reset;
Diagram M1 (A_Diagram, B_Diagram, x, Clock, reset);
Stuctural_Model M2 (A_Structural,B_Structural, x, Clock, reset);
initial #40 $finish;
initial begin Clock = 0; forever #5 Clock = ~Clock; end
initial fork
reset = 0;
#2 reset = 1;
x = 0;
#10 x = 1;
#20 x = 0;
#30 x = 0;
join
endmodule
```
### 結果
- state diagram

- 預期結果
| Time | x | State | Next State |
|:----:|:---:|:-----: |:----------: |
| 5ns | 0 | 00 | 01 |
| 15ns | 1 | 01 | 10 |
| 25ns | 0 | 10 | 11 |
| 35ns | 0 | 11 | 00 |
- Diagram(Behavioral) 和 Structural 的輸出結果都與上面的預期結果相同,A為State前面的bit,B則為後面的bit

## 實驗心得
>這次的實驗為序向電路分析,在實驗一的過程中,我將程式寫完後,發現 Structural 的輸出都一直是 X ,一開始,我先確認了各個 module 的 port 有沒有接錯,但也沒有發現任何錯誤,後來詢問了助教,這才發現原來在 x 和 y 賦值前 reset都是無效的,這樣會變成在第一個正緣的時候因為x , y尚未賦值,輸出也會變成 X,在進入下一個正緣之後,也會因為原本的狀態為 X ,最後的結果也只會是 X。
>由於有了實驗一撞牆的經驗,實驗二就順利很多,我在模擬最一開始的時候就給 x 賦值,在第二個 ns 進行reset ,這樣一來,就可以讓程式在 reset 前就給 input 賦值,就不會產生實驗一的錯誤了。