upsilon/firmware/rtl/autoapproach/autoapproach.v

130 lines
2.7 KiB
Coq
Raw Normal View History

2022-12-28 14:32:35 -05:00
/* Autoapproach module. This module applies a waveform located in memory
* (and copied into Block RAM). This waveform is arbitrary but of fixed
* length.
2023-01-22 23:43:51 -05:00
* time in between sent sample, total period 10-50ms
2022-12-28 14:32:35 -05:00
*/
module autoapproach #(
2023-01-22 23:43:51 -05:00
parameter DAC_WID = 24,
parameter DAC_DATA_WID = 20,
2023-01-22 23:58:38 -05:00
parameter ADC_WID = 24,
2023-02-25 16:17:04 -05:00
parameter TIMER_WID = 32,
parameter WORD_WID = 24,
parameter WORD_AMNT_WID = 11,
parameter [WORD_AMNT_WID-1:0] WORD_AMNT = 2047,
parameter RAM_WID = 32,
parameter RAM_WORD_WID = 16,
parameter RAM_WORD_INCR = 2
2022-12-28 14:32:35 -05:00
) (
input clk,
input arm,
output stopped,
output detected,
input polarity,
2023-01-22 23:43:51 -05:00
input [ADC_WID-1:0] setpoint,
2023-01-22 23:58:38 -05:00
input [TIMER_WID-1:0] time_to_wait,
2022-12-28 14:32:35 -05:00
2023-02-25 16:17:04 -05:00
/* User interface */
input refresh_start,
input [RAM_WID-1:0] start_addr,
output reg refresh_finished,
/* RAM interface */
output reg [RAM_WID-1:0] ram_dma_addr,
input [RAM_WORD_WID-1:0] ram_word,
output reg ram_read,
input ram_valid,
2022-12-28 14:32:35 -05:00
/* DAC wires. */
input dac_finished,
output dac_arm,
2023-01-22 23:43:51 -05:00
output [DAC_WID-1:0] dac_out,
2022-12-28 14:32:35 -05:00
input adc_finished,
output adc_arm,
2023-01-22 23:43:51 -05:00
input [ADC_WID-1:0] measurement
2022-12-28 14:32:35 -05:00
);
2023-02-25 16:17:04 -05:00
bram_interface #(
.WORD_WID(WORD_WID),
.WORD_AMNT_WID(WORD_AMNT_WID),
.WORD_AMNT(WORD_AMNT),
.RAM_WID(RAM_WID),
.RAM_WORD_WID(RAM_WORD_WID),
.RAM_WORD_INCR(RAM_WORD_INCR)
) bram (
.clk(clk),
.word(word),
.word_next(word_next),
.word_last(word_last),
.word_ok(word_ok),
.word_rst(word_rst),
.refresh_start(refresh_start),
.start_addr(start_addr),
.refresh_finished(refresh_finished),
.ram_dma_addr(ram_dma_addr),
.ram_word(ram_word),
.ram_read(ram_read),
.ram_valid(ram_valid)
);
2022-12-28 14:32:35 -05:00
localparam WAIT_ON_ARM = 0;
2023-01-22 23:58:38 -05:00
localparam DO_WAIT = 1;
localparam RECV_WORD = 2;
localparam WAIT_ON_DAC = 3;
localparam WAIT_ON_DETECTION = 4;
localparam DETECTED = 5;
2022-12-28 14:32:35 -05:00
reg [2:0] state = WAIT_ON_ARM;
2023-01-22 23:58:38 -05:00
reg [TIMER_WID-1:0] wait_timer = 0;
2022-12-28 14:32:35 -05:00
always @ (posedge clk) case (state)
WAIT_ON_ARM: if (arm) begin
2023-01-22 23:58:38 -05:00
state <= DO_WAIT;
2022-12-28 14:32:35 -05:00
stopped <= 0;
2023-01-22 23:58:38 -05:00
wait_timer <= time_to_wait;
2022-12-28 14:32:35 -05:00
end else begin
stopped <= 1;
word_rst <= 1;
end
2023-01-22 23:58:38 -05:00
DO_WAIT: if (!arm) begin
state <= WAIT_ON_ARM;
end else if (wait_timer == 0) begin
word_next <= 1;
state <= RECV_WORD;
2023-01-27 17:27:20 -05:00
wait_timer <= time_to_wait;
2023-01-22 23:58:38 -05:00
end else begin
wait_timer <= wait_timer - 1;
end
2022-12-28 14:32:35 -05:00
RECV_WORD: if (word_ok) begin
dac_out <= {4'b0001, word};
dac_arm <= 1;
2023-01-22 23:43:51 -05:00
2022-12-28 14:32:35 -05:00
word_next <= 0;
state <= WAIT_ON_DAC;
end
WAIT_ON_DAC: if (dac_finished) begin
dac_arm <= 0;
2023-01-22 23:43:51 -05:00
/* Was the last word read *the* last word? */
if (word_last) begin
2022-12-28 14:32:35 -05:00
state <= WAIT_ON_DETECTION;
2023-01-22 23:43:51 -05:00
adc_arm <= 1;
2022-12-28 14:32:35 -05:00
end else begin
state <= WAIT_ON_ARM;
end
endcase
WAIT_ON_DETECTION: if (adc_finished) begin
2023-01-22 23:43:51 -05:00
if ((polarity && measurement >= setpt) ||
(!polarity && measurement <= setpt)) begin
2022-12-28 14:32:35 -05:00
state <= DETECTED;
detected <= 1;
end
end
DETECTED: if (!arm) begin
state <= WAIT_ON_ARM;
detected <= 0;
end
endcase
endmodule