使用Chisel3在两个信号4位之间的内积(点积。)

时间:2019-11-28 14:42:57

标签: scala hdl chisel

在使用Chisel3构建硬件组件方面,我是一个新手。现在,我正在尝试制作一个小模块,其中有2个输入(向量A和向量B)和1个输出(输出为1位无符号整数)。 Out是A和B的点积。

我声明了模块DotProduct进行计算。这是我的代码:

<!DOCTYPE html>
<html lang="en">
  <head>


     <!-- Required meta tags -->
     <meta charset="utf-8">
     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!--Bootstrap css-->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

      <title>
        first web page using bootstrap
      </title>
<!-- Font awesome for brand name-->
      <link href="https://fonts.googleapis.com/css?family=Permanent+Marker&display=swap" rel="stylesheet"> 

      <!--Our CSS-->
      <link rel="stylesheet" href="CSS\style.css" >

  </head> 
<body>

  <!-- Nav bar-->

  <nav class="navbar navbar-light navbar-expand-lg mybg" style="background-color: #3be79f;">
      <a class="navbar-brand" href="#" style="color: rgb(141, 7, 7); font-size: 25px; font-family: 'Permanent Marker', cursive;"> Stiffy </a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Products</a>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Categories
            </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdown">
              <a class="dropdown-item" href="#">Beauty</a>
              <a class="dropdown-item" href="#">Health</a>
              <div class="dropdown-divider"></div>
              <a class="dropdown-item" href="#">Lifestyle</a>
            </div>
          </li>
          <li class="nav-item">
            <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Your Cart</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Sign In</a>
          </li>

        </ul>
        <form class="form-inline my-2 my-lg-0">
          <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
          <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
      </div>
    </nav>

    <!--slideshow starts-->


    <div class="fluid-container">

      <div id="myCarousel" class="carousel slide" data-ride="carousel">
        <!-- Indicators -->
        <ol class="carousel-indicators">
          <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
          <li data-target="#myCarousel" data-slide-to="1"></li>
          <li data-target="#myCarousel" data-slide-to="2"></li>
        </ol>

        <!-- Wrapper for slides -->
        <div class="carousel-inner">

          <div class="carousel-item active">
            <img src="../Image\unsplash.jpg"  alt="Los Angeles" style="width:100%;">
            <div class="carousel-caption">
              <h3>Photography</h3>
              <p>It is always so much fun!</p>
            </div>
          </div>

          <div class="carousel-item">
            <img src="../Image\zombie-945622.jpg" alt="Chicago" style="width:100%;">
            <div class="carousel-caption">
              <h3>Ghost</h3>
              <p>Thank you, Mr. Demon</p>
            </div>
          </div>

          <div class="carousel-item">
            <img class="img-1" src="../Image\zombie-979358.jpg" alt="New York" style="width:100%; height: 60%;">
            <div class="carousel-caption">
              <h3> Oops.</h3>
              <p>Ghosts are best.</p>
            </div>
          </div>

        </div>

        <!-- Left and right controls -->
        <a class="carousel-control-prev" href="#myCarousel" data-slide="prev">
          <span class="carousel-control-prev-icon"></span>
        </a>
        <a class="carousel-control-next" href="#myCarousel" data-slide="next">
          <span class="carousel-control-next-icon"></span>
        </a>
      </div>
    </div>


     <!--slideshow ends-->

<!-- Banner starts-->
<div class="fluid-container">
  <div class="row" class="mybanner" style= "background-color: #3be79f;
  text-align: center;"
  >
     <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12"  >

      24*7 Services

     </div>


  <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12"  >

    Cheaper in price

  </div>


<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12"  >

  Deliver ASAP

</div>

</div>

</div>

<!-- Banner ends-->

<!-- other stuff-->
<div class="other">
  <img src="../Image\bride.jpg">
</div>


<!-- JQuery,JS, BOOTSTRAP JS-->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

</body>
</html>

这是我的简单测试

class RealDotProduct extends Module {
  val io = IO(new Bundle {
    val vectorA = Input(Vec(2, UInt(2.W)))
    val vectorB = Input(Vec(2, UInt(2.W)))
    val out  = Output(UInt(1.W))
  })

  val product = 0.U

  //Loop to calculate the inner (dot) product
  for (i <- 0 until 2) {
      product := product + io.vectorA(i) * io.vectorB(i)
  }
  io.out := product
}

我使用sbt运行测试,并得到了错误日志:

class RealDotProductTests(c: RealDotProduct) extends PeekPokeTester(c) {
  import scala.collection.immutable._  
  val in_a = Vector(0,1)
  val in_b = Vector(2,3)

  for (i <- 0 until 2) {
      poke (c.io.vectorA(i), in_a(i))
      poke (c.io.vectorB(i), in_b(i))
  }
  step(1)
  expect(c.io.out, 3)
}

class RealDotProductTester extends ChiselFlatSpec {
  behavior of "RealDotProduct"
  backends foreach {backend =>
    it should s"perform correct math operation on dynamic operand in $backend" in {
      Driver(() => new RealDotProduct, backend)((c) => new RealDotProductTests(c)) should be (true)
    }
  }
}
object RealDotProductMain extends App {
  iotesters.Driver.execute(args, () => new RealDotProduct) {
    c => new RealDotProductTests(c)
  }
}
  • 问题1:能否请您告诉我我的问题出在哪里 编码?
  • 问题2:如果我想用vectorA和的随机值运行许多测试,我应该替换 expect(c.io.out,3)中的 3 向量B?

非常感谢你们。

1 个答案:

答案 0 :(得分:1)

答案1: Chisel正在组装代表所需硬件的连接图。您遇到的问题之一是将事物多次连接到导线product。在软件中这可能有意义,但在硬件中却没有那么重要。

如果您要口头描述您想做的事情,那可能就像是,“将inA和inB中的每个元素配对,并将它们相乘,然后将这些乘积之和加起来。在Chisel中,一种方法就是这样。

val products = io.vectorA.zip(io.vectorB).map { case (a, b) => a * b }
val sum = product.reduce { case (a, b) => a + b }
io,out := sum

该拉链将两个输入vec的元素配对。该映射将每对值转换为一系列乘积。然后,您可以使用reduce总结这些产品。

您的IO端口的宽度也有一些问题。您的out端口没有足够的位来包含两个向量的内积。 Chisel可以处理有关计算所需多少位的工作,但是对于IO端口,您仍然需要仔细考虑所需的位数。

很快,随着您对使用集合的scala习惯(例如VecSeq)越来越熟悉,您将能够编写更多此类模块 简洁地说,是这样的:

  io.out := io.vectorA.zip(io.vectorB).map(_ * _).reduce(_ + _)

答案2: 只需在测试仪中编写一个函数来计算值即可。解决该问题的基本方法是

var sum = 0   for(i <-in_a.indices){     总和+ =(in_a(i)* in_b(i))   }

...   Expect(c.io.out,sum)

您可以做一些更奇特的事情。关于位的宽度。它应该足够大,可以处理您希望对任意输入进行计算得出的最大值。