⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 motfile.cpp

📁 M16C Flash Starter Software Ver.2.0.0.46 Source Files.zip是瑞萨的M16C系列单片机的flash烧写程序。
💻 CPP
字号:
// MotFile.cpp: CMotFile Implementation of the CMotFile class   
//
// This software can be offered for free and used as necessary to aid 
// in your program developments.
//
// RENESAS TECHNOLOGY CORPORATION, RENESAS SOLUTIONS CORPORATION,
// and related original software developers assume no responsibility 
// for any damage or infringement of any third-party's rights, originating 
// in the use of the following software.
// Please use this software under the agreement and acceptance of these conditions. 
//
// Copyright(C)1998(2003) RENESAS TECHNOLOGY CORPORATION AND RENESAS SOLUTIONS CORPORATION
// ALL RIGHTS RESERVED
//
//////////////////////////////////////////////////////////////////////

#include <afxcoll.h>

#include "stdafx.h"
#include "M16Cflsh.h"
#include "MotFile.h"
#include "LineImage.h"

#include "defining.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/disappearance   
//////////////////////////////////////////////////////////////////////

CMotFile::CMotFile()
{
	m_pArrayPage.SetSize(NUM_PAGE2, -1);	// NUM_PAGE1 : 0x0100 ...
	for(int nIndex = 0; NUM_PAGE2 > nIndex; nIndex++)
	{
		m_pArrayPage.SetAt(nIndex, NULL);
	}
}

CMotFile::~CMotFile()
{
	CLineImage* pPage;
	for(int nIndex = 0; NUM_PAGE2 > nIndex; nIndex++)
	{
		if(NULL != (pPage = (CLineImage*)m_pArrayPage.GetAt(nIndex)))
		{
			delete pPage;
		}
	}
	m_pArrayPage.RemoveAll();
}

//////////////////////////////////////////////////////////////////////

	// The data of 1 column is transformed.  
BYTE CMotFile::charToByte(TCHAR chHex)
{
	// If it is not a hexadecimal number returning NOT_MOT it ends.  
	if(!isxdigit(chHex))
	{
		return NOT_MOT;
	}

	// Transforms it into the BYTE style.  
	BYTE byteHex = 0;
	if('0' <= chHex && '9' >= chHex)
	{
		byteHex = chHex - '0';
	}
	else if('a' <= chHex && 'f' >= chHex)
	{
		byteHex = chHex - 'a' + 0x0a;
	}
	else if('A' <= chHex && 'F' >= chHex)
	{
		byteHex = chHex - 'A' + 0x0A;
	}

	return byteHex;
}

	// The data of 1 byte is transformed.  
BOOL CMotFile::strToByte(BYTE* byteHex, CString strHex)
{
	// If byteHex is not the empty returning FALSE it ends.  
	if(BLANK != *byteHex)
	{
		return FALSE;
	}

	// If it is not a hexadecimal number returning FALSE.  
	if(!isxdigit(strHex.GetAt(0)) || !isxdigit(strHex.GetAt(1)))
	{
		return FALSE;
	}

	// Transforms it into the BYTE style.  
	*byteHex = charToByte(strHex.GetAt(0)) * 0x10 + charToByte(strHex.GetAt(1));

	return TRUE;
}

	// Check the record type.  
int CMotFile::getRecType(CString strLine)
{
	return  charToByte(strLine.GetAt(1));
}

	// Check the length of data.  
int CMotFile::getDataLength(CString strLine)
{
	BYTE nLength = BLANK;
	TCHAR strLength[3];
	strLength[0] = strLine.GetAt(2);
	strLength[1] = strLine.GetAt(3);
	strLength[2] = '\0';
	if(!strToByte(&nLength, strLength))
	{
		return 0;
	}
	nLength = nLength - (getRecType(strLine) + 1) - 1;
	return nLength;
}

	// A Mot file is transformed 1 line.  
BOOL CMotFile::lineToBytes(BYTE* byteLine, CString strLine)
{
	//If it is not a Mot file return NULL.  
	if('S' != strLine.GetAt(0))
	{
		return FALSE;
	}

	// Check the record type.  
	int nRecType = getRecType(strLine);

	// Check the length of data.  
	int nLength = getDataLength(strLine);

	// It transforms it into the BYTE style.  
	for(int nIndex = 0; nIndex < PAGE_BYTE; nIndex++)
	{
		byteLine[nIndex] = 0xff;
	}
	if(1 <= nRecType && 3 >= nRecType)
	{
        int nStrIndex = (nRecType + 1) * 2 + 4;
		TCHAR strHex[3];
        for(int nIndex = 0; nIndex < nLength; nIndex++, nStrIndex += 2)
        {
			strHex[0] = strLine.GetAt(nStrIndex);
			strHex[1] = strLine.GetAt(nStrIndex + 1);
			strHex[2] = '\0';
			if(!strToByte(&(byteLine[nIndex]), strHex))
			{
				return FALSE;
			}
        }
    }

    return TRUE;
}

    // Acquire an address from the Mot file of 1 line.  
