#ident "$Id: stack.c,v 1.6 2007/05/03 22:56:10 craig Exp $"
/*
 *               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.
 *
 */

/*
 * Functions to measure and monitor procedure call stack usage.
 * Except for this code, stack overflow errors will be silent,
 * deadly, and hard to debug.
 */


#include "ios.h"
#include "iostypes.h"
#include "ioslibc.h"
#include "flush.i"

static u32        stackStart, stackSize;
static u32        stackFill = 0xfedcba98;

#include "stack.h"

/* ------------------------------------------------------------------------ */

void
initStack(const u32 stackStartIn, const u32 stackSizeIn)
{
    u32         stackPtrVal, *wordPtr;
    int         freeStackWords;

    
    stackStart = stackStartIn;
    stackSize  = stackSizeIn;
    
    /*
     * Initialize the unused part of the stack with a known value.
     * Assumes stack growns down toward lower addresses.
     */
    __asm__("mov %0, sp" : "=r" (stackPtrVal) : );

    freeStackWords = (stackPtrVal - stackStart) / 4;
    /*
     * Hack - some words below the stack pointer may be used, so subtract
     * a small value off the number of free words
     */
    freeStackWords -= 64;
    wordPtr = (u32 *) stackStart;
    for (int i = 0; i < freeStackWords; i++, wordPtr++) {
        *wordPtr = stackFill;
    }
    flushIOPwrite((u8 *) stackStart, freeStackWords * 4);
    /*
    printf ("Lowest stack addr: 0x%x; Highest; 0x%x; stack size: %uK bytes\n",
             stackStart, stackStart + stackSize - 1, stackSize / 1024);
    printf ("Stack pointer value is: 0x%x\n", stackPtrVal);
    */
    if (stackOK() == false) {
        printf ("(initStack) Coding error: Stack looks overrun, but isn't\n");
    }
    // printStackStatus();
}

/* ------------------------------------------------------------------------ */

/*
 * Returns true if everything is OK
 * Returns false if there has been a stack overflow
 */

bool
stackOK()
{
    u32         lowestStackVal;

    lowestStackVal = * (u32 *) stackStart;
    if (lowestStackVal == stackFill) {
        return(true);
    }
    else {
        printf ("@@@@@@@@@@@@ Stack overflow error @@@@@@@@@@@@@@ \n");
        printf ("Stack overflow value: 0x%x\n", lowestStackVal);
        return(false);
    }
}
/* ------------------------------------------------------------------------ */

void
printStackStatus()
{
    u32         stackPtrVal, *wordPtr;
    int         bytesFree, bytesUsed;
    int         totalWordsFree, maxWordsUsed;

    __asm__("mov %0, sp" : "=r" (stackPtrVal) : );
    bytesFree = stackPtrVal - stackStart;
    bytesUsed = stackSize - bytesFree;

    totalWordsFree = 0;
    wordPtr = (u32 *) stackStart;
    for (int i = 0; i < stackSize / 4; i++, wordPtr++) {
        if (*wordPtr == stackFill) {
            totalWordsFree++;
        }
        else {
            break;
        }
    }
    maxWordsUsed = (stackSize / 4) - totalWordsFree;
    printf ("Current stack usage: %d bytes/%dK bytes\n", bytesUsed,
             bytesUsed / 1024);
    printf ("Maximum stack usage: %d bytes/%dK bytes\n", maxWordsUsed * 4,
             maxWordsUsed / 256);
}

/* ------------------------------------------------------------------------ */

/*
 * If the stack has ever grown past the previous maximum, print a message.
 */

void
printStackMaximum()
{
    u32         stackPtrVal, *wordPtr;
    int         totalWordsFree, maxWordsUsed;
    static int  previousMaxWordsUsed = 0;

    __asm__("mov %0, sp" : "=r" (stackPtrVal) : );

    totalWordsFree = 0;
    wordPtr = (u32 *) stackStart;
    for (int i = 0; i < stackSize / 4; i++, wordPtr++) {
        if (*wordPtr == stackFill) {
            totalWordsFree++;
        }
        else {
            break;
        }
    }
    maxWordsUsed = (stackSize / 4) - totalWordsFree;
    if (maxWordsUsed > previousMaxWordsUsed) {
        printf ("Maximum stack usage: %d bytes/%dK bytes\n", maxWordsUsed * 4,
                 maxWordsUsed / 256);
        previousMaxWordsUsed = maxWordsUsed;
    }
}

/* ------------------------------------------------------------------------ */
