

#include "Sim.h"

/* ===============================================================
   Multiplexor
   =============================================================== */
void Mux ( const SD &sd, const Signal &c, const Signal &in, const Signal &out)
{
   Module( sd, "Mux", (in,c), out);    // Start a new coordinate system.

   Signal not_c0, not_c1, out_0, out_1, out_2, out_3;

   Not ( SD(sd,"ab"), c[0], not_c0 );
   Not ( SD(sd,"bb"), c[1], not_c1 );

   And ( SD(sd,"dc"), (in[3], c[1],   c[0]),   out_3 );
   And ( SD(sd,"cc"), (in[2], c[1],   not_c0), out_2 );
   And ( SD(sd,"bc"), (in[1], not_c1, c[0]),   out_1 );
   And ( SD(sd,"ac"), (in[0], not_c1, not_c0), out_0 );

   Or  ( SD(sd,"cd"), (out_0, out_1, out_2, out_3), out[0] );
}

/* ===============================================================
   Demultiplexor
   =============================================================== */
void Demux( const SD &sd, const Signal &c, const Signal &in, const Signal &out)

{
   Module( sd, "DeMux", (in, c), out);    // Start a new coordinate system.

   Signal not_c0, not_c1;

   Not ( SD(sd,"ab"), c[0], not_c0 );
   Not ( SD(sd,"bb"), c[1], not_c1 );

   And ( SD(sd,"dc"), (in, c[1],   c[0]),   out[3]);
   And ( SD(sd,"cc"), (in, c[1],   not_c0), out[2]);
   And ( SD(sd,"bc"), (in, not_c1, c[0]),   out[1]);
   And ( SD(sd,"ac"), (in, not_c1, not_c0), out[0]);
}


void simnet()
{
   Signal sw0, sw1, sw2, sw3;         // Inputs
   Signal c0, c1, d0, d1;             // Controls
   Signal line;
   Signal out0, out1, out2, out3;     // Outputs

   /* --------------------------------------------
      Inputs
      -------------------------------------------- */
   Switch("aa", sw3, '3', Zero);
   Switch("ba", sw2, '2', Zero);
   Switch("ca", sw1, '1', Zero);
   Switch("da", sw0, '0', Zero);

   /* --------------------------------------------
      Controls for the Mux
      -------------------------------------------- */
   Switch("fa", c1, 'b', Zero);
   Switch("ga", c0, 'a', Zero);

   /* --------------------------------------------
      Mux the 4 inputs into 1 output
      -------------------------------------------- */
   Mux( "ab-db", (c1,c0), (sw3,sw2,sw1,sw0), line );

   /* --------------------------------------------
      Controls for the DeMux
      -------------------------------------------- */
   Switch("fe", d1, 'd', Zero);
   Switch("ge", d0, 'c', Zero);

   Demux("ae-de", (d1,d0), line, (out3,out2,out1,out0) );

   Probe("af", out3);
   Probe("bf", out2);
   Probe("cf", out1);
   Probe("df", out0);
}



