// ************************************************************************** //
//                                                                            //
//    eses                   eses                                             //
//   eses                     eses                                            //
//  eses    eseses  esesese    eses   Embedded Systems Group                  //
//  ese    ese  ese ese         ese                                           //
//  ese    eseseses eseseses    ese   Department of Computer Science          //
//  eses   eses          ese   eses                                           //
//   eses   eseses  eseseses  eses    University of Kaiserslautern            //
//    eses                   eses                                             //
//                                                                            //
// ************************************************************************** //
// The following module implements a multiplication of radix-2 numbers. The   //
// computation is done by summing up the usual partial products by the use of //
// carry-ripple adders both for the intermediate steps and the final step.    //
// The depth of the circuit is O(M+N) and therefore not optimal.              //
// ************************************************************************** //

macro M = 5;     // number of digits used for x
macro N = 3;     // number of digits used for y
macro natval(x,m) = sum(i=0..m-1) ((x[i]?1:0) * exp(2,i));


module NatMulCRACRA([M]bool ?x,[N]bool ?y,[M+N]bool p) {
    event [M-1][N]bool pp;  // digits of partial products
    event [M][N-1]bool cp;  // carries for summation
    // construct M x N multiplier array 
    for(i=0..M-1) {
        for(j=0..N-1) {
            let(pin  = (i==0 ? false : pp[i-1][j]))
            let(cin  = (j==0 ? false : cp[i][j-1]))
            let(pout = (j==0 | i==M-1 ? p[i+j] : pp[i][j-1]))
            let(cout = (j==N-1 ? (i==M-1 ? p[i+j+1] : pp[i][j]) : cp[i][j]))
            FullAdd(x[i]&y[j],pin,cin,cout,pout);
        }
    }
    // check the specification
    assert(natval(p,M+N) == natval(x,M) * natval(y,N));
}