Beispiel #1
0
 public static void DBGERR(FOS_System.String testName, FOS_System.String msg)
 {
     BasicConsole.SetTextColour(BasicConsole.error_colour);
     DBGMSG(testName, msg);
     BasicConsole.SetTextColour(BasicConsole.default_colour);
     errors++;
 }
Beispiel #2
0
        private static void EnterCritical(FOS_System.String caller)
        {
            //BasicConsole.WriteLine("Entering critical section...");
            if (AccessLockInitialised)
            {
                if (AccessLock == null)
                {
                    BasicConsole.WriteLine("HeapAccessLock is initialised but null?!");
                    BasicConsole.DelayOutput(10);
                }
                else
                {
                    if (AccessLock.Locked && OutputTrace)
                    {
                        BasicConsole.SetTextColour(BasicConsole.warning_colour);
                        BasicConsole.WriteLine("Warning: Heap about to try to re-enter spin lock...");
                        BasicConsole.Write("Enter lock caller: ");
                        BasicConsole.WriteLine(caller);
                        BasicConsole.SetTextColour(BasicConsole.default_colour);
                    }

                    AccessLock.Enter();
                }
            }
            //else
            //{
            //    BasicConsole.WriteLine("HeapAccessLock not initialised - ignoring lock conditions.");
            //    BasicConsole.DelayOutput(5);
            //}
        }
Beispiel #3
0
 /// <summary>
 /// Outputs, via the basic console, how much memory was cleaned up.
 /// </summary>
 /// <param name="startNumObjs">The number of objects before the cleanup.</param>
 /// <param name="startNumStrings">The number of strings before the cleanup.</param>
 private static void PrintCleanupData(int startNumObjs, int startNumStrings)
 {
     int numObjsFreed = startNumObjs - NumObjs;
     int numStringsFreed = startNumStrings - NumStrings;
     BasicConsole.SetTextColour(BasicConsole.warning_colour);
     BasicConsole.WriteLine(((FOS_System.String)"Freed objects: ") + numObjsFreed);
     BasicConsole.WriteLine(((FOS_System.String)"Freed strings: ") + numStringsFreed);
     BasicConsole.WriteLine(((FOS_System.String)"Used memory  : ") + (Heap.FBlock->used * Heap.FBlock->bsize) + " / " + Heap.FBlock->size);
     BasicConsole.DelayOutput(2);
     BasicConsole.SetTextColour(BasicConsole.default_colour);
 }
Beispiel #4
0
        public static void UpdateCurrentState()
        {
#if SCHEDULER_HANDLER_TRACE
            BasicConsole.SetTextColour(BasicConsole.warning_colour);

            if (Processes.ProcessManager.Processes.Count > 1)
            {
                BasicConsole.WriteLine("Scheduler interrupt started...");
            }
#endif

            if (ProcessManager.CurrentProcess == null ||
                ProcessManager.CurrentThread == null ||
                ProcessManager.CurrentThread_State == null)
            {
                return;
            }

            UpdateInactiveThreads();

            UpdateActiveThreads();

            while (ActiveQueue.Count == 0)
            {
                //#if SCHEDULER_HANDLER_TRACE
                BasicConsole.WriteLine("WARNING: Scheduler preventing infinite loop by early-updating sleeping threads.");
                //#endif
                UpdateInactiveThreads();
            }

            Thread nextThread = (Thread)ActiveQueue.PeekMin();
#if SCHEDULER_HANDLER_TRACE || SCHEDULER_HANDLER_MIN_TRACE
            BasicConsole.Write("Active: ");
            BasicConsole.Write(nextThread.Owner.Name);
            BasicConsole.Write(" - ");
            BasicConsole.WriteLine(nextThread.Name);
#endif
            ProcessManager.SwitchProcess(nextThread.Owner.Id, (int)nextThread.Id);

            if (!ProcessManager.CurrentThread_State->Started)
            {
                SetupThreadForStart();
            }

#if SCHEDULER_HANDLER_TRACE
            if (Processes.ProcessManager.Processes.Count > 1)
            {
                BasicConsole.WriteLine("Scheduler interrupt ended.");
            }

            BasicConsole.SetTextColour(BasicConsole.default_colour);
#endif
        }
Beispiel #5
0
        public static void Main()
        {
            while (!Terminate)
            {
                //bool reenable = Hardware.Processes.Scheduler.Enabled;
                try
                {
                    //if (reenable)
                    //{
                    //    Hardware.Processes.Scheduler.Disable();
                    //}

#if GCTASK_TRACE
                    BasicConsole.SetTextColour(BasicConsole.warning_colour);
                    BasicConsole.Write("GC cleaning...");
                    BasicConsole.WriteLine(Hardware.Processes.ProcessManager.CurrentProcess.Name);
                    BasicConsole.SetTextColour(BasicConsole.default_colour);
#endif

                    FOS_System.GC.Cleanup();

#if GCTASK_TRACE
                    BasicConsole.SetTextColour(BasicConsole.warning_colour);
                    BasicConsole.WriteLine("GC stopped (1).");
                    BasicConsole.SetTextColour(BasicConsole.default_colour);
#endif
                }
                catch
                {
#if GCTASK_TRACE
                    BasicConsole.SetTextColour(BasicConsole.error_colour);
                    BasicConsole.WriteLine("GC error.");
                    BasicConsole.SetTextColour(BasicConsole.default_colour);
#endif
                }

#if GCTASK_TRACE
                BasicConsole.SetTextColour(BasicConsole.warning_colour);
                BasicConsole.WriteLine("GC stopped (2).");
                BasicConsole.SetTextColour(BasicConsole.default_colour);
#endif
                //if (reenable)
                //{
                //    Hardware.Processes.Scheduler.Enable();
                //}
                Processes.SystemCalls.SleepThread(1000);
            }
        }
Beispiel #6
0
        public static void _IncrementRefCount(byte* objPtr)
        {
            if ((uint)objPtr < (uint)sizeof(GCHeader))
            {
                BasicConsole.SetTextColour(BasicConsole.error_colour);
                BasicConsole.WriteLine("Error! GC can't increment ref count of an object in low memory.");

                uint* basePtr = (uint*)ExceptionMethods.BasePointer;
                // Go up the linked-list of stack frames to (hopefully) the outermost caller
                basePtr = (uint*)*(basePtr);    // Frame of IncrementRefCount(x)
                uint retAddr = *(basePtr + 1);  // Caller of IncrementRefCount(x)
                basePtr = (uint*)*(basePtr);    // Frame of caller of IncrementRefCount(x)
                uint ret2Addr = *(basePtr + 1); // Caller of caller of IncrementRefCount(x)
                uint objAddr = (uint)objPtr;
                String msgStr = "Caller: 0x        , Object: 0x        , PCaller: 0x        ";
                // Object: 37
                // Caller: 17
                // PCaller: 58
                ExceptionMethods.FillString(retAddr, 17, msgStr);
                ExceptionMethods.FillString(objAddr, 37, msgStr);
                ExceptionMethods.FillString(ret2Addr, 58, msgStr);
                BasicConsole.WriteLine(msgStr);

                BasicConsole.DelayOutput(5);
                BasicConsole.SetTextColour(BasicConsole.default_colour);
            }
            
            objPtr -= sizeof(GCHeader);
            GCHeader* gcHeaderPtr = (GCHeader*)objPtr;
            if (CheckSignature(gcHeaderPtr))
            {
                gcHeaderPtr->RefCount++;

                if (gcHeaderPtr->RefCount > 0)
                {
                    RemoveObjectToCleanup(gcHeaderPtr);
                }
            }
        }
Beispiel #7
0
        public static void Main()
        {
            OwnerThread = ProcessManager.CurrentThread;

            Kernel.Processes.SystemCalls.SleepThread(Kernel.Processes.SystemCalls.IndefiniteSleepThread);

            while (!Terminate)
            {
                Awake = false;

                DeviceManager.UpdateDevices();

                if (!Awake)
                {
                    if (Kernel.Processes.SystemCalls.SleepThread(Kernel.Processes.SystemCalls.IndefiniteSleepThread) != Kernel.Processes.SystemCallResults.OK)
                    {
                        BasicConsole.SetTextColour(BasicConsole.error_colour);
                        BasicConsole.WriteLine("Failed to sleep device manager thread!");
                        BasicConsole.SetTextColour(BasicConsole.default_colour);
                    }
                }
            }
        }
Beispiel #8
0
        public static void _IncrementRefCount(byte *objPtr)
        {
            if ((uint)objPtr < (uint)sizeof(GCHeader))
            {
                BasicConsole.SetTextColour(BasicConsole.error_colour);
                BasicConsole.WriteLine("Error! GC can't increment ref count of an object in low memory.");
                BasicConsole.DelayOutput(5);
                BasicConsole.SetTextColour(BasicConsole.default_colour);
            }

            objPtr -= sizeof(GCHeader);
            GCHeader *gcHeaderPtr = (GCHeader *)objPtr;

            if (CheckSignature(gcHeaderPtr))
            {
                gcHeaderPtr->RefCount++;

                if (gcHeaderPtr->RefCount > 0)
                {
                    RemoveObjectToCleanup(gcHeaderPtr);
                }
            }
        }
Beispiel #9
0
        public static void Main()
        {
            OwnerThread = ProcessManager.CurrentThread;

            Thread.Sleep_Indefinitely();

            while (!Terminate)
            {
                Awake = false;

                DeviceManager.UpdateDevices();

                if (!Awake)
                {
                    if (!Thread.Sleep_Indefinitely())
                    {
                        BasicConsole.SetTextColour(BasicConsole.error_colour);
                        BasicConsole.WriteLine("Failed to sleep device manager thread!");
                        BasicConsole.SetTextColour(BasicConsole.default_colour);
                    }
                }
            }
        }
Beispiel #10
0
        public static void *Alloc(UInt32 size, UInt32 boundary, FOS_System.String caller)
        {
#if HEAP_TRACE
            if (OutputTrace)
            {
                BasicConsole.SetTextColour(BasicConsole.warning_colour);
                BasicConsole.WriteLine("Attempt to alloc mem....");
                BasicConsole.SetTextColour(BasicConsole.default_colour);
            }
#endif

            if (PreventAllocation)
            {
                bool BCPOEnabled = BasicConsole.PrimaryOutputEnabled;
                BasicConsole.PrimaryOutputEnabled = true;
                BasicConsole.SetTextColour(BasicConsole.error_colour);
                BasicConsole.Write("Allocation of memory prevented! Reason: ");
                BasicConsole.WriteLine(PreventReason);
                BasicConsole.Write("    > Caller: ");
                BasicConsole.WriteLine(caller);
                BasicConsole.DelayOutput(5);
                BasicConsole.SetTextColour(BasicConsole.default_colour);
                BasicConsole.PrimaryOutputEnabled = BCPOEnabled;
                return(null);
            }

            EnterCritical("Alloc");

            HeapBlock *b;
            byte *     bm;
            UInt32     bcnt;
            UInt32     x, y, z;
            UInt32     bneed;
            byte       nid;

            if (boundary > 1)
            {
                size += (boundary - 1);
            }

            /* iterate blocks */
            for (b = fblock; (UInt32)b != 0; b = b->next)
            {
                /* check if block has enough room */
                if (b->size - (b->used * b->bsize) >= size)
                {
                    bcnt  = b->size / b->bsize;
                    bneed = (size / b->bsize) * b->bsize < size ? size / b->bsize + 1 : size / b->bsize;
                    bm    = (byte *)&b[1];

                    for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x)
                    {
                        /* just wrap around */
                        if (x >= bcnt)
                        {
                            x = 0;
                        }

                        if (bm[x] == 0)
                        {
                            /* count free blocks */
                            for (y = 0; bm[x + y] == 0 && y < bneed && (x + y) < bcnt; ++y)
                            {
                                ;
                            }

                            /* we have enough, now allocate them */
                            if (y == bneed)
                            {
                                /* find ID that does not match left or right */
                                nid = GetNID(bm[x - 1], bm[x + y]);

                                /* allocate by setting id */
                                for (z = 0; z < y; ++z)
                                {
                                    bm[x + z] = nid;
                                }

                                /* optimization */
                                b->lfb = (x + bneed) - 2;

                                /* count used blocks NOT bytes */
                                b->used += y;

                                void *result = (void *)(x * b->bsize + (UInt32)(&b[1]));
                                if (boundary > 1)
                                {
                                    result = (void *)((((UInt32)result) + (boundary - 1)) & ~(boundary - 1));

//#if HEAP_TRACE
//                                    ExitCritical();
//                                    BasicConsole.WriteLine(((FOS_System.String)"Allocated address ") + (uint)result + " on boundary " + boundary + " for " + caller);
//                                    EnterCritical("Alloc:Boundary condition");
//#endif
                                }

                                ExitCritical();
                                return(result);
                            }

                            /* x will be incremented by one ONCE more in our FOR loop */
                            x += (y - 1);
                            continue;
                        }
                    }
                }
            }

            {
                bool BCPOEnabled = BasicConsole.PrimaryOutputEnabled;
                BasicConsole.PrimaryOutputEnabled = true;
                BasicConsole.Write("Heap (");
                BasicConsole.Write(name);
                BasicConsole.WriteLine(") out of memory!");
                if (fblock == null)
                {
                    BasicConsole.WriteLine(" !! fblock == null");
                }
                else if (GetTotalFreeMem() < size)
                {
                    if (GetTotalMem() == 0)
                    {
                        BasicConsole.WriteLine(" !! Out of free mem because total mem is zero.");
                    }
                    else if (GetTotalMem() <= 8092)
                    {
                        BasicConsole.WriteLine(" !! Out of free mem because total mem is <= 8092 bytes.");
                    }
                    else
                    {
                        BasicConsole.WriteLine(" !! Genuinely out of memory.");
                    }
                }
                BasicConsole.PrimaryOutputEnabled = BCPOEnabled;
            }

            ExitCritical();

            return(null);
        }
