Added missing signal modules
This commit is contained in:
91
cores/signal/sd_adc_q15/rtl/rc_alpha_q15.vh
Normal file
91
cores/signal/sd_adc_q15/rtl/rc_alpha_q15.vh
Normal file
@@ -0,0 +1,91 @@
|
||||
// rc_alpha_q15.vh
|
||||
// Plain Verilog-2001 constant function: R(ohm), C(pF), Fs(Hz) -> alpha_q15 (Q1.15)
|
||||
// Uses fixed-point approximation: 1 - exp(-x) ≈ x - x^2/2 + x^3/6, where x = 1/(Fs*R*C)
|
||||
// All integer math; suitable for elaboration-time constant folding (e.g., XST).
|
||||
|
||||
`ifndef RC_ALPHA_Q15_VH
|
||||
`define RC_ALPHA_Q15_VH
|
||||
|
||||
function integer alpha_q15_from_rc;
|
||||
input integer R_OHM; // ohms
|
||||
input integer C_PF; // picofarads
|
||||
input integer FS_HZ; // Hz
|
||||
|
||||
// Choose QN for x. N=24 is a good balance for accuracy/width.
|
||||
integer N;
|
||||
|
||||
// We'll keep everything as unsigned vectors; inputs copied into vectors first.
|
||||
reg [63:0] R_u, C_u, FS_u;
|
||||
|
||||
// x = 1 / (Fs * R * C) with C in pF -> x = 1e12 / (Fs*R*C_pf)
|
||||
// x_qN = round( x * 2^N ) = round( (1e12 << N) / denom )
|
||||
reg [127:0] NUM_1E12_SLLN; // big enough for 1e12 << N
|
||||
reg [127:0] DENOM; // Fs*R*C
|
||||
reg [127:0] X_qN; // x in QN
|
||||
|
||||
// Powers
|
||||
reg [255:0] X2; // x^2 in Q(2N)
|
||||
reg [383:0] X3; // x^3 in Q(3N)
|
||||
|
||||
integer term1_q15;
|
||||
integer term2_q15;
|
||||
integer term3_q15;
|
||||
integer acc;
|
||||
|
||||
begin
|
||||
N = 24;
|
||||
|
||||
// Copy integer inputs into 64-bit vectors (no bit-slicing of integers)
|
||||
R_u = R_OHM[31:0];
|
||||
C_u = C_PF[31:0];
|
||||
FS_u = FS_HZ[31:0];
|
||||
|
||||
// Denominator = Fs * R * C_pf (fits in < 2^64 for typical values)
|
||||
DENOM = 128'd0;
|
||||
DENOM = FS_u;
|
||||
DENOM = DENOM * R_u;
|
||||
DENOM = DENOM * C_u;
|
||||
|
||||
// // Guard: avoid divide by zero
|
||||
// if (DENOM == 0) begin
|
||||
// alpha_q15_from_rc = 0;
|
||||
// disable alpha_q15_from_rc;
|
||||
// end
|
||||
|
||||
// Numerator = (1e12 << N). 1e12 * 2^24 ≈ 1.6777e19 (fits in 2^64..2^65),
|
||||
// so use 128 bits to be safe.
|
||||
NUM_1E12_SLLN = 128'd1000000000000 << N;
|
||||
|
||||
// x_qN = rounded division
|
||||
X_qN = (NUM_1E12_SLLN + (DENOM >> 1)) / DENOM;
|
||||
|
||||
// Powers
|
||||
X2 = X_qN * X_qN;
|
||||
X3 = X2 * X_qN;
|
||||
|
||||
// Convert terms to Q1.15:
|
||||
// term1 = x -> shift from QN to Q15
|
||||
term1_q15 = (X_qN >> (N - 15)) & 16'hFFFF;
|
||||
|
||||
// term2 = x^2 / 2 -> Q(2N) to Q15 and /2
|
||||
term2_q15 = (X2 >> (2*N - 15 + 1)) & 16'hFFFF;
|
||||
|
||||
// term3 = x^3 / 6 -> Q(3N) to Q15, then /6 with rounding
|
||||
begin : gen_t3
|
||||
reg [383:0] tmp_q15_wide;
|
||||
reg [383:0] tmp_div6;
|
||||
tmp_q15_wide = (X3 >> (3*N - 15));
|
||||
tmp_div6 = (tmp_q15_wide + 6'd3) / 6;
|
||||
term3_q15 = tmp_div6[15:0];
|
||||
end
|
||||
|
||||
// Combine and clamp
|
||||
acc = term1_q15 - term2_q15 + term3_q15;
|
||||
if (acc < 0) acc = 0;
|
||||
else if (acc > 16'h7FFF) acc = 16'h7FFF;
|
||||
|
||||
alpha_q15_from_rc = acc;
|
||||
end
|
||||
endfunction
|
||||
|
||||
`endif
|
||||
55
cores/signal/sd_adc_q15/rtl/rcmodel_q15.v
Normal file
55
cores/signal/sd_adc_q15/rtl/rcmodel_q15.v
Normal file
@@ -0,0 +1,55 @@
|
||||
`timescale 1ns/1ps
|
||||
|
||||
// =============================================================================
|
||||
// RC model to convert sigma delta samples to Q1.15
|
||||
// Models the RC circuit on the outside of the FPGA
|
||||
// Uses: Yn+1 = Yn + (sd - Yn)*(1-exp(-T/RC))
|
||||
// parameters:
|
||||
// -- alpha_q15 : the 1-exp(-T/RC), defaults to R=3k3, C=220p and T=1/15MHz
|
||||
// rounded to only use two bits (0b3b -> 0b00), the less
|
||||
// bits the better
|
||||
// inout:
|
||||
// -- i_clk : input clock
|
||||
// -- i_rst_n : reset signal
|
||||
// -- i_sd_sample : 1 bit sample output from sd sampler
|
||||
// -- o_sample_q15 : output samples in q.15
|
||||
// =============================================================================
|
||||
module rcmodel_q15 #(
|
||||
parameter integer alpha_q15 = 16'sh0b00
|
||||
)(
|
||||
input wire i_clk,
|
||||
input wire i_rst_n,
|
||||
input wire i_sd_sample,
|
||||
output wire [15:0] o_sample_q15
|
||||
);
|
||||
reg signed [15:0] y_q15;
|
||||
wire signed [15:0] sd_q15 = i_sd_sample ? 16'sh7fff : 16'sh0000;
|
||||
wire signed [15:0] e_q15 = sd_q15 - y_q15;
|
||||
// wire signed [31:0] prod_q30 = $signed(e_q15) * $signed(alpha_q15);
|
||||
wire signed [31:0] prod_q30;
|
||||
// Use shift-add algorithm for multiplication
|
||||
mul_const_shiftadd #(
|
||||
.C($signed(alpha_q15)),
|
||||
.IN_W(16),
|
||||
.OUT_W(32)
|
||||
) alpha_times_e (
|
||||
.i_x(e_q15),
|
||||
.o_y(prod_q30)
|
||||
);
|
||||
wire signed [15:0] y_next_q15 = y_q15 + (prod_q30>>>15);
|
||||
|
||||
// clamp to [0, 0x7FFF] (keeps signal view tidy)
|
||||
function signed [15:0] clamp01_q15(input signed [15:0] v);
|
||||
if (v < 16'sd0000) clamp01_q15 = 16'sd0000;
|
||||
else if (v > 16'sh7FFF) clamp01_q15 = 16'sh7FFF;
|
||||
else clamp01_q15 = v;
|
||||
endfunction
|
||||
|
||||
always @(posedge i_clk or negedge i_rst_n) begin
|
||||
if (!i_rst_n) y_q15 <= 16'sd0000;
|
||||
else y_q15 <= clamp01_q15(y_next_q15);
|
||||
end
|
||||
|
||||
assign o_sample_q15 = y_q15;
|
||||
|
||||
endmodule
|
||||
57
cores/signal/sd_adc_q15/rtl/sd_adc_q15.v
Normal file
57
cores/signal/sd_adc_q15/rtl/sd_adc_q15.v
Normal file
@@ -0,0 +1,57 @@
|
||||
module sd_adc_q15 #(
|
||||
parameter integer R_OHM = 3300,
|
||||
parameter integer C_PF = 220
|
||||
)(
|
||||
input wire i_clk_15,
|
||||
input wire i_rst_n,
|
||||
|
||||
input wire i_adc_a,
|
||||
input wire i_adc_b,
|
||||
output wire o_adc,
|
||||
|
||||
output wire signed [15:0] o_signal_q15,
|
||||
output wire o_signal_valid
|
||||
);
|
||||
`include "rc_alpha_q15.vh"
|
||||
|
||||
wire sd_signal;
|
||||
wire signed [15:0] raw_sample_biased;
|
||||
wire signed [15:0] raw_sample_q15;
|
||||
wire signed [15:0] lpf_sample_q15;
|
||||
|
||||
sd_sampler sd_sampler(
|
||||
.i_clk(i_clk_15),
|
||||
.i_a(i_adc_a), .i_b(i_adc_b),
|
||||
.o_sample(sd_signal)
|
||||
);
|
||||
assign o_adc = sd_signal;
|
||||
|
||||
localparam integer alpha_q15_int = alpha_q15_from_rc(R_OHM, C_PF, 15000000);
|
||||
localparam signed [15:0] alpha_q15 = alpha_q15_int[15:0];
|
||||
localparam signed [15:0] alpha_q15_top = alpha_q15 & 16'hff00;
|
||||
rcmodel_q15 #(
|
||||
.alpha_q15(alpha_q15_top)
|
||||
) rc_model (
|
||||
.i_clk(i_clk_15), .i_rst_n(i_rst_n),
|
||||
.i_sd_sample(sd_signal),
|
||||
.o_sample_q15(raw_sample_q15)
|
||||
);
|
||||
|
||||
lpf_iir_q15_k #(
|
||||
.K(10)
|
||||
) lpf (
|
||||
.i_clk(i_clk_15), .i_rst_n(i_rst_n),
|
||||
.i_x_q15(raw_sample_q15),
|
||||
.o_y_q15(lpf_sample_q15)
|
||||
);
|
||||
|
||||
decimate_by_r_q15 #(
|
||||
.R(375), // 15MHz/375 = 40KHz
|
||||
.CNT_W(10)
|
||||
) decimate (
|
||||
.i_clk(i_clk_15), .i_rst_n(i_rst_n),
|
||||
.i_valid(1'b1), .i_q15(lpf_sample_q15),
|
||||
.o_valid(o_signal_valid), .o_q15(o_signal_q15)
|
||||
);
|
||||
|
||||
endmodule
|
||||
18
cores/signal/sd_adc_q15/rtl/sd_sampler.v
Normal file
18
cores/signal/sd_adc_q15/rtl/sd_sampler.v
Normal file
@@ -0,0 +1,18 @@
|
||||
module sd_sampler(
|
||||
input wire i_clk,
|
||||
input wire i_a,
|
||||
input wire i_b,
|
||||
output wire o_sample
|
||||
);
|
||||
|
||||
wire comp_out;
|
||||
lvds_comparator comp (
|
||||
.a(i_a), .b(i_b), .o(comp_out)
|
||||
);
|
||||
|
||||
reg registered_comp_out;
|
||||
always @(posedge i_clk)
|
||||
registered_comp_out <= comp_out;
|
||||
assign o_sample = registered_comp_out;
|
||||
|
||||
endmodule
|
||||
57
cores/signal/sd_adc_q15/sd_adc_q15.core
Normal file
57
cores/signal/sd_adc_q15/sd_adc_q15.core
Normal file
@@ -0,0 +1,57 @@
|
||||
CAPI=2:
|
||||
|
||||
name: joppeb:signal:sd_adc_q15:1.0
|
||||
description: Sigma-delta ADC front-end with Q1.15 output
|
||||
|
||||
filesets:
|
||||
rtl_common:
|
||||
depend:
|
||||
- joppeb:primitive:lvds_comparator
|
||||
- joppeb:signal:lpf_iir_q15_k
|
||||
- joppeb:signal:decimate_by_r_q15
|
||||
- joppeb:util:mul_const
|
||||
files:
|
||||
- rtl/rc_alpha_q15.vh:
|
||||
is_include_file: true
|
||||
- rtl/rcmodel_q15.v
|
||||
- rtl/sd_adc_q15.v
|
||||
file_type: verilogSource
|
||||
rtl_sampler:
|
||||
files:
|
||||
- rtl/sd_sampler.v
|
||||
file_type: verilogSource
|
||||
sim_sampler:
|
||||
files:
|
||||
- sim/sd_sampler.v
|
||||
file_type: verilogSource
|
||||
tb:
|
||||
files:
|
||||
- tb/tb_sd_adc_q15.v
|
||||
file_type: verilogSource
|
||||
|
||||
targets:
|
||||
default:
|
||||
filesets:
|
||||
- rtl_common
|
||||
- rtl_sampler
|
||||
toplevel: sd_adc_q15
|
||||
parameters:
|
||||
- R_OHM
|
||||
- C_PF
|
||||
sim:
|
||||
default_tool: icarus
|
||||
filesets:
|
||||
- rtl_common
|
||||
- sim_sampler
|
||||
- tb
|
||||
toplevel: tb_sd_adc_q15
|
||||
|
||||
parameters:
|
||||
R_OHM:
|
||||
datatype: int
|
||||
description: RC filter resistor value in ohms
|
||||
paramtype: vlogparam
|
||||
C_PF:
|
||||
datatype: int
|
||||
description: RC filter capacitor value in pF
|
||||
paramtype: vlogparam
|
||||
111
cores/signal/sd_adc_q15/sim/sd_sampler.v
Normal file
111
cores/signal/sd_adc_q15/sim/sd_sampler.v
Normal file
@@ -0,0 +1,111 @@
|
||||
`timescale 1ns/1ps
|
||||
|
||||
// =============================================================================
|
||||
// Sigma-Delta sampler
|
||||
// Simulates an RC circuit between o_sample and i_b and i_a sine at i_a
|
||||
// =============================================================================
|
||||
module sd_sampler(
|
||||
input wire i_clk,
|
||||
input wire i_a,
|
||||
input wire i_b,
|
||||
output wire o_sample
|
||||
);
|
||||
|
||||
// Sine source (i_a input / P)
|
||||
parameter real F_HZ = 5000; // input sine frequency (1 kHz)
|
||||
parameter real AMP = 1.5; // sine amplitude (V)
|
||||
parameter real VCM = 1.65; // common-mode (V), centered in 0..3.3V
|
||||
|
||||
// Comparator behavior
|
||||
parameter real VTH = 0.0; // threshold on (vp - vn)
|
||||
parameter real VHYST = 0.05; // symmetric hysteresis half-width (V)
|
||||
parameter integer ADD_HYST = 0; // 1 to enable hysteresis
|
||||
|
||||
// 1-bit DAC rails (feedback into RC)
|
||||
parameter real VLOW = 0.0; // DAC 0 (V)
|
||||
parameter real VHIGH = 3.3; // DAC 1 (V)
|
||||
|
||||
// RC filter (i_b input / N)
|
||||
parameter real R_OHMS = 3300.0; // 3.3k
|
||||
parameter real C_FARADS = 220e-12; // 220 pF
|
||||
|
||||
// Integration step (ties to `timescale`)
|
||||
parameter integer TSTEP_NS = 10; // sim step in ns (choose << tau)
|
||||
|
||||
// ===== Internal state (simulation only) =====
|
||||
real vp, vn; // comparator i_a/i_b inputs
|
||||
real v_rc; // RC node voltage (== vn)
|
||||
real v_dac; // DAC output voltage from o_sample
|
||||
real t_s; // time in seconds
|
||||
real dt_s; // step in seconds
|
||||
real tau_s; // R*C time constant in seconds
|
||||
real two_pi;
|
||||
reg q; // comparator latched output (pre-delay)
|
||||
reg out;
|
||||
reg sampler;
|
||||
|
||||
initial sampler <= 1'b0;
|
||||
always @(posedge i_clk) begin
|
||||
sampler <= out;
|
||||
end
|
||||
assign o_sample = sampler;
|
||||
|
||||
|
||||
// Helper task: update comparator with optional hysteresis
|
||||
task automatic comp_update;
|
||||
real diff;
|
||||
begin
|
||||
diff = (vp - vn);
|
||||
|
||||
if (ADD_HYST != 0) begin
|
||||
// simple symmetric hysteresis around VTH
|
||||
if (q && (diff < (VTH - VHYST))) q = 1'b0;
|
||||
else if (!q && (diff > (VTH + VHYST))) q = 1'b1;
|
||||
// else hold
|
||||
end else begin
|
||||
q = (diff > VTH) ? 1'b1 : 1'b0;
|
||||
end
|
||||
end
|
||||
endtask
|
||||
|
||||
initial begin
|
||||
// Init constants
|
||||
two_pi = 6.283185307179586;
|
||||
tau_s = R_OHMS * C_FARADS; // ~7.26e-7 s
|
||||
dt_s = TSTEP_NS * 1.0e-9;
|
||||
|
||||
// Init states
|
||||
t_s = 0.0;
|
||||
q = 1'b0; // start low
|
||||
out = 1'b0;
|
||||
v_dac= VLOW;
|
||||
v_rc = (VHIGH + VLOW)/2.0; // start mid-rail to reduce start-up transient
|
||||
vn = v_rc;
|
||||
vp = VCM;
|
||||
|
||||
// Main sim loop
|
||||
forever begin
|
||||
#(TSTEP_NS); // advance discrete time step
|
||||
t_s = t_s + dt_s;
|
||||
|
||||
// 1) Update DAC from previous comparator state
|
||||
v_dac = sampler ? VHIGH : VLOW;
|
||||
|
||||
// 2) RC low-pass driven by DAC: Euler step
|
||||
// dv = (v_dac - v_rc) * dt/tau
|
||||
v_rc = v_rc + (v_dac - v_rc) * (dt_s / tau_s);
|
||||
vn = v_rc;
|
||||
|
||||
// 3) Input sine on i_a
|
||||
vp = VCM + AMP * $sin(two_pi * F_HZ * t_s);
|
||||
|
||||
// 4) Comparator decision (with optional hysteresis)
|
||||
comp_update();
|
||||
|
||||
// 5) Output with propagation delay
|
||||
out = q;
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
endmodule
|
||||
36
cores/signal/sd_adc_q15/tb/tb_sd_adc_q15.v
Normal file
36
cores/signal/sd_adc_q15/tb/tb_sd_adc_q15.v
Normal file
@@ -0,0 +1,36 @@
|
||||
`timescale 1ns/1ps
|
||||
|
||||
module tb_sd_adc_q15();
|
||||
// Clock and reset generation
|
||||
reg clk;
|
||||
reg resetn;
|
||||
initial clk <= 1'b0;
|
||||
initial resetn <= 1'b0;
|
||||
always #6.667 clk <= !clk;
|
||||
initial #40 resetn <= 1'b1;
|
||||
|
||||
// Default run
|
||||
initial begin
|
||||
$dumpfile("out.vcd");
|
||||
$dumpvars;
|
||||
#2_000_000
|
||||
$finish;
|
||||
end;
|
||||
|
||||
wire sd_a;
|
||||
wire sd_b;
|
||||
wire sd_o;
|
||||
wire signed [15:0] decimated_q15;
|
||||
wire decimated_valid;
|
||||
|
||||
sd_adc_q15 #(
|
||||
.R_OHM(3300),
|
||||
.C_PF(220)
|
||||
) dut(
|
||||
.i_clk_15(clk), .i_rst_n(resetn),
|
||||
.i_adc_a(sd_a), .i_adc_b(sd_b), .o_adc(sd_o),
|
||||
.o_signal_q15(decimated_q15),
|
||||
.o_signal_valid(decimated_valid)
|
||||
);
|
||||
|
||||
endmodule
|
||||
Reference in New Issue
Block a user