115. LED制御モジュール実装例2
115
assign waitrequest = 0;
reg [3:0] r_turnon;
reg [3:0][7:0] r_bright;
reg [3:0][7:0] r_cnt;
always @(posedge clk or negedge reset_n) begin
if (!reset_n)
r_turnon <= 0;
else if (write & (address == 0) & byteenable[0])
r_turnon <= writedata[7:0];
end
always @(posedge clk or negedge reset_n) begin
if (!reset_n)
r_bright <= 0;
else if (write & (address == 1)) begin
for(int i = 0; i < 4; i++)
if (byteenable[i])
r_bright[i] <= writedata[i*8+:8];
end
end
always @(posedge clk or negedge reset_n) begin
if (!reset_n)
readdata <= 0;
else if (read) begin
case (address)
0: readdata <= {24'h0, r_turnon};
1: readdata <= r_bright;
default: readdata <= 32'hx;
endcase
end
end
サンプルコード (2/3)
116. LED制御モジュール実装例3
116
always @(posedge clk or negedge reset_n) begin
if (!reset_n)
readdatavalid <= 0;
else
readdatavalid <= read;
end
always @(posedge clk or negedge reset_n) begin
if (!reset_n)
r_cnt <= 0;
else begin
for(int i = 0; i < 4; i++)
if (r_turnon[i])
r_cnt[i] <= r_cnt[i] + 1;
end
end
always @(posedge clk or negedge reset_n) begin
if (!reset_n)
led <= 'hf;
else begin
for(int i = 0; i < 4; i++)
if (r_turnon[i] & (r_cnt[i] <= r_bright[i]))
led[i] <= 0;
else
led[i] <= 1;
end
end
endmodule // ledctrl
サンプルコード (3/3)