
/*   
 *  Check cps backdoor
 *  
 *  Single R/W
 *  Blk Fill/Read/Write
 *
 * :set expandtab
 * :set shiftwidth=4
 * :set tabstop=4
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dv.h"
#include <sc/dv_boot.h>

/* Dole out cycles to the simulator with this granularity */
#define CYCLE_INCREMENT 100

int 
testProc(void *p)
{
    DVCmdStructPnt pcmd;
    u32 data[16];
    exit_comm_area eca;
    u32 seed;
    u32 pass_arg = 0;
    int i;
    int cycle_limit = 0;
    int cycles = 0;
    int rv = 0;
    int flipped;
    int cycle_increment;
    int tcf_off = 1;

    pcmd = (DVCmdStructPnt) p;
    cycle_increment = DV_CpuBdGetTcfon() ? 2 : CYCLE_INCREMENT;
    
    if (pcmd->argc > 1) {
        int i;
        for (i = 1; i < pcmd->argc; i++) {
            char *sp = pcmd->argv[i];
            if (strncmp(sp, "-l", 2) == 0) {
                cycle_limit = strtoul(sp+2, NULL, 10);
                fprintf(pcmd->flog, "Cycle limit set to %d\n", cycle_limit);
            }
            if (strncmp(sp, "-a", 2) == 0) {
                pass_arg = strtoul(sp+2, NULL, 10);
                fprintf(pcmd->flog, "Argument to pass 0x%x\n", pass_arg);
            }
        }
    }
 
    /*
     * Dump the beginning of ROM and DA space to see that the code
     * is actually loaded
     */
    DV_CpuBdIoBlockRead(BOOT_START, 64, 0, data);
    fprintf(pcmd->flog, "[0-3] 0x%x 0x%x 0x%x 0x%x\n", data[0], data[1], data[2], data[3]);
    fprintf(pcmd->flog, "[4-7] 0x%x 0x%x 0x%x 0x%x\n", data[4], data[5], data[6], data[7]);
    fprintf(pcmd->flog, "[8-11] 0x%x 0x%x 0x%x 0x%x\n", data[8], data[9], data[10], data[11]);
    fprintf(pcmd->flog, "[12-15] 0x%x 0x%x 0x%x 0x%x\n", data[12], data[13], data[14], data[15]);

    DV_CpuBdIoBlockRead(DA_START, 64, 0, data);
    fprintf(pcmd->flog, "[0-3] 0x%x 0x%x 0x%x 0x%x\n", data[0], data[1], data[2], data[3]);
    fprintf(pcmd->flog, "[4-7] 0x%x 0x%x 0x%x 0x%x\n", data[4], data[5], data[6], data[7]);
    fprintf(pcmd->flog, "[8-11] 0x%x 0x%x 0x%x 0x%x\n", data[8], data[9], data[10], data[11]);
    fprintf(pcmd->flog, "[12-15] 0x%x 0x%x 0x%x 0x%x\n", data[12], data[13], data[14], data[15]);

    /*
     * Srand has been initialized by the prologue code.  Use
     * rand() to pass a seed to the ARM side to start its PRNG.
     */
    seed = rand();
    i = (seed & 0xc00) >> 10;
    while (i-- > 0) {
        seed = rand();
    }
    eca.runstate = 0x42;
    eca.status = 0x42;
    eca.seed = seed;
    eca.arg  = pass_arg;

    /*
     * Initialize the exit communication area in memory
     */
    rv = DV_CpuBdIoBlockWrite(
             (unsigned int)DA_START+(unsigned int)EXIT_COMM_AREA,
             sizeof (exit_comm_area), 
             0, 
             (u32 *)&eca);
    if (rv != 0) {
        fprintf(pcmd->flog, "IoBlockFill failed rv = %d\n", rv);
        rv = -2;
        goto out;
    }

    /*
     * Feed cycles to the simulator until the program running on the 
     * cpu(s) calls exit, which will pass the exit status through a 
     * magic location in memory.
     */
    while (1) {
        cycles += cycle_increment;
        if (cycle_limit != 0 && cycles > cycle_limit) {
            fprintf(pcmd->flog, "Cycle limit exhausted (test failed)\n");
            rv = -1; 
            break;
        }
        IO_STALL(cycle_increment);
        /*
         * It doesn't work to read from the ECA until memory gets flipped
         */
        flipped = DV_CpuGetRomFlip();
        if (!flipped) {
            continue;
        }
        rv = DV_CpuBdIoBlockRead((unsigned int)EXIT_COMM_AREA, sizeof (exit_comm_area), 0, (unsigned int *)&eca);
        if ((eca.arg == (pass_arg + 1)) && tcf_off) {
             DV_CpuBdSetTcf(1);
             tcf_off = 0;
        }
        if ((eca.arg == (pass_arg + 2)) && (tcf_off==0)) {
            DV_CpuBdSetTcf(0);
            tcf_off = 1;
        }
        if (rv != 0) {
            fprintf(pcmd->flog, "IoBlockRead failed rv = %d\n", rv);
            rv = -2;
            break;
        }
        if (eca.runstate == ECA_RS_DONE) {
            rv = eca.status;
            fprintf(pcmd->flog, "Cpu test done:  exit status = %d\n", rv);
            break;
        }
    }

out:
    DV_CpuBdSetTcf(0);
    fprintf(pcmd->flog, "Cpu test exiting:  total cycles run = %d\n", cycles);
    return rv;
}
