Files
templates/hdl/scope-acquisition/rtl/axi_scope_regs.sv
T

248 lines
11 KiB
Systemverilog

`timescale 1ns/1ps
// Single-clock AXI4-Lite register bank. All *_live inputs must already be
// coherent in clk; this block does not synchronize ADC-domain status buses.
module axi_scope_regs #(
parameter integer ADDR_WIDTH = 12,
parameter [31:0] REFERENCE_RATE_HZ = 32'd100000000,
parameter integer CAPTURE_ADDR_BITS = 8
) (
input wire clk,
input wire aresetn,
input wire [ADDR_WIDTH-1:0] s_axi_awaddr,
input wire [2:0] s_axi_awprot,
input wire s_axi_awvalid,
output wire s_axi_awready,
input wire [31:0] s_axi_wdata,
input wire [3:0] s_axi_wstrb,
input wire s_axi_wvalid,
output wire s_axi_wready,
output reg [1:0] s_axi_bresp,
output reg s_axi_bvalid,
input wire s_axi_bready,
input wire [ADDR_WIDTH-1:0] s_axi_araddr,
input wire [2:0] s_axi_arprot,
input wire s_axi_arvalid,
output wire s_axi_arready,
output reg [31:0] s_axi_rdata,
output reg [1:0] s_axi_rresp,
output reg s_axi_rvalid,
input wire s_axi_rready,
output reg [2:0] cfg_source,
output reg [1:0] cfg_mode,
output reg cfg_falling,
output reg [9:0] cfg_level,
output reg [9:0] cfg_hysteresis,
output reg [31:0] cfg_pre,
output reg [31:0] cfg_post,
output reg [31:0] cfg_auto_timeout,
output reg arm_pulse,
output reg stop_pulse,
output reg soft_reset_pulse,
output reg snapshot_pulse,
input wire [31:0] status_live,
input wire [63:0] trigger_index_live,
input wire [63:0] first_index_live,
input wire [31:0] record_length_live,
input wire [63:0] sample_count_live,
input wire [31:0] fifo1_overflow_live,
input wire [31:0] fifo2_overflow_live,
input wire [31:0] trigger_count_live
);
localparam [1:0] OKAY = 2'b00, SLVERR = 2'b10;
localparam [31:0] CAPTURE_DEPTH = 32'd1 << CAPTURE_ADDR_BITS;
reg aw_pending, w_pending;
reg [ADDR_WIDTH-1:0] write_address;
reg [31:0] write_data;
reg [3:0] write_strobes;
reg [63:0] trigger_snapshot, first_snapshot, samples_snapshot;
reg [31:0] length_snapshot, status_snapshot;
reg [31:0] fifo1_snapshot, fifo2_snapshot, trigger_count_snapshot;
reg [31:0] merged_value;
assign s_axi_awready = aresetn && !aw_pending && !s_axi_bvalid;
assign s_axi_wready = aresetn && !w_pending && !s_axi_bvalid;
assign s_axi_arready = aresetn && !s_axi_rvalid;
initial begin
if (ADDR_WIDTH < 8 || CAPTURE_ADDR_BITS < 1 || CAPTURE_ADDR_BITS > 30)
$error("axi_scope_regs requires ADDR_WIDTH >= 8 and CAPTURE_ADDR_BITS in 1..30");
end
function automatic [31:0] merge_bytes(input [31:0] old_value,
input [31:0] new_value, input [3:0] byte_strobes);
integer byte_number;
begin
merge_bytes = old_value;
for (byte_number = 0; byte_number < 4; byte_number = byte_number + 1)
if (byte_strobes[byte_number])
merge_bytes[byte_number*8 +: 8] = new_value[byte_number*8 +: 8];
end
endfunction
// Read responses capture the addressed value at the AR handshake and
// retain data/response until RREADY. Snapshot words are a separate bank.
function automatic [33:0] read_value(input [ADDR_WIDTH-1:0] address);
begin
read_value = {SLVERR, 32'd0};
if (address[1:0] == 0) begin
case (address)
'h00: read_value = {OKAY, 32'h53434f50};
'h04: read_value = {OKAY, 32'h00010000};
'h08: read_value = {OKAY, 32'd0};
'h0c: read_value = {OKAY, status_live};
'h10: read_value = {OKAY, REFERENCE_RATE_HZ};
'h14: read_value = {OKAY, 32'h0000000f};
'h18: read_value = {OKAY, 32'h00000007};
'h1c: read_value = {OKAY, CAPTURE_DEPTH};
'h20: read_value = {OKAY, 29'd0, cfg_source};
'h24: read_value = {OKAY, 22'd0, cfg_level};
'h28: read_value = {OKAY, 30'd0, cfg_mode};
'h2c: read_value = {OKAY, 31'd0, cfg_falling};
'h30: read_value = {OKAY, cfg_pre};
'h34: read_value = {OKAY, cfg_post};
'h38: read_value = {OKAY, 22'd0, cfg_hysteresis};
'h3c: read_value = {OKAY, cfg_auto_timeout};
'h50: read_value = {OKAY, trigger_snapshot[31:0]};
'h54: read_value = {OKAY, trigger_snapshot[63:32]};
'h58: read_value = {OKAY, first_snapshot[31:0]};
'h5c: read_value = {OKAY, first_snapshot[63:32]};
'h60: read_value = {OKAY, length_snapshot};
'h64: read_value = {OKAY, status_snapshot};
'h68: read_value = {OKAY, samples_snapshot[31:0]};
'h6c: read_value = {OKAY, samples_snapshot[63:32]};
'h70: read_value = {OKAY, fifo1_snapshot};
'h74: read_value = {OKAY, fifo2_snapshot};
'h78: read_value = {OKAY, trigger_count_snapshot};
default: read_value = {SLVERR, 32'd0};
endcase
end
end
endfunction
always @(posedge clk) begin
if (!aresetn) begin
aw_pending <= 0;
w_pending <= 0;
write_address <= 0;
write_data <= 0;
write_strobes <= 0;
s_axi_bvalid <= 0;
s_axi_bresp <= OKAY;
cfg_source <= 0;
cfg_level <= 512;
cfg_mode <= 1;
cfg_falling <= 0;
cfg_pre <= 64;
cfg_post <= 64;
cfg_hysteresis <= 4;
cfg_auto_timeout <= 1000000;
arm_pulse <= 0;
stop_pulse <= 0;
soft_reset_pulse <= 0;
snapshot_pulse <= 0;
trigger_snapshot <= 0;
first_snapshot <= 0;
samples_snapshot <= 0;
length_snapshot <= 0;
status_snapshot <= 0;
fifo1_snapshot <= 0;
fifo2_snapshot <= 0;
trigger_count_snapshot <= 0;
end else begin
arm_pulse <= 0;
stop_pulse <= 0;
soft_reset_pulse <= 0;
snapshot_pulse <= 0;
if (s_axi_bvalid && s_axi_bready) s_axi_bvalid <= 0;
if (s_axi_awvalid && s_axi_awready) begin
write_address <= s_axi_awaddr;
aw_pending <= 1;
end
if (s_axi_wvalid && s_axi_wready) begin
write_data <= s_axi_wdata;
write_strobes <= s_axi_wstrb;
w_pending <= 1;
end
if (aw_pending && w_pending && !s_axi_bvalid) begin
aw_pending <= 0;
w_pending <= 0;
s_axi_bvalid <= 1;
s_axi_bresp <= OKAY;
if (write_address[1:0] != 0) begin
s_axi_bresp <= SLVERR;
end else begin
case (write_address)
'h08: begin
// Without low-byte enable this command is a no-op.
if (write_strobes[0]) begin
merged_value = merge_bytes(0, write_data, write_strobes);
case (merged_value)
32'd0: begin end
32'd1: arm_pulse <= 1;
32'd2: stop_pulse <= 1;
32'd4: soft_reset_pulse <= 1;
32'd8: begin
snapshot_pulse <= 1;
trigger_snapshot <= trigger_index_live;
first_snapshot <= first_index_live;
samples_snapshot <= sample_count_live;
length_snapshot <= record_length_live;
status_snapshot <= status_live;
fifo1_snapshot <= fifo1_overflow_live;
fifo2_snapshot <= fifo2_overflow_live;
trigger_count_snapshot <= trigger_count_live;
end
default: s_axi_bresp <= SLVERR;
endcase
end
end
'h20: begin
merged_value = merge_bytes({29'd0, cfg_source}, write_data, write_strobes);
if (merged_value <= 4) cfg_source <= merged_value[2:0];
else s_axi_bresp <= SLVERR;
end
'h24: begin
merged_value = merge_bytes({22'd0, cfg_level}, write_data, write_strobes);
if (merged_value <= 1023) cfg_level <= merged_value[9:0];
else s_axi_bresp <= SLVERR;
end
'h28: begin
merged_value = merge_bytes({30'd0, cfg_mode}, write_data, write_strobes);
if (merged_value <= 2) cfg_mode <= merged_value[1:0];
else s_axi_bresp <= SLVERR;
end
'h2c: begin
merged_value = merge_bytes({31'd0, cfg_falling}, write_data, write_strobes);
if (merged_value <= 1) cfg_falling <= merged_value[0];
else s_axi_bresp <= SLVERR;
end
'h30: cfg_pre <= merge_bytes(cfg_pre, write_data, write_strobes);
'h34: cfg_post <= merge_bytes(cfg_post, write_data, write_strobes);
'h38: begin
merged_value = merge_bytes({22'd0, cfg_hysteresis}, write_data, write_strobes);
if (merged_value <= 1023) cfg_hysteresis <= merged_value[9:0];
else s_axi_bresp <= SLVERR;
end
'h3c: cfg_auto_timeout <= merge_bytes(cfg_auto_timeout, write_data, write_strobes);
default: s_axi_bresp <= SLVERR;
endcase
end
end
end
end
always @(posedge clk) begin
if (!aresetn) begin
s_axi_rvalid <= 0;
s_axi_rdata <= 0;
s_axi_rresp <= OKAY;
end else begin
if (s_axi_rvalid && s_axi_rready) s_axi_rvalid <= 0;
if (s_axi_arvalid && s_axi_arready) begin
{s_axi_rresp, s_axi_rdata} <= read_value(s_axi_araddr);
s_axi_rvalid <= 1;
end
end
end
endmodule