module lsd(clk, rst, led,ctrl); input clk; input rst; input ctrl; output [7 : 0] led; parameter WIDTH =27; parameter COUNT = 99_999_999; reg [WIDTH - 1 : 0] cnt; reg [7 : 0] light; reg flag; always @(posedge clk) begin if (!rst) cnt <= 0; else if (cnt == COUNT) cnt <= 0; else if (ctrl) cnt <= cnt; else cnt <= cnt + 1; end always @(posedge clk) begin if (light == 8'h01) flag <= 1; //left move else if (light == 8'h80) flag <= 0; //rihgt move else ; end always @(posedge clk) begin if (!rst) light <= 8'h01; else if (cnt == COUNT) begin if (ctrl) light <= light; else if (flag) light <= light << 1; else light <= light >> 1; end else ; end assign led = light; endmodule修改代码,使流水灯能够反向运行
时间: 2024-02-10 14:05:01 浏览: 86
bittorrent-lsd:本地服务发现 (BEP14) 实施
好的,下面是修改后的代码,使流水灯能够反向运行:
```
module lsd(clk, rst, led, ctrl);
input clk;
input rst;
input ctrl;
output [7:0] led;
parameter WIDTH = 27;
parameter COUNT = 99_999_999;
reg [WIDTH-1:0] cnt;
reg [7:0] light;
reg flag;
always @(posedge clk) begin
if (!rst)
cnt <= 0;
else if (cnt == COUNT)
cnt <= 0;
else if (ctrl)
cnt <= cnt;
else
cnt <= cnt + 1;
end
always @(posedge clk) begin
if (light == 8'h01)
flag <= 1; //left move
else if (light == 8'h80)
flag <= 0; //right move
else
flag <= flag;
end
always @(posedge clk) begin
if (!rst)
light <= 8'h01;
else if (cnt == COUNT) begin
if (ctrl)
light <= light;
else if (flag)
light <= light >> 1; //reverse left move
else
light <= light << 1; //reverse right move
end
else
light <= light;
end
assign led = light;
endmodule
```
修改的主要部分在第三个always块中。当计数器达到COUNT时,如果ctrl信号为0,则根据flag信号决定流水灯是向左移动还是向右移动,从而实现反向运行的效果。如果ctrl信号为1,则流水灯保持原状态不变。
阅读全文