43 #include <sys/ioctl.h>
46 #include <sys/types.h>
58 #define FNAME "/dev/acq32/acq32.1.raw"
61 "mmap $Revision: 1.4 $\n" \
62 "usage: mmap opts\n" \
64 " -r read [default]\n" \
66 " -n nop (just block, holding the mapping)\n" \
69 "maps device [ram] space and either\n" \
70 " reads from ram to stdout\n" \
71 "-or-writes to ram from stdin\n" \
72 " -b [-v value] = block fill [default 0xdeadbeef]\n" \
73 " -T [list of test regs] registers walking bit test\n" \
78 int doRegsTest(
volatile unsigned* regs,
unsigned* offsets,
int nregs)
87 for (ir = 0; ir != nregs; ++ir){
88 regs[offsets[ir]] = 0;
90 for (ir = 0; ir != nregs; ++ir){
92 for (cursor = 0x1; cursor; cursor <<= 1){
95 dbg(3,
"regs[%d] := %08x", offsets[ir], cursor);
97 regs[offsets[ir]] = cursor;
99 for (irr = 0; irr != nregs; ++irr){
100 got = regs[offsets[irr]];
102 dbg(3,
"regs[%d] = %08x", irr, got);
109 err(
"regs[%d] wanted 0x%08x got 0x%08x",
110 offsets[irr], wanted, got);
119 regs[offsets[ir]] = cursor;
122 dbg(2,
"complete %d fail %d pass", fail, pass);
140 for (nargs = 0; argv[nargs] != NULL; ++nargs){
144 info(
"pmem %p nargs:%d argv[0] \"%s\"", pmem, nargs, argv[0]);
146 offsets = calloc(nargs,
sizeof(
unsigned));
148 for (ii = 0; ii != nargs; ++ii){
149 offsets[ii] = strtoul(argv[ii], 0, 0);
150 offsets[ii] /=
sizeof(unsigned);
151 dbg(2,
"offsets[%d] (%s) is %d", ii, argv[ii], offsets[ii]);
155 if (
doRegsTest((
unsigned*)pmem, offsets, nargs)){
161 info(
"FAIL:%10d PASS:%10d %s",
163 G_fail==0?
"*** NO ERRORS ***":
":-( :-( :-(");
171 static void alarm_handler(
int signum) {
175 static void quit_handler(
int signum){
176 alarm_handler(signum);
179 static void install_handlers(
void) {
181 memset(&sa, 0,
sizeof(sa));
182 sa.sa_handler = alarm_handler;
184 struct sigaction saq;
185 memset(&saq, 0,
sizeof(saq));
186 sa.sa_handler = quit_handler;
188 if (sigaction(SIGALRM, &sa, NULL)) perror (
"sigaction");
189 if (sigaction(SIGINT, &saq, NULL)) perror (
"sigaction");
193 int main(
int argc,
const char* argv[] )
201 unsigned length = 0x100000;
203 unsigned fill_value = 0xdeadbeef;
204 enum MODE { M_READ, M_WRITE, M_FILL, M_TEST, M_NOP } mode = M_READ;
206 struct poptOption opt_table[] = {
207 {
"device",
'f', POPT_ARG_STRING, &fname, 0 },
208 {
"help",
'h', POPT_ARG_NONE, 0,
'h' },
209 {
"read",
'r', POPT_ARG_NONE, 0,
'r' },
210 {
"write",
'w', POPT_ARG_NONE, 0,
'w' },
211 {
"nop",
'n', POPT_ARG_NONE, 0,
'n' },
212 {
"fill",
'b', POPT_ARG_NONE, 0,
'f' },
213 {
"offset",
'o', POPT_ARG_INT, &offset,
'o' },
214 {
"length",
'l', POPT_ARG_INT, &length,
'l' },
215 {
"value",
'v', POPT_ARG_INT, &fill_value, 0 },
216 {
"regstest",
'T', POPT_ARG_NONE, 0,
'T' },
221 poptContext opt_context;
223 opt_context = poptGetContext( argv[0], argc, argv, opt_table, 0 );
225 int open_mode = O_RDONLY;
226 int mmap_mode = PROT_READ;
228 while ( (rc = poptGetNextOpt( opt_context )) > 0 ){
231 fprintf( stderr,
HELP );
238 mmap_mode = PROT_READ|PROT_WRITE;
246 mmap_mode = PROT_READ|PROT_WRITE;
256 if ( (fd = open( fname, open_mode)) < 0 ){
257 fprintf( stderr,
"mmap: failed to open device \"%s\" - ", fname );
262 region = mmap( NULL, length, mmap_mode, MAP_SHARED, fd, 0 );
264 if ( region == (caddr_t)-1 ){
275 write( 1, (
char*)region+offset, length );
278 unsigned* praw = (
unsigned*)&((
char*)region)[offset];
280 int imax = length/
sizeof(unsigned);
282 for ( iwrite = 0; iwrite != imax; ++iwrite ){
283 praw[iwrite] = fill_value;
294 char* praw = &((
char*)region)[offset];
296 for ( iwrite=0; (cc = getchar()) != -1 && iwrite != length; ++iwrite ){
303 "mmap: blocking. Hit any key to continue, q to quit\n" );
305 while ( getchar() !=
'q' ){
307 FILE* fp = fopen(
"/proc/self/maps",
"r" );
310 while( fgets( aline,
sizeof(aline), fp ) != NULL ) {
311 fputs(aline, stderr);
319 return regsTest(region, poptGetArgs(opt_context));