// cordic.sv // Pipelined CORDIC in rotation mode: sine and cosine of a binary angle. // // angle : 16-bit signed binary angle, theta = angle * 2*pi / 65536 // (-32768 = -180 deg, 16384 = +90 deg). // cos_o, sin_o : signed 16-bit, amplitude 32767 (Q1.15 scaled by 32767/32768). // // Stage 0 rotates by +/-90 degrees when |theta| >= 90 degrees, so the residual // angle stays inside the +/-99.9 degree range the iterations can reach. // Stages 1..N perform one shift-add iteration each. The last stage rounds and // saturates. Latency: N + 2 clock cycles from in_valid to out_valid; one new // angle can enter every clock cycle. // // x and y start at X_INIT = round(32767 * K(N) * 2^G), which pre-compensates the // CORDIC gain, so no multiplier is needed. G guard bits below the output LSB // reduce the effect of truncation in the shifts. `timescale 1ns/1ps module cordic #( parameter int N = 16 // iterations, 12 to 18 (X_INIT rounds to the same value for all of them) ) ( input logic clk, input logic rst, // synchronous, active high; clears valid flags only input logic in_valid, input logic signed [15:0] angle, output logic out_valid, output logic signed [15:0] cos_o, output logic signed [15:0] sin_o ); localparam int G = 3; // guard bits localparam int XW = 16 + G + 1; // x, y width (one bit of headroom) localparam int ZW = 20; // angle width, 2^ZW = one turn // round(32767 * K(16) * 2^3), K(16) = 0.6072529351 (from cordic_model.py) localparam logic signed [XW-1:0] X_INIT = XW'(159183); localparam logic signed [ZW-1:0] QUARTER = ZW'(1 << (ZW - 2)); // atan(2^-i) in units of 2*pi / 2^20, rounded (cordic_model.atan_table()). function automatic logic signed [ZW-1:0] atan_tab(input int i); case (i) 0: atan_tab = ZW'(131072); 1: atan_tab = ZW'(77376); 2: atan_tab = ZW'(40884); 3: atan_tab = ZW'(20753); 4: atan_tab = ZW'(10417); 5: atan_tab = ZW'(5213); 6: atan_tab = ZW'(2607); 7: atan_tab = ZW'(1304); 8: atan_tab = ZW'(652); 9: atan_tab = ZW'(326); 10: atan_tab = ZW'(163); 11: atan_tab = ZW'(81); 12: atan_tab = ZW'(41); 13: atan_tab = ZW'(20); 14: atan_tab = ZW'(10); 15: atan_tab = ZW'(5); 16: atan_tab = ZW'(3); 17: atan_tab = ZW'(1); default: atan_tab = '0; endcase endfunction logic signed [XW-1:0] x [0:N]; logic signed [XW-1:0] y [0:N]; logic signed [ZW-1:0] z [0:N]; logic [N+1:0] vld; // Stage 0: quadrant pre-rotation. logic signed [ZW-1:0] z_in; assign z_in = {angle, {(ZW - 16){1'b0}}}; always_ff @(posedge clk) begin if (angle >= 16'sd16384) begin // [90, 180) degrees x[0] <= '0; y[0] <= X_INIT; z[0] <= z_in - QUARTER; end else if (angle < -16'sd16384) begin // [-180, -90) degrees x[0] <= '0; y[0] <= -X_INIT; z[0] <= z_in + QUARTER; end else begin x[0] <= X_INIT; y[0] <= '0; z[0] <= z_in; end end // Stages 1..N: one iteration each. The sign of the residual angle chooses // the direction; >>> is an arithmetic (sign-extending) shift. for (genvar i = 0; i < N; i++) begin : g_stage always_ff @(posedge clk) begin if (!z[i][ZW-1]) begin // z >= 0: rotate counter-clockwise x[i+1] <= x[i] - (y[i] >>> i); y[i+1] <= y[i] + (x[i] >>> i); z[i+1] <= z[i] - atan_tab(i); end else begin // z < 0: rotate clockwise x[i+1] <= x[i] + (y[i] >>> i); y[i+1] <= y[i] - (x[i] >>> i); z[i+1] <= z[i] + atan_tab(i); end end end // Output stage: round half up, drop G guard bits, saturate to 16 bits. function automatic logic signed [15:0] round_sat(input logic signed [XW-1:0] v); logic signed [XW-1:0] r; r = (v + XW'(1 << (G - 1))) >>> G; if (r > XW'(32767)) round_sat = 16'sh7FFF; else if (r < -XW'(32768)) round_sat = 16'sh8000; else round_sat = r[15:0]; endfunction always_ff @(posedge clk) begin cos_o <= round_sat(x[N]); sin_o <= round_sat(y[N]); end // Valid pipeline, the only registers with a reset. always_ff @(posedge clk) begin if (rst) vld <= '0; else vld <= {vld[N:0], in_valid}; end assign out_valid = vld[N+1]; endmodule