diff --git a/firmware/rtl/autoapproach/autoapproach.v b/firmware/rtl/autoapproach/autoapproach.v index d06205d..4f848c7 100644 --- a/firmware/rtl/autoapproach/autoapproach.v +++ b/firmware/rtl/autoapproach/autoapproach.v @@ -6,7 +6,8 @@ module autoapproach #( parameter DAC_WID = 24, parameter DAC_DATA_WID = 20, - parameter ADC_WID = 24 + parameter ADC_WID = 24, + parameter TIMER_WID = 32 ) ( input clk, input arm, @@ -15,6 +16,7 @@ module autoapproach #( input polarity, input [ADC_WID-1:0] setpoint, + input [TIMER_WID-1:0] time_to_wait, /* BRAM memory interface. Each pulse returns the next value in * the sequence, and also informs the module if the sequence @@ -39,21 +41,32 @@ module autoapproach #( localparam WAIT_ON_ARM = 0; -localparam RECV_WORD = 1; -localparam WAIT_ON_DAC = 2; -localparam WAIT_ON_DETECTION = 3; -localparam DETECTED = 4; +localparam DO_WAIT = 1; +localparam RECV_WORD = 2; +localparam WAIT_ON_DAC = 3; +localparam WAIT_ON_DETECTION = 4; +localparam DETECTED = 5; reg [2:0] state = WAIT_ON_ARM; +reg [TIMER_WID-1:0] wait_timer = 0; + always @ (posedge clk) case (state) WAIT_ON_ARM: if (arm) begin - state <= RECV_WORD; - word_next <= 1; + state <= DO_WAIT; stopped <= 0; + wait_timer <= time_to_wait; end else begin stopped <= 1; word_rst <= 1; end +DO_WAIT: if (!arm) begin + state <= WAIT_ON_ARM; +end else if (wait_timer == 0) begin + word_next <= 1; + state <= RECV_WORD; +end else begin + wait_timer <= wait_timer - 1; +end RECV_WORD: if (word_ok) begin dac_out <= {4'b0001, word}; dac_arm <= 1; diff --git a/firmware/rtl/autoapproach/bram_interface.v b/firmware/rtl/autoapproach/bram_interface.v index a59a6c5..851e155 100644 --- a/firmware/rtl/autoapproach/bram_interface.v +++ b/firmware/rtl/autoapproach/bram_interface.v @@ -86,6 +86,9 @@ reg [WORD_AMNT_WID-1:0] auto_cntr = 0; always @ (posedge clk) if (word_rst) begin auto_cntr <= 0; + word_ok <= 0; + word_last <= 0; + word <= 0; end else if (word_next && !word_ok) begin if (refresh_state == WAIT_ON_REFRESH) begin word <= backing_buffer[auto_cntr]; diff --git a/firmware/rtl/autoapproach/bram_interface_sim.cpp b/firmware/rtl/autoapproach/bram_interface_sim.cpp index d93e845..13dabd6 100644 --- a/firmware/rtl/autoapproach/bram_interface_sim.cpp +++ b/firmware/rtl/autoapproach/bram_interface_sim.cpp @@ -86,6 +86,24 @@ static void test_aa_read_1() { my_assert(ind == WORD_AMNT, "second read value %zu != %d\n", ind, WORD_AMNT); } +static void test_aa_read_interrupted() { + size_t ind = 0; + + mod->word_next = 1; + run_clock(); + for (int i = 0; i < 100; i++) { + handle_read_aa(ind); + run_clock(); + my_assert(!mod->word_last, "too many reads"); + } + mod->word_rst = 1; + run_clock(); + mod->word_rst = 0; + run_clock(); + + test_aa_read_1(); +} + static void refresh_data() { for (size_t i = 0; i < RAM_WID; i++) { ram_refresh_data[i] = mask_extend(rand(), 20); @@ -108,10 +126,15 @@ static void refresh_data() { int main(int argc, char **argv) { init(argc, argv); + printf("test basic read/write\n"); refresh_data(); test_aa_read_1(); refresh_data(); test_aa_read_1(); + + printf("test resetting\n"); + test_aa_read_interrupted(); + printf("ok\n"); return 0;