Beispiel #1
0
        public void TestToString()
        {
            Clock  clock  = new Clock();
            TicTok tictok = new TicTok();

            tictok.Init();

            int  TState = 0;
            AReg areg   = new AReg();
            TReg treg   = new TReg();
            BReg breg   = new BReg();
            CReg creg   = new CReg();
            IReg ireg   = new IReg();

            IPort1 port1 = new IPort1();
            IPort2 port2 = new IPort2();

            MDR mdr = new MDR();
            RAM ram = new RAM();

            mdr.SetRefToRAM(ref ram);

            ALU alu = new ALU(ref areg, ref treg);

            OReg3 oreg3 = new OReg3(ref alu);
            OReg4 oreg4 = new OReg4(ref alu);

            HexadecimalDisplay hexadecimalDisplay = new HexadecimalDisplay(ref oreg3);


            Flag flagReg = new Flag(ref alu);
            PC   pc      = new PC(ref flagReg);
            MAR  mar     = new MAR(ref ram);
            SEQ  seq     = SEQ.Instance();

            Wbus.Instance().Value = string.Concat(Enumerable.Repeat('0', 16));

            Instruction currentInstruction = new Instruction
            {
                OpCode         = "ADD B",
                BinCode        = "10000000",
                TStates        = 4,
                AffectsFlags   = true,
                AddressingMode = AddressingMode.Register,
                Bytes          = 1
            };

            Frame frame = new Frame(currentInstruction, TState, port1, port2, pc, mar, ram,
                                    ram.RAMDump(), mdr, ireg, SEQ.Instance(),
                                    Wbus.Instance().Value, areg, alu, flagReg,
                                    treg, breg, creg, oreg3, oreg4, hexadecimalDisplay);

            _ = frame.ToString();
            _ = frame.OutputRegister();
        }
Beispiel #2
0
        // *************************************************************************

        // *************************************************************************
        // Engine Runtime
        // *************************************************************************
        public void Run()
        {
            Clock  clock  = new Clock();
            TicTok tictok = new TicTok();

            tictok.Init();

            AReg areg = new AReg();
            TReg treg = new TReg();
            BReg breg = new BReg();
            CReg creg = new CReg();
            IReg ireg = new IReg();

            IPort1 iport1 = new IPort1();
            IPort2 iport2 = new IPort2();

            MDR mdr = new MDR();
            RAM ram = new RAM();

            mdr.SetRefToRAM(ref ram);

            ALU alu = new ALU(ref areg, ref treg);

            areg.SetALUReference(ref alu);
            breg.SetALUReference(ref alu);
            creg.SetALUReference(ref alu);

            OReg3 oreg3 = new OReg3(ref alu);
            OReg4 oreg4 = new OReg4(ref alu);
            HexadecimalDisplay hexadecimalDisplay = new HexadecimalDisplay(ref oreg3);

            Flag flagReg = new Flag(ref alu);
            PC   pc      = new PC(ref flagReg);
            MAR  mar     = new MAR(ref ram);
            SEQ  seq     = SEQ.Instance();

            Wbus.Instance().Value = string.Concat(Enumerable.Repeat('0', 16));

            areg.Subscribe(clock);
            treg.Subscribe(clock);
            breg.Subscribe(clock);
            creg.Subscribe(clock);
            ireg.Subscribe(clock);
            mar.Subscribe(clock);

            pc.Subscribe(clock);
            alu.Subscribe(clock); // ALU must come after A and T
            flagReg.Subscribe(clock);
            ram.Subscribe(clock);
            mdr.Subscribe(clock);

            oreg3.Subscribe(clock);
            hexadecimalDisplay.Subscribe(clock);
            oreg4.Subscribe(clock);

            // Load the program into the RAM
            ram.LoadProgram(Program);

            // Load the intsructionSet into the SEQ
            seq.Load(InstructionSet);

            Frame tempFrame;

            #region Program_Exec

            // Since T1-T3 for all of the Intruction is the same,
            // LDA or "0000" will be used as the intruction for all T1-T3's
            clock.IsEnabled = true;

            int max_loop_count = 10_000;
            int loop_counter   = 0;

            int TState = 1;

            // A basic empty instruction state with 3 TStates since on the 4th the instruction
            // will be known and set to a new object reference.
            Instruction currentInstruction = new Instruction()
            {
                OpCode  = "???",
                TStates = 4 // Since by 4 TStates it should know what instruction it is on
            };

            List <string> controlWords = new List <string>();
            bool?         didntJump    = null;

            while (clock.IsEnabled)
            {
                // Log the Instruction
                if (TState == 4)
                {
                    currentInstruction = InstructionSet.Instructions.FirstOrDefault(i => i.BinCode.Equals(ireg.RegContent));
                    seq.LoadBackupControlWords(currentInstruction.MicroCode);

                    string iname      = currentInstruction.OpCode;
                    int    operandVal = Convert.ToInt32(ireg.RegContent, 2);
                    string hexOperand = "0x" + operandVal.ToString("X");
                }

                if (TState <= 3)
                {
                    seq.UpdateControlWordReg(TState, "00000000", didntJump);

                    if (didntJump ?? false)
                    {
                        pc.SkipByte();
                        didntJump = null;
                    }
                }
                else
                {
                    seq.UpdateControlWordReg(TState, ireg.RegContent);
                }

                clock.SendTicTok(tictok);
                tictok.ToggleClockState();
                clock.SendTicTok(tictok);
                tictok.ToggleClockState();

                tempFrame = new Frame(currentInstruction, TState, iport1, iport2, pc, mar, ram,
                                      ram.RAMDump(), mdr, ireg, SEQ.Instance(),
                                      Wbus.Instance().Value, areg, alu, flagReg,
                                      treg, breg, creg, oreg3, oreg4, hexadecimalDisplay);

                _FrameStack.Add(tempFrame);

                // HLT
                if (ireg.RegContent.Equals("01110110", StringComparison.Ordinal) && TState == 5)
                {
                    clock.IsEnabled = false;

                    _RAMDump = ram.RAMDump();
                }

                if (loop_counter >= max_loop_count)
                {
                    throw new EngineRuntimeException("Engine Error: Infinite Loop Detected");
                }
                else
                {
                    loop_counter++;
                }

                if (TState == 7 && currentInstruction.OpCode.StartsWith('J'))
                {
                    pc.CheckForJumpCondition();

                    // PC is going to jump so do not let it fetch the next byte and immediately endx
                    if (!pc.WontJump)
                    {
                        currentInstruction.TStates = 7;
                        didntJump = true;
                    }
                }

                if (TState < currentInstruction.TStates)
                {
                    TState++;
                }
                else
                {
                    TState             = 1;
                    currentInstruction = new Instruction()
                    {
                        OpCode  = "???",
                        TStates = 4 // Since by 4 TStates it should know what instruction it is on
                    };
                }
            }

            OutputReg = oreg3.ToString();

            #endregion Program_Exec
        }
