当前位置:网站首页>Understand chisel language thoroughly 07. Chisel Foundation (IV) - bundle and VEC
Understand chisel language thoroughly 07. Chisel Foundation (IV) - bundle and VEC
2022-07-04 14:08:00 【github-3rr0r】
Chisel Basics ( Four )——Bundle and Vec
Chisel In the first three chapters of basics, we learned about data types 、 Combinational circuit operators and registers , Although it is enough to realize very complex digital circuits , But it's not convenient enough . For example, I need to build a 32 Register group of registers , Then I need to write 32 individual RegInit
Do you ? Another example is that I want to pack several signals together , How can I achieve it ?Chisel Two configurations are provided for grouping related signals , They are Bundle
and Vec
, among :
Bundle
It is used to divide different types of signals into a group ;Vec
Used to represent an indexable 、 A collection of signals of the same type ;
In this part, we will explain them in detail .
Bundle
Introduce
Chisel in Bundle
Used to combine multiple signals , This Bundle
There is no good translation , I prefer to translate it into bundle , It can be understood as binding a bunch of signals , But in order not to cause misunderstanding , I will not translate the latter word .
The whole one bundle It can be quoted as a whole , And the individual fields can be accessed through their names . We define a by defining a class bundle( That is, a set of signals ), This class extends from Bundle
class , Each field of it is marked with val
The form of is given in the building block , Consider the following code :
class Channel() extends Bundle {
val data = UInt(32.W)
val valid = Bool()
}
Channel
This bundle will data
and valid
These two signals are tied together . If you want to use this bundle Words , We can new
One Channel
Then package it into a Wire
Inside ( there Wire
The next article will introduce ), Then use .
Just quote :
val ch = Wire(new Channel())
ch.data := 123.U
ch.valid := true.B
val b = ch.valid
.
Tokens are commonly used in object-oriented ,x.y
It means x
Is a reference to an object and y
This is a field of this object . because Chisel It's also object-oriented , So of course we can use .
To visit bundle In the field .
Chisel Medium Bundle
Follow C Inside struct
、VHDL Inside record
as well as SystemVerilog Inside struct
It's all similar . One bundle It can also be quoted as a whole :
val channel = ch
Vec
Introduce
Chisel Medium Vec
Represents a set of signals of the same type , That's a vector . Every element in the vector can be accessed by index . obviously ,Chisel This vector inside , It is similar to arrays in other programming languages , however Ararry
It's already Scala Keywords inside , therefore Chisel It uses Vec
.
Create vectors by calling Vec
To achieve , This constructor has two parameters , One is the number of elements in the vector , The other is the type of element in the vector . Vectors also need to be encapsulated in a Wire
Inside :
val v = Wire(Vec(3, UInt(4.W)))
Every element in the vector can pass (index)
To visit :
v(0) := 1.U
v(1) := 3.U
v(2) := 5.U
val idx = 1.U(2.W)
val a = v(idx)
It's understandable , Package to Wire
The vector inside is a multiplexer , The selection signal corresponds to the index of the vector , This is packaged as Wire
The situation of . We can also package vectors into Reg
, To define a set of registers .
For example, the following example defines the register group of a processor , All in all 32 individual 32 Bit register , This is the classic 32 position RISC The register implementation of the processor , such as RISC-V Of 32 Bit version . The code is as follows :
val registerFile = Reg(Vec(32, UInt(32.W)))
See no , This is more stupid than writing 32 Is a register much stronger ?
Again , Elements of register heap ( That is, a register ) You can also use indexes to access and use the same as normal registers :
registerFile(idx) := dIn
val dOur = registerFile(idx)
Bundle
and Vec
Use
We are free to put Bundle
and Vec
Put it together .
For example, we can use a bundle Create a vector , We need to pass a prototype to the vector field , For example, for the above Channel
, We can use the following code to define a Channel
vector :
val vecBundle = Wire(Vec(8, new Channel()))
Bundle
There can also be vectors , such as :
class BundleVec extends Bundle {
val field = UInt(8.W)
val vector = Vec(4, UInt(8.W))
}
If we want to create a with reset value bundle Register of type , We have to create that first bundle Of Wire
, Set the initial value of each field as needed , Then pass it to RegInit
:
val initVal = Wire(new Channle())
initVal.data := 0.U
initVal.valid := false.B
val channelReg = RegInit(initVal)
By combining Bundle
and Vec
Equal structure , We can define our own data structure , This is it. Chisel One of the places that embody strong abstract ability .
Partial assignment and Bundle
stay Chisel Partial assignment in is not allowed , Although in Chisel2 Medium can , stay Verilog and VHDL It's also possible , For example, the following code will report an error when the circuit is expanded :
val assignWord = Wire(UInt(16.W))
assignWord(7, 0) := lowByte
assignWord(15, 8) := highByte
under these circumstances , Use Bundle
It's a good solution . For example, first create a part bundle, Then give this bundle Create a Wire
, Then assign values to each individual field , Last use asUInt()
Put this bundle convert to UInt
And assign it to the target UInt
. Be careful , So let's take this Bundle It is defined as a local data structure because it is only used here . The code is as follows :
val assignWord = Wire(UInt(16.W))
class Split extends Bundle {
val high = UInt(8.W)
val low = UInt(8.W)
}
val split = Wire(new Split())
split.low := lowByte
split.high := highByte
assignWord := split.asUInt
But there's a drawback to this approach , That's what we need to know bundle What is the order after the fields inside are merged into a bit vector , Like here high
and low
It can't be reversed .
Another way is to use Bool
Construct vectors , In this way, each value can be assigned independently , Finally, it is converted into UInt
Just fine :
val vecResult = Wire(Vec(4, Bool()))
vecResult(0) := data(0)
vecResult(1) := data(1)
vecResult(2) := data(2)
vecResult(3) := data(3)
val uintResult = vecResult.asUInt
Conclusion
Bundle
and Vec
stay Chisel It's very important , Proficiency can greatly improve the efficiency of writing code , Dealing with more complex 、 Scenes that require more modularity will also be more comfortable . Another core concept is also used in this article Wire
, That is to say Verilog Medium wire
, Namely wire network . This Wire
And what I said before UInt
、SInt
What's different , It directly represents the type of hardware , except Wire
Outside ,Chisel in Reg
and IO
It is also the type of direct hardware , The next part will talk about these three , Say more about how to understand Chisel Generation circuit .
边栏推荐
- 近日小结(非技术文)
- Deming Lee listed on Shenzhen Stock Exchange: the market value is 3.1 billion, which is the husband and wife of Li Hu and Tian Hua
- Programmer anxiety
- Distributed base theory
- Unittest中的TestSuite和TestRunner
- Unittest框架中引入TestFixture
- Haproxy high availability solution
- 安装Mysql
- Whether the loyalty agreement has legal effect
- 1200. Minimum absolute difference
猜你喜欢
2022年山东省安全员C证考试题库及在线模拟考试
【R语言数据科学】:交叉验证再回首
自主工业软件的创新与发展
How to choose a technology stack for web applications in 2022
Node の MongoDB安装
Understanding and difference between viewbinding and databinding
MySQL 45 lecture - learn the actual combat notes of MySQL in Geek time 45 lecture - 06 | global lock and table lock_ Why are there so many obstacles in adding a field to the table
Unity Shader学习(三)试着绘制一个圆
Install Trinity and solve error reporting
Haproxy high availability solution
随机推荐
吃透Chisel语言.04.Chisel基础(一)——信号类型和常量
Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)
结合案例:Flink框架中的最底层API(ProcessFunction)用法
自主工业软件的创新与发展
【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
#yyds干货盘点# 解决名企真题:连续最大和
find命令报错: paths must precede expression(转)
2022危险化学品经营单位主要负责人练习题及模拟考试
markdown 语法之字体标红
WS2818M是CPC8封装,是三通道LED驱动控制专用电路外置IC全彩双信号5V32灯可编程led灯带户外工程
Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
【Antd踩坑】Antd Form 配合Input.Group时出现Form.Item所占据的高度不对
舔狗舔到最后一无所有(状态机)
Interviewer: what is the internal implementation of hash data type in redis?
qt 怎么检测鼠标在不在某个控件上
OpenHarmony应用开发之如何创建DAYU200预览器
FS7867S是一款应用于数字系统供电电源电压监控的电压检测芯片
如何在 2022 年为 Web 应用程序选择技术堆栈
Getting started with the go language is simple: go implements the Caesar password
Dgraph: large scale dynamic graph dataset