Зачем использовать два триггера вместо одного в этом коде Verilog HDL?

Этот код представляет собой средство защиты от кнопок. Но я не могу понять, почему там два шлепанца:

reg PB_sync_0;  always @(posedge clk) PB_sync_0 <= ~PB;  // invert PB to make PB_sync_0 active high
reg PB_sync_1;  always @(posedge clk) PB_sync_1 <= PB_sync_0;

Почему автор этого кода этого не написал?

reg PB_sync_1;  always @(posedge clk) PB_sync_1 <= ~PB;

Вот полный код:

module PushButton_Debouncer(
    input clk,
    input PB,  // "PB" is the glitchy, asynchronous to clk, active low push-button signal

    // from which we make three outputs, all synchronous to the clock
    output reg PB_state,  // 1 as long as the push-button is active (down)
    output PB_down,  // 1 for one clock cycle when the push-button goes down (i.e. just pushed)
    output PB_up   // 1 for one clock cycle when the push-button goes up (i.e. just released)
);

// First use two flip-flops to synchronize the PB signal the "clk" clock domain
reg PB_sync_0;  always @(posedge clk) PB_sync_0 <= ~PB;  // invert PB to make PB_sync_0 active high
reg PB_sync_1;  always @(posedge clk) PB_sync_1 <= PB_sync_0;

// Next declare a 16-bits counter
reg [15:0] PB_cnt;

// When the push-button is pushed or released, we increment the counter
// The counter has to be maxed out before we decide that the push-button state has changed

wire PB_idle = (PB_state==PB_sync_1);
wire PB_cnt_max = &PB_cnt;  // true when all bits of PB_cnt are 1's

always @(posedge clk)
if(PB_idle)
    PB_cnt <= 0;  // nothing's going on
else
begin
    PB_cnt <= PB_cnt + 16'd1;  // something's going on, increment the counter
    if(PB_cnt_max) PB_state <= ~PB_state;  // if the counter is maxed out, PB changed!
end

assign PB_down = ~PB_idle & PB_cnt_max & ~PB_state;
assign PB_up   = ~PB_idle & PB_cnt_max &  PB_state;
endmodule

Спасибо !


person user3821562    schedule 09.07.2014    source источник
comment
См. Последнее изображение здесь: asic-world.com/tidbits/metastablity.html   -  person Ari    schedule 09.07.2014
comment
Это тоже объясняет. sunburst-design.com/papers/CummingsSNUG2008Boston_CDC.pdf   -  person Greg    schedule 09.07.2014
comment
Средство защиты от сбоев делает две вещи: 1) синхронизирует внешний асинхронный вход с внутренними часами и 2) удаляет дребезг для физической кнопки. Синхронизация осуществляется с помощью двойных триггеров, подробные описания которых можно найти по ссылкам в других комментариях.   -  person Morten Zilmer    schedule 09.07.2014
comment
Поскольку этот вопрос в основном касается электроники (устранения неполадок), его, вероятно, следует переместить на ElectronicsSE, хотя, вероятно, он является дубликатом. Вопрос о Verilog приветствуется в stackoverflow, но должен касаться написания Verilog, а не требований к оборудованию.   -  person Morgan    schedule 09.07.2014
comment
Комментарии в коде уже должны ответить на этот вопрос. // Сначала используйте два триггера для синхронизации сигнала PB с тактовой частотой clk ...   -  person andrsmllr    schedule 11.07.2014
comment
Речь идет НЕ о противодействии, а о метастабильности.   -  person Will    schedule 15.07.2014


Ответы (1)


Автор этого кода использует 2 триггера для синхронизации сигнала PB с доменом clk. Как он отметил в комментарии "PB" is the glitchy, asynchronous to clk. Несинхронизация сигнала при смене тактового домена может вызвать метастабильность в системе, как указано в инструменте en.wikipedia.org/ wiki / Metastability_in_electronics

person Razko    schedule 17.09.2014