Examples

Posted in spin

Here some examples found at "http://www.asic-world.com/examples/" and translated into spin.

E1 Adder

@Element(name ="Top",description = "http://www.asic-world.com/examples/systemc/adder.html#Adder",scale=4)
public class E1_Adder_Example extends Module {
	
	// Module
	class Adder extends Module {
		public IInPort<Integer> a;
		public IInPort<Integer> b;
		public IOutPort<Integer> sum;

		public Adder(String name, IModule parent,Scale scale) {
			super(name, parent,scale);
			scale.applyTo(a,b,sum);
			
			new Process(null, this,scale) {

				@Override
				public void initialize() {
					sum.init(0);
					sensitive(a.changed(), b.changed());
				}

				@Override
				public void run(IRunInfo info) {
					sum.write(scale.mask(a.read() + b.read()));
				}
			};
		}
	}

	// Signal
	Source<Integer> s1;
	Source<Integer> s2;
	
	// Example
	E1_Adder_Example() {
		// create
		Adder m = new Adder(null, this,scale);
		s1.init(0);
		s1.connectTo(m.a);
		s2.init(0);
		s2.connectTo(m.b);
		
		// trace
		Trace trace = Trace.create("trace.vcd", this, Trace.VCD);
		trace.connectTo(s1,s2,m.sum);
		
		// signal
		s1.write(3, Time.ns(100));
		s2.write(5, Time.ns(200));
		s1.write(2, Time.ns(300));
		s2.write(6, Time.ns(400));
	}

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

E2 Async Reset Flipflop

@Element(name ="Top",description = "http://www.asic-world.com/examples/systemc/flip_flop.html#Asynchronous_reset_T_-_FF")
public class E2_Async_Reset_Flipflop_Example extends Module {

	// Module
	class FlipFlop extends Module {
		
		public IInPort<Boolean> data, clk, reset;
		public IOutPort<Boolean> q;
		boolean q_l;

		public FlipFlop(String name, IModule parent) {
			super(name, parent);

			new Process(null, this) {

				@Override
				public void initialize() {
					q.init(false);
					sensitive(reset.changedTo(false),clk.changedTo(true));
				}

				@Override
				public void run(IRunInfo info) {
					if (!reset.read()) {
						q_l = false;
					} else if (data.read()) {
						q_l = !q_l;
					}
					q.write(q_l);
				}
			};
		}
	}

	// Signals
	Source<Boolean> data, reset;
	Clock clk;

	// Example
	E2_Async_Reset_Flipflop_Example() {
		// create
		FlipFlop m = new FlipFlop(null, this);
		data.init(false);
		data.connectTo(m.data);
		reset.init(true);
		reset.connectTo(m.reset);
		clk.init(Time.ns(50));
		clk.connectTo(m.clk);

		// trace
		Trace trace = Trace.create("trace.vcd", this, Trace.VCD);
		trace.connectTo(data, reset, clk, m.q);

		// signal
		data.write(true, Time.ns(80));
		reset.write(false, Time.ns(120));
		reset.write(true, Time.ns(170));
		data.write(false, Time.ns(280));
	}

	public static void main(String[] args) {
		new E2_Async_Reset_Flipflop_Example();
		Spin.run(Time.ns(1000));
	}
}

E3 8Bit UpDown Counter

@Element(name = "Top", description = "http://www.asic-world.com/examples/systemc/counters.html#8-Bit_Up/Down_Counter",scale =8)
public class E3_8Bit_UpDown_Counter_Example extends Module {

	// Module
	class Counter extends Module {

		public IInPort<Boolean> enable, clk, reset, up_down;
		public IOutPort<Integer> out;