Beispiel #11
0
        public static void Test_qTDWrapper()
        {
            FOS_System.String testName = "Queue Transfer Descrip";
            DBGMSG(testName, "START");

            errors   = 0;
            warnings = 0;

            EHCI_qTD qTD = new EHCI_qTD();

            try
            {
                byte *pqTD = (byte *)qTD.qtd;

                //Verifications done via two methods:
                //  1. Check value from pointer & manual shifting to confirm set properly
                //  2. Check value from "get" method to confirm reading properly
                //  3. For boolean types, also test & verify setting to false!

                qTD.AlternateNextqTDPointerTerminate = true;
                if ((pqTD[0x04u] & 0x01u) == 0)
                {
                    DBGERR(testName, "AlternateNextqTDPointerTerminate - Failed to set to true.");
                }
                else
                {
                    if (!qTD.AlternateNextqTDPointerTerminate)
                    {
                        DBGERR(testName, "AlternateNextqTDPointerTerminate - Failed to read as true.");
                    }
                    else
                    {
                        pqTD[0x04u] = 0xFF;
                        qTD.AlternateNextqTDPointerTerminate = false;
                        if ((pqTD[0x04u] & 0x1u) != 0)
                        {
                            DBGERR(testName, "AlternateNextqTDPointerTerminate - Failed to set to false.");
                        }
                        else
                        {
                            if (qTD.AlternateNextqTDPointerTerminate)
                            {
                                DBGERR(testName, "AlternateNextqTDPointerTerminate - Failed to read as false.");
                            }
                        }
                    }
                }

                qTD.Buffer0 = (byte *)0xDEADBEEFu;
                //Read back should be 0xDEADB000
                if ((pqTD[0x0Du] & 0xF0u) != 0xB0u ||
                    pqTD[0x0Eu] != 0xADu ||
                    pqTD[0x0Fu] != 0xDEu)
                {
                    DBGERR(testName, "Buffer0 - Failed to set.");
                }
                else
                {
                    if ((uint)qTD.Buffer0 != 0xDEADB000u)
                    {
                        DBGERR(testName, "Buffer0 - Failed to read.");
                    }
                }

                qTD.Buffer1 = (byte *)0x12345678u;
                //Read back should be 0x12345000
                if ((pqTD[0x11u] & 0xF0u) != 0x50u ||
                    pqTD[0x12u] != 0x34u ||
                    pqTD[0x13u] != 0x12u)
                {
                    DBGERR(testName, "Buffer1 - Failed to set.");
                }
                else
                {
                    if ((uint)qTD.Buffer1 != 0x12345000u)
                    {
                        DBGERR(testName, "Buffer1 - Failed to read.");
                    }
                }

                qTD.Buffer2 = (byte *)0xFEEDBEA5u;
                //Read back should be 0xFEEDB000
                if ((pqTD[0x15u] & 0xF0u) != 0xB0u ||
                    pqTD[0x16u] != 0xEDu ||
                    pqTD[0x17u] != 0xFEu)
                {
                    DBGERR(testName, "Buffer2 - Failed to set.");
                }
                else
                {
                    if ((uint)qTD.Buffer2 != 0xFEEDB000u)
                    {
                        DBGERR(testName, "Buffer2 - Failed to read.");
                    }
                }

                qTD.Buffer3 = (byte *)0x09876543u;
                //Read back should be 0x09876000
                if ((pqTD[0x19u] & 0xF0u) != 0x60u ||
                    pqTD[0x1Au] != 0x87u ||
                    pqTD[0x1Bu] != 0x09u)
                {
                    DBGERR(testName, "Buffer3 - Failed to set.");
                }
                else
                {
                    if ((uint)qTD.Buffer3 != 0x09876000u)
                    {
                        DBGERR(testName, "Buffer3 - Failed to read.");
                    }
                }

                qTD.Buffer4 = (byte *)0x24681357u;
                //Read back should be 0x24681000
                if ((pqTD[0x1Du] & 0xF0u) != 0x10u ||
                    pqTD[0x1Eu] != 0x68u ||
                    pqTD[0x1Fu] != 0x24u)
                {
                    DBGERR(testName, "Buffer4 - Failed to set.");
                }
                else
                {
                    if ((uint)qTD.Buffer4 != 0x24681000u)
                    {
                        DBGERR(testName, "Buffer4 - Failed to read.");
                    }
                }

                qTD.CurrentPage = 0xFF;
                //Shift!
                if ((pqTD[0x09u] & 0x70u) != 0x70u)
                {
                    DBGERR(testName, "CurrentPage - Failed to set.");
                }
                else
                {
                    if ((uint)qTD.CurrentPage != 0x07u)
                    {
                        DBGERR(testName, "CurrentPage - Failed to read.");
                    }
                }

                qTD.DataToggle = true;
                if ((pqTD[0x0Bu] & 0x80u) == 0)
                {
                    DBGERR(testName, "DataToggle - Failed to set to true.");
                }
                else
                {
                    if (!qTD.DataToggle)
                    {
                        DBGERR(testName, "DataToggle - Failed to read as true.");
                    }
                    else
                    {
                        pqTD[0x0Bu]    = 0xFF;
                        qTD.DataToggle = false;
                        if ((pqTD[0x0Bu] & 0x80u) != 0)
                        {
                            DBGERR(testName, "DataToggle - Failed to set to false.");
                        }
                        else
                        {
                            if (qTD.DataToggle)
                            {
                                DBGERR(testName, "DataToggle - Failed to read as false.");
                            }
                        }
                    }
                }

                qTD.ErrorCounter = 0xFF;
                //Shift!
                if ((pqTD[0x09u] & 0x0Cu) != 0x0Cu)
                {
                    DBGERR(testName, "ErrorCounter - Failed to set.");
                }
                else
                {
                    if ((uint)qTD.ErrorCounter != 0x03u)
                    {
                        DBGERR(testName, "ErrorCounter - Failed to read.");
                    }
                }

                qTD.InterruptOnComplete = true;
                if ((pqTD[0x09u] & 0x80u) == 0)
                {
                    DBGERR(testName, "InterruptOnComplete - Failed to set to true.");
                }
                else
                {
                    if (!qTD.InterruptOnComplete)
                    {
                        DBGERR(testName, "InterruptOnComplete - Failed to read as true.");
                    }
                    else
                    {
                        pqTD[0x09u]             = 0xFF;
                        qTD.InterruptOnComplete = false;
                        if ((pqTD[0x09u] & 0x80u) != 0)
                        {
                            DBGERR(testName, "InterruptOnComplete - Failed to set to false.");
                        }
                        else
                        {
                            if (qTD.InterruptOnComplete)
                            {
                                DBGERR(testName, "InterruptOnComplete - Failed to read as false.");
                            }
                        }
                    }
                }

                qTD.NextqTDPointer = (EHCI_qTD_Struct *)0xF2610369;
                //Read back should be 0xF2610360
                if ((pqTD[0x00u] & 0xE0u) != 0x60u ||
                    pqTD[0x01u] != 0x03u ||
                    pqTD[0x02u] != 0x61u ||
                    pqTD[0x03u] != 0xF2u)
                {
                    DBGERR(testName, "NextqTDPointer - Failed to set.");
                }
                else
                {
                    if ((uint)qTD.NextqTDPointer != 0xF2610360u)
                    {
                        DBGERR(testName, "NextqTDPointer - Failed to read.");
                    }
                }

                qTD.NextqTDPointerTerminate = true;
                if ((pqTD[0x00u] & 0x01u) == 0)
                {
                    DBGERR(testName, "NextqTDPointerTerminate - Failed to set to true.");
                }
                else
                {
                    if (!qTD.NextqTDPointerTerminate)
                    {
                        DBGERR(testName, "NextqTDPointerTerminate - Failed to read as true.");
                    }
                    else
                    {
                        pqTD[0x00u] = 0xFF;
                        qTD.NextqTDPointerTerminate = false;
                        if ((pqTD[0x00u] & 0x01u) != 0)
                        {
                            DBGERR(testName, "NextqTDPointerTerminate - Failed to set to false.");
                        }
                        else
                        {
                            if (qTD.NextqTDPointerTerminate)
                            {
                                DBGERR(testName, "NextqTDPointerTerminate - Failed to read as false.");
                            }
                        }
                    }
                }

                qTD.PIDCode = 0xFF;
                //Shift!
                if ((pqTD[0x09u] & 0x03u) != 0x03u)
                {
                    DBGERR(testName, "PIDCode - Failed to set.");
                }
                else
                {
                    if ((uint)qTD.PIDCode != 0x03u)
                    {
                        DBGERR(testName, "PIDCode - Failed to read.");
                    }
                }

                qTD.Status = 0xFF;
                //Shift!
                if (pqTD[0x08u] != 0xFFu)
                {
                    DBGERR(testName, "Status - Failed to set.");
                }
                else
                {
                    if ((uint)qTD.Status != 0xFFu)
                    {
                        DBGERR(testName, "Status - Failed to read.");
                    }
                }

                qTD.TotalBytesToTransfer = 0xFFFF;
                //Read back should be 0x7FFF
                if (pqTD[0x0Au] != 0xFFu ||
                    (pqTD[0x0Bu] & 0x7Fu) != 0x7Fu)
                {
                    DBGERR(testName, "TotalBytesToTransfer - Failed to set.");
                }
                else
                {
                    if ((uint)qTD.TotalBytesToTransfer != 0x7FFFu)
                    {
                        DBGERR(testName, "TotalBytesToTransfer - Failed to read.");
                    }
                }
            }
            catch
            {
                errors++;
                BasicConsole.SetTextColour(BasicConsole.warning_colour);
                BasicConsole.WriteLine(ExceptionMethods.CurrentException.Message);
                BasicConsole.SetTextColour(BasicConsole.default_colour);
            }
            finally
            {
                qTD.Free();
            }

            if (errors > 0)
            {
                DBGERR(testName, ((FOS_System.String) "Test failed! Errors: ") + errors + " Warnings: " + warnings);
            }
            else
            {
                if (warnings > 0)
                {
                    DBGWRN(testName, ((FOS_System.String) "Test passed with warnings: ") + warnings);
                }
                else
                {
                    DBGMSG(testName, "Test passed.");
                }
            }

            DBGMSG(testName, "END");

            BasicConsole.DelayOutput(1);
        }
