编辑于 2020/5/20
Python Class
from m5.params import *
from m5.SimObject import SimObject
class HelloObject(SimObject):
type = 'HelloObject'
cxx_header = "learning_gem5/simple_SimObject/hello_object.hh"
SimObject is inherited from m5, files can be found in src/python/m5.
C++ Class
Header File
The only thing we need to do in the file is to declare our class. Since HelloObject is a SimObject, it must inherit from the C++ SimObject class. Most of the time, your SimObject’s parent will be a subclass of SimObject, not SimObject itself.
p.s. I still don't understand what the above bold sentence means.
The header file is shown below:
#ifndef __LEARNING_GEM5_HELLO_OBJECT_HH__
#define __LEARNING_GEM5_HELLO_OBJECT_HH__
#include "params/HelloObject.hh"
#include "sim/sim_object.hh"
class HelloObject : public SimObject
{
public:
HelloObject(HelloObjectParams *p);
};
#endif // __LEARNING_GEM5_HELLO_OBJECT_HH__
C++ File
Next, we need to implement two functions in the .cc file, not just one. The first function, is the constructor for the HelloObject. Another function that we must implement is implicitly created from the SimObject Python declaration, namely, the create function. This function simply returns a new instantiation of the SimObject. Usually this function is very simple.
#include "learning_gem5/simple_SimObject/hello_object.hh"
#include <iostream>
HelloObject::HelloObject(HelloObjectParams *params):
SimObject(params)
{
std::cout << "Hello World! From a SimObject!" << std::endl;
}
HelloObject*
HelloObjectParams::create()
{
return new HelloObject(this);
}
Scons File
Gem5 uses SCons as the build system, so you simply have to create a SConscript file in the directory with the code for the SimObject.
Import('*')
SimObject('HelloObject.py')
Source('hello_object.cc')
Rebuild Gem5
scons build/X86/gem5.opt
Configuration Script
import m5
from m5.objects import *
root = Root(full_system = False)
root.hello = HelloObject()
m5.instantiate()
print("Beginning simulation!")
exit_event = m5.simulate()
print('Exiting @ tick {} because {}'.
format(m5.curTick(), exit_event.getCause()))