Gem5 (3) Creating a SimObject

编辑于 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()))

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值