/* Mux macro */

#include "Sim.h"


/* -----
   mux: Illustrates the use of macros

   in[]:
           in[n-1], in[n-2], ...., in[1], in[0]

   in[0] = right most input
   in[1] = one before the right most input
   ... 

   ----- */
void MyMux ( const Signal &in, const Signal &out)
{
   Sig(not_c0,1);
   Sig(not_c1,1);
   Sig(out_0,1);
   Sig(out_1,1);
   Sig(out_2,1);
   Sig(out_3,1);

   Not ( "1b", in[4], not_c0 );
   Not ( "2b", in[5], not_c1 );

   And ( "1c", (in[0], not_c1, not_c0),   out_0 );
   And ( "2c", (in[1], not_c1, in[4]),    out_1 );
   And ( "3c", (in[2], in[5],  not_c0),   out_2 );
   And ( "4c", (in[3], in[5],  in[4]),    out_3 );

   Or  ( "3d", (out_0, out_1, out_2, out_3),     out[0] );
}


/* -----
   Set up circuit that uses "MyMux"
   ----- */
void simnet()
{
   Sig(in0,1);
   Sig(in1,1);
   Sig(in2,1);
   Sig(in3,1);
   Sig(co0,1);
   Sig(co1,1);
   Sig(out,1);

   /* The input bits to the mux */
   Switch ( "4a", in0, '0', Zero );
   Switch ( "3a", in1, '1', Zero );
   Switch ( "2a", in2, '2', Zero );
   Switch ( "1a", in3, '3', Zero );
   
   /* The control bits of the mux */
   Switch ( "6a", co1, '4', Zero );
   Switch ( "7a", co0, '5', Zero );
   
   MyMux ( (co1, co0, in3, in2, in1, in0), out );
//                   ^^^                      ^^^
//                   in[5]                    in[0]
   
   Probe ( "3e", out );
}



