/*	$Id: perfcntr.c,v 1.1 2004/01/21 21:33:39 pefo Exp $ */

/*
 * Copyright (c) 2003 Opsycon AB  (www.opsycon.se)
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Opsycon AB, Sweden.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

/*
 *   This is a simple program used to demonstrate the SDK
 */

#include <stdio.h>
#include <stdlib.h>
#include <termio.h>
#include <sys/ioctl.h>

/*
 *  Make a system call to set/get the SW performance counters.
 *
 *    0x0000000x	Clear counters and enable count wrap int
 *    0x0000001x	Get counters
 *
 *    x = 0 no counter affected
 *    x = 1 counter 1
 *    x = 2 counter 2
 *    x = 3 counter 1 and 2
 */
inline int
do_sys_cntr(int what, void *cntrs)
{
	register int __status __asm__ ("$2");

	__asm__ volatile (
	    "move  $4,%1\n\t"
	    "move  $5,%2\n\t"
	    "li    $2, 16384\n\t"
	    "syscall\n\t"
	    : "=r" (__status)
	    : "r" (what), "r" (cntrs)
	    : "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9",
	      "$10","$11","$12","$13","$14","$15","$24","$25");
	return __status;
}

/*
 *  Load what value into the counter control register
 */
inline void
do_set_cntctl(int what)
{
	__asm__ volatile (
	    "mtc0  %0,$22"
	    :: "r" (what) );
}
	

int
main(int argc, char **argv)
{
	u_int64_t result[2];

	printf("\n\nQuick performance counter test program!\n\n");

	/* Clear and init PMON2K maintained counters */
	do_sys_cntr(0x03, NULL);

	/* Enable counters */
	do_set_cntctl(0x07800780 | 0x00010000);

	/* Call a function */
	prof_func();

	/* Disable counters */
	do_set_cntctl(0x00000000);

	/* Read result from PMON2K counters */
	do_sys_cntr(0x13, &result);

	/* Print out result */
	printf("cntr1 %lld, cntr2 %lld\n", result[0], result[1]);

	return(0);
}

prof_func()
{
	printf("in profiled function\n");
}
