-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a feature flag called 'Interposable' which allows clients to replace the memory interface of register banks and registers with a custom interposer. Interposers must conform the `MMIOInterposer` protocol which has two simple requirements: `load(from:)` and `store(_:to:)`. Interposers can be used to unit test the correctness of drivers and other structures which abstract over MMIO operations. Internally MMIO uses a new type called MMIOTracingInterposer to test the interposer functionality. This type may become public API in the future to help clients test their code without needing to make their own type conforming to `MMIOInterposer`.
- Loading branch information
Showing
21 changed files
with
886 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//===----------------------------------------------------------*- swift -*-===// | ||
// | ||
// This source file is part of the Swift MMIO open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if FEATURE_INTERPOSABLE | ||
public protocol MMIOInterposer: AnyObject { | ||
// FIXME: Documentation is wrong | ||
/// Loads an instance of `value` from the address pointed to by pointer. | ||
func load<Value>( | ||
from pointer: UnsafePointer<Value> | ||
) -> Value where Value: FixedWidthInteger & UnsignedInteger & _RegisterStorage | ||
|
||
/// Stores an instance of `value` to the address pointed to by pointer. | ||
func store<Value>( | ||
_ value: Value, | ||
to pointer: UnsafeMutablePointer<Value> | ||
) where Value: FixedWidthInteger & UnsignedInteger & _RegisterStorage | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
Tests/MMIOInterposableTests/Extensions/StringInterpolation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//===----------------------------------------------------------*- swift -*-===// | ||
// | ||
// This source file is part of the Swift MMIO open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
extension String.StringInterpolation { | ||
mutating func appendInterpolation(hexNibble value: UInt8) { | ||
let ascii: UInt8 | ||
switch value { | ||
case 0..<10: | ||
ascii = UInt8(ascii: "0") + value | ||
case 10..<16: | ||
ascii = UInt8(ascii: "a") + (value - 10) | ||
default: | ||
preconditionFailure("Invalid hexNibble \(value)") | ||
} | ||
let character = Character(UnicodeScalar(ascii)) | ||
self.appendInterpolation(character) | ||
} | ||
|
||
mutating func appendInterpolation<Value>( | ||
hex value: Value, | ||
bytes size: Int? = nil | ||
) where Value: FixedWidthInteger { | ||
let valueSize = MemoryLayout<Value>.size | ||
precondition((size ?? 0) <= valueSize) | ||
let size = size ?? valueSize | ||
let sizeIsEven = size.isMultiple(of: 2) | ||
|
||
// Big endian so we can iterate from high to low byte | ||
var value = value.bigEndian | ||
|
||
let droppedBytes = valueSize - size | ||
value >>= 8 * droppedBytes | ||
|
||
self.appendLiteral("0x") | ||
for offset in 0..<size { | ||
if offset != 0, offset.isMultiple(of: 2) == sizeIsEven { | ||
self.appendLiteral("_") | ||
} | ||
let byte = UInt8(truncatingIfNeeded: value) | ||
let highNibble = byte >> 4 | ||
let lowNibble = byte & 0xf | ||
self.appendInterpolation(hexNibble: highNibble) | ||
self.appendInterpolation(hexNibble: lowNibble) | ||
value >>= 8 | ||
} | ||
} | ||
} |
Oops, something went wrong.