Skip to content

Commit

Permalink
ignored empty lines and implemented END
Browse files Browse the repository at this point in the history
  • Loading branch information
darkzense committed Nov 19, 2023
1 parent 93c1302 commit 7867c33
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
16 changes: 11 additions & 5 deletions system/assembler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { instructionSet, syntax } from "./cpu/arch.js";
import { getDataByAddress, getDataByVariable } from "./memory/data.js";
import { getDataByVariable } from "./memory/data.js";
import { getSourceCode } from "./editor.js";
import { Instruction } from "./cpu/interface.js";

Expand All @@ -9,6 +9,9 @@ const OPCODE_LENGTH = 4;
const PARAM_LENGTH = 11;

function parseInstruction(text: string) {
if (!text) {
return null;
}
let mnemonic = text.split(" ")[0].toUpperCase();
const operation = instructionSet[mnemonic];
if (!operation) {
Expand Down Expand Up @@ -52,6 +55,9 @@ function parseInstruction(text: string) {

function encodeInstruction(content: string) {
const ins = parseInstruction(content);
if (!ins) {
return "";
}
const code = ins.code.toString(2).padStart(OPCODE_LENGTH, "0");
const addressMode = ins.addressingMode.toString(2);
const operand = ins.operand.toString(2).padStart(PARAM_LENGTH, "0");
Expand Down Expand Up @@ -86,10 +92,10 @@ function decodeText(binaryInstruction: string) {

function makeObjectCode() {
const objCode: string[] = [];

for (const line of getSourceCode()) {
if (!line) {
objCode.push("");
const sourceCode = getSourceCode();
for (const [index, line] of sourceCode.entries()) {
if (index == sourceCode.length - 1 && !line.toLowerCase().includes("end")) {
throw new SyntaxError('Missing "END" instruction.');
}
const objSrc = encodeInstruction(line);
objCode.push(objSrc);
Expand Down
5 changes: 3 additions & 2 deletions system/cpu/arch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Push, Pop } from "../instructions/stack.js";
import { Add, Sub, Mul, Div } from "../instructions/arithmetic.js";
import { Executable } from "./interface.js";
import { End } from "../instructions/process.js";

const syntax: { [key: string]: RegExp } = {
addressable: /\s*([a-zA-Z]+)\s+(\d+)\s*/,
Expand Down Expand Up @@ -41,10 +42,10 @@ const instructionSet: {
type: "op",
executor: new Div(),
},
HALT: {
END: {
code: 6,
type: "op",
executor: null,
executor: new End(),
},
IN: {
code: 7,
Expand Down
7 changes: 6 additions & 1 deletion system/cpu/execution_unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ function fetch(): Instruction | null {
const counter = cpu.programCounter.parseHex();

if (counter == processInfo.objectCode.length) {
processInfo.endSignal = true;
return null;
}

const nextCounterHex = (counter + 1).asHex16();
cpu.aluFlags = "Z=0,Of=0";
cpu.aluFlags = "Z=0,O=0";
cpu.controlUnit = "PCout,MARin,ClrY,SetCin,ADD,Zin";
cpu.programCounter = cpu.aluZ = nextCounterHex;

const binInstruction = processInfo.objectCode[counter];
if (!binInstruction) {
return null;
}
const hexInstruction = binInstruction.parseBin().asHex16();
cpu.instructionRegister = cpu.MDR = hexInstruction;

Expand Down Expand Up @@ -58,6 +62,7 @@ function resetRegisters(): void {

resetSpOffset();
clearStackData();
processInfo.endSignal = false;
}

function execute(instruction: Instruction): void {
Expand Down
4 changes: 1 addition & 3 deletions system/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ function run() {
try {
resetRegisters();
processInfo.objectCode = makeObjectCode();
while (true) {
while (!processInfo.endSignal) {
const ins = fetch();
if (ins) {
execute(ins);
} else {
break;
}
}
} catch (e: any) {
Expand Down
4 changes: 2 additions & 2 deletions system/instructions/arithmetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function performOp(operation: (x: number, y: number) => number) {
const topOfStackAddress = cpu.stackPointer.parseHex();
if (topOfStackAddress == 0) {
throw new ReferenceError(
"There is not enough data in the stack to perform the operation."
"There is not enough data in the stack to perform the operation.",
);
}

Expand All @@ -24,7 +24,7 @@ function performOp(operation: (x: number, y: number) => number) {
cpu.aluFlags = "";
const result = operation(+nextOfStack.value, +topOfStack.value);
cpu.aluFlags += `Z=${+(result == 0)}`;
cpu.aluFlags += `, O=${+(result > MAX_WORD_VALUE)}`;
cpu.aluFlags += `,O=${+(result > MAX_WORD_VALUE)}`;

cpu.stackPointer = nextOfStackAddress;
nextOfStack.value = result.toString();
Expand Down
8 changes: 8 additions & 0 deletions system/instructions/process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Instruction, Executable } from "../cpu/interface.js";
import { processInfo } from "../memory/data.js";

export class End implements Executable {
run(_: Instruction) {
processInfo.endSignal = true;
}
}
1 change: 1 addition & 0 deletions system/memory/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const VAR_COLUMN = 1;

class ProcessInfo {
objectCode: string[] = [];
endSignal: boolean = false;
}

var processInfo = new ProcessInfo();
Expand Down

0 comments on commit 7867c33

Please sign in to comment.