Design a Finite State Machine (FSM) that meets the following specifications:
1. The circuit has one input, w, and one output, z.
2. All changes in the circuit occur on the positive edge of the clock.
3. The output z is equal to 1 if the pattern 101 is detected on the input w.
Otherwise, the value of z is equal to 0. Overlapping sequences should not be
detected.
Otherwise, the value of z is equal to 0. Overlapping sequences should not be
detected.
0 1 2 3 4 5 6 7 8 9 A B C D E F
Input (w) : 0 0 0 1 0 1 0 1 1 0 1 1 0 1 1 0 …
Output (z): 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 …
reg [15:0] D = 16'b0001_0101_1011_0110 ; test pattern
assign Clk=KEY[0]; Clock
assign Clr=KEY[1]; Clear (Reset)
assign LEDG[1:0]=KEY[1:0]; Display Clock , Clear status 1 or 0 ;
assign LEDR[3]=Q; Display output Q
assign LEDR[1:0]=Cs; Display Current output Cs
assign LEDG[7:4]=i; Display i For Pressed number 0-15 , for test pattern
assign LEDR[2]=Din; Display Data input sequence data input
// Ch08 moore.v
// 莫耳狀態機器 (101字序偵測器)
module Moore (SW, LEDR, LEDG , CLOCK_50,CLOCK_27 ,KEY
,HEX0 ,HEX1 ,HEX2,HEX3 ,HEX4 );
input [17:0] SW; // toggle switches
input [3:0] KEY; // Push bottom
input CLOCK_27; //Clock 27MHz
input CLOCK_50; //Clock 50MHz
output [17:0] LEDR; // red LEDS
output [7:0] LEDG; // green LEDs
output [6:0] HEX0,HEX1,HEX2,HEX3 ,HEX4; //7-segment display
//set original program input , output
//Moore (Clk, Clr, D, Q, Cs);
//input Clk, Clr, D; // 一位元輸入
//output Q; // 一位元輸出
//output [1:0] Cs; // 二位元輸出
reg Din;
wire Clk, Clr; // 一位元輸入
reg Q; // 宣告為暫存器資料
reg [1:0] Cs, Ns; // 宣告為暫存器資料
reg [15:0] D = 16'b0001_0101_1011_0110 ;
reg [3:0] i=4'b0;
assign Clk=KEY[0];
assign Clr=KEY[1];
assign LEDG[1:0]=KEY[1:0];
parameter [1:0] // 宣告狀態參數,二進制編碼
S0=2'b00, S1=2'b01, S2=2'b10, S3=2'b11;
// 上緣觸發時脈,上緣同步清除, 序向邏輯電路
always@ (negedge Clk or negedge Clr)
begin
if (!Clr)
begin
Cs = S0; // 切換為起始狀態
i=4'b0;
D <= 16'b0001_0101_1011_0110 ;
end
else
begin
Cs = Ns; // 切換為次一狀態
Din<={D[15-i]};
i=i+1;
end
end
// 決定次一狀態 Ns 與輸出 Q, 組合邏輯電路
always @ (Cs or Din)
case (Cs)
S0 : begin
Q = 0;
if (Din == 0) Ns = S0;
else Ns = S1;
end
S1 : begin
Q = 0;
if (Din == 0) Ns = S2;
else Ns = S1;
end
S2 : begin
Q = 0;
if (Din == 0) Ns = S0;
else Ns = S3;
end
S3 : begin
Q = 1;
if (Din == 0) Ns = S2;
else Ns = S1;
end
endcase
//output Q; // 一位元輸出
//output [1:0] Cs; // 二位元輸出
assign LEDR[3]=Q;
assign LEDR[1:0]=Cs;
assign LEDG[7:4]=i;
assign LEDR[2]=Din;
endmodule
/*
// Ch08 moore.v
// 莫耳狀態機器 (101字序偵測器)
module Moore (Clk, Clr, D, Q, Cs);
input Clk, Clr, D; // 一位元輸入
output Q; // 一位元輸出
output [1:0] Cs; // 二位元輸出
reg Q; // 宣告為暫存器資料
reg [1:0] Cs, Ns; // 宣告為暫存器資料
parameter [1:0] // 宣告狀態參數,二進制編碼
S0=2'b00, S1=2'b01, S2=2'b10, S3=2'b11;
// 上緣觸發時脈,上緣同步清除, 序向邏輯電路
always@ (posedge Clk)
if (Clr) Cs = S0; // 切換為起始狀態
else Cs = Ns; // 切換為次一狀態
// 決定次一狀態 Ns 與輸出 Q, 組合邏輯電路
always @ (Cs or D)
case (Cs)
S0 : begin
Q = 0;
if (D == 0) Ns = S0;
else Ns = S1;
end
S1 : begin
Q = 0;
if (D == 0) Ns = S2;
else Ns = S1;
end
S2 : begin
Q = 0;
if (D == 0) Ns = S0;
else Ns = S3;
end
S3 : begin
Q = 1;
if (D == 0) Ns = S2;
else Ns = S1;
end
endcase
endmodule
*/
沒有留言:
張貼留言