2

一般来说,我对 Verilog 和 HDL 很陌生。我最近购买了一个 Mojo FPGA (Spartan 6) 开发板并一直在使用它。

我感兴趣的一个概念是移位寄存器,特别是,将 ATmega 连接到 FPGA 以在 FPGA 上加载移位寄存器。我有这个与这个 Verilog 之间的工作:

module sr8(
    input clk,        // FPGA clock
    input sshift,     // Shift signal from ATmega
    input sdata,      // Data from ATmega
    output [7:0] PO   // Parallel output
);

// Shift register
reg [7:0] srdata = 8'd0;

reg sshift_FF;
wire sshift_en;
always @(posedge clk) begin
    sshift_FF <= sshift;
end
assign sshift_en = sshift & !sshift_FF;

always @(posedge clk) begin
    if ( sshift_en ) begin
        srdata <= { srdata[6:0], sdata };
    end
end

assign PO = srdata;

endmodule

这真的很简单,当 sshift_en 为 1 时,它在时钟的上升沿 (50 MHz) 移入数据。 sshift_en 是与 sshift 的当前值和触发器 sshift_FF 的输出的互补的结果(即它检查上升沿)。

我真正的问题是:这是一个好的/正确的方法吗? 我已经对在 Verilog 中实现移位寄存器进行了大量研究,我的概念基本上是相同的,只是我希望移位寄存器的“移位”信号由 AVR 指示(运行速度比 50 MHz 时钟慢得多) FPGA)而不是时钟信号(就像我见过的所有例子一样)。

如果我需要在此处提供有关任何内容的更多信息,请告诉我。

先感谢您!

编辑: 我修改了代码以包含同步部分:

module sr8(
    input clk,
    input sshift,
    input sdata,
    output [7:0] PO
);

// Shift register
reg [7:0] srdata = 8'd0;

// Start of modified synchronization section
reg sshift_d, sshift_dd, sshift_ddd, sshift_en;
always @(posedge clk) begin
    sshift_d <= sshift;
    sshift_dd <= sshift_d;
    sshift_ddd <= sshift_dd;
    sshift_en <= sshift_dd & ~sshift_ddd;
end
// End of modified section

always @(posedge clk) begin
    if ( sshift_en ) begin
        srdata <= { srdata[6:0], sdata };
    end
end

assign PO = srdata;

endmodule
4

1 回答 1

1

Your shift register description looks ok to me.

The sshift signalis from AVR is an asynchronous signal wrt your FPGA clock, which is a source of possible metastability. I would use a synchronizer to reduce the chance.

See Section "Synchronization Registers" in this document:

http://www.altera.com/literature/wp/wp-01082-quartus-ii-metastability.pdf

Another minor point: Although interchangable in the case of your example, ! (together with && and ||) is used for logical operations whereas ~ is used for bitwise negation, which IMHO is more proper for this context. A common use of ! is:

if (!condition)

于 2014-03-28T20:46:22.800 回答