|
很多人想研究risc-v,chisel3,想做一个简单的chisel3实验,因为涉及到的步骤很多,且相关代码在github上,下载时速度太慢,很多方面的原因,最后搞得完全没兴趣了,所以本帖将以最简单最快速的方法告诉你如何如:
1)从berkeley在github上下载chisel-tutorial代码很慢,基本上不太容易一次成功,可以从https://gitee.com/whoisliang/chisel-tutorial下载则超快。
命令:git clone https://gitee.com/whoisliang/chisel-tutorial.git 也可以直接从网页中下载zip文件解压即可得到源码
我们主要是想获得Adder.scala和FullAdder.scala两个实验用的文件
2)idea中创建sbt项目:
3)项目建成之后,build.sbt内容为:
name := "AdderVerilog"
version := "0.1"
scalaVersion := "2.11.12"
在上面内容的后面添加:
resolvers += "aliyun" at "http://maven.aliyun.com/nexus/content/groups/public/"
libraryDependencies += "edu.berkeley.cs" %% "chisel3" % "3.1.2"
其实这么做主要的原因是下载相关库的时候从aliyun中央仓库去下载,这样速度会快100倍左右,
另外可以用chisel 3.1.2版本,其实我后来用3.4.1版本做实验也成功了,不过,3.1.2版本的
主main中要用:Driver.execute,而3.4.1版本的主main中要用:(new chisel3.stage.ChiselStage()).emitVerilog。
从https://gitee.com/whoisliang/chisel-tutorial下载的Adder.scala源文件内容如下(我没有用git重新checkout,所以是最新版源代码):
import chisel3._
//A n-bit adder with carry in and carry out
class Adder(val n:Int) extends Module {
val io = IO(new Bundle {
val A = Input(UInt(n.W))
val B = Input(UInt(n.W))
val Cin = Input(UInt(1.W))
val Sum = Output(UInt(n.W))
val Cout = Output(UInt(1.W))
})
//create an Array of FullAdders
// NOTE: Since we do all the wiring during elaboration and not at run-time,
// i.e., we don't need to dynamically index into the data structure at run-time,
// we use an Array instead of a Vec.
val FAs = Array.fill(n)(Module(new FullAdder()).io)
val carry = Wire(Vec(n+1, UInt(1.W)))
val sum = Wire(Vec(n, Bool()))
//first carry is the top level carry in
carry(0) := io.Cin
//wire up the ports of the full adders
for (i <- 0 until n) {
FAs(i).a := io.A(i)
FAs(i).b := io.B(i)
FAs(i).cin := carry(i)
carry(i+1) := FAs(i).cout
sum(i) := FAs(i).sum.asBool
}
io.Sum := sum.asUInt
io.Cout := carry(n)
}
如果我们在build.sbt中用的是3.1.2版chisel3,则需要将sum(i) := FAs(i).sum.asBool这句代码改为:sum(i) := FAs(i).sum.toBool()
FullAdder.scala文件内容如下:
import chisel3._
class FullAdder extends Module {
val io = IO(new Bundle {
val a = Input(UInt(1.W))
val b = Input(UInt(1.W))
val cin = Input(UInt(1.W))
val sum = Output(UInt(1.W))
val cout = Output(UInt(1.W))
})
val a_xor_b = io.a ^ io.b
io.sum := a_xor_b ^ io.cin
val a_and_b = io.a & io.b
val b_and_cin = io.b & io.cin
val a_and_cin = io.a & io.cin
io.cout := a_and_b | b_and_cin | a_and_cin
}
我们自己的主main所在文件MainTest.scala内容如下:
import chisel3._
object MainTest {
def main(args: Array[String]): Unit = {
val m = Driver.execute(Array("--target-dir","generated"),()=>new Adder(8))//run之后,将会在generated子目录下产生Adder.v和Adder.fir文件
//如果是3.4.1版chisel3则:
//(new chisel3.stage.ChiselStage()).emitVerilog(new Adder(8),Array("--target-dir","generated"))
//3.4.1版中还可以:
//(new chisel3.stage.ChiselStage()).emitVerilog(new FullAdder)则不会创建generated子目录,只会在主目录下产生Adder.v和Adder.fir文件
//其实3.4.1版本下还可以产生system verilog文件,可以只产生.fir文件,具体可以看emitxxx相关方法
}
}
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|