先写代码
class表示module,Bundle相当于interface。用Reg()定义一个寄存器。具体如下:
代码语言:javascript复制package mylib
import spinal.core._
import spinal.lib._
import scala.util.Random
// RTL
class MyTopLevel extends Component {
val io = new Bundle {
val enable = in Bool()
val clear = in Bool()
val cnt = out UInt(8 bits)
}
// seq logic
val counter = Reg(UInt(8 bits)) init(0)
when(io.clear) {
counter := 0
}.elsewhen(io.enable) {
counter := counter 1
}
// combinational logic
io.cnt := counter
}
//Generate Verilog
object MyTopLevelVerilog {
def main(args: Array[String]) {
SpinalVerilog(new MyTopLevel)
}
}
其中,
1. if-else if的写法如下:
代码语言:javascript复制when(condition1){
}.elsewhen(condition2){
}
2. 赋值用“:=”
3. 最后一段是用来转verilog的
编译运行
0. 安装sbt
代码语言:javascript复制cd /etc/yum.repos.d/
curl -L https://www.scala-sbt.org/sbt-rpm.repo > sbt-rpm.repo
yum install sbt
1. 下载示例代码
hg clone https://codeload.github.com/SpinalHDL/SpinalTemplateSbt
2. 把上面的代码放在以下目录,并删除自带的两个scala文件
SpinalTemplateSbt-master/src/main/scala/mylib/MyTopLevel.scala
3. 在SpinalTemplateSbt-master目录运行命令
sbt run
3. 等待一会,在SpinalTemplateSbt-master目录就可以找到
MyTopLevel.v
来看看生成的Verilog
代码语言:javascript复制// Generator : SpinalHDL v1.6.4 git head : 598c18959149eb18e5eee5b0aa3eef01ecaa41a1
// Component : MyTopLevel
`timescale 1ns/1ps
module MyTopLevel (
input io_enable,
input io_clear,
output [7:0] io_cnt,
input clk,
input reset
);
reg [7:0] counter;
assign io_cnt = counter;
always @(posedge clk or posedge reset) begin
if(reset) begin
counter <= 8'h0;
end else begin
if(io_clear) begin
counter <= 8'h0;
end else begin
if(io_enable) begin
counter <= (counter 8'h01);
end
end
end
end
endmodule
其它不用写Verilog的方法:
MyHDL,体验一下“用python设计电路”