CosmosOS / Cosmos

Cosmos is an operating system "construction kit". Build your own OS using managed languages such as C#, VB.NET, and more!
https://www.goCosmos.org
BSD 3-Clause "New" or "Revised" License
2.94k stars 551 forks source link

CPU Exceptions on any graphics start #1682

Closed virtualtoja closed 3 years ago

virtualtoja commented 3 years ago

When I try to start any type of graphics it displays random errors. It should start graphics when command "startgui" is executed. The errors are mainly Cpu exception x0D or x06. Also VGAScreen does not work.

My code:

using System;
using System.Collections.Generic;
using System.Threading;
using Sys = Cosmos.System;
using Cosmos.System.Graphics;
using Cosmos.System.FileSystem;
using Cosmos.HAL;
using System.Drawing;

namespace microOS
{
    public class Kernel : Sys.Kernel
    {
        protected override void BeforeRun()
        {
            Console.Clear();
            Console.WriteLine("Starting cmdline...");
            StartCmdLine();
            Console.WriteLine("Done");    
        }

        const string version = "v0.1.3";
        CommandLine cmd;
        GraphicalInterface gui;

        void StartCmdLine() 
        {
            Console.WriteLine("Loading commands...");
            cmd = new CommandLine();
            cmd.InitCommandLine();
            Console.WriteLine($"microOS {version} booted succesfully.");
        }

        void StartGraphicalInterface() 
        {
            gui = new GraphicalInterface();
            gui.InitGUI();
        }

        protected override void Run()
        {
            Console.Write(">>>");
            string cmmd = Console.ReadLine();
            if (cmmd == "startgui")
            {
                Console.WriteLine("Starting graphical interface...");
                StartGraphicalInterface();
            }
            else
            {
                cmd.CheckCommand(cmmd);
            }
        }
    }

    public class GraphicalInterface
    {
        public const string guiver = "v0.0.1";

        Screen screen;

        public void Update()
        {
            screen.FillRectangle(10, 10, 100, 100, new Pen(Color.Black));
        }

        public void InitGUI()
        {
            screen = new Screen(640, 400);
            screen.BackColor = Color.Black;
        }
    }

    public class Screen
    {
        public Color BackColor;
        public VBECanvas canvas;

        public void FillRectangle(int x, int y, int w, int h, Pen pen) 
        {
            canvas.DrawFilledRectangle(pen, x, y, w, h);
        }

        public Screen(int w, int h)
        {
            canvas = (VBECanvas)FullScreenCanvas.GetFullScreenCanvas(new Mode(w, h, ColorDepth.ColorDepth32));
            canvas.Clear();
            canvas.Clear(BackColor);
        }
    }

    public class CommandLine
    {
        public const string cmdver = "v0.0.1";

        public void InitCommandLine()
        {

        }

        public void CheckCommand(string command) 
        {
            if (command.StartsWith("prnt"))
            {
                Console.WriteLine(command.Substring(5));
            }
            else if(command == "clear") 
            {
                Console.Clear();
            }
            else if (command == "help")
            {
                HelpMenu();
            }
            else if(command == "startgui") 
            {

            }
            else
            {
                Console.WriteLine($"Command or file \"{command}\" does not exist.");
            }
        }

        public string[] commands = new string[] { "prnt <text>", "clear", "help" };

        #region Commands

        void HelpMenu()
        {
            Console.Write("List of available commands: ");
            foreach(string command in commands) 
            {
                Console.Write($"{command}, ");
            }
            Console.WriteLine();
        }

        #endregion
    }
}

Screenshots: obraz

obraz

elkaamee326 commented 3 years ago

Did it display the same output for the VGAScreen? At the moment I would recommend using the VMware SVGAII Canvas, because it is very fast and it is easy to put images on there. I think they are still working on VBE, so for now stick with VMware SVGAII. Also, to know the exception put Console.WriteLine statements between each line that looks suspicous of an error happening and then when it stops at that location put a try and catch block.

virtualtoja commented 3 years ago

All the screenshots are on Canvas VGAScreen didnt work for me. VS didnt see the VGAScreen class variables and functions. Also sorry for my bad english.

virtualtoja commented 3 years ago

I have a new problem, I changed the VBECanvas to normal Canvas and it also doesnt work but it has different errors.

