Advertisement
Guest User

divino mult

a guest
May 16th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Mult(Reset, Clock, w_MultStart, w_MultStop, w_MULTHI, w_MULTLO, w_A, w_B);
  2.  
  3. input Clock, Reset, w_MultStart;
  4. input[31:0] w_A, w_B;
  5. output reg w_MultStop;
  6. output reg[31:0] w_MULTHI, w_MULTLO;
  7.  
  8.  
  9. // Temporarios do Mult -- Algoritmo de Booth (https://en.wikipedia.org/wiki/Booth%27s_multiplication_algorithm)
  10. //Os nomes vem da pagina do wikipedia do algoritmo
  11. reg [64:0] A, S, P;
  12. integer y = 32; // size of r
  13. // A is multiplicand (m)
  14. // B is multiplier (r)
  15.  
  16. always @(posedge Clock) begin
  17.     if (Reset) begin // reseta tudo
  18.         w_MULTHI = 32'b0;
  19.         w_MULTLO = 32'b0;
  20.         w_MultStop = 1'b0;
  21.         A = 65'b0;
  22.         S = 65'b0;
  23.         P = 65'b0;
  24.     end
  25.    
  26.     if(y==32) begin // Setup
  27.         A = {w_A, 33'b0};           //A: Fill the most significant (leftmost) bits with the value of m. Fill the remaining (y + 1) bits with zeros.
  28.         S = {(~w_A + 1'b1), 33'b0}; //S: Fill the most significant bits with the value of (-m) in two's complement notation. Fill the remaining (y + 1) bits with zeros.
  29.         P = {32'b0, w_B, 1'b0};     //P: Fill the most significant x bits with zeros. To the right of this, append the value of r. Fill the least significant (rightmost) bit with a zero.
  30.         y = 32;
  31.         w_MultStop = 1'b0;
  32.     end
  33.    
  34.     // Now there are 4 cases, but actually two cases because two of them do nothing
  35.     // Determine the two least significant (rightmost) bits of P.
  36.         // If they are 01, find the value of P + A. Ignore any overflow.
  37.         // If they are 10, find the value of P + S. Ignore any overflow.
  38.         // If they are 00, do nothing. Use P directly in the next step.
  39.         // If they are 11, do nothing. Use P directly in the next step.
  40.     case(P[1:0])
  41.       2'b01: begin
  42.         P = P + A;
  43.       end
  44.       2'b10: begin
  45.         P = P + S;
  46.       end
  47.     endcase
  48.    
  49.     // Arithmetically shift the value obtained in the 2nd step by a single place to the right. Let P now equal this new value.
  50.     P = P >> 1;
  51.     if (P[63] == 1'b1) begin // This will make the shift right the way it should
  52.         P[64] = 1'b1;
  53.     end
  54.    
  55.     // Repeat steps 2 and 3 until they have been done y (no nosso caso sempre 32) times.
  56.     y = y - 1;
  57.     if (y==0) begin
  58.       w_MULTHI = P[64:33];
  59.       w_MULTLO = P[32:1];
  60.       w_MultStop = 1'b1;
  61.       y = -1;
  62.     end
  63.     if ( y == -1) begin
  64.       A = 65'b0;
  65.       S = 65'b0;
  66.       P = 65'b0;
  67.     end
  68.  
  69. end
  70. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement