From 4b57bdfa8074511f3cd97b617572c96d679369b4 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Sun, 4 Jun 2023 12:05:45 +0100 Subject: [PATCH] Update `cordic.sv` and `saw2sin.sv` for better accuracy, `genSaw.sv` to fix polarity of tri/sin --- rtl/cordic.sv | 19 +++++++++++++++---- rtl/genSaw.sv | 4 ++-- rtl/saw2sin.sv | 15 ++++++++++----- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/rtl/cordic.sv b/rtl/cordic.sv index 3eb7f53..1c39097 100644 --- a/rtl/cordic.sv +++ b/rtl/cordic.sv @@ -33,9 +33,11 @@ always_comb cordic_angle[16] = 23'h000028; // 0.000437 degrees always_comb cordic_angle[17] = 23'h000014; // 0.000219 degrees always_comb cordic_angle[18] = 23'h00000A; // 0.000109 degrees +/* verilator lint_off UNOPTFLAT */ logic [18:0] x [0:19]; logic [18:0] y [0:19]; logic [22:0] p [0:19]; +/* verilator lint_on UNOPTFLAT */ // Extend input from input to working widths always_comb x[0] = {1'b0, i_x, {2{1'b0}}}; @@ -43,15 +45,24 @@ always_comb y[0] = {1'b0, i_y, {2{1'b0}}}; always_comb p[0] = {i_qph, {7{1'b0}}}; // Max pos = 23'h3FFF80, Max neg = 23'h400000 +/* verilator lint_off ALWCOMBORDER */ for (genvar i = 0; i < 19; i = i + 1) begin: l_cordic_steps // If phase is negative, rotate CW, otherwise CCW - always_comb x[i+1] = (p[i][22]) ? x[i] + (y[i] >>> (i+1)) : x[i] - (y[i] >>> (i+1)); - always_comb y[i+1] = (p[i][22]) ? y[i] - (x[i] >>> (i+1)) : y[i] + (x[i] >>> (i+1)); - always_comb p[i+1] = (p[i][22]) ? p[i] + cordic_angle[i] : p[i] - cordic_angle[i]; + always_comb + if (p[i][22]) begin + x[i+1] = x[i] + (y[i] >>> (i+1)); + y[i+1] = y[i] - (x[i] >>> (i+1)); + p[i+1] = p[i] + cordic_angle[i]; + end else begin + x[i+1] = x[i] - (y[i] >>> (i+1)); + y[i+1] = y[i] + (x[i] >>> (i+1)); + p[i+1] = p[i] - cordic_angle[i]; + end end +/* verilator lint_on ALWCOMBORDER */ always_comb - if (i_qph > 16'd65508) o_sin = 16'hFFFF; + if (i_qph > 16'd65508) o_sin = 16'hFFFE; else if (i_qph < 16'd32) o_sin = i_qph + (i_qph >> 1); else o_sin = y[19][17:2]; diff --git a/rtl/genSaw.sv b/rtl/genSaw.sv index 69948ee..ee025e5 100644 --- a/rtl/genSaw.sv +++ b/rtl/genSaw.sv @@ -54,8 +54,8 @@ always_comb // Select output waveform case (i_wave[1:0]) 2'd0: o_sample = saw; 2'd1: o_sample = square; - 2'd2: o_sample = triangle; - 2'd3: o_sample = sine; + 2'd2: o_sample = {~triangle[15], triangle[14:0]}; + 2'd3: o_sample = {~sine[15], sine[14:0]}; endcase endmodule diff --git a/rtl/saw2sin.sv b/rtl/saw2sin.sv index f3aee26..c74347d 100644 --- a/rtl/saw2sin.sv +++ b/rtl/saw2sin.sv @@ -13,8 +13,8 @@ always_comb reverse = i_saw[14]; logic [15:0] qsaw; always_comb qsaw = reverse - ? {~i_saw[13:0], 2'b01} // Reverse - : {i_saw[13:0], 2'b00}; // Normal + ? {~i_saw[13:0], 2'b11} // Reverse + : {i_saw[13:0], 2'b01}; // Normal logic [15:0] qsin; cordic cordic @@ -22,8 +22,13 @@ cordic cordic , .o_sin (qsin) ); -always_comb o_sin = invert - ? ~{1'b1, qsin[15:1]} + 1 // Invert - : {1'b1, qsin[15:1]}; // Normal +logic [16:0] sin; +always_comb sin = reverse + ? (invert ? ~{1'b1, qsin[15:0]} // Reverse, Invert + : {1'b1, qsin[15:0]} + 17'd1) // Reverse, Normal + : (invert ? ~{1'b1, qsin[15:0]} + 17'd2 // Normal, Invert + : {1'b1, qsin[15:0]} + 17'd0); // Normal, Normal + +always_comb o_sin = sin[16:1]; endmodule