Beispiel #12
0
        public static void Test_QueueHeadWrapper()
        {
            FOS_System.String testName = "Queue Head Wrapper";
            DBGMSG(testName, "START");

            errors   = 0;
            warnings = 0;

            EHCI_QueueHead qh = new EHCI_QueueHead();

            try
            {
                byte *pQH = (byte *)qh.queueHead;

                //Verifications done via two methods:
                //  1. Check value from pointer & manual shifting to confirm set properly
                //  2. Check value from "get" method to confirm reading properly
                //  3. For boolean types, also test & verify setting to false!

                qh.Active = true;
                if ((pQH[0x18u] & 0x80u) == 0)
                {
                    DBGERR(testName, "Active - Failed to set to true.");
                }
                else
                {
                    if (!qh.Active)
                    {
                        DBGERR(testName, "Active - Failed to read as true.");
                    }
                    else
                    {
                        pQH[0x18u] = 0xFF;
                        qh.Active  = false;
                        if ((pQH[0x18u] & 0x80u) != 0)
                        {
                            DBGERR(testName, "Active - Failed to set to false.");
                        }
                        else
                        {
                            if (qh.Active)
                            {
                                DBGERR(testName, "Active - Failed to read as false.");
                            }
                        }
                    }
                }

                qh.ControlEndpointFlag = true;
                if ((pQH[0x07u] & 0x08u) == 0)
                {
                    DBGERR(testName, "ControlEndpointFlag - Failed to set to true.");
                }
                else
                {
                    if (!qh.ControlEndpointFlag)
                    {
                        DBGERR(testName, "ControlEndpointFlag - Failed to read as true.");
                    }
                    else
                    {
                        pQH[0x07u]             = 0xFF;
                        qh.ControlEndpointFlag = false;
                        if ((pQH[0x07u] & 0x08u) != 0)
                        {
                            DBGERR(testName, "ControlEndpointFlag - Failed to set to false.");
                        }
                        else
                        {
                            if (qh.ControlEndpointFlag)
                            {
                                DBGERR(testName, "ControlEndpointFlag - Failed to read as false.");
                            }
                        }
                    }
                }

                qh.CurrentqTDPointer = (EHCI_qTD_Struct *)0xDEADBEFFu;
                //- Read back should equal 0xDEADBEE0
                if ((pQH[0x0Cu] & 0xF0u) != 0xF0u ||
                    pQH[0x0Du] != 0xBEu ||
                    pQH[0x0Eu] != 0xADu ||
                    pQH[0x0Fu] != 0xDEu)
                {
                    DBGERR(testName, "CurrentqTDPointer - Failed to set.");
                }
                else
                {
                    if ((uint)qh.CurrentqTDPointer != 0xDEADBEF0u)
                    {
                        DBGERR(testName, "CurrentqTDPointer - Failed to read.");
                    }
                }

                qh.DataToggleControl = true;
                if ((pQH[0x05u] & 0x40u) == 0)
                {
                    DBGERR(testName, "DataToggleControl - Failed to set to true.");
                }
                else
                {
                    if (!qh.DataToggleControl)
                    {
                        DBGERR(testName, "DataToggleControl - Failed to read as true.");
                    }
                    else
                    {
                        pQH[0x05u]           = 0xFF;
                        qh.DataToggleControl = false;
                        if ((pQH[0x05u] & 0x40u) != 0)
                        {
                            DBGERR(testName, "DataToggleControl - Failed to set to false.");
                        }
                        else
                        {
                            if (qh.DataToggleControl)
                            {
                                DBGERR(testName, "DataToggleControl - Failed to read as false.");
                            }
                        }
                    }
                }

                qh.DeviceAddress = 0xDE;
                if ((pQH[0x04u] & 0x7Fu) != 0x5Eu)
                {
                    DBGERR(testName, "DeviceAddress - Failed to set.");
                }
                else
                {
                    if ((uint)qh.DeviceAddress != 0x5Eu)
                    {
                        DBGERR(testName, "DeviceAddress - Failed to read.");
                    }
                }

                qh.EndpointNumber = 0xBF;
                //Shift!
                if ((pQH[0x05u] & 0x0Fu) != 0x0Fu)
                {
                    DBGERR(testName, "EndpointNumber - Failed to set.");
                }
                else
                {
                    if ((uint)qh.EndpointNumber != 0x0Fu)
                    {
                        DBGERR(testName, "EndpointNumber - Failed to read.");
                    }
                }

                qh.EndpointSpeed = 0xB3;
                //Shift!
                if ((pQH[0x05u] & 0x30u) != 0x30u)
                {
                    DBGERR(testName, "EndpointSpeed - Failed to set.");
                }
                else
                {
                    if ((uint)qh.EndpointSpeed != 0x03u)
                    {
                        DBGERR(testName, "EndpointSpeed - Failed to read.");
                    }
                }

                qh.HeadOfReclamationList = true;
                if ((pQH[0x05u] & 0x80u) == 0)
                {
                    DBGERR(testName, "HeadOfReclamationList - Failed to set to true.");
                }
                else
                {
                    if (!qh.HeadOfReclamationList)
                    {
                        DBGERR(testName, "HeadOfReclamationList - Failed to read as true.");
                    }
                    else
                    {
                        pQH[0x05u] = 0xFF;
                        qh.HeadOfReclamationList = false;
                        if ((pQH[0x05u] & 0x80u) != 0)
                        {
                            DBGERR(testName, "HeadOfReclamationList - Failed to set to false.");
                        }
                        else
                        {
                            if (qh.HeadOfReclamationList)
                            {
                                DBGERR(testName, "HeadOfReclamationList - Failed to read as false.");
                            }
                        }
                    }
                }

                qh.HighBandwidthPipeMultiplier = 0xDF;
                //Shift!
                if ((pQH[0x0Bu] & 0xC0u) != 0xC0u)
                {
                    DBGERR(testName, "HighBandwidthPipeMultiplier - Failed to set.");
                }
                else
                {
                    if ((uint)qh.HighBandwidthPipeMultiplier != 0x03u)
                    {
                        DBGERR(testName, "HighBandwidthPipeMultiplier - Failed to read.");
                    }
                }

                qh.HorizontalLinkPointer = (EHCI_QueueHead_Struct *)0xDEADBEFE;
                if ((pQH[0x00u] & 0xE0u) != 0xE0u ||
                    pQH[0x01u] != 0xBEu ||
                    pQH[0x02u] != 0xADu ||
                    pQH[0x03u] != 0xDEu)
                {
                    DBGERR(testName, "HorizontalLinkPointer - Failed to set.");
                }
                else
                {
                    if ((uint)qh.HorizontalLinkPointer != 0xDEADBEE0u)
                    {
                        DBGERR(testName, "HorizontalLinkPointer - Failed to read.");
                    }
                }

                qh.HubAddr = 0xBE;
                //Shift!
                if ((pQH[0x0Au] & 0x7Fu) != 0x3Eu)
                {
                    DBGERR(testName, "HubAddr - Failed to set.");
                }
                else
                {
                    if ((uint)qh.HubAddr != 0x3Eu)
                    {
                        DBGERR(testName, "HubAddr - Failed to read.");
                    }
                }

                qh.InactiveOnNextTransaction = true;
                if ((pQH[0x04u] & 0x80u) == 0)
                {
                    DBGERR(testName, "InactiveOnNextTransaction - Failed to set to true.");
                }
                else
                {
                    if (!qh.InactiveOnNextTransaction)
                    {
                        DBGERR(testName, "InactiveOnNextTransaction - Failed to read as true.");
                    }
                    else
                    {
                        pQH[0x04u] = 0xFF;
                        qh.InactiveOnNextTransaction = false;
                        if ((pQH[0x04u] & 0x80u) != 0)
                        {
                            DBGERR(testName, "InactiveOnNextTransaction - Failed to set to false.");
                        }
                        else
                        {
                            if (qh.InactiveOnNextTransaction)
                            {
                                DBGERR(testName, "InactiveOnNextTransaction - Failed to read as false.");
                            }
                        }
                    }
                }

                qh.InterruptScheduleMask = 0xFE;
                //Shift!
                if (pQH[0x08u] != 0xFEu)
                {
                    DBGERR(testName, "InterruptScheduleMask - Failed to set.");
                }
                else
                {
                    if ((uint)qh.InterruptScheduleMask != 0xFEu)
                    {
                        DBGERR(testName, "InterruptScheduleMask - Failed to read.");
                    }
                }

                qh.MaximumPacketLength = 0xDEAD;
                //Shift!
                if (pQH[0x06u] != 0xADu ||
                    (pQH[0x07u] & 0x07u) != 0x06u)
                {
                    DBGERR(testName, "MaximumPacketLength - Failed to set.");
                }
                else
                {
                    if ((uint)qh.MaximumPacketLength != 0x06ADu)
                    {
                        DBGERR(testName, "MaximumPacketLength - Failed to read.");
                    }
                }

                qh.NakCountReload = 0xFF;
                //Shift!
                if ((pQH[0x07u] & 0xF0u) != 0xF0u)
                {
                    DBGERR(testName, "NakCountReload - Failed to set.");
                }
                else
                {
                    if ((uint)qh.NakCountReload != 0x0Fu)
                    {
                        DBGERR(testName, "NakCountReload - Failed to read.");
                    }
                }

                qh.NextqTDPointer = (EHCI_qTD_Struct *)0xDEADBEFF;
                //- Read back should equal 0xDEADBEE0
                if ((pQH[0x10u] & 0xF0u) != 0xF0u ||
                    pQH[0x11u] != 0xBEu ||
                    pQH[0x12u] != 0xADu ||
                    pQH[0x13u] != 0xDEu)
                {
                    DBGERR(testName, "NextqTDPointer - Failed to set.");
                }
                else
                {
                    if ((uint)qh.NextqTDPointer != 0xDEADBEF0u)
                    {
                        DBGERR(testName, "NextqTDPointer - Failed to read.");
                    }
                }

                qh.NextqTDPointerTerminate = true;
                if ((pQH[0x10u] & 0x01u) == 0)
                {
                    DBGERR(testName, "NextqTDPointerTerminate - Failed to set to true.");
                }
                else
                {
                    if (!qh.NextqTDPointerTerminate)
                    {
                        DBGERR(testName, "NextqTDPointerTerminate - Failed to read as true.");
                    }
                    else
                    {
                        pQH[0x10u] = 0xFF;
                        qh.NextqTDPointerTerminate = false;
                        if ((pQH[0x10u] & 0x01u) != 0)
                        {
                            DBGERR(testName, "NextqTDPointerTerminate - Failed to set to false.");
                        }
                        else
                        {
                            if (qh.NextqTDPointerTerminate)
                            {
                                DBGERR(testName, "NextqTDPointerTerminate - Failed to read as false.");
                            }
                        }
                    }
                }

                qh.PortNumber = 0xFF;
                //Shift!
                if ((pQH[0x0Au] & 0x80u) != 0x80u ||
                    (pQH[0x0Bu] & 0x3Fu) != 0x3Fu)
                {
                    DBGERR(testName, "PortNumber - Failed to set.");
                }
                else
                {
                    if ((uint)qh.PortNumber != 0x7Fu)
                    {
                        DBGERR(testName, "PortNumber - Failed to read.");
                    }
                }

                qh.SplitCompletionMask = 0xBE;
                //Shift!
                if (pQH[0x09u] != 0xBEu)
                {
                    DBGERR(testName, "SplitCompletionMask - Failed to set.");
                }
                else
                {
                    if ((uint)qh.SplitCompletionMask != 0xBEu)
                    {
                        DBGERR(testName, "SplitCompletionMask - Failed to read.");
                    }
                }

                qh.Terminate = true;
                if ((pQH[0x00u] & 0x01u) == 0)
                {
                    DBGERR(testName, "Terminate - Failed to set to true.");
                }
                else
                {
                    if (!qh.Terminate)
                    {
                        DBGERR(testName, "Terminate - Failed to read as true.");
                    }
                    else
                    {
                        pQH[0x00u]   = 0xFF;
                        qh.Terminate = false;
                        if ((pQH[0x00u] & 0x01u) != 0)
                        {
                            DBGERR(testName, "Terminate - Failed to set to false.");
                        }
                        else
                        {
                            if (qh.Terminate)
                            {
                                DBGERR(testName, "Terminate - Failed to read as false.");
                            }
                        }
                    }
                }

                qh.Type = 0xFF;
                //Shift!
                if ((pQH[0x00u] & 0x06u) != 0x06u)
                {
                    DBGERR(testName, "Type - Failed to set.");
                }
                else
                {
                    if ((uint)qh.Type != 0x03u)
                    {
                        DBGERR(testName, "Type - Failed to read.");
                    }
                }
            }
            catch
            {
                errors++;
                BasicConsole.SetTextColour(BasicConsole.warning_colour);
                BasicConsole.WriteLine(ExceptionMethods.CurrentException.Message);
                BasicConsole.SetTextColour(BasicConsole.default_colour);
            }
            finally
            {
                qh.Free();
            }

            if (errors > 0)
            {
                DBGERR(testName, ((FOS_System.String) "Test failed! Errors: ") + errors + " Warnings: " + warnings);
            }
            else
            {
                if (warnings > 0)
                {
                    DBGWRN(testName, ((FOS_System.String) "Test passed with warnings: ") + warnings);
                }
                else
                {
                    DBGMSG(testName, "Test passed.");
                }
            }

            DBGMSG(testName, "END");

            BasicConsole.DelayOutput(1);
        }
