📄 romdrv.c
字号:
/***************************************************************************************\
*
* Copyright (c) 2001 National ASIC Center, All Rights Reserved
*
* File Name : romdrv.c
* Version : 1.0
* Programmer : longn_qi
*
* Date of Creation: 2001/11/26
* Date of Last Modify: 2001/11/27
*
* Description: This is an application file for simulating ROM.
*
* Local Function List:
* void InitROM( )
* void FlashUnlockBlock( unsigned char *Address ) (empty)
* void FlashLockBlock( unsigned char *Address ) (empty)
* void FlashEraseBlock( unsigned char *Address )
* unsigned char FlashWriteByte( unsigned char *Address, unsigned char Data )
* unsigned char *FlashWriteBlock( unsigned char *Address, unsigned char *Data, unsigned long len )
* void CopyFlashProgram( ) (empty)
* void WriteFlashProgram( unsigned char *address, unsigned char *data, unsigned long len ) (empty)
* unsigned long FlashReadByte( unsigned char *Address, unsigned char buffer )
* unsigned long FlashReadBlock( unsigned char *Address, unsigned char *buffer, unsigned long len )
*
* Global Variable List:
* None
*
* Note: Further file description refers to simdrv.txt
*
***************************************************************************************
*
* Modification History
*
* 2001/11/27 by longn_qi add functions "FlashReadByte" and "FlashReadBlock".
* 2001/11/26 by longn_qi create file.
*
\***************************************************************************************/
/* System or Standard Header */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <flash.h>
#define ROM_PAGE_SIZE 65536 // 64KB per ROM page
#define ROM_PAGE_NUM 2 // number of ROM page
#define ROM_SIZE (ROM_PAGE_SIZE*ROM_PAGE_NUM) // ROM size (16MByte)
static FILE *fpROM = NULL; // simulate ROM as file
const char *romFilePath = "config\\rom.bin";
FBP FlashUnlockBlockP = (FBP)FlashUnlockBlock;
FBP FlashLockBlockP = (FBP)FlashLockBlock;
FBP FlashEraseBlockP = (FBP)FlashEraseBlock;
FWP FlashWriteByteP = (FWP)FlashWriteByte;
FWBP FlashWriteBlockP = (FWBP)FlashWriteBlock;
FRBP FlashReadBlockP = (FRBP)FlashReadBlock;
void InitROM( )
{
unsigned char *page;
unsigned short i;
fpROM = fopen( romFilePath, "rb" );
if( fpROM == NULL )
{
fpROM = fopen( romFilePath, "wb" );
page = (unsigned char *)malloc( ROM_PAGE_SIZE );
memset( page, 0xff, ROM_PAGE_SIZE );
for( i=0; i<ROM_PAGE_NUM; i++)
fwrite( page, ROM_PAGE_SIZE, 1, fpROM );
free( page );
}
fclose( fpROM );
}
void FlashUnlockBlock( unsigned long Address )
{
}
void FlashLockBlock( unsigned long Address )
{
}
void FlashEraseBlock( unsigned long Address )
{
unsigned long offset = Address;
unsigned char *page;
fpROM = fopen( romFilePath, "r+b" );
if( fpROM == NULL || offset >= ROM_SIZE )
return;
fseek( fpROM, offset, SEEK_SET);
page = (unsigned char *)malloc( ROM_PAGE_SIZE );
memset( page, 0xff, ROM_PAGE_SIZE );
fwrite( page, ROM_PAGE_SIZE, 1, fpROM );
free( page );
fclose( fpROM );
}
unsigned char FlashWriteByte( unsigned long Address, unsigned char Data )
{
unsigned long offset = Address;
fpROM = fopen( romFilePath, "r+b" );
if( fpROM == NULL || offset >= ROM_SIZE )
return 0;
fseek( fpROM, offset, SEEK_SET);
fwrite( &Data, 1, 1, fpROM );
fclose( fpROM );
return 1;
} /* if Write OK return(1)
else return(0) */
unsigned long FlashWriteBlock( unsigned long Address, unsigned long DataAd, unsigned long len )
{
unsigned long offset = Address;
fpROM = fopen( romFilePath, "r+b" );
if( fpROM == NULL || offset >= ROM_SIZE || offset + len > ROM_SIZE )
return 0;
fseek( fpROM, offset, SEEK_SET);
fwrite( (unsigned char *)DataAd, len, 1, fpROM );
fclose( fpROM );
return Address;
}
void CopyFlashProgram( )
{
}
void WriteFlashProgram( unsigned long address, unsigned long DataAd, unsigned long len )
{
}
unsigned long FlashReadByte( unsigned long buffer, unsigned long Address )
{
unsigned long offset = Address, res;
fpROM = fopen( romFilePath, "r+b" );
if( fpROM == NULL || offset >= ROM_SIZE )
return 0;
fseek( fpROM, offset, SEEK_SET);
res = fread( (unsigned char *)buffer, 1, 1, fpROM );
fclose( fpROM );
return res;
}
unsigned long FlashReadBlock( unsigned long buffer, unsigned long Address, unsigned long len )
{
unsigned long offset = Address, res;
fpROM = fopen( romFilePath, "r+b" );
if( fpROM == NULL || offset >= ROM_SIZE || offset + len > ROM_SIZE )
return 0;
fseek( fpROM, offset, SEEK_SET);
res = fread( (unsigned char *)buffer, len, 1, fpROM );
fclose( fpROM );
return res;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -