+
+
+
diff --git a/src/state.ts b/src/state.ts
index b478cd8..849cf41 100644
--- a/src/state.ts
+++ b/src/state.ts
@@ -7,10 +7,9 @@ import AbacusEmulator from "./abacus/abacus.ts";
export interface GlobalState {
mode: 'edit' | 'run';
program: Program;
- emulator: AbacusEmulator;
}
-const defaultProgram: Program = {
+const defaultProgram = new Program({
name: 'New Program',
description: 'A new program',
operations: [
@@ -33,29 +32,40 @@ const defaultProgram: Program = {
new Register({ address: '102', value: '0000', comment: 'Registro 102' }),
new Register({ address: '103', value: '0000', comment: 'Registro 103' }),
],
-};
+ data_registers: [],
+});
// Function to load program from localStorage
function loadSavedProgram(): Program {
const savedProgram = localStorage.getItem('abacusProgram');
- if (savedProgram) {
- try {
- const parsed = JSON.parse(savedProgram);
- // Reconstruct Register objects
- parsed.aux_registers = parsed.aux_registers.map((r: any) => new Register(r));
- parsed.registers = parsed.registers.map((r: any) => new Register(r));
- parsed.operations = parsed.operations.map((o: any) => {
- const operation_type = getOperationTypeById(o.operation_type.id);
- if (!operation_type) {
- throw new Error(`Operation type ${o.operation_type.name} not found`);
- }
- return { code: o.code, operation_type };
- });
- return parsed;
- } catch (e) {
- console.error('Failed to parse saved program:', e);
- }
+ if (!savedProgram)
+ return defaultProgram;
+
+ try {
+ const parsed = JSON.parse(savedProgram);
+ // Reconstruct Register objects
+ parsed.aux_registers = parsed.aux_registers.map((r: any) => new Register(r));
+ parsed.registers = parsed.registers.map((r: any) => new Register(r));
+ parsed.operations = parsed.operations.map((o: any) => {
+ const operation_type = getOperationTypeById(o.operation_type.id);
+ if (!operation_type) {
+ throw new Error(`Operation type ${o.operation_type.name} not found`);
+ }
+ return { code: o.code, operation_type };
+ });
+ parsed.data_registers = parsed.data_registers.map((r: any) => new Register(r));
+ return new Program({
+ name: parsed.name,
+ description: parsed.description,
+ operations: parsed.operations,
+ aux_registers: parsed.aux_registers,
+ registers: parsed.registers,
+ data_registers: parsed.data_registers,
+ });
+ } catch (e) {
+ console.error('Failed to parse saved program:', e);
}
+
return defaultProgram;
}
@@ -72,7 +82,6 @@ watch(() => globalState.program, (newProgram) => {
export function toggleMode() {
globalState.mode = globalState.mode === 'edit' ? 'run' : 'edit';
- globalState.emulator.loadProgram(globalState.program);
}
export function resetProgram() {
diff --git a/src/style.css b/src/style.css
index bb131d6..e54dcda 100644
--- a/src/style.css
+++ b/src/style.css
@@ -47,7 +47,7 @@ button {
transition: border-color 0.25s;
}
button:hover {
- border-color: #646cff;
+ border-color: chocolate;
}
button:focus,
button:focus-visible {
@@ -74,6 +74,6 @@ button:focus-visible {
color: #747bff;
}
button {
- background-color: #f9f9f9;
+ background-color: #e6e6e6;
}
}
diff --git a/test/abacus.test.ts b/test/abacus.test.ts
index 7d713e3..2ab396f 100644
--- a/test/abacus.test.ts
+++ b/test/abacus.test.ts
@@ -11,7 +11,7 @@ Deno.test("AbacusEmulator should throw error if no program is loaded", () => {
});
Deno.test("AbacusEmulator should load a program and run without errors", () => {
- const program: Program = {
+ const program = new Program({
name: 'Test program',
description: 'Test program description',
registers: [
@@ -35,8 +35,9 @@ Deno.test("AbacusEmulator should load a program and run without errors", () => {
code: 'F',
operation_type: OperationTypes.END
}
- ]
- };
+ ],
+ data_registers: [],
+ });
const emulator = new AbacusEmulator();
emulator.loadProgram(program);
@@ -48,11 +49,11 @@ Deno.test("AbacusEmulator should load a program and run without errors", () => {
emulator.run();
// Ensure the execution finished correctly
- expect(emulator.current_address).toEqual('000');
+ expect(emulator.finished).toBeTruthy();
});
Deno.test("AbacusEmulator should load an immediate value into the accumulator", () => {
- const program: Program = {
+ const program = new Program({
name: 'Immediate Load',
description: 'Loads an immediate value into the accumulator',
registers: [
@@ -69,8 +70,9 @@ Deno.test("AbacusEmulator should load an immediate value into the accumulator",
code: 'F',
operation_type: OperationTypes.END
}
- ]
- };
+ ],
+ data_registers: [],
+ });
const emulator = new AbacusEmulator();
emulator.loadProgram(program);
@@ -86,7 +88,7 @@ Deno.test("AbacusEmulator should load an immediate value into the accumulator",
});
Deno.test("AbacusEmulator should load a value from a register into the accumulator", () => {
- const program: Program = {
+ const program = new Program({
name: 'Load Value',
description: 'Loads a value from a register into the accumulator',
registers: [
@@ -105,8 +107,9 @@ Deno.test("AbacusEmulator should load a value from a register into the accumulat
code: 'F',
operation_type: OperationTypes.END
}
- ]
- };
+ ],
+ data_registers: [],
+ });
const emulator = new AbacusEmulator();
emulator.loadProgram(program);
@@ -122,7 +125,7 @@ Deno.test("AbacusEmulator should load a value from a register into the accumulat
});
Deno.test("AbacusEmulator should load a value from aux register, negate it, and store it in register 201 using predefined operation types", () => {
- const program: Program = {
+ const program = new Program({
name: 'Negate and Store',
description: 'Loads a value from aux register, negates it, and stores it in register 201',
registers: [
@@ -150,8 +153,9 @@ Deno.test("AbacusEmulator should load a value from aux register, negate it, and
code: 'F',
operation_type: OperationTypes.END
}
- ]
- };
+ ],
+ data_registers: [],
+ });
const emulator = new AbacusEmulator();
emulator.loadProgram(program);
@@ -167,7 +171,7 @@ Deno.test("AbacusEmulator should load a value from aux register, negate it, and
});
Deno.test("AbacusEmulator should add values from two registers and store the result in a target register", () => {
- const program: Program = {
+ const program = new Program({
name: 'Add and Store',
description: 'Adds values from two registers and stores the result in a target register',
registers: [
@@ -193,8 +197,9 @@ Deno.test("AbacusEmulator should add values from two registers and store the res
code: 'F',
operation_type: OperationTypes.END
}
- ]
- };
+ ],
+ data_registers: [],
+ });
const emulator = new AbacusEmulator();
emulator.loadProgram(program);
@@ -210,7 +215,7 @@ Deno.test("AbacusEmulator should add values from two registers and store the res
});
Deno.test("AbacusEmulator should jump to a specific address if the value in the accumulator is zero", () => {
- const program: Program = {
+ const program = new Program({
name: 'Jump If Zero',
description: 'Jumps to a specific address if the value in the accumulator is zero',
registers: [
@@ -242,8 +247,9 @@ Deno.test("AbacusEmulator should jump to a specific address if the value in the
code: 'F',
operation_type: OperationTypes.END
}
- ]
- };
+ ],
+ data_registers: [],
+ });
const emulator = new AbacusEmulator();
emulator.loadProgram(program);
@@ -260,7 +266,7 @@ Deno.test("AbacusEmulator should jump to a specific address if the value in the
Deno.test("AbacusEmulator should jump to a specific address if the value in the accumulator is negative", () => {
- const program: Program = {
+ const program = new Program({
name: 'Example program',
description: 'Example program description',
registers: [
@@ -303,8 +309,9 @@ Deno.test("AbacusEmulator should jump to a specific address if the value in the
code: 'F',
operation_type: OperationTypes.END
}
- ]
- };
+ ],
+ data_registers: [],
+ });
const emulator = new AbacusEmulator();
emulator.loadProgram(program);
@@ -320,7 +327,7 @@ Deno.test("AbacusEmulator should jump to a specific address if the value in the
});
Deno.test("AbacusEmulator should jump to a specific address if the value in the accumulator is positive", () => {
- const program: Program = {
+ const program = new Program({
name: 'Jump If Positive',
description: 'Jumps to a specific address if the value in the accumulator is positive',
registers: [
@@ -363,8 +370,9 @@ Deno.test("AbacusEmulator should jump to a specific address if the value in the
code: 'F',
operation_type: OperationTypes.END
}
- ]
- };
+ ],
+ data_registers: [],
+ });
const emulator = new AbacusEmulator();
emulator.loadProgram(program);
@@ -380,7 +388,7 @@ Deno.test("AbacusEmulator should jump to a specific address if the value in the
});
Deno.test("AbacusEmulator should the execution when running the END operation", () => {
- const program: Program = {
+ const program = new Program({
name: 'Example program',
description: 'Example program description',
registers: [
@@ -392,8 +400,9 @@ Deno.test("AbacusEmulator should the execution when running the END operation",
code: 'F',
operation_type: OperationTypes.END
}
- ]
- };
+ ],
+ data_registers: [],
+ });
const emulator = new AbacusEmulator();
emulator.loadProgram(program);
@@ -405,5 +414,5 @@ Deno.test("AbacusEmulator should the execution when running the END operation",
emulator.run();
// Ensure the jump occurred correctly
- expect(emulator.current_address).toEqual('000');
+ expect(emulator.finished).toBeTruthy();
});
diff --git a/test/fixtures/example_program.csv b/test/fixtures/example_program.csv
index 5f2c2f9..4e03408 100644
--- a/test/fixtures/example_program.csv
+++ b/test/fixtures/example_program.csv
@@ -35,5 +35,7 @@ ADDR,VALUE,COMMENT
50B,32F7,Suma X
50C,2300,Almacena AC en 300
50D,FCCC,Fin de Program
+DATA,,
+ADDR,VALUE,COMMENT
150,0005,Valor X
250,0003,Valor Y
diff --git a/test/importer.test.ts b/test/importer.test.ts
index 97b8030..a692fca 100644
--- a/test/importer.test.ts
+++ b/test/importer.test.ts
@@ -34,7 +34,7 @@ Deno.test("ProgramImporter should correctly import a program from CSV", async ()
});
// Test program registers section
- expect(program.registers.length).toBe(16);
+ expect(program.registers.length).toBe(14);
expect(program.registers[0]).toEqual({
address: '500',
value: '12F5',
@@ -45,4 +45,17 @@ Deno.test("ProgramImporter should correctly import a program from CSV", async ()
value: 'FCCC',
comment: 'Fin de Program'
});
+
+ // Test data registers section
+ expect(program.data_registers.length).toBe(2);
+ expect(program.data_registers[0]).toEqual({
+ address: '150',
+ value: '0005',
+ comment: 'Valor X'
+ });
+ expect(program.data_registers[1]).toEqual({
+ address: '250',
+ value: '0003',
+ comment: 'Valor Y'
+ });
});