// LED控制模塊
assign led_out = led_reg;
always @ (posedge clk) begin
if(reset) begin
led_reg <= 8'b0;
end else begin
led_reg <= led_reg + 1;
end
end
// 串口發送模塊
assign uart_tx = uart_tx_reg;
always @ (posedge clk) begin
if(reset) begin
uart_tx_reg <= 1'b0;
uart_tx_busy <= 1'b0;
end else if(uart_tx_busy) begin
if(uart_tx_reg[0] == 1'b0) begin
uart_tx_reg <= {uart_tx_reg[7:1], 1'b1};
uart_tx_busy <= 1'b1;
end else begin
uart_tx_reg <= {uart_tx_reg[7:1], 1'b0};
uart_tx_busy <= 1'b0;
end
end
end
// 計數器控制模塊
always @ (posedge clk) begin
if(reset) begin
cnt_next <= 32'b0;
cnt_reg <= 32'b0;
end else begin
cnt_next <= cnt + 1;
if(cnt_next >= COUNTER_MAX) begin
cnt_reg <= 32'b0;
end else begin
cnt_reg <= cnt_next;
end
end
end
// 計數器輸出
assign cnt_out = cnt_reg;
uart_tx_reg <= 8'b0;
uart_tx_busy <= 1'b0;
end else if(uart_tx_busy == 1'b0) begin
uart_tx_reg <= {1'b0, cnt[31:24], cnt[23:16], cnt[15:8], cnt[7:0]};
uart_tx_busy <= 1'b1;
end
end
endmodule
// 計數器模塊
module counter_32(
input clk,
input reset,
output reg [31:0] cnt
);
always @ (posedge clk) begin
if(reset) begin
cnt <= 32'b0;
end else begin
cnt <= cnt + 1;
end
end