|
| 1 | +# Copyright (c) 2021-2025 The Regents of the University of California |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are |
| 6 | +# met: redistributions of source code must retain the above copyright |
| 7 | +# notice, this list of conditions and the following disclaimer; |
| 8 | +# redistributions in binary form must reproduce the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer in the |
| 10 | +# documentation and/or other materials provided with the distribution; |
| 11 | +# neither the name of the copyright holders nor the names of its |
| 12 | +# contributors may be used to endorse or promote products derived from |
| 13 | +# this software without specific prior written permission. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 18 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 19 | +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 21 | +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +""" |
| 28 | +This gem5 configuation script creates a simple board to run an ARM |
| 29 | +"hello world" binary. |
| 30 | +
|
| 31 | +This setup is close to the simplest setup possible using the gem5 |
| 32 | +library. It does not contain any kind of caching, IO, or any non-essential |
| 33 | +components. |
| 34 | +
|
| 35 | +Usage |
| 36 | +----- |
| 37 | +
|
| 38 | +``` |
| 39 | +scons build/ALL/gem5.opt |
| 40 | +./build/ALL/gem5.opt configs/example/gem5_library/arm-hello.py |
| 41 | +``` |
| 42 | +""" |
| 43 | + |
| 44 | +import argparse |
| 45 | +import os |
| 46 | + |
| 47 | +import m5 |
| 48 | + |
| 49 | +from gem5.components.boards.simple_board import SimpleBoard |
| 50 | +from gem5.components.cachehierarchies.classic.no_cache import NoCache |
| 51 | +from gem5.components.memory import SingleChannelDDR3_1600 |
| 52 | +from gem5.components.processors.cpu_types import CPUTypes |
| 53 | +from gem5.components.processors.simple_processor import SimpleProcessor |
| 54 | +from gem5.isas import ISA |
| 55 | +from gem5.resources.resource import obtain_resource |
| 56 | +from gem5.simulate.simulator import Simulator |
| 57 | +from gem5.utils.requires import requires |
| 58 | + |
| 59 | +parser = argparse.ArgumentParser() |
| 60 | + |
| 61 | +parser.add_argument("--dump-config", action="store_true") |
| 62 | + |
| 63 | +parser.add_argument("--json-config", action="store_true") |
| 64 | + |
| 65 | +parser.add_argument("--dot-config", action="store_true") |
| 66 | + |
| 67 | +args = parser.parse_args() |
| 68 | + |
| 69 | +# This check ensures the gem5 binary contains the ARM ISA target. If not, an |
| 70 | +# exception will be thrown. |
| 71 | +requires(isa_required=ISA.ARM) |
| 72 | + |
| 73 | +# In this setup we don't have a cache. `NoCache` can be used for such setups. |
| 74 | +cache_hierarchy = NoCache() |
| 75 | + |
| 76 | +# We use a single channel DDR3_1600 memory system |
| 77 | +memory = SingleChannelDDR3_1600(size="32MiB") |
| 78 | + |
| 79 | +# We use a simple Timing processor with one core. |
| 80 | +processor = SimpleProcessor(cpu_type=CPUTypes.TIMING, isa=ISA.ARM, num_cores=1) |
| 81 | + |
| 82 | +# The gem5 library simple board which can be used to run SE-mode simulations. |
| 83 | +board = SimpleBoard( |
| 84 | + clk_freq="3GHz", |
| 85 | + processor=processor, |
| 86 | + memory=memory, |
| 87 | + cache_hierarchy=cache_hierarchy, |
| 88 | +) |
| 89 | + |
| 90 | +# Here we set the workload. In this case we want to run a simple "Hello World!" |
| 91 | +# program compiled to the ARM ISA. The `obtain_resource` function will |
| 92 | +# automatically download the binary from the gem5 Resources cloud bucket if |
| 93 | +# it's not already present. |
| 94 | +board.set_se_binary_workload( |
| 95 | + obtain_resource("arm-hello64-static", resource_version="1.0.0") |
| 96 | +) |
| 97 | + |
| 98 | +# Lastly we run the simulation. |
| 99 | +simulator = Simulator(board=board) |
| 100 | +simulator.run() |
| 101 | + |
| 102 | + |
| 103 | +def check_file_exists(file_name: str): |
| 104 | + return os.path.isfile(f"{m5.options.outdir}/{file_name}") |
| 105 | + |
| 106 | + |
| 107 | +if args.dump_config and args.json_config and args.dot_config: |
| 108 | + if ( |
| 109 | + not check_file_exists("combined_config.ini") |
| 110 | + or not check_file_exists("combined_config.json") |
| 111 | + or not check_file_exists("combined_config.dot") |
| 112 | + ): |
| 113 | + print( |
| 114 | + "At least one of combined_config.ini, combined_config.json or " |
| 115 | + "combined_config.dot isn't in m5out!" |
| 116 | + ) |
| 117 | + exit(1) |
| 118 | + |
| 119 | +elif args.dump_config: |
| 120 | + if not check_file_exists("custom_config.ini"): |
| 121 | + print("custom_config.ini isn't in m5out!") |
| 122 | + exit(1) |
| 123 | + |
| 124 | +elif args.json_config: |
| 125 | + if not check_file_exists("custom_config.json"): |
| 126 | + print("custom_config.json isn't in m5out!") |
| 127 | + exit(1) |
| 128 | +elif args.dot_config: |
| 129 | + if ( |
| 130 | + not check_file_exists("custom_config.dot") |
| 131 | + or not check_file_exists("custom_config.dot.pdf") |
| 132 | + or not check_file_exists("custom_config.dot.svg") |
| 133 | + ): |
| 134 | + print( |
| 135 | + "At least one of custom_config.dot, custom_config.dot.pdf or " |
| 136 | + "custom_config.dot.svg isn't in m5out!" |
| 137 | + ) |
| 138 | + exit(1) |
| 139 | + |
| 140 | +else: |
| 141 | + if ( |
| 142 | + not check_file_exists("config.ini") |
| 143 | + or not check_file_exists("config.json") |
| 144 | + or not check_file_exists("config.dot") |
| 145 | + or not check_file_exists("config.dot.pdf") |
| 146 | + or not check_file_exists("config.dot.svg") |
| 147 | + ): |
| 148 | + print( |
| 149 | + "At least one of config.ini, config.json, config.dot, " |
| 150 | + "config.dot.pdf, or config.dot.svg is missing!" |
| 151 | + ) |
| 152 | + exit(1) |
0 commit comments