// Multiplication module

// To start the multiplier
//    Load p and q with values to be multiplied
//    Assert start
// Multiplier will start when start has been asserted
//    Multiplier will deassert ready
// When multiplier has finished
//    Multiplier will put product in result
//    Multiplier will assert ready
// Assert ready during a multiplication, restarts the multiplier
module ancienttimes
     #(parameter DATA_WIDTH = 12)
      (input    logic                    clk,
       input    logic                    reset,
       input    logic                    start,
       input    logic [DATA_WIDTH-1:0]   p,
       input    logic [DATA_WIDTH-1:0]   q,
       output   logic [2*DATA_WIDTH-1:0] result,
       output   logic                    ready) ;
  // typedef enum logic[1:0] {IDLE, RUNNING } eqstate ;
  // eqstate state, nextstate ;
  logic [DATA_WIDTH-1:0] shrinking, nextshrinking ;
  logic [2*DATA_WIDTH-1:0] growing, nextgrowing ;
  logic [2*DATA_WIDTH-1:0] nextresult ;

  // State and datapath update
  always_ff @(posedge clk, reset)
    begin
      if (reset)
        begin
          // state <= IDLE ;
          shrinking <= 0 ;
          growing <= 0 ;
          result <= 0 ;
        end
      else
        begin
          // state <= nextstate ;
          shrinking <= nextshrinking ;
          growing <= nextgrowing ;
          result <= nextresult ;
        end
    end

  // data path control
  always_comb
    begin
      if (start == 1)
        begin
          nextshrinking <= p ;
          nextgrowing <= q ;
          nextresult <= 0 ;
        end
      else
        begin
          nextshrinking <= shrinking >> 1 ;
          nextgrowing <= growing << 1 ;
          nextresult <= result + (shrinking[0] ? growing : 0) ;
        end
    end

  // next state
  /*
  always_comb
    if (start == 1 || (state == RUNNING && !shrinking == 0))
      nextstate <= RUNNING ;
    else
      nextstate <= IDLE ;
  */
      
  // ready if IDLE
  // assign ready = ( state == IDLE ) ;
  assign ready = !shrinking ;
  
endmodule

