-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[utils] Utility functions from rocket-chip
- Loading branch information
1 parent
abe2957
commit 41a1de8
Showing
2 changed files
with
54 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2016-2017 SiFive, Inc. See LICENSE.SiFive for details | ||
|
||
package utils | ||
|
||
import chisel3._ | ||
import chisel3.util.{Cat, OHToUInt} | ||
|
||
object OH1 { | ||
def OH1ToOH(x: UInt): UInt = | ||
((x << 1).asUInt | 1.U) & (~Cat(0.U(1.W), x)).asUInt | ||
|
||
def OH1ToUInt(x: UInt): UInt = OHToUInt(OH1ToOH(x)) | ||
|
||
def UIntToOH1(x: UInt, width: Int): UInt = | ||
(~((-1).S(width.W).asUInt << x)(width - 1, 0)).asUInt | ||
|
||
def UIntToOH1(x: UInt): UInt = UIntToOH1(x, (1 << x.getWidth) - 1) | ||
} |
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,35 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2016-2017 SiFive, Inc. See LICENSE.SiFive for details | ||
|
||
import chisel3._ | ||
|
||
import scala.annotation.tailrec | ||
import scala.math.min | ||
|
||
package object utils { | ||
// Fill 1s from low bits to high bits | ||
def leftOR(x: UInt): UInt = leftOR(x, x.getWidth, x.getWidth) | ||
|
||
def leftOR(x: UInt, width: Integer, cap: Integer = 999999): UInt = { | ||
val stop = min(width, cap) | ||
|
||
@tailrec | ||
def helper(s: Int, x: UInt): UInt = | ||
if (s >= stop) x else helper(s + s, x | (x << s)(width - 1, 0)) | ||
|
||
helper(1, x)(width - 1, 0) | ||
} | ||
|
||
// Fill 1s form high bits to low bits | ||
def rightOR(x: UInt): UInt = rightOR(x, x.getWidth, x.getWidth) | ||
|
||
def rightOR(x: UInt, width: Integer, cap: Integer = 999999): UInt = { | ||
val stop = min(width, cap) | ||
|
||
@tailrec | ||
def helper(s: Int, x: UInt): UInt = | ||
if (s >= stop) x else helper(s + s, x | (x >> s)(width - 1, 0)) | ||
|
||
helper(1, x)(width - 1, 0) | ||
} | ||
} |