Beispiel #3
0
        public Frame(Instruction instruction, int TState, IPort1 ip1, IPort2 ip2, PC pc, MAR mar, RAM ram,
                     List <string> ramContents, MDR mdr, IReg ireg, SEQ seq, string wbus_string,
                     AReg areg, ALU alu, Flag flagReg, TReg treg, BReg breg, CReg creg,
                     OReg3 oreg3, OReg4 oreg4, HexadecimalDisplay hexadecimalDisplay)
        {
            InstructionData = instruction;
            if (InstructionData.OpCode.Contains(','))
            {
                InstructionData.OpCode = InstructionData.OpCode.Replace(",", string.Empty);
            }

            this.TState = TState;

            this.AReg = areg.ToString_Frame_Use();
            this.BReg = breg.ToString_Frame_Use();
            this.CReg = creg.ToString_Frame_Use();
            this.TReg = treg.ToString_Frame_Use();
            this.IReg = ireg.ToString_Frame_Use();  // The real ToString() is in use with a substring in it.  This is needed for proper operation
            this.MAR  = mar.ToString_Frame_Use();
            this.MDR  = mdr.RegContent;

            this.PC   = pc.RegContent;
            this.ALU  = alu.ToString();
            this.WBus = wbus_string;

            this.OPort1             = oreg3.ToString_Frame_Use();
            this.OPort2             = oreg4.ToString_Frame_Use();
            this.HexadecimalDisplay = hexadecimalDisplay.RegContent;

            this.RAM = ramContents;

            this.SEQ     = seq.ToString();
            this.WBus    = wbus_string; // I didnt want to mess with the Singleton in the frame, so the value will just be passed as a string
            this.RAM_Reg = ram.ToString_Frame_Use();
            this.Flags   = flagReg.RegContent;

            if (instruction == null)
            {
                this.IReg = "???";
            }

            if (TState > 3)
            {
                Instruction = InstructionData.OpCode;
            }
            else
            {
                Instruction = "???";
            }
        }
