2024-06-28 19:38:36 +08:00
|
|
|
module Crop #(
|
|
|
|
parameter reg [15:0] IN_WIDTH = 1934,
|
|
|
|
parameter reg [15:0] IN_HEIGHT = 1086,
|
2024-07-04 19:37:53 +08:00
|
|
|
parameter reg [15:0] OFFSET_X = 7,
|
|
|
|
parameter reg [15:0] OFFSET_Y = 3,
|
2024-06-28 19:38:36 +08:00
|
|
|
parameter reg [15:0] OUT_WIDTH = 640,
|
|
|
|
parameter reg [15:0] OUT_HEIGHT = 480,
|
|
|
|
parameter reg [4:0] COLOR_DEPTH = 8
|
|
|
|
) (
|
|
|
|
input wire clk,
|
|
|
|
input wire reset,
|
|
|
|
|
|
|
|
input wire in_en,
|
|
|
|
output wire out_ready,
|
|
|
|
output wire out_receive,
|
2024-07-03 21:50:29 +08:00
|
|
|
input wire [COLOR_DEPTH - 1:0] in_data[3],
|
2024-06-28 19:38:36 +08:00
|
|
|
|
|
|
|
input wire in_ready,
|
|
|
|
input wire in_receive,
|
|
|
|
output reg out_en,
|
2024-07-03 21:50:29 +08:00
|
|
|
output reg [COLOR_DEPTH - 1:0] out_data[3]
|
2024-06-28 19:38:36 +08:00
|
|
|
);
|
|
|
|
reg [1:0] state, nextState;
|
|
|
|
localparam reg [1:0] READ_DATA = 0;
|
|
|
|
localparam reg [1:0] HANDLE_DATA = 1;
|
|
|
|
localparam reg [1:0] SEND_DATA = 2;
|
|
|
|
|
|
|
|
reg [15:0] cnt_x, cnt_y;
|
2024-07-03 21:50:29 +08:00
|
|
|
reg [COLOR_DEPTH - 1:0] data[3];
|
2024-06-28 19:38:36 +08:00
|
|
|
wire is_valid;
|
|
|
|
|
|
|
|
// 状态切换
|
2024-07-03 21:50:29 +08:00
|
|
|
always @(posedge clk) begin
|
2024-06-28 19:38:36 +08:00
|
|
|
if (reset) state <= READ_DATA;
|
|
|
|
else state <= nextState;
|
|
|
|
end
|
|
|
|
|
|
|
|
// 下一状态更新
|
|
|
|
always @(*) begin
|
|
|
|
case (state)
|
|
|
|
READ_DATA: nextState = in_en ? HANDLE_DATA : READ_DATA;
|
|
|
|
HANDLE_DATA: nextState = SEND_DATA;
|
|
|
|
SEND_DATA: nextState = (is_valid && !in_receive) ? SEND_DATA : READ_DATA;
|
|
|
|
default: nextState = READ_DATA;
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
|
2024-07-06 21:53:06 +08:00
|
|
|
assign out_ready = (!in_en && state == READ_DATA && !reset) ? 1 : 0;
|
|
|
|
assign out_receive = (in_en && state == READ_DATA && !reset) ? 1 : 0;
|
2024-06-28 19:38:36 +08:00
|
|
|
assign is_valid = ((OFFSET_Y <= cnt_y && cnt_y <= (OFFSET_Y + OUT_HEIGHT - 1)) &&
|
|
|
|
(OFFSET_X <= cnt_x && cnt_x <= (OFFSET_X + OUT_WIDTH))) ? 1 : 0;
|
|
|
|
|
2024-07-03 21:50:29 +08:00
|
|
|
always @(posedge clk) begin
|
2024-06-28 19:38:36 +08:00
|
|
|
if (reset) begin
|
|
|
|
cnt_x <= 0;
|
|
|
|
cnt_y <= 0;
|
2024-07-03 21:50:29 +08:00
|
|
|
data[0] <= 0;
|
|
|
|
data[1] <= 0;
|
|
|
|
data[2] <= 0;
|
2024-06-28 19:38:36 +08:00
|
|
|
|
|
|
|
out_en <= 0;
|
2024-07-03 21:50:29 +08:00
|
|
|
out_data[0] <= 0;
|
|
|
|
out_data[1] <= 0;
|
|
|
|
out_data[2] <= 0;
|
2024-06-28 19:38:36 +08:00
|
|
|
end else begin
|
|
|
|
case (state)
|
|
|
|
READ_DATA: begin
|
|
|
|
if (in_en) begin
|
2024-07-03 21:50:29 +08:00
|
|
|
data[0] <= in_data[0];
|
|
|
|
data[1] <= in_data[1];
|
|
|
|
data[2] <= in_data[2];
|
2024-06-28 19:38:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
HANDLE_DATA: begin
|
|
|
|
if (cnt_x >= IN_WIDTH - 1) begin
|
|
|
|
cnt_x <= 0;
|
|
|
|
cnt_y <= cnt_y + 1;
|
|
|
|
end else begin
|
|
|
|
cnt_x <= cnt_x + 1;
|
|
|
|
end
|
2024-07-04 19:37:53 +08:00
|
|
|
end
|
2024-06-28 19:38:36 +08:00
|
|
|
|
2024-07-04 19:37:53 +08:00
|
|
|
SEND_DATA: begin
|
|
|
|
if (cnt_y >= IN_HEIGHT) begin
|
2024-06-28 19:38:36 +08:00
|
|
|
cnt_y <= 0;
|
|
|
|
end
|
|
|
|
|
|
|
|
if (in_ready && !in_receive && is_valid) begin
|
2024-07-04 19:37:53 +08:00
|
|
|
out_en <= 1;
|
2024-07-03 21:50:29 +08:00
|
|
|
out_data[0] <= data[0];
|
|
|
|
out_data[1] <= data[1];
|
|
|
|
out_data[2] <= data[2];
|
2024-06-28 19:38:36 +08:00
|
|
|
end else out_en <= 0;
|
|
|
|
end
|
|
|
|
|
|
|
|
default: ;
|
|
|
|
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
endmodule
|