File: fpdbgdisasavr.pp

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (1579 lines) | stat: -rw-r--r-- 58,010 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
{ $Id$ }
{
 ---------------------------------------------------------------------------
 fpdbgdisasavr.pp  -  Native Freepascal debugger - avr Disassembler
 ---------------------------------------------------------------------------

 This unit contains an avr disassembler for the Native Freepascal debugger

 ---------------------------------------------------------------------------

 @created(Mon Oct 18th 2019)
 @lastmod($Date$)
 @author()

 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
 *                                                                         *
 ***************************************************************************
}
unit FpDbgDisasAvr;
{$mode objfpc}{$H+}
{$IFDEF INLINE_OFF}{$INLINE OFF}{$ENDIF}
interface

uses
  SysUtils,
  FpDbgUtil, FpDbgInfo, DbgIntfBaseTypes, FpdMemoryTools, {$ifdef FORCE_LAZLOGGER_DUMMY} LazLoggerDummy {$else} LazLoggerBase {$endif},
  FpDbgClasses;

type
  //The function Disassemble decodes the instruction at the given address.
  //Unrecognized instructions are assumed to be data statements [dw XXXX]

  TAvrAsmDecoder = class;

  TAVROpCode = (
    A_ADC, A_ADD, A_ADIW, A_AND, A_ANDI, A_ASR, A_BLD, A_BR, A_BREAK, A_BST,
    A_CALL, A_CBI, A_CL, A_COM, A_CP, A_CPC, A_CPI, A_CPSE, A_DEC, A_DES,
    A_EICALL, A_EIJMP, A_ELPM, A_EOR, A_FMUL, A_FMULS, A_FMULSU, A_ICALL, A_IJMP, A_IN,
    A_INC, A_JMP, A_LAC, A_LAS, A_LAT, A_LD, A_LDD, A_LDI, A_LDS, A_LPM,
    A_LSL, A_LSR, A_MOV, A_MOVW, A_MUL, A_MULS, A_MULSU, A_NEG, A_NOP, A_OR,
    A_ORI, A_OUT, A_POP, A_PUSH, A_RCALL, A_RET, A_RETI, A_RJMP, A_ROL, A_ROR,
    A_SBC, A_SBCI, A_SBI, A_SBIC, A_SBIS, A_SBIW, A_SBR, A_SBRC, A_SBRS, A_SE,
    A_SER, A_SLEEP, A_SPM, A_ST, A_STD, A_STS, A_SUB, A_SUBI, A_SWAP, A_WDR,
    A_XCH, A_INVALID);

  TAVRInstruction = record
    OpCode: TAVROpCode;
    OpCodeMod: integer;  // Use as index to general opcode modifiers, typically in range 0..7
    Size: integer;
    Oper: array[1..2] of integer;
  end;

  TOperandFormat = (ofNone, ofDecimal, ofHex8, ofHex16, ofHex32, ofReg, ofIndex, ofIndexOffset, ofRelAddr);
  // Information mostly for formatting
  TAvrInstructionInfo = record
    OpName: string;
    OperCount: integer;
    OperandFormats: array[1..2] of TOperandFormat;
  end;

  { TAvrDisassembler }

    TAvrDisassembler = object
    private
      function InvalidOpCode(instr: word): TAVRInstruction;
      function SetInstructionInfo(AOpCode: TAVROpCode; AOpcodeMod: integer;
        ASize: integer; AOper: array of integer): TAVRInstruction;
    public
      procedure Disassemble(var AAddress: Pointer; out AnInstruction: TAVRInstruction);
    end;

  { TAvrAsmInstruction }

  TAvrAsmInstruction = class(TDbgAsmInstruction)
  private const
    INSTR_CODEBIN_LEN = 4;
  private
    FProcess: TDbgProcess;
    FAddress: TDBGPtr;
    FCodeBin: array[0..INSTR_CODEBIN_LEN-1] of byte;
    FAvrInstruction: TAVRInstruction;
    FFlags: set of (diCodeRead, diCodeReadError, diDisAss);
  protected
    procedure ReadCode; inline;
    procedure Disassemble; inline;
  public
    constructor Create(AProcess: TDbgProcess);
    procedure SetAddress(AnAddress: TDBGPtr);
    function IsCallInstruction: boolean; override;
    function IsReturnInstruction: boolean; override;
    function IsLeaveStackFrame: boolean; override;
    function InstructionLength: Integer; override;
  end;

{ TAvrAsmDecoder }

  TAvrAsmDecoder = class(TDbgAsmDecoder)
  private const
    MaxPrologueSize = 64;  // Bytes, so ~32 instructions
    MaxEpilogueSize = MaxPrologueSize; // Perhaps a bit smaller, since the locals/parameters do not have to be initialized
    MAX_CODEBIN_LEN = MaxPrologueSize; // About 32 instructions

    // Opcodes for prologue / epilogue parsing
    OpCodeNOP = $0000;
    OpCodeLoadSPL = $B7CD;       // in r28, 0x3d
    OpCodeLoadSPH = $B7DE;       // in r29, 0x3e
    OpCodeLoadSRegR0 = $B60F;    // in r0, 0x3f
    OpCodeLoadSRegR16 = $B70F;   // in r16, 0x3f, avrtiny subarch
    OpCodeCli = $94F8;           // cli
    OpCodeSetSPH = $BFDE;        // out 0x3e, r29
    OpCodeSetSPL = $BFCD;        // out 0x3d, r28
    OpCodeSetSregR0 = $BE0F;     // out 0x3f, r0
    OpCodeSetSregR16 = $BF0F;    // out 0x3f, r16
    OpCodeRet = $9508;           // ret
    OpCodeReti = $9518;          // reti
    // Zero register gets zeroed at start of interrupt
    OpCodeZeroR1 = $2411;        // eor r1, r1
    OpCodeZeroR16 = $2700;       // eor r16, r16
    OpCodePushMask = $920F;      // PUSH 1001 001d dddd 1111
    OpCodePopMask = $900F;       // POP  1001 000d dddd 1111
    // Frame pointer is r28:r29, so fix register index in opcode
    OpCodeAdjustFrameLMask = $50C0;   // subi r28, k,  0101 kkkk dddd kkkk, rd = 16 + d
    OpCodeAdjustFrameHMask = $40D0;   // sbci r28, k,  0100 kkkk dddd kkkk, rd = 16 + d
    // Not yet implemented in FPC, but more compact option to set frame pointer
    OpCodeAdjustFrameMask  = $9720;   // sbiw r28, k,  1001 0111 kkdd kkkk, rd = 24 + d*2
    OpCodeStoreOnStackMask = $8208;   // std Y+2, r24  10q0 qq1r rrrr 1qqq
  private
    FProcess: TDbgProcess;
    FLastErrWasMem: Boolean;
    FCodeBin: array[0..MAX_CODEBIN_LEN-1] of byte;
    FLastInstr: TAvrAsmInstruction;
    function FParsePrologue(AnAddress, AStartPC, AEndPC: TDBGPtr; out
      returnAddressOffset: word; out AnIsOutsideFrame: Boolean): Boolean;
    function FParseEpilogue(AnAddress, AStartPC, AEndPC: TDBGPtr; out
      returnAddressOffset: word; out AnIsOutsideFrame: Boolean): Boolean;

    function FormatInstruction(instr: TAVRInstruction): string;
  protected
    function GetLastErrorWasMemReadErr: Boolean; override;
    function GetMaxInstrSize: integer; override;
    function GetMinInstrSize: integer; override;
    function GetCanReverseDisassemble: boolean; override;
    function ReadCodeAt(AnAddress: TDBGPtr; var ALen: Cardinal): Boolean; inline;

  public
    procedure Disassemble(var AAddress: Pointer; out ACodeBytes: String; out ACode: String; out AnInfo: TDbgInstInfo); override; overload;
    procedure Disassemble(var AAddress: Pointer; out ACodeBytes: String; out ACode: String); override; overload;
    function GetInstructionInfo(AnAddress: TDBGPtr): TDbgAsmInstruction; override;

    // Don't use, not really suited to AVR ABI
    function GetFunctionFrameInfo(AnAddress: TDBGPtr; out
      AnIsOutsideFrame: Boolean): Boolean; override;

    // Rather use the next function to locate the call return address.
    // AStartPC & AEndPC indicates proc limits to help with scanning for prologue/epilogue
    // returnAddressOffset gives the offset to return address relative to Y pointer (r28:r29) inside frame
    // else returnAddressOffset gives the offset to return address relative to SP
    function GetFunctionFrameReturnAddress(AnAddress, AStartPC, AEndPC: TDBGPtr; out
      returnAddressOffset: word; out AnIsOutsideFrame: Boolean): Boolean;

    constructor Create(AProcess: TDbgProcess); override;
    destructor Destroy;
  end;


  { TDbgStackUnwinderAVR }

  TDbgStackUnwinderAVR = class(TDbgStackUnwinder)
  private
    FThread: TDbgThread;
    FProcess: TDbgProcess;
    FAddressSize: Integer;
    FLastFrameBaseIncreased: Boolean;
    FCodeReadErrCnt: integer;
  protected
    property Process: TDbgProcess read FProcess;
    property Thread: TDbgThread read FThread;
    property AddressSize: Integer read FAddressSize;
  public
    constructor Create(AProcess: TDbgProcess);
    procedure InitForThread(AThread: TDbgThread); override;
    procedure InitForFrame(ACurrentFrame: TDbgCallstackEntry; out CodePointer,
      StackPointer, FrameBasePointer: TDBGPtr); override;
    procedure GetTopFrame(out CodePointer, StackPointer, FrameBasePointer: TDBGPtr;
      out ANewFrame: TDbgCallstackEntry); override;
    function Unwind(AFrameIndex: integer; var CodePointer, StackPointer,
      FrameBasePointer: TDBGPtr; ACurrentFrame: TDbgCallstackEntry; out
      ANewFrame: TDbgCallstackEntry): TTDbgStackUnwindResult; override;
  end;

