13 #define YNORSIM_FNAME "emfile-nor"
15 /* Set YNORSIM_BIT_CHANGES to a a value from 1..30 to
16 *simulate bit flipping as the programming happens.
17 * A low value results in faster simulation with less chance of encountering a partially programmed
21 //#define YNORSIM_BIT_CHANGES 15
22 #define YNORSIM_BIT_CHANGES 2
25 /* Simulate 32MB of flash in 256k byte blocks.
29 #define YNORSIM_BLOCK_SIZE_U32 (256*1024/4)
30 #define YNORSIM_DEV_SIZE_U32 (32*1024 * 1024/4)
32 /* Simulate 8MB of flash in 256k byte blocks.
36 #define YNORSIM_BLOCK_SIZE_U32 (256*1024/4)
37 #define YNORSIM_DEV_SIZE_U32 (8*1024 * 1024/4)
40 static __u32 word[YNORSIM_DEV_SIZE_U32];
42 extern int random_seed;
43 extern int simulate_power_failure;
45 static void NorError(void)
47 printf("Nor error\n");
51 static void ynorsim_SaveImage(void)
53 int h = open(YNORSIM_FNAME, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
54 write(h,word,sizeof(word));
58 static void ynorsim_RestoreImage(void)
60 int h = open(YNORSIM_FNAME, O_RDONLY, S_IREAD | S_IWRITE);
61 memset(word,0xFF,sizeof(word));
62 read(h,word,sizeof(word));
67 static void ynorsim_PowerFail(void)
73 static int initialised = 0;
74 static int remaining_ops;
75 static int nops_so_far;
77 int ops_multiplier = 500;
79 static void ynorsim_MaybePowerFail(void)
86 if(simulate_power_failure &&
88 printf("Simulated power failure after %d operations\n",nops_so_far);
93 static void ynorsim_Ready(void)
98 remaining_ops = 1000000000;
99 remaining_ops = (rand() % 10000) * ops_multiplier * YNORSIM_BIT_CHANGES;
100 ynorsim_RestoreImage();
103 void ynorsim_Read32(__u32 *addr,__u32 *buf, int nwords)
113 void ynorsim_WriteOneWord32(__u32 *addr,__u32 val)
121 // Fail due to trying to change a zero into a 1
122 printf("attempt to set a zero to one (%x)->(%x)\n",tmp,val);
126 for(i = 0; i < YNORSIM_BIT_CHANGES; i++){
127 m = 1 << (rand() & 31);
131 ynorsim_MaybePowerFail();
137 ynorsim_MaybePowerFail();
140 void ynorsim_Write32(__u32 *addr, __u32 *buf, int nwords)
143 ynorsim_WriteOneWord32(addr,*buf);
150 void ynorsim_EraseBlock(__u32 *addr)
152 /* Todo... bit flipping */
153 memset(addr,0xFF,YNORSIM_BLOCK_SIZE_U32 * 4);
156 void ynorsim_Initialise(void)
161 void ynorsim_Shutdown(void)
167 __u32 *ynorsim_GetBase(void)