-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeneratepascalunit.pas
More file actions
executable file
·1584 lines (1411 loc) · 48.4 KB
/
generatepascalunit.pas
File metadata and controls
executable file
·1584 lines (1411 loc) · 48.4 KB
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit generatepascalunit;
interface
uses
parsingATDF, classes;
procedure generateUnitFromATDFInfo(var device: TDevice; var SL: TStrings);
procedure generateUnitXFromATDFInfo(var device: TDevice; var SL: TStrings);
implementation
uses
sysutils, math, types;
const
// 's' clashes with attiny40 PSR
setPrefix = 's';
// 'm' clashes at90pwm316 MUBBR
constMaskPrefix = 'c';
type
// avr8 style declarations are per register
TReg = record
aname,
caption: string;
address,
size,
mask: integer;
bitFields: TBitFields; // add bit fields in sorted order
end;
TSortedRegs = array of TReg;
// avr8X style declarations are per register group (record)
TRegGroup = record
aname,
atype: string;
offset: integer;
end;
var
IDList: TStringList; // list used to check uniqueness of identifier names
// Check if ID is unique, append _ to make unique
// then add to IDList
function AddUniqueID(s: string): string;
var
i: integer;
begin
if s <> '' then
begin
i := IDList.IndexOf(s);
while not (i = -1) do
begin
s := s + '_';
i := IDList.IndexOf(s);
end;
IDList.Add(s);
end;
Result := s;
end;
function findPeriphModule(constref device: TDevice; nameInModule, nameInRegGroup: string): PPeriphRegGroup;
var
i, j, k: integer;
begin
Result := nil;
i := 0;
while (i < length(device.PeriphModules)) and (device.PeriphModules[i].aname <> nameInModule) do
inc(i);
if i = length(device.PeriphModules) then exit;
j := 0;
while (j < length(device.PeriphModules[i].periphInstances)) and (device.PeriphModules[i].periphInstances[j].aname <> nameInRegGroup) do
inc(j);
if j = length(device.PeriphModules[i].periphInstances) then exit;
k := 0;
while (k < length(device.PeriphModules[i].periphInstances[j].RegGroup)) and
(device.PeriphModules[i].periphInstances[j].RegGroup[k].nameInModule <> nameInRegGroup) do
inc(k);
if k = length(device.PeriphModules[i].periphInstances[j].RegGroup) then exit;
Result := @(device.PeriphModules[i].periphInstances[j].RegGroup[k]);
end;
procedure insertBitfield(constref source: TBitField; var dest: TBitFields);
var
i, j, len: integer;
begin
len := Length(dest)+1;
SetLength(dest, len);
// Locate spot after next smallest entry
i := 0;
while (i < len-1) and (source.mask >= dest[i].mask) do
inc(i);
// Potentially just after previous duplicate
if (i > 0) and (source.aname = dest[i-1].aname) then
begin
SetLength(dest, len-1); // remove added field
exit;
end;
j := len-1;
while (j > i) do
begin
dest[j] := dest[j-1];
dec(j);
end;
// now i should point to new available slot
dest[i] := source;
end;
function sortedRegisters(constref device: TDevice): TSortedRegs;
var
i, j, k, l, reg_offset, len: integer;
reg: ^TReg;
prg: PPeriphRegGroup;
portChar: char;
bitFieldList: TStringList;
s: string;
begin
bitFieldList := TStringList.Create;
i := 0;
while (i < length(device.AddressSpaces)) and (device.AddressSpaces[i].aname <> 'data') do
inc(i);
j := 0;
while (j < Length(device.AddressSpaces[i].memorySegments)) and
(device.AddressSpaces[i].memorySegments[j].type_ <> 'io') do
inc(j);
SetLength(Result, device.AddressSpaces[i].memorySegments[j].start + device.AddressSpaces[i].memorySegments[j].size);
for i := 0 to High(device.Modules) do
begin
for j := 0 to High(device.Modules[i].registerGroups) do
begin
prg := findPeriphModule(device,
device.Modules[i].aname,
device.Modules[i].registerGroups[j].aname);
// ATtiny80 has TC3 under modules but not peripherals...
if prg = nil then
continue;
if prg^.addressSpace = 'data' then
for k := 0 to High(device.Modules[i].registerGroups[j].registers) do
begin
reg := @device.Modules[i].registerGroups[j].registers[k];
reg_offset := device.Modules[i].registerGroups[j].registers[k].offset;
Result[reg_offset].address := reg_offset + prg^.offset; // TODO: What about module offset?
Result[reg_offset].aname := reg^.aname;
Result[reg_offset].caption := reg^.caption;
Result[reg_offset].size := reg^.size;
Result[reg_offset].mask := reg^.mask;
if length(device.Modules[i].registerGroups[j].registers[k].bitFields) > 0 then
begin
for l := 0 to length(device.Modules[i].registerGroups[j].registers[k].bitFields)-1 do
begin
s := device.Modules[i].registerGroups[j].registers[k].bitFields[l].aname;
if bitFieldList.IndexOf(s) = -1 then // hack to prevent duplicate identifier names from at90usb646
begin
bitFieldList.Add(s);
insertBitfield(device.Modules[i].registerGroups[j].registers[k].bitFields[l], Result[reg_offset].bitFields);
end;
end;
end
else if pos('PORT', reg^.aname) = 1 then // check for PORTA, PORTD etc.
begin
len := Length(Result[reg_offset].aname);
portChar := Result[reg_offset].aname[len];
for l := 0 to 7 do
if ((1 shl l) and device.Modules[i].registerGroups[j].registers[k].mask) > 0 then
begin
len := length(Result[reg_offset].bitFields);
SetLength(Result[reg_offset].bitFields, len+1);
Result[reg_offset].bitFields[len].aname := 'P' + portChar + IntToStr(l);
Result[reg_offset].bitFields[len].mask := (1 shl l);
end;
end;
end;
end;
end;
FreeAndNil(bitFieldList);
end;
procedure processBitFieldOldStyle(constref bitField: TBitField; constref List: TStrings; const indentSpaces: byte; const maskOnly: boolean = false);
var
i, bitsInMask: integer;
idx, padding, s1: string;
begin
idx := '';
SetLength(padding, indentSpaces);
FillChar(padding[1], indentSpaces, ' ');
// check if single bit value
if bitField.mask in [1, 2, 4, 8, 16, 32, 64, 128] then
begin
s1 := format('%s = $%.2x;',
[bitField.aname + idx, round(log2(bitField.mask))]);
List.Add(padding + s1);
end
else // expand mask into several bit definitions
begin
bitsInMask := -1; // identify lowest bit label
for i := 0 to 7 do
if ((1 shl i) and bitField.mask) > 0 then
begin
inc(bitsInMask); // zero based
if bitField.caption = '' then
List.Add(format(padding + '%s = $%.2x;',
[bitField.aname + IntToStr(bitsInMask + bitField.lsb) + idx, i]))
else
List.Add(format(padding + '%s = $%.2x; // %s',
[bitField.aname + IntToStr(bitsInMask + bitField.lsb) + idx, i, bitField.caption]));
end;
end;
end;
procedure processBitField(constref bitField: TBitField; constref List: TStrings; const indentSpaces: byte; const maskOnly: boolean = false);
var
i, bitsInMask: integer;
idx, bmp, padding, s1, s2: string;
begin
idx := 'bp';
bmp := 'bm';
SetLength(padding, indentSpaces);
FillChar(padding[1], indentSpaces, ' ');
// check if single bit value
if bitField.mask in [1, 2, 4, 8, 16, 32, 64, 128] then
begin
if not maskOnly then
begin
s1 := format('%s = $%.2x;',
[bitField.aname + idx, round(log2(bitField.mask))]);
end;
if bitField.caption = '' then
s2 := format('%s = $%.2x;',
[bitField.aname + bmp, bitField.mask])
else
s2 := format('%s = $%.2x; // %s',
[bitField.aname + bmp, bitField.mask, bitField.caption]);
if maskOnly then
List.Add(padding + s2)
else
List.Add(padding + s1 + ' ' + s2);
end
else if not maskOnly then// expand mask into several bit definitions
begin
bitsInMask := -1; // identify lowest bit label
for i := 0 to 7 do
if ((1 shl i) and bitField.mask) > 0 then
begin
inc(bitsInMask); // zero based
if bitField.caption = '' then
List.Add(format(padding + '%s = $%.2x;',
[bitField.aname + IntToStr(bitsInMask + bitField.lsb) + idx, i]))
else
List.Add(format(padding + '%s = $%.2x; // %s',
[bitField.aname + IntToStr(bitsInMask + bitField.lsb) + idx, i, bitField.caption]));
end;
end;
end;
function indexOfBitFieldInValuesGroup(const bitFieldName: string; constref valuegroups: TValueGroups): integer;
var
i: integer;
begin
Result := -1;
i := 0;
if bitFieldName <> '' then
begin
while (i < length(valuegroups)) and ((CompareText(bitFieldName, valuegroups[i].aname) <> 0)) do
inc(i);
if i < length(valuegroups) then
Result := i;
end;
end;
procedure processValueGroup(constref ValueGroup: TValueGroup; const bitFieldOffset: integer;
constref List: TStrings; prefix: string = '');
var
i: integer;
s: string;
begin
// Compose value name as [bitfield name]_[value name]
if prefix = '' then
begin
i := pos('_', ValueGroup.aname);
s := copy(ValueGroup.aname, i+1, 255) + '_'
end
else
s := prefix;
for i := 0 to high(ValueGroup.values) do
begin
// Ensure const name doesn't start with a numerical character
if (s = '') and (valueGroup.values[i].aname[1] in ['0'..'9']) then
s := '_';
List.Add(format(' %s = $%.2x;',
[s + valueGroup.values[i].aname, valueGroup.values[i].value shl bitFieldOffset]));
end;
end;
procedure processBitFieldAsMask(constref bitField: TBitField; constref valuegroups: TValueGroups;
constref List: TStrings; const indentSpaces: byte; const maskOnly: boolean = false;
isAtxmega: boolean = false);
var
i, bitsInMask, mask, lsb: integer;
idx, bmp, padding, s1, s2: string;
begin
// GCC convention
idx := 'bp';
bmp := 'bm';
SetLength(padding, indentSpaces);
FillChar(padding[1], indentSpaces, ' ');
// List of values are supplied
if bitField.values <> '' then
begin
i := indexOfBitFieldInValuesGroup(bitField.values, valuegroups);
if i > -1 then
begin
// Find LSB position of value mask
lsb := 0;
mask := bitField.mask;
while (mask > 0) and ((mask and 1) = 0) do
begin
inc(lsb);
mask := mask shr 1;
end;
// mask is now the largest possible valid value
// Check last value in value-group against mask
if valuegroups[i].values[high(valuegroups[i].values)].value > mask then
List.Add('// **** Inconsistency between mask and values for ' +bitField.aname);
s1 := Copy(bitField.values, pos('_', bitField.values)+1, 255);
if isAtxmega and (bitField.aname <> s1) then
begin
// valuegroups can be shared between bitfields, so use
// bitfield.name in place of values for unique name
List.Add(padding + '// ' + bitField.aname);
List.Add(padding + bitField.aname+'mask = $' + IntToHex(bitField.mask, 2) + ';');
processValueGroup(valuegroups[i], lsb, List, bitField.aname);
end
else
begin
List.Add(padding + '// ' + bitField.values); // perhaps strip module prefix?
// Add generic bit mask for whole value group
// Some registers have multiple modes - this seems to be encoded in the values-group name
// as MODULE_MODE_BITFIELDNAME
// Strip MODULE_ then MODE_BITFIELDNAME ensures a unique name
// Also work for standard case which is labeled MODULE_BITFIELDNAME
List.Add(padding + s1+'mask = $' + IntToHex(bitField.mask, 2) + ';');
processValueGroup(valuegroups[i], lsb, List);
end;
end
else
List.Add('Unexpectedly no matching value-group for "' + bitField.values + '"');
end
else if bitfield.mask < 255 then // expand mask into individual bit definitions, but only for less than 8 bits
begin
// Single bits may share a name with a register - not compatible with Pascal
// Need to add something to ensure uniqueness
List.Add(padding + '// ' + bitField.caption);
// check if single bit value
if bitField.mask in [1, 2, 4, 8, 16, 32, 64, 128] then
begin
s1 := format('%s = $%.2x;',
[bitField.aname + bmp, bitField.mask]);
if not maskOnly then
begin
s2 := format('%s = $%.2x;',
[bitField.aname + idx, round(log2(bitField.mask))]);
end;
if maskOnly then
List.Add(padding + s1)
else
List.Add(padding + s1 + ' ' + s2);
end
else
begin
bitsInMask := -1; // identify lowest bit label
for i := 0 to 7 do
if ((1 shl i) and bitField.mask) > 0 then
begin
inc(bitsInMask); // zero based
s1 := format('%s = $%.2x;',
[bitField.aname + IntToStr(bitsInMask + bitField.lsb) + bmp, 1 shl i]);
if not maskOnly then
begin
s2 := format(padding + '%s = $%.2x;',
[bitField.aname + IntToStr(bitsInMask + bitField.lsb) + idx, i]);
List.Add(padding + s1 + ' ' + s2);
end
else
List.Add(padding + s1);
end;
end;
end;
end;
// Plain style var declaration per register
// var
// PORTB: byte absolute $25; // Port B Data Register
// PORTC: byte absolute $..; // Port B Data Register
// followed by bit field declarations
// const
// PB0 = 0;
procedure generateDeclarationsOpt0(constref device: TDevice; var List: TStrings);
var
i, j: integer;
sortedRegs: TSortedRegs;
r: TReg;
comment: string;
bitDefs: TStringList;
begin
sortedRegs := sortedRegisters(device);
bitDefs := TStringList.Create;
bitDefs.Add('const');
List.Add('var');
for i := 0 to length(sortedRegs)-1 do
begin
r := sortedRegs[i];
if r.aname <> '' then
begin
if r.caption <> '' then
comment := ' // ' + r.caption
else
comment := '';
if r.size = 1 then
List.Add(format(' %s: byte absolute $%.2x;%s', [r.aname, r.address, comment]))
else if r.size = 2 then
begin
List.Add(format(' %s: word absolute $%.2x;%s', [r.aname, r.address, comment]));
List.Add(format(' %sL: byte absolute $%.2x;%s', [r.aname, r.address, comment]));
List.Add(format(' %sH: byte absolute $%.2x;%s;', [r.aname, r.address+1, comment]));
end
else if r.size = 4 then
begin
List.Add(format(' %s: dword absolute $%.2x;%s', [r.aname, r.address, comment]));
List.Add(format(' %sL: word absolute $%.2x;', [r.aname, r.address]));
List.Add(format(' %sH: word absolute $%.2x;', [r.aname, r.address+2]));
end
else
raise Exception.Create('Unexpected register size:: $' + IntToHex(r.size, 2));
// Bit fields
if length(r.bitFields) > 0 then
begin
if r.caption <> '' then
bitDefs.Add(' // '+r.caption);
for j := 0 to high(r.bitFields) do
processBitFieldOldStyle(r.bitFields[j], bitDefs, 2);
end;
end;
end;
if bitDefs.Count > 1 then
begin
List.Add('');
List.Append(bitDefs.Text);
end;
bitDefs.Free;
end;
// Plain style var declaration per register
// var
// PORTB: byte absolute $25; // Port B Data Register
// with bit field declarations
// const
// PB0 = 0;
procedure generateDeclarationsOpt1(constref device: TDevice; var List: TStrings);
var
i, j: integer;
sortedRegs: TSortedRegs;
r: TReg;
previousVar: boolean;
comment: string;
begin
sortedRegs := sortedRegisters(device);
previousVar := false;
for i := 0 to length(sortedRegs)-1 do
begin
r := sortedRegs[i];
if r.aname <> '' then
begin
if not previousVar then
begin
List.Add('var');
previousVar := true;
end;
if r.caption <> '' then
comment := ' // ' + r.caption
else
comment := '';
if r.size = 1 then
List.Add(format(' %s: byte absolute $%.2x;%s', [r.aname, r.address, comment]))
else if r.size = 2 then
begin
List.Add(format(' %s: word absolute $%.2x;%s', [r.aname, r.address, comment]));
List.Add(format(' %sL: byte absolute $%.2x;', [r.aname, r.address]));
List.Add(format(' %sH: byte absolute $%.2x;', [r.aname, r.address+1]));
end
else if r.size = 4 then
begin
List.Add(format(' %s: dword absolute $%.2x;%s', [r.aname, r.address, comment]));
List.Add(format(' %sL: word absolute $%.2x;', [r.aname, r.address]));
List.Add(format(' %sH: word absolute $%.2x;', [r.aname, r.address+2]));
end
else
raise Exception.Create('Unexpected register size:: $' + IntToHex(r.size, 2));
// Bit fields
if length(r.bitFields) > 0 then
begin
List.Add('const');
previousVar := false;
for j := 0 to high(r.bitFields) do
processBitField(r.bitFields[j], List, 2);
end;
end;
end;
end;
// Conventional RTL style declarations
// var
// PORTA: byte absolute $22; // Port A Data Register
procedure generateStandardDeclarationFromRegisterInfo(constref reg: TReg; var typeStr, varStr, constStr: string);
var
j, k, bitsInMask, bitmask, bitnumber: integer;
b: TBitField;
comment, s: string;
typeList, varList, constList: TStringList;
begin
typeList := TStringList.Create;
varList := TStringList.Create;
constList := TStringList.Create;
if reg.aname <> '' then
begin
if reg.caption <> '' then
comment := ' // ' + reg.caption
else
comment := '';
if reg.size = 1 then
varList.Add(format(' %s: byte absolute $%.2x;%s', [reg.aname, reg.address, comment]))
else if reg.size = 2 then
begin
varList.Add(format(' %s: word absolute $%.2x;%s', [reg.aname, reg.address, comment]));
varList.Add(format(' %sL: byte absolute $%.2x;', [reg.aname, reg.address]));
varList.Add(format(' %sH: byte absolute $%.2x;', [reg.aname, reg.address+1]));
end
else if reg.size = 4 then
begin
varList.Add(format(' %s: dword absolute $%.2x;%s', [reg.aname, reg.address, comment]));
varList.Add(format(' %sL: word absolute $%.2x;', [reg.aname, reg.address]));
varList.Add(format(' %sH: word absolute $%.2x;', [reg.aname, reg.address+2]));
end
else
raise Exception.Create('Unexpected register size:: $' + IntToHex(reg.size, 2));
// Bit fields
bitmask := 0;
if length(reg.bitFields) > 0 then
begin
for j := 0 to length(reg.bitFields)-1 do
begin
b := reg.bitFields[j];
if b.caption = '' then
comment := ''
else
comment := ' // ' + b.caption;
// if values has been assigned then there is a corresponding value-group to expand
if reg.bitFields[j].values = '' then
begin
if (bitmask and reg.bitFields[j].mask) = 0 then
begin
// const name and mask name handled together
s := AddUniqueID(b.aname);
constList.Add(format(' %s = %d; ' + constMaskPrefix + '%s = %d;%s',
[s, round(log2(b.mask)), s, b.mask, comment]));
bitmask := bitmask or reg.bitFields[j].mask;
end;
end
else // expand mask into several bit definitions
begin
bitsInMask := -1; // identify lowest bit label
for k := 0 to 7 do
if ((1 shl k) and reg.bitFields[j].mask) > 0 then
begin
inc(bitsInMask); // zero based
bitnumber := bitsInMask + reg.bitFields[j].lsb;
if (bitmask and (1 shl k)) = 0 then
begin
// const name and mask name handled together
s := reg.bitFields[j].aname + IntToStr(bitnumber);
s := AddUniqueID(s);
constList.Add(format(' %s = %d; ' + constMaskPrefix + '%s = %d;%s',
[s, k, s, 1 shl k, comment]));
bitmask := bitmask or (1 shl k);
end;
end;
end;
end;
end;
end;
if typeList.Count > 0 then
if typeStr = '' then
typeStr := TrimRight(typeList.Text)
else
typeStr := typeStr + typeList.LineBreak + TrimRight(typeList.Text);
if varList.Count > 0 then
if varStr = '' then
varStr := TrimRight(varList.Text)
else
varStr := varStr + varList.LineBreak + TrimRight(varList.Text);
if constList.Count > 0 then
if constStr = '' then
constStr := TrimRight(constList.Text)
else
constStr := constStr + constList.LineBreak + TrimRight(constList.Text);
FreeAndNil(constList);
FreeAndNil(varList);
FreeAndNil(typeList);
end;
// Record type declaration of bit fields for ports
// type
// TBitField = 0..1;
// TPORTArec = bitpacked record
// PA0, PA1, PA2, PA3,
// PA4, PA5, PA6, PA7: TBitField;
// end;
//var
// PORTArec: TPORTArec absolute $22;
procedure generateRecordDeclarationFromRegisterInfo(constref reg: TReg; var typeStr, varStr, constStr: string);
var
j, k, bitsInMask, dummycount: integer;
s, recdef: string;
typeList, varList, constList, bitList: TStringList;
b: TBitField;
begin
typeList := TStringList.Create;
varList := TStringList.Create;
constList := TStringList.Create;
bitList := TStringList.Create;
if (reg.aname <> '') and (Length(reg.bitFields) > 0) and (reg.size = 1) then // no bitfields, use generic definitions
begin
// Sort Bits from 0 to 7
bitlist.Clear;
for j := 0 to length(reg.bitFields)-1 do
begin
b := reg.bitFields[j];
if b.mask in [1, 2, 4, 8, 16, 32, 64, 128] then
begin
s := IntToStr(round(log2(b.mask))) + '=' + b.aname;
bitlist.Add(s);
end
else // expand mask into several bit definitions
begin
bitsInMask := -1; // identify lowest bit label
for k := 0 to 7 do
if ((1 shl k) and b.mask) > 0 then
begin
inc(bitsInMask); // zero based
s := IntToStr(k) + '=' + b.aname + IntToStr(bitsInMask + b.lsb);
bitlist.Add(s);
end;
end;
end;
recdef := format(' T%srec = bitpacked record', [reg.aname]);
dummycount := -1;
for j := 0 to 7 do
begin
k := bitlist.IndexOfName(IntToStr(j));
if k > -1 then
recdef := recdef + bitList.LineBreak + ' ' + bitlist.ValueFromIndex[k] + ','
else // skip missing bit,
begin
inc(dummycount);
recdef := recdef + bitList.LineBreak + ' ReservedBit' + IntToStr(j) + ',';
end;
end;
if recdef <> '' then // remove last ,
begin
delete(recdef, length(recdef), 1);
recdef := recdef + ': TBitField;' + bitList.LineBreak + ' end;';
typeList.Add(recdef);
end;
varList.Add(format(' %srec: T%srec absolute $%.2x;', [reg.aname, reg.aname, reg.address]));
end
else if (reg.aname <> '') then
begin
// generic record declaration?
end;
if typeList.Count > 0 then
if typeStr = '' then
typeStr := TrimRight(typeList.Text)
else
typeStr := typeStr + typeList.LineBreak + TrimRight(typeList.Text);
if varList.Count > 0 then
if varStr = '' then
varStr := TrimRight(varList.Text)
else
varStr := varStr + varList.LineBreak + TrimRight(varList.Text);
if constList.Count > 0 then
if constStr = '' then
constStr := TrimRight(constList.Text)
else
constStr := constStr + constList.LineBreak + TrimRight(constList.Text);
FreeAndNil(bitList);
FreeAndNil(constList);
FreeAndNil(varList);
FreeAndNil(typeList);
end;
// Set style declaration of ports
// type
// TPORTAset = bitpacked set of (sPA0, sPA1, sPA2, sPA3, sPA4, sPA5, sPA6, sPA7);
//var
// PORTAset: TPORTAset absolute $22;
procedure generateSetDeclarationFromRegisterInfo(constref reg: TReg; var typeStr, varStr, constStr: string);
var
j, k, bitsInMask: integer;
s, sID, setdef: string;
skippedbit: boolean;
typeList, varList, constList, bitList: TStringList;
b: TBitField;
begin
typeList := TStringList.Create;
varList := TStringList.Create;
constList := TStringList.Create;
bitList := TStringList.Create;
if (reg.aname <> '') and (Length(reg.bitFields) > 0) and (reg.size = 1) then // no bitfields, use generic definitions
begin
// Sort Bits from 0 to 7
bitlist.Clear;
for j := 0 to length(reg.bitFields)-1 do
begin
b := reg.bitFields[j];
if b.mask in [1, 2, 4, 8, 16, 32, 64, 128] then
begin
s := IntToStr(round(log2(b.mask))) + '=' + b.aname;
bitlist.Add(s);
end
else // expand mask into several bit definitions
begin
bitsInMask := -1; // identify lowest bit label
for k := 0 to 7 do
if ((1 shl k) and b.mask) > 0 then
begin
inc(bitsInMask); // zero based
s := IntToStr(k) + '=' + b.aname + IntToStr(bitsInMask + b.lsb);
bitlist.Add(s);
end;
end;
end;
skippedbit := false;
setdef := format(' T%sset = bitpacked set of (', [reg.aname]);
for j := 0 to 7 do
begin
s := '';
sID := '';
k := bitlist.IndexOfName(IntToStr(j));
if k > -1 then
begin
if skippedbit then
begin
skippedbit := false;
s := setPrefix + bitlist.ValueFromIndex[k];
sID := '=' + IntToStr(j);
end
else
s := setPrefix + bitlist.ValueFromIndex[k];
s := AddUniqueID(s);
setdef := setdef + s + sID + ', ';
end
else // skip missing bit,
begin
skippedbit := true;
end;
end;
if setdef <> '' then
begin
delete(setdef, length(setdef)-1, 2);
setdef := setdef + ');';
typeList.Add(setdef);
end;
varList.Add(format(' %sset: T%sset absolute $%.2x;', [reg.aname, reg.aname, reg.address]));
end;
if typeList.Count > 0 then
if typeStr = '' then
typeStr := TrimRight(typeList.Text)
else
typeStr := typeStr + typeList.LineBreak + TrimRight(typeList.Text);
if varList.Count > 0 then
if varStr = '' then
varStr := TrimRight(varList.Text)
else
varStr := varStr + varList.LineBreak + TrimRight(varList.Text);
if constList.Count > 0 then
if constStr = '' then
constStr := TrimRight(constList.Text)
else
constStr := constStr + constList.LineBreak + TrimRight(constList.Text);
FreeAndNil(bitList);
FreeAndNil(constList);
FreeAndNil(varList);
FreeAndNil(typeList);
end;
// Combine standard, set and record definitions for each register.
procedure generateDeclarationsOpt2(constref device: TDevice; var List: TStrings);
var
i, typedefcount: integer;
sortedRegs: TSortedRegs;
r: TReg;
previousVar, previousType: boolean;
s, sType, sVar, sConst: string;
begin
IDList := TStringList.Create;
IDList.CaseSensitive := false;
typedefcount := 0;
List.Add('{$bitpacking on}{$packset 1}{$packenum 1}');
List.Add('type');
List.Add(' TBitField = 0..1;');
sortedRegs := sortedRegisters(device);
previousVar := false;
previousType := false;
for i := 0 to length(sortedRegs)-1 do
begin
r := sortedRegs[i];
if r.aname <> '' then
begin
sType := '';
sVar := '';
sConst := '';
// First standard declarations so that conventional identifiers are used afap for the plain const names
generateStandardDeclarationFromRegisterInfo(r, sType, sVar, sConst);
generateSetDeclarationFromRegisterInfo(r, sType, sVar, sConst);
generateRecordDeclarationFromRegisterInfo(r, sType, sVar, sConst);
if (sType <> '') then
begin
if not previousType then
begin
List.Add('');
List.Add('type');
previousVar := false;
previousType := true;
Inc(typedefcount);
end;
List.Add(sType);
end;
if (sVar <> '') then
begin
if not previousVar then
begin
if not previousType then
List.Add('');
List.Add('var');
previousVar := true;
previousType := false;
end;
List.Add(sVar);
end;
if (sConst <> '') then
begin
List.Add('const'); // always add const identifier
previousVar := false;
previousType := false;
List.Add(sConst);
end;
end
else
s := '';
if s <> '' then
List.Add(s);
end;
List.Add(' // typedefs = ' + IntToStr(typedefcount));
FreeAndNil(IDList);
end;
// Combine register declarations with rest of unit information such as ISR declarations and including startup code
procedure generateUnitFromATDFInfo(var device: TDevice; var SL: TStrings);
var
jmpInstr: string; // jmp/rjmp depending on target capability
i: integer;
pgmsize, sramsize: integer;
cl, vl: TStringList;
begin
cl := TStringList.Create;
vl := TStringList.Create;
try
// device capability checks required later on
pgmsize := 0;
sramsize := 0;
i := 0;
while i < length(device.AddressSpaces) do
begin
if device.AddressSpaces[i].aname = 'prog' then
pgmsize := device.AddressSpaces[i].size;
if device.AddressSpaces[i].aname = 'data' then
sramsize := device.AddressSpaces[i].size;
inc(i);
end;
if pgmsize > 8192 then jmpInstr := 'jmp' else jmpInstr := 'rjmp';
SL.Clear;
SL.Add('unit ' + device.deviceName + ';');
SL.Add(#13#10'interface'#13#10);
generateDeclarationsOpt0(device, SL);
if pgmsize > 8192 then
SL.Add(#13#10'implementation'#13#10#13#10'{$i avrcommon.inc}'#13#10)
else
SL.Add(#13#10'implementation'#13#10'{$define RELBRANCHES}'#13#10'{$i avrcommon.inc}'#13#10);
for i := 0 to High(device.Interrupts) do
begin
if device.Interrupts[i].index <> 0 then
SL.Add(format('procedure %s_ISR; external name ''%s_ISR''; // Interrupt %d %s',
[device.Interrupts[i].name, device.Interrupts[i].name,
device.Interrupts[i].index, device.Interrupts[i].caption]));
end;
SL.Add(#13#10'procedure _FPC_start; assembler; nostackframe; noreturn; public name '+
'''_START''; section ''.init'';'#13#10 +
'asm'#13#10' '+jmpInstr+' __dtors_end');
for i := 0 to High(device.Interrupts) do
if device.Interrupts[i].index <> 0 then
SL.Add(' '+jmpInstr+' ' + device.Interrupts[i].name + '_ISR');
SL.Add('');
for i := 0 to High(device.Interrupts) do
if device.Interrupts[i].index <> 0 then
SL.Add(' .weak ' + device.Interrupts[i].name + '_ISR');
SL.Add('');
for i := 0 to High(device.Interrupts) do
if device.Interrupts[i].index <> 0 then