用8個7-bit的寄存器組成pipeline。每個寄存器的輸出直接驅動7-segment。設計一個FSM控制pipeline:
1) 系統重定後的前8個時鐘,插入正確的字元(HELLO)。
2) 完成1)後,將pipeline的最後一個寄存器的輸出回饋到第一個寄存器的輸入,建立迴圈。
3) 間隔約1s迴圈顯示HELLO
//part6 間隔約1s迴圈顯示HELLO
module FSM_part6(
input [0:0] KEY, //rst_n
input CLOCK_50, //50 MHz
output [0:6] HEX7,HEX6,HEX5,HEX4,HEX3,HEX2,HEX1,HEX0,
output [8:0]LEDG
);
wire clk,rst_n,clk_1;
reg [3:0] q,d; //FSM的現態和次態
reg [0:6] char; //重定後前8個時鐘pipeline寄存器的輸入,也是迴圈顯示的內容
reg pipe_s; //迴圈啟動
wire [0:6] pipe_in,pipe_o0,pipe_o1,pipe_o2,pipe_o3,pipe_o4,pipe_o5,pipe_o6,pipe_o7;
//pipeline寄存器的輸入和輸出
reg [25:0] cnt; //用於分頻的計數器
reg LED;
parameter S0=4'd0,S1=4'd1,S2=4'd2,S3=4'd3,S4=4'd4,S5=4'd5,S6=4'd6,S7=4'd7,S8=4'd8;
//狀態:初始8個,啟動pipeline寄存器後1個
parameter H=7'b1001000,
E = 7'b0110000,
L = 7'b1110001,
O = 7'b0000001,
Blank = 7'b1111111;
assign rst_n=KEY[0];
assign clk=CLOCK_50;
//分頻,產生約1s的時鐘clk_1
always @(posedge clk)
begin
if(!rst_n)
cnt<=1'b0;
else
cnt<=cnt+1;
if (cnt<=26'd25_000_000)
LED<=1'b0;
else
LED<=1'b1;
end
assign clk_1=~|cnt; //產生約1s的時鐘
assign LEDG[8]=LED;
//狀態轉換
always @(posedge clk)
begin
if(!rst_n)
q<=S0;
else
q<=d;
end
//狀態表
always @(q,clk_1)
begin
case(q)
S0:
if(clk_1)
d<=S1;
else
d<=S0;
S1:
if(clk_1)
d<=S2;
else
d<=S1;
S2:
if(clk_1)
d<=S3;
else
d<=S2;
S3:
if(clk_1)
d<=S4;
else
d<=S3;
S4:
if(clk_1)
d<=S5;
else
d<=S4;
S5:
if(clk_1)
d<=S6;
else
d<=S5;
S6:
if(clk_1)
d<=S7;
else
d<=S6;
S7:
if(clk_1)
d<=S8;
else
d<=S7;
S8:
d<=S8;
default:
d<=4'bxxxx;
endcase
end
//每種狀態的輸出
always @(q)
begin
pipe_s=1'b0;
char=7'bxxx_xxxx;
case(q)
S0:
char=H;
S1:
char=E;
S2:
char=L;
S3:
char=L;
S4:
char=O;
S5:
char=Blank;
S6:
char=Blank;
S7:
char=Blank;
S8:
pipe_s=1'b1; //啟動迴圈顯示
default:
d=4'bxxxx;
endcase
end
assign pipe_in=(pipe_s==1'b1)?pipe_o7:char;
//pipeline寄存器
reg_p r0(pipe_in,clk,rst_n,clk_1,pipe_o0);
reg_p r1(pipe_o0,clk,rst_n,clk_1,pipe_o1);
reg_p r2(pipe_o1,clk,rst_n,clk_1,pipe_o2);
reg_p r3(pipe_o2,clk,rst_n,clk_1,pipe_o3);
reg_p r4(pipe_o3,clk,rst_n,clk_1,pipe_o4);
reg_p r5(pipe_o4,clk,rst_n,clk_1,pipe_o5);
reg_p r6(pipe_o5,clk,rst_n,clk_1,pipe_o6);
reg_p r7(pipe_o6,clk,rst_n,clk_1,pipe_o7);
assign HEX0=pipe_o0,
HEX1=pipe_o1,
HEX2=pipe_o2,
HEX3=pipe_o3,
HEX4=pipe_o4,
HEX5=pipe_o5,
HEX6=pipe_o6,
HEX7=pipe_o7;
endmodule
module reg_p(
input [0:6] r,
input clk,rst_n,e,
output reg [0:6] q
);
always @(posedge clk)
begin
if(!rst_n)
q<=7'b111_1111;
else if(e)
q<=r;
end
endmodule
沒有留言:
張貼留言