// D Latch 模組 // 用於保存警報狀態 module d_latch ( input wire D, // D 輸入信號 input wire enable, // 使能信號 output reg Q // Q 輸出信號 ); always @ (D or enable) begin if (enable) begin Q <= D; // 當使能信號為高時,將 D 傳送到 Q end end endmodule // 居家安防系統模組 // 包含兩種警報:不明入侵和火災 module home_security_system ( input wire intrusion_detected, // 偵測到不明入侵信號 input wire fire_detected, // 偵測到火災信號 input wire reset_alarm, // 解除警報信號 input wire clk, // 時脈信號 output wire intrusion_alarm, // 不明入侵警報輸出 output wire fire_alarm // 火災警報輸出 ); // 宣告兩個 D Latch 的信號 wire intrusion_latch_output; wire fire_latch_output; // 實例化 D Latch 模組來記錄不明入侵狀態 d_latch intrusion_latch ( .D(intrusion_detected), // 當偵測到入侵時,D 輸入為 1 .enable(~reset_alarm), // 當解除警報信號為低時,D Latch 會記錄入侵狀態 .Q(intrusion_latch_output) // 記錄入侵狀態 ); // 實例化 D Latch 模組來記錄火災狀態 d_latch fire_latch ( .D(fire_detected), // 當偵測到火災時,D 輸入為 1 .enable(~reset_alarm), // 當解除警報信號為低時,D Latch 會記錄火災狀態 .Q(fire_latch_output) // 記錄火災狀態 ); // 根據 D Latch 的輸出來觸發警報 assign intrusion_alarm = intrusion_latch_output; // 不明入侵警報 assign fire_alarm = fire_latch_output; // 火災警報 endmodule // 測試平台(Testbench)模組 // 模擬不明入侵、火災以及解除警報的情況 module test_home_security_system; reg intrusion_detected; // 模擬不明入侵偵測信號 reg fire_detected; // 模擬火災偵測信號 reg reset_alarm; // 模擬解除警報信號 reg clk; // 時脈信號 wire intrusion_alarm; // 不明入侵警報輸出信號 wire fire_alarm; // 火災警報輸出信號 // 實例化居家安防系統 home_security_system security ( .intrusion_detected(intrusion_detected), .fire_detected(fire_detected), .reset_alarm(reset_alarm), .clk(clk), .intrusion_alarm(intrusion_alarm), .fire_alarm(fire_alarm) ); // 時脈產生 always begin #5 clk = ~clk; // 每 5 單位時間反轉一次時脈 end // 測試序列 initial begin // 初始化信號 clk = 0; intrusion_detected = 0; fire_detected = 0; reset_alarm = 0; // 模擬偵測到不明入侵 #10 intrusion_detected = 1; // 偵測到不明入侵 #10 intrusion_detected = 0; // 入侵結束 // 模擬偵測到火災 #10 fire_detected = 1; // 偵測到火災 #10 fire_detected = 0; // 火災結束 // 模擬解除警報 #10 reset_alarm = 1; // 解除警報 #10 reset_alarm = 0; // 完成解除 // 再次偵測不明入侵 #10 intrusion_detected = 1; // 偵測到不明入侵 #10 intrusion_detected = 0; // 入侵結束 end // 監測警報輸出 initial begin $monitor("At time %0t, intrusion_alarm = %b, fire_alarm = %b", $time, intrusion_alarm, fire_alarm); end endmodule