		public Counter(String name, IModule parent, Scale scale) {
			super(name, parent,scale);
			scale.applyTo(out);

			new Process(null, this,scale) {

				int count = 0;

				@Override
				public void initialize() {
					out.init(0);
					sensitive(clk.changedTo(true));
				}

				@Override
				public void run(IRunInfo info) {
					if (reset.read()) {
						count = 0;
					} else if (enable.read()) {
						if (up_down.read()) {
							count = count + 1;
						} else {
							count = count - 1;
						}
						count = scale.mask(count);
					}
					out.write(count);
				}
			};
		}
	}

	// Signals
	Source<Boolean> enable, reset, up_down;
	Clock clk;

	// Example
	E3_8Bit_UpDown_Counter_Example() {
		// create
		Counter m = new Counter(null, this, scale);
		enable.init(false);
		enable.connectTo(m.enable);
		reset.init(false);
		reset.connectTo(m.reset);
		up_down.init(false);
		up_down.connectTo(m.up_down);
		clk.init(Time.ns(50));
		clk.connectTo(m.clk);

		// trace
		Trace trace = Trace.create("trace.vcd", this, Trace.VCD);
		trace.connectTo(enable, reset, up_down, clk, m.out);

		// signal
		enable.write(true, Time.ns(80));
		up_down.write(true, Time.ns(340));
		reset.write(true, Time.ns(480));
		reset.write(false, Time.ns(570));
		enable.write(false, Time.ns(880));
	}

	public static void main(String[] args) {
		new E3_8Bit_UpDown_Counter_Example();
		Spin.run(Time.ns(1000));
	}
}

E4_Single_Port_Memory

@Element(name = "Top")
public class E4_Single_Port_Memory_Example extends Module {

	// Module
	class Memory extends Module {

		public IInPort<Boolean> cs, we, oe;
		public IInPort<Integer> address;
		public IInPort<Integer> data_in;
		public IOutPort<Integer> data_out;

		int[] mem;

		public Memory(String name, IModule parent, Scale dataWidth, Scale addressWidth) {
			super(name, parent, null);
			dataWidth.applyTo(data_in, data_out);
			addressWidth.applyTo(address);
			mem = new int[1 << addressWidth.val];
			data_out.init(0);

			// Memory Write Block
			// Write Operation : When we = 1, cs = 1
			// Memory Read Block
			// Read Operation : When we = 0, oe = 1, cs = 1
			new Process(null, this, dataWidth) {

				@Override
				public void initialize() {
					sensitive(address.changed(), cs.changed(), we.changed(), data_in.changed(),
 						oe.changed());
				}

				@Override
				public void run(IRunInfo info) {
					if (cs.read() && we.read()) {
						mem[address.read()] = data_in.read();
					}
					if (cs.read() && !we.read() && oe.read()) {
						data_out.write(mem[address.read()] & scale.intMask);
					}
				}
			};
		}
	}

	// Signals
	Source<Boolean> cs, we, oe;
	Source<Integer> address, data;

	// Example
	E4_Single_Port_Memory_Example() {
		// create
		Memory m = new Memory(null, this, Scale.b8,Scale.b16);
		cs.init(false);
		cs.connectTo(m.cs);
		we.init(false);
		we.connectTo(m.we);
		oe.init(false);
		oe.connectTo(m.oe);
		address.init(0);
		address.connectTo(m.address);
		data.init(0);
		data.connectTo(m.data_in);
		
		// trace
		Trace trace = Trace.create("trace.vcd", this, Trace.VCD);
		trace.connectTo(cs, we, oe,address, data,m.data_out);

		// signal
		address.write(0x815, Time.ns(80));
		data.write(0x66, Time.ns(90));
		cs.write(true, Time.ns(120));
		we.write(true, Time.ns(130));
		we.write(false, Time.ns(220));
		cs.write(false, Time.ns(230));
		address.write(0x816, Time.ns(380));
		data.write(0x0, Time.ns(390));
		cs.write(true, Time.ns(420));
		oe.write(true, Time.ns(430));
		oe.write(false, Time.ns(520));
		cs.write(false, Time.ns(530));
		address.write(0x815, Time.ns(680));
		data.write(0x0, Time.ns(690));
		cs.write(true, Time.ns(720));
		oe.write(true, Time.ns(730));
		oe.write(false, Time.ns(820));
		cs.write(false, Time.ns(830));
		address.write(0x0, Time.ns(900));
	}

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

E5 Bus

@Element(name = "Top", description = "Simple bus Example")
public class E5_Bus_Example extends Module {