Beispiel #13
0
        public static void Test_PointerManipulation()
        {
            FOS_System.String testName = "Ptr Manipulation";
            DBGMSG(testName, "START");

            errors   = 0;
            warnings = 0;

            byte *rootPtr = (byte *)FOS_System.Heap.Alloc(4096, 32);

            try
            {
                DBGMSG(testName, ((FOS_System.String) "rootPtr: ") + (uint)rootPtr);

                if (!Validate_PointerBoundaryAlignment(rootPtr, 32))
                {
                    DBGERR(testName, "Pointer not aligned on boundary correctly!");
                }

                byte *retPtr = ReturnPointer(rootPtr);
                if (retPtr != rootPtr)
                {
                    DBGERR(testName, "Passing and returning pointer via method failed.");
                }

                byte *shiftedPtr = (byte *)((uint *)rootPtr + 1);
                if (((uint)shiftedPtr) != ((uint)rootPtr) + 4)
                {
                    DBGERR(testName, ((FOS_System.String) "Shifted pointer not shifted correctly! shiftedPtr: ") + (uint)shiftedPtr);
                }

                EHCITestingObject testObj = new EHCITestingObject();
                testObj.ptr1 = rootPtr;
                testObj.ptr2 = (uint *)rootPtr;
                testObj.ptr3 = (ulong *)rootPtr;
                testObj.ptr4 = rootPtr;
                if (testObj.ptr1 != rootPtr)
                {
                    DBGERR(testName, ((FOS_System.String) "Storing test pointer 1 failed! testObj.ptr1: ") + (uint)testObj.ptr1);
                }
                if (testObj.ptr2 != rootPtr)
                {
                    DBGERR(testName, ((FOS_System.String) "Storing test pointer 2 failed! testObj.ptr2: ") + (uint)testObj.ptr2);
                }
                if (testObj.ptr3 != rootPtr)
                {
                    DBGERR(testName, ((FOS_System.String) "Storing test pointer 3 failed! testObj.ptr3: ") + (uint)testObj.ptr3);
                }
                if (testObj.ptr4 != rootPtr)
                {
                    DBGERR(testName, ((FOS_System.String) "Storing test pointer 4 failed! testObj.ptr4: ") + (uint)testObj.ptr4);
                }
            }
            catch
            {
                errors++;
                BasicConsole.SetTextColour(BasicConsole.warning_colour);
                BasicConsole.WriteLine(ExceptionMethods.CurrentException.Message);
                BasicConsole.SetTextColour(BasicConsole.default_colour);
            }
            finally
            {
                FOS_System.Heap.Free(rootPtr);
            }

            if (errors > 0)
            {
                DBGERR(testName, ((FOS_System.String) "Test failed! Errors: ") + errors + " Warnings: " + warnings);
            }
            else
            {
                if (warnings > 0)
                {
                    DBGWRN(testName, ((FOS_System.String) "Test passed with warnings: ") + warnings);
                }
                else
                {
                    DBGMSG(testName, "Test passed.");
                }
            }

            DBGMSG(testName, "END");

            BasicConsole.DelayOutput(1);
        }
Beispiel #14
0
        public static void Test_StructValueSetting()
        {
            FOS_System.String testName = "Strct Value Setting";
            DBGMSG(testName, "START");

            errors   = 0;
            warnings = 0;

            uint structSize = (uint)sizeof(EHCITestingStruct);

            if (structSize != 8 * 4)
            {
                DBGERR(testName, ((FOS_System.String) "Struct size incorrect! structSize: ") + structSize);
            }
            if (errors == 0)
            {
                EHCITestingStruct *rootPtr = (EHCITestingStruct *)FOS_System.Heap.Alloc(structSize);
                byte *bRootPtr             = (byte *)rootPtr;
                try
                {
                    DBGMSG(testName, ((FOS_System.String) "rootPtr: ") + (uint)rootPtr);

                    rootPtr->u1 = 0xDEADBEEF;
                    if (rootPtr->u1 != 0xDEADBEEF ||
                        bRootPtr[0] != 0xEF ||
                        bRootPtr[1] != 0xBE ||
                        bRootPtr[2] != 0xAD ||
                        bRootPtr[3] != 0xDE)
                    {
                        DBGERR(testName, "Getting/setting struct u1 failed!");
                    }


                    rootPtr->u2 = 0x12345678;
                    if (rootPtr->u1 != 0xDEADBEEF ||
                        bRootPtr[0] != 0xEF ||
                        bRootPtr[1] != 0xBE ||
                        bRootPtr[2] != 0xAD ||
                        bRootPtr[3] != 0xDE)
                    {
                        DBGERR(testName, "Getting/setting struct u2 failed! Affected u1 value.");
                    }
                    if (rootPtr->u2 != 0x12345678 ||
                        bRootPtr[4] != 0x78 ||
                        bRootPtr[5] != 0x56 ||
                        bRootPtr[6] != 0x34 ||
                        bRootPtr[7] != 0x12)
                    {
                        DBGERR(testName, "Getting/setting struct u2 failed!");
                    }


                    rootPtr->u3 = 0xBEEFDEAD;
                    if (rootPtr->u1 != 0xDEADBEEF ||
                        bRootPtr[0] != 0xEF ||
                        bRootPtr[1] != 0xBE ||
                        bRootPtr[2] != 0xAD ||
                        bRootPtr[3] != 0xDE)
                    {
                        DBGERR(testName, "Getting/setting struct u3 failed! Affected u1 value.");
                    }
                    if (rootPtr->u2 != 0x12345678 ||
                        bRootPtr[4] != 0x78 ||
                        bRootPtr[5] != 0x56 ||
                        bRootPtr[6] != 0x34 ||
                        bRootPtr[7] != 0x12)
                    {
                        DBGERR(testName, "Getting/setting struct u3 failed! Affected u2 value.");
                    }
                    if (rootPtr->u3 != 0xBEEFDEAD ||
                        bRootPtr[8] != 0xAD ||
                        bRootPtr[9] != 0xDE ||
                        bRootPtr[10] != 0xEF ||
                        bRootPtr[11] != 0xBE)
                    {
                        DBGERR(testName, "Getting/setting struct u3 failed!");
                    }


                    rootPtr->u4 = 0x09876543;
                    if (rootPtr->u1 != 0xDEADBEEF ||
                        bRootPtr[0] != 0xEF ||
                        bRootPtr[1] != 0xBE ||
                        bRootPtr[2] != 0xAD ||
                        bRootPtr[3] != 0xDE)
                    {
                        DBGERR(testName, "Getting/setting struct u4 failed! Affected u1 value.");
                    }
                    if (rootPtr->u2 != 0x12345678 ||
                        bRootPtr[4] != 0x78 ||
                        bRootPtr[5] != 0x56 ||
                        bRootPtr[6] != 0x34 ||
                        bRootPtr[7] != 0x12)
                    {
                        DBGERR(testName, "Getting/setting struct u4 failed! Affected u2 value.");
                    }
                    if (rootPtr->u3 != 0xBEEFDEAD ||
                        bRootPtr[8] != 0xAD ||
                        bRootPtr[9] != 0xDE ||
                        bRootPtr[10] != 0xEF ||
                        bRootPtr[11] != 0xBE)
                    {
                        DBGERR(testName, "Getting/setting struct u4 failed! Affected u3 value.");
                    }
                    if (rootPtr->u4 != 0x09876543 ||
                        bRootPtr[12] != 0x43 ||
                        bRootPtr[13] != 0x65 ||
                        bRootPtr[14] != 0x87 ||
                        bRootPtr[15] != 0x09)
                    {
                        DBGERR(testName, "Getting/setting struct u4 failed!");
                    }

                    Test_StructValueSettingAsArg("Strct Val Set by val", *rootPtr);
                    Test_StructValueSettingAsArg("Strct Val Set by ptr", rootPtr, bRootPtr);
                }
                catch
                {
                    errors++;
                    BasicConsole.SetTextColour(BasicConsole.warning_colour);
                    BasicConsole.WriteLine(ExceptionMethods.CurrentException.Message);
                    BasicConsole.SetTextColour(BasicConsole.default_colour);
                }
                finally
                {
                    FOS_System.Heap.Free(rootPtr);
                }
            }

            if (errors > 0)
            {
                DBGERR(testName, ((FOS_System.String) "Test failed! Errors: ") + errors + " Warnings: " + warnings);
            }
            else
            {
                if (warnings > 0)
                {
                    DBGWRN(testName, ((FOS_System.String) "Test passed with warnings: ") + warnings);
                }
                else
                {
                    DBGMSG(testName, "Test passed.");
                }
            }

            DBGMSG(testName, "END");

            BasicConsole.DelayOutput(1);
        }
Beispiel #15
0
        public static void *NewObj(FOS_System.Type theType)
        {
            if (!Enabled)
            {
                BasicConsole.SetTextColour(BasicConsole.warning_colour);
                BasicConsole.WriteLine("Warning! GC returning null pointer because GC not enabled.");
                BasicConsole.Write("Last disabler: ");
                BasicConsole.WriteLine(lastDisabler);
                BasicConsole.DelayOutput(10);
                BasicConsole.SetTextColour(BasicConsole.default_colour);

                return(null);
            }

#if GC_TRACE
            if (OutputTrace)
            {
                BasicConsole.WriteLine("NewObj");
            }
#endif

            EnterCritical("NewObj");

            try
            {
                InsideGC = true;

                //Alloc space for GC header that prefixes object data
                //Alloc space for new object

                uint totalSize = theType.Size;
                totalSize += (uint)sizeof(GCHeader);

                GCHeader *newObjPtr = (GCHeader *)Heap.AllocZeroed(totalSize, "GC : NewObject");

                if ((UInt32)newObjPtr == 0)
                {
                    InsideGC = false;

                    BasicConsole.SetTextColour(BasicConsole.error_colour);
                    BasicConsole.WriteLine("Error! GC can't create a new object because the heap returned a null pointer.");
                    BasicConsole.DelayOutput(10);
                    BasicConsole.SetTextColour(BasicConsole.default_colour);

                    return(null);
                }

                NumObjs++;

                //Initialise the GCHeader
                SetSignature(newObjPtr);
                newObjPtr->RefCount = 1;
                //Initialise the object _Type field
                FOS_System.ObjectWithType newObj = (FOS_System.ObjectWithType)Utilities.ObjectUtilities.GetObject(newObjPtr + 1);
                newObj._Type = theType;

                //Move past GCHeader
                byte *newObjBytePtr = (byte *)(newObjPtr + 1);

                InsideGC = false;

                return(newObjBytePtr);
            }
            finally
            {
                ExitCritical();
            }
        }
