- case语句模型
module ex_case(
input wire rst_n,
input wire sclk,
output reg o_dv,
output reg [7:0] o_data,
input wire [9:0] i_data,
input wire [7:0] i_addr
);
reg [2:0] cnt_7;
// 不通功能的寄存器分开always块来写,这样代码的可维护性强,可持续性强。
always @(posedge sclk or negedge rst_n) begin
if (rst_n == 1'b0) begin
cnt_7 <= 3'd0;
end
else begin
cnt_7 <= cnt_7 + 3'd1;
end
end
// 时序逻辑
// case相当于是一个选择器
always @(posedge sclk or negedge rst_n)
if (rst_n == 1'b0) begin
o_data <= 8'd0;
o_dv <= 1'b0;
end
else begin
case(cnt_7) //cnt_7 相当于一个查找表地址
3'd0:begin
o_data <= 3'd7; //当cnt_7等于3'd0执行词条语句
o_dv <= 1'b1;
end
3'd1:begin
o_data <= 3'd0;
o_dv <= 1'b0;
end
3'd2:begin
o_data <= 3'd5;
o_dv <= 1'b1;
end
default: begin
o_data <= 3'd0;
o_dv <= 1'b0;
end
endcase
end
endmodule