/* 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 SD &sd, const Signal &in, const Signal &out)
{
   Signal not_c0, not_c1, out_0, out_1, out_2, out_3;


   Module( sd, in, out);            // Start a new coordinate system.

   Not ( SD(sd,"ab"), in[4], not_c0 );
   Not ( SD(sd,"bb"), in[5], not_c1 );
//       ^^^^^^^^^^^^ 
//    Coordinate system inside Schema Diagram SD

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


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


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

   /* The input bits to the mux */
   Switch ( "da", in0, '0', Zero );
   Switch ( "ca", in1, '1', Zero );
   Switch ( "ba", in2, '2', Zero );
   Switch ( "aa", in3, '3', Zero );
   
   /* The control bits of the mux */
   Switch ( "fa", co1, '4', Zero );
   Switch ( "ga", co0, '5', Zero );
   
   MyMux ( "ab-dd", (co1, co0, in3, in2, in1, in0), out );
//                   ^^^                      ^^^
//                   in[5]                    in[0]
   
   Probe ( "ce", out );
}



