Divider.v 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * 32-bit multicycle divider
  3. */
  4. /*
  5. module Divider(
  6. input clk,
  7. input reset,
  8. input start,
  9. input [31:0] data,
  10. output done,
  11. output [31:0] q
  12. );
  13. */
  14. module Divider #(
  15. parameter WIDTH=32, // width of numbers in bits (integer and fractional)
  16. parameter FBITS=16 // fractional bits within WIDTH
  17. ) (
  18. input clk, // clock
  19. input rst, // reset
  20. input start, // start calculation
  21. input write_a,
  22. output reg busy = 1'b0, // calculation in progress
  23. output reg done = 1'b0, // calculation is complete (high for one tick)
  24. output reg valid = 1'b0, // result is valid
  25. output reg dbz = 1'b0, // divide by zero
  26. output reg ovf = 1'b0, // overflow
  27. input signed [WIDTH-1:0] a_in, // dividend (numerator)
  28. input signed [WIDTH-1:0] b, // divisor (denominator)
  29. output reg signed [WIDTH-1:0] val = 32'd0 // result value: quotient
  30. );
  31. reg signed [WIDTH-1:0] a = 0;
  32. always @(posedge clk)
  33. begin
  34. if (write_a)
  35. begin
  36. a <= a_in;
  37. end
  38. end
  39. localparam WIDTHU = WIDTH - 1; // unsigned widths are 1 bit narrower
  40. localparam FBITSW = (FBITS == 0) ? 1 : FBITS; // avoid negative vector width when FBITS=0
  41. localparam SMALLEST = {1'b1, {WIDTHU{1'b0}}}; // smallest negative number
  42. localparam ITER = WIDTHU + FBITS; // iteration count: unsigned input width + fractional bits
  43. reg [$clog2(ITER):0] i; // iteration counter (allow ITER+1 iterations for rounding)
  44. reg a_sig = 1'b0;
  45. reg b_sig = 1'b0;
  46. reg sig_diff = 1'b0; // signs of inputs and whether different
  47. reg [WIDTHU-1:0] au = 0;
  48. reg [WIDTHU-1:0] bu = 0; // absolute version of inputs (unsigned)
  49. reg [WIDTHU-1:0] quo = 0;
  50. reg [WIDTHU-1:0] quo_next = 0; // intermediate quotients (unsigned)
  51. reg [WIDTHU:0] acc = 0;
  52. reg [WIDTHU:0] acc_next = 0; // accumulator (unsigned but 1 bit wider)
  53. // input signs
  54. always @(*)
  55. begin
  56. a_sig = a[WIDTH-1+:1];
  57. b_sig = b[WIDTH-1+:1];
  58. end
  59. // division algorithm iteration
  60. always @(*)
  61. begin
  62. if (acc >= {1'b0, bu}) begin
  63. acc_next = acc - bu;
  64. {acc_next, quo_next} = {acc_next[WIDTHU-1:0], quo, 1'b1};
  65. end else begin
  66. {acc_next, quo_next} = {acc, quo} << 1;
  67. end
  68. end
  69. // calculation state machine
  70. parameter IDLE = 5'd0;
  71. parameter INIT = 5'd1;
  72. parameter CALC = 5'd2;
  73. parameter ROUND = 5'd3;
  74. parameter SIGN = 5'd4;
  75. parameter DONE = 5'd5;
  76. reg [2:0] state = IDLE;
  77. always @(posedge clk) begin
  78. done <= 0;
  79. case (state)
  80. INIT: begin
  81. state <= CALC;
  82. ovf <= 0;
  83. i <= 0;
  84. {acc, quo} <= {{WIDTHU{1'b0}}, au, 1'b0}; // initialize calculation
  85. end
  86. CALC: begin
  87. if (i == WIDTHU-1 && quo_next[WIDTHU-1:WIDTHU-FBITSW] != 0) begin // overflow
  88. state <= DONE;
  89. busy <= 0;
  90. done <= 1;
  91. ovf <= 1;
  92. end else begin
  93. if (i == ITER-1) state <= ROUND; // calculation complete after next iteration
  94. i <= i + 1;
  95. acc <= acc_next;
  96. quo <= quo_next;
  97. end
  98. end
  99. ROUND: begin // Gaussian rounding
  100. state <= SIGN;
  101. if (quo_next[0] == 1'b1) begin // next digit is 1, so consider rounding
  102. // round up if quotient is odd or remainder is non-zero
  103. if (quo[0] == 1'b1 || acc_next[WIDTHU:1] != 0) quo <= quo + 1;
  104. end
  105. end
  106. SIGN: begin // adjust quotient sign if non-zero and input signs differ
  107. state <= DONE;
  108. if (quo != 0) val <= (sig_diff) ? {1'b1, -quo} : {1'b0, quo};
  109. busy <= 0;
  110. done <= 1;
  111. valid <= 1;
  112. end
  113. DONE: begin
  114. done <= 1;
  115. state <= IDLE;
  116. end
  117. default: begin // IDLE
  118. if (start) begin
  119. valid <= 0;
  120. if (b == 0) begin // divide by zero
  121. state <= DONE;
  122. busy <= 0;
  123. done <= 1;
  124. dbz <= 1;
  125. ovf <= 0;
  126. end else if (a == SMALLEST || b == SMALLEST) begin // overflow
  127. state <= DONE;
  128. busy <= 0;
  129. done <= 1;
  130. dbz <= 0;
  131. ovf <= 1;
  132. end else begin
  133. state <= INIT;
  134. au <= (a_sig) ? -a[WIDTHU-1:0] : a[WIDTHU-1:0]; // register abs(a)
  135. bu <= (b_sig) ? -b[WIDTHU-1:0] : b[WIDTHU-1:0]; // register abs(b)
  136. sig_diff <= (a_sig ^ b_sig); // register input sign difference
  137. busy <= 1;
  138. dbz <= 0;
  139. ovf <= 0;
  140. end
  141. end
  142. end
  143. endcase
  144. if (rst) begin
  145. state <= IDLE;
  146. busy <= 0;
  147. done <= 0;
  148. valid <= 0;
  149. dbz <= 0;
  150. ovf <= 0;
  151. val <= 0;
  152. end
  153. end
  154. endmodule