### Complete Multi-Machine Simulation Example Source: https://context7.com/antmicro/pyrenode3/llms.txt This example demonstrates a comprehensive multi-machine simulation setup in Pyrenode3, including USB connectivity, loading firmware from URLs, and automated terminal testing. ```APIDOC ## Complete Multi-Machine Example ### Description This example demonstrates setting up a complex multi-machine simulation with USB connectivity, loading firmware from URLs, and automated terminal testing. ### Usage Example ```python #!/usr/bin/env python3 from pyrenode3 import RPath from pyrenode3.wrappers import Analyzer, Emulation, Monitor, TerminalTester from Antmicro.Renode.Peripherals.CPU import RegisterValue # Initialize emulation environment e = Emulation() m = Monitor() # Create USB connector for inter-machine communication e.CreateUSBConnector("usb_connector") # Configure FOMU (FPGA USB device) fomu = e.add_mach("fomu") fomu.load_repl("platforms/cpus/fomu.repl") fomu.load_elf( "https://dl.antmicro.com/projects/renode/fomu--foboot.elf-s_112080-c31fe1f32fba7894338f3cf4bfb82ec2a8265683" ) e.Connector.Connect(fomu.sysbus.valenty.internal, e.externals.usb_connector) # Configure HiFive Unleashed (RISC-V Linux host) hifive = e.add_mach("hifive") hifive.load_repl("platforms/cpus/sifive-fu540.repl") hifive.load_elf( "https://dl.antmicro.com/projects/renode/hifive-unleashed--bbl.elf-s_17219640-c7e1b920bf81be4062f467d9ecf689dbf7f29c7a" ) # Load device tree and kernel symbols hifive.sysbus.LoadFdt( RPath("https://dl.antmicro.com/projects/renode/hifive-unleashed--devicetree.dtb-s_10532-70cd4fc9f3b4df929eba6e6f22d02e6ce4c17bd1").path, 0x81000000, "earlyconsole mem=256M@0x80000000" ) hifive.sysbus.LoadSymbolsFrom( RPath("https://dl.antmicro.com/projects/renode/hifive-unleashed--vmlinux.elf-s_80421976-46788813c50dc7eb1a1a33c1730ca633616f75f5").read_file_path ) ``` ``` -------------------------------- ### Install and Configure pyrenode3 Environment Source: https://context7.com/antmicro/pyrenode3/llms.txt Commands to install the library and configure the environment variables required to point to a Renode installation or build. ```bash pip install 'pyrenode3[all] @ git+https://github.com/antmicro/pyrenode3.git' wget https://builds.renode.io/renode-latest.pkg.tar.xz export PYRENODE_PKG=$(pwd)/renode-latest.pkg.tar.xz export PYRENODE_BUILD_DIR=/path/to/renode/sources export PYRENODE_BIN=/path/to/Renode export PYRENODE_RUNTIME=coreclr python -c "import pyrenode3; from pyrenode3.wrappers import Emulation; e = Emulation(); print('Renode loaded!')" ``` -------------------------------- ### Configure CPU Register and Add USB Controller Source: https://context7.com/antmicro/pyrenode3/llms.txt This snippet demonstrates how to set a CPU register to a specific value and add a USB controller to the system description. It's useful for initializing hardware components before simulation starts. ```Python hifive.sysbus.e51.SetRegisterUnsafe(11, RegisterValue.Create(0x81000000, 64)) hifive.LoadPlatformDescriptionFromString( "usb: USB.MPFS_USB @ sysbus 0x30020000 { MainIRQ -> plic@0x20 }" ) ``` -------------------------------- ### Configure Renode Initialization with RenodeLoader Source: https://context7.com/antmicro/pyrenode3/llms.txt RenodeLoader manages the discovery and initialization of Renode binaries. It supports various installation methods including system packages, .NET runtimes, and local build directories via environment variables. ```python from pyrenode3.loader import RenodeLoader import os loader = RenodeLoader() if loader.is_initialized: print(f"Renode root: {loader.root}") os.environ["PYRENODE_PKG"] = "/path/to/renode-latest.pkg.tar.xz" os.environ["PYRENODE_RUNTIME"] = "mono" with loader.in_root(): pass ``` -------------------------------- ### Interact with Peripherals using Peripheral Class Source: https://context7.com/antmicro/pyrenode3/llms.txt Demonstrates the usage of the Peripheral class to access and interact with hardware peripherals through the system bus. It shows how to get peripheral properties, iterate over children, and register event callbacks. ```python from pyrenode3.wrappers import Emulation, Peripheral e = Emulation() machine = e.add_mach("device") machine.load_repl("platforms/cpus/stm30l072.repl") sysbus = machine.sysbus uart = sysbus.usart2 gpio = sysbus.gpioPortA print(f"Peripheral name: {uart.name}") print(f"Peripheral path: {uart.path}") for child in sysbus: print(f"Child: {child.name}") cpu = sysbus.get_child("cpu") parent_machine = uart.mach def on_char_received(char): print(chr(char), end="") uart.CharReceived += on_char_char_received internal_uart = uart.internal ``` -------------------------------- ### Print Simulation Ready Message Source: https://context7.com/antmicro/pyrenode3/llms.txt A simple print statement to indicate that the multi-machine simulation is ready. This is often used as a final confirmation step in simulation setup scripts. ```Python print("Multi-machine simulation ready!") ``` -------------------------------- ### Configure Virtual Machines Source: https://context7.com/antmicro/pyrenode3/llms.txt Shows how to use the Machine class to load platform descriptions, firmware binaries, and interact with the system bus. ```python from pyrenode3.wrappers import Emulation, Monitor from pyrenode3 import RPath e = Emulation() m = Monitor() machine = e.add_mach("mydevice") machine.load_repl("platforms/cpus/stm32l072.repl") machine.load_elf("https://dl.antmicro.com/projects/renode/stm32l07--zephyr-shell_module.elf-s_1195760-e9474da710aca88c89c7bddd362f7adb4b0c4b70") machine.load_elf("/path/to/firmware.elf") machine.load_binary("/path/to/binary.bin", load_point=0x08000000) sysbus = machine.sysbus machine.LoadPlatformDescriptionFromString("usb: USB.MPFS_USB @ sysbus 0x30020000 { MainIRQ -> plic@0x20 }") machine.sysbus.LoadFdt(RPath("https://example.com/devicetree.dtb").path, 0x81000000, "earlyconsole mem=256M@0x80000000") machine.sysbus.LoadSymbolsFrom(RPath("https://example.com/vmlinux.elf").read_file_path) ``` -------------------------------- ### Manage Emulation Instances Source: https://context7.com/antmicro/pyrenode3/llms.txt Demonstrates the Emulation class for creating, configuring, and controlling multiple virtual machine instances. ```python from pyrenode3.wrappers import Emulation, Monitor e = Emulation() machine1 = e.add_mach("stm32") machine2 = e.add_mach("hifive") machine3 = e.add_mach() stm32 = e.get_mach("stm32") hifive = e.hifive for machine in e: print(f"Machine: {machine}") externals = e.externals e.CreateUSBConnector("usb_connector") e.StartAll() e.PauseAll() e.rem_mach("stm32") e.clear() ``` -------------------------------- ### Connect USB Device to Host Source: https://context7.com/antmicro/pyrenode3/llms.txt This code connects a simulated USB device to the host system after the simulation has booted. It's useful for testing USB device functionality in a simulated environment. ```Python e.externals.usb_connector.RegisterInController(hifive.sysbus.usb.internal) ``` -------------------------------- ### Implement Multi-Machine Simulation with USB Connectivity Source: https://context7.com/antmicro/pyrenode3/llms.txt This snippet demonstrates a complex simulation involving multiple machines (FPGA and RISC-V Linux host) connected via a USB bridge, including firmware loading from remote URLs. ```python #!/usr/bin/env python3 from pyrenode3 import RPath from pyrenode3.wrappers import Analyzer, Emulation, Monitor, TerminalTester e = Emulation() m = Monitor() e.CreateUSBConnector("usb_connector") fomu = e.add_mach("fomu") fomu.load_repl("platforms/cpus/fomu.repl") fomu.load_elf("https://dl.antmicro.com/projects/renode/fomu--foboot.elf-s_112080-c31fe1f32fba7894338f3cf4bfb82ec2a8265683") e.Connector.Connect(fomu.sysbus.valenty.internal, e.externals.usb_connector) hifive = e.add_mach("hifive") hifive.load_repl("platforms/cpus/sifive-fu540.repl") hifive.load_elf("https://dl.antmicro.com/projects/renode/hifive-unleashed--bbl.elf-s_17219640-c7e1b920bf81be4062f467d9ecf689dbf7f29c7a") ``` -------------------------------- ### Emulation Management Source: https://context7.com/antmicro/pyrenode3/llms.txt The Emulation class serves as the primary entry point for managing the simulation environment, including machine creation and state control. ```APIDOC ## Emulation Management ### Description Methods to manage the lifecycle of the emulation environment, including adding/removing machines and controlling simulation state. ### Methods - `add_mach(name)`: Creates a new virtual machine. - `get_mach(name)`: Retrieves a machine instance by name. - `StartAll()`: Starts all machines in the emulation. - `PauseAll()`: Pauses all machines. - `clear()`: Removes all machines from the emulation. ### Example ```python from pyrenode3.wrappers import Emulation e = Emulation() machine = e.add_mach("stm32") e.StartAll() ``` ``` -------------------------------- ### Machine Configuration Source: https://context7.com/antmicro/pyrenode3/llms.txt The Machine class allows for configuring individual virtual platforms, loading firmware, and interacting with the system bus. ```APIDOC ## Machine Configuration ### Description Methods to load platform descriptions (REPL), firmware (ELF/Binary), and interact with hardware peripherals via the system bus. ### Methods - `load_repl(path)`: Loads a platform description file. - `load_elf(path)`: Loads an ELF firmware binary. - `load_binary(path, load_point)`: Loads a raw binary at a specific memory address. ### Example ```python machine.load_repl("platforms/cpus/stm32l072.repl") machine.load_elf("firmware.elf") ``` ``` -------------------------------- ### Automate LED Peripheral Testing with LEDTester Source: https://context7.com/antmicro/pyrenode3/llms.txt The LEDTester class allows for programmatic verification of LED states within a Renode emulation. It supports asserting specific states and waiting for state transitions with defined timeouts. ```python from pyrenode3.wrappers import Emulation, LEDTester e = Emulation() machine = e.add_mach("device") machine.load_repl("platforms/cpus/stm32f4_discovery.repl") machine.load_elf("/path/to/blinky.elf") led = machine.sysbus.gpioPortD.led1 led_tester = LEDTester( emulation=e, peripheral=led, name="green_led", defaultTimeout=5.0 ) e.StartAll() led_tester.AssertState(True) led_tester.AssertState(False) led_tester.AssertAndHoldState(True, holdTimeInMilliseconds=1000) ``` -------------------------------- ### Execute Multiple Commands with PyRenoDe3 Source: https://context7.com/antmicro/pyrenode3/llms.txt This snippet demonstrates how to execute a list of commands sequentially using the `m.execute()` method in PyRenoDe3. It includes error handling for each command execution. ```python commands = [ "mach create", "machine LoadPlatformDescription @platforms/cpus/stm32f4.repl", "sysbus LoadELF @/path/to/firmware.elf" ] for cmd in commands: out, err = m.execute(cmd) if err: print(f"Error: {err}") ``` -------------------------------- ### LEDSimulator API Source: https://context7.com/antmicro/pyrenode3/llms.txt This section details the LEDTester class, which allows for automated testing of LED peripherals within a Renode simulation. It covers initialization, state assertion, and waiting for state changes. ```APIDOC ## LEDTester Class ### Description The LEDTester class enables automated testing of LED peripherals. It registers as an external component and monitors LED state changes for verification in test scenarios. ### Usage Example ```python from pyrenode3.wrappers import Emulation, LEDTester e = Emulation() machine = e.add_mach("device") machine.load_repl("platforms/cpus/stm32f4_discovery.repl") machine.load_elf("/path/to/blinky.elf") # Create LED tester for a specific LED peripheral led = machine.sysbus.gpioPortD.led1 led_tester = LEDTester( emulation=e, peripheral=led, name="green_led", defaultTimeout=5.0 ) # Start emulation e.StartAll() # Assert LED state (uses internal Renode testing API) led_tester.AssertState(True) # LED should be on led_tester.AssertState(False) # LED should be off # Wait for LED state change led_tester.AssertAndHoldState(True, holdTimeInMilliseconds=1000) ``` ### Methods - **AssertState(state: bool)**: Asserts that the LED is currently in the specified state (True for on, False for off). - **AssertAndHoldState(state: bool, holdTimeInMilliseconds: int)**: Asserts that the LED is in the specified state and holds it for a given duration. ``` -------------------------------- ### Visualize Peripheral Data with Analyzer Class Source: https://context7.com/antmicro/pyrenode3/llms.txt Demonstrates the Analyzer class for creating visual analyzers of peripheral data, such as UART output and video streams. It requires XWT initialization for GUI backend and shows how to set up analyzers for multiple UARTs. ```python from pyrenode3.wrappers import Analyzer, Emulation, Peripheral from pyrenode3.inits import XwtInit e = Emulation() machine = e.add_mach("device") machine.load_repl("platforms/cpus/stm30l072.repl") machine.load_elf("/path/to/firmware.elf") # Create and show analyzer for UART output uart_analyzer = Analyzer(machine.sysbus.usart2) uart_analyzer.Show() # Initialize XWT before creating analyzers in callbacks XwtInit() # Create analyzers for multiple UARTs def setup_analyzers(machine): from Antmicro.Renode.Peripherals.UART import IUART uarts = list(machine.internal.GetPeripheralsOfType[IUART]()) for uart in uarts: peripheral = Peripheral(uart) analyzer = Analyzer(peripheral) analyzer.Show() setup_analyzers(machine) # Start emulation to see output in analyzer windows e.StartAll() ``` -------------------------------- ### RenodeLoader Configuration Source: https://context7.com/antmicro/pyrenode3/llms.txt This section explains the RenodeLoader class, which handles the initialization and loading of Renode DLLs. It also details how to configure Renode loading from different sources using environment variables. ```APIDOC ## RenodeLoader Class ### Description The RenodeLoader class handles initialization and loading of Renode DLLs from various sources. It's automatically invoked when importing pyrenode3 but can be used directly for advanced configuration. ### Usage Example ```python from pyrenode3.loader import RenodeLoader import os # Check if Renode is already loaded loader = RenodeLoader() if loader.is_initialized: print(f"Renode root: {loader.root}") print(f"Binaries dir: {loader.binaries}") # Manual loading from different sources (before importing pyrenode3) # Note: Only one source can be used, and loading happens at import time # From Arch package (Mono runtime) os.environ["PYRENODE_PKG"] = "/path/to/renode-latest.pkg.tar.xz" os.environ["PYRENODE_RUNTIME"] = "mono" # From .NET package os.environ["PYRENODE_PKG"] = "/path/to/renode-dotnet.pkg.tar.xz" os.environ["PYRENODE_RUNTIME"] = "coreclr" # From build directory os.environ["PYRENODE_BUILD_DIR"] = "/path/to/renode/sources" os.environ["PYRENODE_BUILD_OUTPUT"] = "output/bin/Release" # From portable binary (.NET only) os.environ["PYRENODE_BIN"] = "/path/to/Renode" os.environ["PYRENODE_RUNTIME"] = "coreclr" # Use context manager for operations requiring Renode root directory with loader.in_root(): # File paths relative to Renode root work here pass ``` ### Configuration via Environment Variables - **PYRENODE_PKG**: Path to the Renode package file (.pkg.tar.xz). - **PYRENODE_RUNTIME**: Runtime to use ('mono' or 'coreclr'). - **PYRENODE_BUILD_DIR**: Path to the Renode source build directory. - **PYRENODE_BUILD_OUTPUT**: Path to the build output directory. - **PYRENODE_BIN**: Path to the portable Renode binary directory. ``` -------------------------------- ### Resolve File Paths and URLs with RPath Class Source: https://context7.com/antmicro/pyrenode3/llms.txt Illustrates how the RPath class handles file path resolution and URL fetching, providing a unified interface for loading resources from both local filesystems and remote URLs. It also shows context manager usage for Renode's root directory. ```python from pyrenode3 import RPath from pyrenode3.wrappers import Emulation e = Emulation() machine = e.add_mach("device") rpath = RPath("https://dl.antmicro.com/projects/renode/firmware.elf") local_path = rpath.path read_file_path = rpath.read_file_path local_rpath = RPath("/path/to/local/file.elf") print(local_rpath.path) with RPath.in_root(): machine.load_repl("platforms/cpus/stm32f4.repl") machine.sysbus.LoadSymbolsFrom( RPath("https://example.com/symbols.elf").read_file_path ) machine.sysbus.LoadFdt( RPath("https://example.com/devicetree.dtb").path, 0x81000000, "console=ttyS0" ) ``` -------------------------------- ### Monitor Interface Source: https://context7.com/antmicro/pyrenode3/llms.txt The Monitor class provides a bridge to execute raw Renode monitor commands and scripts. ```APIDOC ## Monitor Interface ### Description Allows execution of standard Renode monitor commands and .resc script files for advanced configuration. ### Methods - `execute(command)`: Executes a single Renode monitor command. - `execute_script(path)`: Executes a full Renode script file. ### Example ```python from pyrenode3.wrappers import Monitor m = Monitor() m.execute("mach create \"testmachine\"") ``` ``` -------------------------------- ### Execute Renode Monitor Commands Source: https://context7.com/antmicro/pyrenode3/llms.txt Utilizes the Monitor class to send raw commands or execute .resc scripts within the Renode environment. ```python from pyrenode3.wrappers import Emulation, Monitor e = Emulation() m = Monitor() output, error = m.execute("help") print(output) output, error = m.execute("mach create \"testmachine\"") output, error = m.execute_script("/path/to/script.resc") ``` -------------------------------- ### Automated Login Test with TerminalTester Source: https://context7.com/antmicro/pyrenode3/llms.txt This snippet illustrates an automated login test using the TerminalTester. It waits for login prompts, sends credentials, and is essential for testing firmware that requires user authentication. ```Python tester = TerminalTester(hifive.sysbus.uart0, 60) tester.WaitFor(["buildroot login:"], includeUnfinishedLine=True) tester.WriteLine("root") tester.WaitFor(["Password:"], includeUnfinishedLine=True) tester.WriteLine("root") ``` -------------------------------- ### Automate UART Testing with TerminalTester Class Source: https://context7.com/antmicro/pyrenode3/llms.txt Shows how to use the TerminalTester class for automated testing of UART-based terminals. It covers waiting for specific output, sending input, and executing commands within a terminal session. ```python from pyrenode3.wrappers import Emulation, TerminalTester e = Emulation() machine = e.add_mach("device") machine.load_repl("platforms/cpus/stm30l072.repl") machine.load_elf("/path/to/firmware.elf") uart = machine.sysbus.usart2 # Create terminal tester with 60-second default timeout tester = TerminalTester(uart, timeout=60.0) e.StartAll() tester.WaitFor(["login:"], includeUnfinishedLine=True) tester.WriteLine("root") tester.WaitFor(["Password:"], includeUnfinishedLine=True) tester.WriteLine("secret") tester.WaitFor(["# ", "$ "], includeUnfinishedLine=True) tester.WriteLine("uname -a") tester.WaitFor(["Linux"], includeUnfinishedLine=True) tester.WaitFor(["expected output"], timeout=tester.to_interval(30.0)) ``` -------------------------------- ### Display UART Analyzer Source: https://context7.com/antmicro/pyrenode3/llms.txt This code snippet shows how to instantiate and display a UART analyzer. It's used for monitoring serial communication during simulation, which is crucial for debugging. ```Python Analyzer(hifive.sysbus.uart0).Show() ``` -------------------------------- ### Manage External Components with ExternalsManager Source: https://context7.com/antmicro/pyrenode3/llms.txt ExternalsManager handles peripherals and connectors like USB bridges. It provides methods to register, retrieve, and connect external components across multiple machines. ```python from pyrenode3.wrappers import Emulation, LEDTester e = Emulation() externals = e.externals e.CreateUSBConnector("usb_bridge") usb = externals.get_external("usb_bridge") for external in externals: print(f"External: {external}") machine1 = e.add_mach("host") machine2 = e.add_mach("device") machine1.load_repl("platforms/cpus/sifive-fu540.repl") machine2.load_repl("platforms/cpus/fomu.repl") e.externals.usb_bridge.RegisterInController(machine1.sysbus.usb.internal) e.Connector.Connect(machine2.sysbus.valenty.internal, e.externals.usb_bridge) ``` -------------------------------- ### ExternalsManager API Source: https://context7.com/antmicro/pyrenode3/llms.txt This section describes the ExternalsManager class, responsible for handling external components connected to Renode machines, such as network bridges and USB connectors. It provides methods for managing these components. ```APIDOC ## ExternalsManager Class ### Description The ExternalsManager class manages external components that connect to machines, such as network bridges, USB connectors, and test utilities. It provides methods to add, retrieve, and iterate over external components. ### Usage Example ```python from pyrenode3.wrappers import Emulation, LEDTester e = Emulation() # Access externals manager externals = e.externals # Create USB connector (adds to externals automatically) e.CreateUSBConnector("usb_bridge") # Access external by name usb = externals.get_external("usb_bridge") # Iterate over all externals for external in externals: print(f"External: {external}") # Check available external names for name in externals._elements(): print(f"External name: {name}") # Connect machines via USB machine1 = e.add_mach("host") machine2 = e.add_mach("device") machine1.load_repl("platforms/cpus/sifive-fu540.repl") machine2.load_repl("platforms/cpus/fomu.repl") # Register device in host's USB controller e.externals.usb_bridge.RegisterInController(machine1.sysbus.usb.internal) e.Connector.Connect(machine2.sysbus.valenty.internal, e.externals.usb_bridge) ``` ### Methods - **get_external(name: str)**: Retrieves an external component by its name. - **_elements()**: Returns a list of names of all registered external components. - **CreateUSBConnector(name: str)**: Creates and registers a new USB connector with the given name. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.