*****************************************************************
* *
* Project : IPXS/ mean *
* Date : 2 may 2002 *
* File : mean.c *
* Version : 1.0 *
* *
* Description: mean 2x2 *
* *
* *
* *
****************************************************************/


#define XS40_SRAM32K 
#define XS40_DIVIDE1

#include <stdlib.h>  // include standard library
#include "XS40.h"  // include the Digilab library header
#include "IPXS.h"
#define NCOL 124 // number of image columns

void ProcessBank(unsigned 15 *AddrRead, unsigned 1 *ActiveBank);

// The main program
void main (void)
{
    unsigned 2 OpMode; 
    unsigned 15 Addr; 
    unsigned 1 ActiveBank;
    unsigned 2 MODE;

    XS40LEDDriver();
    par 
    {
        XS40uCDisable(); 

        while (1) 
           {
                GetCommand(&MODE, &ActiveBank, &Addr);

                if (MODE==0b10)
                {ReadBank(&Addr);}
                else if (MODE==0b01)
                {WriteBank(&Addr);}
                else if (MODE==0b00)
                {ProcessBank(&Addr, &ActiveBank);}
                else
                {delay;}
        } // while(1)
    }//par

}


// Process memory bank
void ProcessBank(unsigned 15 *AddrRead, unsigned 1 *ActiveBank)
{
    unsigned 15 AddrWrite, AddrReadDown, AddrEnd;
    unsigned 8 pixel_up, pixel_down,pixel_out;
    unsigned 9 sumcola,sumcolb;
    XS40SetLEDs(PROCESSMODE);

    par{
        AddrWrite = *ActiveBank? DIRBNK0:DIRBNK1;
        AddrEnd=*ActiveBank? DIRBNK1END:DIRBNK0END;
        }
    try
    {
        do
         { 
    // Cycle 1: Read pixel(i) / calculate the addrress of pixel(i+NCOL)
            par{ XS40ReadSRAM(pixel_up, *AddrRead);
            AddrReadDown= (*AddrRead)+NCOL;}
    // Cycle 2: Read pixel down
            XS40ReadSRAM(pixel_down, AddrReadDown);
    // Cycle 3: Add the pixels and swap the partial sums
            par{ 
                    sumcola= (0@pixel_up)+(0@pixel_down);
                    sumcolb=sumcola;
                 }
    // Cycle 4: Add the partial sums and divide by 4
            pixel_out= ((0@sumcola)+(0@sumcolb))\\2;
    // Ciclo 5: Write the result
            XS40WriteSRAM(AddrWrite,pixel_out);
    // Cycle 6: Increment the addresses
            par{ (*AddrRead)++;AddrWrite++;}

        }while(*AddrRead!=AddrEnd);
 }
reset (RST==1)
{
        return;
}
}