Skip to content

Commit

Permalink
Separate LC3VMCore as a library to share common code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimcetin committed Nov 1, 2024
1 parent d043f11 commit 732ceba
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 384 deletions.
95 changes: 0 additions & 95 deletions .swiftpm/xcode/xcshareddata/xcschemes/lc3vm.xcscheme

This file was deleted.

15 changes: 12 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@
import PackageDescription

let package = Package(
name: "lc3vm",
name: "LC3VM",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.executable(name: "LC3VM", targets: ["LC3VM"]),
.library(name: "LC3VMCore", targets: ["LC3VMCore"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "lc3vm",
name: "LC3VM",
dependencies: [
.target(name: "LC3VMCore"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]
),
.target(
name: "LC3VMCore"
),
.testTarget(
name: "lc3vmTests",
dependencies: ["lc3vm"]
dependencies: ["LC3VMCore"]
),
]
)
147 changes: 0 additions & 147 deletions Sources/Instruction.swift

This file was deleted.

32 changes: 8 additions & 24 deletions Sources/LC3VM.swift → Sources/LC3VM/LC3VM.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//
// lc3vm.swift
// LC3VM.swift
// lc3vm
//
// Created by İbrahim Çetin on 9.10.2024.
//

import ArgumentParser
import Foundation
import LC3VMCore

/// The LC-3 virtual machine.
@main
Expand All @@ -17,26 +18,24 @@ struct LC3VM: AsyncParsableCommand {
var binary: URL

func run() async throws {
// Create the LC-3 hardware
let hardware = Hardware()

// Read the binary file and load it into memory
try hardware.readImage(binary)

// Set up the signal handler
signal(SIGINT) { handle_interrupt($0) }
// Disable input buffering
disable_input_buffering()

// Set condition register to zero which value is 010 (not 000)
hardware.updateConditionFlag(to: .zro)

// Set the program counter to the default starting location
hardware.updateRegister(.pc, with: Constant.pcStart)

// Start the LC-3 machine
hardware.isRunning = true

while hardware.isRunning {
let instruction = hardware.readNextInstruction()

switch instruction.opcode {
switch try instruction.opcode {
case .br:
try BR.execute(instruction)
case .add:
Expand Down Expand Up @@ -72,22 +71,7 @@ struct LC3VM: AsyncParsableCommand {
}
}

// Restore input buffering
restore_input_buffering()
}
}

/// The error type for the LC3 virtual machine.
enum LC3VMError: Error {
case badOpcode
case invalidInstruction
case invalidRegister
case invalidOpcode
case unableToReadImageFile
}

/// The LC-3 constant values.
enum Constant {
static let memorySize = 1 << 16
static let registerCount = 10
static let pcStart: UInt16 = 0x3000 // Default program counter location
}
6 changes: 3 additions & 3 deletions Sources/CHelpers.swift → Sources/LC3VMCore/CHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ import Foundation
var original_tio = termios()

@MainActor
func disable_input_buffering() {
public func disable_input_buffering() {
tcgetattr(STDIN_FILENO, &original_tio)
var new_tio = original_tio
new_tio.c_lflag = UInt(Int32(new_tio.c_lflag) & ~ICANON & ~ECHO)
tcsetattr(STDIN_FILENO, TCSANOW, &new_tio)
}

@MainActor
func restore_input_buffering() {
public func restore_input_buffering() {
tcsetattr(STDIN_FILENO, TCSANOW, &original_tio)
}

@MainActor
func handle_interrupt(_: Int32) {
public func handle_interrupt(_: Int32) {
restore_input_buffering()
print("")
exit(-2)
Expand Down
Loading

0 comments on commit 732ceba

Please sign in to comment.