当前位置:网站首页>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 :
BundleIt is used to divide different types of signals into a group ;VecUsed 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 .
边栏推荐
- 基于链表管理的单片机轮询程序框架
- 如何在 2022 年为 Web 应用程序选择技术堆栈
- Interview disassembly: how to check the soaring usage of CPU after the system goes online?
- 安装trinity、解决报错
- qt 怎么检测鼠标在不在某个控件上
- FS4059C是5V输入升压充电12.6V1.2A给三节锂电池充电芯片 输入小电流不会拉死,温度60°建议1000-1100MA
- #yyds干货盘点# 解决名企真题:连续最大和
- Service Mesh的基本模式
- OpenHarmony应用开发之如何创建DAYU200预览器
- Basic mode of service mesh
猜你喜欢

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

Source code compilation and installation of MySQL

unity不识别rider的其中一种解决方法

Test evaluation of software testing

Go 语言入门很简单:Go 实现凯撒密码

如何在 2022 年为 Web 应用程序选择技术堆栈

sharding key type not supported

Detailed explanation of Fisher information quantity detection countermeasure sample code

MySQL 5 installation and modification free

Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million
随机推荐
Apple 5g chip research and development failure: continue to rely on Qualcomm, but also worry about being prosecuted?
【Antd踩坑】Antd Form 配合Input.Group时出现Form.Item所占据的高度不对
读取 Excel 表数据
2022 practice questions and mock exams for the main principals of hazardous chemical business units
Unity shader learning (3) try to draw a circle
2022年起重机械指挥考试模拟100题模拟考试平台操作
美国土安全部长:国内暴力极端主义是目前美面临的最大恐怖主义威胁之一
字节面试算法题
好博医疗冲刺科创板:年营收2.6亿 万永钢和沈智群为实控人
2022危险化学品经营单位主要负责人练习题及模拟考试
Lick the dog until the last one has nothing (state machine)
Fs4056 800mA charging IC domestic fast charging power IC
吃透Chisel语言.11.Chisel项目构建、运行和测试(三)——Chisel测试之ScalaTest
源码编译安装MySQL
MongoDB常用28条查询语句(转)
基于YOLOv1的口罩佩戴检测
.Net之延迟队列
做事的真正意义和目的,真正想得到什么
JVM series - stack and heap, method area day1-2
Go 语言入门很简单:Go 实现凯撒密码