forked from audetto/AppleWin
/
Debugger_DisassemblerData.cpp
684 lines (572 loc) · 17.1 KB
/
Debugger_DisassemblerData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
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
/*
AppleWin : An Apple //e emulator for Windows
Copyright (C) 2006-2014, Tom Charlesworth, Michael Pohoreski
AppleWin 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.
AppleWin 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.
You should have received a copy of the GNU General Public License
along with AppleWin; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* Description: Data Blocks shown in Disassembler
*
* Author: Copyright (C) 2009 - 2010 Michael Pohoreski
*/
#include "StdAfx.h"
#include "Debug.h"
// Disassembler Data ______________________________________________________________________________
// __ Debugger Interface ____________________________________________________________________________
void _GetAutoSymbolName ( const Nopcode_e &nopcode, const WORD nStartAddress, char *pSymbolName )
{
switch (nopcode)
{
case NOP_ADDRESS:
sprintf( pSymbolName, "A_%04X", nStartAddress ); // DA range
break;
case NOP_STRING_ASCII:
case NOP_STRING_APPLE:
sprintf( pSymbolName, "T_%04X", nStartAddress ); // ASC range
break;
case NOP_WORD_1:
case NOP_WORD_2:
case NOP_WORD_4:
sprintf( pSymbolName, "W_%04X", nStartAddress ); // DW range
break;
case NOP_BYTE_1:
case NOP_BYTE_2:
case NOP_BYTE_4:
case NOP_BYTE_8:
default:
sprintf( pSymbolName, "B_%04X", nStartAddress ); // DB range
break;
}
}
// @param tData_ Filled out with range data
//===========================================================================
WORD _GetDataRange (int nArgs, int iArg, DisasmData_t& tData_)
{
WORD nAddress = 0;
WORD nAddress2 = 0;
WORD nEnd = 0;
int nLen = 0;
memset( (void*) &tData_, 0, sizeof(tData_) );
// DB address
// DB symbol
// bool bisAddress ...
if (nArgs < 1)
{
nAddress = g_nDisasmCurAddress;
}
else
{
// DB foo = 300 // nArgs == 3
RangeType_t eRange = Range_Get( nAddress, nAddress2, iArg);
if ((eRange == RANGE_HAS_END) ||
(eRange == RANGE_HAS_LEN))
{
Range_CalcEndLen( eRange, nAddress, nAddress2, nEnd, nLen );
nLen--; // Disassembly_IsDataAddress() is *inclusive* // KEEP IN SYNC: _CmdDefineByteRange() CmdDisasmDataList() _6502_GetOpmodeOpbyte() FormatNopcodeBytes()
}
else
{
if( nArgs > 1 )
nAddress = g_aArgs[ 2 ].nValue;
else
nAddress = g_aArgs[ 1 ].nValue;
}
}
// 2.7.0.35 DW address -- round the length up to even number for convenience.
// Example: 'DW 6062' is equivalent to: 'DW 6062:6063'
if( g_iCommand == CMD_DEFINE_DATA_WORD1 )
{
if( ~nLen & 1 )
nLen++;
}
tData_.nStartAddress = nAddress;
tData_.nEndAddress = nAddress + nLen;
return nAddress;
}
//===========================================================================
WORD _CmdDefineByteRange(int nArgs,int iArg,DisasmData_t & tData_)
{
WORD nAddress = 0;
WORD nAddress2 = 0;
WORD nEnd = 0;
int nLen = 0;
nAddress = _GetDataRange(nArgs,iArg,tData_);
const char *pSymbolName = "";
char aSymbolName[ MAX_SYMBOLS_LEN+1 ];
SymbolTable_Index_e eSymbolTable = SYMBOLS_ASSEMBLY;
bool bAutoDefineName = false; // 2.7.0.34
if( nArgs > 1 )
{
if( g_aArgs[ 2 ].eToken == TOKEN_COLON ) // 2.7.0.31 Bug fix: DB range, i.e. DB 174E:174F
{
bAutoDefineName = true;
}
else
{
g_aArgs[ 1 ].sArg[MAX_SYMBOLS_LEN] = 0; // truncate to max symbol length
pSymbolName = g_aArgs[1].sArg;
}
}
else
{
bAutoDefineName = true;
}
// 2.7.0.34 Unified auto-defined name: B_, W_, T_ for byte, word, or text respectively
// Old name: auto define D_# DB $XX
// Example 'DB' or 'DW' with 1 arg
// DB 801
if( bAutoDefineName )
{
Nopcode_e nopcode = NOP_BYTE_1;
bool isString = (g_iCommand == CMD_DEFINE_DATA_STR);
if( isString )
nopcode = NOP_STRING_ASCII;
bool isWord1 = (g_iCommand == CMD_DEFINE_DATA_WORD1);
if( isWord1 )
nopcode = NOP_WORD_1;
bool isAddr = (g_iCommand == CMD_DEFINE_ADDR_WORD);
if( isAddr )
nopcode = NOP_ADDRESS;
_GetAutoSymbolName( nopcode, tData_.nStartAddress , aSymbolName );
pSymbolName = aSymbolName;
}
// bRemoveSymbol = false // use arg[2]
// bUpdateSymbol = true // add the symbol to the table
SymbolUpdate( eSymbolTable, pSymbolName, nAddress, false, true );
// TODO: Note: need to call ConsoleUpdate(), as may print symbol has been updated
strcpy_s( tData_.sSymbol, sizeof(tData_.sSymbol), pSymbolName );
return nAddress;
}
// Undefine Data
//
// X
// X addr
// X addr:addr
//
// Example:
// DB hgr 2000:3FFF
// U 1FFF
// X 1FFF:2001
//===========================================================================
Update_t CmdDisasmDataDefCode (int nArgs)
{
// treat memory (bytes) as code
if (! ((nArgs <= 2) || (nArgs == 4)) && (nArgs != 3))
{
return Help_Arg_1( CMD_DISASM_CODE );
}
DisasmData_t tData; // X 1FFF:2001
WORD nAddress = 0; // 8191
int iArg = 0;
if (nArgs == 3)
{
iArg = 1; // #### : ####
nAddress = _GetDataRange( nArgs, iArg, tData );
}
else
{
iArg = 2;
nAddress = _GetDataRange( nArgs, iArg, tData );
}
// Need to iterate through all blocks
// DB TEST1 300:320
// DB TEST2 310:330
// DB TEST3 320:340
// X TEST1
/*
// Edge cases:
U 1FFF
DB 2000:2005
X 1FFF:2001 // Chop 2 head
X 2004:2006 // Chop 2 tail
X 1FFF:2006 // Chop entire
U 1FFF
DB 2000:2000
X 2000:2000 // Chop entire
U 1FFF
DB 2000:2002
X 2001:2001 // Chop middle
U 1FFF
DB 2000:2005
X 2002:2003 // Chop middle
*/
while (nAddress <= tData.nEndAddress)
{
DisasmData_t *pData = Disassembly_IsDataAddress( nAddress );
if( pData )
{
if (( nAddress <= pData->nStartAddress)
&& (tData.nEndAddress >= pData->nEndAddress ))
{
// remove entire
Disassembly_DelData( *pData );
}
else
if (( nAddress <= pData->nStartAddress)
&& (tData.nStartAddress < pData->nEndAddress ))
{
// head
pData->nStartAddress = nAddress+1;
}
else
if (( nAddress > pData->nStartAddress)
&& (tData.nEndAddress >= pData->nEndAddress ))
{
// tail
pData->nEndAddress = nAddress-1;
}
else
{
// middle
SymbolTable_Index_e eSymbolTable = SYMBOLS_ASSEMBLY;
DisasmData_t tSplit = *pData;
pData->nEndAddress = nAddress - 1;
const char *pSymbolName = tSplit.sSymbol;
tSplit.nStartAddress = tData.nEndAddress + 1; // nAddress + 1;
_GetAutoSymbolName( pData->eElementType, tSplit.nStartAddress, tSplit.sSymbol );
SymbolUpdate( eSymbolTable, pSymbolName, tSplit.nStartAddress, false, true );
Disassembly_AddData( tSplit );
}
// TODO: Remove symbol 'D_FA62' from symbol table!
}
nAddress++;
}
return UPDATE_DISASM | ConsoleUpdate();
}
const char* g_aNopcodeTypes[ NUM_NOPCODE_TYPES ] =
{
"-n/a-"
,"byte1"
,"byte2"
,"byte4"
,"byte8"
,"word1"
,"word2"
,"word4"
,"addr "
,"hex "
,"char "
,"ascii"
,"apple"
,"mixed"
,"FAC "
,"bmp "
};
// Command: B
// no args
// List the data blocks
//===========================================================================
Update_t CmdDisasmDataList (int nArgs)
{
// Need to iterate through all blocks
DisasmData_t* pData = NULL;
while( (pData = Disassembly_Enumerate( pData )) )
{
if (pData->iDirective != _NOP_REMOVED)
{
int nLen = strlen( pData->sSymbol );
char sText[CONSOLE_WIDTH * 2];
// <smbol> <type> <start>:<end>
// `TEST `300`:`320
ConsolePrintFormat( sText, "%s%s %s%*s %s%04X%s:%s%04X"
, CHC_CATEGORY
, g_aNopcodeTypes[ pData->eElementType ]
, (nLen > 0) ? CHC_SYMBOL : CHC_DEFAULT
, MAX_SYMBOLS_LEN
, (nLen > 0) ? pData->sSymbol : "???"
, CHC_ADDRESS
, pData->nStartAddress
, CHC_ARG_SEP
, CHC_ADDRESS
, pData->nEndAddress // Disassembly_IsDataAddress() is *inclusive* // KEEP IN SYNC: _CmdDefineByteRange() CmdDisasmDataList() _6502_GetOpmodeOpbyte() FormatNopcodeBytes()
);
}
}
return UPDATE_DISASM | ConsoleUpdate();
}
// Common code
//===========================================================================
Update_t _CmdDisasmDataDefByteX (int nArgs)
{
// DB
// DB symbol // use current instruction pointer
// DB symbol address
// DB symbol range:range
// DB address
// To "return to code" use ."X"
int iCmd = g_aArgs[0].nValue - NOP_BYTE_1;
if (nArgs > 4) // 2.7.0.31 Bug fix: DB range, i.e. DB 174E:174F
{
return Help_Arg_1( CMD_DEFINE_DATA_BYTE1 + iCmd );
}
DisasmData_t tData;
int iArg = 2;
if (nArgs == 3 ) // 2.7.0.31 Bug fix: DB range, i.e. DB 174E:175F
{
if ( g_aArgs[ 2 ].eToken == TOKEN_COLON )
iArg = 1;
}
WORD nAddress = _CmdDefineByteRange( nArgs, iArg, tData );
// TODO: Allow user to select which assembler to use for displaying directives!
// tData.iDirective = FIRST_M_DIRECTIVE + ASM_M_DEFINE_BYTE;
tData.iDirective = g_aAssemblerFirstDirective[ g_iAssemblerSyntax ] + ASM_DEFINE_BYTE;
tData.eElementType = (Nopcode_e)( NOP_BYTE_1 + iCmd );
tData.bSymbolLookup = false;
tData.nTargetAddress = 0;
// Already exists, so update
DisasmData_t *pData = Disassembly_IsDataAddress( nAddress );
if( pData )
{
*pData = tData;
}
else
Disassembly_AddData( tData );
return UPDATE_DISASM | ConsoleUpdate();
}
/*
Usage:
DW
DW symbol
DW symbol address
DW symbol range:range
DW address Auto-define W_#### where # is the address
DW range Auto-define W_#### where # is the address
Examples:
DW 3F2:3F3
*/
//===========================================================================
Update_t _CmdDisasmDataDefWordX (int nArgs)
{
int iCmd = g_aArgs[0].nValue - NOP_WORD_1;
if (nArgs > 4) // 2.7.0.31 Bug fix: DB range, i.e. DB 174E:174F
{
return Help_Arg_1( CMD_DEFINE_DATA_WORD1 + iCmd );
}
DisasmData_t tData;
int iArg = 2;
if (nArgs == 3 ) // 2.7.0.33 Bug fix: DW range, i.e. DW 3F2:3F3
{
if ( g_aArgs[ 2 ].eToken == TOKEN_COLON )
iArg = 1;
}
WORD nAddress = _CmdDefineByteRange( nArgs, iArg, tData );
// tData.iDirective = FIRST_M_DIRECTIVE + ASM_M_DEFINE_WORD;
tData.iDirective = g_aAssemblerFirstDirective[ g_iAssemblerSyntax ] + ASM_DEFINE_WORD;
tData.eElementType = (Nopcode_e)( NOP_WORD_1 + iCmd );
tData.bSymbolLookup = false;
tData.nTargetAddress = 0;
// Already exists, so update
DisasmData_t *pData = Disassembly_IsDataAddress( nAddress );
if( pData )
{
*pData = tData;
}
else
Disassembly_AddData( tData );
return UPDATE_DISASM | ConsoleUpdate();
}
//===========================================================================
Update_t CmdDisasmDataDefAddress8H (int nArgs)
{
return UPDATE_DISASM;
}
//===========================================================================
Update_t CmdDisasmDataDefAddress8L (int nArgs)
{
return UPDATE_DISASM;
}
//===========================================================================
Update_t CmdDisasmDataDefAddress16 (int nArgs)
{
int iCmd = NOP_WORD_1 - g_aArgs[0].nValue;
if (! ((nArgs <= 2) || (nArgs == 4)))
{
return Help_Arg_1( CMD_DEFINE_DATA_WORD1 + iCmd );
}
DisasmData_t tData;
int iArg = 2;
WORD nAddress = _CmdDefineByteRange( nArgs, iArg, tData );
// tData.iDirective = FIRST_M_DIRECTIVE + ASM_M_DEFINE_WORD;
tData.iDirective = g_aAssemblerFirstDirective[ g_iAssemblerSyntax ] + ASM_DEFINE_ADDRESS_16;
tData.eElementType = NOP_ADDRESS;
tData.bSymbolLookup = true;
tData.nTargetAddress = 0; // dynamic -- will be filled in ...
// Already exists, so update
DisasmData_t *pData = Disassembly_IsDataAddress( nAddress );
if( pData )
{
*pData = tData;
}
else
Disassembly_AddData( tData );
return UPDATE_DISASM | ConsoleUpdate();
}
// DB
Update_t CmdDisasmDataDefByte1 ( int nArgs )
{
g_aArgs[0].nValue = NOP_BYTE_1;
return _CmdDisasmDataDefByteX( nArgs );
}
// DB2
Update_t CmdDisasmDataDefByte2 ( int nArgs )
{
g_aArgs[0].nValue = NOP_BYTE_2;
return _CmdDisasmDataDefByteX( nArgs );
}
Update_t CmdDisasmDataDefByte4 ( int nArgs )
{
g_aArgs[0].nValue = NOP_BYTE_4;
return _CmdDisasmDataDefByteX( nArgs );
}
Update_t CmdDisasmDataDefByte8 ( int nArgs )
{
g_aArgs[0].nValue = NOP_BYTE_8;
return _CmdDisasmDataDefByteX( nArgs );
}
// DW
Update_t CmdDisasmDataDefWord1 ( int nArgs )
{
g_aArgs[0].nValue = NOP_WORD_1;
return _CmdDisasmDataDefWordX( nArgs );
}
// DW2
Update_t CmdDisasmDataDefWord2 ( int nArgs )
{
g_aArgs[0].nValue = NOP_WORD_2;
return _CmdDisasmDataDefWordX( nArgs );
}
Update_t CmdDisasmDataDefWord4 ( int nArgs )
{
g_aArgs[0].nValue = NOP_WORD_4;
return _CmdDisasmDataDefWordX( nArgs );
}
// Command: DS
// ASC range Auto-define T_#### where # is the address
Update_t CmdDisasmDataDefString ( int nArgs )
{
int iCmd = 0; // Define Ascii, AppleText, MixedText (DOS3.3)
if (nArgs > 4) // 2.7.0.31 Bug fix: DB range, i.e. DB 174E:174F
{
return Help_Arg_1( CMD_DEFINE_DATA_STR + iCmd );
}
DisasmData_t tData;
int iArg = 2;
if (nArgs == 3 ) // 2.7.0.32 Bug fix: ASC range, i.e. ASC 174E:175F
{
if ( g_aArgs[ 2 ].eToken == TOKEN_COLON )
iArg = 1;
}
WORD nAddress = _CmdDefineByteRange( nArgs, iArg, tData );
// tData.iDirective = g_aAssemblerFirstDirective[ g_iAssemblerSyntax ] + ASM_DEFINE_APPLE_TEXT;
tData.iDirective = FIRST_M_DIRECTIVE + ASM_M_ASCII; // ASM_MERLIN
tData.eElementType = (Nopcode_e)( NOP_STRING_APPLE + iCmd );
tData.bSymbolLookup = false;
tData.nTargetAddress = 0;
// Already exists, so update
DisasmData_t *pData = Disassembly_IsDataAddress( nAddress );
if( pData )
{
*pData = tData;
}
else
Disassembly_AddData( tData );
return UPDATE_DISASM | ConsoleUpdate();
}
// __ Disassembler View Interface ____________________________________________________________________
/// @param pCurrent NULL start a new serch, or continue enumerating
//===========================================================================
DisasmData_t* Disassembly_Enumerate( DisasmData_t *pCurrent )
{
DisasmData_t *pData = NULL; // bIsNopcode = false
int nDataTargets = g_aDisassemblerData.size();
if( nDataTargets )
{
DisasmData_t *pBegin = & g_aDisassemblerData[ 0 ];
DisasmData_t *pEnd = & g_aDisassemblerData[ nDataTargets - 1 ];
if( pCurrent )
{
pCurrent++;
if (pCurrent <= pEnd)
pData = pCurrent;
} else {
pData = pBegin;
}
}
return pData;
}
// returns NULL if address has no data associated with it
//===========================================================================
DisasmData_t* Disassembly_IsDataAddress ( WORD nAddress )
{
DisasmData_t *pData = NULL; // bIsNopcode = false
int nDataTargets = g_aDisassemblerData.size();
if( nDataTargets )
{
// TODO: Replace with binary search -- should store data in sorted order, via start address
pData = & g_aDisassemblerData[ 0 ];
for( int iTarget = 0; iTarget < nDataTargets; iTarget++ )
{
if( pData->iDirective != _NOP_REMOVED )
{
if( (nAddress >= pData->nStartAddress) && (nAddress <= pData->nEndAddress) )
{
return pData;
}
}
pData++;
}
pData = NULL; // bIsNopCode = false
}
return pData;
}
// Notes: tData.iDirective should not be _NOP_REMOVED !
//===========================================================================
void Disassembly_AddData( DisasmData_t tData)
{
g_aDisassemblerData.push_back( tData );
}
// DEPRECATED ! Inlined in _6502_GetOpmodeOpbyte() !
//===========================================================================
void Disassembly_GetData ( WORD nBaseAddress, const DisasmData_t *pData, DisasmLine_t & line_ )
{
if( !pData )
{
#if _DEBUG
ConsoleDisplayError( "Disassembly_GetData() but we don't have a valid DisasmData_t *" );
#endif
return;
}
}
//===========================================================================
void Disassembly_DelData( DisasmData_t tData)
{
// g_aDisassemblerData.erase( );
WORD nAddress = tData.nStartAddress;
DisasmData_t *pData = NULL; // bIsNopcode = false
int nDataTargets = g_aDisassemblerData.size();
if( nDataTargets )
{
// TODO: Replace with binary search -- should store data in sorted order, via start address
pData = & g_aDisassemblerData[ 0 ];
for( int iTarget = 0; iTarget < nDataTargets; iTarget++ )
{
if (pData->iDirective != _NOP_REMOVED)
{
if ((nAddress >= pData->nStartAddress) && (nAddress < pData->nEndAddress))
{
pData->iDirective = _NOP_REMOVED;
}
}
pData++;
}
pData = NULL; // bIsNopCode = false
}
}