The following code illustrates how an RS-latch can be described. An RS-latch has two input ports, two output ports and two two-input NAND gates which can be described in a manner identical to the description of the two-input AND gate. Note that an RS-latch does not have two wires embedded within it. The feedback mechanism means that the two output ports, Q and Qb, also act as input ports to the two NAND gates. Therefore there are no wires created by the RS-latch.
The four ports and the two NAND gates are encapsulated in the RS-latch class as shown in the following class declaration:
class RS_Latch : public Component { public: RS_Latch(Connector &, Connector &, Connector &, Connector &, ckt_time = UNDEF_TIME, char* = "RS_Latch"); private: Input S, R; Output Q, Qb; Nand2 nand2a, nand2b; };
Next, the constructor of the class is defined. The four ports are connected with the four connectors passed into the constructor. The first NAND gate gets its first input from the S input port and its second input from the Qb output port. It sends its output to the Q output port. The second NAND gate gets its two inputs from the Q output port and the R input port of the RS-latch. Its output is sent to the Qb output port.
RS_Latch::RS_Latch(Connector &ci1, Connector &ci2, Connector &co1, Connector &co2, ckt_time dly, char *name) : Component(dly, name), CONNECT(S, ci1, "S"), CONNECT(R, ci2, "R"), CONNECT(Q, co1, "Q"), CONNECT(Qb, co2, "Qb"), nand2a(S, Qb, Q, 1L, "nand2a"), nand2b(Q, R, Qb, 1L, "nand2b") { }
A diagram showing the interconnectivity of the components within the RS-latch is presented in Figure 3.3. Again, the pointers connecting the input and output ports are omitted for clarity. Note how the feedback is achieved.
Figure 3.3: RS-Latch with External Wires