// High-speed counter with serial output. // // Usage: // 1. Connect input signal to clk, everything else to microcontroller. // 2. Reset the counter. // 3. Bring ce high to start counting. // 4. If desired, wait for 'counting' to go high to synchronize gate to slow input. // 5. Wait for counts to accumulate (make sure Fmax can't overflow CTRWIDTH). // 6. Bring ce low to stop counting. // 7. If desired, wait for 'counting' to go low to synchronize end of gate. // 8. Read LSB from sdata, // 9. Strobe sclk, read next bit, repeat. // 10. Frequency is ctr / gate_time. module top(clk, ce, rst, sclk, sdata, counting); input clk; input ce; input rst; input sclk; output sdata; output counting; parameter CTRWIDTH = 28; reg [CTRWIDTH-1:0] ctr; always @(posedge clk or negedge rst) if (~rst) ctr <= 0; else if (~ce) ctr <= ctr; else ctr <= ctr + 1; reg counting; always @(posedge clk or negedge rst) if (~rst) counting <= 0; else if (ce) counting <= 1; else if (~ce) counting <= 0; reg [4:0] index; always @(posedge sclk or negedge rst) if (~rst) index <= 0; else index <= index + 1; assign sdata = ce ? 0 : ctr[index]; endmodule