implementation

uses
  StrUtils, LazClasses, Math,
  FpDbgAvrClasses;

var
  DBG_WARNINGS: PLazLoggerLogGroup;

const
  statusFlagNames: array[0..7] of char = ('c', 'z', 'n', 'v', 's', 'h', 't', 'i');

  branchConditionStr: array[0..15] of string = (
   'cs', 'eq', 'mi', 'vs', 'lt', 'hs', 'ts', 'ie',
   'cc', 'ne', 'pl', 'vc', 'ge', 'hc', 'tc', 'id');

  OpCodeInfo: array[A_ADC..A_INVALID] of TAvrInstructionInfo = (
    (OpName: 'adc'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'add'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'adiw'; OperCount: 2; OperandFormats: (ofReg, ofHex8)),
    (OpName: 'and'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'andi'; OperCount: 2; OperandFormats: (ofReg, ofHex8)),
    (OpName: 'asr'; OperCount: 1; OperandFormats: (ofReg, ofNone)),
    (OpName: 'bld'; OperCount: 2; OperandFormats: (ofReg, ofDecimal)),
    (OpName: 'br'; OperCount: 1; OperandFormats: (ofRelAddr, ofNone)),
    (OpName: 'break'; OperCount: 0; OperandFormats: (ofNone, ofNone)),
    (OpName: 'bst'; OperCount: 2; OperandFormats: (ofReg, ofDecimal)),
    (OpName: 'call'; OperCount: 1; OperandFormats: (ofHex32, ofNone)),
    (OpName: 'cbi'; OperCount: 2; OperandFormats: (ofHex8, ofDecimal)),
    (OpName: 'cl'; OperCount: 0; OperandFormats: (ofNone, ofNone)),
    (OpName: 'com'; OperCount: 1; OperandFormats: (ofReg, ofNone)),
    (OpName: 'cp'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'cpc'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'cpi'; OperCount: 2; OperandFormats: (ofReg, ofHex8)),
    (OpName: 'cpse'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'dec'; OperCount: 1; OperandFormats: (ofReg, ofNone)),
    (OpName: 'des'; OperCount: 1; OperandFormats: (ofHex8, ofNone)),
    (OpName: 'eicall'; OperCount: 0; OperandFormats: (ofNone, ofNone)),
    (OpName: 'eijmp'; OperCount: 0; OperandFormats: (ofNone, ofNone)),
    (OpName: 'elpm'; OperCount: 2; OperandFormats: (ofReg, ofIndex)),
    (OpName: 'eor'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'fmul'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'fmuls'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'fmulsu'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'icall'; OperCount: 0; OperandFormats: (ofNone, ofNone)),
    (OpName: 'ijmp'; OperCount: 0; OperandFormats: (ofNone, ofNone)),
    (OpName: 'in'; OperCount: 2; OperandFormats: (ofReg, ofHex8)),
    (OpName: 'inc'; OperCount: 1; OperandFormats: (ofReg, ofNone)),
    (OpName: 'jmp'; OperCount: 1; OperandFormats: (ofHex32, ofNone)),
    (OpName: 'lac'; OperCount: 2; OperandFormats: (ofIndex, ofReg)),
    (OpName: 'las'; OperCount: 2; OperandFormats: (ofIndex, ofReg)),
    (OpName: 'lat'; OperCount: 2; OperandFormats: (ofIndex, ofReg)),
    (OpName: 'ld'; OperCount: 2; OperandFormats: (ofReg, ofIndex)),
    (OpName: 'ldd'; OperCount: 2; OperandFormats: (ofReg, ofIndexOffset)),
    (OpName: 'ldi'; OperCount: 2; OperandFormats: (ofReg, ofHex8)),
    (OpName: 'lds'; OperCount: 2; OperandFormats: (ofReg, ofHex16)),
    (OpName: 'lpm'; OperCount: 2; OperandFormats: (ofReg, ofIndex)),
    (OpName: 'lsl'; OperCount: 1; OperandFormats: (ofReg, ofNone)),
    (OpName: 'lsr'; OperCount: 1; OperandFormats: (ofReg, ofNone)),
    (OpName: 'mov'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'movw'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'mul'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'muls'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'mulsu'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'neg'; OperCount: 1; OperandFormats: (ofReg, ofNone)),
    (OpName: 'nop'; OperCount: 0; OperandFormats: (ofNone, ofNone)),
    (OpName: 'or'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'ori'; OperCount: 2; OperandFormats: (ofReg, ofHex8)),
    (OpName: 'out'; OperCount: 2; OperandFormats: (ofHex8, ofReg)),
    (OpName: 'pop'; OperCount: 1; OperandFormats: (ofReg, ofNone)),
    (OpName: 'push'; OperCount: 1; OperandFormats: (ofReg, ofNone)),
    (OpName: 'rcall'; OperCount: 1; OperandFormats: (ofRelAddr, ofNone)),
    (OpName: 'ret'; OperCount: 0; OperandFormats: (ofNone, ofNone)),
    (OpName: 'reti'; OperCount: 0; OperandFormats: (ofNone, ofNone)),
    (OpName: 'rjmp'; OperCount: 1; OperandFormats: (ofRelAddr, ofNone)),
    (OpName: 'rol'; OperCount: 1; OperandFormats: (ofReg, ofNone)),
    (OpName: 'ror'; OperCount: 1; OperandFormats: (ofReg, ofNone)),
    (OpName: 'sbc'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'sbci'; OperCount: 2; OperandFormats: (ofReg, ofDecimal)),
    (OpName: 'sbi'; OperCount: 2; OperandFormats: (ofHex8, ofDecimal)),
    (OpName: 'sbic'; OperCount: 2; OperandFormats: (ofHex8, ofDecimal)),
    (OpName: 'sbis'; OperCount: 2; OperandFormats: (ofHex8, ofDecimal)),
    (OpName: 'sbiw'; OperCount: 2; OperandFormats: (ofReg, ofDecimal)),
    (OpName: 'sbr'; OperCount: 2; OperandFormats: (ofReg, ofHex8)),
    (OpName: 'sbrc'; OperCount: 2; OperandFormats: (ofReg, ofDecimal)),
    (OpName: 'sbrs'; OperCount: 2; OperandFormats: (ofReg, ofDecimal)),
    (OpName: 'se'; OperCount: 0; OperandFormats: (ofNone, ofNone)),
    (OpName: 'ser'; OperCount: 0; OperandFormats: (ofNone, ofNone)),
    (OpName: 'sleep'; OperCount: 0; OperandFormats: (ofNone, ofNone)),
    (OpName: 'spm'; OperCount: 1; OperandFormats: (ofIndex, ofNone)),
    (OpName: 'st'; OperCount: 2; OperandFormats: (ofIndex, ofReg)),
    (OpName: 'std'; OperCount: 2; OperandFormats: (ofIndexOffset, ofReg)),
    (OpName: 'sts'; OperCount: 2; OperandFormats: (ofHex16, ofReg)),
    (OpName: 'sub'; OperCount: 2; OperandFormats: (ofReg, ofReg)),
    (OpName: 'subi'; OperCount: 2; OperandFormats: (ofReg, ofDecimal)),
    (OpName: 'swap'; OperCount: 1; OperandFormats: (ofReg, ofNone)),
    (OpName: 'wdr'; OperCount: 0; OperandFormats: (ofNone, ofNone)),
    (OpName: 'xch'; OperCount: 2; OperandFormats: (ofIndex, ofReg)),
    (OpName: '.dw'; OperCount: 1; OperandFormats: (ofHex16, ofNone))
  );

  // Constants for describing index registers of LD, LDD, ST, STS, LP, SPM
  operandNoIndex = 0;
  operandX = 1;
  operandXInc = 2;
  operandDecrX = 3;
  operandY = 4;
  operandYInc = 5;
  operandDecrY = 6;
  operandZ = 7;
  operandZInc = 8;
  operandDecrZ = 9;
  OperandIndexStr: array [operandX..operandDecrZ] of string = (
    'X', 'X+', '-X', 'Y', 'Y+', '-Y', 'Z', 'Z+', '-Z');

