Files
templates/hdl/logic-analyzer/rtl/axi_logic_analyzer.sv
T

205 lines
12 KiB
Systemverilog

`timescale 1ns/1ps
// Eight asynchronous digital inputs, bounded BRAM capture, AXI4-Lite readout.
// AXI and sampling intentionally share clk: no multi-bit CDC in the control path.
module axi_logic_analyzer #(
parameter integer CLOCK_HZ = 100000000,
parameter integer ADDR_WIDTH = 16
)(
(* X_INTERFACE_INFO = "xilinx.com:signal:clock:1.0 clk CLK", X_INTERFACE_PARAMETER = "ASSOCIATED_BUSIF s_axi, ASSOCIATED_RESET aresetn" *) input wire clk,
(* X_INTERFACE_INFO = "xilinx.com:signal:reset:1.0 aresetn RST", X_INTERFACE_PARAMETER = "POLARITY ACTIVE_LOW" *) input wire aresetn,
input wire [11: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 [11: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,
input wire [7:0] digital_inputs,
output wire [7:0] test_outputs
);
localparam integer DEPTH = 1 << ADDR_WIDTH;
(* ASYNC_REG = "TRUE" *) reg [7:0] input_meta, input_sync;
reg [7:0] previous;
reg busy, done, triggered, previous_valid;
reg [2:0] settle;
reg [31:0] count_cfg, captured;
reg [15:0] divider, ticks;
reg [7:0] level_mask, level_value, edge_mask, edge_value;
reg [ADDR_WIDTH-1:0] read_index;
reg [15:0] test_divider, test_ticks, test_phase;
reg [8:0] test_duty;
reg [1:0] test_enable;
reg vf_start, vf_off, vf_stop, vf_apply, vf_heartbeat;
reg signed [31:0] vf_target_cfg;
reg [31:0] vf_up, vf_down;
reg [15:0] vf_top, vf_dead, vf_mod;
wire vf_enabled, vf_fault;
wire signed [31:0] vf_actual, vf_target;
wire [5:0] vf_gates;
localparam integer VF_MAX_STEP=21474836; // 500 Hz at a 100 kHz DDS tick.
wire vf_valid=(CLOCK_HZ%100000==0 && vf_target_cfg>=-VF_MAX_STEP && vf_target_cfg<=VF_MAX_STEP &&
vf_up>=1 && vf_up<=VF_MAX_STEP && vf_down>=1 && vf_down<=VF_MAX_STEP &&
vf_top>=CLOCK_HZ/100000 && vf_top<=CLOCK_HZ/2000 && vf_top>=4 &&
vf_dead>=1 && vf_dead<=vf_top/4 && vf_mod<=29491);
vfd_pwm #(.CLOCK_HZ(CLOCK_HZ)) vfd (
.clk(clk), .aresetn(aresetn), .start(vf_start), .off(vf_off),
.ramp_stop(vf_stop), .apply_target(vf_apply), .heartbeat(vf_heartbeat),
.target_cfg(vf_target_cfg), .up_cfg(vf_up), .down_cfg(vf_down),
.top_cfg(vf_top), .dead_cfg(vf_dead), .mod_cfg(vf_mod),
.enabled(vf_enabled), .fault(vf_fault), .actual(vf_actual),
.target(vf_target), .gates(vf_gates)
);
wire [7:0] legacy_outputs;
assign test_outputs = vf_enabled ? {2'b0,vf_gates} : legacy_outputs;
genvar ch;
generate for (ch = 0; ch < 8; ch = ch + 1) begin: pwm
assign legacy_outputs[ch] = (test_enable == 2) ? test_phase[ch] :
((test_enable == 1) && ({1'b0, test_phase[ch +: 8]} < test_duty));
end endgenerate
wire [7:0] read_sample;
wire match_level = ((input_sync & level_mask) == (level_value & level_mask));
wire match_edge = (edge_mask == 0) || (previous_valid &&
(((input_sync ^ previous) & edge_mask) == edge_mask) &&
((input_sync & edge_mask) == (edge_value & edge_mask)));
wire sample_tick = busy && (settle == 0) && (ticks == 0);
wire store_sample = sample_tick && (triggered || (match_level && match_edge));
// Reuse the portable capture memory from templates. No reset on the RAM.
la_capture_buffer #(.SAMPLE_WIDTH(8), .ADDR_WIDTH(ADDR_WIDTH)) memory (
.wr_clk(clk), .wr_en(aresetn && store_sample),
.wr_addr(captured[ADDR_WIDTH-1:0]), .wr_data(input_sync),
.rd_clk(clk), .rd_addr(read_index), .rd_data(read_sample)
);
reg aw_pending, w_pending;
reg [11:0] write_address;
reg [31:0] write_data;
reg [3:0] write_strobes;
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;
always @(posedge clk) begin
if (!aresetn) begin
input_meta <= 0; input_sync <= 0; previous <= 0;
busy <= 0; done <= 0; triggered <= 0; previous_valid <= 0;
settle <= 0; ticks <= 0; captured <= 0;
count_cfg <= DEPTH; divider <= 0;
level_mask <= 0; level_value <= 0; edge_mask <= 0; edge_value <= 0;
read_index <= 0; aw_pending <= 0; w_pending <= 0;
test_divider <= 0; test_ticks <= 0; test_phase <= 0;
test_duty <= 128; test_enable <= 0;
vf_start<=0; vf_off<=0; vf_stop<=0; vf_apply<=0; vf_heartbeat<=0;
vf_target_cfg<=0; vf_up<=1; vf_down<=1;
vf_top<=2500; vf_dead<=100; vf_mod<=16384;
write_address <= 0; write_data <= 0; write_strobes <= 0;
s_axi_bvalid <= 0; s_axi_bresp <= 0;
s_axi_rvalid <= 0; s_axi_rresp <= 0; s_axi_rdata <= 0;
end else begin
vf_start<=0; vf_off<=0; vf_stop<=0; vf_apply<=0; vf_heartbeat<=0;
input_meta <= digital_inputs; input_sync <= input_meta;
if (!test_enable) begin test_ticks <= 0; test_phase <= 0; end
else if (test_ticks == 0) begin test_ticks <= test_divider; test_phase <= test_phase + 1'b1; end
else test_ticks <= test_ticks - 1'b1;
if (busy) begin
if (settle != 0) settle <= settle - 1'b1;
else if (ticks != 0) ticks <= ticks - 1'b1;
else begin
ticks <= divider; previous <= input_sync; previous_valid <= 1;
if (store_sample) begin
triggered <= 1; captured <= captured + 1;
if (captured == count_cfg - 1) begin busy <= 0; done <= 1; end
end
end
end
if (s_axi_bvalid && s_axi_bready) s_axi_bvalid <= 0;
if (s_axi_rvalid && s_axi_rready) s_axi_rvalid <= 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 <= 2;
// Explicit full-word register ABI; zero strobes are a no-op.
if (write_strobes == 0) s_axi_bresp <= 0;
else if (write_strobes == 15) begin
if (write_address >= 'h48 && write_address <= 'h60) begin
case(write_address)
'h48: case(write_data)
1: if(!vf_enabled && vf_valid) begin vf_start<=1; test_enable<=0; s_axi_bresp<=0; end
2: begin vf_off<=1; s_axi_bresp<=0; end
3: begin vf_stop<=1; s_axi_bresp<=0; end
4: begin vf_heartbeat<=1; s_axi_bresp<=0; end
5: if(vf_enabled && vf_target_cfg>=-VF_MAX_STEP && vf_target_cfg<=VF_MAX_STEP) begin vf_apply<=1; s_axi_bresp<=0; end
default: ;
endcase
'h4c: if($signed(write_data)>=-VF_MAX_STEP && $signed(write_data)<=VF_MAX_STEP) begin vf_target_cfg<=write_data; s_axi_bresp<=0; end
'h50: if(!vf_enabled && write_data>=1 && write_data<=VF_MAX_STEP) begin vf_up<=write_data; s_axi_bresp<=0; end
'h54: if(!vf_enabled && write_data>=1 && write_data<=VF_MAX_STEP) begin vf_down<=write_data; s_axi_bresp<=0; end
'h58: if(!vf_enabled && write_data>=4 && write_data<=65535) begin vf_top<=write_data[15:0]; s_axi_bresp<=0; end
'h5c: if(!vf_enabled && write_data>=1 && write_data<=65535) begin vf_dead<=write_data[15:0]; s_axi_bresp<=0; end
'h60: if(!vf_enabled && write_data<=29491) begin vf_mod<=write_data[15:0]; s_axi_bresp<=0; end
default: ;
endcase
end else if (write_address == 'h28 && write_data == 2) begin
busy <= 0; done <= 0; triggered <= 0; captured <= 0;
s_axi_bresp <= 0;
end else if (!busy) begin
case (write_address)
'h10: if (write_data >= 1 && write_data <= DEPTH) begin count_cfg <= write_data; s_axi_bresp <= 0; end
'h14: if (write_data <= 65535) begin divider <= write_data[15:0]; s_axi_bresp <= 0; end
'h18: if (write_data <= 255) begin level_mask <= write_data[7:0]; s_axi_bresp <= 0; end
'h1c: if (write_data <= 255) begin level_value <= write_data[7:0]; s_axi_bresp <= 0; end
'h20: if (write_data <= 255) begin edge_mask <= write_data[7:0]; s_axi_bresp <= 0; end
'h24: if (write_data <= 255) begin edge_value <= write_data[7:0]; s_axi_bresp <= 0; end
'h28: if (write_data == 1) begin
busy <= 1; done <= 0; triggered <= 0; captured <= 0;
ticks <= 0; settle <= 3; previous_valid <= 0; s_axi_bresp <= 0;
end
'h2c: if (write_data < DEPTH) begin read_index <= write_data[ADDR_WIDTH-1:0]; s_axi_bresp <= 0; end
'h38: if (write_data <= 65535) begin test_divider <= write_data[15:0]; s_axi_bresp <= 0; end
'h3c: if (write_data <= 256) begin test_duty <= write_data[8:0]; s_axi_bresp <= 0; end
'h40: if (write_data <= 2 && (!vf_enabled || write_data==0)) begin test_enable <= write_data[1:0]; s_axi_bresp <= 0; end
default: ;
endcase
end
end
end
if (s_axi_arvalid && s_axi_arready) begin
s_axi_rvalid <= 1; s_axi_rresp <= 0; s_axi_rdata <= 0;
case (s_axi_araddr)
'h00: s_axi_rdata <= 32'h31414c5a; // little-endian "ZLA1"
'h04: s_axi_rdata <= {29'b0, triggered, done, busy};
'h08: s_axi_rdata <= CLOCK_HZ;
'h0c: s_axi_rdata <= DEPTH;
'h10: s_axi_rdata <= count_cfg;
'h14: s_axi_rdata <= {16'b0, divider};
'h18: s_axi_rdata <= {24'b0, level_mask};
'h1c: s_axi_rdata <= {24'b0, level_value};
'h20: s_axi_rdata <= {24'b0, edge_mask};
'h24: s_axi_rdata <= {24'b0, edge_value};
'h2c: s_axi_rdata <= read_index;
'h30: if (done && read_index < captured) s_axi_rdata <= {24'b0, read_sample}; else s_axi_rresp <= 2;
'h34: s_axi_rdata <= captured;
'h38: s_axi_rdata <= {16'b0, test_divider};
'h3c: s_axi_rdata <= {23'b0, test_duty};
'h40: s_axi_rdata <= {30'b0, test_enable};
'h44: s_axi_rdata <= 32'h3146565a; // ZVF1 capability
'h4c: s_axi_rdata <= vf_target_cfg;
'h50: s_axi_rdata <= vf_up;
'h54: s_axi_rdata <= vf_down;
'h58: s_axi_rdata <= {16'b0,vf_top};
'h5c: s_axi_rdata <= {16'b0,vf_dead};
'h60: s_axi_rdata <= {16'b0,vf_mod};
'h64: s_axi_rdata <= {29'b0,(vf_actual!=vf_target),vf_fault,vf_enabled};
'h68: s_axi_rdata <= vf_actual;
'h6c: s_axi_rdata <= 100000;
'h70: s_axi_rdata <= vf_target;
default: s_axi_rresp <= 2;
endcase
end
end
end
endmodule