stdlib,python: Allow setting of to tick exits via m5

This commit adds the following functions to the `m5` python module:

- setMaxTick(tick) -> None
- getMaxTick() -> int
- getTicksUntilMax() -> int
- scheduleTickExitFromCurrent(tick, exit_string) -> None
- scheduleTickExitAbsolute(tick, exit_string) -> None

Until this patch the only way to set an exit at a particular tick was
via `simulate.run` which would reschedule the maximum tick. This
functionality has been explicity exposed via the new `setMaxTick`
function. However, as this is only rescheduling the maximum tick, it
stops scheduling exits at multiple different ticks.

To get around this problem the `scheduleTickExit` functions have been
added. These allow a user to schedule multiple exit events. The
functions contain a `exit_string` parameter that provides the string
the simulator is to return when the specified tick is met. By default
this string is "Tick exit reached" which is used by the stdlib
Simulator module to declare a new `SCHEDULED_TICK` exit event (Note:
this has been deliberatly kept seperate from the `MAX_TICK` exit event.
This commit serves as an attempt to decouple these are two concepts).

Tests are provided in this patch to ensure these new functions work as
intended.

Additional notes:
- The `simulate` function has been fixed to match the documentation. If
  the `num_cycles` is -1 then the maximum ticks is set to MaxTicks.
  Otherwise the max ticks is set to `curTicks() + num_cycles`. The
  functionality of this function will remain unchanged to the end-user.
- Full integration into the Simulator module is not complete as of this
  patch. Users must us the m5 python module to set these exit events.

Change-Id: I6c92b31dd409dc866152224600ea8166cfcba38b
Issue-on: https://gem5.atlassian.net/browse/GEM5-1131
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/66231
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/66331
Reviewed-by: Bobby Bruce <bbruce@ucdavis.edu>
Maintainer: Bobby Bruce <bbruce@ucdavis.edu>
diff --git a/src/python/gem5/simulate/exit_event.py b/src/python/gem5/simulate/exit_event.py
index 0890178..1e14fdd 100644
--- a/src/python/gem5/simulate/exit_event.py
+++ b/src/python/gem5/simulate/exit_event.py
@@ -42,6 +42,7 @@
     SWITCHCPU = "switchcpu"  # An exit needed to switch CPU cores.
     FAIL = "fail"  # An exit because the simulation has failed.
     CHECKPOINT = "checkpoint"  # An exit to load a checkpoint.
+    SCHEDULED_TICK = "scheduled tick exit"
     MAX_TICK = "max tick"  # An exit due to a maximum tick value being met.
     USER_INTERRUPT = (  # An exit due to a user interrupt (e.g., cntr + c)
         "user interupt"
@@ -75,6 +76,8 @@
             return ExitEvent.EXIT
         elif exit_string == "simulate() limit reached":
             return ExitEvent.MAX_TICK
+        elif exit_string == "Tick exit reached":
+            return ExitEvent.SCHEDULED_TICK
         elif exit_string == "switchcpu":
             return ExitEvent.SWITCHCPU
         elif exit_string == "m5_fail instruction encountered":
diff --git a/src/python/gem5/simulate/simulator.py b/src/python/gem5/simulate/simulator.py
index 1d0d3ec..e27679a 100644
--- a/src/python/gem5/simulate/simulator.py
+++ b/src/python/gem5/simulate/simulator.py
@@ -157,6 +157,7 @@
             * ExitEvent.WORKEND: exit simulation
             * ExitEvent.USER_INTERRUPT: exit simulation
             * ExitEvent.MAX_TICK: exit simulation
+            * ExitEvent.SCHEDULED_TICK: exit simulation
             * ExitEvent.SIMPOINT_BEGIN: reset stats
             * ExitEvent.MAX_INSTS: exit simulation
 
@@ -197,6 +198,7 @@
             )(),
             ExitEvent.USER_INTERRUPT: exit_generator(),
             ExitEvent.MAX_TICK: exit_generator(),
