1
0
sunnycase 6 жил өмнө
parent
commit
a303b6765b

+ 16 - 0
src/Chino.Core/Memory/MemoryManager.cs

@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+using System.Text;
+
+namespace Chino.Memory
+{
+    public class MemoryManager
+    {
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        public static extern int GetUsedMemorySize();
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        public static extern int GetFreeMemorySize();
+    }
+}

+ 0 - 62
src/Chino.Kernel/KernelProgram.cs

@@ -17,10 +17,6 @@ namespace Chino.Kernel
             var systemThread = Scheduler.CreateThread(() =>
             var systemThread = Scheduler.CreateThread(() =>
             {
             {
                 var host = new KernelServiceHost();
                 var host = new KernelServiceHost();
-
-                var terminal = Terminal.Default;
-                terminal.Foreground(TerminalColor.White)
-                    .WriteLine("Hello Chino OS!");
                 host.Run();
                 host.Run();
             });
             });
 
 
@@ -29,62 +25,4 @@ namespace Chino.Kernel
             Scheduler.StartCurrentScheduler();
             Scheduler.StartCurrentScheduler();
         }
         }
     }
     }
-
-    enum TerminalColor
-    {
-        Black,
-        Red,
-        Green,
-        White
-    }
-
-    class Terminal
-    {
-        public static Terminal Default { get; } = new Terminal();
-
-        public Terminal Write(string message)
-        {
-            Debug.Write(message);
-            return this;
-        }
-
-        public Terminal WriteLine(string message)
-        {
-            Debug.WriteLine(message);
-            return this;
-        }
-
-        public Terminal WriteLine()
-        {
-            Debug.WriteLine(string.Empty);
-            return this;
-        }
-
-        public Terminal Reset()
-        {
-            Debug.Write("\u001b[0m");
-            return this;
-        }
-
-        public Terminal Foreground(TerminalColor color)
-        {
-            string seq = color switch
-            {
-                TerminalColor.Black => "\u001b[30m",
-                TerminalColor.Red => "\u001b[31m",
-                TerminalColor.Green => "\u001b[32m",
-                TerminalColor.White => "\u001b[37m",
-                _ => string.Empty
-            };
-
-            Debug.Write(seq);
-            return this;
-        }
-
-        public Terminal Ready()
-        {
-            Debug.Write("$ ");
-            return this;
-        }
-    }
 }
 }

+ 4 - 1
src/Chino.Threading/Threading/Scheduler.cs

@@ -250,7 +250,10 @@ namespace Chino.Threading
 
 
         private void IdleMain()
         private void IdleMain()
         {
         {
-            while (true) ;
+            while (true)
+            {
+                //DelayCurrentThread(TimeSlice);
+            }
         }
         }
     }
     }
 
 

+ 2 - 0
src/Native/arch/emulator/io/console.cpp

