mif文件生成

WIDTH=64;这里将一副64*64bmp格式的灰度图数据做成.mif文件以便FPGA可以方便的读进RAM。

bmp前54字节是位图文件头和位图信息头,我们要提取的是54字节后的,bmp存储格式是上下颠倒的,这里用取字模软件提取,取模采用横向取模,字节不颠倒。注:字模软件提取的bmp数据已经去掉了前54字节的头而且顺序也是和图像一致的从上到下,从左到右。要提取的图像如下:

64*64的

取字模后的数据如下:

/*--  调入了一幅图像:C:\Documents and Settings\Administrator\桌面\pika.bmp  --*/
/*--  宽度x高度=64x64  --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xFC,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x3F,0x00,0x00,
0x00,0x00,0xE6,0x00,0x01,0xFF,0x00,0x00,0x00,0x01,0x86,0x00,0x07,0xFF,0x00,0x00,
0x00,0x01,0xC6,0x00,0x0C,0x3E,0x00,0x00,0x00,0x01,0x01,0xF8,0x70,0x1C,0x00,0x00,
0x00,0x01,0x8F,0xFF,0xE0,0x38,0x00,0x00,0x00,0x01,0x84,0x07,0x80,0x30,0x00,0x00,
0x00,0x01,0x60,0x00,0x00,0x60,0x78,0x00,0x00,0x01,0x00,0x00,0x00,0xC0,0xC8,0x00,
0x00,0x03,0x00,0x00,0x01,0xC0,0xCF,0x00,0x00,0x03,0x00,0x38,0x03,0x01,0x07,0x80,
0x00,0x0F,0x00,0x4C,0x16,0x03,0x00,0xC0,0x00,0x0D,0x80,0x7C,0x1C,0x03,0x00,0xE0,
0x00,0x0F,0x00,0x7C,0x0C,0x06,0x00,0x60,0x00,0x17,0x20,0x38,0x0C,0x06,0x00,0x60,
0x00,0x1A,0x60,0x01,0x06,0x06,0x00,0xC0,0x00,0x18,0x00,0x03,0x82,0x0C,0x01,0x80,
0x00,0x1C,0xFF,0xC7,0xC3,0x0C,0x03,0x00,0x00,0x1C,0x77,0x07,0xC1,0xB8,0x0E,0x00,
0x00,0x1C,0x3F,0x07,0xC1,0x18,0x18,0x00,0x00,0xBC,0x1E,0x01,0xF1,0xB0,0x18,0x00,
0x00,0xEC,0x00,0x03,0xB1,0xD8,0x30,0x00,0x01,0x82,0x00,0x07,0x1B,0xCC,0x3C,0x00,
0x01,0x81,0x00,0x02,0x19,0xC6,0x16,0x00,0x00,0xC1,0x00,0x00,0x31,0x66,0x0E,0x00,
0x00,0x60,0x00,0x00,0x20,0x37,0x1C,0x00,0x00,0x28,0x00,0x00,0x00,0xAC,0x30,0x00,
0x00,0x3C,0x00,0x01,0xE1,0xF0,0xE0,0x00,0x00,0x06,0x00,0x1B,0x5B,0xFC,0xC0,0x00,
0x00,0x03,0x00,0x1F,0x03,0xFC,0xE0,0x00,0x00,0x03,0x80,0x35,0x81,0xFC,0x60,0x00,
0x00,0x01,0x00,0x31,0x80,0x7F,0xC0,0x00,0x00,0x03,0x80,0x30,0xE0,0x7F,0x80,0x00,
0x00,0x01,0x00,0x08,0x40,0xF8,0x00,0x00,0x00,0x01,0xC0,0x08,0xC0,0xF0,0x00,0x00,
0x00,0x00,0xC0,0x18,0xC3,0xC0,0x00,0x00,0x00,0x00,0xC0,0x1D,0x83,0x00,0x00,0x00,
0x00,0x00,0x60,0x0E,0x8C,0x00,0x00,0x00,0x00,0x00,0x34,0x06,0x6C,0x00,0x00,0x00,
0x00,0x00,0x1E,0x03,0xF0,0x00,0x00,0x00,0x00,0x00,0x07,0x03,0x80,0x00,0x00,0x00,
0x00,0x00,0x01,0xCF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE7,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

接下来可以在Quartus II里面建立mif文件,然后将上面的值输入······数据这么多怎么输入?

数据一旦很大手动输入是不太可取的,这里介绍:将生产的字模数据转化为适合FPGA的mif文件,我们需要建立的ram块为64*64的,也就是说生成的.mif文件WIDTH为64,DEPTH为64。

1.将上述生成的字模数据保存为.txt用notepad++打开,进行列操作(按alt选中列)进行删除合并列变为如下数据:

0000000000000000   0000000000000000
0000000000000000   0000000000000000
0000000000000000   0000000000000000
0000000000000000   0000000000000000
0000180000000000   0000380000000000
00003C0000000000   00007C0000000000
0000FC0000070000   0000FE00003F0000
0000E60001FF0000   0001860007FF0000
0001C6000C3E0000   000101F8701C0000
00018FFFE0380000   0001840780300000
0001600000607800   0001000000C0C800
0003000001C0CF00   0003003803010780
000F004C160300C0   000D807C1C0300E0
000F007C0C060060   001720380C060060
001A6001060600C0   00180003820C0180
001CFFC7C30C0300   001C7707C1B80E00
001C3F07C1181800   00BC1E01F1B01800
00EC0003B1D83000   018200071BCC3C00
0181000219C61600   00C1000031660E00
0060000020371C00   0028000000AC3000
003C0001E1F0E000   0006001B5BFCC000
0003001F03FCE000   0003803581FC6000
00010031807FC000   00038030E07F8000
0001000840F80000   0001C008C0F00000
0000C018C3C00000   0000C01D83000000
0000600E8C000000   000034066C000000
00001E03F0000000   0000070380000000
000001CF00000000   000000E700000000
0000006300000000   0000007E00000000
0000000000000000   0000000000000000
0000000000000000   0000000000000000
0000000000000000   0000000000000000

这个就是64*64的ram块了,接下来用一段程序就可以转换了,程序如下:

#include <stdio.h>

#define Data_Width 64 //mif 存储数据宽度

#define Data_Depth 64 //mif 存储数据总量 

void main()

{

int i;

__int64  HEX_Data[Data_Depth]; //输入?位HEX 数据(0x...)

//在桌面新建Mif文件

FILE *fp;

fp=fopen("C:\\Documents and Settings\\Administrator\\桌面\\Mif.mif","w");

//写入mif文件初始化数据

fprintf(fp,"WIDTH=%d;\n",Data_Width); //写入mif 存储数据宽度

fprintf(fp,"DEPTH=%d;\n",Data_Depth); //写入mif 存储数据总量

fprintf(fp,"\n");

fprintf(fp,"ADDRESS_RADIX=DEC;\n"); //十进制表示地址

fprintf(fp,"DATA_RADIX=HEX;\n"); //十六进制表示数据

fprintf(fp,"CONTENT BEGIN;\n"); //内容开始

fprintf(fp,"\n");

for(i=0;i<Data_Depth;i++)

{

scanf("%I64x",&HEX_Data[i]);

fprintf(fp,"\t%d\t:\t%I64x;\n",i,HEX_Data[i]); 

}

fprintf(fp,"\n");

fprintf(fp,"END;"); //标志mif 文件CONTENT完毕

fclose(fp); //关闭文件

//CMD界面显示

printf("\n");

printf("Built mif file successfully!\n");

}

编译,在debug里面复制入经notepad++处理后得到的数据即可。

生成的.mif如下:

WIDTH=64;
DEPTH=64;

ADDRESS_RADIX=DEC;
DATA_RADIX=HEX;
CONTENT BEGIN;

 0 : 0;
 1 : 0;
 2 : 0;
 3 : 0;
 4 : 0;
 5 : 0;
 6 : 0;
 7 : 0;
 8 : 180000000000;
 9 : 380000000000;
 10 : 3c0000000000;
 11 : 7c0000000000;
 12 : fc0000070000;
 13 : fe00003f0000;
 14 : e60001ff0000;
 15 : 1860007ff0000;
 16 : 1c6000c3e0000;
 17 : 101f8701c0000;
 18 : 18fffe0380000;
 19 : 1840780300000;
 20 : 1600000607800;
 21 : 1000000c0c800;
 22 : 3000001c0cf00;
 23 : 3003803010780;
 24 : f004c160300c0;
 25 : d807c1c0300e0;
 26 : f007c0c060060;
 27 : 1720380c060060;
 28 : 1a6001060600c0;
 29 : 180003820c0180;
 30 : 1cffc7c30c0300;
 31 : 1c7707c1b80e00;
 32 : 1c3f07c1181800;
 33 : bc1e01f1b01800;
 34 : ec0003b1d83000;
 35 : 18200071bcc3c00;
 36 : 181000219c61600;
 37 : c1000031660e00;
 38 : 60000020371c00;
 39 : 28000000ac3000;
 40 : 3c0001e1f0e000;
 41 : 6001b5bfcc000;
 42 : 3001f03fce000;
 43 : 3803581fc6000;
 44 : 10031807fc000;
 45 : 38030e07f8000;
 46 : 1000840f80000;
 47 : 1c008c0f00000;
 48 : c018c3c00000;
 49 : c01d83000000;
 50 : 600e8c000000;
 51 : 34066c000000;
 52 : 1e03f0000000;
 53 : 70380000000;
 54 : 1cf00000000;
 55 : e700000000;
 56 : 6300000000;
 57 : 7e00000000;
 58 : 0;
 59 : 0;
 60 : 0;
 61 : 0;
 62 : 0;
 63 : 0;

END;

完成了! 注意这里因为是64位的,定义64位的数据在不同的编译器上不一样,我在VC6.0上是用的:

__int64  HEX_Data[Data_Depth];  别的编译器有的是long long等等,具体读者自己可以查阅。

32位的定义为 int就可以啦,8位的char·····注意:多少位还是和机器位数有关··

永不止步步 发表于11-04 13:45 浏览65535次
分享到:

已有0条评论

暂时还没有回复哟,快来抢沙发吧

添加一条新评论

只有登录用户才能评论,请先登录注册哦!

话题作者

永不止步步
金币:67417个|学分:363791个
立即注册
畅学电子网,带你进入电子开发学习世界
专业电子工程技术学习交流社区,加入畅学一起充电加油吧!

x

畅学电子网订阅号