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

Use an associated type to provide the bit width of a register #72

Open
fumoboy007 opened this issue Jan 14, 2024 · 2 comments
Open

Use an associated type to provide the bit width of a register #72

fumoboy007 opened this issue Jan 14, 2024 · 2 comments
Labels
mmio Related to the MMIO library

Comments

@fumoboy007
Copy link

fumoboy007 commented Jan 14, 2024

(I originally proposed this change here.)


Currently, the bit width of a register is specified as a parameter of the @Register macro:

@Register(bitWidth: 32)
struct CR1 {
  @ReadWrite(bits: 0..<1, as: Bool.self)
  var en: EN
}

I propose that we use an associated type to provide the bit width instead of a macro parameter. The associated type could be constrained to FixedWidthInteger & UnsignedInteger.

@Register
struct CR1 {
  typealias RawValue = UInt32

  @ReadWrite(bits: 0..<1, as: Bool.self)
  var en: EN
}

I think this would be more familiar to a Swift developer and it would also make the raw value type explicit.


I wonder if this could also allow the @RegisterBank macro to infer the offsets of the registers. (I haven’t developed a macro yet, so I don’t know the answer.)

Before:

@RegisterBank
struct Control {
  @RegisterBank(offset: 0x0)
  var cr1: Register<CR1>
  @RegisterBank(offset: 0x4)
  var cr2: Register<CR2>
}

After:

@RegisterBank
struct Control {
  var cr1: Register<CR1>
  var cr2: Register<CR2>
}
@fumoboy007 fumoboy007 changed the title Use an associated type to provide the bit width of the register Use an associated type to provide the bit width of a register Jan 14, 2024
@fumoboy007
Copy link
Author

fumoboy007 commented Jan 17, 2024

The associated type could be constrained to FixedWidthInteger & UnsignedInteger.

Or even better, the associated type could be constrained to a hypothetical VolatileRepresentable protocol that was brought up here.

@fumoboy007
Copy link
Author

I wonder if this could also allow the @RegisterBank macro to infer the offsets of the registers. (I haven’t developed a macro yet, so I don’t know the answer.)

Not feasible. A macro can only read the code that it is attached to.

@rauhul rauhul added the mmio Related to the MMIO library label Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mmio Related to the MMIO library
Projects
None yet
Development

No branches or pull requests

2 participants