@@ -17,6 +17,8 @@ void EmulatorConsole::OpenStdHandles(gc_obj_ref<EmulatorConsole> _this)
 {
 {
     _this->_stdIn = (uintptr_t)GetStdHandle(STD_INPUT_HANDLE);
     _this->_stdIn = (uintptr_t)GetStdHandle(STD_INPUT_HANDLE);
     _this->_stdOut = (uintptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
     _this->_stdOut = (uintptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
+
+    THROW_WIN32_IF_NOT(SetConsoleMode(_this->_stdIn._value, 0));
 }
 }
 
 
 int32_t EmulatorConsole::Read(gc_obj_ref<EmulatorConsole> _this, Span_1<uint8_t> buffer)
 int32_t EmulatorConsole::Read(gc_obj_ref<EmulatorConsole> _this, Span_1<uint8_t> buffer)

+ 1 - 0
src/Native/main.cpp

@@ -19,6 +19,7 @@ void InitializeHeap() noexcept;
 int main()
 int main()
 {
 {
     InitializeHeap();
     InitializeHeap();
+    System_Private_CoreLib::System::Activator::_s_CreateInstance<System_Collections::System::Collections::Generic::List_1<int32_t>>();
     // Initialize chip control
     // Initialize chip control
     ChipControl::_s_set_Default(make_object<ArchChipControl>());
     ChipControl::_s_set_Default(make_object<ArchChipControl>());
     KernelProgram::_s_KernelMain();
     KernelProgram::_s_KernelMain();

+ 1 - 1
src/Native/natsu.gc.cpp

@@ -389,7 +389,7 @@ static void prvInsertBlockIntoFreeList(BlockLink_t *pBlockToInsert)
 }
 }
 
 
 using namespace System_Private_CoreLib::System;
 using namespace System_Private_CoreLib::System;
-using namespace Chino_Kernel::Chino::Memory;
+using namespace Chino_Core::Chino::Memory;
 using namespace natsu;
 using namespace natsu;
 
 
 gc_obj_ref<Object> natsu::gc_alloc(const clr_vtable &vtable, size_t size)
 gc_obj_ref<Object> natsu::gc_alloc(const clr_vtable &vtable, size_t size)

+ 1 - 0
src/System.Console/System/IO/StdInReader.cs

@@ -243,6 +243,7 @@ namespace System.IO
                 case '\t':
                 case '\t':
                     return ConsoleKey.Tab;
                     return ConsoleKey.Tab;
 
 
+                case '\r':
                 case '\n':
                 case '\n':
                     return ConsoleKey.Enter;
                     return ConsoleKey.Enter;
 
 

+ 1 - 0
src/System.Runtime/TypeForwards.cs

@@ -24,6 +24,7 @@ using System.Threading;
 [assembly: TypeForwardedTo(typeof(Single))]
 [assembly: TypeForwardedTo(typeof(Single))]
 [assembly: TypeForwardedTo(typeof(Double))]
 [assembly: TypeForwardedTo(typeof(Double))]
 [assembly: TypeForwardedTo(typeof(String))]
 [assembly: TypeForwardedTo(typeof(String))]
+[assembly: TypeForwardedTo(typeof(StringSplitOptions))]
 [assembly: TypeForwardedTo(typeof(Nullable))]
 [assembly: TypeForwardedTo(typeof(Nullable))]
 [assembly: TypeForwardedTo(typeof(Nullable<>))]
 [assembly: TypeForwardedTo(typeof(Nullable<>))]
 [assembly: TypeForwardedTo(typeof(IComparable))]
 [assembly: TypeForwardedTo(typeof(IComparable))]

+ 4 - 0
src/apps/Chino.Apps.Shell/Chino.Apps.Shell.csproj

@@ -6,4 +6,8 @@
     <OutputPath>$(SolutionDir)out/bin/</OutputPath>
     <OutputPath>$(SolutionDir)out/bin/</OutputPath>
   </PropertyGroup>
   </PropertyGroup>
 
 
+  <ItemGroup>
+    <ProjectReference Include="..\..\Chino.Core\Chino.Core.csproj" />
+  </ItemGroup>
+
 </Project>
 </Project>

+ 63 - 0
src/apps/Chino.Apps.Shell/CommandInterpreter.cs

@@ -0,0 +1,63 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Chino.Apps.Shell.Commands;
+
+namespace Chino.Apps.Shell
+{
+    class CommandInterpreter
+    {
+        private readonly Dictionary<string, ShellCommand> _commands = new Dictionary<string, ShellCommand>();
+
+        public CommandInterpreter()
+        {
+            RegisterCommand("free", new FreeCommand());
+            RegisterCommand("echo", new EchoCommand());
+        }
+
+        public void RegisterCommand(string name, ShellCommand command)
+        {
+            _commands.Add(name, command);
+        }
+
+        public void Run()
+        {
+            while (true)
+            {
+                Console.Write("$ ");
+                var input = Console.ReadLine();
+
+                if (!string.IsNullOrWhiteSpace(input))
+                    ProcessInput(input);
+            }
+        }
+
+        private void ProcessInput(string commandline)
+        {
+            var args = commandline.Split(' ', StringSplitOptions.RemoveEmptyEntries);
+            var commandName = args[0];
+            if (_commands.TryGetValue(commandName, out var command))
+            {
+                try
+                {
+                    command.Execute(args);
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine(ex.ToString());
+                }
+            }
+            else
+            {
+                Console.WriteLine($"sh: command not found: {commandName}");
+            }
+        }
+    }
+
+    public abstract class ShellCommand
+    {
+        public string Name { get; }
+
+        public abstract void Execute(string[] args);
+    }
+}

+ 22 - 0
src/apps/Chino.Apps.Shell/Commands/EchoCommand.cs

@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Chino.Memory;
+
+namespace Chino.Apps.Shell.Commands
+{
+    class EchoCommand : ShellCommand
+    {
+        public override void Execute(string[] args)
+        {
+            for (int i = 1; i < args.Length; i++)
+            {
+                Console.Write(args[i]);
+                if (i < args.Length - 1)
+                    Console.Write(' ');
+            }
+
+            Console.WriteLine();
+        }
+    }
+}

+ 19 - 0
src/apps/Chino.Apps.Shell/Commands/FreeCommand.cs

@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Chino.Memory;
+
+namespace Chino.Apps.Shell.Commands
+{
+    class FreeCommand : ShellCommand
+    {
+        public override void Execute(string[] args)
+        {
+            var usedMemory = MemoryManager.GetUsedMemorySize() / 1024f;
+            var freeMemory = MemoryManager.GetFreeMemorySize() / 1024f;
+            Console.WriteLine($"Memory\t\t Total\t {usedMemory + freeMemory:F2} K");
+            Console.WriteLine($"\t\t  Used\t {usedMemory:F2} K");
+            Console.WriteLine($"\t\t  Free\t {freeMemory:F2} K");
+        }
+    }
+}

+ 20 - 7
src/apps/Chino.Apps.Shell/Program.cs

@@ -1,6 +1,7 @@
 using System;
 using System;
 using System.Diagnostics;
 using System.Diagnostics;
 using System.Threading;
 using System.Threading;
+using Chino.Memory;
 
 
 namespace Chino.Apps.Shell
 namespace Chino.Apps.Shell
 {
 {
@@ -9,15 +10,27 @@ namespace Chino.Apps.Shell
         static void Main(string[] args)
         static void Main(string[] args)
         {
         {
             Console.ForegroundColor = ConsoleColor.Green;
             Console.ForegroundColor = ConsoleColor.Green;
-            Console.WriteLine("Hello Shell!");
+
+            // Print logo
+            Console.WriteLine(@"
+ _____ _     _             
+/  __ \ |   (_)            
+| /  \/ |__  _ _ __   ___  
+| |   | '_ \| | '_ \ / _ \ 
+| \__/\ | | | | | | | (_) |
+ \____/_| |_|_|_| |_|\___/ 
+                           ");
+            // Print memory status
+            var usedMemory = MemoryManager.GetUsedMemorySize() / 1024f;
+            var freeMemory = MemoryManager.GetFreeMemorySize() / 1024f;
+            Console.WriteLine($"Memory\t\t Total\t {usedMemory + freeMemory:F2} K");
+            Console.WriteLine($"\t\t  Used\t {usedMemory:F2} K");
+            Console.WriteLine($"\t\t  Free\t {freeMemory:F2} K");
+            Console.WriteLine();
             Console.ForegroundColor = ConsoleColor.White;
             Console.ForegroundColor = ConsoleColor.White;
 
 
-            int i = 0;
-            while (true)
-            {
-                Thread.Sleep(1000);
-                Console.WriteLine("Tick" + i++);
-            }
+            var interpreter = new CommandInterpreter();
+            interpreter.Run();
         }
         }
     }
     }
 }
 }