`timescale 1ns/1ns
module bmp_dumper #
(
parameter BMP_FILENAME = "",
parameter BMP_WIDTH = 32'd1024,
parameter BMP_HIGHT = 32'd768
)
(
input rst,
input clk,
input [23:0] rgb_dat,
input sop,
input vld,
input eop
);
parameter BMP_DAT_NUM = BMP_WIDTH * BMP_HIGHT * 3;
//WORD = 2byte
//typedef struct tagBITMAPFILEHEADER {
// WORD bfType;
// DWORD bfSize;
// WORD bfReserved1;
// WORD bfReserved2;
// DWORD bfOffBits;
//} BITMAPFILEHEADER;
reg [7:0] bf_type[0:1];
reg [7:0] bf_size[0:3];
reg [7:0] bf_rsv[0:3];
reg [7:0] bf_offset[0:3];
wire [31:0] bfsize = BMP_DAT_NUM + 14 + 40;
reg [7:0] bf_header[0:13];
reg [7:0] bi_header[0:39];
//typedef struct tagBITMAPINFOHEADER{
////public:
// DWORD biSize;
// LONG biWidth;
// LONG biHeight;
// WORD biPlanes;
// WORD biBitCount;
// DWORD biCompression;
// DWORD biSizeImage;
// LONG biXPelsPerMeter;
// LONG biYPelsPerMeter;
// DWORD biClrUsed;
// DWORD biClrImportant;
//} BITMAPINFOHEADER;
reg [7:0] bi_size[0:3];
reg [7:0] bi_width[0:3];
reg [7:0] bi_hight[0:3];
reg [7:0] bi_planes[0:1];
reg [7:0] bi_bit_cnt[0:1];
reg [7:0] bi_cmprss[0:3];
reg [7:0] bi_size_img[0:3];
reg [7:0] bi_xmeter[0:3];
reg [7:0] bi_ymeter[0:3];
reg [7:0] bi_clr_used[0:3];
reg [7:0] bi_clrimportant[0:3];
int file_id = 0;
int i = 0;
initial begin
if (BMP_FILENAME == "") begin
$display("bmp filename parameter not set");
$stop;
end
file_id = $fopen(BMP_FILENAME, "wb");
if (file_id == 0) begin
$display("can't open output bmp %s", BMP_FILENAME);
$stop;
end
$display("BMP: %m writing to %s", BMP_FILENAME);
@(posedge clk);
//---------------------------------------------------------------------------//
//bf info
bf_type[0] = 8'h42; //B
bf_type[1] = 8'h4d; //M
bf_rsv[0] = 8'h00;
bf_rsv[1] = 8'h00;
bf_rsv[2] = 8'h00;
bf_rsv[3] = 8'h00;
bf_size[0] = bfsize[07:00];
bf_size[1] = bfsize[15:08];
bf_size[2] = bfsize[23:16];
bf_size[3] = bfsize[31:24];
bf_offset[0] = 8'h36;
bf_offset[1] = 8'h00;
bf_offset[2] = 8'h00;
bf_offset[3] = 8'h00;
bf_header[0] = bf_type[0];
bf_header[1] = bf_type[1];
bf_header[2] = bf_size[0];
bf_header[3] = bf_size[1];
bf_header[4] = bf_size[2];
bf_header[5] = bf_size[3];
bf_header[6] = bf_rsv[0];
bf_header[7] = bf_rsv[1];
bf_header[8] = bf_rsv[2];
bf_header[9] = bf_rsv[3];
bf_header[10] = bf_offset[0];
bf_header[11] = bf_offset[1];
bf_header[12] = bf_offset[2];
bf_header[13] = bf_offset[3];
foreach(bf_header[i])
$fwrite(file_id, "%c", bf_header[i]);
//---------------------------------------------------------------------------//
//bi info
bi_size[0] = 8'h28;
bi_size[1] = 8'h00;
bi_size[2] = 8'h00;
bi_size[3] = 8'h00;
bi_width[0] = BMP_WIDTH[07:00];
bi_width[1] = BMP_WIDTH[15:08];
bi_width[2] = BMP_WIDTH[23:16];
bi_width[3] = BMP_WIDTH[31:24];
bi_hight[0] = BMP_HIGHT[07:00];
bi_hight[1] = BMP_HIGHT[15:08];
bi_hight[2] = BMP_HIGHT[23:16];
bi_hight[3] = BMP_HIGHT[31:24];
bi_planes[0] = 8'h01;
bi_planes[1] = 8'h00;
bi_bit_cnt[0] = 8'h18; //rgb,8/8/8
bi_bit_cnt[1] = 8'h00;
for(i=0;i<3;i++) begin
bi_cmprss[i] = 8'h00;
bi_size_img[i] = 8'h00;
bi_xmeter[i] = 8'h00;
bi_ymeter[i] = 8'h00;
bi_clr_used[i] = 8'h00;
bi_clrimportant[i] = 8'h00;
end
bi_header[00] = bi_size[0];
bi_header[01] = bi_size[1];
bi_header[02] = bi_size[2];
bi_header[03] = bi_size[3];
bi_header[04] = bi_width[0];
bi_header[05] = bi_width[1];
bi_header[06] = bi_width[2];
bi_header[07] = bi_width[3];
bi_header[08] = bi_hight[0];
bi_header[09] = bi_hight[1];
bi_header[10] = bi_hight[2];
bi_header[11] = bi_hight[3];
bi_header[12] = bi_planes[0];
bi_header[13] = bi_planes[1];
bi_header[14] = bi_bit_cnt[0];
bi_header[15] = bi_bit_cnt[1];
bi_header[16] = bi_cmprss[0];
bi_header[17] = bi_cmprss[1];
bi_header[18] = bi_cmprss[2];
bi_header[19] = bi_cmprss[3];
bi_header[20] = bi_size_img[0];
bi_header[21] = bi_size_img[1];
bi_header[22] = bi_size_img[2];
bi_header[23] = bi_size_img[3];
bi_header[24] = bi_xmeter[0];
bi_header[25] = bi_xmeter[1];
bi_header[26] = bi_xmeter[2];
bi_header[27] = bi_xmeter[3];
bi_header[28] = bi_ymeter[0];
bi_header[29] = bi_ymeter[1];
bi_header[30] = bi_ymeter[2];
bi_header[31] = bi_ymeter[3];
bi_header[32] = bi_clr_used[0];
bi_header[33] = bi_clr_used[1];
bi_header[34] = bi_clr_used[2];
bi_header[35] = bi_clr_used[3];
bi_header[36] = bi_clrimportant[0];
bi_header[37] = bi_clrimportant[1];
bi_header[38] = bi_clrimportant[2];
bi_header[39] = bi_clrimportant[3];
foreach(bi_header[i])
$fwrite(file_id, "%c", bi_header[i]);
end
int file_size;
always @ (posedge clk or posedge rst)
begin
if (rst == 1'b1)
file_size <= 32'd0;
else if (sop == 1'b1 && vld == 1'b1)
file_size <= 32'd3;
else if (vld == 1'b1)
file_size <= file_size + 32'd3;
else ;
end
reg [7:0] bf_dat[0:2];
always @ (posedge clk)
begin
if (vld == 1'b1) begin
for(i=0;i<3;i++) begin
bf_dat[i] = rgb_dat[i*8+:8];
$fwrite(file_id, "%c", bf_dat[i]);
end
end
end
endmodule