篩選最小值---verilog

篩選最小值---verilogspa

`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: chensimin
// 
// Create Date: 2018/12/07 15:30:20
// Design Name: 
// Module Name: aes_dru
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module aes_dru (

    input       wire      clk,
    input       wire      rst

    );


//---------------------------------------------------------------------

//計數器,方便對生成的序列進行定位,仿真時,生成信號源的好思路

reg [8:0] cnt_for_serin = 0;

always @ (posedge clk or posedge rst)
begin
    if(rst)
        cnt_for_serin <= 0;
    else 
        cnt_for_serin <= cnt_for_serin + 1'b1;
end

//---------------------------------------------------------------------

//生成serin序列

reg serin = 0;

always @ (posedge clk or posedge rst)
begin
    if(rst)
        serin <= 0;
    else 
    begin
        case(cnt_for_serin)
            5 : serin <= 1'b1;
            12: serin <= 1'b0;
            20: serin <= 1'b1;
            40: serin <= 1'b0;
            70: serin <= 1'b1;
            default: serin <= serin;
        endcase
    end
end

//---------------------------------------------------------------------

reg  [2:0] inffs = 0;

always @ (posedge clk or posedge rst)
begin
    if(rst)
        inffs <= 0;
    else 
        inffs <= {inffs[1:0], serin};
end 

//---------------------------------------------------------------------

//邊沿檢測能夠檢測上升沿和降低沿

assign edge_detect = ^inffs[2:1];

//---------------------------------------------------------------------

//計算出兩個脈衝之間的時鐘週期

reg  [9:0] min_cntr = 0;

always @ (posedge clk or posedge rst)
begin
    if(rst)
        min_cntr <= 0;
    else if(edge_detect)
        min_cntr <= 0;
    else
        min_cntr <= min_cntr + 1;
end 

//---------------------------------------------------------------------

//對邊沿脈衝進行計數

reg [9:0] update_cntr = 0;

always @ (posedge clk or posedge rst)
begin
    if(rst)
        update_cntr <= 0;
    else if(edge_detect)
        update_cntr <= update_cntr + 1'b1;
end

//---------------------------------------------------------------------

//當邊沿脈衝數滿後,產生一個叫update_min的脈衝

wire   update_min;

assign update_min = update_cntr == {10{1'b1}};

//---------------------------------------------------------------------

//當updata_min脈衝到來時,最小值設定爲全1,new_min成爲更新min_capture的條件

reg [9:0] min_capture = 0;

always @ (posedge clk or posedge rst)
begin
    if(rst)
        min_capture <= 0;
    else if(edge_detect)
    begin
        if(update_min)
            min_capture <= {10{1'b1}};
        else if(new_min)
            min_capture <= min_cntr;
    end
end

assign new_min = min_cntr < min_capture;

//---------------------------------------------------------------------

//當結束完一個計數週期後,用min_hold 鎖存本週期的最小值

reg  [9:0] min_hold = 0 ;

always @ (posedge clk or posedge rst)
begin
    if(rst)
        min_hold <= 0;
    else if (edge_detect & update_min)
        min_hold <= min_capture;
end 

//---------------------------------------------------------------------

//

reg  [9:0] sample_cntr = 0;

always @ (posedge clk or posedge rst)
begin
    if(rst)
        sample_cntr <= 0;
    else if (edge_detect | (sample_cntr >= min_hold))
        sample_cntr <= 0;
    else
        sample_cntr <= sample_cntr + 1;
end       

//---------------------------------------------------------------------


endmodule


/*

add_force {/aes_dru/clk} -radix hex {1 0ns} {0 25000ps} -repeat_every 50000ps
add_force {/aes_dru/rst} -radix hex {1 0ns} {0 100ns}


*/

仿真結果:code

相關文章
相關標籤/搜索