Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add closure taking write API #75

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/MMIO/Register.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ extension Register {
#endif
}

@inlinable @inline(__always)
public func write<T>(_ body: (inout Value.Write) -> (T)) -> T {
var newValue = Value.Write(Value.Raw(0))
let returnValue = body(&newValue)
self.write(newValue)
return returnValue
}

@inlinable @inline(__always) @_disfavoredOverload
public func modify<T>(_ body: (Value.Read, inout Value.Write) -> (T)) -> T {
let value = self.read()
Expand Down
8 changes: 8 additions & 0 deletions Tests/MMIOFileCheckTests/Tests/TestRegisterWrite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,27 @@ let r64 = Register<R64>(unsafeAddress: 0x1000)
public func main8() {
r8.write(unsafeBitCast(0 as UInt8, to: R8.Write.self))
// CHECK: store volatile i8 0
r8.write { $0.raw.hi = 1 }
// CHECK: store volatile i8 -128
}

public func main16() {
r16.write(unsafeBitCast(1 as UInt16, to: R16.Write.self))
// CHECK: store volatile i16 1
r16.write { $0.raw.hi = 1 }
// CHECK: store volatile i16 -32768
}

public func main32() {
r32.write(unsafeBitCast(2 as UInt32, to: R32.Write.self))
// CHECK: store volatile i32 2
r32.write { $0.raw.hi = 1 }
// CHECK: store volatile i32 -2147483648
}

public func main64() {
r64.write(unsafeBitCast(3 as UInt64, to: R64.Write.self))
// CHECK: store volatile i64 3
r64.write { $0.raw.hi = 1 }
// CHECK: store volatile i64 -9223372036854775808
}