procedure get_r_d_10(o: word; out r, d: byte);
begin
  r := ((o shr 5) and $10) or (o and $f);
  d := (o shr 4) and $1f;
end;

procedure get_k_r16(o: word; out r, k: byte);
begin
  r := 16 + ((o shr 4) and $f);
  k := byte((o and $0f00) shr 4) or (o and $f);
end;

{ TAvrAsmInstruction }

procedure TAvrAsmInstruction.ReadCode;
begin
  if not (diCodeRead in FFlags) then begin
    if not FProcess.ReadData(FAddress, INSTR_CODEBIN_LEN, FCodeBin) then
      Include(FFlags, diCodeReadError);
    Include(FFlags, diCodeRead);
  end;
end;

procedure TAvrAsmInstruction.Disassemble;
var
  a: PByte;
  Disassembler: TAvrDisassembler;
begin
  if not (diDisAss in FFlags) then begin
    ReadCode;
    if diCodeReadError in FFlags then
      exit;
    a := @FCodeBin[0];
    Disassembler.Disassemble(a, FAvrInstruction);
    Include(FFlags, diDisAss);
  end;
end;

constructor TAvrAsmInstruction.Create(AProcess: TDbgProcess);
begin
  FProcess := AProcess;
  inherited Create;
  AddReference;
end;

procedure TAvrAsmInstruction.SetAddress(AnAddress: TDBGPtr);
begin
  FAddress := AnAddress;
  FFlags := [];
end;

function TAvrAsmInstruction.IsCallInstruction: boolean;
begin
  Result := False;
  ReadCode;
  if diCodeReadError in FFlags then
    exit;

  Disassemble;
  Result := FAvrInstruction.OpCode in [A_CALL, A_RCALL, A_ICALL, A_EICALL];
end;

function TAvrAsmInstruction.IsReturnInstruction: boolean;
begin
  Result := False;
  ReadCode;
  if diCodeReadError in FFlags then
    exit;

  Disassemble;
  Result := FAvrInstruction.OpCode in [A_RET, A_RETI];
end;

function TAvrAsmInstruction.IsLeaveStackFrame: boolean;
begin
  Result := false;
end;

function TAvrAsmInstruction.InstructionLength: Integer;
begin
  Result := 2;
  ReadCode;
  if diCodeReadError in FFlags then
    exit;

  Disassemble;
  Result := FAvrInstruction.Size;
end;

type
  TPrologueState = (psStart, psPush, psLoadSPL, psLoadSPH, psLoadSreg,
    psModifySPL, psModifySPH, psWriteSPH, psWriteSreg, psWriteSPL, psCopyParams);

function TAvrAsmDecoder.FParsePrologue(AnAddress, AStartPC, AEndPC: TDBGPtr;
  out returnAddressOffset: word; out AnIsOutsideFrame: Boolean): Boolean;
var
  ADataLen: Cardinal;
  AData: PByte;
  opcode, frameOffset: word;
  d, k: byte;
  stackState: TPrologueState;
begin
{ AVR function entry example
  3e:	df 93       	push	r29
  40:	cf 93       	push	r28
  42:	3f 92       	push	r3
  44:	2f 92       	push	r2

  // Load SP and calculate BP
  46:	cd b7       	in	r28, 0x3d	; 61    // SPL
  48:	de b7       	in	r29, 0x3e	; 62    // SPH
  4a:	c6 50       	subi	r28, 0x06	; 6
  4c:	d0 40       	sbci	r29, 0x00	; 0
  // Disable interrupts
  4e:	0f b6       	in	r0, 0x3f	; 63
  50:	f8 94       	cli
  // Then write out new SP
  52:	de bf       	out	0x3e, r29	; 62
  // Interleaved with restoring SREG
  54:	0f be       	out	0x3f, r0	; 63
  56:	cd bf       	out	0x3d, r28	; 61
  // Copy param to stack
  58:	8a 83       	std	Y+2, r24	; 0x02
  5a:	9b 83       	std	Y+3, r25	; 0x03
}
  // Also read next instruction, for simple procs/interrupts it may not be easy to spot the end of prologue
  // so the next instruction could tie break
  ADataLen := Min(MaxPrologueSize, AnAddress - AStartPC + 2);
  Result := ReadCodeAt(AStartPC, ADataLen);
  if not Result then
    exit;

  AData := @FCodeBin[0];
  // SP points to next empty slot, previous data is before current SP
  returnAddressOffset := 1;
  AnIsOutsideFrame := true;
  stackState := psStart;
  frameOffset := 0;

  // Loop until prologue buffer is empty, or stepped inside stack frame
  while (ADataLen > 1) and AnIsOutsideFrame do
  begin
    opcode := AData[0] or (AData[1] shl 8);
    inc(AData, 2);
    dec(ADataLen, 2);

    case opcode of
      OpCodeNOP: ;
      OpCodeLoadSPL: stackState := psLoadSPL;
      OpCodeLoadSPH: stackState := psLoadSPH;
      OpCodeLoadSRegR0, OpCodeLoadSRegR16: stackState := psLoadSreg;
      OpCodeCli: ;
      OpCodeSetSPH: stackState := psWriteSPH;
      OpCodeSetSregR0, OpCodeSetSregR16: stackState := psWriteSreg;
      OpCodeSetSPL: stackState := psWriteSPL;
      OpCodeZeroR1, OpCodeZeroR16: ;
    else
      // Push stack
      if (opcode and OpCodePushMask) = OpCodePushMask then
      begin
        if (stackState <= psPush) or (stackState = psLoadSreg) then
        begin
          inc(returnAddressOffset);
          if stackstate < psPush then
            stackState := psPush;
        end
        else
          DebugLn(DBG_WARNINGS, ['Invalid stack state for PUSH opcode: ', stackState]);
      end
      else if (opcode and OpCodeAdjustFrameLMask) = OpCodeAdjustFrameLMask then
      begin
        if stackState >= psLoadSPH then
        begin
          stackState := psModifySPL;
          // Decode register and constant
          get_k_r16(opcode, d, k);
          frameOffset := frameOffset + k;
        end
        else
          DebugLn(DBG_WARNINGS, ['Invalid stack state for OpCodeAdjustFrameLMask opcode: ', stackState]);
      end
      else if (opcode and OpCodeAdjustFrameHMask) = OpCodeAdjustFrameHMask then
      begin
        if stackState >= psLoadSPH then
        begin
          stackState := psModifySPH;
          // Decode register and constant
          get_k_r16(opcode, d, k);
          frameOffset := frameOffset + (k shl 8);
        end
        else
          DebugLn(DBG_WARNINGS, ['Invalid stack state for OpCodeAdjustFrameHMask opcode: ', stackState]);
      end
      else if (opcode and OpCodeAdjustFrameMask) = OpCodeAdjustFrameMask then
      begin
        if stackState >= psLoadSPH then
        begin
          stackState := psModifySPH;
          // Decode constant in SBIW opcode
          k := ((opcode and $00c0) shr 2) or (opcode and $f);
          frameOffset := frameOffset + k;
        end
        else
          DebugLn(DBG_WARNINGS, ['Invalid stack state for OpCodeAdjustFrameMask opcode: ', stackState]);
      end
      else if (opcode and OpCodeStoreOnStackMask) = OpCodeStoreOnStackMask then
      begin
        if stackState >= psWriteSPL then
        begin
          stackState := psCopyParams;
        end
        else
          DebugLn(DBG_WARNINGS, ['Invalid stack state for OpCodeStoreOnStackMask opcode: ', stackState]);
      end
      else  // Any other opcode isn't part of prologue
      begin
        AnIsOutsideFrame := false;
      end;
    end;
  end;

  // Check if frame pointer was updated
  if (stackState >= psWriteSPL) and (frameOffset > 0) then
  begin
    AnIsOutsideFrame := false;
    returnAddressOffset := returnAddressOffset + frameOffset;
  end
  else
    AnIsOutsideFrame := true;
