Repository navigation
Expand file tree
/
Copy pathMemory.cpp
More file actions
169 lines (166 loc) · 5.9 KB
/
Copy pathMemory.cpp
File metadata and controls
169 lines (166 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//---------------------------------------------------------------------------
#include <vcl.h>
#include <string.h>
#pragma hdrstop
#include "Memory.h"
#include "Xbdm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMemoryForm *MemoryForm;
// The 256 bytes on show, the bytes from the read before, and one flag per byte: Changed (differs from the
// read before; framed red) and Edited (typed into the grid and not yet written; framed green).
static unsigned char MemBuf[0x100];
static unsigned char PrevBuf[0x100];
static unsigned char Changed[0x100];
static unsigned char Edited[0x100];
//---------------------------------------------------------------------------
__fastcall TMemoryForm::TMemoryForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
// The address box holds hex digits without a prefix.
unsigned int TMemoryForm::GetAddress()
{
return (unsigned int)(L"0x" + AddressEdit->Text).ToInt();
}
//---------------------------------------------------------------------------
void TMemoryForm::ShowBytes()
{
for (int row = 0; row < 16; row++)
for (int col = 0; col < 16; col++)
MemoryGrid->Cells[col + 1][row + 1] = UnicodeString().sprintf(L"%02X", MemBuf[row * 16 + col]);
}
//---------------------------------------------------------------------------
// Also called after every read to redraw the headers and the bytes.
void __fastcall TMemoryForm::FormCreate(TObject *Sender)
{
const unsigned int addr = GetAddress();
MemoryGrid->ColWidths[0] = 60;
for (int i = 0; i < 16; i++)
{
MemoryGrid->Cells[i + 1][0] = UnicodeString().sprintf(L"%02X", i);
MemoryGrid->Cells[0][i + 1] = UnicodeString().sprintf(L"%08X", addr + 16 * i);
}
ShowBytes();
}
//---------------------------------------------------------------------------
// Enter in the address box (and, as in the original, in the converter box) reads memory.
void __fastcall TMemoryForm::AddressEditKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
if (Key == VK_RETURN)
ReadButtonClick(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TMemoryForm::MemoryGridDrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
TGridDrawState State)
{
if (ACol == 0 || ARow == 0)
return;
const int i = (ARow - 1) * 16 + (ACol - 1);
if (Changed[i])
{
MemoryGrid->Canvas->Brush->Color = clRed;
MemoryGrid->Canvas->FrameRect(Rect);
}
if (Edited[i])
{
MemoryGrid->Canvas->Brush->Color = clGreen;
MemoryGrid->Canvas->FrameRect(Rect);
}
}
//---------------------------------------------------------------------------
void __fastcall TMemoryForm::MemoryGridSetEditText(TObject *Sender, int ACol, int ARow, const UnicodeString Value)
{
const int i = (ARow - 1) * 16 + (ACol - 1);
Edited[i] = 1;
Changed[i] = 0;
}
//---------------------------------------------------------------------------
// Page UP
void __fastcall TMemoryForm::PageUpButtonClick(TObject *Sender)
{
unsigned int addr = GetAddress();
if (addr >= 0x100)
addr -= 0x100;
AddressEdit->Text = UnicodeString().sprintf(L"%08X", addr);
ReadButtonClick(Sender);
}
//---------------------------------------------------------------------------
// Page DN
void __fastcall TMemoryForm::PageDownButtonClick(TObject *Sender)
{
unsigned int addr = GetAddress();
if (addr < 0xFFFFFF00)
addr += 0x100;
AddressEdit->Text = UnicodeString().sprintf(L"%08X", addr);
ReadButtonClick(Sender);
}
//---------------------------------------------------------------------------
// Read. Only a click on this button marks the bytes that changed since the last read; Page UP/DN and Enter
// clear the marks.
void __fastcall TMemoryForm::ReadButtonClick(TObject *Sender)
{
memcpy(PrevBuf, MemBuf, sizeof MemBuf);
const unsigned int addr = GetAddress();
if (pDmGetMemory((const void *)addr, 0x100, MemBuf, NULL) < 0)
memset(MemBuf, 0, sizeof MemBuf);
memset(Edited, 0, sizeof Edited);
memset(Changed, 0, sizeof Changed);
if (Sender == ReadButton)
{
for (int i = 0; i < 0x100; i++)
if (PrevBuf[i] != MemBuf[i])
Changed[i] = 1;
}
FormCreate(Sender);
}
//---------------------------------------------------------------------------
// Write: send each edited byte to the console, one byte per call.
void __fastcall TMemoryForm::WriteButtonClick(TObject *Sender)
{
const unsigned int addr = GetAddress();
for (int i = 0; i < 0x100; i++)
{
if (Edited[i])
{
MemBuf[i] = (unsigned char)(L"0x" + MemoryGrid->Cells[(i & 0xF) + 1][(i >> 4) + 1]).ToInt();
pDmSetMemory((void *)(addr + i), 1, &MemBuf[i], NULL);
}
}
memset(Edited, 0, sizeof Edited);
MemoryGrid->Refresh();
}
//---------------------------------------------------------------------------
// Dec to Hex
void __fastcall TMemoryForm::DecToHexButtonClick(TObject *Sender)
{
const int v = (int)ConvertEdit->Text.ToDouble();
ConvertEdit->Text = UnicodeString().sprintf(L"%08X", v);
}
//---------------------------------------------------------------------------
// Hex to Dec
void __fastcall TMemoryForm::HexToDecButtonClick(TObject *Sender)
{
ConvertEdit->Text = UnicodeString((L"0x" + ConvertEdit->Text).ToInt());
}
//---------------------------------------------------------------------------
// Float to Hex: the bits of the value as a 32-bit float.
void __fastcall TMemoryForm::FloatToHexButtonClick(TObject *Sender)
{
const float f = (float)ConvertEdit->Text.ToDouble();
unsigned int bits;
memcpy(&bits, &f, sizeof bits);
ConvertEdit->Text = UnicodeString().sprintf(L"%08X", bits);
}
//---------------------------------------------------------------------------
// Hex to Float
void __fastcall TMemoryForm::HexToFloatButtonClick(TObject *Sender)
{
const int bits = (L"0x" + ConvertEdit->Text).ToInt();
float f;
memcpy(&f, &bits, sizeof f);
ConvertEdit->Text = UnicodeString().sprintf(L"%f", (double)f);
}
//---------------------------------------------------------------------------