jsos
college code for operating system fundamentals in js
git clone https://9o.is/git/jsos.git
control.js
(2955B)
1 /* ------------
2 Control.js
3
4 Requires global.js.
5
6 Routines for the hardware simulation, NOT for our client OS itself. In this manner, it's A LITTLE BIT like a hypervisor,
7 in that the Document envorinment inside a browser is the "bare metal" (so to speak) for which we write code that
8 hosts our client OS. But that analogy only goes so far, and the lines are blurred, because we are using JavaScript in
9 both the host and client environments.
10
11 This (and other host/simulation scripts) is the only place that we should see "web" code, like
12 DOM manipulation and JavaScript event handling, and so on. (Index.html is the only place for markup.)
13
14 This code references page numbers in the text book:
15 Operating System Concepts 8th editiion by Silberschatz, Galvin, and Gagne. ISBN 978-0-470-12872-5
16 ------------ */
17
18
19 /*
20 * Enables the canvas console with text functions.
21 */
22 function simCanvasInit() {
23 CANVAS = document.getElementById(CANVAS_ID);
24 DRAWING_CONTEXT = CANVAS.getContext('2d');
25 CanvasTextFunctions.enable(DRAWING_CONTEXT);
26 document.getElementById(TALOG).value="";
27 }
28
29 /*
30 * Prints a log in the taLog textarea showing the clock
31 * pulse, the source, the message, and time in ms.
32 *
33 * TODO: Update a log database or streaming device.
34 */
35 function simLog(msg, source) {
36 if (!source) source = "?";
37 var clock = _OSclock;
38 var now = new Date().getTime();
39
40 var str = "({ clock:" + clock + ", source:" + source +
41 ", msg:" + msg + ", now:" + now + " })" + "\n";
42
43 taLog = document.getElementById(TALOG);
44 taLog.value = str + taLog.value;
45 }
46
47 /*
48 * Starts the OS by intiating a cpu and starting the kernel.
49 *
50 * This should be called when the power-on or start button
51 * is clicked.
52 * <input type="button" onclick"simBtnStartOS_click(this)"/>
53 */
54 function simBtnStartOS_click(btn) {
55 simCanvasInit();
56 // Disable and enable elements appropriately.
57 btn.disabled = true;
58 document.getElementById(HALT_BTN).disabled = false;
59 document.getElementById(RESET_BTN).disabled = false;
60 document.getElementById(CANVAS_ID).focus();
61
62 _CPU = new cpu();
63 _Memory = new memory(TOTAL_MEMORY);
64 _Disk = new disk();
65 _CPU.init();
66 _Memory.init();
67 _Disk.init();
68 hardwareClockID = setInterval(simClockPulse, CPU_CLOCK_INTERVAL);
69 krnBootstrap();
70 }
71
72 /*
73 * Halts the OS by shuting down the kernel and clearing
74 * the clock.
75 * TODO: Is there anything else we need to do here?
76 *
77 * This should be called when the halt button is clicked.
78 * <input type="button" onclick"simBtnHaltOS_click(this)"/>
79 */
80 function simBtnHaltOS_click(btn) {
81 simLog("emergency halt", "host");
82 simLog("Attempting Kernel shutdown.", "host");
83
84 krnShutdown();
85 clearInterval(hardwareClockID);
86 }
87
88 /*
89 * Resets the OS by simply reloading the browser page document.
90 */
91 function simBtnReset_click(btn) {
92 location.reload(true);
93 //That boolean parameter is the 'forceget' flag.
94 }