首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Verilog 多源驅動同一輸出衝突解決:狀態機重構實踐

     編輯:程式設計綜合問答

問題根因

原代碼中 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 僅在時序邏輯中賦值
  • 明確狀態轉移:IDLE→COUNTING→DONE→IDLE
  • 非阻塞賦值(<=)確保時序正確
  • 移除 #3 延遲(不可綜合)

驗證建議

使用 ModelSim/iverilog 仿真,波形檢查 oe 上升沿觸發計數、TI_flag 時序關係。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved