Rev | Line | |
---|
[9] | 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 |
|
---|
| 19 | int peek(segment, offset)
|
---|
| 20 | unsigned segment;
|
---|
| 21 | unsigned 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 |
|
---|
| 34 | int poke(segment, offset, value)
|
---|
| 35 | unsigned segment;
|
---|
| 36 | unsigned offset;
|
---|
| 37 | unsigned 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.