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

SRAM API: Add multiple-clocked port API #3383

Merged
merged 13 commits into from Jul 1, 2023
Merged

Conversation

jared-barocsi
Copy link
Contributor

@jared-barocsi jared-barocsi commented Jun 26, 2023

It is sometimes desirable to instantiate a memory with ports clocked with different clocks (e.g. clock domain crossing). This PR adds additional APIs to SRAM which allow users to drive ports using a sequence of clocks:

// Create a multi-clocked 3R, 2W SRAM
val mem = SRAM(
  1024, 
  UInt(8.W), 
  Seq(clock1, clock2, clock3), // Memory read port clocks
  Seq(clock4, clock5),         // Memory write port clocks
  Seq.empty                    // Memory read-write port clocks
)

Contributor Checklist

  • Did you add Scaladoc to every public function/method?
  • Did you add at least one test demonstrating the PR?
  • Did you delete any extraneous printlns/debugging code?
  • Did you specify the type of improvement?
  • Did you add appropriate documentation in docs/src?
  • Did you request a desired merge strategy?
  • Did you add text to be included in the Release Notes for this change?

Type of Improvement

  • Feature (or new API)

Desired Merge Strategy

Squash

Release Notes

Add new SRAM APIs that take three Clock sequences as parameters instead of the number of read/write/read-write ports. This will sequentially instantiate a memory port for each clock in the Clock sequence and drive them accordingly.

Reviewer Checklist (only modified by reviewer)

  • Did you add the appropriate labels? (Select the most appropriate one based on the "Type of Improvement")
  • Did you mark the proper milestone (Bug fix: 3.5.x or 3.6.x depending on impact, API modification or big change: 5.0.0)?
  • Did you review?
  • Did you check whether all relevant Contributor checkboxes have been checked?
  • Did you do one of the following when ready to merge:
    • Squash: You/ the contributor Enable auto-merge (squash), clean up the commit message, and label with Please Merge.
    • Merge: Ensure that contributor has cleaned up their commit history, then merge with Create a merge commit.

@jared-barocsi jared-barocsi added the Feature New feature, will be included in release notes label Jun 26, 2023
Copy link
Contributor

@jackkoenig jackkoenig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good, some style suggestions

