\documentclass[
	solutions,
	shortwarning,
	singlenumbering,
	nopagenumber
]{../../resources/ormc_handout}
\usepackage{../../resources/macros}
\geometry{top = 20mm}


\usepackage{tikz}
\usetikzlibrary{circuits.logic.US}
\usetikzlibrary{plotmarks}


\title{Warm-Up: Flip-Flops}
\subtitle{Prepared by \githref{Mark} on \today.}

\begin{document}

	\maketitle



	\problem{}
	Below is a circuit of logic gates. S and R are input pins, Q is an output pin. \par
	What does it do?

	\begin{center}
	\begin{tikzpicture}[circuit logic US, scale=2]
		% tikz notes:
		% -|: Select vertical of first and horizontal of second
		
		% Gates
		\node[or gate] (or) at (0,0) {\tiny\texttt{or}};
		\node[not gate] (not) at (0,-0.8) {\tiny\texttt{not}};
		\node[and gate] (and) at (1.4,-0.4) {\tiny\texttt{and}};

		% Important locations
		\draw (or.input 1) ++(-0.5, 0) coordinate (start);
		\draw (or.input 1) ++(-0.25, 0) coordinate (startjoin);
		\draw (and.output) ++(0.25, 0) coordinate (endjoin);
		\draw (and.output) ++(0.75, 0) coordinate (end);

		% Connections
		\draw (or.input 2) -- (or.input 2 -| start) node[anchor=east] {S};
		\draw (not.input) -- (not.input -| start) node[anchor=east] {R};
		\draw (or.output) -| (or.output -| 0.75,0) |- (and.input 1);
		\draw (not.output) -| (not.output -| 0.75,0) |- (and.input 2);
		\draw (and.output) -- (and.output -| endjoin) -- ++(0,0.9) -| (startjoin) -- (or.input 1);
		\filldraw (and.output -| endjoin) circle[radius=0.3mm];
		\draw (endjoin) -- (end) node[anchor=west] {Q};
	\end{tikzpicture}
	\end{center}


	\begin{solution}
		This is an S-R (\say{set-reset}) latch. \par
		Set S to high, and Q will stay high until it is reset by R.
	\end{solution}

	\vfill


	\problem{}
	Below is another circuit. How does it work? \par
	\hint{\texttt{nor} = not or. It works exactly as you'd expect.}


	\begin{center}
	\begin{tikzpicture}[circuit logic US, scale=2]

		% Gates
		\node[nor gate] (nora) at (0,0) {\tiny\texttt{nor}};
		\node[nor gate] (norb) at (0,-1.2) {\tiny\texttt{nor}};

		% Important locations
		\draw (nora.input 1) ++(-0.5, 0) coordinate (start);
		\draw (nora.input 1) ++(-0.25, 0) coordinate (startjoin);
		\draw (nora.output) ++(0.25, 0) coordinate (endjoin);
		\draw (nora.output) ++(0.75, 0) coordinate (end);

		% Connections
		% Input extension
		\draw (nora.input 1) -- (nora.input 1 -| start) node[anchor=east] {S};
		%\draw (nora.input 2 -| start) -- (nora.input 2);
		%\draw (norb.input 1 -| start) -- (norb.input 1);
		\draw (norb.input 2) -- (norb.input 2 -| start) node[anchor=east] {R};
		% Output extension
		\draw (norb.output) -- (norb.output -| end) node[anchor=west] {Q};
		%\draw (norb.output) -- ++(1, 0) node[anchor=west] {$\overline{\text{Q}}$};
		% Cross-connection
		\draw (nora.output -| endjoin) -- (endjoin |- 0,-0.4) -- (startjoin |- 0,-0.8) -- (norb.input 1 -| startjoin) -- (norb.input 1);
		\draw (norb.output -| endjoin) -- (endjoin |- 0,-0.8) -- (startjoin |- 0,-0.4) -- (nora.input 2 -| startjoin) -- (nora.input 2);
		\draw (norb.output) -- (norb.output -| endjoin);
		\filldraw (norb.output -| endjoin) circle[radius=0.3mm];
		\draw (nora.output) -- (nora.output -| endjoin);

	\end{tikzpicture}
	\end{center}

	\begin{solution}
		This is another S-R (\say{set-reset}) latch.
	\end{solution}
	
	\vfill
	\pagebreak





	\problem{}
	Empty circles represent inverted inputs/outputs.
	This is the reason a \texttt{nand} gate looks like an \texttt{and} gate with a dot.
	Pay careful attention to the nand gate in the bottom-left.

	\vspace{2mm}

	What does this circuit do?


	\begin{center}
	\begin{tikzpicture}[circuit logic US, scale=2]
		
		
		% Gates
		\node[nand gate] (nandc) at (1.4,0) {\tiny\texttt{nand}};
		\node[nand gate] (nandd) at (1.4,-1.2) {\tiny\texttt{nand}};
		% inputs=in => 1 is inverted, 2 is normal
		\node[nand gate] (nanda) at (nandc.input 1 -| 0,0) {\tiny\texttt{nand}};
		\node[nand gate,inputs=in] (nandb) at (nandd.input 2 -| 0,-1.2) {\tiny\texttt{nand}};

		% Important locations
		\draw (nanda.input 1) ++(-1, 0) coordinate (start);
		\draw (nanda.input 1) ++(-0.5, 0) coordinate (startjoina);
		\draw (nanda.input 1) ++(-0.25, 0) coordinate (startjoinb);
		\draw (nandc.input 1) ++(-0.5, 0) coordinate (midstart);
		\draw (nandc.input 1) ++(-0.25, 0) coordinate (midstartjoin);
		\draw (nandc.output) ++(0.25, 0) coordinate (endjoin);
		\draw (nandc.output) ++(0.75, 0) coordinate (end);

		% Connections
		\draw (nanda.input 1) -- (nanda.input 1 -| start) node[anchor=east] {D};
		\draw (nandb.input 2) -- (nandb.input 2 -| start) node[anchor=east] {C};
		\draw (nanda.input 1 -| startjoinb) |- (nandb.input 1);
		\draw (nandb.input 2 -| startjoina) |- (nanda.input 2);
		\filldraw (nanda.input 1 -| startjoinb) circle[radius=0.3mm];
		\filldraw (nandb.input 2 -| startjoina) circle[radius=0.3mm];
		\draw (nanda.output) -- (nandc.input 1);
		\draw (nandb.output) -- (nandd.input 2);
		\draw (nandc.output) -- (nandc.output -| end) node[anchor=west] {Q};
		\draw (nandc.output -| endjoin) -- (endjoin |- 0,-0.4) -- (midstartjoin |- 0,-0.8) -- (nandd.input 1 -| midstartjoin) -- (nandd.input 1);
		\draw (nandd.output -| endjoin) -- (endjoin |- 0,-0.8) -- (midstartjoin |- 0,-0.4) -- (nandc.input 2 -| midstartjoin) -- (nandc.input 2);
		\draw (nandd.output) -- (nandd.output -| endjoin);
		\draw (nandc.output) -- (nandc.output -| endjoin) circle[radius=0.3mm];
		\filldraw (nandc.output -| endjoin) circle[radius=0.3mm];
	\end{tikzpicture}
	\end{center}


	\begin{solution}
		This is a D (\say{data}) flip-flop. \par
		D is a \say{data} pin. When C is high, this circuit sets Q to D. \par
		When C is low, Q stays at its previous value until D is set to high again.

		\vspace{2mm}

		In other words, this circuit \say{reads} D whenever C is high.
	\end{solution}

	\vfill
	\pagebreak


\end{document}