From 44d7bda2aadf5379863ccabd7c876eeb118e62bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0brahim=20=C3=87etin?= Date: Wed, 18 Dec 2024 15:04:50 +0300 Subject: [PATCH] Refactor LC-3 machine execution into a separate run method --- Sources/LC3VM/LC3VM.swift | 43 ++--------------------------- Sources/LC3VMCore/Hardware.swift | 46 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 41 deletions(-) diff --git a/Sources/LC3VM/LC3VM.swift b/Sources/LC3VM/LC3VM.swift index 0953c96..67d5fcf 100644 --- a/Sources/LC3VM/LC3VM.swift +++ b/Sources/LC3VM/LC3VM.swift @@ -29,47 +29,8 @@ struct LC3VM: AsyncParsableCommand { // Disable input buffering disable_input_buffering() - // Start the LC-3 machine - hardware.isRunning = true - - while hardware.isRunning { - let instruction = hardware.readNextInstruction() - - switch try instruction.opcode { - case .br: - try BR.execute(instruction) - case .add: - try ADD.execute(instruction) - case .ld: - try LD.execute(instruction) - case .st: - try ST.execute(instruction) - case .jsr: - try JSR.execute(instruction) - case .and: - try AND.execute(instruction) - case .ldr: - try LDR.execute(instruction) - case .str: - try STR.execute(instruction) - case .rti: - try RTI.execute(instruction) - case .not: - try NOT.execute(instruction) - case .ldi: - try LDI.execute(instruction) - case .sti: - try STI.execute(instruction) - case .jmp: - try JMP.execute(instruction) - case .res: - try RES.execute(instruction) - case .lea: - try LEA.execute(instruction) - case .trap: - try TRAP.execute(instruction) - } - } + // Run the LC-3 machine + try hardware.run() // Restore input buffering restore_input_buffering() diff --git a/Sources/LC3VMCore/Hardware.swift b/Sources/LC3VMCore/Hardware.swift index ece32c3..1495152 100644 --- a/Sources/LC3VMCore/Hardware.swift +++ b/Sources/LC3VMCore/Hardware.swift @@ -151,6 +151,52 @@ public class Hardware { } } + /// Runs the LC-3 machine. + /// + /// - Throws: An error if the instruction is invalid. + public func run() throws { + isRunning = true + + while isRunning { + let instruction = readNextInstruction() + + switch try instruction.opcode { + case .br: + try BR.execute(instruction) + case .add: + try ADD.execute(instruction) + case .ld: + try LD.execute(instruction) + case .st: + try ST.execute(instruction) + case .jsr: + try JSR.execute(instruction) + case .and: + try AND.execute(instruction) + case .ldr: + try LDR.execute(instruction) + case .str: + try STR.execute(instruction) + case .rti: + try RTI.execute(instruction) + case .not: + try NOT.execute(instruction) + case .ldi: + try LDI.execute(instruction) + case .sti: + try STI.execute(instruction) + case .jmp: + try JMP.execute(instruction) + case .res: + try RES.execute(instruction) + case .lea: + try LEA.execute(instruction) + case .trap: + try TRAP.execute(instruction) + } + } + } + /// Initializes the LC-3 hardware. /// /// - Note: Sets the condition register to zero and the program counter to the default starting location.