/
bin2mif.cpp
57 lines (54 loc) · 1.6 KB
/
bin2mif.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <fstream>
#include <vector>
#include <sstream>
#include <string>
#include <iostream>
#include <iomanip>
#include <math.h>
std::vector<unsigned char> readFile(const char* filename)
{
// open the file:
std::streampos fileSize;
std::ifstream file(filename, std::ios::binary);
// get its size:
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// read the data:
std::vector<unsigned char> fileData(fileSize);
file.read((char*) &fileData[0], fileSize);
return fileData;
}
unsigned getWidth(unsigned size)
{
return size / 4 + ((size % 4)? 1: 0);
}
int main(int argc, char *argv[])
{
if(argc < 5)
{
std::cout << "äîëæíû áûòü àðãóìåíòû: èìÿ_âõîäíîãî_ôàéëà èìÿ_âûõîäíîãî_ôàéëà ðàçìåð_âõîäíîé_ïåðåìåííîé ðàçìåð_âûõîäíîé_ïåðåìåííîé" << std::endl;
std::cout << "íàïðèìåð: out_file.v 11 8" << std::endl;
return 0;
}
std::vector<unsigned char> inputFile = readFile(argv[1]);
std::ofstream out(argv[2], std::ofstream::out);
unsigned num = 0;
unsigned inSize = atoi(argv[3]), outSize = atoi(argv[4]);
unsigned inWidth = getWidth(inSize),
depth = pow(2, inSize),
outWidth = getWidth(outSize);
out << "width=" << outSize << ";" << std::endl;
out << "depth=" << depth << ";" << std::endl;
out << "address_radix=hex;" << std::endl;
out << "data_radix=hex;" << std::endl;
out << "content begin" << std::endl;
out.fill('0');
for(auto ch:inputFile)
{
out << std::setw(inWidth) << std::hex << num++ << ": " << std::setw(outWidth) << std::right << std::hex << (int)ch << ";" << std::endl;
}
out << "end" << std::endl;
out.close();
return 0;
}