implement HCF. Add HelloWorld. Cleanup
This commit is contained in:
parent
2e0b67b69e
commit
2207683040
BIN
HelloWorld.bin
Normal file
BIN
HelloWorld.bin
Normal file
Binary file not shown.
|
@ -85,7 +85,7 @@ export class Computer {
|
||||||
if (this.current_instr === null) {
|
if (this.current_instr === null) {
|
||||||
const parsed_instruction = Computer.parse_instruction(current_byte);
|
const parsed_instruction = Computer.parse_instruction(current_byte);
|
||||||
if (parsed_instruction === null) {
|
if (parsed_instruction === null) {
|
||||||
console.log("invalid instruction");
|
// console.log("invalid instruction");
|
||||||
this.step_forward();
|
this.step_forward();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -104,7 +104,6 @@ export class Computer {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.current_instr.params.length !== this.current_instr.params_found) {
|
if (this.current_instr.params.length !== this.current_instr.params_found) {
|
||||||
// console.log(`Parameter count not fulfilled. Found new parameter ${current_byte}`);
|
|
||||||
this.current_instr.params[this.current_instr.params_found] = current_byte;
|
this.current_instr.params[this.current_instr.params_found] = current_byte;
|
||||||
this.current_instr.params_found += 1;
|
this.current_instr.params_found += 1;
|
||||||
}
|
}
|
||||||
|
@ -137,12 +136,12 @@ export class Computer {
|
||||||
case Instr.Print: {
|
case Instr.Print: {
|
||||||
const [register_no] = inst.params;
|
const [register_no] = inst.params;
|
||||||
const value = this.registers[register_no];
|
const value = this.registers[register_no];
|
||||||
console.log(value);
|
// console.log(value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Instr.Goto: {
|
case Instr.Goto: {
|
||||||
const [parameter] = inst.params;
|
const [parameter] = inst.params;
|
||||||
console.log(`Goto ${parameter}`);
|
// console.log(`Goto ${parameter}`);
|
||||||
this.program_counter = parameter;
|
this.program_counter = parameter;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -159,7 +158,7 @@ export class Computer {
|
||||||
if (register_no >= this.registers.length) {
|
if (register_no >= this.registers.length) {
|
||||||
throw new Error(`Got register number ${register_no} in assign register`);
|
throw new Error(`Got register number ${register_no} in assign register`);
|
||||||
}
|
}
|
||||||
console.log(`Set register ${register_no} to ${new_value}`);
|
// console.log(`Set register ${register_no} to ${new_value}`);
|
||||||
this.registers[register_no] = new_value;
|
this.registers[register_no] = new_value;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -232,10 +231,18 @@ export class Computer {
|
||||||
|
|
||||||
const char = String.fromCharCode(ASCIIbyte);
|
const char = String.fromCharCode(ASCIIbyte);
|
||||||
|
|
||||||
console.log(char);
|
// console.log(char);
|
||||||
$("printout").textContent += char;
|
$("printout").textContent += char;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case Instr.HaltCatchFire: {
|
||||||
|
throw new Error("FIRE FIRE FIRE FIRE");
|
||||||
|
}
|
||||||
|
case Instr.CopyRegReg: {
|
||||||
|
const [register_no_to, register_no_from] = inst.params;
|
||||||
|
this.registers[register_no_to] = this.registers[register_no_from];
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ function main(): void {
|
||||||
|
|
||||||
const ui = new UI(container);
|
const ui = new UI(container);
|
||||||
|
|
||||||
const computer = new Computer(ui.stateUpdateEvent.bind(ui));
|
const computer = new Computer(ui.state_update_event.bind(ui));
|
||||||
|
|
||||||
computer.load_program(program);
|
computer.load_program(program);
|
||||||
ui.set_step_func(computer.cycle.bind(computer));
|
ui.set_step_func(computer.cycle.bind(computer));
|
||||||
|
|
62
src/ui.ts
62
src/ui.ts
|
@ -28,43 +28,39 @@ export class UI {
|
||||||
for (let i = 0; i < 8; i++) {
|
for (let i = 0; i < 8; i++) {
|
||||||
const reg_cell = el("div", `R_${i}`);
|
const reg_cell = el("div", `R_${i}`);
|
||||||
reg_cell.textContent = "00";
|
reg_cell.textContent = "00";
|
||||||
reg_cell.setAttribute("contenteditable", "true");
|
// reg_cell.setAttribute("contenteditable", "true");
|
||||||
reg_cell.setAttribute("spellcheck", "false");
|
// reg_cell.setAttribute("spellcheck", "false");
|
||||||
registers.appendChild(reg_cell);
|
registers.appendChild(reg_cell);
|
||||||
this.register_cells.push(reg_cell);
|
this.register_cells.push(reg_cell);
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line prefer-arrow-callback
|
// // eslint-disable-next-line prefer-arrow-callback
|
||||||
registers.addEventListener("input", function (e) {
|
// registers.addEventListener("input", function (e) {
|
||||||
const allowed_chars = "0123456789ABCDEFG";
|
// const allowed_chars = "0123456789ABCDEFG";
|
||||||
const r = e.target as HTMLElement;
|
// const r = e.target as HTMLElement;
|
||||||
let data = (r.textContent as string).toUpperCase();
|
// let data = (r.textContent as string).toUpperCase();
|
||||||
|
|
||||||
for (let i = 0; i < data.length; i++) {
|
// for (let i = 0; i < data.length; i++) {
|
||||||
if (!allowed_chars.includes(data[i])) {
|
// if (!allowed_chars.includes(data[i])) {
|
||||||
data = "00";
|
// data = "00";
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (data.length > 2) {
|
// e.preventDefault();
|
||||||
// data = r.textContent?.substring(0, 2) ?? "00";
|
// return false;
|
||||||
}
|
// });
|
||||||
e.preventDefault();
|
|
||||||
return false;
|
|
||||||
// r.textContent = ;
|
|
||||||
});
|
|
||||||
|
|
||||||
registers.addEventListener("keydown", (e) => {
|
// registers.addEventListener("keydown", (e) => {
|
||||||
if (e.key === "Enter") {
|
// if (e.key === "Enter") {
|
||||||
e.preventDefault();
|
// e.preventDefault();
|
||||||
(e.target as HTMLElement).blur();
|
// (e.target as HTMLElement).blur();
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
registers.addEventListener("blur", (e) => {
|
// registers.addEventListener("blur", (e) => {
|
||||||
const allowed_chars = "0123456789ABCDEFG";
|
// const allowed_chars = "0123456789ABCDEFG";
|
||||||
const r = e.target as HTMLElement;
|
// const r = e.target as HTMLElement;
|
||||||
const data = (r.textContent as string).toUpperCase();
|
// const data = (r.textContent as string).toUpperCase();
|
||||||
});
|
// });
|
||||||
|
|
||||||
this.registers = registers;
|
this.registers = registers;
|
||||||
|
|
||||||
|
@ -95,7 +91,7 @@ export class UI {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
start_auto(speed: number = 0): void {
|
start_auto(speed: number = 200): void {
|
||||||
if (this.step_func === null) {
|
if (this.step_func === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -125,7 +121,7 @@ export class UI {
|
||||||
this.step_func = f;
|
this.step_func = f;
|
||||||
}
|
}
|
||||||
|
|
||||||
stateUpdateEvent(state: ComputerState): void {
|
state_update_event(state: ComputerState): void {
|
||||||
for (let i = 0; i < 256; i++) {
|
for (let i = 0; i < 256; i++) {
|
||||||
const current = this.program_memory_cells[i];
|
const current = this.program_memory_cells[i];
|
||||||
current.className = "";
|
current.className = "";
|
||||||
|
|
Loading…
Reference in a new issue