/*
 * Use recursive procedure calls to test back-to-back memory accesses:
 * STM and LDM are good ways to stress the memory system
 */

/*
 *              Copyright (C) 2005, BroadOn Communications Corp.
 *
 * These coded instructions, statements, and computer programs contain
 * unpublished  proprietary information of BroadOn Communications Corp.,
 * and  are protected by Federal copyright law. They may not be disclosed
 * to  third  parties or copied or duplicated in any form, in whole or in
 * part, without the prior written consent of BroadOn Communications Corp.
 *
 */

#include <types.h>
#include <sc/sc.h>
#include <ioslibc.h>
#include <sc/dv_boot.h>

volatile int cpu1ready = 0;

int zval = 0;

void
godeep(int seed, int lvl)
{
    int val1;
    int val2;
    int val3;
    int val4;

    val1 = seed + lvl;
    val2 = val1 + 1;
    val3 = val1 + 2;
    val4 = val1 + 3;

    /*
     * Limit the recursion ... just for grins invalidate the stack
     * in the D-cache before we unwind
     */
    if (lvl > 10) {
        DL1Invalidate((void *)&val4, 256);
    } else {
        godeep(val4+1, lvl+1);
    }

    /*
     * Check that the stack and register values
     * got saved and restored correctly
     */
    if (val1 != seed + lvl)
        DVExit(-1);
    else if (val2 != val1 + 1)
        DVExit(-2);
    else if (val3 != val1 + 2)
        DVExit(-3);
    else if (val4 != val1 + 3)
        DVExit(-4);

    return;
}

int 
cpu0main(void)
{
    int val;

    /* For coverage, miss to L2 with a zero value */
    DL1Invalidate((void *)&zval, sizeof zval);
    val = zval;

    DVStartCpu1();

    /* synchronize with cpu1 */
    while (!cpu1ready) ;
    godeep(42, val);

    printf("cpu0: wait for cpu1 ...\n");    
    while (cpu1ready == 1) ;

    printf("cpu0: cpu1 status %d\n", cpu1ready);    
    return 0;
}

int 
cpu1main(void)
{
    /* For coverage, miss to L2 with a zero value */
    DL1Invalidate((void *)&zval, sizeof zval);

    cpu1ready = 1;
    godeep(0xe3e3a5a5, zval);
    cpu1ready = 2;
    return 0;
}
