module cordic_stage_16(clk, rst_n, x_in, y_in, angle_in, angle_adj, x_out, y_out, angle_out);
    parameter SHIFT = 1;

    // inputs
    input clk;
    input rst_n;
    input signed [16-1:0] x_in;
    input signed [16-1:0] y_in;
    input signed [16-1:0] angle_in;
    input signed [16-1:0] angle_adj;

    // outputs
    output reg signed [16-1:0] x_out;
    output reg signed [16-1:0] y_out;
    output reg signed [16-1:0] angle_out;

    // internal signal
    reg signed [16-1:0] new_x;
    reg signed [16-1:0] new_y;
    reg signed [16-1:0] new_angle;

    wire sign;
    wire signed [16-1:0] shifted_x;
    wire signed [16-1:0] shifted_y;

    assign sign = angle_in[16-1];  // angle sign bit
    assign shifted_x = x_in >>> SHIFT;
    assign shifted_y = y_in >>> SHIFT;

    always @(*)
    begin
        new_x = sign ? (x_in + shifted_y) : (x_in - shifted_y);
        new_y = sign ? (y_in - shifted_x) : (y_in + shifted_x);
        new_angle = sign ? (angle_in + angle_adj) : (angle_in - angle_adj);
    end

    always @(posedge clk)
    begin
        if (rst_n == 1'b0)
        begin
            x_out <= 0;
            y_out <= 0;
            angle_out <= 0;
        end
        else begin
            x_out <= new_x;
            y_out <= new_y;
            angle_out <= new_angle;        
        end
    end

endmodule