//输入大写字母、小写字母和符号,输出 I Love You! 字符串
module ily(
input wire clk, //系统时钟
input wire rst_n, //复位
input wire [7:0] lowercase,//小写字母和符号
input wire [7:0] capital, //大写
output reg [31:0] love //输出字符串 I Love You!
);
reg [3:0] state; //状态寄存器
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
love <= 0; //初始化
state <= 0; //状态赋初值
end
else
begin
case(state)
0:begin //这里的“I”也可以写成I的ascii love=73
if(capital == "I") //检测到 I 输出,并跳转
begin
love <= "I";
state <= 1;
end
else
state <= 0; //检测不到 I 一直等待
end
1:begin
if(lowercase == " ")//检测到空格输出 也可以写成空格的ascii码 love=64
begin
love <= " ";
state <= 2;
end
else
state <= 1;//检测不到空格 一直等待
end
2:begin
if(capital == "L")//检测到L输出 love=76
begin
love <= "L";
state <= 3;
end
else
state <= 2;
end
3:begin
if(lowercase == "o")//检测到o输出 love=111
begin
love <= "o";
state <= 4;
end
else
state <= 3;
end
4:begin
if(lowercase == "v")//检测到v输出 love=118
begin
love <= "v";
state <= 5;
end
else
state <= 4;
end
5:begin
if(lowercase == "e")//检测到e输出 love=101
begin
love <= "e";
state <= 6;
end
else
state <= 5;
end
6:begin
if(lowercase == " ")//检测到空格输出 love=64
begin
love <= " ";
state <= 7;
end
else
state <= 6;
end
7:begin
if(capital == "Y")//检测到Y输出 love=89
begin
love <= "Y";
state <= 8;
end
else
state <= 7;
end
8:begin
if(lowercase == "o")//检测到o输出 love=111
begin
love <= "o";
state <= 9;
end
else
state <= 8;
end
9:begin
if(lowercase == "u")//检测到u输出 love=117
begin
love <= "u";
state <= 10;
end
else
state <= 9;
end
10:begin
if(lowercase == "!")//检测到!输出 love=33
begin
love <= "!";
state <= 0;
end
else
state <= 10;
end
default : state <= 0;
endcase
end
end
endmodule
测试代码如下:
`timescale 1ns/1ns //时间单位是1ns 时间精度是1ns
module ily_tb;
reg clk;
reg rst_n;
reg [7:0] lowercase;
reg [7:0] capital;
wire [31:0] love;
initial begin
clk = 1;
rst_n = 0;
lowercase = 0;
capital = 0;
#200
rst_n = 1;
//随机输出
/*forever begin
#20 lowercase = "a"; capital = "A";
#20 lowercase = "o"; capital = "B";
#20 lowercase = "c"; capital = "I";
#20 lowercase = "v"; capital = "L";
#20 lowercase = "!"; capital = "D";
#20 lowercase = "a"; capital = "Y";
#20 lowercase = "e"; capital = "S";
#20 lowercase = " "; capital = "A";
#20 lowercase = "u"; capital = "A";
end*/
//有序输出
/* forever begin
#20 capital = "I";
#20 lowercase = " ";
#20 capital = "L";
#20 lowercase = "o";
#20 lowercase = "v";
#20 lowercase = "e";
#20 lowercase = " ";
#20 capital = "Y";
#20 lowercase = "o";
#20 lowercase = "u";
#20 lowercase = "!";
end */
end
/* initial //通过任务产生随机数
begin
forever begin
#400 //启动任务
locale(); //任务必须写在initial块里启动
end
end
initial
begin
forever begin
#400
cap();
end
end
task cap(); //产生大写字母的任务
integer i;
begin
for(i=0;i<200;i=i+1)
begin
@(posedge clk);
capital <= {$random}%i;
end
end
endtask
task locale(); //产生小写字母的任务
integer i;
begin
for(i=0;i<200;i=i+1)
begin
@(posedge clk);
lowercase <= {$random}%i;
end
end
endtask */
initial begin //产生随机数
forever begin
#20 lowercase <= {$random}%128;
end //产生0-127之间的一个数
end //如果写成 $ramdom%128 会产生一个-127~127之间的数
initial begin
forever begin
#20 capital <= {$random}%128;
end
end
always #10 clk=~clk;
ily ily_inst(
.clk (clk),
.rst_n (rst_n),
.lowercase (lowercase),//小写字母和符号
.capital (capital), //大写
.love (love)
);
endmodule |