Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module atan_const (
- input [3:0] itr,
- output reg [15:0] atan
- );
- always @ (*)
- case (itr)
- 4'd0: atan = 16'h3244;
- 4'd1: atan = 16'h1dac;
- 4'd2: atan = 16'h0fae;
- 4'd3: atan = 16'h07f5;
- 4'd4: atan = 16'h03ff;
- 4'd5: atan = 16'h0200;
- 4'd6: atan = 16'h0100;
- 4'd7: atan = 16'h0080;
- 4'd8: atan = 16'h0040;
- 4'd9: atan = 16'h0020;
- 4'd10: atan = 16'h0010;
- 4'd11: atan = 16'h0008;
- 4'd12: atan = 16'h0004;
- 4'd13: atan = 16'h0002;
- 4'd14: atan = 16'h0001;
- default:atan = 16'h0000;
- endcase
- endmodule
- module rgst #( parameter w = 8)//register's width
- ( input clk,
- input rst_b, //asynchronous reset; active low
- input [w-1:0] d,
- input ld,
- input clr, //synchronous reset; active high output
- output reg [w-1:0] q);
- always @ (posedge clk, negedge rst_b)
- if (!rst_b) q <= 1'd0;
- else if (clr) q <= 1'd0;
- else if (ld) q <= d;
- endmodule
- module arsh (
- input [3:0]i, [15:0]a,
- output [15:0]o);
- assign o = $signed(a) >>> i;
- endmodule
- module cntr #(parameter w = 8)(
- input clk, rst_b, c_up, clr,
- output reg [w-1:0]q);
- always@(posedge clk,negedge rst_b)
- if(!rst_b) q<=1'd0;
- else if(clr) q<=1'd0;
- else if(c_up) q<=q+1'd1;
- endmodule
- module add_sub(
- input s,
- input [15:0]a,b,
- output [15:0]o
- );
- assign o=s?a+b:a-b;
- endmodule
- module ctrl_u(
- input [3:0]itr,
- input clk,rst_b,bgn,
- output fin,ld,init
- );
- reg [2:0]st;
- wire [2:0]st_next;
- assign st_next[0]=st[2]|st[0]&~bgn;
- assign st_next[1]=st[0]&bgn|st[1]&(itr !=4'd15);
- assign st_next[2]=st[1]&(itr == 4'd15);
- assign ld=st[0]&bgn|st[1];
- assign init=st[0]&bgn;
- assign fin=st[2];
- always@(posedge clk,negedge rst_b)
- if(!rst_b) st<=3'd1;
- else st<=st_next;
- endmodule
- module cordic(
- input clk,rst_b,bgn,
- input [15:0]theta,
- output fin,
- output [15:0]cos
- );
- wire [15:0]x_new, y_new, z_new, x_out, y_out, z_out, atan, x_rsh, y_rsh;
- wire [3:0]i;
- wire ld,init;
- rgst #(.w(16))
- a0(
- .ld(ld),
- .clk(clk),
- .clr(1'd0),
- .rst_b(rst_b),
- .d(init ? 16'h26dd : x_new),
- .q(x_out)
- );
- arsh a1(
- .a(y_out),
- .i(i),
- .o(y_rsh)
- );
- add_sub a2(
- .a(x_out),
- .b(y_rsh),
- .s(~z_out[15]),
- .o(x_new)
- );
- rgst #(.w(16))b0(
- .ld(ld),
- .clk(clk),
- .clr(1'd0),
- .rst_b(rst_b),
- .d(init ? 0 : y_new),
- .q(y_out)
- );
- arsh b1(
- .a(x_out),
- .i(i),
- .o(x_rsh)
- );
- add_sub b2(
- .a(y_out),
- .b(x_rsh),
- .s(z_out[15]),
- .o(y_new)
- );
- rgst #(.w(16))c0(
- .ld(ld),
- .clk(clk),
- .clr(1'd0),
- .rst_b(rst_b),
- .d(init ? theta : z_new),
- .q(z_out)
- );
- atan_const atc(
- .itr(i),
- .atan(atan)
- );
- add_sub c2(
- .a(z_out),
- .b(atan),
- .s(~z_out[15]),
- .o(z_new)
- );
- cnt #(.w(4))counter(
- .clk(clk),
- .rst_b(rst_b),
- .c_up(ld),
- .clr(init),
- .q(i)
- );
- ctrl_u control(
- .itr(i),
- .bgn(bgn),
- .clk(clk),
- .rst_b(rst_b),
- .fin(fin),
- .ld(ld),
- .init(init)
- );
- assign cos=fin?x_out:16'bz;
- endmodule
- module cordic_tb (
- output reg clk, rst_b, bgn, [15:0] theta,
- output fin, [15:0] cos);
- cordic test (
- .clk(clk),
- .rst_b(rst_b),
- .bgn(bgn),
- .theta(theta),
- .fin(fin),
- .cos(cos));
- initial begin
- clk = 1'd0;
- repeat (500) #50 clk = ~clk;
- end
- initial begin
- rst_b = 1'd0; #5 rst_b = 1'd1;
- end
- initial begin
- bgn = 1'd1;
- #100 bgn = 1'd0;
- #1700 bgn = 1'd1;
- #100 bgn = 1'd0;
- #1700 bgn = 1'd1;
- #100 bgn = 1'd0;
- end
- initial begin
- theta = 16'h2183;
- #1800 theta = 16'h3244;
- #1800 theta = 16'h4305;
- end
- endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement