jsos
college code for operating system fundamentals in js
git clone https://9o.is/git/jsos.git
disk.js
(5278B)
1 /* ------------
2 disk.js
3
4 Requires global.js.
5
6 Routines for the host CPU simulation, NOT for the OS itself.
7 In this manner, it's A LITTLE BIT like a hypervisor,
8 in that the Document envorinment inside a browser is the "bare metal" (so to speak) for which we write code
9 that hosts our client OS. But that analogy only goes so far, and the lines are blurred, because we are using
10 JavaScript in both the host and client environments.
11
12 This code references page numbers in the text book:
13 Operating System Concepts 8th editiion by Silberschatz, Galvin, and Gagne. ISBN 978-0-470-12872-5
14 ------------ */
15
16 /*
17 * The disk stores data.
18 *
19 * Total storage available is 16,384 bytes with
20 * 4 tracks with 8 sectors with 4 blocks with 64 bytes.
21 *
22 * Write and Read functions handle data as array, not string.
23 */
24 function disk() {
25
26 this.TRACKS = 4;
27 this.SECTORS = 8;
28 this.BLOCKS = 8;
29 this.BYTES = 64;
30
31 this.init = function() {
32 this.output();
33 };
34
35 /*
36 * Reads a single byte from a byte.
37 */
38 this.readByte = function(tsb,byte) {
39 if(this.invalidTSB(tsb) || this.invalidDiskByte(byte))
40 return undefined;
41
42 var data = this.read(tsb);
43 if(data == undefined)
44 return data;
45 return data[byte-1];
46 };
47
48 /*
49 * Reads data from a block.
50 */
51 this.read = function(tsb) {
52 if(this.invalidTSB(tsb))
53 return [];
54
55 var data = localStorage.getItem(tsb.toString());
56 if(data == undefined) return [];
57 return data.split(",");
58 };
59
60 /*
61 * Writes a single byte into a block.
62 */
63 this.writeByte = function(tsb, byte, data) {
64 if(this.invalidTSB(tsb) || !isByte(data))
65 return false;
66
67 var read = this.read(tsb);
68 read[byte-1] = data;
69 localStorage.setItem(tsb.toString(), read);
70 this.output();
71 return true;
72 };
73
74 /*
75 * Writes a sequence of bytes into a block and returns
76 * any data that didn't fit.
77 * @param clear erase everything in the block before writing.
78 */
79 this.write = function(tsb, data, clear) {
80 if(this.invalidTSB(tsb) || this.invalidDiskData(data))
81 return false;
82
83 if(clear) {
84 localStorage.setItem(tsb.toString(), "");
85 localStorage.setItem(tsb.toString(), data.slice(0,this.BYTES));
86 } else {
87 localStorage.setItem(tsb.toString(), this.read(tsb.toString()).concat(data.slice(0,this.BYTES)));
88 }
89 this.output();
90
91 if(data.length > this.BYTES)
92 return data.slice(this.BYTES, data.length);
93 else
94 return [];
95 };
96
97 /*
98 * Checks if byte is a valid location in storage.
99 */
100 this.invalidDiskByte = function(byte) {
101 return !isByte(byte) || byte > this.BYTES;
102 };
103
104 /*
105 * Checks if data is valid and can be stored.
106 */
107 this.invalidDiskData = function(data) {
108 for(i=0; i<data.length; i++) {
109 var dataHex = parseInt(data[i], 16);
110 if(!isByte(dataHex)) { return true; }
111 }
112 return false;
113 };
114
115 /*
116 * Checks if tsb object has invalid
117 * track, sector, block values.
118 */
119 this.invalidTSB = function(tsb) {
120 if(tsb == undefined) return true;
121
122 var t = tsb.track;
123 var s = tsb.sector;
124 var b = tsb.block;
125
126 if(t > this.TRACKS || t < 1 ||
127 s > this.SECTORS || s < 1 ||
128 b > this.BLOCKS || b < 1) return true;
129
130 return false;
131 };
132
133 this.emptyBlock = function() {
134 var data = [];
135 for(i=0; i<this.BYTES; i++)
136 data.push(0);
137 return data;
138 };
139
140 this.output = function() {
141 var total = this.BYTES * this.BLOCKS * this.SECTORS * this.TRACKS;
142 var available = 0;
143 var used = 0;
144
145 for(t=1; t<=this.TRACKS; t++)
146 for(s=1; s<=this.SECTORS; s++)
147 for(b=1; b<=this.BLOCKS; b++) {
148 var file = new File(new tsb(t,s,b));
149 if(file.available())
150 available += this.BYTES;
151 else
152 used += this.BYTES;
153 }
154
155 document.getElementById("diskTotal").innerHTML = Math.round((total/1000)*10)/10+" KB";
156 document.getElementById("diskUsed").innerHTML = Math.round((used/1000)*10)/10+" KB";
157 document.getElementById("diskAvailable").innerHTML = Math.round((available/1000)*10)/10+" KB";
158
159 var canvas = document.getElementById("diskChart");
160 var ctx = canvas.getContext('2d');
161
162 var myColor = ["#ffb78e","#8ED6FF"];
163 var myData = [used,available];
164
165 function getTotal(){
166 var myTotal = 0;
167 for (var j = 0; j < myData.length; j++) {
168 myTotal += (typeof myData[j] == 'number') ? myData[j] : 0;
169 }
170 return myTotal;
171 }
172
173 function plotData() {
174 var lastend = 0;
175 var myTotal = getTotal();
176
177 ctx.clearRect(0, 0, canvas.width, canvas.height);
178
179 for (var i = 0; i < myData.length; i++) {
180 ctx.fillStyle = myColor[i];
181 ctx.beginPath();
182 ctx.moveTo(200,150);
183 ctx.arc(200,150,150,lastend,lastend+
184 (Math.PI*2*(myData[i]/myTotal)),false);
185 ctx.lineTo(200,150);
186 ctx.fill();
187 lastend += Math.PI*2*(myData[i]/myTotal);
188 }
189 }
190
191 plotData();
192
193 };
194 };
195
196 /*
197 * Holds a track, sector, block value.
198 */
199 function tsb(t,s,b) {
200 this.track = t;
201 this.sector = s;
202 this.block = b;
203
204 this.toString = function() {
205 return this.track.toString()+
206 this.sector.toString()+
207 this.block.toString();
208 };
209 };