-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deasm6502View.cpp
643 lines (544 loc) · 16 KB
/
Deasm6502View.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
/*-----------------------------------------------------------------------------
6502 Macroassembler and Simulator
Copyright (C) 1995-2003 Michal Kowalski
This program 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 program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-----------------------------------------------------------------------------*/
// Deasm6502View.cpp : implementation file
//
#include "stdafx.h"
//#include "6502.h"
#include "Deasm6502View.h"
#include "Deasm6502Doc.h"
#include "resource.h"
#include "DeasmGoto.h"
#include "Deasm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CFont CDeasm6502View::m_Font;
LOGFONT CDeasm6502View::m_LogFont;
COLORREF CDeasm6502View::m_rgbBkgnd;
COLORREF CDeasm6502View::m_rgbAddress= RGB(127,127,127);
COLORREF CDeasm6502View::m_rgbCode= RGB(191,191,191);
COLORREF CDeasm6502View::m_rgbInstr= RGB(0,0,0);
bool CDeasm6502View::m_bDrawCode= TRUE;
/////////////////////////////////////////////////////////////////////////////
// CDeasm6502View
IMPLEMENT_DYNCREATE(CDeasm6502View, CView)
CDeasm6502View::CDeasm6502View()
{
m_nFontHeight = 0;
m_nFontWidth = 0;
}
CDeasm6502View::~CDeasm6502View()
{
}
BEGIN_MESSAGE_MAP(CDeasm6502View, CView)
ON_WM_CONTEXTMENU()
ON_WM_VSCROLL()
ON_WM_MOUSEWHEEL() // 1.3.2 added mouse scroll wheel support
ON_WM_KEYDOWN()
ON_WM_ERASEBKGND()
ON_COMMAND(ID_DEASM_GOTO, OnDeasmGoto)
ON_UPDATE_COMMAND_UI(ID_DEASM_GOTO, OnUpdateDeasmGoto)
ON_WM_CONTEXTMENU()
ON_WM_SIZE()
ON_MESSAGE(CBroadcast::WM_USER_EXIT_DEBUGGER, OnExitDebugger)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDeasm6502View drawing
int CDeasm6502View::no_of_lines(RECT &rect) // obl. iloœci wierszy w oknie
{
if (m_nFontHeight == 0)
return 1; // return 1 row
int h= rect.bottom - rect.top;
if (h >= m_nFontHeight)
return h / m_nFontHeight; // no of rows in window
else
return 1; // always at least one row
}
void CDeasm6502View::OnDraw(CDC* pDC) // deasemblowany program - wyœwietlanie instrukcji
{
CDeasm6502Doc *pDoc = (CDeasm6502Doc*)GetDocument();
if (pDoc == NULL)
return;
RECT rect;
GetClientRect(&rect);
int lines= no_of_lines(rect);
rect.bottom = rect.top + m_nFontHeight;
RECT mark= rect; // space for indicators
rect.left += m_nFontHeight; // left margin
if (rect.left >= rect.right)
rect.right = rect.left;
CDeasm deasm;
INT32 ptr= pDoc->m_uStartAddr;
INT32 ptr1 = ptr;
for (int i=0; i<=lines; i++)
{
if (ptr < ptr1) break;
if (pDC->RectVisible(&mark)) // pointer fields to refresh?
{
Breakpoint bp= theApp.m_global.GetBreakpoint(UINT16(ptr));
if (bp & BPT_MASK) // jest miejsce przerwania?
draw_breakpoint(*pDC,mark.left,mark.top,m_nFontHeight,bp & BPT_DISABLED ? FALSE : TRUE);
if (pDoc->m_nPointerAddr == ptr) // w tym wierszu strza³ka?
draw_pointer(*pDC,mark.left,mark.top,m_nFontHeight);
}
ptr1 = ptr;
const CString &str= deasm.DeasmInstr(*pDoc->m_pCtx,CDeasm::DeasmFmt(CDeasm::DF_ADDRESS|CDeasm::DF_CODE_BYTES),ptr);
if (pDC->RectVisible(&rect)) // wiersz instrukcji do odœwie¿enia?
{
pDC->SetTextColor(m_rgbAddress);
pDC->TextOut(rect.left,rect.top,LPCTSTR(str),6); //65816
if (m_bDrawCode)
{
pDC->SetTextColor(m_rgbCode);
pDC->TextOut(rect.left+m_nFontWidth*6,rect.top,LPCTSTR(str)+6,14); //65816
}
pDC->SetTextColor(m_rgbInstr);
pDC->TextOut(rect.left+m_nFontWidth*19,rect.top,LPCTSTR(str)+19,str.GetLength()-19); //65816
}
rect.top += m_nFontHeight;
rect.bottom += m_nFontHeight;
mark.top += m_nFontHeight;
mark.bottom += m_nFontHeight;
}
}
/////////////////////////////////////////////////////////////////////////////
// CDeasm6502View diagnostics
#ifdef _DEBUG
void CDeasm6502View::AssertValid() const
{
CView::AssertValid();
}
void CDeasm6502View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDeasm6502View message handlers
void CDeasm6502View::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
if (pInfo || pDC->IsPrinting())
return;
pDC->SetBkMode(OPAQUE);
pDC->SelectObject(&m_Font);
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
m_nFontHeight = (int)tm.tmHeight + (int)tm.tmExternalLeading;
m_nFontWidth = tm.tmAveCharWidth;
pDC->SetBkColor(m_rgbBkgnd);
CView::OnPrepareDC(pDC, pInfo);
}
BOOL CDeasm6502View::PreCreateWindow(CREATESTRUCT& cs)
{
bool ret= CView::PreCreateWindow(cs);
cs.style |= WS_VSCROLL;
return ret;
}
//=============================================================================
void CDeasm6502View::ScrollToLine(UINT32 addr)
{
CDeasm6502Doc *pDoc= (CDeasm6502Doc *)GetDocument();
if (pDoc == NULL)
return;
if (pDoc->m_uStartAddr == addr) // ¿¹dany adres jest aktualnym pocz¹tkiem?
return;
if (pDoc->m_uStartAddr > addr) // move up
{
RECT rect;
GetClientRect(&rect);
int lines= no_of_lines(rect);
CDeasm deasm;
UINT32 start= addr; // start at addr
bool redraw= TRUE;
for (int i=0; i<lines; i++) // idziemy od 'addr' w dó³, a¿ spotkamy 'm_uStartAddr'
{ // lub skoñczy siê iloœæ dostêpnych wierszy
if (deasm.FindNextAddr(start,*pDoc->m_pCtx) == 0)
break; // "przewiniêcie siê" adresu
if (start > pDoc->m_uStartAddr)
break; // rozkazy "nie trafiaj¹" w siebie, przerysowujemy wszystko
if (start == pDoc->m_uStartAddr)
{
RECT rect;
get_view_rect(rect);
int y= (i+1) * m_nFontHeight;
UpdateWindow(); // dla unikniêcia problemów z odœwie¿aniem
pDoc->m_uStartAddr = addr;
ScrollWindow(0,y,&rect,&rect);
UpdateWindow();
redraw = FALSE;
break;
}
}
if (redraw)
{
pDoc->m_uStartAddr = addr;
InvalidateRect(NULL);
UpdateWindow();
}
}
else // move down
{
RECT rect;
GetClientRect(&rect);
int lines= no_of_lines(rect);
CDeasm deasm;
UINT32 start= pDoc->m_uStartAddr;
bool redraw= TRUE;
int i;
for (i= 1; i<lines; i++) // idziemy od 'm_uStartAddr' w dó³, a¿ spotkamy 'addr'
{ // lub skoñczy siê iloœæ dostêpnych wierszy
if (deasm.FindNextAddr(start,*pDoc->m_pCtx) == 0)
break; // "przewiniêcie siê" adresu
if (start > addr)
break; // rozkazy "nie trafiaj¹" w siebie, przerysowujemy wszystko
if (start == addr)
return; // strza³ka mieœci siê w oknie
}
if (i==lines) // poprzednia pêtla zakoñczona normalnie (nie przez 'break') ?
{
for (int i=0; i<lines; i++) // idziemy od 'start' w dó³, a¿ spotkamy 'addr'
{ // lub skoñczy siê iloœæ dostêpnych wierszy
if (deasm.FindNextAddr(start,*pDoc->m_pCtx) == 0)
break; // "przewiniêcie siê" adresu
if (start > addr)
break; // rozkazy "nie trafiaj¹" w siebie, przerysowujemy wszystko
if (start == addr)
{
RECT rect;
get_view_rect(rect);
int y= (i+1) * m_nFontHeight;
UpdateWindow(); // dla unikniêcia problemów z odœwie¿aniem
for (int j=0; j<=i; j++) // wyznaczenie nowego adresu pocz¹tku okna
deasm.FindNextAddr(pDoc->m_uStartAddr,*pDoc->m_pCtx);
ScrollWindow(0,-y,&rect,&rect);
UpdateWindow();
redraw = FALSE;
break;
}
}
}
if (redraw)
{
pDoc->m_uStartAddr = addr;
InvalidateRect(NULL);
UpdateWindow();
}
}
return;
}
BOOL CDeasm6502View::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll)
{
if (bDoScroll)
return TRUE;
return CView::OnScroll(nScrollCode, nPos, bDoScroll);
}
BOOL CDeasm6502View::OnScrollBy(CSize sizeScroll, BOOL bDoScroll)
{
return CView::OnScrollBy(sizeScroll, bDoScroll);
}
void CDeasm6502View::OnInitialUpdate()
{
CView::OnInitialUpdate();
// SetScrollRange(SB_VERT,-0x8000,0x7FFF);
set_scroll_range();
CDeasm6502Doc *pDoc = (CDeasm6502Doc*)GetDocument();
if (pDoc)
SetScrollPos(SB_VERT,(int)pDoc->m_uStartAddr-0x8000); //65816 fix
m_Font.DeleteObject();
m_Font.CreateFontIndirect(&m_LogFont);
}
//-----------------------------------------------------------------------------
// przesuniêcie zawartoœci okna deasemblacji
//
void CDeasm6502View::scroll(UINT nSBCode, int nPos, int nRepeat)
{
CDeasm6502Doc *pDoc = (CDeasm6502Doc*)GetDocument();
if (pDoc == NULL)
return;
CDeasm deasm;
switch (nSBCode)
{
case SB_ENDSCROLL: // End scroll
break;
case SB_LINEDOWN: // Scroll one line down
switch (deasm.FindNextAddr(pDoc->m_uStartAddr, *pDoc->m_pCtx))
{
case 0:
break; // dalej ju¿ siê nie da
case 1:
RECT rect;
GetClientRect(&rect);
ScrollWindow(0, -m_nFontHeight, &rect, &rect);
UpdateWindow();
break;
}
break;
case SB_LINEUP: // Scroll one line up
switch (deasm.FindPrevAddr(pDoc->m_uStartAddr, *pDoc->m_pCtx))
{
case 0:
break; // jesteœmy ju¿ na pocz¹tku
case 1:
RECT rect;
GetClientRect(&rect);
ScrollWindow(0,m_nFontHeight, &rect, &rect);
UpdateWindow();
break;
case -1:
InvalidateRect(NULL); // przerysowanie ca³ego okna - zmieni³o siê kilka rozkazów
break;
default:
ASSERT(FALSE);
break;
}
break;
case SB_PAGEDOWN: // Scroll one page down
{
RECT rect;
get_view_rect(rect);
switch (deasm.FindNextAddr(pDoc->m_uStartAddr, *pDoc->m_pCtx, no_of_lines(rect)))
{
case 0:
break; // dalej ju¿ siê nie da
case 1:
InvalidateRect(NULL); // przerysowanie ca³ego okna
break;
}
break;
}
case SB_PAGEUP: // Scroll one page up
{
RECT rect;
get_view_rect(rect);
switch (deasm.FindPrevAddr(pDoc->m_uStartAddr, *pDoc->m_pCtx, no_of_lines(rect)))
{
case 0:
break; // jesteœmy ju¿ na pocz¹tku
case 1:
case -1:
InvalidateRect(NULL); // przerysowanie ca³ego okna - zmieni³o siê kilka rozkazów
break;
default:
ASSERT(FALSE);
break;
}
break;
}
case SB_TOP: // Scroll to top
{
RECT rect;
get_view_rect(rect);
int dy= no_of_lines(rect); // iloϾ wierszy w oknie
int lines;
lines = deasm.FindDelta(pDoc->m_uStartAddr,0, *pDoc->m_pCtx, dy);
if (lines < 0)
InvalidateRect(NULL); // przerysowanie ca³ego okna
else if (lines > 0)
{
if (lines >= dy)
InvalidateRect(NULL); // przerysowanie ca³ego okna
else
ScrollWindow(0,lines * m_nFontHeight,&rect,&rect);
}
break;
}
case SB_BOTTOM: // Scroll to bottom
{
RECT rect;
get_view_rect(rect);
int dy= no_of_lines(rect); // iloϾ wierszy w oknie
int lines;
if (theApp.m_global.m_bProc6502==2)
lines = deasm.FindDelta(pDoc->m_uStartAddr, 0xFFFFF0, *pDoc->m_pCtx, dy);
else
lines = deasm.FindDelta(pDoc->m_uStartAddr, 0xFFF0, *pDoc->m_pCtx, dy);
if (lines < 0)
InvalidateRect(NULL); // przerysowanie ca³ego okna
else if (lines > 0)
{
if (lines >= dy)
InvalidateRect(NULL); // przerysowanie ca³ego okna
else
ScrollWindow(0, -lines * m_nFontHeight, &rect, &rect);
}
break;
}
case SB_THUMBPOSITION: // Scroll to the absolute position. The current position is provided in nPos
break;
case SB_THUMBTRACK: // Drag scroll box to specified position. The current position is provided in nPos
{
RECT rect;
get_view_rect(rect);
int dy= no_of_lines(rect); // iloϾ wierszy w oknie
int lines;
int maxmem = 0xffff;
if (theApp.m_global.m_bProc6502==2) maxmem = 0xffffff;
SCROLLINFO si; // 1.3.3 added si structure to get 32 bit resolution on the scrollbar
ZeroMemory(&si, sizeof(si));
si.cbSize = sizeof si;
si.fMask = SIF_TRACKPOS;
if (!(GetScrollInfo(SB_VERT, &si)))
break;
int pos = (si.nTrackPos);
if (nRepeat==2) pos = (nPos); // 1.3.3 for goto memory cmd use passed value
lines = deasm.FindDelta(pDoc->m_uStartAddr,pos, *pDoc->m_pCtx, dy);
if (lines !=0) InvalidateRect(NULL); // przerysowanie ca³ego okna
break;
}
default:
break;
}
SetScrollPos(SB_VERT,(int)pDoc->m_uStartAddr);
}
void CDeasm6502View::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
scroll(nSBCode, nPos);
}
BOOL CDeasm6502View::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) // 1.3.2 addes Mouse scroll wheel support
{
if (zDelta > 0)
scroll(SB_LINEUP,0,0);
else
scroll(SB_LINEDOWN,0,0);
return true;
}
void CDeasm6502View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch (nChar)
{
case VK_DOWN:
scroll(SB_LINEDOWN,0,nRepCnt);
break;
case VK_UP:
scroll(SB_LINEUP,0,nRepCnt);
break;
case VK_NEXT:
scroll(SB_PAGEDOWN,0,nRepCnt);
break;
case VK_PRIOR:
scroll(SB_PAGEUP,0,nRepCnt);
break;
case VK_HOME:
scroll(SB_TOP,0,nRepCnt);
break;
case VK_END:
scroll(SB_BOTTOM,0,nRepCnt);
break;
default:
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
}
//=============================================================================
void CDeasm6502View::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
switch (LOWORD(lHint))
{
case 1: // przerysowaæ wskaŸniki?
{
RECT rect;
GetClientRect(&rect);
rect.right = m_nFontHeight; // wielkoϾ lewego marginesu
InvalidateRect(&rect);
UpdateWindow();
break;
}
case 2: // przesun¹æ zawartoœæ okna?
if(theApp.m_global.m_bBank)
ScrollToLine(HIWORD(lHint)+(theApp.m_global.m_bPBR<<16));
else
ScrollToLine(UINT16(HIWORD(lHint)));
break;
case 0: // przerysowaæ zawartoœæ okna?
InvalidateRect(NULL);
break;
}
}
//=============================================================================
afx_msg LRESULT CDeasm6502View::OnExitDebugger(WPARAM /* wParam */, LPARAM /* lParam */)
{
GetDocument()->OnCloseDocument();
return 0;
}
//-----------------------------------------------------------------------------
BOOL CDeasm6502View::OnEraseBkgnd(CDC* pDC)
{
RECT rect;
GetClientRect(&rect);
pDC->FillSolidRect(&rect,m_rgbBkgnd);
return TRUE;
// return CView::OnEraseBkgnd(pDC);
}
//-----------------------------------------------------------------------------
void CDeasm6502View::OnDeasmGoto()
{
static UINT32 addr= 0;
CDeasmGoto dlg;
dlg.m_uAddress = addr;
if (dlg.DoModal() == IDOK)
{
addr = dlg.m_uAddress;
scroll(SB_THUMBTRACK,addr,2);
}
}
void CDeasm6502View::OnUpdateDeasmGoto(CCmdUI* pCmdUI)
{
pCmdUI->Enable(true);
}
//-----------------------------------------------------------------------------
// Popup menu
void CDeasm6502View::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu menu;
VERIFY(menu.LoadMenu(IDR_POPUP_DEASM6502));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
if (point.x == -1 && point.y == -1) // menu wywo³ane przy pomocy klawiatury?
{
CRect rect;
GetClientRect(rect);
point = rect.TopLeft();
CPoint ptTopLeft(0, 0);
ClientToScreen(&ptTopLeft);
point.x = ptTopLeft.x + rect.Width() / 2; // ustawiamy siê na œrodku okna
point.y = ptTopLeft.y + rect.Height() / 2;
}
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd());
}
void CDeasm6502View::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
set_scroll_range();
}
void CDeasm6502View::set_scroll_range() //65816 fix
{
RECT rect;
GetClientRect(&rect);
int lines= no_of_lines(rect);
SCROLLINFO si;
si.cbSize = sizeof si;
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
if (theApp.m_global.m_bProc6502==2) {
si.nMin = 0x0;
si.nMax = 0xffffff;
} else {
si.nMin = 0x0;
si.nMax = 0xffff;
}
si.nPage = lines; // estimate only
SetScrollInfo(SB_VERT, &si, FALSE);
}