Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SimpleComponents;
  7.  
  8. namespace Machine
  9. {
  10.     public class CPU16
  11.     {
  12.         //this "enum" defines the different control bits names
  13.         public const int J3 = 0, J2 = 1, J1 = 2, D3 = 3, D2 = 4, D1 = 5, C6 = 6, C5 = 7, C4 = 8, C3 = 9, C2 = 10, C1 = 11, A = 12, X2 = 13, X1 = 14, Type = 15;
  14.  
  15.         public int Size { get; private set; }
  16.  
  17.         //CPU inputs
  18.         public WireSet Instruction { get; private set; }
  19.         public WireSet MemoryInput { get; private set; }
  20.         public Wire Reset { get; private set; }
  21.  
  22.         //CPU outputs
  23.         public WireSet MemoryOutput { get; private set; }
  24.         public Wire MemoryWrite { get; private set; }
  25.         public WireSet MemoryAddress { get; private set; }
  26.         public WireSet InstructionAddress { get; private set; }
  27.  
  28.         //CPU components
  29.         private ALU m_gALU;
  30.         private Counter m_rPC;
  31.         private MultiBitRegister m_rA, m_rD;
  32.         private BitwiseMux m_gAMux, m_gMAMux;
  33.  
  34.         //here we initialize and connect all the components, as in Figure 5.9 in the book
  35.         public CPU16()
  36.         {
  37.             Size =  16;
  38.  
  39.             Instruction = new WireSet(Size);
  40.             MemoryInput = new WireSet(Size);
  41.             MemoryOutput = new WireSet(Size);
  42.             MemoryAddress = new WireSet(Size);
  43.             InstructionAddress = new WireSet(Size);
  44.             MemoryWrite = new Wire();
  45.             Reset = new Wire();
  46.  
  47.             m_gALU = new ALU(Size);
  48.             m_rPC = new Counter(Size);
  49.             m_rA = new MultiBitRegister(Size);
  50.             m_rD = new MultiBitRegister(Size);
  51.  
  52.             m_gAMux = new BitwiseMux(Size);
  53.             m_gMAMux = new BitwiseMux(Size);
  54.  
  55.             m_gAMux.ConnectInput1(Instruction);
  56.             m_gAMux.ConnectInput2(m_gALU.Output);
  57.  
  58.             m_rA.ConnectInput(m_gAMux.Output);
  59.  
  60.             m_gMAMux.ConnectInput1(m_rA.Output);
  61.             m_gMAMux.ConnectInput2(MemoryInput);
  62.             m_gALU.InputY.ConnectInput(m_gMAMux.Output);
  63.  
  64.             m_gALU.InputX.ConnectInput(m_rD.Output);
  65.  
  66.             m_rD.ConnectInput(m_gALU.Output);
  67.  
  68.             MemoryOutput.ConnectInput(m_gALU.Output);
  69.             MemoryAddress.ConnectInput(m_rA.Output);
  70.  
  71.             InstructionAddress.ConnectInput(m_rPC.Output);
  72.             m_rPC.ConnectInput(m_rA.Output);
  73.             m_rPC.ConnectReset(Reset);
  74.  
  75.             //now, we call the code that creates the control unit
  76.             ConnectControls();
  77.         }
  78.  
  79.         //add here components to implement the control unit
  80.         private BitwiseMultiwayMux m_gJumpMux;//an example of a control unit compnent - a mux that controls whether a jump is made
  81.  
  82.         private OrGate orGate;
  83.         private NotGate notGateA;
  84.         private AndGate andGateD;
  85.         private AndGate andGateMW;
  86.         private AndGate andGatePC;
  87.  
  88.         private NotGate notEQ;
  89.         private AndGate andGT;
  90.         private NotGate notGT1;
  91.         private NotGate notGT2;
  92.         private NotGate notGE;
  93.         private OrGate orLE;
  94.         private OrGate orGate1;
  95.         private NotGate not1;
  96.  
  97.         private Wire zero;
  98.         private Wire one;
  99.         private Wire JLT;
  100.         private Wire JEQ;
  101.         private Wire JNE;
  102.         private Wire JGT;
  103.         private Wire JGE;
  104.         private Wire JLE;
  105.         private WireSet zeroSet;
  106.         private WireSet oneSet;
  107.         private WireSet JLTSet;
  108.         private WireSet JEQSet;
  109.         private WireSet JNESet;
  110.         private WireSet JGTSet;
  111.         private WireSet JGESet;
  112.         private WireSet JLESet;
  113.  
  114.         WireSet controlBits;
  115.  
  116.         private void ConnectControls()
  117.         {
  118.             //1. connect control of mux 1 (selects entrance to register A)
  119.             m_gAMux.ConnectControl(Instruction[Type]);
  120.             //2. connect control to mux 2 (selects A or M entrance to the ALU)
  121.             m_gMAMux.ConnectControl(Instruction[A]);
  122.             //3. consider all instruction bits only if C type instruction (MSB of instruction is 1)
  123.  
  124.             //4. connect ALU control bits
  125.             m_gALU.ZeroX.ConnectInput(Instruction[C1]);
  126.             m_gALU.NotX.ConnectInput(Instruction[C2]);
  127.             m_gALU.ZeroY.ConnectInput(Instruction[C3]);
  128.             m_gALU.NotY.ConnectInput(Instruction[C4]);
  129.             m_gALU.F.ConnectInput(Instruction[C5]);
  130.             m_gALU.NotOutput.ConnectInput(Instruction[C6]);
  131.             //5. connect control to register D (very simple)
  132.             andGateD = new AndGate();
  133.             m_rD.Load.ConnectInput(andGateD.Output);
  134.             andGateD.ConnectInput1(Instruction[Type]);
  135.             andGateD.ConnectInput2(Instruction[D2]);
  136.             //6. connect control to register A (a bit more complicated)
  137.             orGate = new OrGate();
  138.             notGateA = new NotGate();
  139.             m_rA.Load.ConnectInput(orGate.Output);
  140.             orGate.ConnectInput1(notGateA.Output);
  141.             orGate.ConnectInput2(Instruction[D1]);
  142.             notGateA.ConnectInput(Instruction[Type]);
  143.             //7. connect control to MemoryWrite
  144.             andGateMW = new AndGate();
  145.             MemoryWrite.ConnectInput(andGateMW.Output);
  146.             andGateMW.ConnectInput1(Instruction[Type]);
  147.             andGateMW.ConnectInput2(Instruction[D3]);
  148.             //8. create inputs for jump mux
  149.             zero = new Wire();
  150.  
  151.             one = new Wire();
  152.             orGate1 = new OrGate();
  153.             not1 = new NotGate();
  154.             one = orGate1.Output;
  155.             orGate1.ConnectInput1(Instruction[0]);
  156.             orGate1.ConnectInput2(not1.Output);
  157.             not1.ConnectInput(Instruction[0]);
  158.  
  159.             JLT = m_gALU.Negative;
  160.  
  161.             JEQ = m_gALU.Zero;
  162.  
  163.             notEQ = new NotGate();
  164.             JNE = notEQ.Output;
  165.             notEQ.ConnectInput(m_gALU.Zero);
  166.  
  167.             andGT = new AndGate();
  168.             notGT1 = new NotGate();
  169.             notGT2 = new NotGate();
  170.             JGT = andGT.Output;
  171.             andGT.ConnectInput1(notGT1.Output);
  172.             andGT.ConnectInput2(notGT2.Output);
  173.             notGT1.ConnectInput(m_gALU.Negative);
  174.             notGT2.ConnectInput(m_gALU.Zero);
  175.  
  176.             notGE = new NotGate();
  177.             JGE = notGE.Output;
  178.             notGE.ConnectInput(m_gALU.Negative);
  179.  
  180.             orLE = new OrGate();
  181.             JLE = orLE.Output;
  182.             orLE.ConnectInput1(m_gALU.Negative);
  183.             orLE.ConnectInput2(m_gALU.Zero);
  184.  
  185.             //9. connect jump mux (this is the most complicated part)
  186.             m_gJumpMux = new BitwiseMultiwayMux(1, 3);
  187.  
  188.             zeroSet = new WireSet(1);
  189.             zeroSet[0].ConnectInput(zero);
  190.             oneSet = new WireSet(1);
  191.             oneSet[0].ConnectInput(one);
  192.             JLTSet = new WireSet(1);
  193.             JLTSet[0].ConnectInput(JLT);
  194.             JEQSet = new WireSet(1);
  195.             JEQSet[0].ConnectInput(JEQ);
  196.             JNESet = new WireSet(1);
  197.             JNESet[0].ConnectInput(JNE);
  198.             JGTSet = new WireSet(1);
  199.             JGTSet[0].ConnectInput(JGT);
  200.             JGESet = new WireSet(1);
  201.             JGESet[0].ConnectInput(JGE);
  202.             JLESet = new WireSet(1);
  203.             JLESet[0].ConnectInput(JLE);
  204.  
  205.             m_gJumpMux.ConnectInput(0, zeroSet);
  206.             m_gJumpMux.ConnectInput(1, JGTSet);
  207.             m_gJumpMux.ConnectInput(2, JEQSet);
  208.             m_gJumpMux.ConnectInput(3, JGESet);
  209.             m_gJumpMux.ConnectInput(4, JLTSet);
  210.             m_gJumpMux.ConnectInput(5, JNESet);
  211.             m_gJumpMux.ConnectInput(6, JLESet);
  212.             m_gJumpMux.ConnectInput(7, oneSet);
  213.  
  214.             controlBits = new WireSet(3);
  215.             controlBits[0].ConnectInput(Instruction[J3]);
  216.             controlBits[1].ConnectInput(Instruction[J2]);
  217.             controlBits[2].ConnectInput(Instruction[J1]);
  218.  
  219.             m_gJumpMux.ConnectControl(controlBits);
  220.  
  221.             //10. connect PC load control
  222.             andGatePC = new AndGate();
  223.             m_rPC.Load.ConnectInput(andGatePC.Output);
  224.             andGatePC.ConnectInput1(m_gJumpMux.Output[0]);
  225.             andGatePC.ConnectInput2(Instruction[Type]);
  226.  
  227.         }
  228.  
  229.         public override string ToString()
  230.         {
  231.             return "A=" + m_rA + ", D=" + m_rD + ", PC=" + m_rPC + ",Ins=" + Instruction;
  232.         }
  233.  
  234.         private string GetInstructionString()
  235.         {
  236.             if (Instruction[Type].Value == 0)
  237.                 return "@" + Instruction.GetValue();
  238.             return Instruction[Type].Value + "XX " +
  239.                "a" + Instruction[A] + " " +
  240.                "c" + Instruction[C1] + Instruction[C2] + Instruction[C3] + Instruction[C4] + Instruction[C5] + Instruction[C6] + " " +
  241.                "d" + Instruction[D1] + Instruction[D2] + Instruction[D3] + " " +
  242.                "j" + Instruction[J1] + Instruction[J2] + Instruction[J3];
  243.         }
  244.  
  245.         //use this function in debugging to print the current status of the ALU. Feel free to add more things for printing.
  246.         public void PrintState()
  247.         {
  248.             Console.WriteLine("CPU state:");
  249.             Console.WriteLine("PC=" + m_rPC + "=" + m_rPC.Output.GetValue());
  250.             Console.WriteLine("A=" + m_rA + "=" + m_rA.Output.GetValue());
  251.             Console.WriteLine("D=" + m_rD + "=" + m_rD.Output.GetValue());
  252.             Console.WriteLine("Ins=" + GetInstructionString());
  253.             Console.WriteLine("ALU=" + m_gALU);
  254.             Console.WriteLine("inM=" + MemoryInput);
  255.             Console.WriteLine("outM=" + MemoryOutput);
  256.             Console.WriteLine("addM=" + MemoryAddress);
  257.         }
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement