source: drsdaq/VME/struck/sis1100/V2.02/test/sdram_rw_7.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: 4.8 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
17char* pathname;
18
19
20/****************************************************************************/
21static int getopts(int argc, char* argv[])
22{
23 extern char *optarg;
24 extern int optind;
25 extern int opterr;
26 extern int optopt;
27
28 const char* optstring="f:";
29
30 int c, errflag = 0;
31
32 pathname=0;
33 while (!errflag && ((c = getopt(argc, argv, optstring)) != -1)) {
34 switch (c) {
35 case 'f': pathname=optarg;
36 break;
37 case '?':
38 case 'h':
39 errflag=1;
40 }
41 }
42 if (errflag || !pathname) {
43 fprintf(stderr, "usage: %s -f pathname\n", argv[0]);
44 return -1;
45 } else {
46 return 0;
47 }
48}
49/****************************************************************************/
50static off_t select_size(off_t max)
51{
52 off_t size;
53 size=random()&0x1fffff;
54 return size;
55}
56/****************************************************************************/
57static off_t select_start(off_t max, off_t size)
58{
59 off_t diff=max-size;
60 off_t mask, start;
61 mask=1;
62 while (mask<diff) {mask<<=1; mask++;}
63 do {
64 start=random()&mask;
65 } while (start>diff);
66 return start;
67}
68/****************************************************************************/
69static void fill_random(u_int32_t* buf, off_t size)
70{
71 /*off_t i;*/
72 for (; size; buf++, size--) *buf=random();
73 /*for (i=0; size; buf++, size--, i++) *buf=i;*/
74}
75/****************************************************************************/
76static int check(u_int32_t* obuf, u_int32_t* ibuf, off_t size)
77{
78 off_t i;
79 int count;
80
81 if (!bcmp(obuf, ibuf, size*sizeof(int))) {
82 return 0;
83 }
84 for (i=0, count=0; (i<size) && (count<10); i++) {
85 if (obuf[i]!=ibuf[i]) {
86 fprintf(stderr, "check: [0x%08Lx] 0x%08x --> 0x%08x\n",
87 i, obuf[i], ibuf[i]);
88 count++;
89 }
90 }
91 return -1;
92}
93/****************************************************************************/
94int main(int argc, char* argv[])
95{
96 u_int32_t *ibuf, *obuf;
97 int p, res;
98 off_t max;
99
100 srandom(17);
101 if (getopts(argc, argv)<0) return 1;
102 if ((p=open(pathname, O_RDWR, 0))<0) {
103 fprintf(stderr, "open %s: %s\n", pathname, strerror(errno));
104 return 1;
105 }
106 max=lseek(p, 0, SEEK_END);
107 if (max==(off_t)-1) {
108 perror("lseek(0, SEEK_END)");
109 return 1;
110 }
111 printf("size of sdram is %08Lx (%Ld MByte)\n", max, max/(1<<20));
112 max/=sizeof(int);
113 ibuf=malloc(max*sizeof(int));
114 printf("ibuf=0x%08X\n", (unsigned int)ibuf);
115 obuf=malloc(max*sizeof(int));
116 printf("obuf=0x%08X\n", (unsigned int)obuf);
117 if (!ibuf || !obuf) {
118 perror("calloc");
119 return 1;
120 }
121 while (1) {
122 off_t start_w, start_r, start_m, size;
123
124 size=select_size(max);
125 start_w=select_start(max, size);
126 start_r=select_start(max, size);
127 start_m=select_start(max, size);
128 fill_random(obuf+start_w, size);
129 if (lseek(p, sizeof(int)*start_m, SEEK_SET)==(off_t)-1) {
130 fprintf(stderr, "lseek(0x%08Lx): %s\n",
131 sizeof(int)*start_m, strerror(errno));
132 return 1;
133 }
134 res=write(p, obuf+start_w, size*sizeof(int));
135 if (res!=size*sizeof(int)) {
136 fprintf(stderr, "write: %s\n", strerror(errno));
137 return 1;
138 }
139 if (lseek(p, -size*sizeof(int), SEEK_CUR)==(off_t)-1) {
140 fprintf(stderr, "lseek(0x%08Lx (rel)): %s\n",
141 -size*sizeof(int), strerror(errno));
142 return 1;
143 }
144 res=read(p, ibuf+start_r, size*sizeof(int));
145 if (res!=size*sizeof(int)) {
146 fprintf(stderr, "read: %s\n", strerror(errno));
147 return 1;
148 }
149 if (check(obuf+start_w, ibuf+start_r, size))
150 return 1;
151 printf("."); fflush(stdout);
152 }
153 return 0;
154}
Note: See TracBrowser for help on using the repository browser.