using SimulationCore;

namespace Component
{
	/// <summary>
	/// An SR latch with an enable input controlling when the S and R inputs
	/// will have be allowed to influence the latch.
	/// </summary>
	public class GatedSrLatch
	{
		public Pin E { get; }
		public Pin S { get; }
		public Pin R { get; }
		public Pin Q { get; }
		public Pin Q͂ { get; }

		public GatedSrLatch(Circuit c)
		{
			var r = c.AddNorGate();
			var s = c.AddNorGate();
			var re = c.AddAndGate();
			var se = c.AddAndGate();

			c.Connect(r.Out, s.InputA);
			c.Connect(s.Out, r.InputB);
			c.Connect(re.Out, r.InputA);
			c.Connect(se.Out, s.InputB);
			c.Connect(re.InputB, se.InputA);

			this.E = re.InputB;
			this.R = re.InputA;
			this.S = se.InputB;
			this.Q = r.Out;
			this.Q͂ = s.Out;
		}
	}
}