// NCO and quadrature mixer: s = I*cos(phase) - Q*sin(phase), scaled by 2^-16. // A 32-bit phase accumulator advances by tuning_word every clock; its top // 8 bits address a 256-entry Q1.15 sine table. The cosine is the same table // read a quarter cycle (64 entries) ahead. Latency from i_in/q_in: 3 clocks. `timescale 1ns / 1ps module nco_mixer #( parameter SINE_FILE = "sine256.hex" ) ( input logic clk, input logic rst, input logic [31:0] tuning_word, input logic signed [15:0] i_in, input logic signed [15:0] q_in, output logic signed [15:0] s_out ); logic signed [15:0] sine [256]; initial $readmemh(SINE_FILE, sine); logic [31:0] phase; logic [7:0] addr; logic signed [15:0] cos_r, sin_r, i_r, q_r; logic signed [31:0] p_i, p_q; logic signed [31:0] diff; assign addr = phase[31:24]; // |p_i - p_q| <= 2 * 32767^2 < 2^31, so the difference fits in 32 bits. assign diff = p_i - p_q + 32'sd32768; // + 0.5 LSB of the output: round always_ff @(posedge clk) begin if (rst) begin phase <= '0; cos_r <= '0; sin_r <= '0; i_r <= '0; q_r <= '0; p_i <= '0; p_q <= '0; s_out <= '0; end else begin phase <= phase + tuning_word; cos_r <= sine[addr + 8'd64]; // stage 1: table look-up sin_r <= sine[addr]; i_r <= i_in; q_r <= q_in; p_i <= i_r * cos_r; // stage 2: products (DSP blocks) p_q <= q_r * sin_r; s_out <= 16'(diff >>> 16); // stage 3: difference, round, scale end end endmodule