end;

type
  // State sequence runs in reverse direction
  TEpilogueState = (esStart, esRet, esPop, esWriteSPH, esWriteSreg, esWriteSPL,
    esLoadSreg, esModifyFPH, esModifyFPL);

function TAvrAsmDecoder.FParseEpilogue(AnAddress, AStartPC, AEndPC: TDBGPtr;
  out returnAddressOffset: word; out AnIsOutsideFrame: Boolean): Boolean;
var
  ADataLen: Cardinal;
  AData: PByte;
  opcode, frameOffset: word;
  d, k: byte;
  stackState: TEpilogueState;
begin
{ AVR function epilogue example
  // Move FP to previous frame
  ba:	cd 5f       	subi	r28, 0xFD	; 253
  bc:	df 4f       	sbci	r29, 0xFF	; 255

  // Update SP
  be:	0f b6       	in	r0, 0x3f	; 63
  c0:	f8 94       	cli
  c2:	de bf       	out	0x3e, r29	; 62
  c4:	0f be       	out	0x3f, r0	; 63
  c6:	cd bf       	out	0x3d, r28	; 61

  // Restore saved regs
  c8:	cf 91       	pop	r28
  ca:	df 91       	pop	r29
  cc:	08 95       	ret
}

  ADataLen := Min(MaxEpilogueSize, AEndPC - AnAddress);
  Result := ReadCodeAt(AEndPC - ADataLen, ADataLen);
  if not Result then
    exit;

  AData := @FCodeBin[ADataLen - 2];
  // SP points to next empty slot, previous data is before current SP
  returnAddressOffset := 1;
  AnIsOutsideFrame := true;
  stackState := esStart;
  frameOffset := 0;

  // Loop until epilogue buffer is empty, or stepped inside stack frame
  while (ADataLen > 1) and AnIsOutsideFrame do
  begin
    opcode := AData[0] or (AData[1] shl 8);
    dec(AData, 2);
    dec(ADataLen, 2);

    case opcode of
      OpCodeNOP: ;
      OpCodeLoadSRegR0, OpCodeLoadSRegR16: stackState := esLoadSreg;
      OpCodeCli: ;
      OpCodeSetSPH: stackState := esWriteSPH;
      OpCodeSetSregR0, OpCodeSetSregR16: stackState := esWriteSreg;
      OpCodeSetSPL: stackState := esWriteSPL;
      OpCodeRet, OpCodeReti: stackState := esRet;
    else
      // Pop stack
      if (opcode and OpCodePopMask) = OpCodePopMask then
      begin
        if (stackState <= esPop) or (stackState = esWriteSreg) then
        begin
          inc(returnAddressOffset);
          if stackState < esPop then
            stackState := esPop;
        end
        else
          DebugLn(DBG_WARNINGS, ['Invalid stack state for POP opcode: ', stackState]);
      end
      else if (opcode and OpCodeAdjustFrameLMask) = OpCodeAdjustFrameLMask then
      begin
        if stackState < esModifyFPL then
        begin
          stackState := esModifyFPL;
          // Decode register and constant
          get_k_r16(opcode, d, k);
          // Normally subtract negative values to increase FP
          k := (256 - word(k));
          frameOffset := frameOffset + k;
        end
        else
          DebugLn(DBG_WARNINGS, ['Invalid stack state for OpCodeAdjustFrameLMask opcode: ', stackState]);
      end
      else if (opcode and OpCodeAdjustFrameHMask) = OpCodeAdjustFrameHMask then
      begin
        if stackState < esModifyFPH then
        begin
          stackState := esModifyFPH;
          // Decode register and constant
          get_k_r16(opcode, d, k);
          // Normally subtract negative values to increase FP, + carry bit
          k := (256 - word(k) - 1);
          frameOffset := frameOffset + (k shl 8);
        end
        else
          DebugLn(DBG_WARNINGS, ['Invalid stack state for OpCodeAdjustFrameHMask opcode: ', stackState]);
      end
      else  // Any other opcode isn't part of prologue
      begin
        AnIsOutsideFrame := false;
      end;
    end;
  end;

  // Check before frame pointer gets adjusted
  if (stackState >= esModifyFPL) and (frameOffset > 0) then
  begin
    AnIsOutsideFrame := false;
    returnAddressOffset := returnAddressOffset + frameOffset;
  end
  // Frame pointer inconsistent, so work relative to SP
  // Unreliable, SP can be adjusted inside frame
  //else if (stackState = esModifyFPH) then
  //begin
  //  AnIsOutsideFrame := true;
  //  returnAddressOffset := returnAddressOffset + frameOffset;
  //end
  //else if (stackState > esPop) then
  //begin
  //
  //end
  else
    AnIsOutsideFrame := true;
end;

function TAvrAsmDecoder.FormatInstruction(instr: TAVRInstruction): string;
const
  OutputFormats: array[0..2] of string = (
    '%s',
    '%:-7s %s',
    '%:-7s %s, %s');
var
  OpName: string;
  ops: array[1..2] of string;
  info: TAvrInstructionInfo;
  i: integer;
begin
  info := OpCodeInfo[instr.OpCode];
  OpName := info.OpName;

  // Special cases first
  case instr.OpCode of
    A_BR: OpName := OpName + branchConditionStr[instr.OpCodeMod];
    A_CL, A_SE: OpName := OpName + statusFlagNames[instr.OpCodeMod];
  end;

  // Instructions with variable number of operands
  if ((instr.OpCode in [A_ELPM, A_LPM]) and (instr.Oper[2] = operandNoIndex)) or
     ((instr.OpCode = A_SPM) and (instr.Oper[1] = operandNoIndex)) then
    info.OperCount := 0;

  if info.OperCount > 0 then
  begin
    for i := 1 to info.OperCount do
    begin
      case info.OperandFormats[i] of
        ofDecimal:     ops[i] := IntToStr(instr.Oper[i]);
        ofHex8:        ops[i] := '$'+IntToHex(instr.Oper[i], 2);
        ofHex16:       ops[i] := '$'+IntToHex(instr.Oper[i], 4);
        ofHex32:       ops[i] := '$'+IntToHex(instr.Oper[i], 8);
        ofReg:         ops[i] := 'r' + IntToStr(instr.Oper[i]);
        ofIndex:       ops[i] := OperandIndexStr[instr.Oper[i]];
        ofIndexOffset:
        begin
          // IndexOffset should be either Y+ or Z+
          if not(instr.OpCodeMod in [operandYInc, operandZInc]) then
            writeln('ERROR');
          ops[i] := OperandIndexStr[instr.OpCodeMod] + IntToStr(instr.Oper[i]);
        end;
        ofRelAddr:
        begin
          if instr.Oper[i] >= 0 then
            ops[i] := '.+' + IntToStr(instr.Oper[i])
          else
            ops[i] := '.' + IntToStr(instr.Oper[i]);
        end;
      else
        writeln('ERROR');
      end;
    end;

    if info.OperCount = 1 then
      Result := Format(OutputFormats[1], [OpName, ops[1]])
    else
      Result := Format(OutputFormats[2], [OpName, ops[1], ops[2]]);
  end
  else
    Result := Format(OutputFormats[0], [OpName]);
end;

function TAvrAsmDecoder.GetLastErrorWasMemReadErr: Boolean;
begin
  Result := FLastErrWasMem;
end;

function TAvrAsmDecoder.GetMaxInstrSize: integer;
begin
  Result := 4;
end;

function TAvrAsmDecoder.GetMinInstrSize: integer;
begin
  Result := 2;
end;

function TAvrAsmDecoder.GetCanReverseDisassemble: boolean;
begin
  Result := true;
end;

function TAvrAsmDecoder.ReadCodeAt(AnAddress: TDBGPtr; var ALen: Cardinal
  ): Boolean;
begin
  Result := FProcess.ReadData(AnAddress, ALen, FCodeBin[0], ALen);
  FLastErrWasMem := not Result;
end;

procedure TAvrAsmDecoder.Disassemble(var AAddress: Pointer; out
  ACodeBytes: String; out ACode: String; out AnInfo: TDbgInstInfo);
var
  decoder: TAvrDisassembler;
  k: byte;
  pcode: PByte;
  instr: TAVRInstruction;
begin
  AnInfo := default(TDbgInstInfo);
  pcode := AAddress;
  decoder.Disassemble(AAddress, instr);
  Inc(AAddress, instr.Size);

  ACodeBytes := '';
  for k := 0 to instr.Size-1 do
    ACodeBytes := ACodeBytes + HexStr(pcode[k], 2) + ' ';
  Delete(ACodeBytes, length(ACodeBytes), 1);
  ACode := FormatInstruction(instr);

  // Todo: Indicate whether currrent instruction has a destination code address
  //       call jump branch(?)
  //       Return information in AnInfo
end;

procedure TAvrAsmDecoder.Disassemble(var AAddress: Pointer; out
  ACodeBytes: String; out ACode: String);
var
  AnInfo: TDbgInstInfo;
begin
  Disassemble(AAddress, ACodeBytes, ACode, AnInfo);
end;

function TAvrAsmDecoder.GetInstructionInfo(AnAddress: TDBGPtr
  ): TDbgAsmInstruction;
begin
  if (FLastInstr = nil) or (FLastInstr.RefCount > 1) then begin
    ReleaseRefAndNil(FLastInstr);
    FLastInstr := TAvrAsmInstruction.Create(FProcess);
  end;

  FLastInstr.SetAddress(AnAddress);
  Result := FLastInstr;
end;

function TAvrAsmDecoder.GetFunctionFrameInfo(AnAddress: TDBGPtr; out
  AnIsOutsideFrame: Boolean): Boolean;
begin
  Result := False;
end;

function TAvrAsmDecoder.GetFunctionFrameReturnAddress(AnAddress, AStartPC,
  AEndPC: TDBGPtr; out returnAddressOffset: word; out AnIsOutsideFrame: Boolean
  ): Boolean;
begin
  { Cases to consider:
    A - if (AStartPC + MaxPrologueSize < AnAddress) and (AnAddress + MaxEpilogueSize < AEndPC)
        then currently inside stack frame. Parse prologue to figure out
        offset from frame pointer to return address.

    B - if (AStartPC + MaxPrologueSize < AnAddress)
        then possibly before final stack frame.  Need to parse prologue up to AnAddress
        to figure out how far the stack has moved since entry to calculate offset
        from SP to return address. If frame pointer has been configured before
        AnAddress, assume inside frame and return offset relative to frame pointer.

    C - if (AnAddress + MaxEpilogueSize < AEndPC)
        then possibly inside frame teardown.  Need to reverse parse epilogue up to AnAddress
        to figure out how much frame will unwind to calculate offset to return address.
        If frame pointer has been restored before AnAddress then ouside frame.
  }

  if (AnAddress = AStartPC) or (AnAddress = AEndPC) then
  begin
    // Frame not yet constructed, so return address is located via SP + offset
    returnAddressOffset := 1;
    AnIsOutsideFrame := true;
    exit;
  end
  //else if (AStartPC + MaxPrologueSize > AnAddress) then
  else if (AnAddress - AStartPC) < (AEndPC - AnAddress) then
    result := FParsePrologue(AnAddress, AStartPC, AEndPC, returnAddressOffset, AnIsOutsideFrame)
  else
    result := FParseEpilogue(AnAddress, AStartPC, AEndPC, returnAddressOffset, AnIsOutsideFrame);
end;

constructor TAvrAsmDecoder.Create(AProcess: TDbgProcess);
begin
  FProcess := AProcess;
end;

destructor TAvrAsmDecoder.Destroy;
begin
  ReleaseRefAndNil(FLastInstr);
  inherited Destroy;
end;

{ TAvrDisassembler }

function TAvrDisassembler.InvalidOpCode(instr: word): TAVRInstruction;
begin
  result.OpCode := A_INVALID;
  result.Size := 2;
  result.Oper[1] := instr;
end;

function TAvrDisassembler.SetInstructionInfo(AOpCode: TAVROpCode;
  AOpcodeMod: integer; ASize: integer; AOper: array of
  integer): TAVRInstruction;
var
  i: integer;
begin
  Result.OpCode := AOpCode;
  Result.OpCodeMod := AOpcodeMod;
  Result.Size := ASize;

  for i := 1 to 2 do
  begin
    if i <= OpCodeInfo[AOpCode].OperCount then
      Result.Oper[i] := AOper[i-1]
    else
      Result.Oper[i] := 0;
  end;
end;

procedure TAvrDisassembler.Disassemble(var AAddress: Pointer; out
  AnInstruction: TAVRInstruction);
var
  CodeIdx, r, d, k, q: byte;
  a: SmallInt;
  pcode: PByte;
  code, addr16: word;
  _set: boolean;
  instr: TAVRInstruction absolute AnInstruction;
