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

Merge properties of Raw struct into Read/Write structs #71

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

Merge properties of Raw struct into Read/Write structs #71

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

Comments

@fumoboy007
Copy link

(I originally proposed this change here.)


The @Register macro generates 3 structs: Read, Write, and Raw. The Raw struct is accessed through the .raw property of the Read and Write structs:

@Register(bitWidth: 32)
struct CR1 {
  @ReadOnly(bits: 0..<1, as: Bool.self)
  var en: EN
  @WriteOnly(bits: 1..<2, as: Bool.self)
  var clken: CLKEN
}

cr1.modify { (read, write) in
  print(read.raw.en)
  write.raw.clken = 1
}

One issue with this approach is that the generated Raw struct is reused by both the Read and Write structs, which means it needs to contain all properties of both structs. However, that allows a developer to write something like write.raw.en = 1, which is unsafe because en is supposed to be read-only.

Instead, I propose that the properties of the Raw struct be merged into the Read/Write structs. The new properties could be prefixed with $, similar to property wrappers, to denote that the properties are just a different view on the non-prefixed properties.

cr1.modify { (read, write) in
  print(read.$en)
  write.$clken = 1

  write.$en = 1  // compile error: `$en` does not exist
}

(If #70 is accepted, this change would also make the Read/Write structs less awkward by avoiding having similarly-named rawValue and raw properties.)

@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