Skip to content

Commit

Permalink
clear the stack data in every execution
Browse files Browse the repository at this point in the history
  • Loading branch information
darkzense committed Nov 19, 2023
1 parent 87a3770 commit 3dc85ae
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion system/cpu/execution_unit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cpu from "./components.js";
import { processInfo } from "../memory/data.js";
import { clearStackData, processInfo } from "../memory/data.js";
import { decodeInstruction } from "../assembler.js";
import { instructionSet } from "./arch.js";
import { Instruction, Executable } from "./interface.js";
Expand Down Expand Up @@ -57,6 +57,7 @@ function resetRegisters(): void {
}

resetSpOffset();
clearStackData();
}

function execute(instruction: Instruction): void {
Expand Down
6 changes: 4 additions & 2 deletions system/instructions/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export class Pop implements Executable {

const topOfStack = getDataByAddress("stack-data", cpu.stackPointer)!;
if (!topOfStack.value) {
throw new ReferenceError("There is no data at the address of the stack pointer.");
throw new ReferenceError(
"There is no data at the address of the stack pointer.",
);
}

let nextStackPointer = cpu.stackPointer.parseHex();
Expand All @@ -56,7 +58,7 @@ export class Pop implements Executable {
cpu.MAR = hexOperand;
const actualData = getDataByAddress("main-data", hexOperand)!;
// cpu.controlUnit = "IR(5-16)out, MARin, READ";

cpu.MDR = (+topOfStack.value).asHex16();
cpu.controlUnit = "SPout, MDRin, WRITE";
actualData.value = topOfStack.value;
Expand Down
14 changes: 13 additions & 1 deletion system/memory/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,16 @@ function getDataByVariable(tbodyId: string, variable: string) {
return getDataCell(tbodyId, VAR_COLUMN, variable);
}

export { getDataByAddress, getDataByVariable, processInfo };
function clearStackData() {
const stackTbody = <HTMLTableSectionElement>(
document.getElementById("stack-data")
);

for (let row of stackTbody.rows) {
const inputCells = row.getElementsByTagName("input");
inputCells[1].value = ""; // Variable
inputCells[2].value = ""; // Value
}
}

export { getDataByAddress, getDataByVariable, clearStackData, processInfo };

0 comments on commit 3dc85ae

Please sign in to comment.