Changeset 4544


Ignore:
Timestamp:
07/20/08 22:34:37 (5 years ago)
Author:
werner
Message:

Added word size selection.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • developers/werner/poke/poke.c

    r4543 r4544  
    1616#include <stdlib.h> 
    1717#include <stdio.h> 
     18#include <string.h> 
    1819#include <fcntl.h> 
    1920#include <sys/mman.h> 
     
    2122 
    2223#define PAGE_SIZE 4096 
    23 #define MEM(addr) (*(uint32_t *) (mem+((addr) & (PAGE_SIZE-1)))) 
     24#define MEM(bits, addr) (*(uint##bits##_t *) (mem+((addr) & (PAGE_SIZE-1)))) 
    2425 
    2526static volatile void *mem; 
     
    3132static void __attribute__((noreturn)) usage(const char *name) 
    3233{ 
    33         fprintf(stderr, "usage: %s hex_address [value]\n", name); 
     34        fprintf(stderr, "usage: %s [-8|-16|-32] hex_address [value]\n", name); 
    3435        exit(1); 
    3536} 
     
    4243        unsigned long addr; 
    4344        uint32_t val; 
     45        int bits = 32; 
     46        char **args = argv+1; 
    4447 
    4548        fd = open("/dev/mem", O_RDWR); 
     
    4851                exit(1); 
    4952        } 
     53        if (argc > 1 && *argv[1] == '-') { 
     54                if (!strcmp(argv[1], "-8")) 
     55                        bits = 8; 
     56                else if (!strcmp(argv[1], "-16")) 
     57                        bits = 16; 
     58                else if (!strcmp(argv[1], "-32")) 
     59                        bits = 32; 
     60                else 
     61                    usage(*argv); 
     62                args++; 
     63                argc--; /* dirty */ 
     64        } 
    5065        switch (argc) { 
    5166        case 3: 
    52                 val = strtoul(argv[2], &end, 0); 
     67                val = strtoul(args[1], &end, 0); 
    5368                if (*end) 
    5469                        usage(*argv); 
    5570                /* fall through */ 
    5671        case 2: 
    57                 addr = strtoul(argv[1], &end, 16); 
     72                addr = strtoul(args[0], &end, 16); 
    5873                if (*end) 
    5974                        usage(*argv); 
     
    6883                exit(1); 
    6984        } 
    70         if (argc == 2) 
    71                 printf("0x%08lx\n", (unsigned long) MEM(addr)); 
    72         else 
    73                 MEM(addr) = val; 
     85        if (argc == 2) { 
     86                switch (bits) { 
     87                case 8: 
     88                        printf("0x%02lx\n", (unsigned long) MEM(8, addr)); 
     89                        break; 
     90                case 16: 
     91                        printf("0x%04lx\n", (unsigned long) MEM(16, addr)); 
     92                        break; 
     93                case 32: 
     94                        printf("0x%08lx\n", (unsigned long) MEM(32, addr)); 
     95                        break; 
     96                } 
     97        } 
     98        else { 
     99                switch (bits) { 
     100                case 8: 
     101                        MEM(8, addr) = val; 
     102                        break; 
     103                case 16: 
     104                        MEM(16, addr) = val; 
     105                        break; 
     106                case 32: 
     107                        MEM(32, addr) = val; 
     108                        break; 
     109                } 
     110        } 
    74111        return 0; 
    75112} 
Note: See TracChangeset for help on using the changeset viewer.