source: trunk/minix/lib/other/peekpoke.c@ 11

Last change on this file since 11 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.2 KB
Line 
1/* Peek and poke using /dev/mem.
2 *
3 * Callers now ought to check the return values.
4 *
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 */
9
10#include <sys/types.h>
11#include <fcntl.h>
12#include <unistd.h>
13
14_PROTOTYPE( int peek, (unsigned segment, unsigned offset));
15_PROTOTYPE( int poke, (unsigned segment, unsigned offset, unsigned value));
16
17#define SEGSIZE 0x10
18
19int peek(segment, offset)
20unsigned segment;
21unsigned offset;
22{
23 unsigned char chvalue;
24 static int infd = -1;
25
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);
32}
33
34int poke(segment, offset, value)
35unsigned segment;
36unsigned offset;
37unsigned value;
38{
39 unsigned char chvalue;
40 static int outfd = -1;
41
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);
49}
Note: See TracBrowser for help on using the repository browser.