begin
  pcode := AAddress;
  CodeIdx := 0;
  code := pcode[CodeIdx];
  inc(CodeIdx);
  code := code or (pcode[CodeIdx] shl 8);
  inc(CodeIdx);

  case (code and $f000) of
    $0000:
    begin
      case (code) of
        $0000: instr := SetInstructionInfo(A_NOP, 0, 2, []);
      else
        case (code and $fc00) of
          $0400:
          begin // CPC compare with carry 0000 01rd dddd rrrr
            get_r_d_10(code, r, d);
            instr := SetInstructionInfo(A_CPC, 0, 2, [d, r]);
          end;
          $0c00:
          begin // ADD without carry 0000 11 rd dddd rrrr
            get_r_d_10(code, r, d);
            instr := SetInstructionInfo(A_ADD, 0, 2, [d, r]);
          end;
          $0800:
          begin // SBC subtract with carry 0000 10rd dddd rrrr
            get_r_d_10(code, r, d);
            instr := SetInstructionInfo(A_SBC, 0, 2, [d, r]);
          end;
          else
            case (code and $ff00) of
              $0100:
              begin // MOVW – Copy Register Word 0000 0001 dddd rrrr
                d := ((code shr 4) and $f) shl 1;
                r := ((code) and $f) shl 1;
                instr := SetInstructionInfo(A_MOVW, 0, 2, [d, r]);
              end;
              $0200:
              begin // MULS – Multiply Signed 0000 0010 dddd rrrr
                r := 16 + (code and $f);
                d := 16 + ((code shr 4) and $f);
                instr := SetInstructionInfo(A_MULS, 0, 2, [d, r]);
              end;
              $0300:
              begin // MUL Multiply 0000 0011 fddd frrr
                r := 16 + (code and $7);
                d := 16 + ((code shr 4) and $7);
                case (code and $88) of
                  $00: instr := SetInstructionInfo(A_MULSU, 0, 2, [d, r]);
                  $08: instr := SetInstructionInfo(A_FMUL, 0, 2, [d, r]);
                  $80: instr := SetInstructionInfo(A_FMULS, 0, 2, [d, r]);
                  $88: instr := SetInstructionInfo(A_FMULSU, 0, 2, [d, r]);
                end;
              end;
              else
                instr := InvalidOpCode(code);
            end; // case (code and $ff00)
        end;  // case (code and $fc00)
      end; // case code of
    end;
    $1000:
    begin
      case (code and $fc00) of
        $1800:
        begin // SUB without carry 0000 10 rd dddd rrrr
          get_r_d_10(code, r, d);
          instr := SetInstructionInfo(A_SUB, 0, 2, [d, r]);
        end;
        $1000:
        begin // CPSE Compare, skip if equal 0000 00 rd dddd rrrr
          get_r_d_10(code, r, d);
          instr := SetInstructionInfo(A_CPSE, 0, 2, [d, r]);
        end;
        $1400:
        begin // CP Compare 0000 01 rd dddd rrrr
          get_r_d_10(code, r, d);
          instr := SetInstructionInfo(A_CP, 0, 2, [d, r]);
        end;
        $1c00:
        begin // ADD with carry 0001 11 rd dddd rrrr
          get_r_d_10(code, r, d);
          instr := SetInstructionInfo(A_ADC, 0, 2, [d, r]);
        end;
        else
          instr := InvalidOpCode(code);
      end;
    end;
    $2000:
    begin
      case (code and $fc00) of
        $2000:
        begin // AND 0010 00rd dddd rrrr
          get_r_d_10(code, r, d);
          instr := SetInstructionInfo(A_AND, 0, 2, [d, r]);
        end;
        $2400:
        begin // EOR 0010 01rd dddd rrrr
          get_r_d_10(code, r, d);
          instr := SetInstructionInfo(A_EOR, 0, 2, [d, r]);
        end;
        $2800:
        begin // OR Logical OR 0010 10rd dddd rrrr
          get_r_d_10(code, r, d);
          instr := SetInstructionInfo(A_OR, 0, 2, [d, r]);
        end;
        $2c00:
        begin // MOV 0010 11rd dddd rrrr
          get_r_d_10(code, r, d);
          instr := SetInstructionInfo(A_MOV, 0, 2, [d, r]);
        end;
        else
          instr := InvalidOpCode(code);
      end;
    end;
    $3000:
    begin // CPI 0011 KKKK rrrr KKKK
      get_k_r16(code, d, k);
      instr := SetInstructionInfo(A_CPI, 0, 2, [d, k]);
    end;
    $4000:
    begin // SBCI Subtract Immediate With Carry 0100 kkkk dddd kkkk
      get_k_r16(code, d, k);
      instr := SetInstructionInfo(A_SBCI, 0, 2, [d, k]);
    end;
    $5000:
    begin // SUB Subtract Immediate 0101 kkkk dddd kkkk
      get_k_r16(code, d, k);
      instr := SetInstructionInfo(A_SUBI, 0, 2, [d, k]);
    end;
    $6000:
    begin // ORI aka SBR Logical AND with Immediate 0110 kkkk dddd kkkk
      get_k_r16(code, d, k);
      instr := SetInstructionInfo(A_ORI, 0, 2, [d, k]);
    end;
    $7000:
    begin // ANDI Logical AND with Immediate 0111 kkkk dddd kkkk
      get_k_r16(code, d, k);
      instr := SetInstructionInfo(A_ANDI, 0, 2, [d, k]);
    end;
    $a000,
    $8000:
    begin
      case (code and $d008) of
        $a000,
        $8000:
        begin // LD (LDD) – Load Indirect using Z 10q0 qq0r rrrr 0qqq
              // ST (STD) - Store Indirect with Z 10q0 qq1r rrrr 0qqq
          // This check overlaps with the 16 bit LDS/STS instructions of reduced core tiny
          // Todo: only activate this check if debugging a tiny
          if false and ((code and $A000) = $A000) then
          begin
            r := 16 + ((code shr 4) and $0F);
            k := ((code shr 5) and $30) or (code and $0F);
            if (code and $0100) = 0 then
              k := k or $80
            else
              k := k or $40;

            if (code and $800) = 0 then
              instr := SetInstructionInfo(A_LDS, 0, 2, [r, k])
            else
              instr := SetInstructionInfo(A_STS, 0, 2, [k, r]);
          end
          else
          begin
            d := (code shr 4) and $1f;
            q := ((code and $2000) shr 8) or ((code and $0c00) shr 7) or (code and $7);
            if (code and $0200) <> 0 then // store
            begin
              if q > 0 then
                instr := SetInstructionInfo(A_STD, operandZInc, 2, [q, d])
              else
              begin
                q := OperandZ + (code and 3);
                instr := SetInstructionInfo(A_ST, 0, 2, [q, d]);
              end;
            end
            else  // load
            begin
              if q > 0 then
                instr := SetInstructionInfo(A_LDD, operandZInc, 2, [d, q])
              else
                instr := SetInstructionInfo(A_LD, 0, 2, [d, operandZ + (code and 3)]);
            end;
          end;
        end;
        $a008,
        $8008:
        begin // LD (LDD) – Load Indirect using Y 10q0 qq0r rrrr 1qqq
          d := (code shr 4) and $1f;
          q := ((code and $2000) shr 8) or ((code and $0c00) shr 7) or (code and $7);
          if (code and $0200) <> 0 then // store
          begin
            if q > 0 then
              instr := SetInstructionInfo(A_STD, operandYInc, 2, [q, d])
            else
              instr := SetInstructionInfo(A_ST, 0, 2, [operandY + (code and 3), d]);
          end
          else  // load
          begin
            if q > 0 then
              instr := SetInstructionInfo(A_LDD, operandYInc, 2, [d, q])
            else
              instr := SetInstructionInfo(A_LD, 0, 2, [d, operandY + (code and 3)]);
          end;
        end;
        else
          instr := InvalidOpCode(code);
      end;
    end;
    $9000:
    begin
      if ((code and $ff0f) = $9408) then  // clear/set SREG flags
      begin
        k := (code shr 4) and 7;
        if ((code and $0080) = 0) then
          instr := SetInstructionInfo(A_SE, k, 2, [])
        else
          instr := SetInstructionInfo(A_CL, k, 2, []);
      end
      else
        case (code) of
          $9409: instr := SetInstructionInfo(A_IJMP, 0, 2, []);
          $9419: instr := SetInstructionInfo(A_EIJMP, 0, 2, []);
          $9508: instr := SetInstructionInfo(A_RET, 0, 2, []);
          $9509: instr := SetInstructionInfo(A_ICALL, 0, 2, []);
          $9519: instr := SetInstructionInfo(A_EICALL, 0, 2, []);
          $9518: instr := SetInstructionInfo(A_RETI, 0, 2, []);
          $9588: instr := SetInstructionInfo(A_SLEEP, 0, 2, []);
          $9598: instr := SetInstructionInfo(A_BREAK, 0, 2, []);
          $95a8: instr := SetInstructionInfo(A_WDR, 0, 2, []);
          $95c8: instr := SetInstructionInfo(A_LPM, 0, 2, [0, operandNoIndex]);
          $95d8: instr := SetInstructionInfo(A_ELPM, 0, 2, [0, operandNoIndex]);
          $95e8: instr := SetInstructionInfo(A_SPM, 0, 2, [operandNoIndex]);
          $95f8: instr := SetInstructionInfo(A_SPM, 0, 2, [operandZInc]);
          else
          begin
            case (code and $fe0f) of
              $9000:
              begin // LDS Load Direct from fData Space, 32 bits
                r := (code shr 4) and $1f;
                addr16 := pcode[CodeIdx];
                inc(CodeIdx);
                addr16 := addr16 or (pcode[CodeIdx] shl 8);
                inc(CodeIdx);
                instr := SetInstructionInfo(A_LDS, 0, 4, [r, addr16]);
              end;
              $9005,
              $9004:
              begin // LPM Load Program Memory 1001 000d dddd 01oo
                r := (code shr 4) and $1f;
                if (code and 1 = 1) then
                  instr := SetInstructionInfo(A_LPM, 0, 2, [r, operandZInc])
                else
                  instr := SetInstructionInfo(A_LPM, 0, 2, [r, operandZ]);
              end;
              $9006,
              $9007:
              begin // ELPM Extended Load Program Memory 1001 000d dddd 01oo
                r := (code shr 4) and $1f;
                if (code and 1 = 1) then
                  instr := SetInstructionInfo(A_ELPM, 0, 2, [r, operandZInc])
                else
                  instr := SetInstructionInfo(A_ELPM, 0, 2, [r, operandZ]);
              end;
              $900c,
              $900d,
              $900e:
              begin // LD Load Indirect from fData using X 1001 000r rrrr 11oo
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_LD, 0, 2, [r, operandX + (code and 3)]);
              end;
              $920c,
              $920d,
              $920e:
              begin // ST Store Indirect fData Space X 1001 001r rrrr 11oo
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_ST, 0, 2, [operandX + (code and 3), r])
              end;
              $9009,
              $900a:
              begin // LD Load Indirect from fData using Y 1001 000r rrrr 10oo
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_LD, 0, 2, [r, operandY + (code and 3)])
              end;
              $9209,
              $920a:
              begin // ST Store Indirect fData Space Y 1001 001r rrrr 10oo
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_ST, 0, 2, [operandY + (code and 3), r])
              end;
              $9200:
              begin // STS Store Direct to Data Space, 32 bits
                r := (code shr 4) and $1f;
                addr16 := pcode[CodeIdx];
                inc(CodeIdx);
                addr16 := addr16 or (pcode[CodeIdx] shl 8);
                inc(CodeIdx);
                instr := SetInstructionInfo(A_STS, 0, 4, [addr16, r])
              end;
              $9001,
              $9002:
              begin // LD Load Indirect from Data using Z 1001 001r rrrr 00oo
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_LD, 0, 2, [r, operandZ + (code and 3)])
              end;
              $9201,
              $9202:
              begin // ST Store Indirect Data Space Z 1001 001r rrrr 0Xoo X=0 or XMega instructions X=1
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_ST, 0, 2, [operandZ + (code and 3), r])
              end;

              $9204..$9207:
              begin  // AVR8X instructions
                r := (code shr 4) and $1f;
                case (code and 3) of
                  0: instr := SetInstructionInfo(A_XCH, 0, 2, [operandZ, r]);
                  1: instr := SetInstructionInfo(A_LAS, 0, 2, [operandZ, r]);
                  2: instr := SetInstructionInfo(A_LAC, 0, 2, [operandZ, r]);
                  3: instr := SetInstructionInfo(A_LAT, 0, 2, [operandZ, r]);
                end;
              end;

              $900f:
              begin // POP 1001 000d dddd 1111
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_POP, 0, 2, [r])
              end;
              $920f:
              begin // PUSH 1001 001d dddd 1111
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_PUSH, 0, 2, [r])
              end;
              $9400:
              begin // COM – One’s Complement
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_COM, 0, 2, [r])
              end;
              $9401:
              begin // NEG – Two’s Complement
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_NEG, 0, 2, [r])
              end;
              $9402:
              begin // SWAP – Swap Nibbles
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_SWAP, 0, 2, [r])
              end;
              $9403:
              begin // INC – Increment
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_INC, 0, 2, [r])
              end;
              $9405:
              begin // ASR – Arithmetic Shift Right 1001 010d dddd 0101
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_ASR, 0, 2, [r])
              end;
              $9406:
              begin // LSR 1001 010d dddd 0110
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_LSR, 0, 2, [r])
              end;
              $9407:
              begin // ROR 1001 010d dddd 0111
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_ROR, 0, 2, [r])
              end;
              $940a:
              begin // DEC – Decrement
                r := (code shr 4) and $1f;
                instr := SetInstructionInfo(A_DEC, 0, 2, [r])
              end;
              $940c,
              $940d:
              begin // JMP Long Call to sub, 32 bits
                k := ((code and $01f0) shr 3) or (code and 1);
                addr16 := pcode[CodeIdx];
                inc(CodeIdx);
                addr16 := addr16 or (pcode[CodeIdx] shl 8);
                inc(CodeIdx);
                instr := SetInstructionInfo(A_JMP, 0, 4, [dword(k shl 16) or (dword(addr16) shl 1)]);
              end;
              $940e,
              $940f:
              begin // CALL Long Call to sub, 32 bits
                k := ((code and $01f0) shr 3) or (code and 1);
                addr16 := pcode[CodeIdx];
                inc(CodeIdx);
                addr16 := addr16 or (pcode[CodeIdx] shl 8);
                inc(CodeIdx);
                instr := SetInstructionInfo(A_CALL, 0, 4, [dword(k shl 16) or (dword(addr16) shl 1)]);
              end;
              else
              begin
                case (code and $ff00) of
                  $9600:
                  begin // ADIW - Add Immediate to Word 1001 0110 KKdd KKKK
                    r := 24 + ((code shr 3) and $6);
                    k := ((code and $00c0) shr 2) or (code and $f);
                    instr := SetInstructionInfo(A_ADIW, 0, 2, [r, k]);
                  end;
                  $9700:
                  begin // SBIW - Subtract Immediate from Word 1001 0110 KKdd KKKK
                    r := 24 + ((code shr 3) and $6);
                    k := ((code and $00c0) shr 2) or (code and $f);
                    instr := SetInstructionInfo(A_SBIW, 0, 2, [r, k]);
                  end;
                  $9800:
                  begin // CBI - Clear Bit in I/O Register 1001 1000 AAAA Abbb
                    d := (code shr 3) and $1f;
                    k := code and $7;
                    instr := SetInstructionInfo(A_CBI, 0, 2, [d, k]);
                  end;
                  $9900:
                  begin // SBIC - Skip if Bit in I/O Register is Cleared 1001 0111 AAAA Abbb
                    d := (code shr 3) and $1f;
                    k := code and $7;
                    instr := SetInstructionInfo(A_SBIC, 0, 2, [d, k]);
                  end;
                  $9a00:
                  begin // SBI - Set Bit in I/O Register 1001 1000 AAAA Abbb
                    d := (code shr 3) and $1f;
                    k := code and $7;
                    instr := SetInstructionInfo(A_SBI, 0, 2, [d, k]);
                  end;
                  $9b00:
                  begin // SBIS - Skip if Bit in I/O Register is Set 1001 1011 AAAA Abbb
                    d := (code shr 3) and $1f;
                    k := code and $7;
                    instr := SetInstructionInfo(A_SBIS, 0, 2, [d, k]);
                  end;
                  else
                    case (code and $fc00) of
                      $9c00:
                      begin // MUL - Multiply Unsigned 1001 11rd dddd rrrr
                        get_r_d_10(code, r, d);
                        instr := SetInstructionInfo(A_MUL, 0, 2, [d, k]);
                      end;
                      else
                        instr := InvalidOpCode(code);
                    end;
                end;
              end;
            end;
          end;
        end;
    end;
    $b000:
    begin
      case (code and $f800) of
        $b800:
        begin // OUT A,Rr 1011 1AAr rrrr AAAA
          r := (code shr 4) and $1f;
          d := ((((code shr 9) and 3) shl 4) or ((code) and $f));
          instr := SetInstructionInfo(A_OUT, 0, 2, [d, r]);
        end;
        $b000:
        begin // IN Rd,A 1011 0AAr rrrr AAAA
          r := (code shr 4) and $1f;
          d := ((((code shr 9) and 3) shl 4) or ((code) and $f));
          instr := SetInstructionInfo(A_IN, 0, 2, [r, d]);
        end;
        else
          instr := InvalidOpCode(code);
      end;
    end;
    $c000:
    begin // RJMP 1100 kkkk kkkk kkkk
      a := smallint((word(code) shl 4) and $ffff) div 16;
      instr := SetInstructionInfo(A_RJMP, 0, 2, [a shl 1]);
    end;
    $d000:
    begin // RCALL 1100 kkkk kkkk kkkk
      a := smallint((word(code) shl 4) and $ffff) div 16;
      instr := SetInstructionInfo(A_RCALL, 0, 2, [a shl 1]);
    end;
    $e000:
    begin // LDI Rd, K 1110 KKKK RRRR KKKK -- aka SER (LDI r, $ff)
      d := 16 + ((code shr 4) and $f);
      k := ((code and $0f00) shr 4) or (code and $f);
      instr := SetInstructionInfo(A_LDI, 0, 2, [d, k]);
    end;
    $f000:
    begin
      case (code and $fe00) of
        $f000,
        $f200,
        $f400,
        $f600:
        begin // All the fSREG branches
          a := smallint(smallint(code shl 6) shr 9) * 2; // offset
          k := code and 7;
          _set := (code and $0400) = 0; // this bit means BRXC otherwise BRXS
          if not _set then
            k := k + 8;
          instr := SetInstructionInfo(A_BR, k, 2, [a]);
        end;
        $f800,
        $f900:
        begin // BLD – Bit Load from T into a Bit in Register 1111 100r rrrr 0bbb
          d := (code shr 4) and $1f; // register index
          k := code and 7;
          instr := SetInstructionInfo(A_BLD, 0, 2, [d, k]);
        end;
        $fa00,
        $fb00:
        begin // BST – Bit Store into T from bit in Register 1111 100r rrrr 0bbb
          r := (code shr 4) and $1f; // register index
          k := code and 7;
          instr := SetInstructionInfo(A_BST, 0, 2, [r, k]);
        end;
        $fc00,
        $fe00:
        begin // SBRS/SBRC – Skip if Bit in Register is Set/Clear 1111 11sr rrrr 0bbb
          r := (code shr 4) and $1f; // register index
          k := code and 7;
          _set := (code and $0200) <> 0;
          if _set then
            instr := SetInstructionInfo(A_SBRS, 0, 2, [r, k])
          else
            instr := SetInstructionInfo(A_SBRC, 0, 2, [r, k]);
        end;
        else
          instr := InvalidOpCode(code);
      end;
    end;
    else
      instr := InvalidOpCode(code);
  end;
end;

{ TDbgStackUnwinderAVR }

constructor TDbgStackUnwinderAVR.Create(AProcess: TDbgProcess);
begin
  FProcess := AProcess;
  FAddressSize := 2;
  FCodeReadErrCnt := 0;
end;

procedure TDbgStackUnwinderAVR.InitForThread(AThread: TDbgThread);
begin
  FThread := AThread;
end;

procedure TDbgStackUnwinderAVR.InitForFrame(ACurrentFrame: TDbgCallstackEntry;
  out CodePointer, StackPointer, FrameBasePointer: TDBGPtr);
var
  R: TDbgRegisterValue;
begin
  CodePointer := ACurrentFrame.AnAddress;

  // Frame pointer is r29:r28 (if used)
  FrameBasePointer := ACurrentFrame.FrameAdress;

  //Could update using GetStackBasePointerRegisterValue

  //R := ACurrentFrame.RegisterValueList.FindRegisterByDwarfIndex(FDwarfNumBP);
  //if R <> nil then
  //  FrameBasePointer := R.NumValue;

  StackPointer := 0;
  R := ACurrentFrame.RegisterValueList.FindRegisterByDwarfIndex(SPindex);
  if R = nil then exit;
  StackPointer := R.NumValue;
end;

procedure TDbgStackUnwinderAVR.GetTopFrame(out CodePointer, StackPointer,
  FrameBasePointer: TDBGPtr; out ANewFrame: TDbgCallstackEntry);
