For /dev/mem, map in memory to be copied to memory's own address space
[minix3.git] / lib / other / peekpoke.c
blobd311a123bc75f3725fbf5cbbe0711d6771fa93ef
1 /* Peek and poke using /dev/mem.
3 * Callers now ought to check the return values.
5 * Calling peek() requires read permission on /dev/mem, and consumes
6 * a file descriptor. Calling poke() requires write permission, and
7 * consumes another file descriptor.
8 */
10 #include <sys/types.h>
11 #include <fcntl.h>
12 #include <unistd.h>
14 _PROTOTYPE( int peek, (unsigned segment, unsigned offset));
15 _PROTOTYPE( int poke, (unsigned segment, unsigned offset, unsigned value));
17 #define SEGSIZE 0x10
19 int peek(segment, offset)
20 unsigned segment;
21 unsigned offset;
23 unsigned char chvalue;
24 static int infd = -1;
26 if (infd < 0) infd = open("/dev/mem", O_RDONLY);
27 if (infd < 0 ||
28 lseek(infd, (unsigned long) segment * SEGSIZE + offset, SEEK_SET) < 0 ||
29 read(infd, (char *) &chvalue, (unsigned) 1) != 1)
30 return(-1);
31 return(chvalue);
34 int poke(segment, offset, value)
35 unsigned segment;
36 unsigned offset;
37 unsigned value;
39 unsigned char chvalue;
40 static int outfd = -1;
42 chvalue = value;
43 if (outfd < 0) outfd = open("/dev/mem", O_WRONLY);
44 if (outfd < 0 ||
45 lseek(outfd, (unsigned long) segment * SEGSIZE + offset, SEEK_SET) < 0 ||
46 write(outfd, (char *) &chvalue, (unsigned) 1) != 1)
47 return(-1);
48 return(chvalue);