Beispiel #16
0
        public static void Main()
        {
            BasicConsole.WriteLine("Kernel task! ");
            BasicConsole.WriteLine(" > Executing normally...");

            #region Dictionary Test

            /*try
             * {
             *  UInt32Dictionary dic = new UInt32Dictionary();
             *
             *  for (uint i = 0; i < 9; i += 3)
             *  {
             *      BasicConsole.WriteLine("Dictionary test loop");
             *      BasicConsole.WriteLine("--------------------");
             *
             *      uint key1 = 0xC0DEC0DEu + (0x100u * i);
             *      uint key2 = 0xC0DEC0DEu + (0x100u * (i+1));
             *      uint key3 = 0xC0DEC0DEu + (0x100u * (i+2));
             *
             *      uint value1 = 0xDEADBEE0u + (0x1u * i);
             *      uint value2 = 0xDEADBEE0u + (0x1u * (i+1));
             *      uint value3 = 0xDEADBEE0u + (0x1u * (i+2));
             *
             *      dic.Add(key1, value1);
             *      dic.Add(key2, value2);
             *      dic.Add(key3, value3);
             *
             *      for(uint j = 50 * i; j < (50 * (i+1))-20; j++)
             *      {
             *          dic.Add(j, j);
             *      }
             *
             *      if (!dic.Contains(key1))
             *      {
             *          BasicConsole.WriteLine("Dictionary doesn't contain key 1.");
             *      }
             *      else if (dic[key1] != value1)
             *      {
             *          BasicConsole.WriteLine("Dictionary value for key 1 wrong.");
             *      }
             *      else
             *      {
             *          BasicConsole.WriteLine("Okay (1)");
             *      }
             *      if (!dic.Contains(key2))
             *      {
             *          BasicConsole.WriteLine("Dictionary doesn't contain key1");
             *      }
             *      else if (dic[key2] != value2)
             *      {
             *          BasicConsole.WriteLine("Dictionary value for key1 wrong.");
             *      }
             *      else
             *      {
             *          BasicConsole.WriteLine("Okay (2)");
             *      }
             *      if (!dic.Contains(key3))
             *      {
             *          BasicConsole.WriteLine("Dictionary doesn't contain key1");
             *      }
             *      else if (dic[key3] != value3)
             *      {
             *          BasicConsole.WriteLine("Dictionary value for key1 wrong.");
             *      }
             *      else
             *      {
             *          BasicConsole.WriteLine("Okay (3)");
             *      }
             *
             *      dic.Remove(key1);
             *
             *      if (dic.Contains(key1))
             *      {
             *          BasicConsole.WriteLine("Dictionary contains key1!");
             *      }
             *      else
             *      {
             *          BasicConsole.WriteLine("Okay (4)");
             *      }
             *
             *      BasicConsole.WriteLine("Iterating");
             *      UInt32Dictionary.Iterator itr = dic.GetIterator();
             *      while (itr.HasNext())
             *      {
             *          UInt32Dictionary.KeyValuePair pair = itr.Next();
             *          BasicConsole.WriteLine("Pair: key=" + (FOS_System.String)pair.Key + ", value=" + pair.Value);
             *      }
             *
             *      dic.Remove(key2);
             *
             *      for (uint j = (50 * i)+30; j < (50 * (i + 1)); j++)
             *      {
             *          dic.Add(j, j);
             *      }
             *
             *      if (dic.Contains(key2))
             *      {
             *          BasicConsole.WriteLine("Dictionary contains key2!");
             *      }
             *      else
             *      {
             *          BasicConsole.WriteLine("Okay (5)");
             *      }
             *
             *
             *      dic.Remove(key3);
             *
             *      if (dic.Contains(key3))
             *      {
             *          BasicConsole.WriteLine("Dictionary contains key3!");
             *      }
             *      else
             *      {
             *          BasicConsole.WriteLine("Okay (6)");
             *      }
             *
             *      itr = dic.GetIterator();
             *      while (itr.HasNext())
             *      {
             *          UInt32Dictionary.KeyValuePair pair = itr.Next();
             *          BasicConsole.WriteLine("Pair: key=" + (FOS_System.String)pair.Key + ", value=" + pair.Value);
             *      }
             *  }
             * }
             * catch
             * {
             *  BasicConsole.WriteLine("Error testing UInt32Dictionary.");
             *  BasicConsole.WriteLine(ExceptionMethods.CurrentException.Message);
             * }
             * BasicConsole.DelayOutput(5);
             */
            #endregion

            DeferredSyscallsInfo_Unqueued = new Queue(256, false);
            DeferredSyscallsInfo_Queued   = new Queue(DeferredSyscallsInfo_Unqueued.Capacity, false);
            for (int i = 0; i < DeferredSyscallsInfo_Unqueued.Capacity; i++)
            {
                DeferredSyscallsInfo_Unqueued.Push(new DeferredSyscallInfo());
            }

            try
            {
                BasicConsole.WriteLine(" > Initialising system calls...");
                ProcessManager.CurrentProcess.SyscallHandler = SyscallHandler;
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.RegisterSyscallHandler);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.DeregisterSyscallHandler);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.StartThread);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.SleepThread);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.WakeThread);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.RegisterPipeOutpoint);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.GetNumPipeOutpoints);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.GetPipeOutpoints);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.WaitOnPipeCreate);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.ReadPipe);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.WritePipe);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.SendMessage);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.ReceiveMessage);

                //ProcessManager.CurrentProcess.OutputMemTrace = true;

                BasicConsole.WriteLine(" > Starting Idle process...");
                Process IdleProcess = ProcessManager.CreateProcess(Tasks.IdleTask.Main, "Idle", false, true);
                ProcessManager.RegisterProcess(IdleProcess, Scheduler.Priority.ZeroTimed);

                BasicConsole.WriteLine(" > Starting deferred syscalls thread...");
                DeferredSyscallsThread = ProcessManager.CurrentProcess.CreateThread(DeferredSyscallsThread_Main, "Deferred Sys Calls");

#if DEBUG
                BasicConsole.WriteLine(" > Starting Debugger thread...");
                Debug.Debugger.MainThread = ProcessManager.CurrentProcess.CreateThread(Debug.Debugger.Main, "Debugger");
#endif

                BasicConsole.WriteLine(" > Starting GC Cleanup thread...");
                ProcessManager.CurrentProcess.CreateThread(Tasks.GCCleanupTask.Main, "GC Cleanup");

                BasicConsole.WriteLine(" > Starting Window Manager...");
                Process WindowManagerProcess = ProcessManager.CreateProcess(WindowManagerTask.Main, "Window Manager", false, true);
                WindowManagerTask_ProcessId = WindowManagerProcess.Id;
                //WindowManagerProcess.OutputMemTrace = true;
                ProcessManager.RegisterProcess(WindowManagerProcess, Scheduler.Priority.Normal);

                BasicConsole.WriteLine(" > Waiting for Window Manager to be ready...");
                while (!WindowManagerTask.Ready)
                {
                    BasicConsole.WriteLine(" > [Wait pause]");
                    SystemCalls.SleepThread(1000);
                }
                BasicConsole.WriteLine(" > Window Manager reported ready.");

                BasicConsole.WriteLine(" > Starting Device Manager...");
                Process DeviceManagerProcess = ProcessManager.CreateProcess(DeviceManagerTask.Main, "Device Manager", false, true);
                //DeviceManagerProcess.OutputMemTrace = true;
                ProcessManager.RegisterProcess(DeviceManagerProcess, Scheduler.Priority.Normal);


                //TODO: Main task for commands etc

                BasicConsole.WriteLine("Started.");

                BasicConsole.PrimaryOutputEnabled = false;
                //BasicConsole.SecondaryOutputEnabled = false;

                try
                {
                    BasicConsole.WriteLine("KT > Creating virtual keyboard...");
                    keyboard = new Hardware.Keyboards.VirtualKeyboard();

                    BasicConsole.WriteLine("KT > Creating virtual console...");
                    console = new Consoles.VirtualConsole();

                    BasicConsole.WriteLine("KT > Connecting virtual console...");
                    console.Connect();

                    BasicConsole.WriteLine("KT > Creating main shell...");
                    Shells.MainShell shell = new Shells.MainShell(console, keyboard);

                    BasicConsole.WriteLine("KT > Running...");

                    uint loops = 0;
                    while (!Terminating)
                    {
                        try
                        {
                            //FOS_System.String msg = "KT > Hello, world! (" + (FOS_System.String)loops++ + ")";
                            //console.WriteLine(msg);
                            //BasicConsole.WriteLine(msg);
                            //SystemCalls.SleepThread(1000);
                            shell.Execute();
                        }
                        catch
                        {
                            BasicConsole.WriteLine("KT > Error executing MainShell!");
                            BasicConsole.WriteLine(ExceptionMethods.CurrentException.Message);
                        }
                    }
                }
                catch
                {
                    BasicConsole.WriteLine("KT > Error initialising!");
                    BasicConsole.WriteLine(ExceptionMethods.CurrentException.Message);
                }

                //BasicConsole.WriteLine(" > Starting Play Notes task...");
                //ProcessManager.CurrentProcess.CreateThread(Hardware.Tasks.PlayNotesTask.Main);

                //Console.InitDefault();
                //Shell.InitDefault();

                //BasicConsole.PrimaryOutputEnabled = false;
                //Shell.Default.Execute();
                //BasicConsole.PrimaryOutputEnabled = true;

                //if (!Shell.Default.Terminating)
                //{
                //    Console.Default.WarningColour();
                //    Console.Default.WriteLine("Abnormal shell shutdown!");
                //    Console.Default.DefaultColour();
                //}
                //else
                //{
                //    Console.Default.Clear();
                //}
            }
            catch
            {
                BasicConsole.PrimaryOutputEnabled = true;
                BasicConsole.SetTextColour(BasicConsole.warning_colour);
                BasicConsole.WriteLine(ExceptionMethods.CurrentException.Message);
                BasicConsole.SetTextColour(BasicConsole.default_colour);
            }

            BasicConsole.WriteLine();
            BasicConsole.WriteLine("---------------------");
            BasicConsole.WriteLine();
            BasicConsole.WriteLine("End of kernel task.");

            ExceptionMethods.HaltReason = "Managed main thread ended.";

            //TODO: Exit syscall
        }
Beispiel #17
0
        public static void Main()
        {
            BasicConsole.WriteLine("Kernel task! ");
            BasicConsole.WriteLine(" > Executing normally...");

            try
            {
                PriorityQueue  queue    = new PriorityQueue(20);
                TestComparable toRemove = new TestComparable(2);
                queue.Insert(new TestComparable(20));
                queue.Insert(new TestComparable(19));
                queue.Insert(new TestComparable(18));
                queue.Insert(new TestComparable(17));
                queue.Insert(new TestComparable(16));
                queue.Insert(new TestComparable(15));
                queue.Insert(new TestComparable(5));
                queue.Insert(new TestComparable(4));
                queue.Insert(new TestComparable(3));
                queue.Insert(toRemove);
                queue.Insert(new TestComparable(2));
                queue.Insert(new TestComparable(1));

                BasicConsole.WriteLine(queue.ToString());

                queue.Delete(toRemove);

                BasicConsole.WriteLine(queue.ToString());

                TestComparable minNode = (TestComparable)queue.ExtractMin();
                while (minNode != null)
                {
                    BasicConsole.WriteLine(minNode.Key);

                    minNode = (TestComparable)queue.ExtractMin();
                }
            }
            catch
            {
                BasicConsole.WriteLine("Error!");
                BasicConsole.WriteLine(ExceptionMethods.CurrentException.Message);
            }

            //BasicConsole.DelayOutput(100);

            DeferredSyscallsInfo_Unqueued = new Queue(256, false);
            DeferredSyscallsInfo_Queued   = new Queue(DeferredSyscallsInfo_Unqueued.Capacity, false);
            for (int i = 0; i < DeferredSyscallsInfo_Unqueued.Capacity; i++)
            {
                DeferredSyscallsInfo_Unqueued.Push(new DeferredSyscallInfo());
            }

            try
            {
                BasicConsole.WriteLine(" > Initialising system calls...");
                ProcessManager.CurrentProcess.SyscallHandler = SyscallHandler;
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.RegisterSyscallHandler);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.DeregisterSyscallHandler);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.StartThread);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.SleepThread);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.WakeThread);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.RegisterPipeOutpoint);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.GetNumPipeOutpoints);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.GetPipeOutpoints);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.WaitOnPipeCreate);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.ReadPipe);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.WritePipe);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.SendMessage);
                ProcessManager.CurrentProcess.SyscallsToHandle.Set((int)SystemCallNumbers.ReceiveMessage);

                //ProcessManager.CurrentProcess.OutputMemTrace = true;

                //BasicConsole.WriteLine(" > Starting deferred syscalls thread...");
                //DeferredSyscallsThread = ProcessManager.CurrentProcess.CreateThread(DeferredSyscallsThread_Main, "Deferred Sys Calls");

                //BasicConsole.WriteLine(" > Starting GC Cleanup thread...");
                //ProcessManager.CurrentProcess.CreateThread(Tasks.GCCleanupTask.Main, "GC Cleanup");

                BasicConsole.WriteLine(" > Starting Idle thread...");
                ProcessManager.CurrentProcess.CreateThread(Tasks.IdleTask.Main, "Idle");

