Gem5 学习 4 - 搭建基本的系统

本文档按照gem5官方教程记录了学习gem5的过程,包括使用simple.py和caches.py+two_level.py搭建带缓存的简单系统。内容涉及系统架构、缓存模型(Classic caches vs Ruby)、如何修改配置以使用自己的测试程序,并展示了测试结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面按照gem5官网的学习手册记录一下学习过程,便于复习
注意
1、我参考的是老版本教程。因为我用的版本比较低,新版本在mem_ctrl那边的设置不太一样,但是他们的源代码都在./configs/learning_gem5/part1/simple.py,可以直接看
2、我修改了运行的测试程序。在上一篇博客里我写了一个和helloworld类似的测试程序,这里我改成了自己的。具体编译过程见gem5学习-用se模式测试自己的程序
3、我在configs下新建了文件夹。因此我的路径是./configs/sys_code/simple.py,这个在caches.py的代码里面需要根据自己的情况调整

学习gem5 Part 1(搭建基本系统)

源代码
官方教程在
http://learning.gem5.org/book/part1/simple_config.html
http://learning.gem5.org/book/part1/cache_config.html

simple.py

我们的配置脚本将为一个非常简单的系统建模。我们只有一个简单的CPU内核。该CPU内核将连接到系统范围的内存总线。而且,我们将有一个DDR3内存通道,该通道也已连接到内存总线。
下面是搭建系统的示意图
image

在configs文件夹下新建一个文件夹,放置测试的代码
比如我./configs/sys_code/simple.py
把下面的代码拷进去
注意,我把binary改成自己的了

# -*- coding: utf-8 -*-
""" This file creates a barebones system and executes 'hello', a simple Hello World application.
翻:这个文件创建基本的系统并运行helloworld

See Part 1, Chapter 2: Creating a simple configuration script in the
learning_gem5 book for more information about this script.
翻:看learning_gem5可以详细的学习这个脚本

IMPORTANT: If you modify this file, it's likely that the Learning gem5 book also needs to be updated. For now, email Jason <power.jg@gmail.com>
翻:如果你修正了文件,说明学习手册也得改,通知下Jason
"""


from __future__ import print_function

# import the m5 (gem5) library created when gem5 is built
# 导入gem5库文件,注意这个脚本直接用python是跑不通的,我们得在编译后的gem5.opt下面跑,所以刚才的命令前面要加build/X86/gem5.opt来执行
import m5
# import all of the SimObjects
# 直接把m5里面的objects类别直接导入,这样就不用每次再写m5.object.simobjectxxx了
from m5.objects import *

# create the system we are going to simulate
# System()函数创建系统,system下面有时钟/cpu/内存/等子模块,也就是我们下面设置的clk_domain/cpu/mem等
system = System()

# Set the clock fequency of the system (and all of its children)
# 创建完系统,开始设置里面的参数
# 设置sys中的时钟参数,SrcClockDomain()函数设置时钟域,在时钟域上用.clock设置频率,.voltage_domain设置电压,这里使用了VoltageDomain()函数设置为默认电压域
system.clk_domain = SrcClockDomain()
system.clk_domain.clock = '1GHz'
system.clk_domain.voltage_domain = VoltageDomain()

# Set up the system
'''
You will almost always use timing mode for the memory simulation, except in special cases like fast-forwarding and restoring from a checkpoint.
翻:正常情况都用timing模式,支持'512MB'或'2ns'这种内存或时间单位
'''
# 开始模拟内存,sys中的mem_mode这里使用的是'timing'模式
# 内存的范围是512MB(基本的计算机)
system.mem_mode = 'timing'               # Use timing accesses
system.mem_ranges = [AddrRange('512MB')] # Create an address range

# Create a simple CPU
# 开始创建CPU,使用TimingSimpleCPU()函数,创建一个基于计时模式的CPU
'''
This CPU model executes each instruction in a single clock cycle to execute, except memory requests, which flow through the memory system.
翻:计时模式的CPU在单个时钟周期内执行每条指令,除了内存请求(通过内存系统)
'''
system.cpu = TimingSimpleCPU()

# Create a memory bus, a system crossbar, in this case
# 用SystemXBar()创建内存总线
system.membus = SystemXBar()

# Hook the CPU ports up to the membus
# 让CPU的缓存连接到内存总线上,因为系统很简单,所以没缓存,直接连到总线
system.cpu.icache_port = system.membus.slave
system.cpu.dcache_port = system.membus.slave

# create the interrupt controller for the CPU and connect to the membus
# 创建CPU中断控制,连接内存总线 createInterruptController()
system.cpu.createInterruptController()

# For x86 only, make sure the interrupts are connected to the memory
# Note: these are directly connected to the memory bus and are not cached
# 中断直连内存总线,无缓存;pio和中断端口连接内存是X86的要求,其他ISA(如ARM)不需要这三条
# 指令集架构(英语:Instruction Set Architecture,缩写为ISA)
# m5.defines.buildEnv['TARGET_ISA']检查当前ISA架构
if m5.defines.buildEnv['TARGET_ISA'] == "x86":
    system.cpu.interrupts[0].pio = system.membus.master
    system.cpu.interrupts[0].int_master = system.membus.slave
    system.cpu.interrupts[0].int_slave = system.membus.master

# Create a DDR3 memory controller and connect it to the membus
# 创建DDR3内存控制器直连内存总线,system.mem_ctrl
system.mem_ctrl = DDR3_1600_8x8()
system.mem_ctrl.range = system.mem_ranges[0]
system.mem_ctrl.port = system.membus.master

# C
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小张的学习手册

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值