-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathEntryPoint.cs
138 lines (122 loc) · 5.06 KB
/
EntryPoint.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// ------------------------------------------------------------------------------
// <copyright from='2002' to='2002' company='Scott Hanselman'>
// Copyright (c) Scott Hanselman. All Rights Reserved.
// </copyright>
// ------------------------------------------------------------------------------
//
// Scott Hanselman's Tiny Academic Virtual CPU and OS
// Copyright (c) 2002, Scott Hanselman (scott@hanselman.com)
// All rights reserved.
//
// A BSD License
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// Neither the name of Scott Hanselman nor the names of its contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Hanselman.CST352
{
/// <summary>
/// "Bootstraps" the system by creating an <see cref="OS"/>, setting the size of the <see cref="CPU"/>'s memory,
/// and loading each <see cref="Program"/> into memory. Then, for each <see cref="Program"/> we create a
/// <see cref="Process"/>. Then we start everything by calling <see cref="OS.execute()"/>
/// </summary>
class EntryPoint
{
public static IConfigurationRoot Configuration { get; set; }
/// <summary>
/// The entry point for the virtual OS
/// </summary>
[STAThread]
static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
OS theOS = null;
uint bytesOfVirtualMemory = 0;
uint bytesOfPhysicalMemory = 0;
PrintHeader();
if (args.Length < 2)
PrintInstructions();
else
{
//try
{
// Total addressable (virtual) memory taken from the command line
bytesOfVirtualMemory = uint.Parse(args[0]);
bytesOfPhysicalMemory = uint.Parse(Configuration["PhysicalMemory"]);
// Setup static physical memory
CPU.initPhysicalMemory(bytesOfPhysicalMemory);
// Create the OS and Memory Manager with Virtual Memory
theOS = new OS(bytesOfVirtualMemory);
// Let the CPU know about the OS
CPU.theOS = theOS;
Console.WriteLine("CPU has {0} bytes of physical memory",CPU.physicalMemory.Length);
Console.WriteLine("OS has {0} bytes of virtual (addressable) memory",theOS.memoryMgr.virtualMemSize);
// For each file on the command line, load the program and create a process
for (int i = 1; i < args.Length; i++)
{
if (File.Exists(args[i]))
{
Program p = Program.LoadProgram(args[i]);
Process rp = theOS.createProcess(p, uint.Parse(Configuration["ProcessMemory"]));
Console.WriteLine("Process id {0} has {1} bytes of process memory and {2} bytes of heap",rp.PCB.pid,Configuration["ProcessMemory"],rp.PCB.heapAddrEnd-rp.PCB.heapAddrStart);
p.DumpProgram();
}
}
// Start executing!
theOS.execute();
}
//catch (Exception e)
{
//PrintInstructions();
//Console.WriteLine(e.ToString());
}
// Pause
Console.WriteLine("OS execution complete. Press Enter to continue...");
Console.ReadLine();
}
}
/// <summary>
/// Prints the static instructions on how to invoke from the command line
/// </summary>
private static void PrintInstructions()
{
Console.WriteLine("");
Console.WriteLine("usage: OS membytes [files]");
}
/// <summary>
/// Prints the static informatonal header
/// </summary>
private static void PrintHeader()
{
Console.WriteLine("Scott's CST352 Virtual Operating System");
Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().FullName);
Console.WriteLine("Copyright (C) Scott Hanselman 2002. All rights reserved." + System.Environment.NewLine);
}
}
}