var
  i: Integer;
  R: TDbgRegisterValue;
begin
  FLastFrameBaseIncreased := True;
  CodePointer      := Thread.GetInstructionPointerRegisterValue;
  StackPointer     := Thread.GetStackPointerRegisterValue;
  FrameBasePointer := Thread.GetStackBasePointerRegisterValue;
  ANewFrame        := TDbgCallstackEntry.create(Thread, 0, FrameBasePointer, CodePointer);

  // Frame pointer may not have been updated yet
  if FrameBasePointer > StackPointer then
    FrameBasePointer := StackPointer;

  i := Thread.RegisterValueList.Count;
  while i > 0 do begin
    dec(i);
    R := Thread.RegisterValueList[i];
    ANewFrame.RegisterValueList.DbgRegisterAutoCreate[R.Name].SetValue(R.NumValue, R.StrValue, R.Size, R.DwarfIdx);
  end;
end;

function TDbgStackUnwinderAVR.Unwind(AFrameIndex: integer; var CodePointer,
  StackPointer, FrameBasePointer: TDBGPtr; ACurrentFrame: TDbgCallstackEntry;
  out ANewFrame: TDbgCallstackEntry): TTDbgStackUnwindResult;
const
  MAX_FRAMES = 50000; // safety net
  // To read RAM, add data space offset to address
  DataOffset = $800000;
  Size = 2;
var
  LastFrameBase: TDBGPtr;
  OutSideFrame: Boolean;
  startPC, endPC: TDBGPtr;
  returnAddrStackOffset: word;
  b: byte;
begin
  ANewFrame := nil;
  Result := suFailed;

  if StackPointer = 0 then
    exit;
  if not FLastFrameBaseIncreased then
    exit;

  OutSideFrame := False;
  LastFrameBase := FrameBasePointer;

  // Get start/end PC of proc from debug info
  if not Self.Process.FindProcStartEndPC(CodePointer, startPC, endPC) then
  begin
    { Assume we are at beginning of proc. GetFunctionFrameReturnAddress should then
      assume we are outside the stack frame (or no stack frame exists).
    }
    endPC := CodePointer;
  end;

  if not TAvrAsmDecoder(Process.Disassembler).GetFunctionFrameReturnAddress(CodePointer, startPC, endPC, returnAddrStackOffset, OutSideFrame) then
  begin
    OutSideFrame := False;
  end;

  if OutSideFrame then begin
    // Before adjustment of frame pointer, or after restoration of frame pointer,
    // return PC should be located by offset from SP
    if not Process.ReadData(DataOffset or (StackPointer + returnAddrStackOffset), Size, CodePointer) or
      (CodePointer = 0) then
      exit;
    {$PUSH}{$R-}{$Q-}
    StackPointer := StackPointer + returnAddrStackOffset + 1;
    FrameBasePointer := StackPointer; // After popping return-addr from "StackPtr"
    {$POP}
  end
  else begin
    // Inside stack frame, return PC should be located by offset from FP
    if not Process.ReadData(DataOffset or (FrameBasePointer + returnAddrStackOffset), Size, CodePointer) or (CodePointer = 0) then exit;
    {$PUSH}{$R-}{$Q-}
    FrameBasePointer := StackPointer + returnAddrStackOffset + Size - 1; // After popping return-addr from stack
    // An estimate of SP, needed when attempting unwinding of next frame
    // If registers are spilled to stack this will be wrong.
    StackPointer := FrameBasePointer;
    {$POP}
  end;
  // Convert return address from BE to LE, shl 1 to get byte address
  CodePointer := BEtoN(word(CodePointer)) shl 1;

  FLastFrameBaseIncreased := (FrameBasePointer <> 0) and (FrameBasePointer > LastFrameBase);

  ANewFrame:= TDbgCallstackEntry.create(Thread, AFrameIndex, FrameBasePointer, CodePointer);
  ANewFrame.RegisterValueList.DbgRegisterAutoCreate[nPC].SetValue(CodePointer, IntToStr(CodePointer),Size, PCindex);
  ANewFrame.RegisterValueList.DbgRegisterAutoCreate[nSP].SetValue(StackPointer, IntToStr(StackPointer),Size, SPindex);
  ANewFrame.RegisterValueList.DbgRegisterAutoCreate['r28'].SetValue(byte(FrameBasePointer), IntToStr(byte(FrameBasePointer)),Size, 28);
  b := byte(FrameBasePointer shr 8);
  ANewFrame.RegisterValueList.DbgRegisterAutoCreate['r29'].SetValue(b, IntToStr(b), Size, 29);

  FCodeReadErrCnt := 0;
  Result := suSuccess;
end;

initialization
  DebugLogger.FindOrRegisterLogGroup('DBG_WARNINGS' {$IFDEF DBG_WARNINGS} , True {$ENDIF} );

end.