Skip to content

Commit

Permalink
Fix parsing issue with labels + bump patch
Browse files Browse the repository at this point in the history
  • Loading branch information
cogsandsquigs committed Jul 10, 2023
1 parent 138faa4 commit 0c769df
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
authors = ["Ian Pratt <[email protected]>"]
edition = "2021"
license = "MIT"
version = "0.1.2"
version = "0.1.3"
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ if ProcessInfo.processInfo.environment["NAND7400_LOCAL"] != nil {
} else {
FFIbinaryTarget = .binaryTarget(
name: "Nand7400FFI",
url: "https://github.com/cogsandsquigs/nand7400/releases/download/v0.1.2/Nand7400FFI.xcframework.zip",
checksum: "779628641feef26b40191dad961939fd655c1a9e5dd4ca5046ae095d9e0bc89e"
url: "https://github.com/cogsandsquigs/nand7400/releases/download/v0.1.3/Nand7400FFI.xcframework.zip",
checksum: "2c59fed7e0e8852ca9581825c5e5ef238e5a2c1bfa4ae4d4b0a7b6e6f91fb71c"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
849C7CD32A57406D002E8A21 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849C7CD22A57406D002E8A21 /* ContentView.swift */; };
849C7CD52A57406F002E8A21 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 849C7CD42A57406F002E8A21 /* Assets.xcassets */; };
849C7CD82A57406F002E8A21 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 849C7CD72A57406F002E8A21 /* Preview Assets.xcassets */; };
84F67B1D2A5B902E00AFC01D /* Nand7400 in Frameworks */ = {isa = PBXBuildFile; productRef = 84F67B1C2A5B902E00AFC01D /* Nand7400 */; };
84F67B232A5C559200AFC01D /* Nand7400 in Frameworks */ = {isa = PBXBuildFile; productRef = 84F67B222A5C559200AFC01D /* Nand7400 */; };
84F67B262A5C57EE00AFC01D /* Nand7400 in Frameworks */ = {isa = PBXBuildFile; productRef = 84F67B252A5C57EE00AFC01D /* Nand7400 */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -27,7 +28,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
84F67B1D2A5B902E00AFC01D /* Nand7400 in Frameworks */,
84F67B262A5C57EE00AFC01D /* Nand7400 in Frameworks */,
84F67B232A5C559200AFC01D /* Nand7400 in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -44,7 +46,6 @@
849C7CC42A57406C002E8A21 = {
isa = PBXGroup;
children = (
849C7CDE2A574080002E8A21 /* Packages */,
849C7CCF2A57406C002E8A21 /* Example */,
849C7CCE2A57406C002E8A21 /* Products */,
8414C62F2A5742C5007A5DFC /* Frameworks */,
Expand Down Expand Up @@ -78,13 +79,6 @@
path = "Preview Content";
sourceTree = "<group>";
};
849C7CDE2A574080002E8A21 /* Packages */ = {
isa = PBXGroup;
children = (
);
name = Packages;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand All @@ -102,7 +96,8 @@
);
name = Example;
packageProductDependencies = (
84F67B1C2A5B902E00AFC01D /* Nand7400 */,
84F67B222A5C559200AFC01D /* Nand7400 */,
84F67B252A5C57EE00AFC01D /* Nand7400 */,
);
productName = Example;
productReference = 849C7CCD2A57406C002E8A21 /* Example.app */;
Expand Down Expand Up @@ -133,7 +128,7 @@
);
mainGroup = 849C7CC42A57406C002E8A21;
packageReferences = (
84F67B1B2A5B902E00AFC01D /* XCRemoteSwiftPackageReference "nand7400" */,
84F67B242A5C57EE00AFC01D /* XCRemoteSwiftPackageReference "nand7400" */,
);
productRefGroup = 849C7CCE2A57406C002E8A21 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -222,6 +217,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
NAND7400_LOCAL = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
Expand Down Expand Up @@ -276,6 +272,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
NAND7400_LOCAL = YES;
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
Expand Down Expand Up @@ -308,11 +305,12 @@
PRODUCT_BUNDLE_IDENTIFIER = com.github.cogsandsquigs.XcodeExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "watchsimulator watchos macosx iphonesimulator iphoneos driverkit appletvsimulator appletvos";
SUPPORTED_PLATFORMS = "appletvos appletvsimulator iphoneos iphonesimulator macosx watchos watchsimulator";
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGETED_DEVICE_FAMILY = "1,2,3,4";
};
name = Debug;
};
Expand Down Expand Up @@ -341,11 +339,12 @@
PRODUCT_BUNDLE_IDENTIFIER = com.github.cogsandsquigs.XcodeExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "watchsimulator watchos macosx iphonesimulator iphoneos driverkit appletvsimulator appletvos";
SUPPORTED_PLATFORMS = "appletvos appletvsimulator iphoneos iphonesimulator macosx watchos watchsimulator";
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGETED_DEVICE_FAMILY = "1,2,3,4";
};
name = Release;
};
Expand Down Expand Up @@ -373,20 +372,24 @@
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
84F67B1B2A5B902E00AFC01D /* XCRemoteSwiftPackageReference "nand7400" */ = {
84F67B242A5C57EE00AFC01D /* XCRemoteSwiftPackageReference "nand7400" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/cogsandsquigs/nand7400";
requirement = {
branch = main;
kind = branch;
kind = exactVersion;
version = 0.1.2;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
84F67B1C2A5B902E00AFC01D /* Nand7400 */ = {
84F67B222A5C559200AFC01D /* Nand7400 */ = {
isa = XCSwiftPackageProductDependency;
productName = Nand7400;
};
84F67B252A5C57EE00AFC01D /* Nand7400 */ = {
isa = XCSwiftPackageProductDependency;
package = 84F67B1B2A5B902E00AFC01D /* XCRemoteSwiftPackageReference "nand7400" */;
package = 84F67B242A5C57EE00AFC01D /* XCRemoteSwiftPackageReference "nand7400" */;
productName = Nand7400;
};
/* End XCSwiftPackageProductDependency section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/cogsandsquigs/nand7400",
"state" : {
"branch" : "main",
"revision" : "fdcae2e9177c38eeea0919001aec07da456756d3"
"revision" : "add8cbefbd9e421a1a64b986aaee67d8bca59b24",
"version" : "0.1.2"
}
}
],
Expand Down
26 changes: 19 additions & 7 deletions nand7400-ffi/examples/XcodeExample/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@
import Nand7400
import SwiftUI

let assemblyConf = AssemblerConfig(
opcodes: [
Opcode(mnemonic: "nop", binary: 0x00, numArgs: 0),
Opcode(mnemonic: "lda", binary: 0x01, numArgs: 0),
Opcode(mnemonic: "ldb", binary: 0x02, numArgs: 0),
Opcode(mnemonic: "add", binary: 0x03, numArgs: 3),
Opcode(mnemonic: "jmp", binary: 0x04, numArgs: 2),
Opcode(mnemonic: "hlt", binary: 0xFF, numArgs: 0),
]
)

struct ContentView: View {
@State private var assemblyText = "// Write some assembly..."
@State private var currentBinary: Data = Data()
@State private var assembler = Assembler(config: AssemblerConfig(opcodes: []))
@State private var assembler = Assembler(config: assemblyConf)
@State private var errorMessage: String = ""
@State private var haveError = false

Expand All @@ -21,21 +32,22 @@ struct ContentView: View {
TextEditor(text: self.$assemblyText)
.font(Font.system(size:15).monospaced())
.padding(.top, 5)
.overlay(
RoundedRectangle(cornerRadius: 4)
.stroke(.blue, lineWidth: 2)
)

Divider()

Text(self.currentBinary.map {String(format: "%01x", $0)}.joined(separator:" "))
Text(self.currentBinary.map {String(format: "0x%02x", $0)}.joined(separator:" "))

}
.overlay(
RoundedRectangle(cornerRadius: 4)
.stroke(.blue, lineWidth: 2)
)


Button(action: {
do {
self.currentBinary = try assembler.assemble(source: assemblyText)
print(self.currentBinary.map {String(format: "%01x", $0)}.joined(separator:" "))
print(self.currentBinary.map {String(format: "0x%02x", $0)}.joined(separator:" "))
} catch AssemblerError.OpcodeDne(mnemonic: let mnemonic, span: _) {
self.errorMessage = "Opcode \'" + mnemonic + "\' does not exist!"
self.haveError = true
Expand Down
4 changes: 1 addition & 3 deletions nand7400/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ impl Assembler {
.trim_end_matches(':');

// Add the label to the symbol table.
self.symbols
// -1 because the length accounts for the first byte of the label
.insert(name.to_string(), ast.len() + LABEL_SIZE - 1);
self.symbols.insert(name.to_string(), ast.len()); // No +1 because `len` is 0-indexed.

// We don't insert it into the binary because it doesn't actually take up any space.
}
Expand Down
19 changes: 17 additions & 2 deletions nand7400/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn test_basic_assembly() {

assert_eq!(
result.unwrap(),
vec![0x00, 0x01, 0xCA, 0x04, 0x00, 0x07, 0x00, 0x03, 0x01, 0x02, 0x03, 0xFF]
vec![0x00, 0x01, 0xCA, 0x04, 0x00, 0x06, 0x00, 0x03, 0x01, 0x02, 0x03, 0xFF]
);
}

Expand All @@ -58,7 +58,22 @@ fn test_parse_comments() {

assert_eq!(
result.unwrap(),
vec![0x00, 0x01, 0xCA, 0x04, 0x00, 0x07, 0x00, 0x03, 0x01, 0x02, 0x03, 0xFF]
vec![0x00, 0x01, 0xCA, 0x04, 0x00, 0x06, 0x00, 0x03, 0x01, 0x02, 0x03, 0xFF]
);
}

/// Test if we can parse labels correctly.
#[test]
fn test_parse_labels() {
let mut assembler = get_assembler();

let file = include_str!("programs/simple_jump_label.asm");

let result = assembler.assemble(file);

assert_eq!(
result.unwrap(),
vec![0x04, 0x00, 0x03, 0x03, 0x01, 0x02, 0x03]
);
}

Expand Down
4 changes: 4 additions & 0 deletions nand7400/src/tests/programs/simple_jump_label.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
jmp LABEL

LABEL:
add 0x01 0x02 0x03

0 comments on commit 0c769df

Please sign in to comment.