#if DEBUG
                BasicConsole.WriteLine(" > Starting Debugger thread...");
                Debug.Debugger.MainThread = ProcessManager.CurrentProcess.CreateThread(Debug.Debugger.Main, "Debugger");
#endif

                bool OK = true;
                while (OK)
                {
                    ;
                }

                BasicConsole.WriteLine(" > Starting Window Manager...");
                Process WindowManagerProcess = ProcessManager.CreateProcess(WindowManagerTask.Main, "Window Manager", false, true);
                WindowManagerTask_ProcessId = WindowManagerProcess.Id;
                //WindowManagerProcess.OutputMemTrace = true;
                ProcessManager.RegisterProcess(WindowManagerProcess, Scheduler.Priority.Normal);

                BasicConsole.WriteLine(" > Waiting for Window Manager to be ready...");
                while (!WindowManagerTask.Ready)
                {
                    BasicConsole.WriteLine(" > [Wait pause]");
                    Thread.Sleep(1000);
                }
                BasicConsole.WriteLine(" > Window Manager reported ready.");

                BasicConsole.WriteLine(" > Starting Device Manager...");
                Process DeviceManagerProcess = ProcessManager.CreateProcess(DeviceManagerTask.Main, "Device Manager", false, true);
                //DeviceManagerProcess.OutputMemTrace = true;
                ProcessManager.RegisterProcess(DeviceManagerProcess, Scheduler.Priority.Normal);


                //TODO: Main task for commands etc

                BasicConsole.WriteLine("Started.");

                BasicConsole.PrimaryOutputEnabled = false;
                //BasicConsole.SecondaryOutputEnabled = false;

                try
                {
                    BasicConsole.WriteLine("KT > Creating virtual keyboard...");
                    keyboard = new Hardware.Keyboards.VirtualKeyboard();

                    BasicConsole.WriteLine("KT > Creating virtual console...");
                    console = new Consoles.VirtualConsole();

                    BasicConsole.WriteLine("KT > Connecting virtual console...");
                    console.Connect();

                    BasicConsole.WriteLine("KT > Creating main shell...");
                    Shells.MainShell shell = new Shells.MainShell(console, keyboard);

                    BasicConsole.WriteLine("KT > Running...");

                    //uint loops = 0;
                    while (!Terminating)
                    {
                        try
                        {
                            shell.Execute();
                        }
                        catch
                        {
                            BasicConsole.WriteLine("KT > Error executing MainShell!");
                            BasicConsole.WriteLine(ExceptionMethods.CurrentException.Message);
                        }
                    }
                }
                catch
                {
                    BasicConsole.WriteLine("KT > Error initialising!");
                    BasicConsole.WriteLine(ExceptionMethods.CurrentException.Message);
                }

                //BasicConsole.WriteLine(" > Starting Play Notes task...");
                //ProcessManager.CurrentProcess.CreateThread(Hardware.Tasks.PlayNotesTask.Main);

                //Console.InitDefault();
                //Shell.InitDefault();

                //BasicConsole.PrimaryOutputEnabled = false;
                //Shell.Default.Execute();
                //BasicConsole.PrimaryOutputEnabled = true;

                //if (!Shell.Default.Terminating)
                //{
                //    Console.Default.WarningColour();
                //    Console.Default.WriteLine("Abnormal shell shutdown!");
                //    Console.Default.DefaultColour();
                //}
                //else
                //{
                //    Console.Default.Clear();
                //}
            }
            catch
            {
                BasicConsole.PrimaryOutputEnabled = true;
                BasicConsole.SetTextColour(BasicConsole.warning_colour);
                BasicConsole.WriteLine(ExceptionMethods.CurrentException.Message);
                BasicConsole.SetTextColour(BasicConsole.default_colour);
            }

            BasicConsole.WriteLine();
            BasicConsole.WriteLine("---------------------");
            BasicConsole.WriteLine();
            BasicConsole.WriteLine("End of kernel task.");

            ExceptionMethods.HaltReason = "Managed main thread ended.";

            //TODO: Exit syscall
        }
Beispiel #18
0
        public static void Main()
        {
            OwnerThread = ProcessManager.CurrentThread;

            Thread.Sleep_Indefinitely();

            while (!Terminate)
            {
                //Scheduler.Disable();
                Awake = false;

                //BasicConsole.WriteLine("Playing notes...");

                while (LiveNoteRequests.Count > 0)
                {
                    //BasicConsole.WriteLine("Playing note...");

                    NoteRequest theReq = (NoteRequest)LiveNoteRequests.Pop();

                    try
                    {
                        Hardware.Timers.PIT.MusicalNote      note     = theReq.note;
                        Hardware.Timers.PIT.MusicalNoteValue duration = theReq.duration;
                        int bpm = theReq.bpm;

                        int  dur_ms = (int)duration * 60 * 1000 / (bpm * 16);
                        long do_ms  = dur_ms;
                        if (dur_ms >= 2000)
                        {
                            dur_ms -= 2000;
                            do_ms   = 2000;
                        }
                        else
                        {
                            dur_ms = 0;
                        }
                        NoteState state = new NoteState()
                        {
                            dur_ms = dur_ms
                        };

                        if (note != Timers.PIT.MusicalNote.Silent)
                        {
                            Hardware.Timers.PIT.ThePIT.PlaySound((int)note);
                        }

                        Playing = true;

                        state.handlerId = Hardware.Timers.PIT.ThePIT.RegisterHandler(new Hardware.Timers.PITHandler(SysCall_StopNoteHandler, state, 1000000L * do_ms, true));

                        while (Playing)
                        {
                            Thread.Sleep(50);
                        }
                    }
                    catch
                    {
                        BasicConsole.Write("Error processing note request! ");
                        if (ExceptionMethods.CurrentException != null)
                        {
                            BasicConsole.Write(ExceptionMethods.CurrentException.Message);
                        }
                        BasicConsole.WriteLine();
                        Playing = false;
                        Hardware.Timers.PIT.ThePIT.MuteSound();
                        BasicConsole.DelayOutput(15);
                    }
                    finally
                    {
                        DeadNoteRequests.Push(theReq);
                    }
                }

                //BasicConsole.WriteLine("Finished playing notes.");

                if (!Awake)
                {
                    //BasicConsole.WriteLine("Sleeping non-critical interrupts thread...");

                    //Scheduler.Enable();
                    if (!Thread.Sleep_Indefinitely())
                    {
                        BasicConsole.SetTextColour(BasicConsole.error_colour);
                        BasicConsole.WriteLine("Failed to sleep play notes thread!");
                        BasicConsole.SetTextColour(BasicConsole.default_colour);
                    }
                }
            }
        }
Beispiel #19
0
        public static void *NewArr(int length, FOS_System.Type elemType)
        {
            if (!Enabled)
            {
                BasicConsole.SetTextColour(BasicConsole.warning_colour);
                BasicConsole.WriteLine("Warning! GC returning null pointer because GC not enabled.");
                BasicConsole.Write("Last disabler: ");
                BasicConsole.WriteLine(lastDisabler);
                BasicConsole.DelayOutput(10);
                BasicConsole.SetTextColour(BasicConsole.default_colour);

                return(null);
            }

#if GC_TRACE
            if (OutputTrace)
            {
                BasicConsole.WriteLine("NewArr");
            }
#endif

            EnterCritical("NewArr");

            try
            {
                if (length < 0)
                {
                    ExceptionMethods.Throw_OverflowException();
                }

                InsideGC = true;

                //Alloc space for GC header that prefixes object data
                //Alloc space for new array object
                //Alloc space for new array elems

                uint totalSize = ((FOS_System.Type) typeof(FOS_System.Array)).Size;
                if (elemType.IsValueType)
                {
                    totalSize += elemType.Size * (uint)length;
                }
                else
                {
                    totalSize += elemType.StackSize * (uint)length;
                }
                totalSize += (uint)sizeof(GCHeader);

                GCHeader *newObjPtr = (GCHeader *)Heap.AllocZeroed(totalSize, "GC : NewArray");

                if ((UInt32)newObjPtr == 0)
                {
                    InsideGC = false;

                    BasicConsole.SetTextColour(BasicConsole.error_colour);
                    BasicConsole.WriteLine("Error! GC can't create a new array because the heap returned a null pointer.");
                    BasicConsole.DelayOutput(10);
                    BasicConsole.SetTextColour(BasicConsole.default_colour);

                    return(null);
                }

                NumObjs++;

                //Initialise the GCHeader
                SetSignature(newObjPtr);
                newObjPtr->RefCount = 1;

                FOS_System.Array newArr = (FOS_System.Array)Utilities.ObjectUtilities.GetObject(newObjPtr + 1);
                newArr._Type    = (FOS_System.Type) typeof(FOS_System.Array);
                newArr.length   = length;
                newArr.elemType = elemType;

                //Move past GCHeader
                byte *newObjBytePtr = (byte *)(newObjPtr + 1);

                InsideGC = false;

                return(newObjBytePtr);
            }
            finally
            {
                ExitCritical();
            }
        }
Beispiel #20
0
        protected void InterruptHandler()
        {
#if UHCI_TRACE
            BasicConsole.SetTextColour(BasicConsole.warning_colour);
            BasicConsole.WriteLine("UHCI: Interrupt handler");
            BasicConsole.SetTextColour(BasicConsole.default_colour);
            BasicConsole.DelayOutput(20);
#endif

            ushort val = USBSTS.Read_UInt16();

            if (val == 0) // Interrupt came from another UHCI device
            {
                return;
            }

            //if ((val & UHCI_Consts.STS_USBINT) == 0)
            //{
            //    //printf("\nUSB UHCI %u: ", u->num);
            //}

            //textColor(IMPORTANT);

#if UHCI_TRACE
            BasicConsole.SetTextColour(BasicConsole.warning_colour);
#endif

            if ((val & UHCI_Consts.STS_USBINT) != 0)
            {
#if UHCI_TRACE
                BasicConsole.WriteLine(((FOS_System.String) "UHCI Frame: ") + FRNUM.Read_UInt16() + " - USB transaction completed");
#endif
                USBSTS.Write_UInt16(UHCI_Consts.STS_USBINT); // reset interrupt
                TransactionsCompleted++;
            }

            if ((val & UHCI_Consts.STS_RESUME_DETECT) != 0)
            {
#if UHCI_TRACE
                BasicConsole.WriteLine("UHCI: Resume Detect");
#endif
                USBSTS.Write_UInt16(UHCI_Consts.STS_RESUME_DETECT); // reset interrupt
            }

#if UHCI_TRACE
            BasicConsole.SetTextColour(BasicConsole.error_colour);
#endif

            if ((val & UHCI_Consts.STS_HCHALTED) != 0)
            {
#if UHCI_TRACE
                BasicConsole.WriteLine("UHCI: Host Controller Halted");
#endif
                USBSTS.Write_UInt16(UHCI_Consts.STS_HCHALTED); // reset interrupt
            }

            if ((val & UHCI_Consts.STS_HC_PROCESS_ERROR) != 0)
            {
#if UHCI_TRACE
                BasicConsole.WriteLine("UHCI: Host Controller Process Error");
#endif
                USBSTS.Write_UInt16(UHCI_Consts.STS_HC_PROCESS_ERROR); // reset interrupt
            }

            if ((val & UHCI_Consts.STS_USB_ERROR) != 0)
            {
#if UHCI_TRACE
                BasicConsole.WriteLine("UHCI: USB Error");
#endif
                USBSTS.Write_UInt16(UHCI_Consts.STS_USB_ERROR); // reset interrupt
            }

            if ((val & UHCI_Consts.STS_HOST_SYSTEM_ERROR) != 0)
            {
#if UHCI_TRACE
                BasicConsole.WriteLine("UHCI: Host System Error");
#endif
                USBSTS.Write_UInt16(UHCI_Consts.STS_HOST_SYSTEM_ERROR); // reset interrupt
            }

#if UHCI_TRACE
            BasicConsole.SetTextColour(BasicConsole.default_colour);
#endif
        }
