To sum up the question, I would like to know what is the maximum frequency with which I can toggle an output of an FPGA. I do not intend this question to be specific to any particular board or vendor. The following paragraphs briefly describe my setup, and what I have tried so far.
I got an FPGA and a 12 MHz clock source. With no previous experience with such devices, I began searching for Verilog examples and experimenting. So far, I can blink a LED once a second (increment a counter by 358 on every pulse), read button presses (no debouncing yet), and now I am trying different combinations of the two.
I decided to write a small hardware description that would produce exactly 10 pulses when I press the button. Here is what I wrote:
reg led_state;
reg btn_state;
reg [3:0] counter;
always @(posedge clk) begin
if (counter < 4'd10) begin
led_state <= ~led_state;
counter <= counter + led_state;
end
if (~btn && btn_state) begin
counter <= 0;
end
btn_state <= btn;
end
assign led_g = led_state;
I noticed that the code works correctly, producing exactly 10 pulses. However, the frequency of the pulses is 6 MHz (half of the input frequency). I suspect this is because I am using posedge clk
, and I was able to get the full 12 MHz out with simply assign led_g = clk;
.
To achieve the full speed of the output, I need to run on both posedge clk
and negedge clk
. So I searched if it's possible to do so, and I found a solution in Verilog:
reg r_clk;
wire xor_clk = r_clk ^ clk;
always @(posedge xor_clk)
r_clk = ~r_clk;
After adding it to my hardware description, I get exactly 10 pulses at exactly 12 MHz. As I understand, it should be possible to multiply the clock signal again to get 24 MHz, then again to get 48 MHz, and so on... What is the trick here? There should definitely be some limitation that would prevent me from doing it, but I don't see it. What is that limitation, and what is the fastest achievable output speed?