Line data Source code
1 : #include "cfoInterfaceLib/CFO_Compiler.hh"
2 :
3 : #include <cstring>
4 : #include <fstream>
5 : #include <iostream>
6 :
7 : std::ifstream inFile;
8 : std::fstream setupFile;
9 : std::ofstream outFile;
10 : int FPGAClock = 40000000;
11 :
12 : /********************************************
13 : Program Setup Function
14 : ********************************************/
15 0 : void help()
16 : {
17 0 : std::ifstream setupFile("setup.in");
18 0 : std::string clockSpeed;
19 0 : std::string inputFile;
20 0 : std::string outputFile;
21 0 : getline(setupFile, clockSpeed, '=');
22 0 : getline(setupFile, clockSpeed);
23 0 : getline(setupFile, inputFile, '=');
24 0 : getline(setupFile, inputFile);
25 0 : getline(setupFile, outputFile, '=');
26 0 : getline(setupFile, outputFile);
27 0 : setupFile.close();
28 0 : std::cout << "\n***************************************" << std::endl;
29 0 : std::cout << "This is the compiler for the MU2E's CFO" << std::endl;
30 0 : std::cout << "***************************************" << std::endl;
31 0 : std::cout << "To change the default clock speed along with input and output sources use ./CFO_Compiler --new\n"
32 0 : << std::endl;
33 0 : std::cout << "The default FPGA Clock Speed is \"" << clockSpeed << "Hz\"" << std::endl;
34 0 : std::cout << "The default input source file is \"" << inputFile << "\"" << std::endl;
35 0 : std::cout << "The default output source file is \"" << outputFile << "\"" << std::endl;
36 0 : std::cout << "\nTo use non-default input source file, use the first command line argument" << std::endl;
37 0 : std::cout << "To use non-default output file, use the second command line argument" << std::endl;
38 0 : std::cout << "Clock Speed can only be changed through the [--new] command" << std::endl;
39 :
40 0 : std::cout << "\nE.g. ./CFO_Compiler [input file name] [output binary filename]\n"
41 0 : << std::endl;
42 :
43 0 : std::cout << "\nFor more information on this compiler, look at the CFO's Compiler and Interpreter Mu2e Guides."
44 0 : << std::endl;
45 0 : }
46 :
47 0 : void newIOFiles()
48 : {
49 0 : std::string inputFile;
50 0 : std::string outputFile;
51 0 : std::string clockSpeed;
52 0 : setupFile.open("setup.in", std::ios::out | std::ios::trunc);
53 :
54 0 : std::cout << "What's the FPGA Clock Speed Being Used (Hz)? \n(Used to calculate time units in instructions) ";
55 0 : std::cin >> clockSpeed;
56 0 : setupFile << "clockSpeed=";
57 0 : for (size_t i = 0; i < clockSpeed.size(); i++)
58 : {
59 0 : setupFile << clockSpeed[i];
60 : }
61 0 : setupFile << "\n";
62 :
63 0 : std::cout << "What's the name of DEFAULT input source? ";
64 0 : std::cin >> inputFile;
65 0 : setupFile << "InputFile=";
66 0 : for (size_t i = 0; i < inputFile.size(); i++)
67 : {
68 0 : setupFile << inputFile[i];
69 : }
70 0 : setupFile << "\n";
71 :
72 0 : std::cout << "What's the name of DEFAULT output source? ";
73 0 : std::cin >> outputFile;
74 0 : setupFile << "OutputFile=";
75 0 : for (size_t i = 0; i < outputFile.size(); i++)
76 : {
77 0 : setupFile << outputFile[i];
78 : }
79 0 : setupFile.close();
80 0 : std::cout << "DEFAULT input file: " << inputFile << std::endl;
81 0 : std::cout << "DEFAULT output file: " << outputFile << std::endl;
82 0 : std::cout << "DEFAULT Clock Speeed: " << clockSpeed << "Hz (" << atoi(clockSpeed.c_str()) / 1000000 << "MHz)"
83 0 : << std::endl;
84 : std::cout
85 : << "\nThanks! Run again the ./CFO_Compiler with your new DEFAULT Files"
86 : << "\n*To setup new default files use \"./Compiler -new\" "
87 : << "\n*To run compiler without changing the DEFAULT files, use \"./CFO_Compiler [inputsource] [outputsource]"
88 0 : << "\n*To repeat this information or if you need help use \"./CFO_Compiler --help\"" << std::endl;
89 0 : }
90 :
91 0 : void setIOFiles(int argc, char** argv)
92 : { // Sets the Input Output Files.
93 0 : std::string inFileName;
94 0 : std::string outFileName;
95 0 : std::string clockSpeed;
96 0 : setupFile.open("setup.in");
97 :
98 0 : if (argc <= 1)
99 : {
100 0 : getline(setupFile, clockSpeed, '=');
101 0 : getline(setupFile, clockSpeed);
102 0 : getline(setupFile, inFileName, '='); // ignore string before '='
103 0 : getline(setupFile, inFileName);
104 0 : getline(setupFile, outFileName, '=');
105 0 : getline(setupFile, outFileName);
106 : }
107 :
108 0 : else if (argc == 2)
109 : {
110 0 : if (strcmp(argv[1], "--help") == 0)
111 : {
112 0 : setupFile.close();
113 0 : help();
114 0 : exit(EXIT_SUCCESS);
115 : }
116 0 : else if (strcmp(argv[1], "--new") == 0)
117 : {
118 0 : setupFile.close();
119 0 : newIOFiles();
120 0 : exit(EXIT_SUCCESS);
121 : }
122 : else
123 : {
124 0 : inFileName = argv[1];
125 0 : getline(setupFile, clockSpeed, '=');
126 0 : getline(setupFile, clockSpeed);
127 0 : getline(setupFile, inFileName, '=');
128 0 : getline(setupFile, inFileName);
129 : }
130 : }
131 :
132 0 : else if (argc == 3)
133 : {
134 0 : inFileName = argv[1];
135 0 : outFileName = argv[2];
136 0 : getline(setupFile, clockSpeed, '=');
137 0 : getline(setupFile, clockSpeed);
138 : }
139 :
140 : else
141 : {
142 0 : std::cout << "Compiler can only have two or less command tail arguments: " << std::endl;
143 : }
144 :
145 0 : std::cout << "setting files..." << std::endl;
146 :
147 0 : inFile.open(inFileName.c_str(), std::ios::in);
148 0 : outFile.open(outFileName.c_str(), std::ios::out | std::ios::binary);
149 0 : if (!(setupFile.is_open()))
150 : {
151 : throw std::runtime_error(
152 0 : "Setup File (setup.in) didn't open. \nDoes it exist? \nIf not: run \"./CFO_Compiler --new\"");
153 : }
154 0 : if (!(inFile.is_open()))
155 : {
156 0 : throw std::runtime_error("Input File (" + inFileName + ") didn't open. Does it exist?");
157 : }
158 0 : if (!(outFile.is_open()))
159 : {
160 0 : throw std::runtime_error("Output File (" + outFileName + ") didn't open. Does it exist?");
161 : }
162 :
163 0 : std::cout << "Input Source: " << inFileName << std::endl;
164 0 : std::cout << "Output Binary: " << outFileName << std::endl;
165 0 : FPGAClock = atoi(clockSpeed.c_str());
166 0 : std::cout << "FPGA Clock Speed used: " << FPGAClock << "Hz" << std::endl;
167 0 : std::cout << "Done!" << std::endl
168 0 : << std::endl;
169 0 : setupFile.close();
170 0 : }
171 :
172 : /*******************
173 : ** MAIN
174 : ******************/
175 0 : int main(int argc, char* argv[])
176 : {
177 0 : std::cout << "Start " << std::endl;
178 :
179 : std::string inFileName =
180 0 : "/home/mu2ehwdev/ots/srcs/mu2e_pcie_utils/cfoInterfaceLib/Commands.txt";
181 : std::string outFileName =
182 0 : "/home/mu2ehwdev/ots/srcs/mu2e_pcie_utils/cfoInterfaceLib/Commands.bin";
183 :
184 0 : CFOLib::CFO_Compiler compiler;
185 0 : compiler.processFile(inFileName, outFileName);
186 :
187 0 : std::cout << "Done " << std::endl;
188 0 : }
|