FPGA生成图像模板,产生RBG格式棋盘格。利用Modelsim仿真,将FPGA生成的图片转化成BMP图片保存并显示。
一:仿真代码
`timescale 1ns/1ps
module design_1_wrapper_top
();
reg clk_100MHz;
reg clk_300MHz;
wire [31:0]clk_rate;
wire [31:0]frame_rate;
wire [15:0]heighth;
wire o_hblank;
wire o_pixel_clk;
wire o_vblank;
wire [11:0]ov_U;
wire [11:0]ov_V;
wire [11:0]ov_data;
wire stable;
wire [15:0]width;
wire clk_455M5;
reg [7:0] rd_data ;
reg wr_en ;
initial begin
clk_100MHz=0;
clk_300MHz=0;
end
always #3 clk_100MHz=~clk_100MHz;
always #1 clk_300MHz=~clk_300MHz;
design_1_wrapper
design_1_wrapper_inst
(
.clk_100MHz (clk_100MHz ),
.clk_455M5 (clk_455M5 ),
.clk_rate (clk_rate ),
.frame_rate (frame_rate ),
.heighth (heighth ),
.o_hblank (o_hblank ),
.o_pixel_clk (o_pixel_clk),
.o_vblank (o_vblank ),
.ov_U (ov_U ),
.ov_V (ov_V ),
.ov_data (ov_data ),
.stable (stable ),
.width (width )
);
reg [7:0] wr_bmp_data [0:2100000] ;
parameter CLK_PERIOD =5 ;
parameter W_BMP_WIDTH =32'd1920 ;
parameter W_BMP_HIGHT =32'd1080 ;
parameter PIXEL_BITS =16'h0018 ; //24bits
parameter PIXEL_BYTES =PIXEL_BITS/8 ; //3bytes
parameter IMAGE_SIZE =((((W_BMP_WIDTH*PIXEL_BYTES)>>2)+1)<<2)*W_BMP_HIGHT ;
parameter BMP_FILE_HEAD = 32'd54 ;
parameter BM_WINDOWS = 16'h4d42 ;
parameter FILE_SIZE =IMAGE_SIZE+BMP_FILE_HEAD ;
integer out_image ;
integer i ;
integer j ;
initial begin
out_image = $fopen("I:picture\\output_file.bmp","wb");
//BM
wr_bmp_data[0 ] = BM_WINDOWS[0+:8] ;
wr_bmp_data[1 ] = BM_WINDOWS[8+:8] ;
//bmp file size
wr_bmp_data[2 ] = FILE_SIZE[0 +:8] ;
wr_bmp_data[3 ] = FILE_SIZE[8 +:8] ;
wr_bmp_data[4 ] = FILE_SIZE[16+:8] ;
wr_bmp_data[5 ] = FILE_SIZE[24+:8] ;
//reserved
wr_bmp_data[6 ] = 8'h00 ;
wr_bmp_data[7 ] = 8'h00 ;
wr_bmp_data[8 ] = 8'h00 ;
wr_bmp_data[9 ] = 8'h00 ;
//offset
wr_bmp_data[10] = BMP_FILE_HEAD[0 +:8] ;
wr_bmp_data[11] = BMP_FILE_HEAD[8 +:8] ;
wr_bmp_data[12] = BMP_FILE_HEAD[16+:8] ;
wr_bmp_data[13] = BMP_FILE_HEAD[24+:8] ;
//bmp information struct
wr_bmp_data[14] = 8'h28 ;
wr_bmp_data[15] = 8'h00 ;
wr_bmp_data[16] = 8'h00 ;
wr_bmp_data[17] = 8'h00 ;
//write bmp width
wr_bmp_data[18] = W_BMP_WIDTH[0+:8] ;
wr_bmp_data[19] = W_BMP_WIDTH[8+:8] ;
wr_bmp_data[20] = W_BMP_WIDTH[16+:8] ;
wr_bmp_data[21] = W_BMP_WIDTH[24+:8] ;
//write bmp hight
wr_bmp_data[22] = W_BMP_HIGHT[0+:8] ;
wr_bmp_data[23] = W_BMP_HIGHT[8+:8] ;
wr_bmp_data[24] = W_BMP_HIGHT[16+:8] ;
wr_bmp_data[25] = W_BMP_HIGHT[24+:8] ;
//bit planes
wr_bmp_data[26] = 8'h01 ;
wr_bmp_data[27] = 8'h00 ;
//one pixel use bits
wr_bmp_data[28] = PIXEL_BITS[0+:8] ;
wr_bmp_data[29] = PIXEL_BITS[8+:8] ;
//compress
wr_bmp_data[30] = 8'h00 ;
wr_bmp_data[31] = 8'h00 ;
wr_bmp_data[32] = 8'h00 ;
wr_bmp_data[33] = 8'h00 ;
//bmp image size
wr_bmp_data[34] = IMAGE_SIZE[0 +:8] ;
wr_bmp_data[35] = IMAGE_SIZE[8 +:8] ;
wr_bmp_data[36] = IMAGE_SIZE[16+:8] ;
wr_bmp_data[37] = IMAGE_SIZE[24+:8] ;
wr_bmp_data[38] = 8'hC4 ;
wr_bmp_data[39] = 8'h0e ;
wr_bmp_data[40] = 8'h00 ;
wr_bmp_data[41] = 8'h00 ;
wr_bmp_data[42] = 8'hC4 ;
wr_bmp_data[43] = 8'h0e ;
wr_bmp_data[44] = 8'h00 ;
wr_bmp_data[45] = 8'h00 ;
//use color board
wr_bmp_data[46] = 8'h00 ;
wr_bmp_data[47] = 8'h00 ;
//important color
wr_bmp_data[48] = 8'h00 ;
wr_bmp_data[49] = 8'h00 ;
wr_bmp_data[50] = 8'h00 ;
wr_bmp_data[51] = 8'h00 ;
wr_bmp_data[52] = 8'h00 ;
wr_bmp_data[53] = 8'h00 ;
j=0;
for(i=0;i<54;i=i+1)
begin
@(posedge clk_455M5)
$fwrite(out_image,"%c",wr_bmp_data[i]);
end
for(i=54;i
if((j%(W_BMP_WIDTH*3)==0))begin
j=0;
end
wait(o_vblank);
wait(o_hblank);
wr_en=1;
@(posedge clk_300MHz)begin
if(j%(PIXEL_BITS/8)==0)
$fwrite(out_image,"%c",ov_data[7:0]);
else if(j%(PIXEL_BITS/8)==1)
$fwrite(out_image,"%c",ov_U[7:0]);
else if(j%(PIXEL_BITS/8)==2)
$fwrite(out_image,"%c",ov_V[7:0]);
end
wait(o_vblank);
wait(o_hblank);
#1
j=j+1;
wait(o_vblank);
wait(o_hblank);
end
wr_en=0;
$fclose(out_image);
end
endmodule
二:代码仿真
产生BMP图像。