Beispiel #21
0
        protected void ResetHC()
        {
#if UHCI_TRACE
            BasicConsole.WriteLine("UHCI: ResetHC");
            BasicConsole.DelayOutput(5);
#endif

            //Processes.Scheduler.Disable();

            // http://www.lowlevel.eu/wiki/Universal_Host_Controller_Interface#Informationen_vom_PCI-Treiber_holen

            ushort legacySupport = pciDevice.ReadRegister16(UHCI_Consts.PCI_LEGACY_SUPPORT);
            pciDevice.WriteRegister16(UHCI_Consts.PCI_LEGACY_SUPPORT, UHCI_Consts.PCI_LEGACY_SUPPORT_STATUS); // resets support status bits in Legacy support register

            USBCMD.Write_UInt16(UHCI_Consts.CMD_GRESET);
            Hardware.Devices.Timer.Default.Wait(50);
            USBCMD.Write_UInt16(0);

            RootPortCount = (byte)(pciDevice.BaseAddresses[4].Size() / 2);
#if UHCI_TRACE
            BasicConsole.WriteLine(((FOS_System.String) "UHCI: RootPortCount=") + RootPortCount);
#endif
            for (byte i = 2; i < RootPortCount; i++)
            {
                if ((PORTSC1.Read_UInt16((ushort)(i * 2)) & UHCI_Consts.PORT_VALID) == 0 ||
                    (PORTSC1.Read_UInt16((ushort)(i * 2)) == 0xFFFF))
                {
                    RootPortCount = i;
                    break;
                }
            }
#if UHCI_TRACE
            BasicConsole.WriteLine(((FOS_System.String) "UHCI: RootPortCount=") + RootPortCount);
#endif

            if (RootPortCount > UHCI_Consts.PORTMAX)
            {
                RootPortCount = UHCI_Consts.PORTMAX;
            }
#if UHCI_TRACE
            BasicConsole.WriteLine(((FOS_System.String) "UHCI: RootPortCount=") + RootPortCount);
            BasicConsole.DelayOutput(1);
#endif

            RootPorts.Empty();
            for (byte i = 0; i < RootPortCount; i++)
            {
                RootPorts.Add(new HCPort()
                {
                    portNum = i
                });
            }

#if UHCI_TRACE
            BasicConsole.WriteLine("UHCI: Checking HC state: Get USBCMD...");
            BasicConsole.DelayOutput(1);
#endif

            ushort usbcmd = USBCMD.Read_UInt16();
#if UHCI_TRACE
            BasicConsole.WriteLine("UHCI: Checking HC state: Check...");
            BasicConsole.DelayOutput(1);
#endif
            if ((legacySupport & ~(UHCI_Consts.PCI_LEGACY_SUPPORT_STATUS | UHCI_Consts.PCI_LEGACY_SUPPORT_NO_CHG | UHCI_Consts.PCI_LEGACY_SUPPORT_PIRQ)) != 0 ||
                (usbcmd & UHCI_Consts.CMD_RS) != 0 ||
                (usbcmd & UHCI_Consts.CMD_CF) != 0 ||
                (usbcmd & UHCI_Consts.CMD_EGSM) == 0 ||
                (USBINTR.Read_UInt16() & UHCI_Consts.INT_MASK) != 0)
            {
#if UHCI_TRACE
                BasicConsole.WriteLine("UHCI: Checking HC state: Do reset...");
                BasicConsole.DelayOutput(1);
#endif

                USBSTS.Write_UInt16(UHCI_Consts.STS_MASK);
                Hardware.Devices.Timer.Default.Wait(1);
                USBCMD.Write_UInt16(UHCI_Consts.CMD_HCRESET);

                byte timeout = 50;
                while ((USBCMD.Read_UInt16() & UHCI_Consts.CMD_HCRESET) != 0)
                {
                    if (timeout == 0)
                    {
#if UHCI_TRACE
                        BasicConsole.WriteLine("UHCI: HC Reset timed out!");
                        BasicConsole.DelayOutput(1);
#endif
                        break;
                    }
                    Hardware.Devices.Timer.Default.Wait(10);
                    timeout--;
                }

#if UHCI_TRACE
                BasicConsole.WriteLine("UHCI: Checking HC state: Turning off interrupts and HC...");
                BasicConsole.DelayOutput(1);
#endif

                USBINTR.Write_UInt16(0); // switch off all interrupts
                USBCMD.Write_UInt16(0);  // switch off the host controller

#if UHCI_TRACE
                BasicConsole.WriteLine("UHCI: Checking HC state: Disabling ports...");
                BasicConsole.DelayOutput(1);
#endif

                for (byte i = 0; i < RootPortCount; i++) // switch off the valid root ports
                {
                    PORTSC1.Write_UInt16(0, (ushort)(i * 2));
                }
            }

            // TODO: mutex for frame list

#if UHCI_TRACE
            BasicConsole.WriteLine("UHCI: Creating queue head...");
            BasicConsole.DelayOutput(1);
#endif

            UHCI_QueueHead_Struct *qh = (UHCI_QueueHead_Struct *)FOS_System.Heap.AllocZeroedAPB((uint)sizeof(UHCI_QueueHead_Struct), 32, "UHCI : ResetHC");
            qh->next     = (UHCI_QueueHead_Struct *)UHCI_Consts.BIT_T;
            qh->transfer = (UHCI_qTD_Struct *)UHCI_Consts.BIT_T;
            qh->q_first  = null;
            qh->q_last   = null;
            qhPointer    = qh;

#if UHCI_TRACE
            BasicConsole.WriteLine("UHCI: Setting up frame list entries...");
            BasicConsole.DelayOutput(1);
#endif

            for (ushort i = 0; i < 1024; i++)
            {
                FrameList[i] = UHCI_Consts.BIT_T;
            }

#if UHCI_TRACE
            BasicConsole.WriteLine("UHCI: Setting SOFMOD...");
            BasicConsole.DelayOutput(1);
#endif

            // define each millisecond one frame, provide physical address of frame list, and start at frame 0
            SOFMOD.Write_Byte(0x40); // SOF cycle time: 12000. For a 12 MHz SOF counter clock input, this produces a 1 ms Frame period.

#if UHCI_TRACE
            BasicConsole.WriteLine("UHCI: Setting frame base addr and frame num...");
            BasicConsole.DelayOutput(1);
#endif

            FRBASEADD.Write_UInt32((uint)VirtMemManager.GetPhysicalAddress(FrameList));
            FRNUM.Write_UInt16(0);

#if UHCI_TRACE
            BasicConsole.WriteLine("UHCI: Setting PCI PIRQ...");
            BasicConsole.DelayOutput(1);
#endif

            // set PIRQ
            pciDevice.WriteRegister16(UHCI_Consts.PCI_LEGACY_SUPPORT, UHCI_Consts.PCI_LEGACY_SUPPORT_PIRQ);

#if UHCI_TRACE
            BasicConsole.WriteLine("UHCI: Starting HC...");
            BasicConsole.DelayOutput(1);
#endif

            // start host controller and mark it configured with a 64-byte max packet
            USBSTS.Write_UInt16(UHCI_Consts.STS_MASK);
            USBINTR.Write_UInt16(UHCI_Consts.INT_MASK); // switch on all interrupts
            USBCMD.Write_UInt16((ushort)(UHCI_Consts.CMD_RS | UHCI_Consts.CMD_CF | UHCI_Consts.CMD_MAXP));

#if UHCI_TRACE
            BasicConsole.WriteLine("UHCI: Reset CSC ports...");
            BasicConsole.DelayOutput(1);
#endif

            for (byte i = 0; i < RootPortCount; i++) // reset the CSC of the valid root ports
            {
                PORTSC1.Write_UInt16(UHCI_Consts.PORT_CS_CHANGE, (ushort)(i * 2));
            }

#if UHCI_TRACE
            BasicConsole.WriteLine("UHCI: Forcing global resume...");
            BasicConsole.DelayOutput(1);
#endif

            USBSTS.Write_UInt16(UHCI_Consts.STS_MASK);
#if UHCI_TRACE
            BasicConsole.WriteLine("     - STS MASK set");
#endif
            USBCMD.Write_UInt16((ushort)(UHCI_Consts.CMD_RS | UHCI_Consts.CMD_CF | UHCI_Consts.CMD_MAXP | UHCI_Consts.CMD_FGR));
#if UHCI_TRACE
            BasicConsole.WriteLine("     - FGR issued");
#endif
            Hardware.Devices.Timer.Default.Wait(20);
            USBCMD.Write_UInt16((ushort)(UHCI_Consts.CMD_RS | UHCI_Consts.CMD_CF | UHCI_Consts.CMD_MAXP));
#if UHCI_TRACE
            BasicConsole.WriteLine("     - FGR cleared");
            BasicConsole.DelayOutput(1);
#endif

            Hardware.Devices.Timer.Default.Wait(100);

#if UHCI_TRACE
            BasicConsole.WriteLine("UHCI: Getting run state...");
            BasicConsole.DelayOutput(1);
#endif

            run = (USBCMD.Read_UInt16() & UHCI_Consts.CMD_RS) != 0;



            if (!run)
            {
                BasicConsole.SetTextColour(BasicConsole.error_colour);
                BasicConsole.WriteLine("UHCI: Run/Stop not set!");
                BasicConsole.SetTextColour(BasicConsole.default_colour);
                BasicConsole.DelayOutput(5);
            }
            else
            {
                if ((USBSTS.Read_UInt16() & UHCI_Consts.STS_HCHALTED) == 0)
                {
                    Status = HCIStatus.Active;

                    EnablePorts(); // attaches the ports
                }
                else
                {
                    BasicConsole.SetTextColour(BasicConsole.error_colour);
                    BasicConsole.WriteLine("UHCI: HC Halted!");
                    BasicConsole.SetTextColour(BasicConsole.default_colour);
                    BasicConsole.DelayOutput(5);
                }
            }
        }
Beispiel #22
0
        public static void _DecrementRefCount(byte *objPtr)
        {
            if ((uint)objPtr < (uint)sizeof(GCHeader))
            {
                BasicConsole.SetTextColour(BasicConsole.error_colour);
                BasicConsole.WriteLine("Error! GC can't decrement ref count of an object in low memory.");
                BasicConsole.DelayOutput(5);
                BasicConsole.SetTextColour(BasicConsole.default_colour);
            }

            GCHeader *gcHeaderPtr = (GCHeader *)(objPtr - sizeof(GCHeader));

            if (CheckSignature(gcHeaderPtr))
            {
                gcHeaderPtr->RefCount--;

                //If the ref count goes below 0 then there was a circular reference somewhere.
                //  In actuality we don't care we can just only do cleanup when the ref count is
                //  exactly 0.
                if (gcHeaderPtr->RefCount == 0)
                {
#if GC_TRACE
                    if (OutputTrace)
                    {
                        BasicConsole.WriteLine("Cleaned up object.");
                    }
#endif

                    FOS_System.Object obj = (FOS_System.Object)Utilities.ObjectUtilities.GetObject(objPtr);
                    if (obj is FOS_System.Array)
                    {
                        //Decrement ref count of elements
                        FOS_System.Array arr = (FOS_System.Array)obj;
                        if (!arr.elemType.IsValueType)
                        {
                            FOS_System.Object[] objArr = (FOS_System.Object[])Utilities.ObjectUtilities.GetObject(objPtr);
                            for (int i = 0; i < arr.length; i++)
                            {
                                DecrementRefCount(objArr[i], true);
                            }
                        }
                    }
                    //Cleanup fields
                    FieldInfo *FieldInfoPtr = obj._Type.FieldTablePtr;
                    //Loop through all fields. The if-block at the end handles moving to parent
                    //  fields.
                    while (FieldInfoPtr != null)
                    {
                        if (FieldInfoPtr->Size > 0)
                        {
                            FOS_System.Type fieldType = (FOS_System.Type)Utilities.ObjectUtilities.GetObject(FieldInfoPtr->FieldType);
                            if (!fieldType.IsValueType &&
                                !fieldType.IsPointer)
                            {
                                byte *            fieldPtr    = objPtr + FieldInfoPtr->Offset;
                                FOS_System.Object theFieldObj = (FOS_System.Object)Utilities.ObjectUtilities.GetObject(fieldPtr);

                                DecrementRefCount(theFieldObj, true);

#if GC_TRACE
                                if (OutputTrace)
                                {
                                    BasicConsole.WriteLine("Cleaned up field.");
                                }
#endif
                            }

                            FieldInfoPtr++;
                        }

                        if (FieldInfoPtr->Size == 0)
                        {
                            FieldInfoPtr = (FieldInfo *)FieldInfoPtr->FieldType;
                        }
                    }

                    AddObjectToCleanup(gcHeaderPtr, objPtr);
                }
            }
        }
