From 5a84cbf7f5c7682b51755d9ae0fe73dc81462dc8 Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Sat, 9 Jan 2010 16:14:30 +1300 Subject: [PATCH] Basic simulation implementation (memory IO only). --- Makefile | 2 +- device.h | 3 ++ main.c | 50 +++++++++++++++--------- sim.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+), 19 deletions(-) create mode 100644 sim.c diff --git a/Makefile b/Makefile index 4260d16..358b520 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ clean: .SUFFIXES: .c .o mspdebug: main.o fet.o rf2500.o dis.o uif.o ihex.o elf32.o stab.o util.o \ - bsl.o + bsl.o sim.o $(CC) $(CFLAGS) -o $@ $^ -lusb .c.o: diff --git a/device.h b/device.h index 10c6dcd..083056a 100644 --- a/device.h +++ b/device.h @@ -54,4 +54,7 @@ const struct device *fet_open(const struct fet_transport *transport, /* MSP430 FET Bootloader implementation. */ const struct device *fet_open_bl(const struct fet_transport *transport); +/* Dummy/simulation implementation. */ +const struct device *sim_open(void); + #endif diff --git a/main.c b/main.c index 2593430..7ff4d2d 100644 --- a/main.c +++ b/main.c @@ -616,7 +616,7 @@ static void reader_loop(void) static void usage(const char *progname) { fprintf(stderr, -"Usage: %s [-u device] [-j] [-B] [-v voltage] [command ...]\n" +"Usage: %s [-u device] [-j] [-B] [-s] [-v voltage] [command ...]\n" "\n" " -u device\n" " Open the given tty device (MSP430 UIF compatible devices).\n" @@ -626,6 +626,8 @@ static void usage(const char *progname) " Set the supply voltage, in millivolts.\n" " -B\n" " Debug the FET itself through the bootloader.\n" +" -s\n" +" Start in simulation mode (only memory IO is allowed).\n" "\n" "By default, the first RF2500 device on the USB bus is opened.\n" "\n" @@ -642,6 +644,7 @@ int main(int argc, char **argv) int flags = 0; int want_jtag = 0; int want_bootloader = 0; + int want_sim = 0; int vcc_mv = 3000; puts( @@ -650,7 +653,8 @@ int main(int argc, char **argv) "This is free software; see the source for copying conditions. There is NO\n" "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"); - while ((opt = getopt(argc, argv, "u:jv:B")) >= 0) + /* Parse arguments */ + while ((opt = getopt(argc, argv, "u:jv:Bs")) >= 0) switch (opt) { case 'u': uif_device = optarg; @@ -668,28 +672,38 @@ int main(int argc, char **argv) want_bootloader = 1; break; + case 's': + want_sim = 1; + break; + default: usage(argv[0]); return -1; } - /* Open the appropriate transport */ - if (uif_device) - trans = uif_open(uif_device); - else { - trans = rf2500_open(); - flags |= FET_PROTO_RF2500; - } - if (!trans) - return -1; + /* Open a device */ + if (want_sim) { + msp430_dev = sim_open(); + } else { + /* Open the appropriate transport */ + if (uif_device) + trans = uif_open(uif_device); + else { + trans = rf2500_open(); + flags |= FET_PROTO_RF2500; + } + if (!trans) + return -1; + + /* Then initialize the device */ + if (!want_jtag) + flags |= FET_PROTO_SPYBIWIRE; + if (want_bootloader) + msp430_dev = fet_open_bl(trans); + else + msp430_dev = fet_open(trans, flags, vcc_mv); + } - /* Then initialize the device */ - if (!want_jtag) - flags |= FET_PROTO_SPYBIWIRE; - if (want_bootloader) - msp430_dev = fet_open_bl(trans); - else - msp430_dev = fet_open(trans, flags, vcc_mv); if (!msp430_dev) return -1; diff --git a/sim.c b/sim.c new file mode 100644 index 0000000..765fd3e --- /dev/null +++ b/sim.c @@ -0,0 +1,115 @@ +/* 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 +#include +#include "device.h" + +#define MEM_SIZE 65536 + +static u_int8_t *memory; + +static void sim_close(void) +{ + if (memory) { + free(memory); + memory = NULL; + } +} + +static int sim_control(device_ctl_t action) +{ + switch (action) { + case DEVICE_CTL_ERASE: + memset(memory, 0xff, MEM_SIZE); + return 0; + + case DEVICE_CTL_HALT: + return 0; + + default: + fprintf(stderr, "sim: CPU control is not implemented\n"); + break; + } + + return -1; +} + +static int sim_wait(void) +{ + return 0; +} + +static int sim_breakpoint(u_int16_t addr) +{ + return 0; +} + +static int sim_getregs(u_int16_t *regs) +{ + fprintf(stderr, "sim: register fetch is not implemented\n"); + return -1; +} + +static int sim_setregs(const u_int16_t *regs) +{ + fprintf(stderr, "sim: register store is not implemented\n"); + return -1; +} + +static int sim_readmem(u_int16_t addr, u_int8_t *mem, int len) +{ + if (addr + len > MEM_SIZE) + len = MEM_SIZE - addr; + + memcpy(mem, memory + addr, len); + return 0; +} + +static int sim_writemem(u_int16_t addr, const u_int8_t *mem, int len) +{ + if (addr + len > MEM_SIZE) + len = MEM_SIZE - addr; + + memcpy(memory + addr, mem, len); + return 0; +} + +static const struct device sim_device = { + .close = sim_close, + .control = sim_control, + .wait = sim_wait, + .breakpoint = sim_breakpoint, + .getregs = sim_getregs, + .setregs = sim_setregs, + .readmem = sim_readmem, + .writemem = sim_writemem +}; + +const struct device *sim_open(void) +{ + memory = malloc(MEM_SIZE); + if (!memory) { + perror("sim: can't allocate memory"); + return NULL; + } + + printf("Simulation started, 0x%x bytes of RAM\n", MEM_SIZE); + return &sim_device; +}