Вы находитесь на странице: 1из 20

SystemDotNet

Discrete time simulation with


SystemDotNet
Carsten Wulff
http://www.sf.net/projects/

carsten@wulff.no
systemdotnet
2004-09-13
Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics
SystemDotNet
What is discrete time simulation

• Divide time into timesteps which are


simulated in sequence
• Common concepts:
– Signals: Container for values
– Process: Something that transforms signals

carsten@wulff.no
– Event Queue: Schedules changes for signals
– Module: Logical collection of signals and
processes

Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics


SystemDotNet
Basic Rules

• The order of execution of events at the


same timestep does not matter
• The system is causal. Changes in the
future does not effect signals in the past

carsten@wulff.no
Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics
SystemDotNet
SystemDotNet Overview
Event queue and
simulator control

Modules
Output writers
Signals

carsten@wulff.no
Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics
SystemDotNet
Divide and Conquer

• The Event Queue


• The Simulator
• The Signals
• The Process

carsten@wulff.no
• The Modules

Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics


SystemDotNet
The Event Queue
Conventional

30ns 30ns 21ns 21ns 20ns 10ns 10ns

In SystemDotNet

10ns 10ns 10ns

21ns 21ns 21ns

Run Events

20ns 20ns

carsten@wulff.no
30ns 30ns 30ns

Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics


SystemDotNet
What can be added to the Event
Queue?

• Pretty much anything as long as you


implement IRunnable
public virtual void Write(IRunnable ite, long delay)
{
long key = time + delay;

if (!queue.ContainsKey(key))

carsten@wulff.no
queue.Add(key, new List<IRunnable>());

queue[key].Add(ite);
}

Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics


SystemDotNet
The Event Queue Algorithm
Start

Current time step = next time step

Are there any events


scheduled for this timestep
Yes
Yes
No

Is there another time


step?

carsten@wulff.no
No
Remove events from the queue and
execute them

Stop Find next time step

Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics


SystemDotNet
Code Implementation

carsten@wulff.no
Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics
SystemDotNet
The Simulator

• Extends the Event Queue


• Sets up simulation thread
• Singelton pattern
• Code implementation ThreadRun();

carsten@wulff.no
Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics
SystemDotNet
The Signals

• All signals are based on SignalBase


• SignalBase cannot hold any value
• Signal<Type>: Can be of any type, but to
decrease simulation time they should be able
to distinguish between different values of the
object.

carsten@wulff.no
• SignalCollection<Type>: Dynamic collection
of signals that can be used instead of
Signal<Type> array.
Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics
SystemDotNet
SignalBase.SensitiveType

• The default state is ValueChanged. When a new


value is written to the signal it tries to determine if the
value has changed. If it has it fires the Changed
event.
• If you choose ValueUpdated the signal will always fire
the Changed event when the signal is written to.
• When using reference types instead of value types as

carsten@wulff.no
the signal content you should override Object.Equals
to determine if two objects are the same.

Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics


SystemDotNet
Defining new signal

• Extend SignalBase
• Implement ISignal
• Override ToString()
• Implement AddListener and

carsten@wulff.no
RemoveListener
• Add Read and Write methods

Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics


SystemDotNet
Implement Add and Remove
Listener

• These methods are used to connect


signals to each other
• Defines what should happen when you
write asignal.Connect(anothersignal)

carsten@wulff.no
internal override void AddListener(SignalBase c)
{
((Signal<T>)c).Wire.ValueUpdated += delegate(SignalValueHolder<T> sender) { this.Wire.Value =
sender.Value; };
}

Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics


SystemDotNet
Implement Read and Write
public T Read()
{
return Wire.Value;
}
public virtual void Write(T Value, long delay)
{
if (delay < 0)
throw new Exception("Negative delay is not allowed. It
would sort of ruin the causality of things :-)");

//Make new time event


TimeEvent<T> te = new TimeEvent<T>(this.Wire, Value);

carsten@wulff.no
//Add it to the event queue
Simulator.GetInstance().Write(te, delay);
}

Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics


SystemDotNet
The Process

• Normally a method that is run when the


Changed event of a signal is fired
[Process("Clk")]
public void Next()
{
Clk.Write(!Clk.Read(), 200);

carsten@wulff.no
}

Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics


SystemDotNet
The Module
• Rules:
– All signals that will fire processes must be public fields
– Should set the Parent property if it contains child modules
– All Processes must be public methods and conform to the EmtpyHandler
delegate

public class Clock : ModuleBase


{
public Signal<bool> Clk = new Signal<bool>(); //Clk signal
public override void First()
{
Clk.Write(true, 200);
}
[Process("Clk")]

carsten@wulff.no
public void Next()
{
Clk.Write(!Clk.Read(), 200);
}
}

Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics


SystemDotNet
The Module methods
• OnInit(): Called before signals are connected
to processes
• OnLoad(): Called just after signals are
connected to processes and signal names
are set
• First(): Called before the first timestep of the

carsten@wulff.no
simulator
• OnStop(): Called when the simulator stops

Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics


SystemDotNet
How does modules connect
processes to signals

• Runs through all public fields in the module


and finds all objects that inherit SignalBase.
• Runs through all public methods in the
module and finds those that use the attribute
[Process(”signal”)].
• Extracts the signal name from the Process

carsten@wulff.no
attribute and compares it to the signals in the
module. If they match it adds the method to
the Changed event of the signal
Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics
SystemDotNet
Code run through of
SdnReferenceDesign

carsten@wulff.no
Norwegian University of Science and Technology (NTNU), Departement of Physical Electronics

Вам также может понравиться