-
Notifications
You must be signed in to change notification settings - Fork 2
/
hal-write.ulp
414 lines (327 loc) · 9.68 KB
/
hal-write.ulp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/******************************************************************************
* HAL-Configurator
*
* Author: Martin Schoeneck 2008
* Additional gates and tweaks: Ed Nisley KE4ZNU 2010
* Charalampos Alexopoulos 2011
* Ed Nisley KE4ZNU 2013
*****************************************************************************/
#usage "<h1>HAL-Configurator</h1>Start from a Schematic where symbols from hal-config.lbr are used!";
string output_path = "./";
string dev_loadrt = "LOADRT";
string dev_loadusr = "LOADUSR";
string dev_thread = "THREAD";
string dev_parameter = "PARAMETER";
string dev_names[] = {
"CONSTANT", // must be first entry to make set_constants() work
"ABS", // 2.4
"AND2",
"BLEND", // 2.4
"CHARGE-PUMP", // 2.4
"COMP",
"CONV_S32_FLOAT", // 2.4
"CONV_FLOAT_S32", // 2.4
"CONV_S32_BIT", // 2.4
"CONV_BIT_S32", // 2.4
"CONV_BIT_U32", // 2.4
"DDT", // 2.4
"DEADZONE", // 2.4
"DEBOUNCE", // 2.4
"EDGE",
"ENCODER", // 2.4
"ENCODER-RATIO", // 2.4
"ESTOP-LATCH",
"FLIPFLOP",
"FREQGEN", // 2.4
"LIMIT1", // 2.4
"LOWPASS",
"MULT2", // 2.4
"MUX2",
"MUX4", // 2.4
"MUX8", // 2.4
"NEAR", // 2.4
"NOT",
"ONESHOT",
"OR2",
"SAMPLER", // 2.4
"SCALE", // 2.4
"SIGGEN", // 2.4
"STEPGEN", // 2.4
"SELECT8", // 2.4
"SUM2",
"TIMEDELAY", // 2.4
"TOGGLE", // 2.4
"UPDOWN", // 2.4
"WCOMP", // 2.4
"XOR2", // 2.4
"" // end flag
};
/*******************************************************************************
* Global Stuff
******************************************************************************/
string FileName;
string ProjectPath;
string ProjectName;
void Info(string Message) {
dlgMessageBox(";<b>Info</b><p>\n" + Message);
}
void Warn(string Message) {
dlgMessageBox("!<b>Warning</b><p>\n" + Message + "<p>see usage");
}
void Error(string Message) {
dlgMessageBox(":<hr><b>Error</b><p>\n" + Message + "<p>see usage");
exit(1);
}
string replace(string str, char a, char b) {
// in string str replace a with b
int pos = -1;
do {
// find that character
pos = strchr(str, a);
// replace if found
if(pos >= 0) {
str[pos] = b;
}
} while(pos >= 0);
return str;
}
// the part name contains an index and is written in capital letters
string get_module_name(UL_PART P) {
// check module name, syntax: INDEX:NAME
string mod_name = strlwr(P.name);
// split string at the : if exists
string a[];
int c = strsplit(a, mod_name, ':');
mod_name = a[c-1];
// if name starts with '[' we need uppercase letters
if(mod_name[0] == '[') {
mod_name = strupr(mod_name);
}
return mod_name;
}
string comment(string mess) {
string str = "\n\n####################################################\n";
if(mess != "") {
str += "# " + mess + "\n";
}
return str;
}
// if this is a device for loading a module, load it (usr/rt)
string load_module(UL_PART P) {
string str = "";
// it's a module if the device's name starts with LOADRT/LOADUSR
if((strstr(P.device.name, dev_loadrt) == 0) ||
(strstr(P.device.name, dev_loadusr) == 0)) {
// now add the string to our script
str += P.value + "\n";
}
return str;
}
// count used digital gates (and, or, etc) and load module if neccessary
string load_blocks() {
string str = "";
int index;
int dev_counters[];
string dname[];
// count the gates that are used
schematic(S) { S.parts(P) {
strsplit(dname,P.device.name,'.'); // extract first part of name
if ("" != lookup(dev_names,dname[0],0)) {
for (index = 0; (dname[0] != dev_names[index]) ; index++) {
continue;
}
dev_counters[index]++;
}
} }
// force lowercase module names...
for (index = 0; ("" != dev_names[index]) ; index++) {
if (dev_counters[index]) {
sprintf(str,"%sloadrt %s\t\tcount=%d\n",str,strlwr(dev_names[index]),dev_counters[index]);
}
}
return str;
}
string hook_function(UL_NET N) {
string str = "";
// is this net connected to a thread (work as functions here)?
int noclkpins = 0;
string thread_name = ""; // this net should be connected to a thread
string thread_position = "";
N.pinrefs(PR) {
// this net is connected to a clk-pin
if(PR.pin.function == PIN_FUNCTION_FLAG_CLK) {
// check the part: is it a thread-device?
if(strstr(PR.part.device.name, dev_thread) == 0) {
// we need the name of the thread
thread_name = strlwr(PR.part.name);
// and we need the position (position _ is ignored)
thread_position = strlwr(PR.pin.name);
thread_position = replace(thread_position, '_', ' ');
}
} else {
// no clk-pin, this is no function-net
noclkpins++;
break;
}
}
// found a thread?
if(noclkpins == 0 && thread_name != "") {
// all the other pins are interesting now
N.pinrefs(PR) {
// this pin does not belong to the thread
if(strstr(PR.part.device.name, dev_thread) != 0) {
// name of the pin is name of the function
//string function_name = strlwr(PR.pin.name);
string function_name = strlwr(PR.instance.gate.name);
// if functionname starts with a '.', it will be appended to the modulename
if(function_name[0] == '.') {
// if the name is only a point, it will be ignored
if(strlen(function_name) == 1) {
function_name = "";
}
function_name = get_module_name(PR.part) + function_name;
}
str += "addf " + function_name + "\t\t" + thread_name + "\t" + thread_position + "\n";
}
}
}
return str;
}
string set_parameter(UL_NET N) {
string str = "";
// is this net connected to a parameter-device?
int nodotpins = 0;
string parameter_value = "";
N.pinrefs(PR) {
// this net is connected to a dot-pin
if(PR.pin.function == PIN_FUNCTION_FLAG_DOT) {
// check the part: is it a parameter-device?
// str += "** dev name [" + PR.part.device.name + "] [" + dev_parameter + "]\n";
if(strstr(PR.part.device.name, dev_parameter) == 0) {
// we need the value of that parameter
parameter_value = PR.part.value;
// str += "** value [" + PR.part.value +"]\n";
}
} else {
// no clk-pin, this is no function-net
nodotpins++;
break;
}
}
// found a parameter?
if(nodotpins == 0 && parameter_value != "") {
// all the other pins are interesting now
N.pinrefs(PR) {
// str += "** dev name [" + PR.part.device.name + "] [" + dev_parameter + "]\n";
// this pin does not belong to the parameter-device
if(strstr(PR.part.device.name, dev_parameter) != 0) {
// name of the pin is name of the function
//string parameter_name = strlwr(PR.pin.name);
string parameter_name = strlwr(PR.instance.gate.name);
// if functionname starts with a '.', it will be appended to the modulename
// str += "** param (gate) name [" + parameter_name + "]\n";
if(parameter_name[0] == '.') {
// if the name is only a point, it will be ignored
if(strlen(parameter_name) == 1) {
parameter_name = "";
}
parameter_name = get_module_name(PR.part) + parameter_name;
// str += "** param (part) name [" + parameter_name + "]\n";
}
str += "setp " + parameter_name + "\t" + parameter_value + "\n";
}
}
}
return str;
}
// if this is a 'constant'-device, set its value
// NOTE: this is hardcoded to use the first entry in the dev_names[] array!
string set_constants(UL_PART P) {
string str = "";
// 'constant'-device?
if(strstr(P.device.name, dev_names[0]) == 0) {
str += "setp " + get_module_name(P) + ".value\t" + P.value + "\n";
}
return str;
}
string connect_net(UL_NET N) {
string str = "";
// find all neccessary net-members
string pins = "";
N.pinrefs(P) {
// only non-functional pins are connected
if(P.pin.function == PIN_FUNCTION_FLAG_NONE) {
string pin_name = strlwr(P.pin.name);
string part_name = strlwr(P.part.name);
pin_name = replace(pin_name, '$', '_');
part_name = replace(part_name, '$', '_');
pins += part_name + "." + pin_name + " ";
}
}
if(pins != "") {
string net_name = strlwr(N.name);
net_name = replace(net_name, '$', '_');
str += "net " + net_name + " " + pins + "\n";
}
return str;
}
/*******************************************************************************
* Main program.
******************************************************************************/
// is the schematic editor running?
if (!schematic) {
Error("No Schematic!<br>This program will only work in the schematic editor.");
}
schematic(S) {
ProjectPath = filedir(S.name);
ProjectName = filesetext(filename(S.name), "");
}
// build configuration
string cs = "# HAL config file automatically generated by Eagle-CAD ULP:\n";
cs += "# [" + argv[0] + "]\n";
cs += "# (C) Martin Schoeneck.de 2008\n";
cs += "# Charalampos Alexopoulos 2011\n";
cs += "# Mods Ed Nisley KE4ZNU 2010 2013\n";
FileName = ProjectPath + ProjectName + ".hal";
cs += "# Path [" + ProjectPath + "]\n";
cs += "# ProjectName [" + ProjectName + "]\n";
// ask for a filename: where should we write the configuration?
FileName = dlgFileSave("Save Configuration", FileName, "*.hal");
if(!FileName) {
exit(0);
}
cs += "# File name [" + FileName + "]\n";
cs += "# Created [" + t2string(time(),"hh:mm:ss dd-MMM-yyyy") + "]\n\n";
schematic(S) {
// load modules
cs += comment("Load realtime and userspace modules");
S.parts(P) {
cs += load_module(P);
}
// load blocks
cs += load_blocks();
// add functions
cs += comment("Hook functions into threads");
S.nets(N) {
cs += hook_function(N);
}
// set parameters
cs += comment("Set parameters");
S.nets(N) {
cs += set_parameter(N);
}
// set constant values
cs += comment("Set constants");
S.parts(P) {
cs += set_constants(P);
}
// build nets and connect them
cs += comment("Connect Modules with nets");
S.nets(N) {
cs += connect_net(N);
}
}
// open/overwrite the target file to save the configuration
output(FileName, "wt") {
printf(cs);
}