Comment on lines 431 to 439
private def memInterface_impl[T <: Data](
size: BigInt,
tpe: T
)(readPortClocks: Seq[Clock],
writePortClocks: Seq[Clock],
readwritePortClocks: Seq[Clock],
memoryFile: Option[MemoryFile]
)(evidenceOpt: Option[T <:< Vec[_]],
sourceInfo: SourceInfo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a real purpose to having 3 parameter lists here? Since this is a private method I would just defined it with a normal single parameter list.

Suggested change
private def memInterface_impl[T <: Data](
size: BigInt,
tpe: T
)(readPortClocks: Seq[Clock],
writePortClocks: Seq[Clock],
readwritePortClocks: Seq[Clock],
memoryFile: Option[MemoryFile]
)(evidenceOpt: Option[T <:< Vec[_]],
sourceInfo: SourceInfo
private def memInterface_impl[T <: Data](
size: BigInt,
tpe: T,
readPortClocks: Seq[Clock],
writePortClocks: Seq[Clock],
readwritePortClocks: Seq[Clock],
memoryFile: Option[MemoryFile],
evidenceOpt: Option[T <:< Vec[_]],
sourceInfo: SourceInfo

Comment on lines 461 to 463
for ((clock, i) <- readPortClocks.zipWithIndex) {
_out.readPorts(i).data := mem.read(_out.readPorts(i).address, _out.readPorts(i).enable, clock)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for ((clock, i) <- readPortClocks.zipWithIndex) {
_out.readPorts(i).data := mem.read(_out.readPorts(i).address, _out.readPorts(i).enable, clock)
}
for ((clock, port) <- readPortClocks.zip(_out.readPort)) {
port.data := mem.read(port.address, port.enable, clock)
}

I should've caught this with the earlier PR, but unless you are writing performance critical code using Arrays, indexing a Seq is code smell. This same change for the write and readwrite ports will make those loops much cleaner.

Comment on lines 306 to 314
memInterface_impl(size, tpe)(
Seq.fill(numReadPorts)(Builder.forcedClock),
Seq.fill(numWritePorts)(Builder.forcedClock),
Seq.fill(numReadwritePorts)(Builder.forcedClock),
None
)(
Some(evidence),
sourceInfo
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
memInterface_impl(size, tpe)(
Seq.fill(numReadPorts)(Builder.forcedClock),
Seq.fill(numWritePorts)(Builder.forcedClock),
Seq.fill(numReadwritePorts)(Builder.forcedClock),
None
)(
Some(evidence),
sourceInfo
)
): SRAMInterface[T] = {
val clock = Builder.forcedClock
memInterface_impl(size, tpe)(
Seq.fill(numReadPorts)(clock),
Seq.fill(numWritePorts)(clock),
Seq.fill(numReadwritePorts)(clock),
None
)(
Some(evidence),
sourceInfo
)
}

This would be cleaner with just assigning clock to a val, also that's just generally a good idea because Builder.forcedClock requires accessing the Chisel runtime in the Dynamic variable which is just unnecessary extra compute to do more than once here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion for the other places calling Builder.forcedClock a bunch.

Comment on lines 551 to 558
val (counter, _) = Counter(true.B, 11)
val clock1: Clock = (counter === 1.U).asClock
val clock2: Clock = (counter === 2.U).asClock
val clock3: Clock = (counter === 3.U).asClock

val readClocks = Seq(clock1, clock2, clock3)
val writeClocks = Seq(clock2, clock3, clock1)
val readwriteClocks = Seq(clock3, clock1, clock2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fun but maybe too clever by half, it would be more straightforward to just have Vecs of clocks (which are Seqs)

Suggested change
val (counter, _) = Counter(true.B, 11)
val clock1: Clock = (counter === 1.U).asClock
val clock2: Clock = (counter === 2.U).asClock
val clock3: Clock = (counter === 3.U).asClock
val readClocks = Seq(clock1, clock2, clock3)
val writeClocks = Seq(clock2, clock3, clock1)
val readwriteClocks = Seq(clock3, clock1, clock2)
val readClocks = IO(Input(Vec(3, Clock())))
val writeClocks = IO(Input(Vec(3, Clock())))
val readwriteClocks = IO(Input(Vec(3, Clock())))

Then below you just look for the clocks, eg. readClocks[$i]

Comment on lines 346 to 354
memInterface_impl(
size,
tpe,
Seq.fill(numReadPorts)(Builder.forcedClock),
Seq.fill(numWritePorts)(Builder.forcedClock),
Seq.fill(numReadwritePorts)(Builder.forcedClock),
Some(memoryFile),
Some(evidence),
sourceInfo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please assign Builder.forcedClock to a val here as well.

Comment on lines 308 to 316
memInterface_impl(
size,
tpe,
Seq.fill(numReadPorts)(Builder.forcedClock),
Seq.fill(numWritePorts)(Builder.forcedClock),
Seq.fill(numReadwritePorts)(Builder.forcedClock),
None,
Some(evidence),
sourceInfo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please assign Builder.forcedClock to a val here as well.

@jackkoenig jackkoenig added this to the 3.6.x milestone Jun 29, 2023
Copy link
Contributor

@jackkoenig jackkoenig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but please address the 2 minor nits

@jared-barocsi jared-barocsi enabled auto-merge (squash) July 1, 2023 05:45
@jared-barocsi jared-barocsi merged commit d79ae71 into main Jul 1, 2023
11 checks passed
@jared-barocsi jared-barocsi deleted the sram-multiclocks branch July 1, 2023 06:08
@mergify mergify bot added the Backported This PR has been backported label Jul 1, 2023
mergify bot pushed a commit that referenced this pull request Jul 1, 2023
This PR adds new APIs for the SRAM module to instantiate a number of ports connected to individual clocks. This allows the creation of memories driven by multiple clocks, for use cases like clock domain crossover.

(cherry picked from commit d79ae71)

# Conflicts:
#	src/test/scala/chiselTests/Mem.scala
mergify bot pushed a commit that referenced this pull request Jul 1, 2023
This PR adds new APIs for the SRAM module to instantiate a number of ports connected to individual clocks. This allows the creation of memories driven by multiple clocks, for use cases like clock domain crossover.

(cherry picked from commit d79ae71)

# Conflicts:
#	src/test/scala/chiselTests/Mem.scala
jared-barocsi added a commit that referenced this pull request Jul 5, 2023
* SRAM API: Add multiple-clocked port API (#3383)

This PR adds new APIs for the SRAM module to instantiate a number of ports connected to individual clocks. This allows the creation of memories driven by multiple clocks, for use cases like clock domain crossover.

(cherry picked from commit d79ae71)

---------

Co-authored-by: Jared Barocsi <82000041+jared-barocsi@users.noreply.github.com>
Co-authored-by: Jared Barocsi <jared.barocsi@sifive.com>
jared-barocsi added a commit that referenced this pull request Jul 6, 2023
* SRAM API: Add multiple-clocked port API (#3383)

This PR adds new APIs for the SRAM module to instantiate a number of ports connected to individual clocks. This allows the creation of memories driven by multiple clocks, for use cases like clock domain crossover.

(cherry picked from commit d79ae71)

---------

Co-authored-by: Jared Barocsi <82000041+jared-barocsi@users.noreply.github.com>
Co-authored-by: Jared Barocsi <jared.barocsi@sifive.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported This PR has been backported Feature New feature, will be included in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants