forked from rusefi/rusefi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logicdata.cpp
500 lines (418 loc) · 8.97 KB
/
logicdata.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* @file logicdata.cpp
*
* Based on LogicdataStreamFile.java by andreika
*
* Created on: Jul 19, 2020
* @author Andrey Belomutskiy, (c) 2012-2020
*/
#include "logicdata.h"
#include <stdio.h>
#include <stdlib.h>
#include <cstdint>
#include <string.h>
#define frequency 1000000
#define frequencyDiv 10
#define magic 0x7f
#define BLOCK 0x15
#define CHANNEL_BLOCK 0x16
#define SUB 0x54
#define title "Data save2"
#define FLAG_NOTEMPTY 2
#define FLAG_NOTEMPTY_LONG 3
#define FLAG_EMPTY 5
#define LOGIC4 0x40FD
#define LOGIC8 0x673B
#define SIGN_FLAG 0x80000000L
// todo: numChannels 7 or numChannels 8 does not work? :(
#define numChannels 6
#define reservedDurationInSamples 10
#define MAX_STRING_SIZE 40
static char channelNames[][MAX_STRING_SIZE] = { "Primary", "Secondary", "TDC",
"Sync", "Coil", "Injector", "Channel 6", "Channel 7" };
static int CHANNEL_FLAGS[] = { 0x13458b, 0x0000ff, 0x00a0f9, 0x00ffff, 0x00ff00,
0xff0000, 0xf020a0, };
static FILE *ptr;
static int realDurationInSamples;
static int scaledDurationInSamples;
static void writeByte(uint8_t value) {
fwrite(&value, 1, sizeof(value), ptr);
}
static void writeAs(int64_t value, int numBytes) {
if (value == 0) {
writeByte(0);
} else {
writeByte(numBytes);
for (int i = 0; i < numBytes; i++) {
writeByte((uint8_t) ((value >> (i * 8)) & 0xff));
}
}
}
// This is the main secret of this format! :)
static void write(int64_t value) {
if (value < 0 || value > 0xFFFFFFFFL) {
writeAs(value, 8);
} else if (value == 0) {
writeByte(0);
} else if (value <= 0xff) {
writeAs(value, 1);
} else if (value <= 0xffff) {
writeAs(value, 2);
} else if (value <= 0xffffff) {
writeAs(value, 3);
} else {
writeAs(value, 4);
}
}
static void writeString(const char *value) {
int len = strlen(value);
write(len);
for (int i = 0; i < len; i++) {
writeByte(value[i]);
}
}
// todo: some C++ magic would allow us to drop 'count' parameter
// todo: Look at efi::size in util
static void write(int values[], int count) {
for (int i = 0; i < count; i++) {
write(values[i]);
}
}
static void write(int value, int num) {
for (int i = 0; i < num; i++)
write(value);
}
static void writeId(int i1, int i2) {
write((numChannels == 4) ? LOGIC4 : LOGIC8);
write(i1);
write(i2);
}
static void writeHeader() {
writeByte(magic);
write(strlen(title));
writeString(title);
write(BLOCK);
write(SUB);
write(frequency);
write(0);
write(reservedDurationInSamples);
write(frequency / frequencyDiv);
write(0, 2);
write(numChannels);
write(BLOCK);
write(0);
write(BLOCK);
for (int i = 0; i < numChannels; i++) {
writeId(i, 1);
}
write(0);
write(BLOCK);
writeId(0, 0);
write(0);
write(0);
}
static void writeDouble(double value) {
static_assert(sizeof(double) == 8);
if (value == 0.0) {
writeByte(0);
} else {
writeByte(8);
char *ptr = (char*) (void*) &value;
for (int i = 0; i < 8; i++) {
writeByte(ptr[i]);
}
}
}
static void writeChannelHeader(int ch) {
write(0xff);
write(ch);
writeString(channelNames[ch]);
write(0, 2);
writeDouble(1.0);
write(0);
writeDouble(0.0);
write(1); // or 2
writeDouble(0.0); // or 1.0
// this part sounds like the 'next' pointer?
if (ch == numChannels - 1) {
write(0);
} else {
writeId(1 + ch, 1);
for (int i = 0; i < 3; i++) {
write((CHANNEL_FLAGS[ch] >> (i * 8)) & 0xff);
}
}
}
static void writeEdges(int64_t *chDeltas, bool useLongDeltas, int numEdges) {
for (int i = 0; i < numEdges; i++) {
uint64_t d = chDeltas[i];
// set 16-bit 'sign' flag
if (!useLongDeltas && (d & SIGN_FLAG) == SIGN_FLAG)
d = (d & 0x7fff) | (SIGN_FLAG >> 16);
writeByte((uint8_t) (d & 0xff));
writeByte((uint8_t) ((d >> 8) & 0xff));
if (useLongDeltas) {
writeByte((uint8_t) ((d >> 16) & 0xff));
writeByte((uint8_t) ((d >> 24) & 0xff));
}
}
writeByte(0x00);
}
static void writeRaw(int value, int num) {
for (int i = 0; i < num; i++) {
writeByte(value);
}
}
static void writeChannelData(int ch, int64_t *chDeltas, int chLastState,
int lastRecord, bool useLongDeltas, int numEdges) {
if (numEdges == 0)
lastRecord = 0;
write(CHANNEL_BLOCK);
// channel#0 is somehow special...
if (ch == 0) {
write(SUB);
write(BLOCK);
}
write(ch + 1);
write(0);
write(realDurationInSamples);
write(1);
write(lastRecord);
int numSamplesLeft = realDurationInSamples - lastRecord;
write(numSamplesLeft);
write(chLastState);
int chFlag =
(numEdges == 0) ?
FLAG_EMPTY :
(useLongDeltas ? FLAG_NOTEMPTY_LONG : FLAG_NOTEMPTY);
write(chFlag);
if (ch == 0) {
write(0);
write(BLOCK);
write(0, 11);
if (useLongDeltas) {
write(BLOCK);
write(0, 6);
}
write(BLOCK);
} else {
write(0, 10);
if (useLongDeltas) {
write(0, 5);
}
}
write(numEdges);
write(0);
write(numEdges);
write(0);
write(numEdges);
writeEdges(chDeltas, useLongDeltas, numEdges);
if (ch == 0) {
write(BLOCK);
write(0, 6);
if (!useLongDeltas) {
write(BLOCK);
write(0, 6);
}
write(BLOCK);
} else {
write(0, 4);
if (!useLongDeltas) {
write(0, 5);
}
}
if (numEdges == 0) {
write(0, 5);
return;
}
write(1);
write(0);
write(1);
write(0);
write(1);
write(0, 16);
writeRaw(0xFF, 8);
writeRaw(chFlag, 1);
writeRaw(0x00, 7);
}
static void writeChannelDataHeader() {
write(BLOCK);
write(scaledDurationInSamples);
write(0, 5);
write(numChannels);
write(0, 3);
writeId(0, 1);
write(0);
write(BLOCK);
write(0, 3);
for (int i = 0; i < numChannels; i++) {
writeChannelHeader(i);
}
write(BLOCK);
int SUB_ARRAY[] = { SUB, SUB, 0, SUB, 0, SUB };
write(SUB_ARRAY, 6);
write(0, 6);
write(BLOCK);
write(0, 2);
write(realDurationInSamples);
write(0);
write(SUB);
write(reservedDurationInSamples);
write(frequency / frequencyDiv);
write(0, 2);
write(SUB);
write(0, 2);
write(1);
write(0, 3);
writeId(0, 0);
write(BLOCK);
int SAM_ARRAY[] = { realDurationInSamples, realDurationInSamples,
realDurationInSamples };
write(SAM_ARRAY, 3);
write(0);
write(SUB);
write(0);
write(BLOCK);
write(0);
write(BLOCK);
write(SUB, 4);
write(0);
write(BLOCK);
write(frequency);
write(0, 3);
write(1);
write(0, 3);
writeId(0, 0);
int ARR_6[] = { 0, 1, 1, 0, 1, 0x13 };
write(ARR_6, 6);
write(SUB);
write(BLOCK);
write(0);
write(realDurationInSamples);
write(0, 2);
write(numChannels);
int ARR_3[] = { 1, 0, 1 };
write(ARR_3, 3);
}
static void writeTimingMarker() {
write(BLOCK);
write(numChannels + 2);
write(0, 4);
writeString("Timing Marker Pair");
writeString("A1");
writeString("A2");
write(0, 2);
write(SUB);
write(0, 9);
}
static void writeChannelDataFooter() {
write(0, 3);
write(1);
write(1);
write(0);
write(numChannels);
}
static int getChannelState(int ch, CompositeEvent *event) {
switch (ch) {
case 0:
return event->primaryTrigger;
case 1:
return event->secondaryTrigger;
case 2:
return event->isTDC;
case 3:
return event->sync;
case 4:
return event->coil;
case 5:
return event->injector;
}
return -1;
}
static void writeEvents(CompositeEvent *events, int count) {
// we need at least 2 records
if (count < 2)
return;
int firstRecordTs = events[1].timestamp;
int lastRecordTs = events[count - 1].timestamp;
// we don't know the total duration, so we create a margin after the last record which equals to the duration of the first event
realDurationInSamples = lastRecordTs + firstRecordTs;
scaledDurationInSamples = realDurationInSamples / 4;
writeChannelDataHeader();
int64_t *chDeltas = (int64_t*) malloc(sizeof(int64_t) * count);
bool useLongDeltas = false;
for (int ch = 0; ch < numChannels; ch++) {
int chPrevState = -1;
int prevTs = 0;
int deltaCount = 0;
for (int i = 0; i < count; i++) {
CompositeEvent *event = &events[i];
int chState = getChannelState(ch, event);
int ts = event->timestamp;
if (chPrevState == -1) {
chPrevState = chState;
}
if (chState != chPrevState) {
long delta = ts - prevTs;
if (delta > 0x7fff) {
useLongDeltas = true;
}
// encode state
if (chState == 0)
delta |= SIGN_FLAG;
chDeltas[deltaCount++] = delta;
prevTs = ts;
chPrevState = chState;
}
}
writeChannelData(ch, chDeltas, chPrevState, prevTs, useLongDeltas,
deltaCount);
}
free(chDeltas);
writeChannelDataFooter();
}
static void writeFooter() {
write(BLOCK);
for (int i = 0; i < numChannels; i++) {
writeId(i, 1);
}
write(1);
writeId(numChannels, 0x15);
for (int i = 0; i < numChannels; i++) {
writeId(i, 1);
}
write(1);
write(0);
write(frequency);
write(0, 16);
write(0x01);
write(0x23); // ???
write(SUB);
write(BLOCK);
write(numChannels + 1);
write(0);
write(0xFFFFFFFFFFFFFFFFL);
write(0xFFFFFFFFL);
write(1);
write(0, 3);
write(BLOCK);
write(0);
write(BLOCK);
write(0);
writeDouble(1.0);
write(SUB);
write(0, 6);
write(1);
write(0, 4);
write(1);
write(0x29); // ???
write(SUB);
writeTimingMarker();
}
void writeFile(const char * fileName, CompositeEvent *events, int count) {
ptr = fopen(fileName, "wb");
writeHeader();
writeEvents(events, count);
writeFooter();
fclose(ptr);
}