source: drsdaq/VME/struck/sis1100/V2.02/test/sdram_rw_3.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.3 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
17static int generate_size(int max)
18{
19 int maxbits, bits;
20 int mask, size;
21
22 maxbits=0;
23 while (1<<maxbits<max) maxbits++;
24 bits=random()%maxbits+1;
25 mask=0xffffffff>>(32-bits);
26 size=random()%mask+1;
27 if ((size>max) || (size==0)) {
28 printf("invalid size %d\n", size);
29 printf(" max =0x%08x\n", max);
30 printf(" maxbits=%d\n", maxbits);
31 printf(" bits =%d\n", bits);
32 printf(" mask =0x%08x\n", mask);
33 size=1;
34 }
35 return size;
36}
37
38static int generate_start(int max, int size)
39{
40 int space, offs;
41 space=max-size;
42 offs=random()%(space+1);
43 return offs;
44}
45
46/*
47 * static void fill_buf(int size, int* buf)
48 * {
49 * int i;
50 * for (i=0; i<size; i++) {
51 * buf[i]=random();
52 * }
53 * }
54 */
55
56static void fill_buf(int size, int* buf, int num)
57{
58 int i;
59 for (i=0; i<size; i++) {
60 buf[i]=num<<28|i;
61 }
62}
63
64static int test_buf(int size, int* obuf, int* ibuf)
65{
66 int i, n=0;
67
68 for (i=0; i<size; i++) {
69 if (obuf[i]!=ibuf[i]) {
70 if (n++<20) {
71 printf("[%3d] 0x%08X --> 0x%08X\n", i, obuf[i], ibuf[i]);
72 }
73 }
74 }
75 if (n) printf(" %d errors\n", n);
76 return n;
77}
78
79static int do_write(int p, int start, int size, int* data)
80{
81 off_t pos;
82 int res;
83
84 pos=lseek(p, sizeof(int)*start, SEEK_SET);
85 if (pos==(off_t)-1) {
86 perror("do_write::lseek");
87 return 1;
88 }
89 res=write(p, data, size*sizeof(int));
90 if (res!=size*sizeof(int)) {
91 u_int32_t error;
92 ioctl(p, SIS1100_LAST_ERROR, &error);
93 if (res<0) {
94 fprintf(stderr, "write: %s; error=0x%x\n", strerror(errno), error);
95 } else {
96 fprintf(stderr, "write: res=%d; error=0x%x\n", res, error);
97 }
98 return 1;
99 }
100 return 0;
101}
102
103static int do_read(int p, int start, int size, int* data)
104{
105 off_t pos;
106 int res;
107
108 pos=lseek(p, sizeof(int)*start, SEEK_SET);
109 if (pos==(off_t)-1) {
110 perror("do_read::lseek");
111 return 1;
112 }
113 res=read(p, data, size*sizeof(int));
114 if (res!=size*sizeof(int)) {
115 u_int32_t error;
116 ioctl(p, SIS1100_LAST_ERROR, &error);
117 if (res<0) {
118 fprintf(stderr, "read: %s; error=0x%x\n", strerror(errno), error);
119 } else {
120 fprintf(stderr, "read: res=%d; error=0x%x\n", res, error);
121 }
122 return 1;
123 }
124 return 0;
125}
126
127int main(int argc, char* argv[])
128{
129 int *ibuf, *obuf;
130 off_t max;
131 int p, size, offs, num;
132
133 if (argc!=2) {
134 fprintf(stderr, "usage: %s path\n", argv[0]);
135 return 1;
136 }
137
138 if ((p=open(argv[1], O_RDWR, 0))<0) {
139 perror("open");
140 return 1;
141 }
142
143 max=lseek(p, 0, SEEK_END);
144 if (max==(off_t)-1) {
145 perror("lseek(0, SEEK_END)");
146 return 1;
147 }
148 fprintf(stderr, "size of sdram is %08Lx (%Ld MByte)\n", max, max/(1<<20));
149
150 max/=sizeof(int);
151 ibuf=calloc(max, sizeof(int));
152 obuf=calloc(max, sizeof(int));
153 if (!ibuf || !obuf) {
154 perror("calloc");
155 return 1;
156 }
157 srandom(17);
158
159 if (do_write(p, 0, max, obuf)) {
160 printf("initial write failed\n");
161 return 1;
162 }
163
164 num=0;
165 while (++num) {
166 size=generate_size(16384);
167 /*printf("size=%d\n", size);*/
168 offs=generate_start(max, size);
169 if (offs+size>max) {
170 printf("invalid offs: size=%d offs=%d\n", size, offs);
171 }
172 /*printf(" offs=%d\n", offs);*/
173
174 printf("write %08X words from %08X; (%d)\n", size, offs, num);
175 fill_buf(size, obuf+offs, num);
176 if (do_write(p, offs, size, obuf+offs)) {
177 printf("write failed\n");
178 return 1;
179 }
180
181 printf("read %08X words from %08X\n", size, offs);
182 if (do_read(p, offs, size, ibuf+offs)) {
183 printf("read failed\n");
184 return 1;
185 }
186 /*if (test_buf(size, obuf+offs, ibuf+offs)) return 1;*/
187 test_buf(size, obuf+offs, ibuf+offs);
188
189 }
190
191 free(obuf);
192 free(ibuf);
193 close(p);
194
195 return 0;
196}
Note: See TracBrowser for help on using the repository browser.