# 數位系統實驗報告Lab14
:::info
學號: B112040003
系級: 資工115
姓名: 張景旭
:::
```
實驗日期: 2023/12/04
```
## 實驗一
### 內容
* 設計並驗證使用Verilog HDL structure modeling實現四位通用移位寄存器。
* 編寫Verilog HDL以實現以下功能:
1. 重置所有觸發器為0。
2. 平行載入“1111”。
3. 無變化。
4. 向右移位 => “0011”。
5. 向左移位 => “1100”。
### 過程
1. 繪製電路結構

2. 用Verilog HDL 硬體描述語言表示電路的結構
```verilog=
`timescale 1ns / 1ps
module D_flip_flop_AR_b (Q, D, Clk, rst);
output Q;
input D, Clk, rst;
reg Q;
always @ (posedge Clk, negedge rst)
if (rst == 0) Q <= 1'b0;
else Q <= D;
endmodule
module mux_4x1_beh(
output reg Y,
input I0, I1, I2, I3,
input [1: 0] Select
);
always @ (*)
case (Select)
2'b00: Y = I0;
2'b01: Y = I1;
2'b10: Y = I2;
2'b11: Y = I3;
endcase
endmodule
module Shift_Register_4_beh (
output [3: 0] A, // Register output
input [3: 0] I, // Parallel input
input s1, s0, // Select inputs
MSB_in, LSB_in, // Serial inputs
CLK, Clear_b // Clock and Clear_b
);
wire [3:0] D_in, select;
wire rst;
assign rst = Clear_b;
assign select = {s1,s0};
mux_4x1_beh mux3(D_in[3],A[3],MSB_in,A[2],I[3],select),
mux2(D_in[2],A[2],A[3],A[1],I[2],select),
mux1(D_in[1],A[1],A[2],A[0],I[1],select),
mux0(D_in[0],A[0],A[1],LSB_in,I[0],select);
D_flip_flop_AR_b D3(A[3],D_in[3],CLK,rst),
D2(A[2],D_in[2],CLK,rst),
D1(A[1],D_in[1],CLK,rst),
D0(A[0],D_in[0],CLK,rst);
endmodule
```
3. 撰寫testbench檔案
```verilog=
module t_Shift_Register_4_beh ();
reg s1, s0, // Select inputs
MSB_in, LSB_in, // Serial inputs
clk, reset_b; // Clock and Clear_b
reg [3: 0] I_par; // Parallel input
wire [3: 0] A_par; // Register output
Shift_Register_4_beh M0
(A_par, I_par,s1, s0, MSB_in, LSB_in, clk, reset_b);
initial #200 $finish;
initial begin clk = 0; forever #5 clk = ~clk; end
initial fork
// test reset action load
#3 reset_b = 1;
#4 reset_b = 0;
#9 reset_b = 1;
// test parallel load
#10 I_par = 4'hA;
#10 {s1, s0} = 2'b11;
// test shift right
#30 MSB_in = 1'b0;
#30 {s1, s0} = 2'b01;
// test shift left
#80 LSB_in = 1'b1;
#80 {s1, s0} = 2'b10;
// test circulation of data
#130 {s1, s0} = 2'b11;
#140 {s1, s0} = 2'b00;
// test reset on the fly
#150 reset_b = 1'b0;
#160 reset_b = 1'b1;
#160 {s1, s0} = 2'b11;
join
endmodule
```
4. Run Synthesis and Simulation
5. 確認結果是否符合預期
### 結果
- 輸出結果符合預期

## 實驗二
### 內容
* 製作一個可以判斷練續3個'1'(含以上)的序向邏輯電路
1. 繪製 state table and the state diagram
2. 用T flip-flops設計序向邏輯
3. 用Verilog HDL 硬體描述語言表示電路的行為
4. 用Verilog HDL 硬體描述語言表示電路的結構
5. 輸入0111110110
### 過程
1. 找出 input equation

2. 用Verilog HDL 硬體描述語言表示電路的結構
```verilog=
`timescale 1ns / 100 ps
module D_flip_flop_AR_b (Q, D, Clk, rst);
output Q;
input D, Clk, rst;
reg Q;
always @ (posedge Clk, negedge rst)
if (rst == 0) Q <= 1'b0;
else Q <= D;
endmodule
module T_flip_flop_3 (Q,T, Clk, rst);
output Q;
input T, Clk, rst;
//reg Q;
wire T_b,n1,n2;
wire D_in;
xor(D_in, Q,T);
D_flip_flop_AR_b MM(Q,D_in,Clk,rst);
endmodule
module BCD_Counter_4bit (A, Count, Reset);
output [3:0] A;
input Count,Reset;
wire [3:0] T_in;
wire rst;
assign rst = ~Reset;
assign T_in[0] = 1;
assign T_in[1]= ~A[3]&A[0];
assign T_in[2]= A[1]&A[0];
assign T_in[3]= A[3]&A[0]|A[2]&A[1]&A[0];
T_flip_flop_3 T3(A[3],T_in[3],Count, rst),
T2(A[2],T_in[2],Count, rst),
T1(A[1],T_in[1],Count, rst),
T0(A[0],T_in[0],Count, rst);
endmodule
```
3. 撰寫testbench檔案
```verilog=
//Stimulus for testing ripple counter
module testcounter;
reg Count;
reg Reset;
wire [3:0] A;
//Instantiate ripple counter
BCD_Counter_4bit M0 (A, Count, Reset);
always
#5 Count = ~Count;
initial
begin
Count = 1'b0;
Reset = 1'b1;
#4 Reset = 1'b0;
end
initial #170 $finish;
endmodule
```
4. Run Synthesis and Simulation
5. 確認結果是否符合預期
### 結果
- 輸出結果符合上面預期結果

## 實驗心得
> 在實驗一中,我運用了以前學過的多工器和 D flip-flops 這兩項基本元件,組成了一個通用移位寄存器。這裡的多工器扮演了關鍵的角色,負責在不同模式下選擇輸入數據的來源。透過巧妙的設計,我成功實現了四種操作模式:重置、平行載入、向右移位和向左移位。
對多工器的運用不僅展現了對數位電路元件的熟悉程度,同時也突顯了模組化設計的優勢。通用移位寄存器的設計具有擴展性,可以方便地加入更多的功能或進行修改,而不需要大幅度改動整個電路結構。
> 在實驗二中,我首先面對了一個需要先分析 state table 的挑戰。透過這個過程,我得到了序向邏輯電路的輸入方程式,這是設計 T flip-flops 所必需的。這一步驟展現了對邏輯設計的理解和分析能力,尤其是在處理狀態轉換的過程中。
接著,我使用 T flip-flops 實現了 BCD Counter,這是一個相當有趣的應用。T flip-flops 提供了一個簡潔且有效的方式,使得在序向邏輯電路中實現計數功能變得更加容易。這也強調了硬體描述語言的優勢,讓我們可以以更抽象的層次來思考和設計電路。