/*
 * Test interaction of L1 D-cache between the two processors
 */

#include <types.h>
#include <sc/sc.h>
#include <ioslibc.h>
#include "splockb.h"
#include <sc/dv_boot.h>
#include "dv_cpu_local.h"

struct {
    u8   c0owner;
    u8   c1owner;
    u8   pad;
    u8   lock;
    u32  c0count;
    u32  c1count;
} foo __attribute__ ((aligned (16)));

#define FPGA 1
#if FPGA
#define ITERATION_LIMIT 400000
#define DIFF_LIMIT 5000
#else
#define ITERATION_LIMIT 400
#define DIFF_LIMIT 50
#endif

void
cleanup()
{
    s32 diff;

    diff = foo.c0count - foo.c1count;

#if FPGA
    printf("Cleanup:  cpu0 count %d, cpu1 count %d\n", foo.c0count, foo.c1count);
#endif

    /*
     * Detect unfairness, which may mean that one of the
     * cpus encounted an error of some sort.
     */
    if (diff < 0) diff = -diff;
    if (diff > DIFF_LIMIT) {
#if FPGA
        printf("Iteration count difference too much: %d\n", diff);
#endif
        DVExit(-10);
    } else {
        DVExit(0);
    }

}

/*
 * Waste a random but small number of cycles
 */
void
randwait(void)
{
    volatile int i = rand() & 0xf;

    while (i-- > 0) ;
}

int 
cpu0main(void)
{
    volatile exit_comm_area *ecap = EXIT_COMM_AREA;
    u32 mask;

    /* Seed PRNG with info from the host side */
    mask = ecap->runstate;
    mask = ecap->status;
    mask = ecap->seed;
    srand(mask);

    /* Initialize the shared structure */
    __skInitSplockB(&foo.lock);
    foo.c0owner = 0;
    foo.c1owner = 0;
    foo.c0count = 0;
    foo.c1count = 0;

    DVStartCpu1();

    /* Try to grab the lock and then increment our count */
    while (1) {
        /* Add a little randomization before going for the lock */
        randwait();
        mask = __skSplockBDisable(&foo.lock);
        /* We have the lock ... make the most of it! */
        if (foo.c0owner != 0) {
            /* something is horribly wrong */
            DVExit(-4);
        }
        foo.c0owner = 1;
        if (foo.c1owner != 0) {
            /* something is horribly wrong */
            DVExit(-5);
        }
        foo.c0count += 1;
        /* Clear the ownership and release the lock */
        foo.c0owner = 0;
        (void) __skSpunlockBRestore(&foo.lock, mask);
        if (foo.c0count > ITERATION_LIMIT) {
            break;
        } 
    }
    cleanup();

    return 0;
}

int 
cpu1main(void)
{
    u32 mask;

    /* Try to grab the lock and then increment our count */
    while (1) {
        mask = __skSplockBDisable(&foo.lock);
        /* We have the lock ... make the most of it! */
        if (foo.c1owner != 0) {
            /* something is horribly wrong */
            DVExit(-6);
        }
        foo.c1owner = 1;
        if (foo.c0owner != 0) {
            /* something is horribly wrong */
            DVExit(-7);
        }
        foo.c1count += 1;
        /* Clear the ownership and release the lock */
        foo.c1owner = 0;
        (void) __skSpunlockBRestore(&foo.lock, mask);
        if (foo.c1count > ITERATION_LIMIT) {
            break;
        } 
    }
    while (1) ;
    return 0;
}
