source: drsdaq/VME/struck/sis1100/V2.02/test/sdram_rw_4.c@ 23

Last change on this file since 23 was 22, checked in by ogrimm, 16 years ago
First commit of drsdaq program
File size: 7.1 KB
Line 
1#define _GNU_SOURCE
2#define _LARGEFILE_SOURCE
3#define _LARGEFILE64_SOURCE
4#define _FILE_OFFSET_BITS 64
5#include <stdio.h>
6#include <errno.h>
7#include <string.h>
8#include <sys/types.h>
9#include <time.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <fcntl.h>
13#include <sys/ioctl.h>
14
15#include "dev/pci/sis1100_var.h"
16
17/****************************************************************************/
18#if 0
19static void light_set(int p, int v)
20{
21 u_int32_t reg, set, res;
22
23 set=((v&1)<<7)|((v&6)<<9);
24 res=~(set<<16)&0x0c800000;
25 reg=set|res;
26 ioctl(p, SIS1100_FRONT_IO, &reg);
27}
28#endif
29/****************************************************************************/
30static int* alloc_buf(int num)
31{
32 unsigned long int p;
33 int pagesize, pagemask;
34
35 pagesize=getpagesize();
36 pagemask=pagesize-1;
37 printf("pagesize=%d\n", pagesize);
38 p=(unsigned long int)calloc(num+pagesize, 1);
39 if (!p) return (int*)p;
40 printf("p_0=0x%08lx\n", p);
41 p=(p+pagesize-1)&(~pagemask);
42 printf("p_1=0x%08lx\n", p);
43 return (int*)p;
44}
45#if 0
46static int generate_size(int max)
47{
48 int maxbits, bits;
49 int mask, size;
50
51 maxbits=0;
52 while (1<<maxbits<max) maxbits++;
53 bits=random()%maxbits+1;
54 mask=0xffffffff>>(32-bits);
55 size=random()%mask+1;
56 if ((size>max) || (size==0)) {
57 printf("invalid size %d\n", size);
58 printf(" max =0x%08x\n", max);
59 printf(" maxbits=%d\n", maxbits);
60 printf(" bits =%d\n", bits);
61 printf(" mask =0x%08x\n", mask);
62 size=1;
63 }
64 return size;
65}
66#endif
67
68static int generate_start(int max, int size)
69{
70 int space, offs;
71 space=max-size;
72 offs=random()%(space+1);
73 return offs;
74}
75
76static int test_obuf(int size, int* obuf)
77{
78 int i, n;
79
80 n=0;
81 for (i=0; i<size; i+=4) {
82 if ((obuf[i]!=0x12345678)||(obuf[i+1]!=i)) {
83 if (!n) printf("\nobuf corrupted:\n");
84 if (n<20)
85 printf("[%08X]: %08X %08X %08X %08X\n",
86 i, obuf[i], obuf[i+1], obuf[i+2], obuf[i+3]);
87 n++;
88 }
89 }
90 return n;
91}
92
93static int test_buf(int size, int* obuf, int* ibuf)
94{
95 int i, n, res, state;
96
97 n=0; state=0;
98 for (i=0; (i<size)&&(n<5); i+=4) {
99 res=(ibuf[i]!=0x12345678)||(ibuf[i+1]!=i);
100 if (res!=state) {
101 int start, j;
102 printf("\n~~~~~~~~~~~~~~~~~~~~~~~%d~~~~~~~~~~~~~~~~~~~~~~~\n", res);
103 start=i-20; if (start<0) start=0;
104 for (j=start; j<start+40; j+=4)
105 printf("[%08X]: %08X %08X %08X %08X\n",
106 j, ibuf[j], ibuf[j+1], ibuf[j+2], ibuf[j+3]);
107 state=res;
108 n++;
109 }
110 }
111 return n;
112}
113
114static int do_write(int p, int start, int size, int* data)
115{
116 off_t pos;
117 int res;
118
119 pos=lseek(p, sizeof(int)*start, SEEK_SET);
120 if (pos==(off_t)-1) {
121 perror("do_write::lseek");
122 return 1;
123 }
124 res=write(p, data, size*sizeof(int));
125 if (res!=size*sizeof(int)) {
126 u_int32_t error;
127 ioctl(p, SIS1100_LAST_ERROR, &error);
128 if (res<0) {
129 fprintf(stderr, "write: %s; error=0x%x\n", strerror(errno), error);
130 } else {
131 fprintf(stderr, "write: res=%d; error=0x%x\n", res, error);
132 }
133 return 1;
134 }
135 return 0;
136}
137
138static int test_write(int p, int start, int size, int* data)
139{
140 off_t pos;
141 int res, word1, word2;
142
143 pos=lseek(p, sizeof(int)*(start+2), SEEK_SET);
144 if (pos==(off_t)-1) {
145 perror("test_write::lseek a");
146 return 1;
147 }
148 res=read(p, &word1, sizeof(int));
149 if (res!=sizeof(int)) {
150 perror("test_write a");
151 return 1;
152 }
153 pos=lseek(p, sizeof(int)*(start+size-2), SEEK_SET);
154 if (pos==(off_t)-1) {
155 perror("test_write::lseek b");
156 return 1;
157 }
158 res=read(p, &word2, sizeof(int));
159 if (res!=sizeof(int)) {
160 perror("test_write b");
161 return 1;
162 }
163 if ((word1!=start)||(word2!=start)) {
164 printf("test_write: start=%08X\n", start);
165 printf("test_write: [%08X]: %08X-->%08X\n", start+2, data[2], word1);
166 printf(" [%08X]: %08X-->%08X\n", start+size-2,
167 data[size-2], word2);
168 return 1;
169 }
170 return 0;
171}
172
173static int do_read(int p, int start, int size, int* data)
174{
175 off_t pos;
176 int res;
177
178 pos=lseek(p, sizeof(int)*start, SEEK_SET);
179 if (pos==(off_t)-1) {
180 perror("do_read::lseek");
181 return 1;
182 }
183 res=read(p, data, size*sizeof(int));
184 if (res!=size*sizeof(int)) {
185 u_int32_t error;
186 ioctl(p, SIS1100_LAST_ERROR, &error);
187 if (res<0) {
188 fprintf(stderr, "read: %s; error=0x%x\n", strerror(errno), error);
189 } else {
190 fprintf(stderr, "read: res=%d; error=0x%x\n", res, error);
191 }
192 return 1;
193 }
194 return 0;
195}
196
197static void prepare_data(int* buf, int offs, int size)
198{
199 int* start=buf+offs;
200 int i;
201
202 for (i=0; i<size; i+=4) {
203 start[i+2]=offs;
204 start[i+3]=size;
205 }
206}
207
208int main(int argc, char* argv[])
209{
210 int *ibuf, *obuf;
211 off_t max;
212 int p, size, offs, num, i, j;
213
214 if (argc!=2) {
215 fprintf(stderr, "usage: %s path\n", argv[0]);
216 return 1;
217 }
218
219 if ((p=open(argv[1], O_RDWR, 0))<0) {
220 perror("open");
221 return 1;
222 }
223
224 max=lseek(p, 0, SEEK_END);
225 if (max==(off_t)-1) {
226 perror("lseek(0, SEEK_END)");
227 return 1;
228 }
229 fprintf(stderr, "size of sdram is %08Lx (%Ld MByte)\n", max, max/(1<<20));
230
231 max/=sizeof(int);
232
233 ibuf=alloc_buf(max*sizeof(int));
234 obuf=alloc_buf(max*sizeof(int));
235 if (!ibuf || !obuf) {
236 perror("calloc");
237 return 1;
238 }
239
240 for (i=0; i<max; i+=4) {
241 obuf[i]=0x12345678;
242 obuf[i+1]=i;
243 }
244
245 if (do_write(p, 0, max, obuf)) {
246 printf("initial write failed\n");
247 return 1;
248 }
249 if (test_obuf(max, obuf)) {
250 printf("initial obuf test failed\n");
251 }
252 srandom(17);
253 num=0;
254 while (++num) {
255 int res;
256
257 for (j=0; j<1000; j++) {
258 size=/*generate_size(max)*/ 16384;
259 size=(size+3)&~3;
260
261 /*printf("size=%d\n", size);*/
262 offs=generate_start(max, size);
263 offs&=~3;
264 if (offs+size>max) {
265 printf("\ninvalid offs: size=%d offs=%d\n", size, offs);
266 }
267 /*printf(" offs=%d\n", offs);*/
268
269 /*printf("write %08X words from %08X; (%d)\n", size, offs, num);*/
270 /*printf("+"); fflush(stdout);*/
271 prepare_data(obuf, offs, size);
272 if (do_write(p, offs, size, obuf+offs)) {
273 printf("\nwrite failed\n");
274 return 1;
275 }
276 if (test_write(p, offs, size, obuf+offs)) {
277 printf("test_write: Fehler\n");
278 return 1;
279 }
280 }
281
282 if (do_read(p, 0, max, ibuf)) {
283 printf("\nread failed\n");
284 return 1;
285 }
286
287 printf("-"); fflush(stdout);
288 res=test_buf(max, obuf, ibuf);
289 if (res) {
290 ioctl(p, SIS1100_DUMP);
291 return 0;
292 }
293
294 }
295
296 close(p);
297
298 return 0;
299}
Note: See TracBrowser for help on using the repository browser.