Beispiel #23
0
 public static void DBGWRN(FOS_System.String testName, FOS_System.String msg)
 {
     BasicConsole.SetTextColour(BasicConsole.warning_colour);
     DBGMSG(testName, msg);
     BasicConsole.SetTextColour(BasicConsole.default_colour);
 }
Beispiel #24
0
        protected override void _IssueTransfer(USBTransfer transfer)
        {
#if UHCI_TRACE
            BasicConsole.WriteLine("UHCI: Issue Transfer");
            BasicConsole.DelayOutput(5);
#endif

            UHCITransaction firstTransaction = (UHCITransaction)((USBTransaction)transfer.transactions[0]).underlyingTz;
            UHCITransaction lastTransaction  = (UHCITransaction)((USBTransaction)transfer.transactions[transfer.transactions.Count - 1]).underlyingTz;
            UHCI_qTD.SetIntOnComplete(lastTransaction.qTD, true);  // We want an interrupt after complete transfer
            CreateQH((UHCI_QueueHead_Struct *)transfer.underlyingTransferData, (uint)transfer.underlyingTransferData, firstTransaction.qTD);

#if UHCI_TRACE
            BasicConsole.WriteLine("    Queue head data:");
            BasicConsole.DumpMemory(transfer.underlyingTransferData, sizeof(UHCI_QueueHead_Struct));
            BasicConsole.WriteLine("    Transactions data:");
            for (int i = 0; i < transfer.transactions.Count; i++)
            {
                BasicConsole.Write(" ");
                BasicConsole.Write(i);
                BasicConsole.WriteLine(" : ");
                BasicConsole.WriteLine("  - qTD:");
                BasicConsole.DumpMemory(
                    ((UHCITransaction)((USBTransaction)transfer.transactions[i]).underlyingTz).qTD, sizeof(UHCI_qTD_Struct));
                BasicConsole.WriteLine("  - qTDBuffer:");
                BasicConsole.DumpMemory(
                    ((UHCITransaction)((USBTransaction)transfer.transactions[i]).underlyingTz).qTDBuffer, 16);
            }
            BasicConsole.DelayOutput(60);

            BasicConsole.WriteLine("UHCI: Issuing transfer...");
#endif

            for (byte i = 0; i < UHCI_Consts.NUMBER_OF_UHCI_RETRIES && !transfer.success; i++)
            {
                TransactionsCompleted = 0;
                for (int j = 0; j < transfer.transactions.Count; j++)
                {
                    USBTransaction  elem = (USBTransaction)transfer.transactions[j];
                    UHCITransaction uT   = (UHCITransaction)(elem.underlyingTz);
                    uT.qTD->u1 = uT.qTD->u1 & 0xFF00FFFF;
                    UHCI_qTD.SetActive(uT.qTD, true);
                }

                // stop scheduler
                USBSTS.Write_UInt16(UHCI_Consts.STS_MASK);
                USBCMD.Write_UInt16((ushort)(USBCMD.Read_UInt16() & ~UHCI_Consts.CMD_RS));
                while ((USBSTS.Read_UInt16() & UHCI_Consts.STS_HCHALTED) == 0)
                {
                    Hardware.Devices.Timer.Default.Wait(10);
                }

                // update scheduler
                uint qhPhysAddr = ((uint)VirtMemManager.GetPhysicalAddress(transfer.underlyingTransferData) | UHCI_Consts.BIT_QH);
                FrameList[0] = qhPhysAddr;
                FRBASEADD.Write_UInt32((uint)VirtMemManager.GetPhysicalAddress(FrameList));
                FRNUM.Write_UInt16(0);
                // start scheduler
                USBSTS.Write_UInt16(UHCI_Consts.STS_MASK);
                USBCMD.Write_UInt16((ushort)(USBCMD.Read_UInt16() | UHCI_Consts.CMD_RS));
                while ((USBSTS.Read_UInt16() & UHCI_Consts.STS_HCHALTED) != 0)
                {
                    Hardware.Devices.Timer.Default.Wait(10);
                }

#if UHCI_TRACE
                BasicConsole.WriteLine(((FOS_System.String) "USBINT val: ") + USBINTR.Read_UInt16());
#endif

                // run transactions
                bool active  = true;
                int  timeout = 100; //5 seconds
                while (active && timeout > 0)
                {
                    active = false;
                    for (int j = 0; j < transfer.transactions.Count; j++)
                    {
                        USBTransaction  elem = (USBTransaction)transfer.transactions[j];
                        UHCITransaction uT   = (UHCITransaction)(elem.underlyingTz);
                        active = active || ((uT.qTD->u1 & 0x00FF0000) == 0x00800000);
                    }

                    Hardware.Devices.Timer.Default.Wait(50);
                    timeout--;
                }

#if UHCI_TRACE
                BasicConsole.WriteLine("Finished waiting.");
#endif

                FrameList[0] = UHCI_Consts.BIT_T;

                if (timeout == 0 ||
                    TransactionsCompleted != transfer.transactions.Count)
                {
#if UHCI_TRACE
                    BasicConsole.SetTextColour(BasicConsole.error_colour);
                    BasicConsole.WriteLine("UHCI: Error! Transactions wait timed out or wrong number of transactions completed.");
                    BasicConsole.SetTextColour(BasicConsole.default_colour);

                    BasicConsole.WriteLine(((FOS_System.String) "Transactions completed: ") + TransactionsCompleted);

                    if (timeout == 0)
                    {
                        BasicConsole.SetTextColour(BasicConsole.error_colour);
                        BasicConsole.WriteLine("UHCI: Error! Transfer timed out.");
                        BasicConsole.SetTextColour(BasicConsole.default_colour);
                    }
#endif

                    transfer.success = false;

                    bool completeDespiteNoInterrupt = true;
                    for (int j = 0; j < transfer.transactions.Count; j++)
                    {
                        USBTransaction  elem = (USBTransaction)transfer.transactions[j];
                        UHCITransaction uT   = (UHCITransaction)(elem.underlyingTz);

#if UHCI_TRACE
                        BasicConsole.WriteLine(((FOS_System.String) "u1=") + uT.qTD->u1 + ", u2=" + uT.qTD->u2);
                        BasicConsole.WriteLine(((FOS_System.String) "Status=") + (byte)(uT.qTD->u1 >> 16));
#endif
                        completeDespiteNoInterrupt = completeDespiteNoInterrupt && isTransactionSuccessful(uT);
                    }

                    transfer.success = completeDespiteNoInterrupt;

#if UHCI_TRACE
                    BasicConsole.SetTextColour(BasicConsole.warning_colour);
                    BasicConsole.WriteLine(((FOS_System.String) "Complete despite no interrupts: ") + completeDespiteNoInterrupt);
                    BasicConsole.SetTextColour(BasicConsole.default_colour);

                    BasicConsole.DelayOutput(5);
#endif
                }
                else
                {
                    transfer.success = true;
                }

                if (transfer.success)
                {
                    // check conditions and save data
                    for (int j = 0; j < transfer.transactions.Count; j++)
                    {
                        USBTransaction  elem = (USBTransaction)transfer.transactions[j];
                        UHCITransaction uT   = (UHCITransaction)(elem.underlyingTz);
                        transfer.success = transfer.success && isTransactionSuccessful(uT); // executed w/o error

                        if (uT.inBuffer != null && uT.inLength != 0)
                        {
                            MemoryUtils.MemCpy_32((byte *)uT.inBuffer, (byte *)uT.qTDBuffer, uT.inLength);
                        }
                    }
                }

#if UHCI_TRACE
                if (!transfer.success)
                {
                    BasicConsole.SetTextColour(BasicConsole.error_colour);
                    BasicConsole.WriteLine("UHCI: Transfer failed.");
                    BasicConsole.SetTextColour(BasicConsole.default_colour);
                }
                else
                {
                    BasicConsole.SetTextColour((char)0x0200);
                    BasicConsole.WriteLine("Transfer succeeded.");
                    BasicConsole.SetTextColour(BasicConsole.default_colour);
                }
#endif
            }
        }
Beispiel #25
0
        public static void *NewString(int length)
        {
            if (!Enabled)
            {
                BasicConsole.SetTextColour(BasicConsole.warning_colour);
                BasicConsole.WriteLine("Warning! GC returning null pointer because GC not enabled.");
                BasicConsole.Write("Last disabler: ");
                BasicConsole.WriteLine(lastDisabler);
                BasicConsole.DelayOutput(10);
                BasicConsole.SetTextColour(BasicConsole.default_colour);

                return(null);
            }

#if GC_TRACE
            if (OutputTrace)
            {
                BasicConsole.WriteLine("NewString");
            }
#endif

            EnterCritical("NewString");

            try
            {
                if (length < 0)
                {
                    BasicConsole.SetTextColour(BasicConsole.error_colour);
                    BasicConsole.WriteLine("Error! GC can't create a new string because \"length\" is less than 0.");
                    BasicConsole.DelayOutput(5);
                    BasicConsole.SetTextColour(BasicConsole.default_colour);

                    ExceptionMethods.Throw_OverflowException();
                }

                InsideGC = true;

                //Alloc space for GC header that prefixes object data
                //Alloc space for new string object
                //Alloc space for new string chars

                uint totalSize = ((FOS_System.Type) typeof(FOS_System.String)).Size;
                totalSize += /*char size in bytes*/ 2 * (uint)length;
                totalSize += (uint)sizeof(GCHeader);

                GCHeader *newObjPtr = (GCHeader *)Heap.AllocZeroed(totalSize, "GC : NewString");

                if ((UInt32)newObjPtr == 0)
                {
                    InsideGC = false;

                    BasicConsole.SetTextColour(BasicConsole.error_colour);
                    BasicConsole.WriteLine("Error! GC can't create a new string because the heap returned a null pointer.");
                    BasicConsole.DelayOutput(10);
                    BasicConsole.SetTextColour(BasicConsole.default_colour);

                    return(null);
                }

                NumStrings++;

                //Initialise the GCHeader
                SetSignature(newObjPtr);
                //RefCount to 0 initially because of FOS_System.String.New should be used
                //      - In theory, New should be called, creates new string and passes it back to caller
                //        Caller is then required to store the string in a variable resulting in inc.
                //        ref count so ref count = 1 in only stored location.
                //        Caller is not allowed to just "discard" (i.e. use Pop IL op or C# that generates
                //        Pop IL op) so ref count will always at some point be incremented and later
                //        decremented by managed code. OR the variable will stay in a static var until
                //        the OS exits...

                newObjPtr->RefCount = 0;

                FOS_System.String newStr = (FOS_System.String)Utilities.ObjectUtilities.GetObject(newObjPtr + 1);
                newStr._Type  = (FOS_System.Type) typeof(FOS_System.String);
                newStr.length = length;

                //Move past GCHeader
                byte *newObjBytePtr = (byte *)(newObjPtr + 1);

                InsideGC = false;

                return(newObjBytePtr);
            }
            finally
            {
                ExitCritical();
            }
        }