jsos
college code for operating system fundamentals in js
git clone https://9o.is/git/jsos.git
memory.js
(3335B)
1 /* ------------
2 CPU.js
3
4 Requires global.js.
5
6 Core Memory implementation.
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 core memory has an array to save bytes of data.
18 * Bytes will be formatted as integers of 0-255, but
19 * outputs in hex format.
20 */
21 function memory(maxBytes) {
22
23 /* The main container of bytes in memory. */
24 this.memory = new Array();
25
26 /*
27 * Whether it should update the output when
28 * a byte is set in memory.
29 */
30 var showOuput = true;
31
32 /* Initializes the memory with zeros. */
33 this.init = function() {
34 for(var i=0; i<maxBytes; i++) {
35 this.memory[i] = 0;
36 }
37 this.output();
38 }
39
40 /*
41 * Allocates an array of bytes starting from base.
42 */
43 this.allocateBytes = function(bytes, base) {
44 this.showOutput = false;
45
46 for(var i=0; i<bytes.length; i++) {
47 this.setByte(bytes[i], base+i);
48 }
49
50 this.showOutput = true;
51 this.output();
52 }
53
54 /*
55 * Deallocates an array of bytes starting from base.
56 */
57 this.deallocateBytes = function(base, limit) {
58 var result = [];
59 this.showOutput = false;
60
61 for(var i=base; i<=limit; i++) {
62 result[i-base] = this.getByte(i)
63 this.setByte(0, i);
64 }
65
66 this.showOutput = true;
67 this.output();
68
69 return result;
70 }
71
72 /*
73 * Gets byte value in a location.
74 */
75 this.getByte = function(location) {
76 if(!this.isValidLocation(location))
77 krnTrapError(_ERROR_STACK_OVERFLOW);
78 return this.memory[location];
79 }
80
81 /*
82 * Allocate a byte in a location in memory.
83 */
84 this.setByte = function(byte, location) {
85 if(!isByte(byte))
86 krnTrapError(_ERROR_INVALID_BYTE_VALUE);
87 if(!this.isValidLocation(location))
88 krnTrapError(_ERROR_STACK_OVERFLOW);
89 this.memory[location] = byte;
90
91 if(showOuput) this.output();
92 }
93
94 /* Memory dump. */
95 this.dumpMemory = function() {
96 return this.memory;
97 }
98
99 /*
100 * Outputs memory dump to interface.
101 */
102 this.output = function() {
103
104 // 8 columns
105 function extract(dump) {
106 var i=0;
107 var html = "";
108 for(var j=0; j < dump.length; j++) {
109 if(i == 8) {
110 i=0;
111 html += '</tr>';
112 if(j < dump.length)
113 html += '<tr><th align="right">'+
114 pad(j.toString(16),4).toUpperCase()+'</th>';
115 }
116 i++;
117 html += '<td>'+
118 pad(dump[j].toString(16),2).toUpperCase()+'</td>';
119 }
120 return html;
121 }
122
123 var dump = this.dumpMemory();
124 var html = '<tr><th>location</th></tr><tr>';
125 html += '<th align="right">0000</th>';
126 html += extract(dump);
127 html += '</tr>';
128 document.
129 getElementById("memoryDumpTable").innerHTML = html;
130 }
131
132 /*
133 * Checks if the location address is valid.
134 */
135 this.isValidLocation = function(location) {
136 return location > -1 && location < maxBytes;
137 }
138 }