Progress xsthammer scripts
[yosys.git] / tests / xsthammer / generate.cc
1
2 #include <sys/stat.h>
3 #include <sys/types.h>
4 #include <stdio.h>
5 #include <string>
6
7 const char *arg_types[][2] = {
8 { "{dir} [3:0] {name}", "{name}" }, // 00
9 { "{dir} [4:0] {name}", "{name}" }, // 01
10 { "{dir} [5:0] {name}", "{name}" }, // 02
11 { "{dir} signed [3:0] {name}", "{name}" }, // 03
12 { "{dir} signed [4:0] {name}", "{name}" }, // 04
13 { "{dir} signed [5:0] {name}", "{name}" } // 05
14 };
15
16 const char *ops[] = {
17 "+", // 00
18 "-", // 01
19 "*", // 02
20 "&&", // 03
21 "||", // 04
22 "&", // 05
23 "|", // 06
24 "^", // 07
25 "<<", // 08
26 ">>", // 09
27 "<<<", // 10
28 ">>>", // 11
29 };
30
31 void strsubst(std::string &str, const std::string &match, const std::string &replace)
32 {
33 size_t pos;
34 while ((pos = str.find(match)) != std::string::npos)
35 str.replace(pos, match.size(), replace);
36 }
37
38 int main()
39 {
40 mkdir("rtl", 0777);
41 for (int ai = 0; ai < sizeof(arg_types)/sizeof(arg_types[0]); ai++)
42 for (int bi = 0; bi < sizeof(arg_types)/sizeof(arg_types[0]); bi++)
43 for (int yi = 0; yi < sizeof(arg_types)/sizeof(arg_types[0]); yi++)
44 for (int oi = 0; oi < sizeof(ops)/sizeof(ops[0]); oi++)
45 {
46 std::string a_decl = arg_types[ai][0];
47 strsubst(a_decl, "{dir}", "input");
48 strsubst(a_decl, "{name}", "a");
49
50 std::string b_decl = arg_types[bi][0];
51 strsubst(b_decl, "{dir}", "input");
52 strsubst(b_decl, "{name}", "b");
53
54 std::string y_decl = arg_types[yi][0];
55 strsubst(y_decl, "{dir}", "output");
56 strsubst(y_decl, "{name}", "y");
57
58 std::string a_ref = arg_types[ai][1];
59 strsubst(a_ref, "{dir}", "input");
60 strsubst(a_ref, "{name}", "a");
61
62 std::string b_ref = arg_types[bi][1];
63 strsubst(b_ref, "{dir}", "input");
64 strsubst(b_ref, "{name}", "b");
65
66 std::string y_ref = arg_types[yi][1];
67 strsubst(y_ref, "{dir}", "output");
68 strsubst(y_ref, "{name}", "y");
69
70 char buffer[1024];
71 snprintf(buffer, 1024, "rtl/binary_ops_%02d%02d%02d%02d.v", ai, bi, yi, oi);
72
73 FILE *f = fopen(buffer, "w");
74 fprintf(f, "module binary_ops_%02d%02d%02d%02d(a, b, y);\n", ai, bi, yi, oi);
75 fprintf(f, "%s;\n", a_decl.c_str());
76 fprintf(f, "%s;\n", b_decl.c_str());
77 fprintf(f, "%s;\n", y_decl.c_str());
78 fprintf(f, "assign %s = %s %s %s;\n", y_ref.c_str(),
79 a_ref.c_str(), ops[oi], b_ref.c_str());
80 fprintf(f, "endmodule\n");
81 fclose(f);
82 }
83 return 0;
84 }
85