VIRTUAL MACHINE
SOFTWARE DOCUMENTATION
Mar 10, 2025
Virtual Machine 2
TABLE OF CONTENTS
1. Block Overview 3
2. Description 6
3. Parts of the Code 9
4. Differences with C/C++ 13
Virtual Machine 3
BLOCK OVERVIEW
The Virtual Machine block implements the VisualSim Script language, seamlessly integrating standard
programming constructs with RegEx functions and offering full compatibility with the graphical editor.
EXAMPLE CODE
Below is an example demonstrating basic functionality in the Virtual Machine:
Key Features:
• Enhanced RegEx Language: Incorporates C-like control structures such as if, else-if, else,
while, and SWITCH/CASE/BREAK for improved scripting capabilities.
Virtual Machine 4
• Behavioural Simulation: Enables the modelling of resources, cycle-accurate hardware
components, and algorithmic workflows.
• Advanced Queue Management: Facilitates the declaration of both event-driven and
time-based queues.
• Robust Event Control: Allows event cancellations for TIMEQ and WAIT operations using
the CANCEL keyword.
• Seamless Virtual Integration: Supports virtual connections within models for flexible
data flow.
• Customizable Parameter Handling: Offers a text window parameter Optional
Parameters for managing hidden parameters without affecting existing model
configurations.
The Virtual_Machine block supports a wide range of execution constructs, making it a flexible
tool for modeling hardware/software interactions and complex behaviors within VisualSim.
PARAMETERS
Parameter Name Parameter Value Description
Path to block, can include "VS/"
Path none
to be generic
Name of [Link], using above
Read_File none
path
Save parsed script, using above
Save_Files false
path
Virtual Machine 5
Parameter Name Parameter Value Description
Profile file name, e.g.,
Profile_File none
"[Link]"
Output listens to block to a file,
Listen_to_File none
e.g., "[Link]"
Duplicate incoming data
structure; for single input and
Duplicate_Input true
output, can improve
performance
Profile script as an array of
instructions executed by Virtual
Profile 0
Machine, useful for estimating
algorithm performance
Maximum loops in a while loop;
default is one million, but some
Maximum_Loops 10000000
models may require a larger
value
Defines a unique Virtual
Block_Reference Block_Name Machine block (Block_Path +
Block_Name)
VM block will add user-defined
Port_Order_Array {"input"}
fields internally; otherwise,
Virtual Machine 6
users can define the reading
order here
Default is false, meaning no
scheduler times or stats are
Add_Scheduler_Times_to_DS false
added. Set to true for all QUEUE,
TIMEQ statistics
DESCRIPTION
The Virtual Machine contains an initialize section, an input queue, the sequence of code
execution, and outputs.
• The initialize section is the portion of the code prior to LABEL: BEGIN. This is executed at
the beginning of the simulation.
• The input queue is a non-priority queue that lists data structures arriving at all input
ports and from virtual connections. Data structures trigger code execution in the order
of arrival.
• The outputs can be a port of the block or a virtual device in the model. Outputs are
triggered using RegEx or the SEND function.
Virtual Machine 7
i. SEQUENCE OF OPERATION
1. At the start of the simulation, all initialization code is executed without requiring an
input or trigger.
2. When a data structure arrives at the input port or via a virtual connection, it is placed in
the input queue.
3. If the Virtual Machine is free, the head of the queue is removed and stored in
port_token, which then executes from LABEL: BEGIN.
4. Execution proceeds sequentially until:
• It encounters WAIT, EXIT, QUEUE, GTO(END), TIMEQ, or Event, causing
execution to pause.
• Execution completes, allowing the next data structure in the queue to
execute.
5. When a data structure is released from QUEUE or TIMEQ, it resumes execution at the
next line of code.
6. The SEND function immediately executes and continues execution until reaching an END
or encountering a delay point. Data structures in the output queue are then sent to their
destinations.
7. If SEND targets a LABEL in the same block, it creates an independent thread.
8. WAIT stops execution entirely, while TIMEQ delays the current transaction but allows
others to proceed.
Virtual Machine 8
ii. HANDLING MULTIPLE TRANSACTIONS
• Each arriving data structure is assigned a port name (port_name) or "virtual" if it comes
from a virtual connection.
• Unlike Processing or Decision blocks, transactions should not be addressed by port
name to avoid conflicts from simultaneous executions.
• The special keyword port_token is used to address the currently executing transaction.
• Multiple port_token values can exist in different states (TIMEQ, WAIT, or a separate
thread).
iii. EVENT HANDLING AND CANCELLATION
Cancellation of TIMEQ and WAIT events is supported via:
• CANCEL() (Block command)
• cancelEvent() (RegEx command)
iv. VARIABLE ACCESS
• Use port_token.field_name, variable_name.field_name, or parameter.field_name to
access fields.
• The port_name can be used for control decisions, such as SWITCH (port_name) or if-
else conditions.
Virtual Machine 9
v. MULTITHREADING WITH THREADS
• Most Virtual Machine code operates as a single sequence (thread).
• Threads can be created by:
1. Defining a LABEL: Name.
2. Writing a standalone code sequence (often a continuous loop).
3. Calling the new thread using SEND(Label_Name, value).
• SEND starts the new thread but does not execute it immediately.
• The new thread starts execution when the active thread reaches:
o TIMEQ
o GTO (END)
o The end of execution for ****port_token.
• TIMEQ can be used to enforce different execution rates for each thread.
PARTS OF THE CODE
This outlines the structure and execution flow of the Virtual Machine (VM) block. The code
consists of two main phases:
• Initialization
• Execution
Understanding these phases is crucial for optimizing performance and ensuring correct
functionality.
Virtual Machine 10
a. INITIALIZATION
The Initialize phase includes all code that appears before the LABEL: BEGIN statement. During
this phase:
• All variables are defined and assigned initial values.
• Parameter values are assigned to internal variables for optimization.
• Execution sequences and loops can be set up for future execution.
• Statistics gathering and reporting mechanisms are established for execution at
termination.
Alternative Initialization
Instead of using LABEL: BEGIN, you can set Self_Start = true to initialize the block automatically.
If Self_Start is used, LABEL: BEGIN must not be included.
Flowchart – Initialization
START
DEFINE VARIABLES
ASSIGN INITIAL VALUES
SETUP EXECUTION SEQUENCES
Virtual Machine 11
b. EXECUTION
After initialization, the Execution phase begins. Code in this phase:
• Uses incoming data structures, variables, and parameters.
• Leverages RegEx functions for virtual connections.
• Supports self-triggering using Self_Start = true.
• Can be triggered by incoming data structures from ports or virtual connections.
Execution Flow
1. Triggering Mechanism
• If Self_Start = true, the block starts automatically.
• Otherwise, execution begins when an input is received.
2. Processing
• Uses variables, built-in functions, and RegEx methods.
3. WAIT Operations
• Execution can pause until a specified time/event occurs.
4. SEND Operations
• Sends data to output ports, virtual connections, or another block.
Key Considerations
• WAIT operations cause temporary execution pause until time or an event occurs.
• SEND functions transfer data to outputs, triggering further execution.
• Execution happens in zero-time unless delayed by conditions.
Virtual Machine 12
Flowchart – Execution
START
CHECK SELF_START FLAG
RECEIVE INPUT OR EVENT
PROCESS DATA AND EXECUTE
WAIT (IF ANY)
SEND OUTPUT AND DATA
Virtual Machine 13
DIFFERENCES FROM C/C++ PROGRAM
The Virtual Machine (VM) operates similarly to function blocks in C programming or scripting.
While it supports C/C++ syntax for simple logic expressions, several key differences exist. This
document outlines the distinctions between Virtual Machine programming and C/C++.
Key Differences
1. Statement Termination
• Virtual Machine does not require a ; at the end of each expression statement.
• The ; can still be used for end-of-line C/C++ compatibility.
2. Expression Rules
• Each line must contain only one operation.
• Valid operations include:
I. Expressions
II. Labels (LABEL:)
III. Curly bracket {
3. Variable Declaration
• Variable declarations are not required unless an initial value is assigned.
• Declaring a variable with Name = value; automatically determines the data type.
• If no explicit declaration, the first use of the variable on the Left-Hand Side (LHS) of an
expression determines its type.
4. Control Flow Syntax
• Control statements must be written in uppercase:
Virtual Machine 14
I. LABEL
II. SWITCH
III. CASE
IV. BREAK
• CASE: values can be:
I. Integer
II. Long
III. Double
IV. String
V. Model parameter
• Each CASE must end with one of the following:
I. BREAK
II. RETURN
III. GTO(Value)
IV. GTO (END)
• The last CASE statement must be CASE: DEFAULT to define the scope of SWITCH.
5. Pointer Usage
• Pointers are not supported.
• Variables can only be referenced by name.
6. Block Syntax
• At least one statement line must exist between { and }.
7. Comment Styles
Virtual Machine 15
• Both C/C++ comment styles are supported:
I. Multi-line comments: /* comment */
II. Single-line comments: // comment