// ************************************************************************** //
//                                                                            //
//    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 module below computes the discrete convolution of an input stream x(t) //
// with respect to given weights w[0],...w[N-1], i.e., it computes an output  //
// stream y(t) defined as                                                     //
//                                                                            //
//        y_out(t+N-1) = sum(k=0..N-1) w[k] * x_in(t+k)                       //
//                                                                            //
// The weights w[i] can be loaded into the module by piping them in via the   //
// input while setting input lw=True until all weights have arrived at the    //
// right places. Having N cells, the module can compute the products and      //
// their sums in one step, and in each step one convolution is obtained.      //
// In the version below, the w[k] stay in the cells, while both x(t) and y(t) //
// are piped through the array (see [Kung82]). Note that the weights have to  //
// be piped in in the reverse ordering when the results should be the same    //
// as for ConvArray01.                                                        //
//                                                                            //
// For N=5, the array implements the equations:                               //
//                                                                            //
//            next(w[0]) = lw?x_in:w[0]                                       //
//            next(w[1]) = lw?w[0]:w[1]                                       //
//            next(w[2]) = lw?w[1]:w[2]                                       //
//            next(w[3]) = lw?w[2]:w[3]                                       //
//            next(w[4]) = lw?w[3]:w[4]                                       //
//            next(x[0]) = x_in                                               //
//            next(x[1]) = x[0]                                               //
//            next(x[2]) = x[1]                                               //
//            next(x[3]) = x[2]                                               //
//            next(x[4]) = x[3]                                               //
//            y[0] = w[0]*x[0]                                                //
//            y[1] = w[1]*x[1]                                                //
//            y[2] = w[2]*x[2]                                                //
//            y[3] = w[3]*x[3]                                                //
//            y[4] = w[4]*x[4]                                                //
//            y_out = y[4]+y[3]+y[2]+y[1]+y[0]                                //
//                                                                            //
// Thus, the output stream is computed as desired above.                      //
// For weights w[4..0] = [10,8,6,4,2] and inputs x_in = 0,1,2,3,4,5,...,      //
// we therefore obtain:                                                       //
//                                                                            //
//    y_out(10) = 2*5 + 4*4 + 6*3 + 8*2 + 10*1 =  70                          //
//    y_out(11) = 2*6 + 4*5 + 6*4 + 8*3 + 10*2 = 100                          //
//    y_out(12) = 2*7 + 4*6 + 6*5 + 8*4 + 10*3 = 130                          //
//    y_out(13) = 2*8 + 4*7 + 6*6 + 8*5 + 10*4 = 160                          //
//    y_out(14) = 2*9 + 4*8 + 6*7 + 8*6 + 10*5 = 190                          //
//                                                                            //
// ************************************************************************** //

macro N = 5;

module ConvArray02(int ?x_in,!y_out,bool ?lw) {
    [N]int w,x,y;

    loop {
        y_out = sum(k=0..N-1) y[k];
        for(j=0..N-1) {
            next(w[j]) = (lw ? (j==0 ? x_in : w[j-1]) : w[j]);
            next(x[j]) = (j==0 ? x_in : x[j-1]);
            y[j] = w[j] * x[j];
        }
        pause;
    }
}
drivenby {
    [N]int dx,dw; // local stores for x and w in driver

    // first load weights 2,4,6,8
    for(i=0..N-1) {
        x_in = 2*(N-1-i)+2;
        dw[i] = 2*i+2;
        lw = true;
        pause;
    }
    // now do some computation
    for(i=0..2*N-1) {
        x_in = i+1;
        lw = false;
        for(j=0..N-1) 
            next(dx[j]) = (j==0 ? x_in : dx[j-1]);
        if(i>=N)
            assert(y_out == sum(k=0..N-1) (dw[k] * dx[k]));
        pause;
    }
}