Difference between revisions of "Defining CPU Models (as of M5 2.0 - beta 3)"

From gem5
Jump to: navigation, search
(Building MyCPU)
(Building MyCPU)
Line 130: Line 130:
 
Navigate to the M5 top-level directory and build your model:
 
Navigate to the M5 top-level directory and build your model:
 
<pre>
 
<pre>
scons build/MIPS_SE/m5.debug CPU_MODELS=MyCPU
+
me@mymachine:~/m5/src/python/objects$cd ~/m5
 +
me@mymachine:~/m5$scons build/MIPS_SE/m5.debug CPU_MODELS=MyCPU
 +
 
 
</pre>
 
</pre>
  

Revision as of 18:51, 22 May 2007

Overview

First, make sure you have basic understanding of how the CPU models function within the M5 framework. A good start is the CPU Models page.

This brief tutorial will show you how to create a custom CPU model called 'MyCPU', which will just be a renamed version of the AtomicSimpleCPU. After you learn how to compile and build 'MyCPU', then you have the liberty to edit the 'MyCPU' code at your heart's content without worrying about breaking any existing M5 CPU Models.

Port C++ Code for MyCPU

The easiest way is to derive a new C++ class of your CPU Model from M5 CPU Models that are already defined and the easiest model to start with is the 'AtomicSimpleCPU' located in the 'm5/src/cpu/simple' directory.


For this example, we'll just copy the files from the 'm5/src/cpu/simple' and place them in our own CPU directory: m5/src/cpu/mycpu.

me@mymachine:~/m5$ cd src/cpu 
me@mymachine:~/m5/src/cpu$ mkdir mycpu 
me@mymachine:~/m5/src/cpu$ cp -r simple/* mycpu


Now check the mycpu directory to make sure you've copied the files successfully:

me@mymachine:~/m5/src/cpu$ cd mycpu 
me@mymachine:~/m5/src/cpu/mycpu$ ls
atomic.cc  atomic.hh  base.cc  base.hh  timing.cc  timing.hh


Since we want to change 'AtomicSimpleCPU' to 'MyCPU' we will just replace all the names in the atomic.* files and name them mycpu.* files:

me@mymachine:~/m5/src/cpu/mycpu$ perl -pe s/AtomicSimpleCPU/MyCPU/g atomic.hh > mycpu.hh
me@mymachine:~/m5/src/cpu/mycpu$ perl -pe s/AtomicSimpleCPU/MyCPU/g atomic.cc > mycpu.cc
me@mymachine:~/m5/src/cpu/mycpu$ ls
atomic.cc  atomic.hh  base.cc  base.hh  mycpu.hh mycpu.cc timing.cc  timing.hh


NOTE: The AtomicSimpleCPU is really just based off the BaseSimpleCPU (src/cpu/simple/base.hh) so your new CPU Model MyCPU is really a derivation off of this CPU model. Additionally, the BaseSimpleCPU model is derived from the BaseCPU (src/cpu/base.hh) so you can see that M5 is heavily object oriented.

Making M5 Recognize MyCPU

Now that you've created a separate directory and files for your MyCPU code (i.e. m5/src/cpu/mycpu), there are a couple files that need to be updated so that M5 can recognize M5 as a build option:

  • m5/SConstruct: Add the name of your CPU model (MyCPU) to the 'ALL_CPU_LIST'
...
# Define the universe of supported CPU models
env['ALL_CPU_LIST'] = ['AtomicSimpleCPU', 'TimingSimpleCPU',
                       'FullCPU', 'O3CPU','MixieCPU',
                       'OzoneCPU', 'MyCPU']
... ..
  • m5/src/cpu/SConscript: Add your CPU model and the relevant files that need to be built in here
... 
if 'AtomicSimpleCPU' in env['CPU_MODELS']:
    need_simple_base = True
    sources += Split('simple/atomic.cc')

if 'MyCPU' in env['CPU_MODELS']:
    need_simple_base = True
    sources += Split('simple/mycpu.cc')
...
  • m5/src/cpu/static_inst.hh: Put a forward class declaration of your model in here
...
class CheckerCPU;
class FastCPU;
class AtomicSimpleCPU;
class TimingSimpleCPU;
class InorderCPU;
class MyCPU;
...
  • m5/src/cpu/cpu_models.py: Add in CPU Model-specific information for the ISA Parser. The ISA Parser will use this when referring to the "Execution Context" for executing instructions. For instance, the AtomicSimpleCPU's instructions get all of their information from the actual CPU (since it's a 1 CPI machine). Thus, instructions only need to know the current state or "Execution Context" of the 'AtomicSimpleCPU' object. However, the instructions in a O3CPU needed to know the register values (& other state) only known to that current instruction so it's "Execution Context" is the O3DynInst object. (check out the ISA Description Language documentation page for more details)
...
CpuModel('AtomicSimpleCPU', 'atomic_simple_cpu_exec.cc',
         '#include "cpu/simple/atomic.hh"',
         { 'CPU_exec_context': 'AtomicSimpleCPU' })
CpuModel('MyCPU', 'mycpu_exec.cc',
         '#include "cpu/mycpu/mycpu.hh"',
         { 'CPU_exec_context': 'MyCPU' })
...
  • m5/src/python/objects/MyCPU.py: Create a python file (e.g. MyCPU.py) so that your CPU can be recognized as a simulation object. For this example, we will just use the same code in the SimpleCPU.py file but replace the name 'AtomicSimpleCPU' with 'MyCPU'.

Create a file named MyCPU.py file to represent your CPU Model:

me@mymachine:~/m5/src/cpu/mycpu$cd ~/m5/src/python/objects
me@mymachine:~/m5/src/python/objects$emacs MyCPU.py&

Add this to your file:

from m5.params import *
from m5 import build_env
from BaseCPU import BaseCPU

class MyCPU(BaseCPU):
    type = 'MyCPU'
    width = Param.Int(1, "CPU width")
    simulate_stalls = Param.Bool(False, "Simulate cache stall cycles")
    function_trace = Param.Bool(False, "Enable function trace")
    function_trace_start = Param.Tick(0, "Cycle to start function trace")
    if build_env['FULL_SYSTEM']:
        profile = Param.Latency('0ns', "trace the kernel stack")
    icache_port = Port("Instruction Port")
    dcache_port = Port("Data Port")
    _mem_ports = ['icache_port', 'dcache_port']
  • m5/src/python/objects/__init__.py: Add the base name of your python file into the 'file_bases' list.
# specify base part of all object file names
file_bases = ['AlphaConsole',
              'O3CPU',
              'AlphaTLB',

...
	      'MyCPU',
              'OzoneCPU',
...
              'Uart']

Building MyCPU

Navigate to the M5 top-level directory and build your model:

me@mymachine:~/m5/src/python/objects$cd ~/m5
me@mymachine:~/m5$scons build/MIPS_SE/m5.debug CPU_MODELS=MyCPU

If you have dual-core CPU, use this command-line to speed-up the compilation:

scons -j2 build/MIPS_SE/m5.debug CPU_MODELS=MyCPU

Creating Configuration Options For MyCPU

Create and edit configuration files for your model:

  • m5/configs/test/MyCPUConfig.py: Define a configuration class w/corresponding parameters for your model. Look to 'FullO3Config.py' in the same directory for an example of how to do this.
  • m5/configs/test/test.py: Import your model's configuration at the top of the file (i.e. 'import MyCPUConfig') and add in a parser option for your CPU model (e.g. '--my_cpu').


Testing MyCPU

Test your model:

build/ALPHA_SE/m5.debug configs/example/se.py --my_cpu --cmd=<bin_path>