finish reconstruction and pass simulation

This commit is contained in:
SikongJueluo
2024-06-28 19:38:36 +08:00
parent 85910958f9
commit c527835ff6
7 changed files with 372 additions and 310 deletions

View File

@@ -1,23 +1,23 @@
module demosaic2 #(
parameter reg [16:0] IM_WIDTH = 512, // 图像宽度
parameter reg [16:0] IM_HEIGHT = 256, // 图像高度
parameter reg [1:0] RAW_TYPE = 3, // 0:grbg 1:rggb 2:bggr 3:gbrg
parameter reg [5:0] DATA_SIZE = 16
module Demosaic2 #(
parameter reg [15:0] IM_WIDTH = 512, // 图像宽度
parameter reg [15:0] IM_HEIGHT = 256, // 图像高度
parameter reg [ 1:0] RAW_TYPE = 3, // 0:grbg 1:rggb 2:bggr 3:gbrg
parameter reg [ 4:0] DATA_SIZE = 16
) (
// 基本信号
input clk,
input reset,
input wire clk,
input wire reset,
// 数据输入信号
input in_en,
input [DATA_SIZE - 1:0] in_data [2], // 数据输入线012分别表示第一三行
output out_ready, // 数据请求线高电平请求三个数据直到读取完才拉低
output out_receive,
input wire in_en,
input wire [DATA_SIZE - 1:0] in_data [3], // 数据输入线012分别表示第一三行
output wire out_ready, // 数据请求线高电平请求三个数据直到读取完才拉低
output wire out_receive,
// en: 输出数据有效信号高电平有效
input in_ready,
input in_receive,
output out_en,
input wire in_ready,
input wire in_receive,
output reg out_en,
output reg [DATA_SIZE - 1:0] out_r,
output reg [DATA_SIZE - 1:0] out_g,
output reg [DATA_SIZE - 1:0] out_b
@@ -26,15 +26,15 @@ module demosaic2 #(
// 常量包括状态机
// localparam IM_SIZE = IM_HEIGHT * IM_WIDTH;
localparam READ_DATA = 0;
localparam COLOR_GEN = 1;
localparam SEND_DATA = 2;
localparam SLIDE_WINDOW = 3;
localparam reg [2:0] READ_DATA = 0;
localparam reg [2:0] COLOR_GEN = 1;
localparam reg [2:0] SEND_DATA = 2;
localparam reg [2:0] SLIDE_WINDOW = 3;
// 寄存器
reg [2:0] state, nextState;
reg [15:0] data_cache[2][2]; // 缓存颜色数据行列3x3
reg [11:0] pos_x, pos_y; // 滑动窗口左上角位置
reg [15:0] data_cache[9]; // 缓存颜色数据行列3x3
reg [15:0] pos_x, pos_y; // 滑动窗口左上角位置
reg [2:0] cnt_data; // 记录输入数据数量最大值256
reg [1:0] raw_type;
reg [15:0] red, blue, green;
@@ -47,23 +47,21 @@ module demosaic2 #(
end
// 下一状态更新
always @(state or cnt_data) begin
always @(*) begin
case (state)
// 记录够3x3个数据后进行rgb转换
READ_DATA: nextState = (cnt_data >= 3) ? COLOR_GEN : READ_DATA;
COLOR_GEN: nextState = SEND_DATA;
SEND_DATA: nextState = (in_receive) ? SLIDE_WINDOW : SEND_DATA;
READ_DATA: nextState = (cnt_data >= 3) ? COLOR_GEN : READ_DATA;
COLOR_GEN: nextState = SEND_DATA;
SEND_DATA: nextState = (in_receive) ? SLIDE_WINDOW : SEND_DATA;
SLIDE_WINDOW: nextState = READ_DATA;
default: nextState = 0;
default: nextState = READ_DATA;
endcase
end
// 请求数据
assign out_ready = (cnt_data <= 2 && !in_en && state == READ_DATA) ? 1 : 0;
assign out_ready = (cnt_data <= 2 && !in_en && state == READ_DATA) ? 1 : 0;
// 收到数据
assign out_receive = (in_en && state == READ_DATA) ? 1 : 0;
// 发送数据有效位
assign out_en = (in_ready && state == SEND_DATA) ? 1 : 0;
// 各状态执行的操作
always @(posedge clk or posedge reset) begin
@@ -85,9 +83,9 @@ module demosaic2 #(
// 读取数据
READ_DATA: begin
if (in_en) begin
data_cache[cnt_data][0] <= in_data[0];
data_cache[cnt_data][1] <= in_data[1];
data_cache[cnt_data][2] <= in_data[2];
data_cache[0 + cnt_data * 3] <= in_data[0];
data_cache[1 + cnt_data * 3] <= in_data[1];
data_cache[2 + cnt_data * 3] <= in_data[2];
cnt_data <= cnt_data + 1;
end
@@ -96,33 +94,33 @@ module demosaic2 #(
COLOR_GEN: begin
// 生成rgb图像
// data case 0 case 1 case 2 case 3
// 0 1 2 G R G R G R B G B G B G
// 3 4 5 B G B G B G G R G R G R
// 6 7 8 G R G R G R B G B G B G
// 0 3 6 G R G R G R B G B G B G
// 1 4 7 B G B G B G G R G R G R
// 2 5 8 G R G R G R B G B G B G
case (raw_type)
0: begin // Missing B, R on G
blue <= (data_cache[0][1] + data_cache[2][1]) / 2;
red <= (data_cache[1][0] + data_cache[1][2]) / 2;
green <= data_cache[1][1];
blue <= (data_cache[1] + data_cache[7]) / 2;
red <= (data_cache[3] + data_cache[5]) / 2;
green <= data_cache[4];
end
1: begin // Missing G, R on B
green <= (data_cache[0][1] + data_cache[1][0] + data_cache[1][2] + data_cache[2][1]) / 4;
red <= (data_cache[0][0] + data_cache[0][2] + data_cache[2][0] + data_cache[2][2]) / 4;
blue <= data_cache[1][1];
green <= (data_cache[1] + data_cache[3] + data_cache[5] + data_cache[7]) / 4;
red <= (data_cache[0] + data_cache[2] + data_cache[6] + data_cache[8]) / 4;
blue <= data_cache[4];
end
2: begin // Missing G, B on R
green <= (data_cache[0][1] + data_cache[1][0] + data_cache[1][2] + data_cache[2][1]) / 4;
blue <= (data_cache[0][0] + data_cache[0][2] + data_cache[2][0] + data_cache[2][2]) / 4;
red <= data_cache[1][1];
green <= (data_cache[1] + data_cache[3] + data_cache[5] + data_cache[7]) / 4;
blue <= (data_cache[0] + data_cache[2] + data_cache[6] + data_cache[8]) / 4;
red <= data_cache[4];
end
3: begin // Missing B, R on G
red <= (data_cache[0][1] + data_cache[2][1]) / 2;
blue <= (data_cache[1][0] + data_cache[1][2]) / 2;
green <= data_cache[1][1];
red <= (data_cache[1] + data_cache[7]) / 2;
blue <= (data_cache[3] + data_cache[5]) / 2;
green <= data_cache[4];
end
default: ;
endcase
@@ -136,11 +134,12 @@ module demosaic2 #(
end
SEND_DATA: begin
if (in_ready) begin
out_r <= red;
out_b <= blue;
out_g <= green;
end
if (in_ready && !in_receive) begin
out_en <= 1;
out_r <= red;
out_b <= blue;
out_g <= green;
end else out_en <= 0;
end
SLIDE_WINDOW: begin
@@ -169,12 +168,12 @@ module demosaic2 #(
cnt_data <= 2;
// 窗口右移
data_cache[0][0] <= data_cache[1][0];
data_cache[0][1] <= data_cache[1][1];
data_cache[0][2] <= data_cache[1][2];
data_cache[1][0] <= data_cache[2][0];
data_cache[1][1] <= data_cache[2][1];
data_cache[1][2] <= data_cache[2][2];
data_cache[0] <= data_cache[3];
data_cache[1] <= data_cache[4];
data_cache[2] <= data_cache[5];
data_cache[3] <= data_cache[6];
data_cache[4] <= data_cache[7];
data_cache[5] <= data_cache[8];
end
end