原代碼中 flag 同時被 assign 持續賦值與 always 塊過程賦值驅動,導致多驅動衝突(Multiple Drivers),綜合報錯。
module Timer (
input wire rst,
input wire oe,
input wire clk,
output reg TI_flag
);
parameter Ttime = 100;
parameter [1:0] IDLE = 2'd0, COUNTING = 2'd1, DONE = 2'd2;
reg [1:0] state = IDLE;
reg [31:0] cnt = 0;
always @(posedge clk or negedge rst) begin
if (!rst) begin
state <= IDLE;
cnt <= 0;
TI_flag <= 1'b1;
end else begin
case (state)
IDLE: begin
if (oe) begin
cnt <= 0;
TI_flag <= 1'b0;
state <= COUNTING;
end
end
COUNTING: begin
if (cnt < Ttime - 1) cnt <= cnt + 1;
else begin
TI_flag <= 1'b1;
state <= DONE;
end
end
DONE: begin
if (!oe) state <= IDLE; // 等待 oe 釋放
end
endcase
end
end
endmodule
TI_flag 僅在時序邏輯中賦值<=)確保時序正確#3 延遲(不可綜合)使用 ModelSim/iverilog 仿真,波形檢查 oe 上升沿觸發計數、TI_flag 時序關係。