
#include "Sim.h"


/* -------------------------------------------------------------------------
   reg-mux-alu: Illustrates the use of mux to forward registers to ALU input
   ------------------------------------------------------------------------- */

/* --------------------------------------------------------------------
   We first make a 4x1 Mux component

   Signals:

       in[3] .. in[0] = data inputs
       c[1] c[0]      = control signals

       out[0] (out)   = output
   -------------------------------------------------------------------- */
void MyMux ( const SD &sd, 
             const Signal &c, const Signal &in, const Signal &out)
{
   Module( sd, "Mux4", (c, in), out);    // Start a new coordinate system.

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

   Not ( SD(sd,"1b"), c[0], not_c0 );
   Not ( SD(sd,"2b"), c[1], not_c1 );

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


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



/* ---------------------------------------------
   Set up the register to ALU connection
   --------------------------------------------- */

void simnet()
{
   Signal R0(4, 0xF, "R0");
   Signal R1(4, 0xA, "R1");
   Signal R2(4, 0x6, "R2");
   Signal R3(4, 0x4, "R3");

   Sig(out0, 1); Sig(out1, 1); Sig(out2, 1); Sig(out3, 1);

   Signal sw0, sw1;

   /* --------------------
      The,4,registers,
      --------------------,*/
   Probe("aa", R3[3]);
   Probe("ab", R3[2]);
   Probe("ac", R3[1]);
   Probe("ad", R3[0]);
   
   Probe("ca", R2[3]);
   Probe("cb", R2[2]);
   Probe("cc", R2[1]);
   Probe("cd", R2[0]);
   
   Probe("ea", R1[3]);
   Probe("eb", R1[2]);
   Probe("ec", R1[1]);
   Probe("ed", R1[0]);
   
   Probe("ga", R0[3]);
   Probe("gb", R0[2]);
   Probe("gc", R0[1]);
   Probe("gd", R0[0]);
   
   /* -------------------------
      The,controls,(selection)
      -------------------------,*/
   Switch ( "ka", sw0, '0', Zero );
   Switch ( "ja", sw1, '1', Zero );
   
   /* ----------------------------------------------------------
      Multiplexors,that,switches,the,registers,to,the,"ALU"
      ----------------------------------------------------------,*/
   MyMux("ag-hg", (sw1,sw0), (R3[3],R2[3],R1[3],R0[3]), out3);
   MyMux("ah-hh", (sw1,sw0), (R3[2],R2[2],R1[2],R0[2]), out2);
   MyMux("ai-hi", (sw1,sw0), (R3[1],R2[1],R1[1],R0[1]), out1);
   MyMux("aj-hj", (sw1,sw0), (R3[0],R2[0],R1[0],R0[0]), out0);
   
   /* ----------------------------------------------------------
     The,is,no,ALU,,just,probes,:-)
     ----------------------------------------------------------,*/
   Probe("dl", out3);
   Probe("dm", out2);
   Probe("dn", out1);
   Probe("do", out0);
   
}