+            ExitEvent.SCHEDULED_TICK: exit_generator(),
             ExitEvent.SIMPOINT_BEGIN: warn_default_decorator(
                 reset_stats_generator,
                 "simpoint begin",
diff --git a/src/python/m5/simulate.py b/src/python/m5/simulate.py
index a47d4ca..744d95f 100644
--- a/src/python/m5/simulate.py
+++ b/src/python/m5/simulate.py
@@ -54,7 +54,7 @@
 from m5.util.dot_writer import do_dot, do_dvfs_dot
 from m5.util.dot_writer_ruby import do_ruby_dot
 
-from .util import fatal
+from .util import fatal, warn
 from .util import attrdict
 
 # define a MaxTick parameter, unsigned 64 bit
@@ -205,6 +205,63 @@
     return sim_out
 
 
+def setMaxTick(tick: int) -> None:
+    """Sets the maximum tick the simulation may run to. When when using the
+    stdlib simulator module, reaching this max tick triggers a
+    `ExitEvent.MAX_TICK` exit event.
+
+    :param tick: the maximum tick (absolute, not relative to the current tick).
+    """
+    if tick <= curTick():
+        warn("Max tick scheduled for the past. This will not be triggered.")
+    _m5.event.setMaxTick(tick=tick)
+
+
+def getMaxTick() -> int:
+    """Returns the current maximum tick."""
+    return _m5.event.getMaxTick()
+
+
+def getTicksUntilMax() -> int:
+    """Returns the current number of ticks until the maximum tick."""
+    return getMaxTick() - curTick()
+
+
+def scheduleTickExitFromCurrent(
+    ticks: int, exit_string: str = "Tick exit reached"
+) -> None:
+    """Schedules a tick exit event from the current tick. I.e., if ticks == 100
+    then an exit event will be scheduled at tick `curTick() + 100`.
+
+    The default `exit_string` value is used by the stdlib Simulator module to
+    declare this exit event as `ExitEvent.SCHEDULED_TICK`.
+
+    :param ticks: The simulation ticks, from `curTick()` to schedule the exit
+    event.
+    :param exit_string: The exit string to return when the exit event is
+    triggered.
+    """
+    scheduleTickExitAbsolute(tick=ticks + curTick(), exit_string=exit_string)
+
+
+def scheduleTickExitAbsolute(
+    tick: int, exit_string: str = "Tick exit reached"
+) -> None:
+    """Schedules a tick exit event using absolute ticks. I.e., if tick == 100
+    then an exit event will be scheduled at tick 100.
+
+    The default `exit_string` value is used by the stdlib Simulator module to
+    declare this exit event as `ExitEvent.SCHEDULED_TICK`.
+
+    :param tick: The absolute simulation tick to schedule the exit event.
+    :param exit_string: The exit string to return when the exit event is
+    triggered.
+    """
+    if tick <= curTick():
+        warn("Tick exit scheduled for the past. This will not be triggered.")
+    _m5.event.scheduleTickExit(tick=tick, exit_string=exit_string)
+
+
 def drain():
     """Drain the simulator in preparation of a checkpoint or memory mode
     switch.
diff --git a/src/python/pybind11/event.cc b/src/python/pybind11/event.cc
index 7a02221..827768f 100644
--- a/src/python/pybind11/event.cc
+++ b/src/python/pybind11/event.cc
@@ -107,6 +107,10 @@
 
     m.def("simulate", &simulate,
           py::arg("ticks") = MaxTick);
+    m.def("setMaxTick", &set_max_tick, py::arg("tick"));
+    m.def("getMaxTick", &get_max_tick, py::return_value_policy::copy);
+    m.def("scheduleTickExit", &schedule_tick_exit, py::arg("tick"),
+        py::arg("exit_string"));
     m.def("terminateEventQueueThreads", &terminateEventQueueThreads);
     m.def("exitSimLoop", &exitSimLoop);
     m.def("getEventQueue", []() { return curEventQueue(); },
diff --git a/src/sim/simulate.cc b/src/sim/simulate.cc
index c5d0794..4993859 100644
--- a/src/sim/simulate.cc
+++ b/src/sim/simulate.cc
@@ -180,16 +180,14 @@
 };
 
 /** Simulate for num_cycles additional cycles.  If num_cycles is -1
- * (the default), do not limit simulation; some other event must
- * terminate the loop.  Exported to Python.
+ * (the default), we simulate to MAX_TICKS unless the max ticks has been set
+ * via the 'set_max_tick' function prior. This function is exported to Python.
  * @return The SimLoopExitEvent that caused the loop to exit.
  */
 GlobalSimLoopExitEvent *
 simulate(Tick num_cycles)
 {
     std::unique_ptr<GlobalSyncEvent, DescheduleDeleter> quantum_event;
-    const Tick exit_tick = num_cycles < MaxTick - curTick() ?
-                                        curTick() + num_cycles : MaxTick;
 
     inform("Entering event queue @ %d.  Starting simulation...\n", curTick());
 
@@ -197,11 +195,22 @@
         simulatorThreads.reset(new SimulatorThreads(numMainEventQueues));
 
     if (!simulate_limit_event) {
-        simulate_limit_event = new GlobalSimLoopExitEvent(
-            mainEventQueue[0]->getCurTick(),
-            "simulate() limit reached", 0);
+        // If the simulate_limit_event is not set, we set it to MaxTick.
+        set_max_tick(MaxTick);
     }
-    simulate_limit_event->reschedule(exit_tick);
+
+    if (num_cycles != -1) {
+        // If the user has specified an exit event after X cycles, do so here.
+        // Note: This will override any prior set max_tick behaviour (such as
+        // that above when it is set to MAxTick).
+        const Tick max_tick = num_cycles < MaxTick - curTick() ?
+                                    curTick() + num_cycles : MaxTick;
+
+        // This is kept to `set_max_tick` instead of `schedule_tick_exit` to
+        // preserve backwards functionality. It may be better to deprecate this
+        // behaviour at some point in favor of `schedule_tick_exit`.
+        set_max_tick(max_tick);
+    }
 
     if (numMainEventQueues > 1) {
         fatal_if(simQuantum == 0,
@@ -231,6 +240,34 @@
     return global_exit_event;
 }
 
+void set_max_tick(Tick tick)
+{
+    if (!simulate_limit_event) {
+        simulate_limit_event = new GlobalSimLoopExitEvent(
+            mainEventQueue[0]->getCurTick(),
+            "simulate() limit reached", 0);
+    }
+    simulate_limit_event->reschedule(tick);
+}
+
+
+Tick get_max_tick()
+{
+    if (!simulate_limit_event) {
+        /* If the GlobalSimLoopExitEvent has not been setup, the maximum tick
+         * is `MaxTick` as declared in "src/base/types.hh".
+         */
+        return MaxTick;
+    }
+
+    return simulate_limit_event->when();
+}
+
+void schedule_tick_exit(Tick tick, std::string exit_string)
+{
+    new GlobalSimLoopExitEvent(tick, exit_string, 0);
+}
+
 void
 terminateEventQueueThreads()
 {
diff --git a/src/sim/simulate.hh b/src/sim/simulate.hh
index 5ef4995..e7c4fa6 100644
--- a/src/sim/simulate.hh
+++ b/src/sim/simulate.hh
@@ -45,7 +45,37 @@
 
 class GlobalSimLoopExitEvent;
 
-GlobalSimLoopExitEvent *simulate(Tick num_cycles = MaxTick);
+GlobalSimLoopExitEvent *simulate(Tick num_cycles = -1);
+
+/**
+ * @brief Set the maximum tick.
+ *
+ * This function will schedule, or reschedule, the maximum tick for the
+ * simulation.
+ *
+ * This will setup the GlobalSimLoopExitEvent if it does not already exist.
+ *
+ * @param tick The maximum tick.
+ */
+void set_max_tick(Tick tick);
+
+/**
+ * @brief Get the maximum simulation tick.
+ *
+ *
+ * @returns The maximum simulation tick.
+ */
+Tick get_max_tick();
+
+/**
+ * @brief Schedule an exit event at a particular tick.
+ *
+ * Schedule a tick with a particular exit string.
+ *
+ * @param tick The tick at which the simulation loop should exit.
+ * @param exit_string The exit string explaining the exit.
+ */
+void schedule_tick_exit(Tick tick, std::string exit_string);
 
 /**
  * Terminate helper threads when running in parallel mode.
diff --git a/tests/gem5/to_tick/configs/tick-exit.py b/tests/gem5/to_tick/configs/tick-exit.py
new file mode 100644
index 0000000..9b412cb
--- /dev/null
+++ b/tests/gem5/to_tick/configs/tick-exit.py
@@ -0,0 +1,100 @@
+# Copyright (c) 2022 The Regents of the University of California
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+"""
+
+"""
+
+from gem5.resources.resource import Resource
+from gem5.isas import ISA
+from gem5.components.memory import SingleChannelDDR3_1600
+from gem5.components.boards.simple_board import SimpleBoard
+from gem5.components.cachehierarchies.classic.no_cache import NoCache
+from gem5.components.processors.simple_processor import SimpleProcessor
+from gem5.components.processors.cpu_types import CPUTypes
+from gem5.simulate.simulator import Simulator
+from gem5.simulate.exit_event import ExitEvent
+
+import m5
+
+import argparse
+
+parser = argparse.ArgumentParser()
+
+parser.add_argument(
+    "-t",
+    "--tick-exits",
+    type=int,
+    nargs="+",
+    required=True,
+    help="Set the tick exits to exit.",
+)
+
+parser.add_argument(
+    "-r",
+    "--resource-directory",
+    type=str,
+    required=False,
+    help="The directory in which resources will be downloaded or exist.",
+)
+
+args = parser.parse_args()
+
+# Setup the system.
+motherboard = SimpleBoard(
+    clk_freq="3GHz",
+    processor=SimpleProcessor(
+        cpu_type=CPUTypes.TIMING,
+        isa=ISA.X86,
+        num_cores=1,
+    ),
+    memory=SingleChannelDDR3_1600(),
+    cache_hierarchy=NoCache(),
+)
+
+# Set the workload
+binary = Resource(
+    "x86-hello64-static", resource_directory=args.resource_directory
+)
+motherboard.set_se_binary_workload(binary)
+
+
+def scheduled_tick_generator():
+    while True:
+        print(f"Exiting at: {m5.curTick()}")
+        yield False
+
+
+# Run the simulation
+simulator = Simulator(
+    board=motherboard,
+    on_exit_event={ExitEvent.SCHEDULED_TICK: scheduled_tick_generator()},
+)
+
+for tick in args.tick_exits:
+    m5.scheduleTickExitFromCurrent(tick)
+
+simulator.run()
diff --git a/tests/gem5/to_tick/configs/tick-to-max.py b/tests/gem5/to_tick/configs/tick-to-max.py
new file mode 100644
index 0000000..2b679df
--- /dev/null
+++ b/tests/gem5/to_tick/configs/tick-to-max.py
@@ -0,0 +1,123 @@
+# Copyright (c) 2022 The Regents of the University of California
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+"""
+This configuration script is used to test running a simulation to a specified
+maximum tick. This script was setup to test setting the number of ticks to
+run before, at, or after the running of `simulator.run`.
+
+**Note:** There can only ever be one MAX_TICK exit event scheduled at any one
+time.
+"""
+
+from gem5.resources.resource import Resource
+from gem5.isas import ISA
+from gem5.components.memory import SingleChannelDDR3_1600
+from gem5.components.boards.simple_board import SimpleBoard
+from gem5.components.cachehierarchies.classic.no_cache import NoCache
+from gem5.components.processors.simple_processor import SimpleProcessor
+from gem5.components.processors.cpu_types import CPUTypes
+from gem5.simulate.simulator import Simulator
+
+import m5
+
+import argparse
+
+parser = argparse.ArgumentParser()
+
+parser.add_argument(
+    "-b",
+    "--set-ticks-before",
+    type=int,
+    required=False,
+    help="Set the number of ticks to run to prior to executing "
+    "`simulator.run`.",
+)
+
+parser.add_argument(
+    "-e",
+    "--set-ticks-at-execution",
+    type=int,
+    required=False,
+    help="Set the number of ticks to run via `simulator.run`.",
+)
+
+parser.add_argument(
+    "-a",
+    "--set-ticks-after",
+    type=int,
+    required=False,
+    help="Set the number of ticks to run after `simulator.run` has ceased "
+    "execution.",
+)
+
+parser.add_argument(
+    "-r",
+    "--resource-directory",
+    type=str,
+    required=False,
+    help="The directory in which resources will be downloaded or exist.",
+)
+
+args = parser.parse_args()
+
+# Setup the system.
+motherboard = SimpleBoard(
+    clk_freq="3GHz",
+    processor=SimpleProcessor(
+        cpu_type=CPUTypes.TIMING,
+        isa=ISA.X86,
+        num_cores=1,
+    ),
+    memory=SingleChannelDDR3_1600(),
+    cache_hierarchy=NoCache(),
+)
+
+# Set the workload
+binary = Resource(
+    "x86-hello64-static", resource_directory=args.resource_directory
+)
+motherboard.set_se_binary_workload(binary)
+
+# Set the max ticks before setting up the simulation, if applicable.
+if args.set_ticks_before:
+    m5.setMaxTick(args.set_ticks_before)
+
+# Run the simulation
+simulator = Simulator(board=motherboard)
+
+if args.set_ticks_at_execution:
+    simulator.run(max_ticks=args.set_ticks_at_execution)
+else:
+    simulator.run()
+
+# Set the max ticks after the simulator run.
+if args.set_ticks_after:
+    m5.setMaxTick(args.set_ticks_after)
+
+print(f"Current Tick: {m5.curTick()}")
+print(f"Current Max Tick: {m5.getMaxTick()}")
+print(f"Ticks until max: {m5.getTicksUntilMax()}")
diff --git a/tests/gem5/to_tick/ref/tick-exit-10-20-30-40.txt b/tests/gem5/to_tick/ref/tick-exit-10-20-30-40.txt
new file mode 100644
index 0000000..05f8159
--- /dev/null
+++ b/tests/gem5/to_tick/ref/tick-exit-10-20-30-40.txt
@@ -0,0 +1,6 @@
+Global frequency set at 1000000000000 ticks per second
+Exiting at: 10
+Exiting at: 20
+Exiting at: 30
+Exiting at: 40
+Hello world!
diff --git a/tests/gem5/to_tick/ref/tick-exit-100.txt b/tests/gem5/to_tick/ref/tick-exit-100.txt
new file mode 100644
index 0000000..62f9330
--- /dev/null
+++ b/tests/gem5/to_tick/ref/tick-exit-100.txt
@@ -0,0 +1,3 @@
+Global frequency set at 1000000000000 ticks per second
+Exiting at: 100
+Hello world!
diff --git a/tests/gem5/to_tick/ref/tick-to-max-at-execution-100.txt b/tests/gem5/to_tick/ref/tick-to-max-at-execution-100.txt
new file mode 100644
index 0000000..1507716
--- /dev/null
+++ b/tests/gem5/to_tick/ref/tick-to-max-at-execution-100.txt
@@ -0,0 +1,4 @@
+Global frequency set at 1000000000000 ticks per second
+Current Tick: 100
+Current Max Tick: 100
+Ticks until max: 0
diff --git a/tests/gem5/to_tick/ref/tick-to-max-at-execution-and-after-100-200.txt b/tests/gem5/to_tick/ref/tick-to-max-at-execution-and-after-100-200.txt
new file mode 100644
index 0000000..b1cde8a
--- /dev/null
+++ b/tests/gem5/to_tick/ref/tick-to-max-at-execution-and-after-100-200.txt
@@ -0,0 +1,4 @@
+Global frequency set at 1000000000000 ticks per second
+Current Tick: 100
+Current Max Tick: 200
+Ticks until max: 100
diff --git a/tests/gem5/to_tick/ref/tick-to-max-before-execution-250.txt b/tests/gem5/to_tick/ref/tick-to-max-before-execution-250.txt
new file mode 100644
index 0000000..b26e9eb
--- /dev/null
+++ b/tests/gem5/to_tick/ref/tick-to-max-before-execution-250.txt
@@ -0,0 +1,4 @@
+Global frequency set at 1000000000000 ticks per second
+Current Tick: 250
+Current Max Tick: 250
+Ticks until max: 0
diff --git a/tests/gem5/to_tick/test_to_tick.py b/tests/gem5/to_tick/test_to_tick.py
new file mode 100644
index 0000000..ba5bcbf
--- /dev/null
+++ b/tests/gem5/to_tick/test_to_tick.py
@@ -0,0 +1,174 @@
+# Copyright (c) 2022 The Regents of the University of California
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+from testlib import *
+
+if config.bin_path:
+    resource_path = config.bin_path
+else:
+    resource_path = joinpath(absdirpath(__file__), "..", "resources")
+
+# This test sets the tick to max tick via the `simulator.run` function. This is
+# set to 100. Therefore, at the end of the execution the expected current tick
+# should be 100, with the max tick still 100. The number of expected ticks to
+# max is therefore 0.
+gem5_verify_config(
+    name="test-to-max-tick-at-execution-100",
+    verifiers=[
+        verifier.MatchStdoutNoPerf(
+            joinpath(getcwd(), "ref", "tick-to-max-at-execution-100.txt")
+        )
+    ],
+    fixtures=(),
+    config=joinpath(
+        config.base_dir,
+        "tests",
+        "gem5",
+        "to_tick",
+        "configs",
+        "tick-to-max.py",
+    ),
+    config_args=[
+        "--resource-directory",
+        resource_path,
+        "--set-ticks-at-execution",
+        "100",
+    ],
+    valid_isas=(constants.all_compiled_tag,),
+    valid_hosts=constants.supported_hosts,
+    length=constants.quick_tag,
+)
+
+# This test sets the max tick  via the `simulator.run` function at tick 100.
+# The `m5.setMaxTick` function is then called after, passing the value 200 .
+# This means at the end of execution the current tick is 100, and the max tick
+# is 200. The number of expected ticks to max is therefore 100.
+gem5_verify_config(
+    name="test-to-max-tick-at-execution-and-after-100-200",
+    verifiers=[
+        verifier.MatchStdoutNoPerf(
+            joinpath(
+                getcwd(),
+                "ref",
+                "tick-to-max-at-execution-and-after-100-200.txt",
+            )
+        )
+    ],
+    fixtures=(),
+    config=joinpath(
+        config.base_dir,
+        "tests",
+        "gem5",
+        "to_tick",
+        "configs",
+        "tick-to-max.py",
+    ),
+    config_args=[
+        "--resource-directory",
+        resource_path,
+        "--set-ticks-at-execution",
+        "100",
+        "--set-ticks-after",
+        "200",
+    ],
+    valid_isas=(constants.all_compiled_tag,),
+    valid_hosts=constants.supported_hosts,
+    length=constants.quick_tag,
+)
+
+# This test sets the max tick to 250 via the `m5.setMaxTick` prior to running
+# `simulator.run`. This means at the end of execution the current tick is 250
+# and the max tick is 250. The expected number of ticks to max is therefore 0.
+gem5_verify_config(
+    name="test-to-max-tick-before-execution-250",
+    verifiers=[
+        verifier.MatchStdoutNoPerf(
+            joinpath(getcwd(), "ref", "tick-to-max-before-execution-250.txt")
+        )
+    ],
+    fixtures=(),
+    config=joinpath(
+        config.base_dir,
+        "tests",
+        "gem5",
+        "to_tick",
+        "configs",
+        "tick-to-max.py",
+    ),
+    config_args=[
+        "--resource-directory",
+        resource_path,
+        "--set-ticks-before",
+        "250",
+    ],
+    valid_isas=(constants.all_compiled_tag,),
+    valid_hosts=constants.supported_hosts,
+    length=constants.quick_tag,
+)
+
+# Tests the scheduling of a tick exit event at tick 100.
+gem5_verify_config(
+    name="test-to-tick-exit-100",
+    verifiers=[
+        verifier.MatchStdoutNoPerf(
+            joinpath(getcwd(), "ref", "tick-exit-100.txt")
+        )
+    ],
+    fixtures=(),
+    config=joinpath(
+        config.base_dir, "tests", "gem5", "to_tick", "configs", "tick-exit.py"
+    ),
+    config_args=["--resource-directory", resource_path, "--tick-exits", "100"],
+    valid_isas=(constants.all_compiled_tag,),
+    valid_hosts=constants.supported_hosts,
+    length=constants.quick_tag,
+)
+
+# Tests the scheduling of a tick exit event at tick 10, 20, 30, and 40.
+gem5_verify_config(
+    name="test-to-tick-exit-10-20-30-40",
+    verifiers=[
+        verifier.MatchStdoutNoPerf(
+            joinpath(getcwd(), "ref", "tick-exit-10-20-30-40.txt")
+        )
+    ],
+    fixtures=(),
+    config=joinpath(
+        config.base_dir, "tests", "gem5", "to_tick", "configs", "tick-exit.py"
+    ),
+    config_args=[
+        "--resource-directory",
+        resource_path,
+        "--tick-exits",
+        "10",
+        "20",
+        "30",
+        "40",
+    ],
+    valid_isas=(constants.all_compiled_tag,),
+    valid_hosts=constants.supported_hosts,
+    length=constants.quick_tag,
+)