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

📄 nvrambuild.cpp

📁 The PCI Local bus concept was developed to break the PC data I/O bottleneck and clearly opens the d
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	in = 0;
	for( i=1 ; i <= num_data_rows ; i++ ) {
		chindex = 0;
		szTemp2[chindex] = 0;
		for( j=1 ; j <= num_cols ; j++ ) {

			if( in < num_data_bytes ) {
				switch( num_base ) {
				case 2:
					pMain->ConvertNumberToString( data_bytes[in], 8, num_base, (char *)szTemp );
					//sprintf( szTemp, "%x", data_bytes[in] );
					break;
				case 10:
					sprintf( szTemp, "%03d", data_bytes[in] );
					break;
				default:
				case 16:
					sprintf( szTemp, "%02x", data_bytes[in] );
					break;
				}

				if( data_bytes[in] >= 0x20 && !(data_bytes[in] & 0x80) )
					szTemp2[chindex] = data_bytes[in];
				else
					szTemp2[chindex] = ' ';
			}
			else {
				strcpy( szTemp, "" );
				szTemp2[chindex] = ' ';
			}

			// Write the data to the grid
			pGridMethods->bSetGridValueString(&m_GridControl, i, j, szTemp);

			szTemp2[++chindex] = 0;

			in++;

		}

		// Write text mapping after line is done
		pGridMethods->bSetGridValueString(&m_GridControl, i, j, szTemp2 );


	}

	if( num_data_rows != num_rows ) {
		for( i=num_data_rows+1 ; i <= num_rows ; i++ ) {
			for( j=1 ; j <= num_cols ; j++ ) {
				// Write the data to the grid
				pGridMethods->bSetGridValueString(&m_GridControl, i, j, "");
			}
			// Write text mapping after line is done
			pGridMethods->bSetGridValueString(&m_GridControl, i, j, "" );
		}

	}

	grid_overhead_flag = TRUE;

}

void CNVRAMBuild::AddNameToComboList( char *new_name )
{

	int i,cnt;
	bool match;
	char szTemp[256];

	if( file_byte_size && strlen( new_name ) > 0 ) {

		match = FALSE;
		cnt = m_Combo_FILE_NAMES.GetCount();
		for( i=0 ; i < cnt ; i++ ) {
			m_Combo_FILE_NAMES.GetLBText( i, (LPTSTR) &szTemp ); 
			if( strcmp( (char *)szTemp, new_name ) == 0 ) {
				match = TRUE;
				break;
			}
		}

		if( !match ) {
			m_Combo_FILE_NAMES.AddString( new_name );
			m_Combo_FILE_NAMES.SelectString( -1, (LPCTSTR)new_name );
		}
	}

}

int CNVRAMBuild::ReadIntelHexFile( void )
{

	char FileName[256];
	char szTemp[512];
	FILE *fp;							// "file pointer"

	UINT byte_address, len, addr, recordtype;
	int load_ok,i;
	long segment;
	char line[200];
	int data;

	load_ok = TRUE;
	segment = 0;

	i = m_Combo_NVRAM_TYPE.GetCurSel();
	num_data_bytes = NVRAM_Size[i];

	file_byte_size = 0;

	m_Edit_CURRENT_FILE_NAME.GetWindowText( (char *)&FileName, sizeof(FileName ) - 1 );

	if( strlen( FileName ) ) {

		if ((fp = fopen(FileName, "rb")) == NULL) {
			sprintf( szTemp,"Could Not Open File!\nName: %s",(char *)&FileName );
			MessageBox( szTemp,gheading, MB_OK | MB_ICONSTOP );
			return -1;
		}

	}

	fscanf(fp, "%s", line);

	/* Intel Hex format */
	if (line[0] == ':') {
		do {
			len = hex2i(&line[1], 2);
			addr = hex2i(&line[3], 4);
			recordtype = hex2i(&line[7], 2);

			switch (recordtype) {

			case 0x00: /* Data record */
			if ((segment + addr) <= num_data_bytes) {
				for (byte_address = 0; byte_address < len; byte_address++) {
					data = hex2i(&line[9+byte_address*2], 2);


					if ((segment + addr + byte_address) <= num_data_bytes) {
						data_bytes[segment + addr + byte_address] = data;
						if( (segment + addr + byte_address) > file_byte_size )
							file_byte_size = segment + addr + byte_address;

					   //*(buffer + segment + addr + byte_address) = data;
					   //if ((segment + addr + byte_address + 1) > *loaded)
						//  *loaded = segment + addr + byte_address + 1;
					} else {
						//if (abort_too_large) {
						//	load_ok = FALSE;
						//	//wperror("Insufficient memory to load file!!!", YES);
						//	goto ErrorOccurred;
						//}
					}
				}
			} else {
				//if (abort_too_large) {
				//	load_ok = FALSE;
				//	//wperror("Insufficient memory to load file!!!", YES);
				//	goto ErrorOccurred;
				//}
			}
			break;

			case 0x01: /* End of data record */
			break;

			case 0xff:
			segment = hex2i(&line[9], 4) << 4;
			break;
			}
		} while (fscanf(fp, "%s", line) != EOF);
	}

//ErrorOccurred:

	if (fp)
		fclose(fp);

	if( file_byte_size )
		strcpy( (char *)&latest_file_name, FileName );
	else
		strcpy( (char *)&latest_file_name, "" );

	return load_ok;

}

int CNVRAMBuild::ReadFile( void )
{

	char FileName[256];
	char szTemp[512];
	FILE *fp;							// "file pointer"
	int i;

	i = m_Combo_NVRAM_TYPE.GetCurSel();
	num_data_bytes = NVRAM_Size[i];


	file_byte_size = 0;

	m_Edit_CURRENT_FILE_NAME.GetWindowText( (char *)&FileName, sizeof(FileName ) - 1 );

	if( strlen( FileName ) ) {

		if ((fp = fopen(FileName, "rb")) == NULL) {
			sprintf( szTemp,"Could Not Open File!\nName: %s",(char *)&FileName );
			MessageBox( szTemp,gheading, MB_OK | MB_ICONSTOP );
			return -1;
		}

		// Seek to end of file and get the locations
		fseek( fp, 0, SEEK_END );
		file_byte_size = ftell( fp );

		// Seek to start of file and read data block
		//fseek( fp, offset_data_bytes, SEEK_SET );
		fseek( fp, 0, SEEK_SET );
		num_data_bytes = fread( (void *)&data_bytes, 1, MAX_DATA_BUFFER, fp );

		fclose(fp);
	}
	else {
		MessageBox( "No File Name Entered!",gheading, MB_OK | MB_ICONSTOP );
	}

	if( file_byte_size )
		strcpy( (char *)&latest_file_name, FileName );
	else
		strcpy( (char *)&latest_file_name, "" );

	return 1;

}

void CNVRAMBuild::SaveFile(void)
{

	char FileName[256];
	char szTemp[512];
	FILE *fp;							// "file pointer"
	int i;

	i = m_Combo_NVRAM_TYPE.GetCurSel();
	num_data_bytes = NVRAM_Size[i];


	file_byte_size = 0;

	m_Edit_CURRENT_FILE_NAME_WRITE.GetWindowText( (char *)&FileName, sizeof(FileName ) - 1 );

	if( strlen( FileName ) ) {

		if ((fp = fopen(FileName, "wb")) == NULL) {
			sprintf( szTemp,"Could Not Open File!\nName: %s",(char *)&FileName );
			MessageBox( szTemp,gheading, MB_OK | MB_ICONSTOP );
			return;
		}

		i = fwrite( (void *)&data_bytes, 1, num_data_bytes, fp );

		fclose(fp);
	}
	else {
		MessageBox( "No File Name Entered!",gheading, MB_OK | MB_ICONSTOP );
	}

}

void CNVRAMBuild::SaveIntelHexFile( void )
{

	int index, retval;
	byte *ptr;
	FILE *fp;
  
	retval = 0;

	m_Edit_CURRENT_FILE_NAME_WRITE.GetWindowText( sztemp, sizeof( sztemp ) - 1 );

	if ((fp = fopen(sztemp, "w")) == NULL) {
		MessageBox( "Unable to save Intel Hex File", gheading, MB_OK );
		return;
	}

	retval = Save_HEX_Data( fp );

	/* Output End of Data record */
	if (retval != EOF)
		retval = Output_Hex_EOF(fp);

	fclose(fp);

}

int CNVRAMBuild::Save_HEX_Data( FILE *fp )
{
	int addr, bytes, retval;
	unsigned char *ptr;

	retval = 0;
	addr = 0;
	ptr = (unsigned char *)&data_bytes;

	while ((addr < num_data_bytes) && (retval != EOF)) {
		/*
		** Compute and output byte count.
		*/
		retval = Output_HEX_Line(fp, addr, num_data_bytes - addr, ptr, &bytes);
		addr += bytes;
		ptr += bytes;
	}
	return(retval);
}

int CNVRAMBuild::Output_HEX_Line(FILE *fp, int addr, long bytes_left, unsigned char *ptr, int *bytes_written)
{
	int index, retval;
	byte chksum;

	/* Output max of 32 bytes per line */
	*bytes_written = (bytes_left > 32) ? 32 : bytes_left;

	chksum = 0;

	/* Output initial collen */
	if ((retval = fputc(':', fp)) != EOF) {
		retval = fprintf(fp, "%2.2x", *bytes_written);
		chksum += *bytes_written;
	}

	/* Output address */
	if (retval != EOF) {
		fprintf(fp, "%4.4x", addr);
		chksum += (addr >> 8) + (addr & 0xff);
	}

	/* Output record type */
	if (retval != EOF)
		fprintf(fp, "%2.2x", 0);

	/* Output data */
	for (index = 0; (index < *bytes_written) && (retval != EOF); index++) {
		retval = fprintf(fp, "%2.2x", *ptr);
		chksum += *ptr++;
	}

	/* Output checksum */
	if (retval != EOF) {
		chksum = 0 - chksum;
		retval = fprintf(fp, "%2.2x\n", chksum);
	}
	return(retval);
}

int CNVRAMBuild::Output_Hex_EOF(FILE *fp)
{
	return(fprintf(fp, ":00000001FF\n"));
}


int CNVRAMBuild::hex2i( char *str, int cnt )
{

	int i;
	char t[16];

	if( cnt > 16 )
		cnt = 16;

	i = 0;
	while( cnt > 0 ) {
		t[i++] = *str++;
		cnt--;
	}
	t[i] = 0;

	i = pMain->atoh( (char *) &t );

	return i;
}


void CNVRAMBuild::OnHex() 
{
	// TODO: Add your control notification handler code here
	m_Radio_HEX.SetCheck( TRUE );
	m_Radio_DECIMAL.SetCheck( FALSE );
	m_Radio_BINARY.SetCheck( FALSE );

	old_num_base = num_base;
	num_base = 16;

	m_Static_ADDR_BASE.SetWindowText( "(Hex)" );
	m_Static_DATA_BASE.SetWindowText( "(Hex)" );

	SetupDataGrid();
	DrawDataValues();

	SetupEditData();

}

void CNVRAMBuild::OnDecimal() 
{
	// TODO: Add your control notification handler code here
	m_Radio_HEX.SetCheck( FALSE );
	m_Radio_DECIMAL.SetCheck( TRUE );
	m_Radio_BINARY.SetCheck( FALSE );

	old_num_base = num_base;
	num_base = 10;

	m_Static_ADDR_BASE.SetWindowText( "(Dec)" );
	m_Static_DATA_BASE.SetWindowText( "(Dec)" );

	SetupDataGrid();
	DrawDataValues();
	
	SetupEditData();

}

void CNVRAMBuild::OnBinary() 
{
	// TODO: Add your control notification handler code here
	m_Radio_HEX.SetCheck( FALSE );
	m_Radio_DECIMAL.SetCheck( FALSE );
	m_Radio_BINARY.SetCheck( TRUE );

	old_num_base = num_base;
	num_base = 2;

	m_Static_ADDR_BASE.SetWindowText( "(Hex)" );
	m_Static_DATA_BASE.SetWindowText( "(Binary)" );

	SetupDataGrid();
	DrawDataValues();
	
	SetupEditData();

}

void CNVRAMBuild::KillWindow( int id ) 
{

	switch( id ) {
	case IDD_DIALOG_PCI_PARAMETERS:
		pStdPCIParams = NULL;
		break;
	}

}

/////////////////////////////////////////////////////////////////////////////
// CNVRAMBuild message handlers

void CNVRAMBuild::OnCancel() 
{
	// TODO: Add extra cleanup here
	int ret;

	if( data_dirty ) {
		ret = MessageBox( "** Warning ** NVRAM Grid Data was Changed, but NOT Written to NVRAM!\n\nDo You Wish to Write to NVRAM Before Shutting Down Window?\n\n    Yes = Write Data to NVRAM, then Close\n    No = Close Without Writing Data to NVRAM\n    Cancel = Do Not Close",
			gheading, MB_YESNOCANCEL | MB_ICONEXCLAMATION );

		if( ret == IDCANCEL )
			return;
		if( ret == IDYES )
			OnButtonWriteNvram();
	}

	if( pStdPCIParams )
		pStdPCIParams->DestroyWindow();

	if( pMain ) {
		pMain->pNVRAMBuild[cur_board] = NULL;
		//pMain->m_Button_NVRAM_BUILD.EnableWindow( TRUE );
	}
	
	CDialog::OnCancel();
}

void CNVRAMBuild::OnButtonBrowse() 
{
	// TODO: Add your control notification handler code here
	char FileName[1024];
	bool ret;
	int i;

	pSaveRestore = NULL;

	pSaveRestore = new CSaveRestore;

	strcpy( pSaveRestore->szFileName, "" );

	if( m_Check_READ_INTEL_HEX.GetCheck() )
		ret = pSaveRestore->WIN_GetBrowseFileName( (char *)&FileName, 1, 0 );
	else
		ret = pSaveRestore->WIN_GetBrowseFileName( (char *)&FileName, 0, 0 );

	delete pSaveRestore;

	if( ret ) {
		m_Edit_CURRENT_FILE_NAME.SetWindowText( (char *)&FileName );
		offset_data_bytes = 0;

		if( !m_Check_READ_MERGE.GetCheck() ) {
			i = m_Combo_NVRAM_TYPE.GetCurSel();
			num_data_bytes = NVRAM_Size[i];

			for( i=0 ; i < num_data_bytes ; i++ )
				data_bytes[i] = 0;
		}

		if( m_Check_READ_INTEL_HEX.GetCheck() ) {
			ReadIntelHexFile();
		}
		else {
			ReadFile();
		}

		SetupDataGrid();
		DrawDataValues();

		AddNameToComboList( (char *)&latest_file_name );
	}
	
}

void CNVRAMBuild::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CNVRAMBuild::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CNVRAMBuild::OnButtonReread() 
{
	// TODO: Add your control notification handler code here

	DisableAll();

	ReadData();

	SetupDataGrid();
	DrawDataValues();
	
	EnableAll();

⌨️ 快捷键说明

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