Advertisement
Guest User

Untitled

a guest
May 7th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns / 1ps
  2. //////////////////////////////////////////////////////////////////////////////////
  3. // Company:
  4. // Engineer:
  5. //
  6. // Create Date:
  7. // Design Name:
  8. // Module Name:
  9. // Project Name:
  10. // Target Devices:
  11. // Tool Versions:
  12. // Description:
  13. //
  14. // Dependencies:
  15. //
  16. // Revision:
  17. // Revision 0.01 - File Created
  18. // Additional Comments:
  19. //
  20. //////////////////////////////////////////////////////////////////////////////////
  21.  
  22.  
  23. module crt(
  24.         input clk,
  25.         input rst,
  26.         input start,
  27.        
  28.         input [7:0] x_in,
  29.        
  30.         output busy,
  31.         output reg [7:0] y_out
  32.     );
  33.    
  34.     // states
  35.    
  36.     localparam idle = 4'b0000;
  37.     localparam next = 4'b0001;
  38.     localparam reset = 4'b0010;
  39.    
  40.     localparam y_double_mul_ready = 4'b0011;
  41.     localparam y_triple_mul_start = 4'b0100;
  42.     localparam y_triple_mul_ready = 4'b0101;
  43.     localparam y_inc_b_mul_start = 4'b0110;
  44.     localparam y_inc_b_mul_ready = 4'b0111;
  45.     localparam b_inc_and_shift = 4'b1000;
  46.     localparam x_b_condition = 4'b1001;
  47.     localparam loop = 4'b1010;
  48.    
  49.     reg [3:0] state, next_state;
  50.  
  51.     reg [7:0] y, b, x;
  52.     reg [7:0] a_mul1, b_mul1;
  53.     reg start_mul1;
  54.     wire [7:0] result_mul1;
  55.     reg reset_mul1;
  56.     wire busy_mul1;
  57.     reg [4:0] counter;
  58.     wire [4:0] end_step;
  59.    
  60.     mult mul1(
  61.         .clk_i(clk),
  62.         .rst_i(reset_mul1),
  63.         .a_bi(a_mul1),
  64.         .b_bi(b_mul1),
  65.         .start_i(start_mul1),
  66.         .busy_o(busy_mul1),
  67.         .y_bo(result_mul1)
  68.     );
  69.      
  70.     assign busy = (state != idle);
  71.     assign end_step = (counter == 5'd0);
  72.    
  73.     always@ (posedge clk)
  74.         if(rst)begin
  75.            counter <= 6;
  76.            y_out <= 0;
  77.            y <= 0;
  78.            state <= idle;
  79.         end else begin
  80.        
  81.             case (state)
  82.                 reset:
  83.                     begin
  84.                        reset_mul1 <= 0;  
  85.                        state <= next;
  86.                     end
  87.                 next:
  88.                     begin
  89.                         state <= next_state;
  90.                     end
  91.                 idle:
  92.                     if(start) begin // starting y * 2 mul process
  93.                         state <= reset;
  94.                         next_state <= y_double_mul_ready;
  95.                         counter <= 6;
  96.                         y <= 0;
  97.                         x <= x_in;
  98.                         reset_mul1 <= 1;
  99.                         start_mul1 <= 1;
  100.                         a_mul1 <= 2;
  101.                         b_mul1 <= y;
  102.                     end
  103.                 y_double_mul_ready:  // y = 2 * y
  104.                     if(!busy_mul1) begin
  105.                         state <= y_triple_mul_start;
  106.                         start_mul1 <= 0;
  107.                         y <= result_mul1;  
  108.                     end
  109.                 y_triple_mul_start: // starting y * 3 mul process
  110.                     begin
  111.                         state <= reset;
  112.                         next_state <= y_triple_mul_ready;
  113.                         reset_mul1 <= 1;
  114.                         start_mul1 <= 1;
  115.                         a_mul1 <= y;
  116.                         b_mul1 <= 3;                    
  117.                     end
  118.                 y_triple_mul_ready: // b = y * 3
  119.                     if(!busy_mul1) begin
  120.                         state <= y_inc_b_mul_start;
  121.                         start_mul1 <= 0;
  122.                         b <= result_mul1;
  123.                     end  
  124.                 y_inc_b_mul_start: // starting b*(y+1) or 3*y*(y+1) mul process
  125.                     begin
  126.                         state <= reset;
  127.                         next_state <= y_inc_b_mul_ready;
  128.                         reset_mul1 <= 1;
  129.                         start_mul1 <= 1;
  130.                         a_mul1 <= y+1;
  131.                         b_mul1 <= b;
  132.                     end
  133.                 y_inc_b_mul_ready: // b = 3*y*(y+1)
  134.                     if(!busy_mul1) begin
  135.                         state <= b_inc_and_shift;
  136.                         b <= result_mul1;
  137.                     end
  138.                 b_inc_and_shift: // b = (3*y*(y+1))+1 << counter
  139.                     begin
  140.                         state <= x_b_condition;
  141.                         b <= (b+1) << counter;
  142.                     end
  143.                 x_b_condition: // checking for x >= b and doing operations according to algorithm
  144.                     if( x >= b ) begin
  145.                         state <= loop;
  146.                         x <= x - b;
  147.                         y <= y + 1;
  148.                     end else begin
  149.                         state <= loop;
  150.                     end
  151.                 loop: // starting again or finishing calculation
  152.                     begin
  153.                         if (end_step) begin // finishing calculation
  154.                             state <= idle;
  155.                             y_out <= y;
  156.                         end else begin // starting again
  157.                             counter <= counter-3;
  158.                             state <= reset;
  159.                             next_state <= y_double_mul_ready;
  160.                             a_mul1 <= 2; // starting y * 2 mul process again
  161.                             b_mul1 <= y;
  162.                             reset_mul1 <= 1;
  163.                         end    
  164.                     end                
  165.             endcase
  166.         end
  167. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement