-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
927 lines (814 loc) · 28.7 KB
/
script.js
File metadata and controls
927 lines (814 loc) · 28.7 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
// DOM Elements
const execute = document.getElementById("execute");
const result = document.getElementById("pcb-display");
const outputblock = document.getElementById("error");
const datablock = document.getElementById("datablock");
const memoryBlock = document.getElementById("memory");
const renew = document.getElementById("renew");
const instructionBlock = document.getElementById("instructionBlock");
const modalOverlay = document.getElementById("modalOverlay");
const instBtn = document.getElementById("instBtn");
const closeBtn = document.getElementById("close");
const jobCardBtn = document.getElementById("jobCardBtn");
const jobCardBlock = document.getElementById("jobCardBlock");
const jobCardCloseBtn = document.getElementById("jobCardClose");
const pauseBtn = document.getElementById("pauseBtn");
const resumeBtn = document.getElementById("resumeBtn");
const speedRange = document.getElementById("speedRange");
const speedValue = document.getElementById("speedValue");
const currentStage = document.getElementById("currentStage");
const executionLog = document.getElementById("executionLog");
var datacount = 0;
var executionDelay = 500;
var isPaused = false;
var executionQueue = [];
var isExecuting = false;
// Speed control
speedRange.addEventListener("input", (e) => {
executionDelay = parseInt(e.target.value);
speedValue.textContent = `${executionDelay}ms`;
});
// Pause/Resume controls
pauseBtn.addEventListener("click", () => {
isPaused = true;
pauseBtn.disabled = true;
resumeBtn.disabled = false;
addLog("⏸ Execution Paused", "log-action");
});
resumeBtn.addEventListener("click", () => {
isPaused = false;
pauseBtn.disabled = false;
resumeBtn.disabled = true;
addLog("▶ Execution Resumed", "log-success");
processNextStep();
});
// Modal controls
jobCardBtn.addEventListener("click", () => {
jobCardBlock.style.display = "block";
modalOverlay.style.display = "block";
});
jobCardCloseBtn.addEventListener("click", () => {
jobCardBlock.style.display = "none";
modalOverlay.style.display = "none";
});
instBtn.addEventListener("click", () => {
instructionBlock.style.display = "block";
modalOverlay.style.display = "block";
});
closeBtn.addEventListener("click", () => {
instructionBlock.style.display = "none";
modalOverlay.style.display = "none";
});
modalOverlay.addEventListener("click", () => {
instructionBlock.style.display = "none";
jobCardBlock.style.display = "none";
modalOverlay.style.display = "none";
});
renew.addEventListener("click", () => {
location.reload();
});
// Initialize memory blocks
let F = 0;
let L = 0;
while (F < 299) {
const divblock = document.createElement("div");
divblock.className = "byte-container";
memoryBlock.appendChild(divblock);
for (let j = 0; j < 40; j++) {
if (L % 4 == 0 && L != 0) {
F = F + 1;
L = 0;
}
const div = document.createElement("div");
div.className = "byte";
div.id = `${F}${L}`;
div.innerText = div.id;
L = L + 1;
divblock.appendChild(div);
}
}
// Utility Functions
function addLog(message, className = "") {
const logEntry = document.createElement("div");
logEntry.className = `log-entry ${className}`;
logEntry.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
executionLog.appendChild(logEntry);
executionLog.scrollTop = executionLog.scrollHeight;
}
function updateStage(stage) {
currentStage.textContent = stage;
addLog(stage, "log-stage");
}
function highlightMemoryBlock(start, end) {
for (let i = start; i <= end; i++) {
for (let j = 0; j < 4; j++) {
const cell = document.getElementById(`${i}${j}`);
if (cell) {
cell.classList.add("memory-highlight");
setTimeout(() => {
cell.classList.remove("memory-highlight");
}, 1000);
}
}
}
scrollToMemoryBlock(start);
}
function scrollToMemoryBlock(blockIndex) {
const cell = document.getElementById(`${blockIndex}0`);
if (cell) {
cell.scrollIntoView({ behavior: "smooth", block: "center" });
}
}
async function sleep(ms) {
return new Promise((resolve) => {
const checkPause = () => {
if (!isPaused) {
setTimeout(resolve, ms);
} else {
setTimeout(checkPause, 100);
}
};
checkPause();
});
}
var buffer = [];
class opsys {
constructor() {
this.MM = new Array(300).fill(null).map(() => Array(4).fill("-"));
this.IR = new Array(4).fill(" ");
this.R = new Array(40).fill(" ");
this.Ic = 0;
this.si = 0;
this.ti = 0;
this.pi = 0;
this.C = false;
this.buffer = "";
this.EM = 0;
this.ptr = 0;
this.rd = 0;
this.function = "";
this.opAdd = 0;
this.instructionCount = 0;
this.pcb = {
pid: 0,
ttl: 0,
tll: 0,
ttc: 0,
llc: 0,
};
}
printPcb() {
result.innerHTML = `
<div class="panel-title">Process Control Block</div>
<div class="pcb-grid">
<div class="pcb-item">
<div class="pcb-label">PROCESS ID</div>
<div class="pcb-value">${this.pcb.pid}</div>
</div>
<div class="pcb-item">
<div class="pcb-label">Instruction Count</div>
<div class="pcb-value">${this.instructionCount}</div>
</div>
<div class="pcb-item">
<div class="pcb-label">TIME LIMIT</div>
<div class="pcb-value">${this.pcb.ttl}</div>
</div>
<div class="pcb-item">
<div class="pcb-label">TIME COUNTER</div>
<div class="pcb-value">${this.pcb.ttc}</div>
</div>
<div class="pcb-item">
<div class="pcb-label">LINE LIMIT</div>
<div class="pcb-value">${this.pcb.tll}</div>
</div>
<div class="pcb-item">
<div class="pcb-label">LINE COUNTER</div>
<div class="pcb-value">${this.pcb.llc}</div>
</div>
</div>
`;
}
async Terminate() {
await sleep(executionDelay);
updateStage("🛑 Program Terminated");
this.printPcb();
pauseBtn.disabled = true;
resumeBtn.disabled = true;
isExecuting = false;
}
errmsg(EM) {
const errors = [
"NO Error - Execution completed successfully",
"Out Of Data - No input data available",
"Line Limit Exceeded - Program exceeded line limit",
"Time Limit Exceeded - Program exceeded time limit",
"Operation Code Error - Invalid instruction",
"Operand Error - Invalid operand value",
"Invalid Page Fault - Page fault error",
"Instruction Limit Exceeded - More than 10 instructions in a job",
];
outputblock.innerHTML = `
<div class="panel-title" style="color: ${
EM === 0 ? "var(--crt-primary)" : "var(--crt-error)"
};">
${EM === 0 ? "Status" : "Error Code " + EM}
</div>
<div style="margin-top: 10px;color: ${
EM === 0 ? "var(--crt-primary)" : "var(--crt-error)"
};"">${errors[EM]}</div>
`;
addLog(errors[EM], EM === 0 ? "log-success" : "log-error");
}
async Write() {
updateStage("📤 Writing Output Data (PD Operation)");
await sleep(executionDelay);
this.pcb.llc++;
addLog(`Line Counter incremented: ${this.pcb.llc}`, "log-action");
if (this.pcb.llc > this.pcb.tll) {
this.errmsg(2);
await this.Terminate();
throw new Error("Line limit exceeded");
}
let sadd = this.rd;
let eadd = sadd + 9;
var outdata = "";
addLog(`Reading data from memory blocks ${sadd} to ${eadd}`, "log-action");
highlightMemoryBlock(sadd, eadd);
await sleep(executionDelay);
for (let i = sadd; i <= eadd; i++) {
for (let j = 0; j < 4; j++) {
if (this.MM[i][j] !== "-" && this.MM[i][j] !== undefined) {
outdata = outdata + this.MM[i][j];
}
}
}
datablock.innerHTML = `
<div class="panel-title" style="color: var(--accent-blue);">Output Data</div>
<div style="margin-top: 10px;">${outdata}</div>
`;
addLog(`Output: "${outdata}"`, "log-data");
}
async Read(data) {
updateStage("📥 Reading Input Data (GD Operation)");
await sleep(executionDelay);
if (data.length === 0) {
this.errmsg(1);
await this.Terminate();
throw new Error("Out of data");
}
var row = this.rd;
var dataArray = [];
if(datacount >= data.length){
this.errmsg(1);
await this.Terminate();
throw new Error("Out of data");
}
addLog(`Reading data from input buffer: "${data[datacount]}"`, "log-data");
await sleep(executionDelay);
for (let i = 0; i < data[datacount].length; i++) {
dataArray.push(data[datacount].charAt(i));
}
if (data[datacount].length < 4) {
while (dataArray.length < 4) {
dataArray.push("-");
}
}
addLog(`Allocating memory blocks ${row} to ${row + 9}`, "log-action");
highlightMemoryBlock(row, row + 9);
await sleep(executionDelay);
for (let k = row; k <= row + 9; k++) {
for (let l = 0; l < 4; l++) {
document.getElementById(`${k}${l}`).title = `Allocated Memory (GD)`;
document.getElementById(`${k}${l}`).style.backgroundColor = "var(--memory-allocated)";
document.getElementById(`${k}${l}`).innerText = "-";
}
}
let si = 0;
if (data[datacount].length > 4) {
while (si < data[datacount].length) {
for (let i = si; i < si + 4; i++) {
dataArray.push(data[datacount].charAt(i));
}
this.MM[row] = dataArray;
for (let k = 0; k < 4; k++) {
const cell = document.getElementById(`${row}${k}`);
cell.style.backgroundColor = "var(--memory-data)";
cell.style.color = "white";
cell.innerHTML = dataArray[k];
cell.title = `Read and Stored Data (GD); Data : ${dataArray[k]}`;
cell.style.border = "1px solid var(--crt-primary)";
}
row++;
si = si + 4;
dataArray = [];
}
} else {
this.MM[row] = dataArray;
for (let k = 0; k < 4; k++) {
const cell = document.getElementById(`${row}${k}`);
cell.style.backgroundColor = "var(--memory-data)";
cell.style.color = "white";
cell.innerHTML = dataArray[k];
if (dataArray[k] !== "-") {
cell.title = `Allocated Memory (GD); Data : ${dataArray[k]}`;
}
cell.style.border = "1px solid var(--crt-primary)";
}
}
addLog(`Data stored successfully in memory`, "log-success");
datacount++;
}
isFree(fream_no) {
let esf = 0;
for (let i = fream_no * 10; i < fream_no * 10 + 9; i++) {
for (let j = 0; j < 4; j++) {
if (this.MM[i][j] !== "-") {
esf = 1;
break;
}
}
if (esf === 0) {
return true;
} else {
return false;
}
}
}
async MOS(data) {
updateStage("⚙️ Master Operating System (MOS) Handling Interrupt");
await sleep(executionDelay);
if (this.ti === 0 && this.si === 1) {
addLog("MOS: Service Interrupt - Read Operation", "log-action");
await this.Read(data);
} else if (this.ti === 0 && this.si === 2) {
addLog("MOS: Service Interrupt - Write Operation", "log-action");
await this.Write();
} else if (this.ti === 0 && this.si === 3 && this.pi === 0) {
addLog("MOS: Program terminated normally", "log-success");
this.errmsg(0);
await this.Terminate();
} else if (this.ti === 2 && this.si === 1) {
this.errmsg(3);
await this.Terminate();
throw new Error("Time limit exceeded");
} else if (this.ti === 2 && this.si === 2) {
await this.Write();
this.errmsg(3);
await this.Terminate();
throw new Error("Time limit exceeded");
} else if (this.ti === 2 && this.si === 3 && this.pi === 0) {
this.errmsg(0);
await this.Terminate();
} else if (this.ti === 0 && this.pi === 1) {
this.errmsg(4);
await this.Terminate();
throw new Error("Operation code error");
} else if (this.ti === 0 && this.pi === 2) {
this.errmsg(5);
await this.Terminate();
throw new Error("Operand error");
} else if (this.ti === 0 && this.pi === 3) {
if (this.function === "GD" || this.function === "SR") {
updateStage("🔧 Handling Page Fault - Allocating New Frame");
await sleep(executionDelay);
var fream_no = Math.floor(Math.random() * 30);
while (!this.isFree(fream_no)) {
fream_no = Math.floor(Math.random() * 30);
}
addLog(`Page Fault: Allocating frame ${fream_no}`, "log-action");
var progce = [];
progce.push("0");
progce.push("0");
progce.push(String(Math.floor(fream_no / 10)));
progce.push(String(fream_no % 10));
this.MM[this.ptr + this.opAdd / 10] = progce;
highlightMemoryBlock(this.ptr + this.opAdd / 10, this.ptr + this.opAdd / 10);
await sleep(executionDelay);
for (let k = 0; k < 4; k++) {
const cell = document.getElementById(`${this.ptr + this.opAdd / 10}${k}`);
cell.style.backgroundColor = "var(--memory-page-table)";
cell.style.color = "white";
cell.innerHTML = progce[k];
cell.style.border = "1px solid var(--crt-primary)";
}
this.Ic--;
addLog(`Page table updated successfully`, "log-success");
} else if (this.function === "PD") {
this.errmsg(6);
await this.Terminate();
throw new Error("Invalid page fault");
} else {
this.errmsg(4);
await this.Terminate();
throw new Error("Operation code error");
}
} else if (this.ti === 2 && this.pi === 1) {
this.errmsg(3);
this.errmsg(4);
await this.Terminate();
throw new Error("Time and operation error");
} else if (this.ti === 2 && this.pi === 2) {
this.errmsg(3);
this.errmsg(5);
await this.Terminate();
throw new Error("Time and operand error");
} else if (this.ti === 2 && this.pi === 3) {
this.errmsg(3);
await this.Terminate();
throw new Error("Time limit exceeded");
}
}
simulation() {
if (this.pcb.ttc > this.pcb.ttl) {
this.ti = 2;
addLog(`⏰ Time limit exceeded: ${this.pcb.ttc} > ${this.pcb.ttl}`, "log-error");
}
}
async addressMap(vr, data) {
updateStage(`🗺️ Address Translation: Virtual Address ${vr}`);
await sleep(executionDelay);
let pte;
let rd;
if (vr >= 0 && vr <= 99) {
pte = this.ptr + Math.floor(vr / 10);
addLog(`Address Mapping: VA=${vr}, Page Table Entry=${pte}`, "log-action");
highlightMemoryBlock(pte, pte);
await sleep(executionDelay);
if (this.MM[pte][0] === "*") {
addLog(`Page Fault detected for virtual address ${vr}`, "log-error");
this.pi = 3;
return -1;
} else {
const sfno = this.MM[pte].join("");
const fno = parseInt(sfno);
rd = fno * 10 + (vr % 10);
addLog(`Physical Address: ${rd} (Frame: ${fno})`, "log-success");
return rd;
}
} else {
addLog(`Invalid operand address: ${vr}`, "log-error");
this.pi = 2;
await this.MOS(data);
}
}
async executeuserprogram(data) {
updateStage("🚀 Starting User Program Execution");
await sleep(executionDelay);
while (true) {
this.si = 3;
this.pi = 0;
this.ti = 0;
updateStage(`📍 Fetching Instruction at IC=${this.Ic}`);
await sleep(executionDelay);
const ra = await this.addressMap(this.Ic, data);
if (ra === -1) {
await this.Terminate();
break;
}
highlightMemoryBlock(ra, ra);
console.log(ra);
await sleep(executionDelay);
this.IR = this.MM[ra];
this.Ic++;
let firstElement = this.IR[0];
let secondElement = this.IR[1];
this.function = firstElement + secondElement;
const instructionStr = this.IR.join("");
addLog(`Instruction Fetched: ${instructionStr} (${this.function})`, "log-data");
await sleep(executionDelay);
let operandAdd;
if (this.IR[0] !== "H") {
let tenthPlace = this.IR[2];
let unitplace = this.IR[3];
var oprand = tenthPlace + unitplace;
if (!/[0-9]/.test(tenthPlace) || !/[0-9]/.test(unitplace)) {
this.pi = 2;
await this.MOS(data);
break;
}
this.opAdd = parseInt(oprand);
addLog(`Operand: ${this.opAdd}`, "log-data");
operandAdd = await this.addressMap(this.opAdd);
}
if (operandAdd === -1 && this.IR[0] !== "H") {
this.pi = 3;
await this.MOS(data);
continue;
}
if (this.function === "LR") {
updateStage("📥 Load Register (LR) - Loading data to register");
await sleep(executionDelay);
highlightMemoryBlock(operandAdd, operandAdd + 9);
await sleep(executionDelay);
let r_count = 0;
for (let i = operandAdd; i <= operandAdd + 9; i++) {
for (let j = 0; j < 4; j++) {
this.R[r_count++] = this.MM[i][j];
}
}
var gpr = "";
for (let i = 0; i < r_count; i++) {
if (this.R[i] !== "-" && this.R[i] !== undefined) {
gpr = gpr + this.R[i];
}
}
document.getElementById("Rregister").innerText = gpr;
addLog(`Register loaded with: "${gpr}"`, "log-success");
this.si = 0;
this.pcb.ttc++;
} else if (this.function === "SR") {
updateStage("💾 Store Register (SR) - Storing register to memory");
await sleep(executionDelay);
let r_count = 0;
var dataArray = [];
highlightMemoryBlock(operandAdd, operandAdd + 9);
await sleep(executionDelay);
for (let i = operandAdd; i <= operandAdd + 9; i++) {
for (let j = 0; j < 4; j++) {
document.getElementById(`${i}${j}`).title = "Allocated Memory (SR)";
document.getElementById(`${i}${j}`).style.backgroundColor = "var(--memory-allocated)";
dataArray.push(this.R[r_count]);
if (this.R[r_count] !== "-" && this.R[r_count] !== undefined) {
document.getElementById(`${i}${j}`).title = "Stored Data (SR); Data : " + this.R[r_count];
document.getElementById(`${i}${j}`).style.backgroundColor = "var(--memory-data)";
}
document.getElementById(`${i}${j}`).innerText = this.R[r_count];
r_count++;
}
this.MM[i] = dataArray;
dataArray = [];
}
addLog(`Register data stored to memory blocks ${operandAdd}-${operandAdd + 9}`, "log-success");
this.si = 0;
this.pcb.ttc += 2;
} else if (this.function === "CR") {
updateStage("🔍 Compare Register (CR) - Comparing register with memory");
await sleep(executionDelay);
highlightMemoryBlock(operandAdd, operandAdd + 9);
await sleep(executionDelay);
let r_count = 0;
let flag = 0;
for (let i = operandAdd; i <= operandAdd + 9; i++) {
for (let j = 0; j < 4; j++) {
if (this.MM[i][j] !== this.R[r_count++]) {
flag = 1;
break;
}
}
}
if (flag === 0) {
this.C = true;
addLog("Comparison: EQUAL - Toggle set to TRUE", "log-success");
} else {
this.C = false;
addLog("Comparison: NOT EQUAL - Toggle set to FALSE", "log-action");
}
this.pcb.ttc++;
this.si = 0;
} else if (this.function === "BT") {
updateStage("🔀 Branch if True (BT) - Conditional jump");
await sleep(executionDelay);
if (this.C === true) {
this.Ic = parseInt(this.IR[3]);
addLog(`Branch taken: IC set to ${this.Ic}`, "log-action");
} else {
addLog("Branch not taken: Toggle is FALSE", "log-action");
continue;
}
this.si = 0;
this.pcb.ttc++;
} else if (this.function === "GD") {
updateStage("📥 Get Data (GD) - Reading input");
await sleep(executionDelay);
this.si = 1;
this.rd = operandAdd;
this.pcb.ttc = this.pcb.ttc + 2;
} else if (this.function === "PD") {
updateStage("📤 Put Data (PD) - Writing output");
await sleep(executionDelay);
this.si = 2;
this.pcb.ttc++;
this.rd = operandAdd;
} else if (this.IR[0] === "H") {
updateStage("🛑 Halt (H) - Program termination requested");
await sleep(executionDelay);
addLog("HALT instruction encountered", "log-action");
this.si = 3;
this.pcb.ttc++;
this.simulation();
await this.MOS(data);
break;
} else {
this.si = 0;
this.pi = 1;
addLog(`Invalid operation code: ${this.function}`, "log-error");
}
this.simulation();
await this.MOS(data);
// Update PCB display after each instruction
this.printPcb();
}
}
async startexecution(data) {
updateStage("🎯 Initializing Program Execution");
await sleep(executionDelay);
this.Ic = 0;
addLog(`Instruction Counter initialized: IC=${this.Ic}`, "log-action");
await this.executeuserprogram(data);
}
async Allocatefream() {
updateStage("🗃️ Allocating Page Table Frame");
await sleep(executionDelay);
let ptsi;
while (true) {
const fream_no = Math.floor(Math.random() * 30);
if (this.isFree(fream_no)) {
ptsi = fream_no * 10;
addLog(`Page Table allocated at frame ${fream_no} (block ${ptsi})`, "log-success");
highlightMemoryBlock(ptsi, ptsi + 9);
await sleep(executionDelay);
for (let i = ptsi; i <= ptsi + 9; i++) {
document.getElementById(`${i}0`).innerText = "#";
document.getElementById(`${i}0`).title = "Page Table Entry";
document.getElementById(`${i}1`).innerText = "#";
document.getElementById(`${i}1`).title = "Page Table Entry";
document.getElementById(`${i}2`).innerText = "#";
document.getElementById(`${i}2`).title = "Page Table Entry";
document.getElementById(`${i}3`).innerText = "#";
document.getElementById(`${i}3`).title = "Page Table Entry";
}
var star = ["*", "*", "*", "*"];
for (let i = ptsi; i <= ptsi + 9; i++) {
document.getElementById(`${i}0`).style.backgroundColor = "var(--memory-page-table)";
document.getElementById(`${i}1`).style.backgroundColor = "var(--memory-page-table)";
document.getElementById(`${i}2`).style.backgroundColor = "var(--memory-page-table)";
document.getElementById(`${i}3`).style.backgroundColor = "var(--memory-page-table)";
this.MM[i] = star;
}
break;
} else {
continue;
}
}
this.ptr = ptsi;
addLog(`Page Table Register (PTR) set to ${this.ptr}`, "log-action");
}
async Load() {
let i = 0;
while (i < buffer.length) {
var line = buffer[i++];
var cardID = line.substr(0, 4);
if (cardID === "$AMJ") {
updateStage("📋 Loading Job Card");
await sleep(executionDelay);
await this.Allocatefream();
const pid = line.substr(4, 4);
const timelimit = line.substr(8, 4);
const linelimit = line.substr(12, 4);
this.pcb.pid = parseInt(pid);
this.pcb.ttl = parseInt(timelimit);
this.pcb.tll = parseInt(linelimit);
this.pcb.llc = 0;
this.pcb.ttc = 0;
addLog(`Job Card Loaded - PID: ${this.pcb.pid}, Time Limit: ${this.pcb.ttl}, Line Limit: ${this.pcb.tll}`, "log-success");
await sleep(executionDelay);
continue;
} else if (cardID === "$DTA") {
updateStage("📊 Loading Data Section");
await sleep(executionDelay);
var databuffer = [];
if (buffer[i].substr(0, 4) === "$END") {
this.errmsg(1);
await this.Terminate();
break;
}
while (true) {
if (buffer[i].substr(0, 4) === "$END") {
break;
}
databuffer.push(buffer[i++]);
}
addLog(`Data buffer loaded with ${databuffer.length} entries`, "log-success");
await sleep(executionDelay);
await this.startexecution(databuffer);
} else if (cardID === "$END") {
updateStage("✅ Job Card End Marker Found");
await sleep(executionDelay);
addLog("Job processing completed", "log-success");
break;
} else {
updateStage("📝 Loading Program Instructions");
await sleep(executionDelay);
let fream_no = Math.floor(Math.random() * 29);
let progei = fream_no;
var progce = [];
progce.push("0");
progce.push("0");
progce.push(String(Math.floor(progei / 10)));
progce.push(String(progei % 10));
this.MM[this.ptr] = progce;
highlightMemoryBlock(this.ptr, this.ptr);
await sleep(executionDelay);
for (let x = 0; x < 4; x++) {
document.getElementById(`${this.ptr}${x}`).innerHTML = "";
document.getElementById(`${this.ptr}${x}`).innerHTML = progce[x];
}
for (let i = 0; i <= 3; i++) {
document.getElementById(`${this.ptr}${i}`).style.backgroundColor = "var(--memory-page-table)";
}
addLog(`Page table entry created pointing to frame ${progei}`, "log-action");
await sleep(executionDelay);
var inst = line;
var row = progei * 10;
var singleInstruction = [];
addLog(`Loading instructions to memory blocks ${row}-${row + 9}`, "log-action");
highlightMemoryBlock(row, row + 9);
await sleep(executionDelay);
for (let k = row; k <= row + 9; k++) {
for (let l = 0; l < 4; l++) {
document.getElementById(`${k}${l}`).innerHTML = "-";
document.getElementById(`${k}${l}`).title = "Instruction Memory Allocated";
document.getElementById(`${k}${l}`).style.backgroundColor = "var(--memory-instructions)";
}
}
for (let j = 0; j < inst.length; j++) {
if (inst.charAt(j) === "H") {
this.MM[row] = ["H", "-", "-", "-"];
for (let i = 0; i <= 3; i++) {
document.getElementById(`${row}${i}`).style.backgroundColor = "var(--memory-instructions)";
document.getElementById(`${row}${i}`).innerHTML = "";
if (i > 0) {
document.getElementById(`${row}${i}`).innerHTML = "-";
} else {
document.getElementById(`${row}${i}`).innerHTML = "H";
document.getElementById(`${row}${i}`).title = "Instruction Memory (User Program)";
}
}
row++;
singleInstruction = [];
this.instructionCount++;
} else {
singleInstruction.push(inst.charAt(j));
if (singleInstruction.length == 4) {
this.MM[row] = singleInstruction;
for (let k = 0; k < 4; k++) {
document.getElementById(`${row}${k}`).style.backgroundColor = "var(--memory-instructions)";
document.getElementById(`${row}${k}`).innerHTML = "";
document.getElementById(`${row}${k}`).innerHTML = singleInstruction[k];
document.getElementById(`${row}${k}`).title = "Instruction Memory (User Program)";
}
row++;
singleInstruction = [];
this.instructionCount++;
}
}
}
if(this.instructionCount>10){
addLog(`Instruction limit exceeded: ${this.instructionCount} > 10`, "log-error");
this.errmsg(7);
await this.Terminate();
throw new Error("Instruction limit exceeded");
}
addLog(`Instructions loaded: ${inst}`, "log-data");
await sleep(executionDelay);
}
}
}
}
execute.addEventListener("click", processParagraph);
async function processParagraph() {
if (isExecuting) {
addLog("⚠️ Execution already in progress", "log-error");
return;
}
var inputParagraph = document.getElementById("paragraphInput").value;
if (!inputParagraph.trim()) {
addLog("⚠️ No input provided", "log-error");
return;
}
var lines = inputParagraph.split("\n");
buffer = [];
for (var i = 0; i < lines.length; i++) {
var trimmedLine = lines[i].trim();
if (trimmedLine !== "") {
buffer.push(trimmedLine);
}
}
isExecuting = true;
pauseBtn.disabled = false;
resumeBtn.disabled = true;
executionLog.innerHTML = "";
addLog("🚀 System Initialized", "log-success");
addLog("Starting execution...", "log-stage");
try {
const vos = new opsys();
await vos.Load();
} catch (e) {
addLog(`❌ Execution terminated: ${e.message}`, "log-error");
console.log("Execution terminated:", e.message);
} finally {
isExecuting = false;
pauseBtn.disabled = true;
resumeBtn.disabled = true;
}
}