📄 ncdebug.c
字号:
UINT * Val,
void * Ptr,
enum NCDEBUG_WORDSIZE WordSize,
char * AsciiNum
)
{ // Get a word (8, 16, 32 bit) from memory, convert to string
switch (WordSize)
{
case WORDSIZE_BYTE:
*Val = *(PBYTE)Ptr;
sprintf(AsciiNum, "%2.2x", *Val);
break;
case WORDSIZE_WORD:
*Val = *(USHORT*)Ptr;
sprintf(AsciiNum, "%4.4x", *Val);
break;
case WORDSIZE_LONG:
*Val = *(ULONG*)Ptr;
sprintf(AsciiNum, "%8.8x", *Val);
break;
default:
printf("GetWord(): Illegal Word Size: 0x%x\n", WordSize);
}
}
///////////////////////////////////////////////////////////////////////////////
void
DisplayMemoryLine(
PVOID Ptr,
UINT Count,
enum NCDEBUG_WORDSIZE WordSize
)
{
UINT ii;
UINT Val = (UINT)Ptr;
char AsciiNum[] = "01234567";
CHAR Addr[9];
CHAR AsciiCode[NCDEBUG_BYTES_PER_LINE + 1];
CHAR LineBuf[55] = {0};
PCHAR pTmpLine = &AsciiCode[0];
// Convert memory address to a string
sprintf(&Addr[0], "%8.8x", Val);
// Build one line (generally 16 bytes) of memory content, including
// the memory's ASCII translation
for (ii = 0; ii < NCDEBUG_BYTES_PER_LINE; ii += WordSize)
{ // For every byte in the line of memory:
if (ii == 8)
strcat(LineBuf, " -");
strcat(LineBuf, " ");
if (ii < Count)
{ // Read memory value into a temporary location (so real location
// won't be read twice):
// - Each location is displayed twice: once for raw, once for ASCII
// - May be reading a chip's FIFO (And we don't want to read it
// twice for one display line!)
GetWord(&Val, Ptr, WordSize, AsciiNum);
strcat(LineBuf, AsciiNum);
switch (WordSize)
{
case WORDSIZE_BYTE:
*(PBYTE)(AsciiCode + ii) = (BYTE)Val;
break;
case WORDSIZE_WORD:
*(PUSHORT)(AsciiCode + ii) = (USHORT)Val;
break;
case WORDSIZE_LONG:
*(PULONG)(AsciiCode + ii) = (ULONG)Val;
break;
}
}
else
{
switch(WordSize)
{
case WORDSIZE_BYTE:
strcat(LineBuf, " ");
break;
case WORDSIZE_WORD:
strcat(LineBuf, " ");
break;
case WORDSIZE_LONG:
strcat(LineBuf, " ");
break;
}
}
Ptr = (PBYTE)Ptr + WordSize;
}
// Get the ASCII representation of the memory content
for (ii = 0; ii < NCDEBUG_BYTES_PER_LINE; ii++)
{
if (ii < Count)
{
if ((AsciiCode[ii] < 0x20) || (AsciiCode[ii] >= 0x80))
AsciiCode[ii] = '.';
}
else
AsciiCode[ii] = ' ';
}
AsciiCode[ii] = 0;
printf("%s%s %s\n", Addr, LineBuf, AsciiCode);
}
///////////////////////////////////////////////////////////////////////////////
#include <conio.h> // Required for _kbhit()
///////////////////////////////////////////////////////////////////////////////
void
NcDisplayMemory(
UINT Volume,
PVOID Ptr,
UINT Count,
enum NCDEBUG_WORDSIZE WordSize // Byte, Word, Ulong
)
{ // Display memory:
// - Single byte, two-byte or four-byte formats
// - Show address
// - raw and ASCII decoding
// - Automatic line breaks (every 16 bytes)
UINT BytesInThisLine = 0;
#define NCPRINTF(_vol, _x_) \
if(_vol <= gPrintVolume) { \
printf _x_ ; \
}
if (Volume > gPrintVolume)
{ // Calling function's volume setting is less than the current volume
// control setting. Generally, print only when volume control setting
// is quite high. That is, print only when the user WANTS to see lots
// of printed messages!
return;
}
ASSERT((INT)Count >= 0); // Sanity check: Negative count would be too much!
for(; Count; Count -= BytesInThisLine)
{
if (_kbhit())
{ // User wants to quit display
// - Probably displaying excessive amount of memory!
INT ch = _getch();
printf(" Display memory: terminated by user\n");
break;
}
BytesInThisLine = (Count > NCDEBUG_BYTES_PER_LINE)? NCDEBUG_BYTES_PER_LINE: Count;
DisplayMemoryLine (Ptr, BytesInThisLine, WordSize);
Ptr = (PCHAR)Ptr + BytesInThisLine;
}
}
#endif // _NCDEBUG && _NC_RDK_AND_WINDOWS
#if _NCDEBUG
///////////////////////////////////////////////////////////////////////////////
// Support for filling buffers with 'friendly' (human-readable) data
#define FILL_WITH_FRIENDLY_TEXT TRUE // TRUE: Add friendly text to buffer
#define FILL_WITH_PACKET_MARKERS TRUE // TRUE: Add markers on packet boundries
void
FriendlyFillBuffer(
PNC_ENDPOINT_OBJECT Endpoint
)
{ // Fill a buffer (usually a USB IN buffer) with 'friendly' info
// - Helpful for debugging transfers
// - The endpoint is expected to 'created'
PNC_TRANSFER_OBJECT Transfer = Endpoint->Transfer;
UINT ii;
#if FILL_WITH_PACKET_MARKERS || FILL_WITH_FRIENDLY_TEXT
USHORT MaxPacketSize;
#endif
#if FILL_WITH_PACKET_MARKERS
UINT jj = 0;
#endif // FILL_WITH_PACKET_MARKERS
ASSERT(PrivDeviceObject != NULL);
ASSERT(Transfer != NULL);
ASSERT(Transfer->TransferBuffer != NULL);
// First fill buffer with pattern
for (ii = 0; ii < Transfer->TransferSize; ii++)
{
*((PBYTE)(Transfer->TransferBuffer) + ii) = (BYTE)ii;
}
#if FILL_WITH_PACKET_MARKERS
// Add packet markers at packet boundries
MaxPacketSize = (USHORT)Endpoint->Priv.EpMaxPkt.Word;
if (MaxPacketSize == 0)
{
NCPRINTF(VOLUME_MINIMUM, ("FriendlyFillBuffer(): Logical Ep:%2.2x: MaxPacket is zero!\n", Endpoint->LogicalEp));
MaxPacketSize = sizeof(USHORT);
}
for (ii = 0; ii < Transfer->TransferSize; ii += MaxPacketSize)
{
*(PUSHORT)((PBYTE)(Transfer->TransferBuffer) + ii) = (USHORT)jj++;
}
#endif // FILL_WITH_PACKET_MARKERS
#if FILL_WITH_FRIENDLY_TEXT
// Add friendly text
// - Porting: Some compilers do not support _snprintf(). This friendly text can be
// restructured as needed (or set Fill With Friendly Text to FALSE)
/* _snprintf(
(char *)Transfer->TransferBuffer,
Transfer->TransferSize,
"Hello from: %s by %s... "
"This endpoint: USB-EP:0x%2.2x, %s %s.... "
"Maximum packet size: 0x%3.3x.... "
"Buffer size: 0x%8.8x.... "
"Remaining buffer content: incrementing pattern (8-bit) with packet markers (16-bit) at each packet boundry....",
PrivDeviceObject->ClientStrings[PrivDeviceObject->DeviceDescriptor->iProduct],
PrivDeviceObject->ClientStrings[PrivDeviceObject->DeviceDescriptor->iManufacturer],
Endpoint->EpDescriptor->bEndpointAddress,
EpTypeString[(Endpoint->EpDescriptor->bmAttributes) & 0x03],
EpDirString[(Endpoint->EpDescriptor->bEndpointAddress>>8) & 0x01],
Endpoint->Priv.EpMaxPkt.Word,
Transfer->TransferSize
);
*/
#endif // FILL_WITH_FRIENDLY_TEXT
}
#endif // _NCDEBUG
///////////////////////////////////////////////////////////////////////////////
// End of file
///////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -