forked from Blackfrosch/VAGEDCSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EDC15MFileParser.cs
843 lines (777 loc) · 43.2 KB
/
EDC15MFileParser.cs
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace VAGSuite
{
public class EDC15MFileParser : IEDCFileParser
{
public override string ExtractInfo(byte[] allBytes)
{
// assume info will be @ 0x53452 12 bytes
string retval = string.Empty;
try
{
int partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[5] { 0x45, 0x44, 0x43, 0x20, 0x20 }, new byte[5] { 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress - 8, 12).Trim();
}
}
catch (Exception)
{
}
return retval;
}
public override string ExtractPartnumber(byte[] allBytes)
{
// assume info will be @ 0x53446 12 bytes
string retval = string.Empty;
try
{
int partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[5] { 0x45, 0x44, 0x43, 0x20, 0x20 }, new byte[5] { 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress - 20, 12).Trim();
}
}
catch (Exception)
{
}
return retval;
}
public override string ExtractSoftwareNumber(byte[] allBytes)
{
string retval = string.Empty;
try
{
int partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[5] { 0x45, 0x44, 0x43, 0x20, 0x20 }, new byte[5] { 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress + 5, 8).Trim();
retval = retval.Replace(" ", "");
}
}
catch (Exception)
{
}
return retval;
}
public override string ExtractBoschPartnumber(byte[] allBytes)
{
return Tools.Instance.ExtractBoschPartnumber(allBytes);
/*string retval = string.Empty;
try
{
int partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[5] { 0x45, 0x44, 0x43, 0x20, 0x20 }, new byte[5] { 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress + 23, 10).Trim();
}
}
catch (Exception)
{
}
return retval;*/
}
private string DetermineNumberByFlashBank(long address, List<CodeBlock> currBlocks)
{
foreach (CodeBlock cb in currBlocks)
{
if (cb.StartAddress <= address && cb.EndAddress >= address)
{
/*if (cb.CodeID == 1) return "codeblock 1";// - MAN";
if (cb.CodeID == 2) return "codeblock 2";// - AUT (hydr)";
if (cb.CodeID == 3) return "codeblock 3";// - AUT (elek)";
return cb.CodeID.ToString();*/
if (cb.BlockGearboxType == GearboxType.Automatic)
{
return "codeblock " + cb.CodeID.ToString() + " AUT";
}
return "codeblock " + cb.CodeID.ToString();
}
}
long bankNumber = address / 0x10000;
return "flashbank " + bankNumber.ToString();
}
private int DetermineCodeBlockByByAddress(long address, List<CodeBlock> currBlocks)
{
foreach (CodeBlock cb in currBlocks)
{
if (cb.StartAddress <= address && cb.EndAddress >= address)
{
return cb.CodeID;
}
}
return 0;
}
public override SymbolCollection parseFile(string filename, out List<CodeBlock> newCodeBlocks, out List<AxisHelper> newAxisHelpers)
{
newCodeBlocks = new List<CodeBlock>();
SymbolCollection newSymbols = new SymbolCollection();
newAxisHelpers = new List<AxisHelper>();
byte[] allBytes = File.ReadAllBytes(filename);
string boschnumber = ExtractBoschPartnumber(allBytes);
string softwareNumber = ExtractSoftwareNumber(allBytes);
partNumberConverter pnc = new partNumberConverter();
VerifyCodeBlocks(allBytes, newSymbols, newCodeBlocks);
for (int t = 0; t < allBytes.Length - 1; t += 2)
{
int len2skip = 0;
if (CheckMap(t, allBytes, newSymbols, newCodeBlocks, out len2skip))
{
if (len2skip > 2) len2skip -= 2; // make sure we don't miss maps
if ((len2skip % 2) > 0) len2skip -= 1;
if (len2skip < 0) len2skip = 0;
// t += len2skip;
// Console.WriteLine("map detected: " + t.ToString("X8"));
}
}
newSymbols.SortColumn = "Flash_start_address";
newSymbols.SortingOrder = GenericComparer.SortOrder.Ascending;
newSymbols.Sort();
NameKnownMaps(allBytes, newSymbols, newCodeBlocks);
BuildAxisIDList(newSymbols, newAxisHelpers);
MatchAxis(newSymbols, newAxisHelpers);
FindSVBL(allBytes, filename, newSymbols, newCodeBlocks);
SymbolTranslator strans = new SymbolTranslator();
foreach (SymbolHelper sh in newSymbols)
{
sh.Description = strans.TranslateSymbolToHelpText(sh.Varname);
}
// check for must have maps... if there are maps missing, report it
return newSymbols;
}
private void MatchAxis(SymbolCollection newSymbols, List<AxisHelper> newAxisHelpers)
{
foreach (SymbolHelper sh in newSymbols)
{
if (!sh.YaxisAssigned)
{
foreach (AxisHelper ah in newAxisHelpers)
{
if (sh.X_axis_ID == ah.AxisID)
{
sh.Y_axis_descr = ah.Description;
sh.YaxisUnits = ah.Units;
sh.Y_axis_offset = ah.Offset;
sh.Y_axis_correction = ah.Correction;
break;
}
}
}
if (!sh.XaxisAssigned)
{
foreach (AxisHelper ah in newAxisHelpers)
{
if (sh.Y_axis_ID == ah.AxisID)
{
sh.X_axis_descr = ah.Description;
sh.XaxisUnits = ah.Units;
sh.X_axis_offset = ah.Offset;
sh.X_axis_correction = ah.Correction;
break;
}
}
}
}
}
private void BuildAxisIDList(SymbolCollection newSymbols, List<AxisHelper> newAxisHelpers)
{
foreach (SymbolHelper sh in newSymbols)
{
if (!sh.Varname.StartsWith("2D") && !sh.Varname.StartsWith("3D"))
{
AddToAxisCollection(newAxisHelpers, sh.Y_axis_ID, sh.X_axis_descr, sh.XaxisUnits, sh.X_axis_correction, sh.X_axis_offset);
AddToAxisCollection(newAxisHelpers, sh.X_axis_ID, sh.Y_axis_descr, sh.YaxisUnits, sh.Y_axis_correction, sh.Y_axis_offset);
}
}
}
private void AddToAxisCollection(List<AxisHelper> newAxisHelpers, int ID, string descr, string units, double correction, double offset)
{
if (ID == 0) return;
foreach (AxisHelper ah in newAxisHelpers)
{
if (ah.AxisID == ID) return;
}
AxisHelper ahnew = new AxisHelper();
ahnew.AxisID = ID;
ahnew.Description = descr;
ahnew.Units = units;
ahnew.Correction = correction;
ahnew.Offset = offset;
newAxisHelpers.Add(ahnew);
}
public override void FindSVBL(byte[] allBytes, string filename, SymbolCollection newSymbols, List<CodeBlock> newCodeBlocks)
{
bool found = true;
int offset = 0;
while (found)
{
int SVBLAddress = Tools.Instance.findSequence(allBytes, offset, new byte[10] { 0xDF, 0x7A, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0xDF, 0x7A }, new byte[10] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });
if (SVBLAddress > 0)
{
//Console.WriteLine("Alternative SVBL " + SVBLAddress.ToString("X8"));
SymbolHelper shsvbl = new SymbolHelper();
shsvbl.Category = "Detected maps";
shsvbl.Subcategory = "Limiters";
shsvbl.Flash_start_address = SVBLAddress - 2;
// if value = 0xC3 0x00 -> two more back
int[] testValue = Tools.Instance.readdatafromfileasint(filename, (int)shsvbl.Flash_start_address, 1, EDCFileType.EDC15M);
if (testValue[0] == 0xC300) shsvbl.Flash_start_address -= 2;
shsvbl.Varname = "SVBL Boost limiter [" + DetermineNumberByFlashBank(shsvbl.Flash_start_address, newCodeBlocks) + "]";
shsvbl.Length = 2;
shsvbl.CodeBlock = DetermineCodeBlockByByAddress(shsvbl.Flash_start_address, newCodeBlocks);
newSymbols.Add(shsvbl);
offset = SVBLAddress + 1;
}
else found = false;
}
}
public override void NameKnownMaps(byte[] allBytes, SymbolCollection newSymbols, List<CodeBlock> newCodeBlocks)
{
SymbolAxesTranslator st = new SymbolAxesTranslator();
foreach (SymbolHelper sh in newSymbols)
{
// sh.X_axis_descr = st.TranslateAxisID(sh.X_axis_ID);
// sh.Y_axis_descr = st.TranslateAxisID(sh.Y_axis_ID);
if (sh.Length == 450)
{
if (sh.X_axis_length == 15 && sh.Y_axis_length == 15)
{
if (sh.X_axis_ID / 256 == 0xE0 && sh.Y_axis_ID / 256 == 0xFC)
{
sh.Category = "Detected maps";
sh.Subcategory = "Fuel";
int injDurCount = GetMapNameCountForCodeBlock("Start of injection (SOI)", sh.CodeBlock, newSymbols, false);
double tempRange = 0;// GetTemperatureSOIRange(sh.MapSelector, injDurCount - 1);
sh.Varname = "Start of injection (SOI) " + tempRange.ToString() + " °C [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";// " + sh.Flash_start_address.ToString("X8") + " " + sh.X_axis_ID.ToString("X4") + " " + sh.Y_axis_ID.ToString("X4");
sh.Y_axis_descr = "Engine speed (rpm)";
sh.YaxisUnits = "rpm";
sh.X_axis_correction = 0.01; // TODODONE : Check for x or y
sh.XaxisUnits = "mg/st";
sh.X_axis_descr = "IQ (mg/stroke)";
sh.Correction = 0.023437;
sh.Z_axis_descr = "Start position (degrees BTDC)";
}
}
}
//12x13 E0 DD = EGR
if (sh.Length == 312)
{
if (sh.X_axis_length == 13 && sh.Y_axis_length == 12)
{
if (sh.X_axis_ID / 256 == 0xE0 && sh.Y_axis_ID / 256 == 0xDD)
{
sh.Category = "Detected maps";
sh.Subcategory = "Misc";
int egrCount = GetMapNameCountForCodeBlock("EGR", sh.CodeBlock, newSymbols, false);
sh.Varname = "EGR " + egrCount.ToString("D2") + " [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";
sh.Correction = 0.1;
sh.X_axis_correction = 0.01;
sh.Z_axis_descr = "Mass Air Flow (mg/stroke)";
sh.X_axis_descr = "IQ (mg/stroke)";
sh.Y_axis_descr = "Engine speed (rpm)";
sh.YaxisUnits = "rpm";
sh.XaxisUnits = "mg/st";
}
}
}
else if (sh.Length == 208)
{
if (sh.X_axis_length == 13 && sh.Y_axis_length == 8)
{
if (sh.X_axis_ID / 256 == 0xE0 && sh.Y_axis_ID / 256 == 0xC1)
{
sh.Category = "Detected maps";
sh.Subcategory = "Misc";
sh.Varname = "Driver wish [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";
sh.Correction = 0.01;
sh.X_axis_correction = 0.01;
sh.X_axis_descr = "Throttle position";
sh.Z_axis_descr = "Requested IQ (mg)";
sh.Y_axis_descr = "Engine speed (rpm)";
sh.YaxisUnits = "rpm";
sh.XaxisUnits = "TPS %";
}
}
}
else if (sh.Length == 182) // 7*13
{
if (sh.X_axis_ID / 256 == 0xE0 && sh.Y_axis_ID / 256 == 0xC1)
{
sh.Category = "Detected maps";
sh.Subcategory = "Misc";
sh.Varname = "Driver wish [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";
sh.Correction = 0.01;
sh.X_axis_correction = 0.01;
sh.X_axis_descr = "Throttle position";
sh.Z_axis_descr = "Requested IQ (mg)";
sh.Y_axis_descr = "Engine speed (rpm)";
sh.YaxisUnits = "rpm";
sh.XaxisUnits = "TPS %";
}
}
else if (sh.Length == 176)
{
if (sh.X_axis_length == 11 && sh.Y_axis_length == 8)
{
if (sh.X_axis_ID / 256 == 0xE0 && sh.Y_axis_ID / 256 == 0xC2)
{
int smCount = GetMapNameCountForCodeBlock("Smoke limiter", sh.CodeBlock, newSymbols, false);
sh.Category = "Detected maps";
sh.Subcategory = "Limiters";
sh.Varname = "Smoke limiter [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";
sh.Z_axis_descr = "Maximum IQ (mg)";
sh.Y_axis_descr = "Engine speed (rpm)";
sh.X_axis_descr = "Airflow mg/stroke";
sh.Correction = 0.01;
sh.X_axis_correction = 0.1;
sh.YaxisUnits = "rpm";
sh.XaxisUnits = "mg/st";
}
}
}
else if (sh.Length == 40)
{
if (sh.X_axis_length == 20 && sh.Y_axis_length == 1)
{
if (sh.X_axis_ID / 256 == 0xE0)
{
sh.Category = "Detected maps";
sh.Subcategory = "Limiters";
int lmCount = GetMapNameCountForCodeBlock("Torque limiter", sh.CodeBlock, newSymbols, false);
sh.Varname = "Torque limiter (" + lmCount.ToString() + ") [" + DetermineNumberByFlashBank(sh.Flash_start_address, newCodeBlocks) + "]";
sh.Z_axis_descr = "Maximum IQ (mg)";
//sh.Y_axis_descr = "Atm. pressure (mbar)";
sh.Y_axis_descr = "Engine speed (rpm)";
sh.Correction = 0.01;
sh.YaxisUnits = "rpm";
}
}
}
}
}
private bool MapContainsNegativeValues(byte[] allBytes, SymbolHelper sh)
{
for (int i = 0; i < sh.Length; i += 2)
{
int currval = Convert.ToInt32(allBytes[sh.Flash_start_address + i + 1]) * 256 + Convert.ToInt32(allBytes[sh.Flash_start_address + i]);
if (currval > 0xF000) return true;
}
return false;
}
private int GetMapNameCountForCodeBlock(string varName, int codeBlock, SymbolCollection newSymbols, bool debug)
{
int count = 0;
if (debug) Console.WriteLine("Check " + varName + " " + codeBlock);
foreach (SymbolHelper sh in newSymbols)
{
if (debug)
{
if (!sh.Varname.StartsWith("2D") && !sh.Varname.StartsWith("3D"))
{
Console.WriteLine(sh.Varname + " " + sh.CodeBlock);
}
}
if (sh.Varname.StartsWith(varName) && sh.CodeBlock == codeBlock)
{
if (debug) Console.WriteLine("Found " + sh.Varname + " " + sh.CodeBlock);
count++;
}
}
count++;
return count;
}
private int CheckCodeBlock(int offset, byte[] allBytes, SymbolCollection newSymbols, List<CodeBlock> newCodeBlocks)
{
int codeBlockID = 0;
try
{
int endOfTable = Convert.ToInt32(allBytes[offset + 0x01000]) + Convert.ToInt32(allBytes[offset + 0x01001]) * 256 + offset;
int codeBlockAddress = Convert.ToInt32(allBytes[offset + 0x01002]) + Convert.ToInt32(allBytes[offset + 0x01003]) * 256 + offset;
if (endOfTable == offset + 0xC3C3) return 0;
codeBlockID = Convert.ToInt32(allBytes[codeBlockAddress]) + Convert.ToInt32(allBytes[codeBlockAddress + 1]) * 256;
foreach (CodeBlock cb in newCodeBlocks)
{
if (cb.StartAddress <= codeBlockAddress && cb.EndAddress >= codeBlockAddress)
{
cb.CodeID = codeBlockID;
cb.AddressID = codeBlockAddress;
}
}
}
catch (Exception)
{
}
return codeBlockID;
}
private bool AddToSymbolCollection(SymbolCollection newSymbols, SymbolHelper newSymbol, List<CodeBlock> newCodeBlocks)
{
if (newSymbol.Length >= 800) return false;
foreach (SymbolHelper sh in newSymbols)
{
if (sh.Flash_start_address == newSymbol.Flash_start_address)
{
// Console.WriteLine("Already in collection: " + sh.Flash_start_address.ToString("X8"));
return false;
}
}
newSymbols.Add(newSymbol);
newSymbol.CodeBlock = DetermineCodeBlockByByAddress(newSymbol.Flash_start_address, newCodeBlocks);
return true;
}
private bool isValidLength(int length, int id)
{
int idstrip = id / 256;
if (idstrip == 0xEB /*|| idstrip == 0xDE*/)
{
if (length > 0 && length <= 32) return true;
}
else
{
if (length > 0 && length < 32) return true;
}
return false;
}
private bool isAxisID(int id)
{
int idstrip = id / 256;
if (idstrip == 0xDB) return true;
if (idstrip == 0xC0 || idstrip == 0xC1 || idstrip == 0xC2 || idstrip == 0xC4 || idstrip == 0xC5) return true;
if (idstrip == 0xE0 || idstrip == 0xE4 || idstrip == 0xE5 || idstrip == 0xE9 || idstrip == 0xEA || idstrip == 0xEB || idstrip == 0xEC) return true;
if (idstrip == 0xDA || idstrip == 0xDC || idstrip == 0xDD || idstrip == 0xDE) return true;
if (idstrip == 0xF9 || idstrip == 0xFE || idstrip == 0xFC) return true;
if (idstrip == 0xD7 || idstrip == 0xE6) return true;
if (idstrip == 0xD5) return true;
return false;
}
// we need to check AHEAD for selector maps
// if these are present we may be facing a complex map structure
// which we need to handle in a special way (selectors always have data like 00 01 00 02 00 03 00 04 etc)
private bool CheckMap(int t, byte[] allBytes, SymbolCollection newSymbols, List<CodeBlock> newCodeBlocks, out int len2Skip)
{
len2Skip = 0;
bool retval = false;
bool mapFound = false;
bool _dontGenMaps = false;
List<MapSelector> mapSelectors = new List<MapSelector>();
if (t < allBytes.Length - 0x100)
{
if (CheckAxisCount(t, allBytes, out mapSelectors) > 3)
{
// check for selectors as well, and count them in the process
Console.WriteLine("Offset " + t.ToString("X8") + " has more than 3 consecutive axis");
/*foreach (MapSelector ms in mapSelectors)
{
Console.WriteLine("selector: " + ms.StartAddress.ToString("X8") + " " + ms.MapLength.ToString() + " " + ms.NumRepeats.ToString());
}*/
_dontGenMaps = true;
}
int xaxisid = (Convert.ToInt32(allBytes[t + 1]) * 256) + Convert.ToInt32(allBytes[t]);
if (isAxisID(xaxisid))
{
int xaxislen = (Convert.ToInt32(allBytes[t + 3]) * 256) + Convert.ToInt32(allBytes[t + 2]);
// Console.WriteLine("Valid XID: " + xaxisid.ToString("X4") + " @" + t.ToString("X8") + " len: " + xaxislen.ToString("X2"));
if (isValidLength(xaxislen, xaxisid))
{
//Console.WriteLine("Valid XID: " + xaxisid.ToString("X4") + " @" + t.ToString("X8") + " len: " + xaxislen.ToString("X2"));
// misschien is er nog een as
int yaxisid = (Convert.ToInt32(allBytes[t + 5 + (xaxislen * 2)]) * 256) + Convert.ToInt32(allBytes[t + 4 + (xaxislen * 2)]);
int yaxislen = (Convert.ToInt32(allBytes[t + 7 + (xaxislen * 2)]) * 256) + Convert.ToInt32(allBytes[t + 6 + (xaxislen * 2)]);
if (isAxisID(yaxisid) && isValidLength(yaxislen, yaxisid))
{
// 3d map
int zaxisid = (Convert.ToInt32(allBytes[t + 9 + (xaxislen * 2) + (yaxislen * 2)]) * 256) + Convert.ToInt32(allBytes[t + 8 + (xaxislen * 2) + (yaxislen * 2)]);
//Console.WriteLine("Valid YID: " + yaxisid.ToString("X4") + " @" + t.ToString("X8") + " len: " + yaxislen.ToString("X2"));
//Console.WriteLine(t.ToString("X8") + " XID: " + xaxisid.ToString("X4") + " XLEN: " + xaxislen.ToString("X2") + " YID: " + yaxisid.ToString("X4") + " YLEN: " + yaxislen.ToString("X2"));
SymbolHelper newSymbol = new SymbolHelper();
newSymbol.X_axis_length = xaxislen;
newSymbol.Y_axis_length = yaxislen;
newSymbol.X_axis_ID = xaxisid;
newSymbol.Y_axis_ID = yaxisid;
newSymbol.X_axis_address = t + 4;
newSymbol.Y_axis_address = t + 8 + (xaxislen * 2);
newSymbol.Length = xaxislen * yaxislen * 2;
newSymbol.Flash_start_address = t + 8 + (xaxislen * 2) + (yaxislen * 2);
if (isAxisID(zaxisid))
{
int zaxislen = (Convert.ToInt32(allBytes[t + 11 + (xaxislen * 2) + (yaxislen * 2)]) * 256) + Convert.ToInt32(allBytes[t + 10 + (xaxislen * 2) + (yaxislen * 2)]);
int zaxisaddress = t + 12 + (xaxislen * 2) + (yaxislen * 2);
if (isValidLength(zaxislen, zaxisid))
{
// newSymbol.Flash_start_address += 0x10; // dan altijd 16 erbij
int len2skip = (4 + zaxislen * 2);
if (len2skip < 16) len2skip = 16; // at least 16 bytes
newSymbol.Flash_start_address += len2skip;
if (!_dontGenMaps)
{
// this has something to do with repeating several times with the same axis set
Console.WriteLine("Added " + len2skip.ToString() + " because of z axis " + newSymbol.Flash_start_address.ToString("X8"));
}
else
{
int maxisid = (Convert.ToInt32(allBytes[t + 13 + (xaxislen * 2) + (yaxislen * 2) + (zaxislen * 2)]) * 256) + Convert.ToInt32(allBytes[t + 12 + (xaxislen * 2) + (yaxislen * 2) + zaxislen * 2]);
int maxislen = (Convert.ToInt32(allBytes[t + 15 + (xaxislen * 2) + (yaxislen * 2) + (zaxislen * 2)]) * 256) + Convert.ToInt32(allBytes[t + 14 + (xaxislen * 2) + (yaxislen * 2) + zaxislen * 2]);
//maxislen *= 2;
int maxisaddress = t + 16 + (xaxislen * 2) + (yaxislen * 2);
if (isAxisID(maxisid))
{
newSymbol.Flash_start_address += (maxislen * 2) + 4;
}
// special situation, handle selectors
Console.WriteLine("Map start address = " + newSymbol.Flash_start_address.ToString("X8"));
long lastFlashAddress = newSymbol.Flash_start_address;
foreach (MapSelector ms in mapSelectors)
{
// check the memory size between the start of the map and the
// start of the map selector
long memsize = ms.StartAddress - lastFlashAddress;
memsize /= 2; // in words
if (ms.NumRepeats > 0)
{
int mapsize = Convert.ToInt32(memsize) / ms.NumRepeats;
//Console.WriteLine("selector: " + ms.StartAddress.ToString("X8") + " " + ms.MapLength.ToString() + " " + ms.NumRepeats.ToString());
//Console.WriteLine("memsize = " + memsize.ToString() + " mapsize " + mapsize.ToString());
//Console.WriteLine("starting at address: " + lastFlashAddress.ToString("X8"));
if ((xaxislen * yaxislen) == mapsize)
{
// first axis set
for (int i = 0; i < ms.NumRepeats; i++)
{
SymbolHelper shGen2 = new SymbolHelper();
shGen2.MapSelector = ms;
shGen2.X_axis_length = newSymbol.X_axis_length;
shGen2.Y_axis_length = newSymbol.Y_axis_length;
shGen2.X_axis_ID = newSymbol.X_axis_ID;
shGen2.Y_axis_ID = newSymbol.Y_axis_ID;
shGen2.X_axis_address = newSymbol.X_axis_address;
shGen2.Y_axis_address = newSymbol.Y_axis_address;
shGen2.Length = mapsize * 2;
//shGen2.Category = "Generated";
long address = lastFlashAddress;
shGen2.Flash_start_address = address;
//shGen2.Correction = 0.023437; // TEST
//shGen2.Varname = "Generated* " + shGen2.Flash_start_address.ToString("X8") + " " + ms.StartAddress.ToString("X8") + " " + ms.NumRepeats.ToString() + " " + i.ToString();
shGen2.Varname = "3D " + shGen2.Flash_start_address.ToString("X8") + " " + shGen2.X_axis_ID.ToString("X4") + " " + shGen2.Y_axis_ID.ToString("X4");
retval = AddToSymbolCollection(newSymbols, shGen2, newCodeBlocks);
if (retval)
{
mapFound = true;
len2Skip += (xaxislen * 2) + (yaxislen * 2) + shGen2.Length;
t += (xaxislen * 2) + (yaxislen * 2) + shGen2.Length;
}
lastFlashAddress = address + mapsize * 2;
// Console.WriteLine("Set last address to " + lastFlashAddress.ToString("X8"));
}
lastFlashAddress += ms.NumRepeats * 4 + 4;
}
else if ((zaxislen * maxislen) == mapsize)
{
// second axis set
for (int i = 0; i < ms.NumRepeats; i++)
{
SymbolHelper shGen2 = new SymbolHelper();
shGen2.MapSelector = ms;
shGen2.X_axis_length = maxislen;
shGen2.Y_axis_length = zaxislen;
shGen2.X_axis_ID = maxisid;
shGen2.Y_axis_ID = zaxisid;
shGen2.X_axis_address = maxisaddress;
shGen2.Y_axis_address = zaxisaddress;
shGen2.Length = mapsize * 2;
//shGen2.Category = "Generated";
long address = lastFlashAddress;
shGen2.Flash_start_address = address;
//shGen2.Varname = "Generated** " + shGen2.Flash_start_address.ToString("X8");
shGen2.Varname = "3D " + shGen2.Flash_start_address.ToString("X8") + " " + shGen2.X_axis_ID.ToString("X4") + " " + shGen2.Y_axis_ID.ToString("X4");
retval = AddToSymbolCollection(newSymbols, shGen2, newCodeBlocks);
if (retval)
{
mapFound = true;
len2Skip += (xaxislen * 2) + (yaxislen * 2) + shGen2.Length;
t += (xaxislen * 2) + (yaxislen * 2) + shGen2.Length;
}
lastFlashAddress = address + mapsize * 2;
//Console.WriteLine("Set last address 2 to " + lastFlashAddress.ToString("X8"));
}
lastFlashAddress += ms.NumRepeats * 4 + 4;
}
}
//if(ms.NumRepeats
}
}
}
}
newSymbol.Varname = "3D " + newSymbol.Flash_start_address.ToString("X8") + " " + xaxisid.ToString("X4") + " " + yaxisid.ToString("X4");
retval = AddToSymbolCollection(newSymbols, newSymbol, newCodeBlocks);
if (retval)
{
mapFound = true;
len2Skip += (xaxislen * 2) + (yaxislen * 2) + newSymbol.Length;
t += (xaxislen * 2) + (yaxislen * 2) + newSymbol.Length;
}
}
else
{
if (yaxisid > 0xC000 && yaxisid < 0xF000 && yaxislen <= 32) Console.WriteLine("Unknown map id: " + yaxisid.ToString("X4") + " len " + yaxislen.ToString("X4") + " at address " + t.ToString("X8"));
SymbolHelper newSymbol = new SymbolHelper();
newSymbol.X_axis_length = xaxislen;
newSymbol.X_axis_ID = xaxisid;
newSymbol.X_axis_address = t + 4;
newSymbol.Length = xaxislen * 2;
newSymbol.Flash_start_address = t + 4 + (xaxislen * 2);
newSymbol.Varname = "2D " + newSymbol.Flash_start_address.ToString("X8") + " " + xaxisid.ToString("X4");
//newSymbols.Add(newSymbol);
newSymbol.CodeBlock = DetermineCodeBlockByByAddress(newSymbol.Flash_start_address, newCodeBlocks);
retval = AddToSymbolCollection(newSymbols, newSymbol, newCodeBlocks);
if (retval)
{
mapFound = true;
len2Skip += (xaxislen * 2);
t += (xaxislen * 2);
}
// 2d map
}
}
}
}
return retval;
}
private int CheckAxisCount(int offset, byte[] allBytes, out List<MapSelector> mapSelectors)
{
int axisCount = 0;
mapSelectors = new List<MapSelector>();
bool axisFound = true;
int t = offset;
while (axisFound)
{
axisFound = false;
int axisid = (Convert.ToInt32(allBytes[t + 1]) * 256) + Convert.ToInt32(allBytes[t]);
if (isAxisID(axisid))
{
int axislen = (Convert.ToInt32(allBytes[t + 3]) * 256) + Convert.ToInt32(allBytes[t + 2]);
if (axislen > 0 && axislen < 32)
{
axisCount++;
axisFound = true;
t += 4 + (axislen * 2);
}
}
}
// search from offset 't' for selectors
// maximum searchrange = 0x1000
int BytesToSearch = 5120 + 16;
if (axisCount > 3)
{
while (BytesToSearch > 0)
{
int axisid = (Convert.ToInt32(allBytes[t + 1]) * 256) + Convert.ToInt32(allBytes[t]);
if (isAxisID(axisid))
{
//Console.WriteLine("Checking address: " + t.ToString("X8"));
int axislen = (Convert.ToInt32(allBytes[t + 3]) * 256) + Convert.ToInt32(allBytes[t + 2]);
if (axislen <= 10) // more is not valid for selectors
{
// read & verify data (00 00 00 01 00 02 00 03 etc)
bool selectorValid = true;
int num = 0;
uint prevSelector = 0;
for (int i = 0; i < (axislen * 2); i += 2)
{
uint selValue = Convert.ToUInt32(allBytes[t + 4 + (axislen * 2) + i]) + Convert.ToUInt32(allBytes[t + 4 + (axislen * 2) + 1 + i]);
//Console.WriteLine("Selval: " + selValue.ToString() + " num: " + num.ToString());
if (allBytes[t + 4 + (axislen * 2) + i] != 0)
{
selectorValid = false;
break;
}
if (allBytes[t + 4 + (axislen * 2) + 1 + i] > 9)
{
selectorValid = false;
break;
}
if (prevSelector > selValue)
{
selectorValid = false;
break;
}
prevSelector = selValue;
/*if (num != selValue)
{
// not a valid selector
selectorValid = false;
break;
}*/
num++;
}
if (selectorValid)
{
// create a new selector
//Console.WriteLine("Selector valid " + t.ToString("X8"));
MapSelector newSel = new MapSelector();
newSel.NumRepeats = axislen;
newSel.StartAddress = t;
mapSelectors.Add(newSel);
BytesToSearch = 5120 + 16;
}
}
}
t += 2;
BytesToSearch -= 2;
}
}
return axisCount;
}
private void VerifyCodeBlocks(byte[] allBytes, SymbolCollection newSymbols, List<CodeBlock> newCodeBlocks)
{
//000001=automatic,000002=manual,000003=4wd ????
Tools.Instance.m_codeBlock5ID = 0;
Tools.Instance.m_codeBlock6ID = 0;
Tools.Instance.m_codeBlock7ID = 0;
bool found = true;
int offset = 0;
int defaultCodeBlockLength = 0x10000;
int currentCodeBlockLength = 0;
int prevCodeBlockStart = 0;
while (found)
{
int CodeBlockAddress = Tools.Instance.findSequence(allBytes, offset, new byte[11] { 0xC1, 0x02, 0x00, 0x68, 0x00, 0x25, 0x03, 0x00, 0x00, 0x10, 0x27 }, new byte[11] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });
if (CodeBlockAddress > 0)
{
CodeBlock newcodeblock = new CodeBlock();
newcodeblock.StartAddress = CodeBlockAddress - 1;
if (prevCodeBlockStart == 0) prevCodeBlockStart = newcodeblock.StartAddress;
else if (currentCodeBlockLength == 0)
{
currentCodeBlockLength = newcodeblock.StartAddress - prevCodeBlockStart;
if (currentCodeBlockLength > 0x10000) currentCodeBlockLength = 0x10000;
}
// find the next occurence of the checksum
newCodeBlocks.Add(newcodeblock);
offset = CodeBlockAddress + 1;
}
else found = false;
}
foreach (CodeBlock cb in newCodeBlocks)
{
if (currentCodeBlockLength != 0) cb.EndAddress = cb.StartAddress + currentCodeBlockLength - 1;
else cb.EndAddress = cb.StartAddress + defaultCodeBlockLength - 1;
}
foreach (CodeBlock cb in newCodeBlocks)
{
int autoSequenceIndex = Tools.Instance.findSequence(allBytes, cb.StartAddress, new byte[7] { 0x45, 0x44, 0x43, 0x20, 0x20, 0x41, 0x47 }, new byte[7] { 1, 1, 1, 1, 1, 1, 1 });
int manualSequenceIndex = Tools.Instance.findSequence(allBytes, cb.StartAddress, new byte[7] { 0x45, 0x44, 0x43, 0x20, 0x20, 0x53, 0x47 }, new byte[7] { 1, 1, 1, 1, 1, 1, 1 });
if (autoSequenceIndex < cb.EndAddress && autoSequenceIndex >= cb.StartAddress) cb.BlockGearboxType = GearboxType.Automatic;
if (manualSequenceIndex < cb.EndAddress && manualSequenceIndex >= cb.StartAddress) cb.BlockGearboxType = GearboxType.Manual;
}
if (Tools.Instance.m_currentfilelength >= 0x80000)
{
Tools.Instance.m_codeBlock5ID = CheckCodeBlock(0x50000, allBytes, newSymbols, newCodeBlocks); //manual specific
Tools.Instance.m_codeBlock6ID = CheckCodeBlock(0x60000, allBytes, newSymbols, newCodeBlocks); //automatic specific
Tools.Instance.m_codeBlock7ID = CheckCodeBlock(0x70000, allBytes, newSymbols, newCodeBlocks); //quattro specific
}
}
}
}