spin is an experimental project and far from beeing available for commercial usage. If you are interrested in spin: mailto://spin@toem.de.

Introducing Spin 4 - Dedicated Processes

Beside normal and threaded process, there are additional specialized processes (e.g. Statemachine) available. Reasons are:

  • Simplify non-threaded Processes.
  • Ready to use patterns to ease development.
  • Force the user to use special patterns to enable transformation of the prototyping code into hardware patterns.

Threaded process are often just used to simplify development, but have the drawback of overhead in execution. spin tries to simplify development using non-threaded processes and enables the use of threads for parallelization (async proclets).

To setup a state-machine you need to derive from the class Statemachine in de.toem.spin.processes package

public AModule(Module parent) {
	super(parent);

	new Statemachine(null, this) {

		@Override
		public void initialize() {
			schedule(0,start);
		}

		....
	};
....

In the initialize method we call a derived form of the schedule call with the a State parameter. So it will start execution at 0 time in state "start". To add states we use the inner-class State from Statemachine:

new Statemachine(null, this) {

	@Override
	public void initialize() {
		schedule(0,start);
	}

	State start = new State() {

		public State enter(IRunInfo info) {
			log("Entered start");
			sensitive(in1.changed());
			return null;
		}

		public State handle(IRunInfo info) {
			if (info.getSensitivity() == in1.changed()) {
				insensitive(in1.changed());
				log("handle in1.changed -> change state to gotIn1");
				return gotIn1;
			}
			return null;
		}
	};
Now we have the state "start". Each state has to provide 2 methods:
Method Description
State enter(IRunInfo info) Is called whenever a state is entered. To directly slip to another state return the state object or return null to keep the state.
State handle(IRunInfo info) Is called when a state has been entered and 1: schedule(delta) was called; 2: a notification from a sensitivity object comes in. To determine the cause, use the getSensitivity() method of the info parameter. Return the new state to change state or null to keep.

To control the process, use the methods of the normal process (e.g. sensitive, insensitive, schedule,...).

Here the complete example and the ouput:

public class I4_Dedicated extends Module {

	IOut<Integer> signal;
	

	public class AModule extends Module {

		public IIn<Integer> in1;
		public IIn<Integer> in2;

		public AModule(Module parent) {
			super(parent);

		new Statemachine(null, this) {

				@Override
				public void initialize() {
					schedule(0,start);
				}

				State start = new State() {

					public State enter(IRunInfo info) {
						log("Entered start");
						sensitive(in1.changed());
						return null;
					}

					public State handle(IRunInfo info) {
						if (info.getSensitivity() == in1.changed()) {
							insensitive(in1.changed());
							log("handle in1.changed -> change state to gotIn1");
							return gotIn1;
						}
						return null;
					}
				};

				State gotIn1 = new State() {

					public State enter(IRunInfo info) {
						log("Entered gotIn1");
						schedule(Time.ns(100));
						return null;
					}

					public State handle(IRunInfo info) {
						log("100 ns later -> change state to waited");
						return waited;
					}
				};

				State waited = new State() {

					public State enter(IRunInfo info) {
						log("Entered waited");
						sensitive(in2.changed());
						return null;
					}

					public State handle(IRunInfo info) {
						insensitive(in2.changed());
						log("handle in2.changed -> change state to start");
						return start;
					}
				};

			};
		}
	}

	I4_Dedicated() {

		AModule m = new AModule(this);
		signal.connectTo(m.in1,m.in2);
		signal.init(0);
		signal.write( 5,Time.ns(100));
		signal.write( 7,Time.ns(500));
	}

	public static void main(String[] args) {
		new I4_Dedicated();
		Spin.run();
	}

}
spin 0.5.3
Copyright (c) 2011-2012 by toem
ALL RIGHTS RESERVED
-----------------------------------------------
-----------------------------------------------
No. of Modules: 2 
No. of Threads: 0 
No. of Connections: 1 
-----------------------------------------------
[00000000] Info : I4_Dedicated.AModule.proc   Entered start
[00000100] Info : I4_Dedicated.AModule.proc   handle in1.changed -> change state to gotIn1
[00000100] Info : I4_Dedicated.AModule.proc   Entered gotIn1
[00000200] Info : I4_Dedicated.AModule.proc   100 ns later -> change state to waited
[00000200] Info : I4_Dedicated.AModule.proc   Entered waited
[00000500] Info : I4_Dedicated.AModule.proc   handle in2.changed -> change state to start
[00000500] Info : I4_Dedicated.AModule.proc   Entered start
-----------------------------------------------
Simulated  Time: 500 [ns]
Simulation Time: 0,002000 [s]
Factor:4000,000000
Context switches :0
-----------------------------------------------
 

Print

User Rating: 0 / 5

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive