Verilog - 看時序寫代碼(01--02)

寫在前面

整理一些簡單的根據時序圖編寫Verilog代碼的實例,幫助新手學習,老手鞏固。每次更新兩題,根據難度會挑選一些進行講解。

題目 01

在這裏插入圖片描述

題目 01 答案

always @(posedge clk or negedge rst_n)begin
    if(!rst_n)begin
        cnt <= 0;
    end
    else if(add_cnt)begin
        if(end_cnt)
            cnt <= 0;
        else
            cnt <= cnt + 1;
    end
end

assign add_cnt = dout == 1;       
assign end_cnt = add_cnt && cnt== 5-1;   

always  @(posedge clk or negedge rst_n)begin
    if(rst_n == 1'b0)begin
        dout<= 0;
    end
    else if(en==1)begin
        dout<= 1;
    end
    else if(end_cnt==1)begin
        dout<= 0;
    end
end


題目 02

在這裏插入圖片描述

題目 02 答案

本題和01類似,只不過需要切換計數器的時間

always @(posedge clk or negedge rst_n)begin
    if(!rst_n)begin
        cnt <= 0;
    end
    else if(add_cnt)begin
        if(end_cnt)
            cnt <= 0;
        else
            cnt <= cnt + 1;
    end
end

assign add_cnt = dout == 1;       
assign end_cnt = add_cnt && cnt== x-1;   

always  @(posedge clk or negedge rst_n)begin
    if(rst_n == 1'b0)begin
        dout<= 0;
    end
    else if(en1==1||en2==1)begin
        dout<= 1;
    end
    else if(end_cnt==1)begin
        dout<= 0;
    end
end

always  @(posedge clk or negedge rst_n)begin
    if(rst_n == 1'b0)begin
        x<=0;
    end
    else if (en1==1)begin
        x<=2;
    end
    else if(en2==1)begin
        x<=4;
    end
end




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章