Beispiel #4
0
        public override XmlElement Export(XmlDocument doc, XmlElement parent)
        {
            XmlElement current = base.Export(doc, parent);

            if (ActiveFrom != null)
            {
                current.SetAttribute("ACTIVE_FROM", ActiveFrom.ToString());
            }
            if (ActiveTill != null)
            {
                current.SetAttribute("ACTIVE_TILL", ActiveTill.ToString());
            }
            if (Confcal != null)
            {
                current.SetAttribute("CONFCAL", Confcal.ToString());
            }
            if (Date != null)
            {
                current.SetAttribute("DATE", Date.ToString());
            }
            if (Days != null)
            {
                current.SetAttribute("DAYS", Days.ToString());
            }
            if (DaysAndOr != null)
            {
                current.SetAttribute("DAYS_AND_OR", DaysAndOr.ToString());
            }
            if (DaysCal != null)
            {
                current.SetAttribute("DAYSCAL", DaysCal.ToString());
            }
            if (Level != null)
            {
                current.SetAttribute("LEVEL", Level.ToString());
            }
            if (MaxWait != null)
            {
                current.SetAttribute("MAXWAIT", MaxWait.ToString());
            }
            if (Retro != null)
            {
                current.SetAttribute("RETRO", Retro.ToString());
            }
            if (Shift != null)
            {
                current.SetAttribute("SHIFT", Shift.ToString());
            }
            if (ShiftNum != null)
            {
                current.SetAttribute("SHIFTNUM", ShiftNum.ToString());
            }
            if (TagsActiveFrom != null)
            {
                current.SetAttribute("TAGS_ACTIVE_FROM", TagsActiveFrom.ToString());
            }
            if (TagsActiveTill != null)
            {
                current.SetAttribute("TAGS_ACTIVE_TILL", TagsActiveTill.ToString());
            }
            if (WeekDays != null)
            {
                current.SetAttribute("WEEKDAYS", WeekDays.ToString());
            }
            if (Weekscal != null)
            {
                current.SetAttribute("WEEKSCAL", Weekscal.ToString());
            }
            if (JAN != null)
            {
                current.SetAttribute("JAN", JAN.ToString());
            }
            if (FEB != null)
            {
                current.SetAttribute("FEB", FEB.ToString());
            }
            if (MAR != null)
            {
                current.SetAttribute("MAR", MAR.ToString());
            }
            if (APR != null)
            {
                current.SetAttribute("APR", APR.ToString());
            }
            if (MAY != null)
            {
                current.SetAttribute("MAY", MAY.ToString());
            }
            if (JUN != null)
            {
                current.SetAttribute("JUN", JUN.ToString());
            }
            if (JUL != null)
            {
                current.SetAttribute("JUL", JUL.ToString());
            }
            if (AUG != null)
            {
                current.SetAttribute("AUG", AUG.ToString());
            }
            if (SEP != null)
            {
                current.SetAttribute("SEP", SEP.ToString());
            }
            if (OCT != null)
            {
                current.SetAttribute("OCT", OCT.ToString());
            }
            if (NOV != null)
            {
                current.SetAttribute("NOV", NOV.ToString());
            }
            if (DEC != null)
            {
                current.SetAttribute("DEC", DEC.ToString());
            }
            return(current);
        }
