Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / docs / mmi / mmiexample / mmiexample.C
// ========== Copyright Header Begin ==========================================
//
// OpenSPARC T2 Processor File: mmiexample.C
// Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
//
// The above named program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License version 2 as published by the Free Software Foundation.
//
// The above named program is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this work; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
//
// ========== Copyright Header End ============================================
// mmiexample.C
// simple device model that uses the SAM MMI API.
// 2005.05.31
#include <stdio.h>
#include <sys/types.h>
#include "mmi.h"
extern "C" void mmiexample_instance_creator(const char * modname, const char * instancename);
extern "C" int mmiexample_access(uint32_t cpuid, void* obj, uint64_t paddr, mmi_bool_t wr, uint32_t size, uint64_t* buf, uint8_t bytemask);
// mmiexample - a simple device that returns monotonically increasing counter values
// store resets the counter
// "mmiexample" is instantiated using a simple sysconf directive:
// sysconf mmiexample example1 # no arguments
struct mmiexample_s {
int counter;
}; // struct mmiexample_s
extern "C" void _init()
{
mmi_register_instance_creator("example", mmiexample_instance_creator);
}
void mmiexample_instance_creator(const char * modname, const char * instance_name)
{
printf("%s: Instantiating %s\n", modname, instance_name);
// call mmi_argc() and mmi_argv() here to get device node arguments
mmiexample_s * instance_data = new mmiexample_s;
instance_data->counter = 0;
// we currently support only one instance at a hardcoded physical address of 0xdeadbeef0
mmi_map_physio(0xdeadbeef0, 8, (void *)instance_data, mmiexample_access);
} // example_instance_creator()
int mmiexample_access(uint32_t cpuid, void* obj, uint64_t paddr, mmi_bool_t wr, uint32_t size, uint64_t* buf, uint8_t bytemask)
{
mmiexample_s * instance_data = (mmiexample_s *) obj;
// should check args like paddr, size etc but for now we assume that a uint64_t is passed in *buf
if (wr) {
instance_data->counter = (int) *buf;
return 0;
} else {
*buf = (uint64_t) (++ instance_data->counter);
return 0;
}
}