cucapra / filament

Fearless hardware design
http://filamenthdl.com/
MIT License
137 stars 8 forks source link

`struct` definitions in Filament #99

Open rachitnigam opened 1 year ago

rachitnigam commented 1 year ago

With complex bundle types showing up in the arguments of components, it seems that a more general technique to represent IO interfaces is in need. Borrowing ideas from Chisel, I think we can use the concept of Bundle (different from filament bundles which are just arrays).

The high-level idea is that a struct definition has a delay and it ensures that all sub-fields hold onto values for less time than the delay of the struct:

struct PacketInfo<G: 1> {
  id: @[G, G+4] 32,
  fields: for<#i> @[G+#i, G+#i+1] 32, 
}

Type-checking should reject this struct definition because the id field holds onto values for 4 cycles while the PacketInfo specifies that the event G reoccurs every cycle. This is problematic because this means that providing a new PacketInfo every cycle will cause problems.

Components like Mac can be defined like this now:

struct MacInputs<G: 1> {
  left: @[G, G+1] 32,
  right: @[G, G+1] 32,
  acc: @[G+2, G+3] 32
}
comp Mac<G: 1>(inp: MacInputs<G>) -> (@[G, G+1] out: 32) { ... }

Of course, you should be able to specify parameters for your structs.

rachitnigam commented 1 year ago

Probably worth reading the documentation on Amaranth HDL's approach to structs