|
| 1 | +#ifndef SERDES_AMS_ADAPTION_H |
| 2 | +#define SERDES_AMS_ADAPTION_H |
| 3 | + |
| 4 | +#include <systemc> |
| 5 | +#include <vector> |
| 6 | +#include "common/parameters.h" |
| 7 | + |
| 8 | +namespace serdes { |
| 9 | + |
| 10 | +/** |
| 11 | + * @brief AdaptionDe - DE Domain Adaptive Control Module |
| 12 | + * |
| 13 | + * This module implements the adaptive control algorithms for SerDes link: |
| 14 | + * - AGC (Automatic Gain Control) with PI controller |
| 15 | + * - DFE tap update (LMS/Sign-LMS/NLMS algorithms) |
| 16 | + * - Threshold adaptation |
| 17 | + * - CDR PI controller |
| 18 | + * |
| 19 | + * Features: |
| 20 | + * - Multi-rate scheduling: fast path (CDR PI, threshold) and slow path (AGC, DFE) |
| 21 | + * - Safety mechanisms: freeze on error, rollback to snapshot |
| 22 | + * - Parameter clamping and rate limiting |
| 23 | + */ |
| 24 | +class AdaptionDe : public sc_core::sc_module { |
| 25 | +public: |
| 26 | + // ======================================================================== |
| 27 | + // Input Ports (from RX/CDR/System) |
| 28 | + // ======================================================================== |
| 29 | + sc_core::sc_in<double> phase_error; // Phase error from CDR (s or normalized UI) |
| 30 | + sc_core::sc_in<double> amplitude_rms; // Amplitude RMS from RX amplitude statistics |
| 31 | + sc_core::sc_in<int> error_count; // Error count from Sampler decision errors |
| 32 | + sc_core::sc_in<double> isi_metric; // ISI metric (optional, for DFE strategy) |
| 33 | + sc_core::sc_in<int> mode; // Operating mode: 0=init, 1=training, 2=data, 3=freeze |
| 34 | + sc_core::sc_in<bool> reset; // Global reset signal |
| 35 | + sc_core::sc_in<double> scenario_switch; // Scenario switch event (optional) |
| 36 | + |
| 37 | + // ======================================================================== |
| 38 | + // Output Ports (to RX/CDR) |
| 39 | + // ======================================================================== |
| 40 | + sc_core::sc_out<double> vga_gain; // VGA gain setting (linear) |
| 41 | + sc_core::sc_out<double> ctle_zero; // CTLE zero frequency (Hz, optional) |
| 42 | + sc_core::sc_out<double> ctle_pole; // CTLE pole frequency (Hz, optional) |
| 43 | + sc_core::sc_out<double> ctle_dc_gain; // CTLE DC gain (linear, optional) |
| 44 | + |
| 45 | + // DFE taps (fixed 8 independent ports) |
| 46 | + sc_core::sc_out<double> dfe_tap1; |
| 47 | + sc_core::sc_out<double> dfe_tap2; |
| 48 | + sc_core::sc_out<double> dfe_tap3; |
| 49 | + sc_core::sc_out<double> dfe_tap4; |
| 50 | + sc_core::sc_out<double> dfe_tap5; |
| 51 | + sc_core::sc_out<double> dfe_tap6; |
| 52 | + sc_core::sc_out<double> dfe_tap7; |
| 53 | + sc_core::sc_out<double> dfe_tap8; |
| 54 | + |
| 55 | + sc_core::sc_out<double> sampler_threshold; // Sampler threshold (V) |
| 56 | + sc_core::sc_out<double> sampler_hysteresis; // Sampler hysteresis (V) |
| 57 | + sc_core::sc_out<double> phase_cmd; // Phase interpolator command (s) |
| 58 | + sc_core::sc_out<int> update_count; // Update counter for diagnostics |
| 59 | + sc_core::sc_out<bool> freeze_flag; // Freeze/rollback status flag |
| 60 | + |
| 61 | + // ======================================================================== |
| 62 | + // Constructor and Process Declaration |
| 63 | + // ======================================================================== |
| 64 | + SC_HAS_PROCESS(AdaptionDe); |
| 65 | + |
| 66 | + /** |
| 67 | + * @brief Constructor |
| 68 | + * @param nm Module name |
| 69 | + * @param params Adaption parameters |
| 70 | + */ |
| 71 | + AdaptionDe(sc_core::sc_module_name nm, const AdaptionParams& params); |
| 72 | + |
| 73 | + /** |
| 74 | + * @brief Destructor |
| 75 | + */ |
| 76 | + ~AdaptionDe(); |
| 77 | + |
| 78 | + // ======================================================================== |
| 79 | + // Public Methods for External Access |
| 80 | + // ======================================================================== |
| 81 | + |
| 82 | + /** |
| 83 | + * @brief Get current AGC gain |
| 84 | + */ |
| 85 | + double get_current_gain() const { return m_current_gain; } |
| 86 | + |
| 87 | + /** |
| 88 | + * @brief Get current DFE taps |
| 89 | + */ |
| 90 | + const double* get_dfe_taps() const { return m_dfe_taps; } |
| 91 | + |
| 92 | + /** |
| 93 | + * @brief Get update count |
| 94 | + */ |
| 95 | + int get_update_count() const { return m_update_count; } |
| 96 | + |
| 97 | + /** |
| 98 | + * @brief Check if frozen |
| 99 | + */ |
| 100 | + bool is_frozen() const { return m_freeze_flag; } |
| 101 | + |
| 102 | +private: |
| 103 | + // ======================================================================== |
| 104 | + // Parameters |
| 105 | + // ======================================================================== |
| 106 | + AdaptionParams m_params; |
| 107 | + |
| 108 | + // ======================================================================== |
| 109 | + // Internal State Variables |
| 110 | + // ======================================================================== |
| 111 | + // AGC state |
| 112 | + double m_agc_integral; // PI controller integral term |
| 113 | + double m_current_gain; // Current VGA gain |
| 114 | + double m_prev_gain; // Previous gain (for rate limiting) |
| 115 | + |
| 116 | + // DFE state |
| 117 | + double m_dfe_taps[8]; // Fixed 8 DFE taps |
| 118 | + double m_dfe_history[8]; // Decision history for feedback |
| 119 | + |
| 120 | + // CDR PI state |
| 121 | + double m_cdr_integral; // PI controller integral term |
| 122 | + double m_current_phase_cmd; // Current phase command |
| 123 | + |
| 124 | + // Threshold state |
| 125 | + double m_current_threshold; // Current sampler threshold |
| 126 | + double m_current_hysteresis; // Current hysteresis window |
| 127 | + int m_prev_error_count; // Previous error count for trend detection |
| 128 | + |
| 129 | + // Update tracking |
| 130 | + int m_update_count; // Total update count |
| 131 | + int m_fast_update_count; // Fast path update count |
| 132 | + int m_slow_update_count; // Slow path update count |
| 133 | + bool m_freeze_flag; // Freeze flag |
| 134 | + |
| 135 | + // Snapshot structure for rollback |
| 136 | + struct Snapshot { |
| 137 | + double vga_gain; |
| 138 | + double dfe_taps[8]; |
| 139 | + double threshold; |
| 140 | + double hysteresis; |
| 141 | + double phase_cmd; |
| 142 | + sc_core::sc_time timestamp; |
| 143 | + bool valid; |
| 144 | + |
| 145 | + Snapshot() : vga_gain(0), threshold(0), hysteresis(0), |
| 146 | + phase_cmd(0), valid(false) { |
| 147 | + for (int i = 0; i < 8; ++i) dfe_taps[i] = 0; |
| 148 | + } |
| 149 | + }; |
| 150 | + std::vector<Snapshot> m_snapshots; |
| 151 | + sc_core::sc_time m_last_snapshot_time; |
| 152 | + |
| 153 | + // Timing |
| 154 | + sc_core::sc_time m_fast_period; |
| 155 | + sc_core::sc_time m_slow_period; |
| 156 | + |
| 157 | + // ======================================================================== |
| 158 | + // SC_METHOD/SC_THREAD Processes |
| 159 | + // ======================================================================== |
| 160 | + |
| 161 | + /** |
| 162 | + * @brief Fast path update process (CDR PI + threshold adaptation) |
| 163 | + * Called at fast_update_period interval |
| 164 | + */ |
| 165 | + void fast_path_process(); |
| 166 | + |
| 167 | + /** |
| 168 | + * @brief Slow path update process (AGC + DFE tap update) |
| 169 | + * Called at slow_update_period interval |
| 170 | + */ |
| 171 | + void slow_path_process(); |
| 172 | + |
| 173 | + /** |
| 174 | + * @brief Reset handler process |
| 175 | + * Triggered by reset signal |
| 176 | + */ |
| 177 | + void reset_process(); |
| 178 | + |
| 179 | + // ======================================================================== |
| 180 | + // Algorithm Implementation Methods |
| 181 | + // ======================================================================== |
| 182 | + |
| 183 | + /** |
| 184 | + * @brief AGC PI controller update |
| 185 | + * @param amplitude Current amplitude RMS |
| 186 | + * @return Updated gain value |
| 187 | + */ |
| 188 | + double agc_pi_update(double amplitude); |
| 189 | + |
| 190 | + /** |
| 191 | + * @brief DFE LMS algorithm update |
| 192 | + * @param error Decision error |
| 193 | + */ |
| 194 | + void dfe_lms_update(double error); |
| 195 | + |
| 196 | + /** |
| 197 | + * @brief DFE Sign-LMS algorithm update |
| 198 | + * @param error Decision error |
| 199 | + */ |
| 200 | + void dfe_sign_lms_update(double error); |
| 201 | + |
| 202 | + /** |
| 203 | + * @brief DFE NLMS algorithm update |
| 204 | + * @param error Decision error |
| 205 | + */ |
| 206 | + void dfe_nlms_update(double error); |
| 207 | + |
| 208 | + /** |
| 209 | + * @brief Threshold adaptation update |
| 210 | + * @param error_count Current error count |
| 211 | + * @return Updated threshold value |
| 212 | + */ |
| 213 | + double threshold_adapt(int error_count); |
| 214 | + |
| 215 | + /** |
| 216 | + * @brief CDR PI controller update |
| 217 | + * @param phase_err Phase error input |
| 218 | + * @return Updated phase command |
| 219 | + */ |
| 220 | + double cdr_pi_update(double phase_err); |
| 221 | + |
| 222 | + // ======================================================================== |
| 223 | + // Safety and Helper Methods |
| 224 | + // ======================================================================== |
| 225 | + |
| 226 | + /** |
| 227 | + * @brief Check for freeze conditions |
| 228 | + * @return true if should freeze |
| 229 | + */ |
| 230 | + bool check_freeze_condition(); |
| 231 | + |
| 232 | + /** |
| 233 | + * @brief Save current parameters to snapshot |
| 234 | + */ |
| 235 | + void save_snapshot(); |
| 236 | + |
| 237 | + /** |
| 238 | + * @brief Rollback to last valid snapshot |
| 239 | + * @return true if rollback successful |
| 240 | + */ |
| 241 | + bool rollback_to_snapshot(); |
| 242 | + |
| 243 | + /** |
| 244 | + * @brief Clamp value to range |
| 245 | + */ |
| 246 | + double clamp(double val, double min_val, double max_val) const; |
| 247 | + |
| 248 | + /** |
| 249 | + * @brief Sign function |
| 250 | + */ |
| 251 | + double sign(double val) const; |
| 252 | + |
| 253 | + /** |
| 254 | + * @brief Write all DFE tap outputs |
| 255 | + */ |
| 256 | + void write_dfe_outputs(); |
| 257 | + |
| 258 | + /** |
| 259 | + * @brief Initialize all internal states |
| 260 | + */ |
| 261 | + void initialize_state(); |
| 262 | + |
| 263 | + /** |
| 264 | + * @brief Write all outputs with current state |
| 265 | + */ |
| 266 | + void write_all_outputs(); |
| 267 | +}; |
| 268 | + |
| 269 | +} // namespace serdes |
| 270 | + |
| 271 | +#endif // SERDES_AMS_ADAPTION_H |
0 commit comments