`using System; using System.Collections.Generic; using System.Threading; using Sys = Cosmos.System; using Cosmos.System.Graphics; using Cosmos.System.FileSystem; using Cosmos.HAL; using System.Drawing; using Cosmos.HAL.Network;

namespace microOS { public class Kernel : Sys.Kernel { protected override void BeforeRun() { Console.Clear(); Console.WriteLine("Starting cmdline..."); StartCmdLine(); Console.WriteLine("Done"); }

    public const string version = "v0.1.5";
    CommandLine cmd;

    void StartCmdLine()
    {
        Console.WriteLine("Loading commands...");
        cmd = new CommandLine();
        cmd.InitCommandLine();
        Console.WriteLine($"microOS {version} booted succesfully.");
    }

    void StartGraphicalInterface()
    {
        Screen.Init();

        Sys.MouseManager.ScreenHeight = (uint)Screen.screenY;
        Sys.MouseManager.ScreenWidth = (uint)Screen.screenX;

        guimode = true;
        Screen.ClearScreen(Color.Black);
    }

    public bool guimode = false;

    protected override void Run()
    {
        if(guimode)
        {
            DrawCursor(Color.White);
        }
        else
        {
            Console.Write(">>>");
            string cmmd = Console.ReadLine();
            if (cmmd == "startgui")
            {
                Console.WriteLine("Starting graphical interface...");
                StartGraphicalInterface();
            }
            else
            {
                cmd.CheckCommand(cmmd);
            }
        }

    }

    void DrawCursor(Color c)
    {
        Screen.SetPixel((int)Sys.MouseManager.X, (int)Sys.MouseManager.Y, c);
        Screen.SetPixel((int)Sys.MouseManager.X, (int)Sys.MouseManager.Y-1, c);
        Screen.SetPixel((int)Sys.MouseManager.X+1, (int)Sys.MouseManager.Y, c);
        Screen.SetPixel((int)Sys.MouseManager.X+1, (int)Sys.MouseManager.Y-1, c);

        Screen.DrawScreen();
    }
}

public class CommandLine
{
    public const string cmdver = "v0.1.1";

    public void InitCommandLine()
    {

    }

    public void CheckCommand(string command)
    {
        if (command.StartsWith("prnt"))
        {
            try
            {
                Console.WriteLine(command.Substring(5));
            }
            catch { }
        }
        else if (command == "clear")
        {
            Console.Clear();
        }
        else if (command == "help")
        {
            HelpMenu();
        }
        else if(command == "ver")
        {
            Console.WriteLine($"cmdline version: {cmdver} microOS version: {Kernel.version}");
        }
        else if (command.StartsWith("pkg"))
        {
            if (command.Substring(4).StartsWith("remove"))
            {
                string pkgname = command.Substring(11);

            }
        }
        else
        {
            Console.WriteLine($"Command or file \"{command}\" does not exist.");
        }
    }

    public string[] commands = new string[] { "prnt <text>", "clear", "help", "pkg <args>", "ver" };

    #region Commands

    void HelpMenu()
    {
        Console.Write("List of available commands: ");
        foreach (string command in commands)
        {
            Console.Write($"{command}, ");
        }
        Console.WriteLine();
    }

    #endregion
}

public static class Screen
{
    public static int screenX = 320;
    public static int screenY = 200;

    private static Color[] pixelBuffer = new Color[(screenX * screenY) + screenX];
    private static Color[] pixelBufferOld = new Color[(screenX * screenY) + screenX];

    private static Canvas canvas = FullScreenCanvas.GetFullScreenCanvas();

    public static void Init()
    {
        canvas.Mode = new Mode(screenX, screenY, ColorDepth.ColorDepth32);
    }

    public static void SetPixel(int x, int y, Color c)
    {
        if (x > screenX || y > screenY) return;
        pixelBuffer[(x * y) + x] = c;
    }

    public static void ClearScreen(Color c)
    {
        canvas.Clear(c);
    }

    public static void DrawScreen()
    {
        Pen pen = new Pen(Color.White);
        for (int y = 0, h = screenY; y < h; y++)
        {
            for (int x = 0, w = screenX; x < h; x++)
            {
                if (!(pixelBuffer[(x * y) + x] == pixelBufferOld[(y * y) + x]))
                {
                    pen.Color = pixelBuffer[(y * screenX) + x];
                    canvas.DrawPoint(pen, x, y);
                }
            }
        }
        for (int i = 0, len = pixelBuffer.Length; i < len; i++)
        {
            pixelBuffer[i] = pixelBufferOld[i];
        }
    }
}

public static class DiskManagement
{
    public static CosmosVFS fs;

    public static void Init()
    {
        fs = new CosmosVFS();
        Sys.FileSystem.VFS.VFSManager.RegisterVFS(fs);
        fs.Initialize();
    }

}

public static class PackageManager
{
    public static void Remove(string packageName)
    {

    }

    public static void Install(string packageName)
    {

    }
}

} `

This is my new code.

In VMWare it displays black screen:

obraz

On real hardware it displays this:

f2e6eb96-3094-4624-8b64-8224f914a1b3

elkaamee326 commented 3 years ago

I don't think Canvas is supported on real hardware yet (I mean there are still lots of bugs), also did the VGA Canvas work? Edit: the only driver that will work on real hardware is VGA, all the other ones don't work.

ArdhenisAflah commented 3 years ago

same i have problem with canvas always. you have ide with this?

ArdhenisAflah commented 3 years ago

i think i hopeless with that :)

quajak commented 3 years ago

Changing the mode of the canvas might be the problem.

ArdhenisAflah commented 3 years ago

@quajak and how i can solve that? can i not use a canvas mode?

quajak commented 3 years ago

Pass the correct canvas mode when intialising the canvas. To change the mode while running, disable the canvas and then reinitialise it with the new mode.

ArdhenisAflah commented 3 years ago

ok, thanks!.