From 0f15fd3e21d7092dd733f29f33d9494b3c13bfa4 Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Fri, 8 Jan 2010 18:03:51 +1300 Subject: [PATCH] Move utilities into separate file. --- Makefile | 2 +- dis.c | 3 +-- fet.c | 5 +---- main.c | 39 +++------------------------------------ rf2500.c | 3 +-- uif.c | 3 +-- util.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ util.h | 28 ++++++++++++++++++++++++++++ 8 files changed, 87 insertions(+), 47 deletions(-) create mode 100644 util.c create mode 100644 util.h diff --git a/Makefile b/Makefile index 0c6ee48..2ad8a37 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ clean: .SUFFIXES: .c .o -mspdebug: main.o fet.o rf2500.o dis.o uif.o ihex.o elf32.o stab.o +mspdebug: main.o fet.o rf2500.o dis.o uif.o ihex.o elf32.o stab.o util.o $(CC) $(CFLAGS) -o $@ $^ -lusb .c.o: diff --git a/dis.c b/dis.c index 26003cd..6bc3173 100644 --- a/dis.c +++ b/dis.c @@ -22,6 +22,7 @@ #include "dis.h" #include "stab.h" +#include "util.h" /**********************************************************************/ /* Disassembler @@ -429,8 +430,6 @@ int dis_decode(u_int8_t *code, u_int16_t offset, u_int16_t len, return ret; } -#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) - /* Return the mnemonic for an operation, if possible. * * If the argument is not a valid operation, this function returns the diff --git a/fet.c b/fet.c index 09bac63..fe3cd0c 100644 --- a/fet.c +++ b/fet.c @@ -26,10 +26,9 @@ #include #include +#include "util.h" #include "device.h" -#define ARRAY_LEN(a) ((sizeof(a)) / sizeof((a)[0])) - static const struct fet_transport *fet_transport; static int fet_is_rf2500; @@ -551,8 +550,6 @@ static const struct { } }; -extern void hexdump(int addr, const u_int8_t *data, int len); - static int do_identify(void) { int i; diff --git a/main.c b/main.c index 234dd5e..e68a55d 100644 --- a/main.c +++ b/main.c @@ -28,41 +28,10 @@ #include "device.h" #include "binfile.h" #include "stab.h" +#include "util.h" static const struct device *msp430_dev; -void hexdump(int addr, const u_int8_t *data, int len) -{ - int offset = 0; - - while (offset < len) { - int i, j; - - /* Address label */ - printf(" %04x:", offset + addr); - - /* Hex portion */ - for (i = 0; i < 16 && offset + i < len; i++) - printf(" %02x", - ((const unsigned char *)data)[offset + i]); - for (j = i; j < 16; j++) - printf(" "); - - /* Printable characters */ - printf(" |"); - for (j = 0; j < i; j++) { - int c = ((const unsigned char *)data)[offset + j]; - - printf("%c", (c >= 32 && c <= 126) ? c : '.'); - } - for (; j < 16; j++) - printf(" "); - printf("|\n"); - - offset += i; - } -} - /********************************************************************** * Command-line interface */ @@ -551,13 +520,11 @@ static const struct command all_commands[] = { " Load symbols from the given file.\n"}, }; -#define NUM_COMMANDS (sizeof(all_commands) / sizeof(all_commands[0])) - const struct command *find_command(const char *name) { int i; - for (i = 0; i < NUM_COMMANDS; i++) + for (i = 0; i < ARRAY_LEN(all_commands); i++) if (!strcasecmp(name, all_commands[i].name)) return &all_commands[i]; @@ -581,7 +548,7 @@ static int cmd_help(char **arg) int i; printf("Available commands:"); - for (i = 0; i < NUM_COMMANDS; i++) + for (i = 0; i < ARRAY_LEN(all_commands); i++) printf(" %s", all_commands[i].name); printf("\n"); printf("Type \"help \" for more information.\n"); diff --git a/rf2500.c b/rf2500.c index 97a7d03..2c22f92 100644 --- a/rf2500.c +++ b/rf2500.c @@ -21,8 +21,7 @@ #include #include "transport.h" - -extern void hexdump(int addr, const u_int8_t *data, int len); +#include "util.h" /********************************************************************* * USB transport diff --git a/uif.c b/uif.c index 4e02a64..1deefaf 100644 --- a/uif.c +++ b/uif.c @@ -27,8 +27,7 @@ #include #include "transport.h" - -extern void hexdump(int addr, const u_int8_t *data, int len); +#include "util.h" static int serial_fd = -1; diff --git a/util.c b/util.c new file mode 100644 index 0000000..78798bc --- /dev/null +++ b/util.c @@ -0,0 +1,51 @@ +/* MSPDebug - debugging tool for the eZ430 + * Copyright (C) 2009, 2010 Daniel Beer + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include "util.h" + +void hexdump(int addr, const u_int8_t *data, int len) +{ + int offset = 0; + + while (offset < len) { + int i, j; + + /* Address label */ + printf(" %04x:", offset + addr); + + /* Hex portion */ + for (i = 0; i < 16 && offset + i < len; i++) + printf(" %02x", data[offset + i]); + for (j = i; j < 16; j++) + printf(" "); + + /* Printable characters */ + printf(" |"); + for (j = 0; j < i; j++) { + int c = data[offset + j]; + + printf("%c", (c >= 32 && c <= 126) ? c : '.'); + } + for (; j < 16; j++) + printf(" "); + printf("|\n"); + + offset += i; + } +} diff --git a/util.h b/util.h new file mode 100644 index 0000000..310f7a3 --- /dev/null +++ b/util.h @@ -0,0 +1,28 @@ +/* MSPDebug - debugging tool for the eZ430 + * Copyright (C) 2009, 2010 Daniel Beer + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef UTIL_H_ +#define UTIL_H_ + +#include + +void hexdump(int addr, const u_int8_t *data, int len); + +#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) + +#endif