	// Module
	class BusMember extends Module {

		public IInPort<Logic> we, oe;
		public IInPort<LInteger> address;
		public IInOutPort<LInteger> data;

		int value;
		long base;

		public BusMember(String name, IModule parent, long base) {
			super(name, parent, null);
			this.base = base;
			data.init(LInteger.Z);

			new Process(null, this) {

				@Override
				public void initialize() {
					sensitive(we.changed(), oe.changed());
					setScale(data.getScale());
				}

				@Override
				public void run(IRunInfo info) {
					if (info.getSensitivity() == we.changed() && we.read() == Logic.L0
							&& address.read().intValue() == BusMember.this.base) {
						value = data.read().intValue();
						log("written:", Integer.toHexString(value));
					}
					if (info.getSensitivity() == oe.changed() && address.read().intValue() 
						== BusMember.this.base)
						if (oe.read() == Logic.L0) {
							data.write(LInteger.valueOf(scale.mask(value)));
						} else
							data.write(LInteger.Z);
				}
			};
		}
	}

	// Bus
	Source<Logic> we, oe;
	@Element(scale = 24)
	Source<LInteger> address;
	@Element(scale = 16)
	Sounk<LInteger> data;

	// Example
	E5_Bus_Example() {
		// create
		BusMember m1 = new BusMember(null, this, 0x1000);
		m1.address.connectTo(address);
		m1.data.connectTo(data);
		m1.we.connectTo(we);
		m1.oe.connectTo(oe);
		BusMember m2 = new BusMember(null, this, 0x2000);
		m2.address.connectTo(address);
		m2.data.connectTo(data);
		m2.we.connectTo(we);
		m2.oe.connectTo(oe);

		address.init(LInteger.Z);
		data.init(LInteger.Z);
		oe.init(Logic.L1);
		we.init(Logic.L1);

		// trace
		Trace trace = Trace.create("trace.vcd", this, Trace.VCD);
		trace.connectTo(we, oe, address, data);

		// signal
		address.write(LInteger.valueOf(0x1000), Time.ns(80));
		data.write(LInteger.valueOf(0x555), Time.ns(90));
		we.write(Logic.L0, Time.ns(130));
		we.write(Logic.L1, Time.ns(220));

		address.write(LInteger.valueOf(0x2000), Time.ns(280));
		data.write(LInteger.valueOf(0x666), Time.ns(290));
		we.write(Logic.L0, Time.ns(330));
		we.write(Logic.L1, Time.ns(420));

		address.write(LInteger.valueOf(0x1000), Time.ns(480));
		data.write(LInteger.Z, Time.ns(490));
		oe.write(Logic.L0, Time.ns(530));
		oe.write(Logic.L1, Time.ns(620));

		address.write(LInteger.valueOf(0x2000), Time.ns(680));
		data.write(LInteger.Z, Time.ns(690));
		oe.write(Logic.L0, Time.ns(730));
		oe.write(Logic.L1, Time.ns(820));

		address.write(LInteger.valueOf(0x1000), Time.ns(880));
		data.write(LInteger.valueOf(0x000), Time.ns(890)); // oops
		oe.write(Logic.L0, Time.ns(930));
		oe.write(Logic.L1, Time.ns(1020));

		address.write(LInteger.Z, Time.ns(1080));
		data.write(LInteger.Z, Time.ns(1090));
		oe.write(Logic.Z, Time.ns(1130));
		oe.write(Logic.Z, Time.ns(1220));
	}

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

Print

User Rating: 0 / 5

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive