📄 flashspecificsim.c
字号:
/*
* COPYRIGHT (c) 1995,2000 TriMedia Technologies Inc.
*
* +-----------------------------------------------------------------+
* | THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY ONLY BE USED |
* | AND COPIED IN ACCORDANCE WITH THE TERMS AND CONDITIONS OF SUCH |
* | A LICENSE AND WITH THE INCLUSION OF THE THIS COPY RIGHT NOTICE. |
* | THIS SOFTWARE OR ANY OTHER COPIES OF THIS SOFTWARE MAY NOT BE |
* | PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY OTHER PERSON. THE |
* | OWNERSHIP AND TITLE OF THIS SOFTWARE IS NOT TRANSFERRED. |
* +-----------------------------------------------------------------+
*
* Module name : FlashSpecificSim.c 1.6
*
* Title : Low level flash manipulation routines
*
* Last update : 18:32:23 - 99/03/07
*
* Reviewed :
*
* Revision history :
*
* Description : Simulation of flash access functions
* on allocated memory. This simulation
* trashes the contents of the flash
* memory, or reads it in from a file
* called "FLASH.image", when present.
* On exit, the flash contents are stored
* to "FLASH.image".
*
* Flash erasure, and flash writing is
* subject to random errors in the written
* contents, as follows:
*
* - Upon erasure, some words might not be
* completely set to 0xffffffff.
* - Upon writing, some bits might fail to
* switch from 1 to 0.
*
* The maximum amount of bits in error can
* be specifed in macro MAX_ERRORBITS.
*
* The error rate can be specified in
* macro ERROR_RATE.
*
*/
/*------------------------------- Includes -----------------------------------*/
#include "stdio.h"
#include "fcntl.h"
#include <tmlib/tmtypes.h>
/*-------------------------------- Functions ---------------------------------*/
#define MY_SZOF_FLASH_BLOCK 0x40000
#define MY_NROF_FLASH_BLOCKS 32
Pointer FLASH_BASE;
Int SZOF_FLASH_BLOCK = (Int) MY_SZOF_FLASH_BLOCK;
Int NROF_FLASH_BLOCKS = (Int) MY_NROF_FLASH_BLOCKS;
;
#define SZOF_FLASH (SZOF_FLASH_BLOCK * NROF_FLASH_BLOCKS)
#ifndef ERROR_RATE
#define ERROR_RATE 0
#endif
#define FLASH_IMAGE "FLASH.image"
#define MAX_ERRORBITS 7
#define FS_TRACE printf
static void write_flash()
{
Int fd= open(FLASH_IMAGE, O_CREAT | O_WRONLY | O_BINARY, 0777 );
if (fd != -1) {
write(fd, FLASH_BASE, SZOF_FLASH) ;
close(fd);
}
}
Bool FLASH_init()
{
Int i;
Int fd;
Bool result;
FLASH_BASE= (Pointer)malloc(SZOF_FLASH);
if (FLASH_BASE == Null) {
FS_TRACE(("Flash allocation failed"));
return False;
}
atexit(write_flash);
fd= open(FLASH_IMAGE, O_RDONLY | O_BINARY );
if (fd != -1) {
result= read(fd, FLASH_BASE, SZOF_FLASH) == SZOF_FLASH;
close(fd);
}
return True;
}
#if ERROR_RATE
static int my_rand(void)
{
static int rand_next = 1;
rand_next = (rand_next) * 1103515245 + 12345;
return (unsigned int)((rand_next)/65536) % 32768;
}
Int disturbed( Int data )
{
Int i;
UInt32 r = my_rand()&0xffff;
Int errormask= 0;
if ( r < ERROR_RATE) {
for (i=0; i<MAX_ERRORBITS; i++) {
Int errorbit= my_rand() & 31;
errormask |= (1 << errorbit);
}
}
return data | errormask;
}
#else
#define disturbed(x) (x)
#endif
Bool FLASH_block_erase(UInt ab, Bool check_if_necessary)
{
int i;
UInt32* f= (Pointer) ( ((UInt)FLASH_BASE) + ab*SZOF_FLASH_BLOCK );
for (i=0; i<SZOF_FLASH_BLOCK/4; i++) {
*(f++) = ~disturbed(0);
}
return True;
}
Bool
FLASH_write(Pointer address, UInt32 data)
{
UInt32* f= (Pointer) ( ((UInt)FLASH_BASE) + ((UInt)address) );
*f &= disturbed(data);
return *f == data;
}
Bool
FLASH_block_write( Pointer flash, Pointer image, Int nrof_words )
{
Int32 *f = ((Int32*)flash) + nrof_words;
Int32 *i = ((Int32*)image) + nrof_words;
Bool result;
do {
result = FLASH_write( --f, *(--i) );
} while ((Pointer)f != flash && result);
return result;
}
UInt32
FLASH_read(Pointer address)
{
UInt32* f= (Pointer) ( ((UInt)FLASH_BASE) + ((UInt)address) );
return *f;
}
void
FLASH_block_read( Pointer flash, Pointer image, Int nrof_words )
{
Int32 *f = ((Int32*)flash);
Int32 *i = ((Int32*)image);
while (nrof_words--) {
*(i++) = FLASH_read( f++ );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -