简体   繁体   English

怎么看懂这行凿码

[英]How to understand this line of chisel code

I'm in the process of learning chisel and scala language and try to analyse some lines of rocket-chip code.Could anyone try to explain me this line?我正在学习凿子和 scala 语言并尝试分析火箭芯片代码的一些行。有人可以解释一下吗? https://github.com/chipsalliance/rocket-chip/blob/54237b5602a273378e3b35307bb47eb1e58cb9fb/src/main/scala/rocket/RocketCore.scala#L957 https://github.com/chipsalliance/rocket-chip/blob/54237b5602a273378e3b35307bb47eb1e58cb9fb/src/main/scala/rocket/RocketCore.scala#L957

I understand what log2Up function is doing but don't understand why that log2Up(n)-1 and 0,were passed like "arguments" to addr which is val of type UInt??我了解 log2Up function 正在做什么,但不明白为什么 log2Up(n)-1 和 0 像“参数”一样传递给 addr,它是 UInt 类型的 val?

I could not find where UInt was defined, but if I had to guess, UInt is a class that has an apply method.我找不到UInt的定义位置,但如果我不得不猜测, UInt是一个具有apply方法的class This is a special method that allows us to use a parenthesis operator on an instance of the class.这是一种特殊方法,允许我们在 class 的实例上使用括号运算符。

For example lets say we have a class called Multiply that defines an apply method.例如,假设我们有一个名为Multiply的 class 定义了一个apply方法。

class Multiply {
  def apply(a: Int, b: Int): Int = a * b
}

This allows you to call operator () on any instance of that class.这允许您在该 class 的任何实例上调用 operator () For example:例如:

val mul = new Multiply()
println(mul(5, 6)) // prints 30

What i concluded is that we use addr(log2Up(n)-1, 0) to get addres bits starting from zero up to log2Up(n)-1 bit.我得出的结论是,我们使用 addr(log2Up(n)-1, 0) 来获取从零开始到 log2Up(n)-1 位的地址位。 Lets take an example.让我们举个例子。

If we made object of class RegFile in this way如果我们这样制作了 class RegFile 的 object

val reg = RegFile(31, 10)

First, memory rf is created.首先,创建 memory rf。 Size of that memory is 31 data of type UInt width of 10, starting from 0 up to 30. When we compute log2Up(n)-1 we get 4, and we have something like this: addr(4,0). memory 的大小是 31 个 UInt 类型的数据,宽度为 10,从 0 到 30。当我们计算 log2Up(n)-1 时,我们得到 4,我们有这样的东西:addr(4,0)。 This gives us last five bits of addr.这给了我们最后五位 addr。 Like @Jack Koenig said in one of the comment above: "Rocket's register file uses a little trick where it reverses the order of the registers physically compared to the RISC-V", that's why we use ~addr.就像@Jack Koenig 在上面的评论中所说的那样:“Rocket 的寄存器文件使用了一个小技巧,与 RISC-V 相比,它在物理上颠倒了寄存器的顺序”,这就是我们使用 ~addr 的原因。 And at least rf(~addr) gives us back what is in that memory location.至少 rf(~addr) 让我们返回了 memory 位置中的内容。

This is implemented in this way to provide adequate memory access.这是以这种方式实现的,以提供足够的 memory 访问。 Take a look what whould be if we try to get data from memory location that we don't have in our memory.看看如果我们试图从 memory 中没有的 memory 位置获取数据会发生什么。 If method access was called in this way如果以这种方式调用方法访问

access(42)

We try to access memory location on 41th place, but we only have 31 memory location(30 is top).我们尝试在第 41 位访问 memory 位置,但我们只有 31 个 memory 位置(30 是顶部)。 42 binary is 101010. Using what i said above 42二进制是101010。使用我上面所说的

~addr(log2Up(n)-1,0)

would return us 10101 or 21 in decimal.将以十进制返回 10101 或 21。 Because order of registers is reversed this is 10th memory location (we try to access 41th but only have 31, 41 minus 31 is 10).因为寄存器的顺序是颠倒的,所以这是第 10 个 memory 位置(我们尝试访问第 41 个但只有 31 个,41 减去 31 是 10)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM