1WaKa_WaKa1

fsm4

Apr 6th, 2023
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns / 1ps
  2.  
  3. module fsm4(
  4.     input clk,
  5.     input reset,
  6.     input enable,
  7.     input [31:0] A,
  8.     input [31:0] B,
  9.     output reg [35:0] res
  10. );
  11.    
  12. parameter PLUG = 0;
  13. parameter SUM_A_B = 1;
  14. parameter SUM1_MUL_4 = 2;
  15. parameter SUM_LMUL_B = 3;
  16. parameter B_DIV_2 = 4;
  17. parameter A_MUL_4 = 5;
  18. parameter SUM_BDIV_AMUL = 6;
  19. parameter L_BRACKETS_DIV_2 = 7;
  20. parameter END_SUM = 8;
  21.  
  22. reg [3:0] cur_state = 0;
  23. reg [3:0] next_state = 0;
  24. reg [35:0] sum_1, sum_2, mul_1, div_1;
  25.  
  26. always @(posedge clk or posedge reset or negedge reset) begin
  27.     if (reset) begin
  28.         res <= 0;
  29.         cur_state = PLUG;  
  30.     end
  31.     if (enable) begin
  32.         res <= 0;
  33.     end
  34. end
  35.  
  36. always @(negedge clk) begin
  37.     case (cur_state)
  38.         PLUG:
  39.             cur_state = SUM_A_B;
  40.         SUM_A_B:
  41.             cur_state = SUM1_MUL_4;
  42.         SUM1_MUL_4:
  43.             cur_state = SUM_LMUL_B;
  44.         SUM_LMUL_B:
  45.             cur_state = B_DIV_2;
  46.         B_DIV_2:
  47.             cur_state = A_MUL_4;
  48.         A_MUL_4:
  49.             cur_state = SUM_BDIV_AMUL;
  50.         SUM_BDIV_AMUL:
  51.             cur_state = L_BRACKETS_DIV_2;
  52.         L_BRACKETS_DIV_2:
  53.             cur_state = END_SUM;
  54.         END_SUM:
  55.             cur_state = 0;
  56.         default:
  57.             cur_state = 0;
  58.     endcase
  59. end
  60.  
  61. always @(posedge clk) begin
  62.     case (cur_state)
  63.         PLUG: ;
  64.         SUM_A_B:
  65.             sum_1 <= A + B;
  66.         SUM1_MUL_4:
  67.             mul_1 <= sum_1 << 2;
  68.         SUM_LMUL_B:
  69.             sum_1 <= mul_1 + B;
  70.         B_DIV_2:
  71.             div_1 <= B >> 1;
  72.         A_MUL_4:
  73.             mul_1 <= A << 2;
  74.         SUM_BDIV_AMUL:
  75.             sum_2 <= div_1 + mul_1;
  76.         L_BRACKETS_DIV_2:
  77.             div_1 <= sum_1 >> 1;
  78.         END_SUM:
  79.             res <= div_1 + sum_2;
  80.         default:
  81.             next_state = 0;
  82.     endcase
  83. end
  84.  
  85. endmodule
  86.  
Add Comment
Please, Sign In to add comment