字符串简介
一个字符串是由双引号"括起来并包含在一行中的字符序列。
在表达式和赋值语句中,用作操作数的字符串被视为由8bit ASCII码值表示的无符号整数常量。
字符串声明
字符串变量是wire/reg类型的变量,宽度等于字符串中的字符个数乘以8。
reg [8*12 -1 : 0] stringVar; // 可以存储12个字符
initial begin
stringVal = "Hello World!";
end
字符串操作
可以使用Verilog的运算符来操作字符串,操作的值都是以8bit ASCII值为一个整体。
$display自动地在输出后进行换行,$write则在输出后不换行。在$display和$write中输出格式控制是用双引号括起来的字符串。
// 字符串打印
$display("%s is stored as %h", stringVar,stringVar);
// 字符串拼接
stringVar = {"Hello ","World!"};
- 当一个变量声明的位宽大于保存被赋字符串所需的位宽时,左边的多余位宽在赋值后用0填充。
- 如果字符串长度大于声明的字符串变量的位宽,则该字符串将被向左截断,最左边的字符将丢失。
格式说明符,%和格式字符组成,将输出地数据转换成指定的格式输出。
转义字符
输出数字( 八进制、十进制、十六进制)时:
- 如果对应所有位都为不定值,则输出小写x, 部分位为不定态输出大写X。
- 如果对应所有位都为高阻值,则输出小写z, 部分位为高阻值输出大写Z。
输出字符画
在线生成字符画网站
- 根据文字生成字符画
- https://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something%20
- 根据图片生成字符画
- https://www.degraeve.com/img2txt.php
- http://life.chacuo.net/convertphoto2char
- 根据流程图生成字符画
- https://asciiflow.com/
`timescale 1ns / 1ns
module tb_ascii_pic();
initial begin
show_pass();
# 100 ;
$stop;
end
task automatic show_pass();
begin: pass
$display("pwd: %m");
$display(".----------------. .----------------. .----------------. .----------------. " );
$display("| .--------------. || .--------------. || .--------------. || .--------------. |");
$display("| | ______ | || | __ | || | _______ | || | _______ | |");
$display("| | |_ __ \ | || | / \ | || | / ___ | | || | / ___ | | |");
$display("| | | |__) | | || | / /\ \ | || | | (__ \_| | || | | (__ \_| | |");
$display("| | | ___/ | || | / ____ \ | || | '.___`-. | || | '.___`-. | |");
$display("| | _| |_ | || | _/ / \ \_ | || | |`\____) | | || | |`\____) | | |");
$display("| | |_____| | || ||____| |____|| || | |_______.' | || | |_______.' | |");
$display("| | | || | | || | | || | | |");
$display("| '--------------' || '--------------' || '--------------' || '--------------' |");
$display("'----------------' '----------------' '----------------' '----------------' " );
$display("time: %t",$time );
end
endtask
task automatic show_fail();
begin: fail
$display("pwd: %m");
$display(".----------------. .----------------. .----------------. .----------------. " );
$display("| .--------------. || .--------------. || .--------------. || .--------------. |");
$display("| | _________ | || | __ | || | _____ | || | _____ | |");
$display("| | |_ ___ | | || | / \ | || | |_ _| | || | |_ _| | |");
$display("| | | _| | || | / ____ \ | || | | | | || | | | _ | |");
$display("| | _| |_ | || | _/ / \ \_ | || | _| |_ | || | _| |__/ | | |");
$display("| | |_____| | || ||____| |____|| || | |_____| | || | |________| | |");
$display("| | | || | | || | | || | | |");
$display("| '--------------' || '--------------' || '--------------' || '--------------' |");
$display("'----------------' '----------------' '----------------' '----------------' " );
$display("time: %t",$time );
end
endtask
endmodule