+
Program 
+
+
+
+
+
 
+
+
+
+
+
 
@@ -225,6 +240,8 @@
output: document.getElementById('output-content'),
log: document.getElementById('log-content'),
contextSelector: document.getElementById('context-selector'),
+ contextEnabler0: document.getElementById('context-enabler0'),
+ contextEnabler1: document.getElementById('context-enabler1'),
fileSelector0: document.getElementById('file-selector0'),
fileSelector1: document.getElementById('file-selector1'),
playButton: document.getElementById('play-button'),
diff --git a/js/execution.js b/js/execution.js
index dc2aab4..c505793 100644
--- a/js/execution.js
+++ b/js/execution.js
@@ -20,7 +20,22 @@ class Execution {
Elements.output.innerHTML = '';
Elements.log.innerHTML = '';
- Module.reset(); // 2, [0, 1]);
+
+ let enable_ctx0 = document.getElementById("context-enabler0").checked;
+ let enable_ctx1 = document.getElementById("context-enabler1").checked;
+ const ctx_list = [];
+ if (enable_ctx0) {ctx_list.push(0)};
+ if (enable_ctx1) {ctx_list.push(1)};
+ console.log("enable list: ", ctx_list);
+ Module.reset(2, ctx_list);
+
+ // Module.reset(); // 2, [0, 1]);
+ // Module.reset(2, [0, 1]);
+ // Module.reset(2, [0]);
+ // Module.reset(2, [1]);
+
+
+
while (!Module.lockSimulator(100));
RegisterUtils.init(ctx);
MemoryUtils.init(ctx);
@@ -30,7 +45,8 @@ class Execution {
InstructionUtils.init(ctx);
InstructionUtils.highlightCurrentInstruction();
// Elements.contextSelector.selectedIndex = ctx;
-
+ stdout[ctx] = "";
+ stderr[ctx] = "";
} else {
InstructionUtils.init(ctx);
InstructionUtils.highlightCurrentInstruction();
diff --git a/js/index.js b/js/index.js
index 31c2699..3d4b284 100644
--- a/js/index.js
+++ b/js/index.js
@@ -64,18 +64,35 @@ async function main(fileInput = `Tests/${fileList[0]}`, ctx = null) {
const stream = FS.open('input_'+ctx+'.s', 'w+');
FS.write(stream, new Uint8Array(data), 0, data.byteLength, 0);
FS.close(stream);
- Execution.init(false, ctx);
- changeContext(ctx);
- Elements.contextSelector.selectedIndex = ctx;
- switch (ctx) {
- case 0:
- Elements.fileSelector0.value = fileInput;
- break;
- case 1:
- Elements.fileSelector1.value = fileInput;
- break;
- default:
+
+ // when upload file != enabled ctx, not change anything
+ let cur_ctx = document.getElementById("context-selector").value;
+ if (ctx == cur_ctx) {
+ Execution.init(false, ctx);
+ changeContext(ctx);
+
+
+ const ctx_list = []; // [0]
+ if (document.getElementById("context-enabler0").checked) {ctx_list.push(0)};
+ if (document.getElementById("context-enabler1").checked) {ctx_list.push(1)};
+ let new_ctx_idx = 0;
+ for (var i = 0; i < ctx_list.length; i++) {
+ if (ctx == ctx_list[i]) {new_ctx_idx = i; break;}
+ }
+ Elements.contextSelector.selectedIndex = new_ctx_idx;
+
+ // Elements.contextSelector.selectedIndex = ctx;
+ switch (ctx) {
+ case 0:
+ Elements.fileSelector0.value = fileInput;
+ break;
+ case 1:
+ Elements.fileSelector1.value = fileInput;
+ break;
+ default:
+ }
}
+
}
// Execution.init();
@@ -94,7 +111,155 @@ async function loadData(fileInput) {
}
}
+let prev_ctx_list = [0, 1];
+
+async function initModule() {
+
+ let enable_ctx0 = document.getElementById("context-enabler0").checked;
+ let enable_ctx1 = document.getElementById("context-enabler1").checked;
+ console.log("program 0 enabled: ", enable_ctx0);
+ console.log("program 1 enabled: ", enable_ctx1);
+
+ // TODO: add module reset when
+ const ctx_list = []; // [0]
+ if (enable_ctx0) {ctx_list.push(0)};
+ if (enable_ctx1) {ctx_list.push(1)};
+ Module.reset(2, ctx_list);
+
+ let cur_ctx = document.getElementById("context-selector").value;
+
+ // 1. Re-initialize the context selector options (remove disabled ctx)
+ Elements.contextSelector.innerHTML = '';
+ ctx_list.forEach(ctx_idx => {
+ const option = document.createElement("option");
+ if (ctx_idx == 0) programName = "Program 1";
+ else if (ctx_idx == 1) programName = "Program 2";
+
+ option.text = programName;
+ option.value = ctx_idx;
+ Elements.contextSelector.add(option);
+ });
+
+ // 2. switch the option of context selector to enabled one
+ // and switch the context to the enabled one
+
+ // 1) disable a ctx
+ if (prev_ctx_list.length > ctx_list.length) {
+ let diff_ctx = prev_ctx_list.filter(item => !ctx_list.includes(item))[0];
+ // From 1 => 0
+ if (ctx_list.length == 0) {
+ Elements.contextSelector.selectedIndex = -1;
+ // clean frontend
+ // Output
+ Elements.output.innerHTML = "";
+ Elements.log.innerHTML = "";
+
+ // Reg
+ Elements.generalReg.innerHTML = "";
+ Elements.specialReg.innerHTML = "";
+ Elements.floatReg.innerHTML = "";
+ Elements.doubleReg.innerHTML = "";
+
+ // Insn
+ Elements.userTextContent.innerHTML = "";
+ Elements.kernelTextContent.innerHTML = "";
+ Elements.kernelTextContainer.innerHTML = "";
+
+ // Mem
+ Elements.userData.innerHTML = "";
+ Elements.kernelData.innerHTML = "";
+ Elements.kernelDataContainer.innerHTML = "";
+ Elements.stack.innerHTML = "";
+ }
+ // From 2 => 1
+ else {
+ // diabled = selected => switch ctx to another
+ if (diff_ctx == cur_ctx) {
+ let new_ctx = ctx_list[0];
+ // Elements.contextSelector.selectedIndex = new_ctx;
+ Elements.contextSelector.selectedIndex = 0;
+
+ Execution.ctx = new_ctx;
+ if (Module.lockSimulator(100)) {
+ updateStdOut(new_ctx);
+ updateStdErr(new_ctx);
+ RegisterUtils.init(new_ctx);
+ MemoryUtils.init(new_ctx);
+ InstructionUtils.update(new_ctx);
+ Module.unlockSimulator();
+ }
+ }
+ // disable != selected
+ else {
+ Execution.ctx = cur_ctx;
+ if (Module.lockSimulator(100)) {
+ updateStdOut(cur_ctx);
+ updateStdErr(cur_ctx);
+ RegisterUtils.init(cur_ctx);
+ MemoryUtils.init(cur_ctx);
+ InstructionUtils.update(cur_ctx);
+ Module.unlockSimulator();
+ }
+ }
+ }
+
+ }
+ // enable
+ else if (prev_ctx_list.length < ctx_list.length) {
+ // From 1 => 2
+ let diff_ctx = ctx_list.filter(item => !prev_ctx_list.includes(item))[0];
+ let new_ctx = diff_ctx;
+ Execution.ctx = new_ctx;
+ console.log("enable ctx, change to new ctx: ", new_ctx);
+
+ let new_ctx_idx = 0;
+ for (var i = 0; i < ctx_list.length; i++) {
+ if (new_ctx == ctx_list[i]) {new_ctx_idx = i; break;}
+ }
+ Elements.contextSelector.selectedIndex = new_ctx_idx;
+ if (Module.lockSimulator(100)) {
+ updateStdOut(new_ctx);
+ updateStdErr(new_ctx);
+ RegisterUtils.init(new_ctx);
+ MemoryUtils.init(new_ctx);
+ InstructionUtils.update(new_ctx);
+ Module.unlockSimulator();
+ }
+ }
+
+ prev_ctx_list = ctx_list.slice();
+}
+
async function changeContext(ctx) {
+
+ var ctx_enabled = false;
+ if (ctx == 0) ctx_enabled = document.getElementById("context-enabler0").checked;
+ else if (ctx == 1) ctx_enabled = document.getElementById("context-enabler1").checked;
+
+ // If the ctx is not enabled, clean the front end
+ if (!ctx_enabled) {
+ // Output
+ Elements.output.innerHTML = "";
+ Elements.log.innerHTML = "";
+
+ // Reg
+ Elements.generalReg.innerHTML = "";
+ Elements.specialReg.innerHTML = "";
+ Elements.floatReg.innerHTML = "";
+ Elements.doubleReg.innerHTML = "";
+
+ // Insn
+ Elements.userTextContent.innerHTML = "";
+ Elements.kernelTextContent.innerHTML = "";
+ Elements.kernelTextContainer.innerHTML = "";
+
+ // Mem
+ Elements.userData.innerHTML = "";
+ Elements.kernelData.innerHTML = "";
+ Elements.kernelDataContainer.innerHTML = "";
+ Elements.stack.innerHTML = "";
+ }
+
console.log("change ctx from ", Execution.ctx, " to ", ctx);
Execution.ctx = ctx;
console.log("current ctx: ", Execution.ctx);
@@ -102,7 +267,9 @@ async function changeContext(ctx) {
// Module.pause();
// Execution.playing = false;
// Elements.playButton.innerHTML = "Continue";
- if (Module.lockSimulator(100)) {
+
+
+ if (ctx_enabled && Module.lockSimulator(100)) {
updateStdOut(Execution.ctx);
updateStdErr(Execution.ctx);
// RegisterUtils.update(Execution.ctx);
diff --git a/spim/spim.cpp b/spim/spim.cpp
index 0d100fd..7b72c40 100644
--- a/spim/spim.cpp
+++ b/spim/spim.cpp
@@ -259,13 +259,16 @@ val getSpecialRegVals(int ctx) {
return val(typed_memory_view(9, specialRegs));
}
-void reset_sim() { // unsigned int max_contexts, val active_contexts) {
- /* std::vector
arr = emscripten::vecFromJSArray(active_contexts); */
- /* std::set s(arr.begin(), arr.end()); */
- /* std::set s({0, 1}); */
- /* reset(2, s); */
- start_simulator(2, std::set{0, 1});
+
+void reset_sim(unsigned int max_contexts, const emscripten::val& arr) { // unsigned int max_contexts, val active_contexts) {
+ std::set ctx_set;
+
+ for (size_t i = 0; i < arr["length"].as(); i++) {
+ ctx_set.insert(arr[i].as());
+ }
+
+ start_simulator(max_contexts, ctx_set);
}
EMSCRIPTEN_BINDINGS(simulationSettings) { function("setDelay", &setDelay); }