本文作为SpinalHDL学习笔记第五十六篇,介绍SpinalHDL的UInt/SInt数据类型。
目录:
1.描述(Description)
2.声明(Declaration)
3.操作符(Operators)
一.描述(Description)
UInt/SInt数据类型是⼀个能⽤在有/⽆符号计算中的bits向量。
二.声明(Declaration)
声明整型的语法如下所⽰:([]中的是可填项)
val myUInt = UInt(8 bits)
myUInt := U(2, 8 bits)
myUInt := U(2)
myUInt := U"0000_0101" //基默认⼆进制
myUInt := U"h1A" //x/h->基16, d->基10, o->基8, b->基2
myUInt := U"8'h1A"
myUInt := 2 //可以使⽤Scala Int作为字⾯值
val myBool := myUInt === U(7 -> true, (downto 0) -> false)
val myBool := myUInt === U(myUInt.range -> true)
//在赋值的时候, 可以去掉U/S, 也⽀持[default -> ???]
myUInt := (default -> true) //赋值“11111111”
myUInt := (myUInt.range -> true) //赋值“11111111
myUInt := (7 -> true, default -> false) //赋值“10000000”
myUInt := ((4 downto 1) -> true, default -> false) //赋值“00011110”
Verilog:
wire [7:0] myUInt;
reg [7:0] _zz_myBool;
wire myBool;
function [7:0] zz__zz_myBool(input dummy);
begin
zz__zz_myBool[7] = 1'b1;
zz__zz_myBool[6 : 0] = 7'h0;
end
endfunction
wire [7:0] _zz_1;
assign myUInt = 8'h02;
assign myUInt = 8'h02;
assign myUInt = 8'h05;
assign myUInt = 8'h1A;
assign myUInt = 8'h1A;
assign myUInt = 8'h02;
assign _zz_1 = zz__zz_myBool(1'b0);
always @(*) _zz_myBool = _zz_1;
assign myBool = (myUInt == _zz_myBool);
assign _zz_myBool[7 : 0] = 8'hff;
assign myBool = (myUInt == _zz_myBool);
assign myUInt = 8'hff;
assign myUInt = 8'hff;
assign myUInt = 8'h80;
assign myUInt = 8'h1E;
三.操作符(Operators)
下⾯是UInt和SInt⽀持的操作符: