module knight_rider_led (
input wire clk, // 時脈訊號
input wire rst_n, // 非同步重置訊號 (低有效)
output reg [9:0] leds // 10 顆 LED 控制輸出
);
reg [3:0] position; // 當前亮燈的位置 (範圍 0~9)
reg direction; // 方向 (0: 向右, 1: 向左)
reg [23:0] counter; // 降頻計數器 (調整 LED 變化速度)
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
counter <= 0;
position <= 0; // LED 從左邊開始
direction <= 0; // 初始方向向右
end else begin
counter <= counter + 1;
if (counter == 24'd12_500_000) begin // 降頻: 讓 LED 變化速度變慢
counter <= 0;
if (direction == 0) begin // 向右移動
if (position == 9) direction <= 1; // 到最右邊後,改為向左
else position <= position + 1;
end else begin // 向左移動
if (position == 0) direction <= 0; // 到最左邊後,改為向右
else position <= position - 1;
end
end
end
end
// LED 輸出控制: 點亮當前位置 LED,然後熄滅
always @(*) begin
leds = 10'b0000000000; // 預設全部 LED 都熄滅
leds[position] =