int CMotFile::getAddress(CString strLine)
{
	// If it is not a Mot file return-1.  
	if('S' != strLine.GetAt(0) && 's' != strLine.GetAt(0))
	{
		return -1;
	}

	// Check the record type.  
	int nRecType = getRecType(strLine);

	// Acquire the address.  
    int nAddress = 0;
    int nEndIndex = (nRecType + 1) * 2 + 4;
	TCHAR strHex[3];
    for(int nIndex = 4; nIndex < nEndIndex; nIndex += 2)
    {
		BYTE byteHex = BLANK;
		strHex[0] = strLine.GetAt(nIndex);
		strHex[1] = strLine.GetAt(nIndex + 1);
		strHex[2] = '\r';
		if(!strToByte(&byteHex, strHex))
		{
			return 0;
		}
		nAddress = nAddress * 0x0100 + byteHex;
	}

	return nAddress;
}

	// Check whether the line is not sitting astride in a page   
BOOL CMotFile::isOverPage(CString strLine)
{
	int address = getAddress(strLine);
	int length = getDataLength(strLine);
	if((address / PAGE_SIZE) != ((address + length - 1) / PAGE_SIZE))
	{
		return TRUE;
	}
	return FALSE;
}

	// Make transmission data from a Mot file.   
BOOL CMotFile::MakeImage(CString strFilePath)
{
	// Delete transmission data.  
	CLineImage* pPage;
	for(int nIndex = 0; NUM_PAGE2 > nIndex; nIndex++)
	{
		if(NULL != (pPage = (CLineImage*)m_pArrayPage.GetAt(nIndex)))
		{
			delete pPage;
			m_pArrayPage.SetAt(nIndex, NULL);
		}
	}

	//Open a Mot file.
	CStdioFile fileHex(strFilePath, CFile::modeRead); 
	CString strLine;

	// Make transmission data from the Mot file.  
	while(fileHex.ReadString(strLine))
	{

		// If it is not a Mot file return FALSE.  
		if(strLine.GetLength())
		{			
			if('S' != strLine.GetAt(0))
			{
				return FALSE;
			}
	
			//Acquire the record type.  
			int nRecType = getRecType(strLine);
	
			// Transform and preserve data.  
			if(1 <= nRecType && 3 >= nRecType)
			{
				//Ccheck the address, page.  
				int nAddress = getAddress(strLine);
				int nPage = nAddress / PAGE_SIZE;

				// If the page is not secured it secures it.  
				CLineImage* pImPage;
				if(NULL == (pImPage = (CLineImage*)m_pArrayPage.GetAt(nPage)))
				{
					pImPage = new CLineImage;
					m_pArrayPage.SetAt(nPage, pImPage);
					pImPage->ClearPage();
				}

				// Transforming.
				BYTE byteLine[PAGE_SIZE];
				for(int nIndex = 0; nIndex < PAGE_SIZE; nIndex++)
				{
					byteLine[nIndex] = BLANK;
				}
				// If it is the file where is not able to handle return FALSE.  
				if(!lineToBytes(byteLine, strLine))
				{
					AfxGetMainWnd()->MessageBox(GetResString(IDS_NACCEPT), GetResString(IDS_FLASH_TITLE));
					return FALSE;
				}
				// save
				int length = 0;
				length = getDataLength(strLine);
				if(isOverPage(strLine))
				{
//		BUG FIX 99.4.23
//					pImPage->SetBody(byteLine, nAddress, length);
					pImPage->SetBody(byteLine, nAddress,  (PAGE_SIZE - (nAddress % PAGE_SIZE)));
//		BUG FIX END
					for(int nNextIndex = 0, nIndex = PAGE_SIZE - (nAddress % PAGE_SIZE); length > nNextIndex; nNextIndex++, nIndex++)
					{
						byteLine[nNextIndex] = byteLine[nIndex];
					}
					length = nNextIndex;
					while(nNextIndex < PAGE_SIZE)
					{
						byteLine[nNextIndex] = BLANK;
						nNextIndex++;
					}
					nAddress = nAddress - (nAddress % PAGE_SIZE) + PAGE_SIZE;
					nPage = nAddress / PAGE_SIZE;
					if(NULL == (pImPage = (CLineImage*)m_pArrayPage.GetAt(nPage)))
					{
						pImPage = new CLineImage;
						m_pArrayPage.SetAt(nPage, pImPage);
						pImPage->ClearPage();
					}
				}
				pImPage->SetBody(byteLine, nAddress, length);
			}	

			// If it ends normally return TRUE.  
			else if(7 <= nRecType && 9 >= nRecType)
			{
				return TRUE;
			}
		}
	}
	return FALSE;
}

	// Take out the data of the page that was designated.  
CLineImage* CMotFile::GetPage(BYTE* byteData, int nPage)
{
	// It is when nPage is exceeding the biggest index.  
	if(int nMax = m_pArrayPage.GetUpperBound() < nPage)
	{
		return (CLineImage*)m_pArrayPage.GetAt(nMax);
	}

	CLineImage* pcliPage = (CLineImage*)m_pArrayPage.GetAt(nPage);
	if(NULL != pcliPage)
	{
		BYTE byteBody[PAGE_SIZE];
		pcliPage->GetBody(byteBody);
		for(int nIndex = 0; nIndex < PAGE_SIZE; nIndex++)
		{
			byteData[nIndex] = byteBody[nIndex];
		}
	}

	return pcliPage;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -