module y( clk, rst_n, en_in, in_data, en_out, ou ); input clk; input rst_n; input en_in; input [15:0] in_data; output [7:0] ou; output en_out; /* 根据输入,提取rgb565各分量 */ /* 按公式计算亮度y */ /* 第一步 处理浮点系数,并分别计算2.460r,2.376g,0.938b */ reg [7:0] r, g, b; reg [11:0] r_ou, g_ou, b_ou; reg en_2, en_3; always @(posedge clk or negedge rst_n) begin if (~rst_n) begin r <= 8'b0; g <= 8'b0; b <= 8'b0; en_2 <= 1'b0; en_3 <= 1'b0; end else begin if (en_in) begin r <= in_data[15:11]; g <= in_data[10:5]; b <= in_data[4:0]; en_2 <= 1'b1; end end end always @(*) begin r_ou = r << 2; g_ou = g << 2; b_ou = b << 1; en_out = en_2; end /* 第三步 用第二步求和结果-128,得出最终输出数据 */ always @(posedge clk or negedge rst_n) begin if (~rst_n) begin ou <= 8'b0; end else begin if (en_out) begin ou <= (r_ou + g_ou - b_ou) - 8'd128; end end end endmodule [2024-06-27 16:53:10 | AI写代码神器 | 857点数解答]