Beispiel #5
0
        /// <summary>
        /// Randomly generates a firm object (production technology and output market parameters).
        /// </summary>
        /// <param name="ip">A pointer to the collection of input parameters.</param>
        /// <param name="FirmID">Unique identifier for this firm (run number)</param>
        public Firm(InputParameters ip, int FirmID)
        {
            // Choose random values for DISP2 (the top DISP1 resources
            // account for DISP2 percent of total resource costs), and
            // density (sparsity) of resource consumption pattern matrix
            this.g = GenRandNumbers.GenUniformDbl(ip.DISP2_MIN, ip.DISP2_MAX);
            this.d = GenRandNumbers.GenUniformDbl(ip.DNS_MIN, ip.DNS_MAX);

            // Generate the true product margins and the true, optimal
            // decision vector. Keep generating new margins until there
            // is at least one product in the optimal mix.
            RowVector MAR, DECT0;

            do
            {
                MAR   = this.GenMargins(ip);
                DECT0 = MAR.Map(x => (x < 1.0) ? 0.0 : 1.0);
            } while (DECT0.TrueForAll(x => x == 0.0));

            // Generate vector of maximum production quantities
            this.mxq = this.GenMXQ(ip);
            // And associated vector of optimal production quantities
            ColumnVector QT = mxq.ewMultiply(DECT0);

            // Flowchart 5.1 - Create resource consumption pattern matrix
            this.res_cons_pat = GenResConsPat(ip);

            // Flowchart 5.2 - Compute TRU
            // Calculate vector of total units of resource
            // consumption, by product
            ColumnVector TRU = this.CalcResConsumption(QT);

            // Flowchart 5.3 - Compute MAXRU
            // Calculate resource consumption under the assumption
            // that all products are produced at maximum quantity
            ColumnVector MAXRU = this.CalcResConsumption(mxq);

            RowVector RCC, PC_B, RCCN;
            double    TCT0;

            #region Flowchart 5.4 - Generate RCC, RCU, and RCCN

            /* -------------------------------- */
            // Flowchart 5.4(a)-(g)

            // Generate vector of total resource costs (RCC)
            RCC = GenRCC(ip);

            /* -------------------------------- */
            // Flowchart 5.4(h)

            // Now generate unit resource costs (RCU) by doing element-wise
            // division of RCC by MAXRU
            this.rcu = RCC.Map((x, i) => x / MAXRU[i]);

            /* -------------------------------- */
            // Flowchart 5.4(i)

            // Compute new RCC vector (RCCN) based on unit resource
            // costs (RCU) and true unit resource consumption (TRU)
            RCCN = this.rcu.ewMultiply(TRU);
            // Check to see if the first resource (RCCN[0]) is the largest.
            // If not, increase RCU[0] by just enough to make it so.
            if (RCCN[0] < RCCN.Skip(1).Max() + 1)
            {
                RCCN[0]     = Math.Ceiling(RCCN.Max()) + 1.0;
                this.rcu[0] = RCCN[0] / TRU[0];
            }

            #endregion

            // Flowchart 5.5 - Calculate PC_B
            // Calculate true unit product costs
            PC_B = this.CalcTrueProductCosts();

            // Flowchart 5.6 - Compute total costs TCT0
            // Compute total costs
            TCT0 = this.CalcTotCosts(QT);

            // Flowchart 5.7 - Rename RCCN to RCC
            RCC         = RCCN;
            initial_rcc = RCC;

            #region Flowchart 5.8 - Calculate SP, TRV0, PROFITT0

            // Calculate product selling prices, total revenue, and profit
            this.sp = PC_B.ewMultiply(MAR);
            double TRV0 = this.sp * QT;
            this.profitt0 = TRV0 - TCT0;

            #endregion

            // 5.9(a) Create RANK vector
            // Note: this method provides a stable sort. It's important to use a stable sort.
            // LOOKUP IN VERSION.TXT WHY IT'S IMPORTANT TO USE A STABLE SORT HERE.
            initial_rank = Enumerable.Range(0, RCC.Dimension).OrderByDescending(i => RCC[i]).ToArray();

            #region Flowchart 5.9(b) - Create RES_CONS_PAT_PRCT

            this.res_cons_pat_prct = new RectangularMatrix(ip.RCP, ip.CO);

            for (int r = 0; r < this.res_cons_pat.RowCount; ++r)
            {
                RowVector rv = this.res_cons_pat.Row(r);
                if (TRU[r] != 0.0)
                {
                    rv = rv.Map((alt_ij, col) => alt_ij * QT[col] / TRU[r]);
                    if (Math.Abs(rv.Sum() - 1.0) > 0.01)
                    {
                        throw new ApplicationException("Sum of row of RES_CONS_PAT_PRCT not equal to 1.");
                    }
                }
                else
                {
                    rv = rv.Map(alt_ij => 0.0);
                }

                this.res_cons_pat_prct.CopyRowInto(rv, r);
            }

            #endregion

            #region Flowchart 5.9(c) - Create correlation matrix
            // Create correlation matrix for rows of RES_CONS_PAT_PRCT
            MultivariateSample mvs = new MultivariateSample(ip.RCP);
            for (int c = 0; c < this.res_cons_pat_prct.ColumnCount; ++c)
            {
                mvs.Add(this.res_cons_pat_prct.Column(c));
            }

            this.pearsoncorr = new SymmetricMatrix(ip.RCP);

            for (int i = 0; i < mvs.Dimension; ++i)
            {
                for (int j = i; j < mvs.Dimension; ++j)
                {
                    //PearsonCorr[i, j] = mvs.PearsonRTest( i, j ).Statistic;
                    this.pearsoncorr[i, j] = mvs.TwoColumns(i, j).PearsonRTest().Statistic;
                }
            }

            #endregion

            // Flowchart 5.10 - Logging true system
            // Note: I'm deliberately passing copies of the fields MXQ, SP, etc.
            Output.LogFirm(
                ip, this, FirmID,
                MAR, DECT0,
                TRV0, TCT0, profitt0,
                RCC);
        }