Advertisement
Guest User

Untitled

a guest
Feb 4th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1 ns / 1 ns
  2.  
  3. module CORDIC_ABS
  4.   #(
  5.     parameter INPUT_WIDTH = 16,
  6.     parameter OUTPUT_WIDTH = 16
  7.   )
  8.   (
  9.     input wire clk,
  10.     input wire start,
  11.     input wire signed [INPUT_WIDTH - 1:0] X,
  12.     input wire signed [INPUT_WIDTH - 1:0] Y,
  13.     output reg [OUTPUT_WIDTH - 1: 0] abs = 0,
  14.     output wire ce_out
  15.   );
  16.  
  17.   localparam WIDTH_XY = OUTPUT_WIDTH;
  18.   localparam iterations = WIDTH_XY;
  19.   localparam STATE_IDLE = 0, STATE_RUN = 1, STATE_STOP = 2;
  20.  
  21.   reg [1:0] state = STATE_IDLE;
  22.  
  23.   reg signed [WIDTH_XY - 1:0] x;
  24.   reg signed [WIDTH_XY - 1:0] y;
  25.  
  26.   reg [$clog2(iterations) - 1:0] iteration_cnt = 0;
  27.  
  28.   assign ce_out = (state == STATE_STOP);
  29.  
  30.   always @(posedge clk) begin
  31.     case (state)
  32.       STATE_IDLE: begin
  33.         if (start) begin
  34.           state <= STATE_RUN;
  35.           x <= (X < 0) ? -X : X;
  36.           y <= (Y < 0) ? -Y : Y;
  37.           iteration_cnt <= 0;
  38.         end
  39.       end
  40.       STATE_RUN: begin
  41.         if (y > 0) begin
  42.           x <= x + (y >>> iteration_cnt);
  43.           y <= y - (x >>> iteration_cnt);
  44.         end else begin
  45.           x <= x - (y >>> iteration_cnt);
  46.           y <= y + (x >>> iteration_cnt);
  47.         end
  48.         if (iteration_cnt == iterations - 1) begin
  49.           state <= STATE_STOP;
  50.         end
  51.         iteration_cnt <= iteration_cnt + 1'b1;
  52.       end
  53.       STATE_STOP: begin
  54.         state <= STATE_IDLE;
  55.         abs <= x;
  56.       end
  57.       default: begin
  58.         state <= STATE_IDLE;
  59.       end
  60.     endcase
  61.   end
  62. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement