Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add arguments to reset() #9

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@

<nav class="navbar navbar-light bg-light" id="toolbar">
<div class="form-inline">
<div>Program&nbsp</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="context-enabler0" onchange="initModule()" checked>
<label class="form-check-label" for="enabled-ctx0">
1
</label>
</div>
<div>&nbsp</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="context-enabler1" onchange="initModule()" checked>
<label class="form-check-label" for="enabled-ctx1">
2
</label>
</div>
<div>&nbsp</div>
<div>
<select id="context-selector" onchange="changeContext(this.value)" class="form-control form-control"></select>
</div>
Expand Down Expand Up @@ -225,6 +240,8 @@ <h5 class="card-header">Log</h5>
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'),
Expand Down
20 changes: 18 additions & 2 deletions js/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code (not limited to this one instance)




while (!Module.lockSimulator(100));
RegisterUtils.init(ctx);
MemoryUtils.init(ctx);
Expand All @@ -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();
Expand Down
191 changes: 179 additions & 12 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -94,15 +111,165 @@ 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);

// 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);
Expand Down
15 changes: 9 additions & 6 deletions spim/spim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned int> arr = emscripten::vecFromJSArray<unsigned int>(active_contexts); */
/* std::set<unsigned int> s(arr.begin(), arr.end()); */
/* std::set<unsigned int> s({0, 1}); */

/* reset(2, s); */
start_simulator(2, std::set<unsigned int>{0, 1});

void reset_sim(unsigned int max_contexts, const emscripten::val& arr) { // unsigned int max_contexts, val active_contexts) {
std::set<unsigned int> ctx_set;

for (size_t i = 0; i < arr["length"].as<size_t>(); i++) {
ctx_set.insert(arr[i].as<unsigned int>());
}

start_simulator(max_contexts, ctx_set);
}

EMSCRIPTEN_BINDINGS(simulationSettings) { function("setDelay", &setDelay); }
Expand Down