Package se.sics.mspsim.core

Examples of se.sics.mspsim.core.MSP430


      }
    });

    handler.registerCommand("speed", new BasicCommand("set the speed factor for the CPU", "[factor]") {
      public int executeCommand(CommandContext context) {
        MSP430 cpu = registry.getComponent(MSP430.class);
        if (cpu == null) {
          context.err.println("could not access the CPU.");
          return 1;
        } else if (context.getArgumentCount() == 0) {
          /* No speed specified. Simply show current speed. */
        } else {
          double rate = context.getArgumentAsDouble(0);
          if (rate > 0.0) {
            cpu.setExecutionRate(rate);
          } else {
            context.err.println("Speed factor must be larger than zero.");
            return 1;
          }
        }
        double rate = cpu.getExecutionRate();
        context.out.printf("Speed factor is set to %.2f\n", rate);
        return 0;
      }
    });

    handler.registerCommand("echo", new BasicCommand("echo arguments", "") {
      public int executeCommand(CommandContext context) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0, n = context.getArgumentCount(); i < n; i++) {
          if (i > 0) sb.append(' ');
          sb.append(context.getArgument(i));
        }
        context.out.println(sb.toString());
        return 0;
      }
    });
   
   

    handler.registerCommand("source", new BasicCommand("run script", "[-v] <filename>") {
      public int executeCommand(CommandContext context) {
          boolean verbose = false;
          if (context.getArgumentCount() > 1) {
              verbose = "-v".equals(context.getArgument(0));
          }
        File fp = new File(context.getArgument(context.getArgumentCount() - 1));
        if (!fp.canRead()) {
          context.err.println("could not find the script file '" + context.getArgument(0) + "'.");
          return 1;
        }
        try {
          FileInputStream infs = new FileInputStream(fp);
          BufferedReader input = new BufferedReader(new InputStreamReader(infs));
          try {
            String line;
            while ((line = input.readLine()) != null) {
              if (verbose) context.out.println(line);
              context.executeCommand(line);
            }
          } finally {
            input.close();
          }
        } catch (IOException e) {
          e.printStackTrace(context.err);
          return 1;
        }
        return 0;
      }
    });

    handler.registerCommand("repeat", new BasicAsyncCommand("repeat the specified command line", "[-t delay] [-c count] <command line>") {

      private MSP430 cpu;
      private int period = 1;
      private int count = 0;
      private int maxCount = -1;
      private String commandLine;
      private boolean isRunning = true;

      public int executeCommand(final CommandContext context) {
        int index = 0;
        do {
          String a = context.getArgument(index);
          if (a.startsWith("-")) {
            if (a.equals("-t")) {
              period = context.getArgumentAsInt(index + 1);
              index += 2;
            } else if (a.equals("-c")) {
              maxCount = context.getArgumentAsInt(index + 1);
              index += 2;
            } else {
              context.err.println("illegal option: " + a);
              return 1;
            }
          } else {
            break;
          }
        } while (true);
        if (index + 1 < context.getArgumentCount()) {
          context.err.println("too many arguments");
          return 1;
        }
        commandLine = context.getArgument(index);

        cpu = registry.getComponent(MSP430.class);
        if (cpu == null) {
          context.err.println("could not access the CPU.");
          return 1;
        }

        cpu.scheduleTimeEventMillis(new TimeEvent(0) {

          @Override
          public void execute(long t) {
            if (isRunning) {
              count++;
              context.executeCommand(commandLine);
              if ((maxCount <= 0) || (count < maxCount)) {
                cpu.scheduleTimeEventMillis(this, period * 1000d);
              } else {
                stopCommand(context);
              }
            }
          }

        }, period * 1000d);
        return 0;
      }

      public void stopCommand(CommandContext context) {
        isRunning = false;
        context.err.println("[repeat exit: " + commandLine + ']');
        context.exit(0);
      }
    });

    handler.registerCommand("exec", new ExecCommand());

    handler.registerCommand("trig", new BasicLineCommand("trigg command when getting input", "<command>") {
      String command = null;
      CommandContext context;
      public int executeCommand(CommandContext context) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0, n = context.getArgumentCount(); i < n; i++) {
          if (i > 0) sb.append(' ');
          sb.append(context.getArgument(i));
        }
        command = sb.toString();
        this.context = context;
        return 0;
      }
      public void lineRead(String line) {
        context.executeCommand(command);
      }
    });

    handler.registerCommand("install", new BasicCommand("install and start a plugin", "ClassName [Name]") {
      @Override
      public int executeCommand(CommandContext context) {
        String className = context.getArgument(0);
        String name = className;
        if (context.getArgumentCount() > 1) {
          name = context.getArgument(1);
        }
        if (registry.getComponent(name) != null) {
          context.err.println("Another component with name '" + name + "' is already installed");
          return 1;
        }
        Class<?> pluginClass = null;
        PluginRepository plugins = (PluginRepository) registry.getComponent("pluginRepository");
        try {
          try {
            pluginClass = plugins != null ? plugins.loadClass(className) :
              Class.forName(className);
          } catch (ClassNotFoundException e) {
            String newClassName = "se.sics.mspsim.plugin." + className;
            pluginClass = plugins != null ? plugins.loadClass(newClassName) :
              Class.forName(newClassName);
          }
          Object component = pluginClass.newInstance();
          registry.registerComponent(name, component);
          return 0;
        } catch (Exception e1) {
          e1.printStackTrace(context.err);
        }
        return 1;
      }
    });
   
    handler.registerCommand("service", new BasicCommand("handle service plugins", "[-f] [class name|service name] [start|stop]") {
      @Override
      public int executeCommand(CommandContext context) {
        int index = 0;
        boolean verbose = true;
        if (context.getArgumentCount() > 0 && "-f".equals(context.getArgument(index))) {
          index++;
          verbose = false;
        }
        if (context.getArgumentCount() == index) {
          ServiceComponent[] sc = registry.getAllComponents(ServiceComponent.class);
          if (sc.length == 0) {
            context.out.println("No services found.");
          } else {
            for (ServiceComponent service : sc) {
              context.out.printf(" %-20s %s\n", service.getName(), service.getStatus());
            }
          }
          return 0;
        }
        String name = context.getArgument(index++);
        ServiceComponent sc = getServiceForName(registry, name);
        if (sc == null) {
          if (verbose) {
            context.err.println("could not find service '" + name + "'");
            return 1;
          }
          return 0;
        }
        if (context.getArgumentCount() == index) {
          context.out.printf(" %-20s %s\n", sc.getName(), sc.getStatus());
          return 0;
        }
        String operation = context.getArgument(index);
        if ("start".equals(operation)) {
          if (sc.getStatus() == ServiceComponent.Status.STARTED) {
            context.out.println("service " + sc.getName() + " already started");
          } else {
            sc.start();
            context.out.println("service " + sc.getName() + " started");
          }
          return 0;
        }
        if ("stop".equals(operation)) {
          if (sc.getStatus() == ServiceComponent.Status.STOPPED) {
            context.out.println("service " + sc.getName() + " already stopped");
          } else {
            sc.stop();
            context.out.println("service " + sc.getName() + " stopped");
          }
          return 0;
        }
        context.err.println("unknown operation '" + operation + "'");
        return 1;
      }
    });

    handler.registerCommand("rflistener", new BasicLineCommand("an rflistener", "<input|output> <rf-chip>") {
      CommandContext context;
      RFSource source;
      RFListener listener;
      final MSP430 cpu = registry.getComponent(MSP430.class);
      public int executeCommand(CommandContext ctx) {
        this.context = ctx;
        String inout = context.getArgument(0);
        Chip chip = cpu.getChip(context.getArgument(1));
        if (chip == null) {
          context.err.println("Error: could not find chip '" + context.getArgument(1) + '\'');
          return 1;
        }
        if ("output".equals(inout)) {
View Full Code Here


    return registry.getComponent(ELF.class);
  }

  public void setupCommands(ComponentRegistry registry, CommandHandler ch) {
    this.registry = registry;
    final MSP430 cpu = registry.getComponent(MSP430.class);
    final GenericNode node = registry.getComponent(GenericNode.class, "node");
    if (cpu != null) {
      ch.registerCommand("break", new BasicAsyncCommand("add a breakpoint to a given address or symbol",
          "<address or symbol>") {
        private int address;
        private MemoryMonitor monitor;
        public int executeCommand(final CommandContext context) {
          address = context.getArgumentAsAddress(0);
          if (address < 0) {
            context.err.println("unknown symbol: " + context.getArgument(0));
            return 1;
          }
          monitor = new MemoryMonitor.Adapter() {
              private long lastCycles = -1;
              @Override
              public void notifyReadBefore(int address, AccessMode mode, AccessType type) {
                  if (type == AccessType.EXECUTE && cpu.cycles != lastCycles) {
                      context.out.println("*** Break at $" + cpu.getAddressAsString(address));
                      cpu.triggBreakpoint();
                      lastCycles = cpu.cycles;
                  }
              }
          };
          cpu.addWatchPoint(address, monitor);
          context.err.println("Breakpoint set at $" + cpu.getAddressAsString(address));
          return 0;
        }
        public void stopCommand(CommandContext context) {
          cpu.removeWatchPoint(address, monitor);
        }
      });

      ch.registerCommand("watch",
          new BasicAsyncCommand("add a write/read watch to a given address or symbol", "<address or symbol> [length] [char | hex | break]") {
        int mode = 0;
        int address = 0;
        int length = 1;
        MemoryMonitor monitor;
        public int executeCommand(final CommandContext context) {
          address = context.getArgumentAsAddress(0);
          if (address < 0) {
            context.err.println("unknown symbol: " + context.getArgument(0));
            return -1;
          }
          if (context.getArgumentCount() > 1) {
              for (int i = 1; i < context.getArgumentCount(); i++) {
                  String modeStr = context.getArgument(i);
                  if (Character.isDigit(modeStr.charAt(0))) {
                      length = Integer.parseInt(modeStr);
                  } else if ("char".equals(modeStr)) {
                      mode = Utils.ASCII_UNMODIFIED; // 4
                  } else if ("break".equals(modeStr)) {
                      mode = 10;
                  } else if ("hex".equals(modeStr)) {
                      mode = Utils.HEX; // 2
                  }
              }
          }
          if (length < 1) {
              context.err.println("please specify a length of at least one byte");
              return -1;
          }
          monitor = new MemoryMonitor.Adapter() {
              private void cpuAction(AccessType type, int adr, int data) {
                  if (mode == 0 || mode == 10) {
                      int pc = cpu.getPC();
                      String adrStr = getSymOrAddr(cpu, context, adr);
                      String pcStr = getSymOrAddrELF(cpu, getELF(), pc);
                      String op = "op";
                      if (type == AccessType.READ) {
                          op = "Read";
                      } else if (type == AccessType.WRITE){
                          op = "Write";
                      }
                      context.out.println("*** " + op + " from " + pcStr +
                              ": " + adrStr + " = 0x" + Utils.hex(data, 4));
                      if (mode == 10) {
                          cpu.triggBreakpoint();
                      }
                  } else {
                      if (length > 1) {
                          Memory mem = cpu.getMemory();
                          for (int i = address; i < address + length; i++) {
                              context.out.print(Utils.toString(mem.get(i, AccessMode.BYTE), Utils.BYTE, mode));
                          }
                          context.out.println();
                      } else {
                          context.out.print(Utils.toString(data, Utils.BYTE, mode));
                      }
                  }
              }

            @Override
            public void notifyReadBefore(int addr, AccessMode mode, AccessType type) {
                cpuAction(AccessType.READ, addr, cpu.getMemory().get(addr, mode));
            }
            @Override
            public void notifyWriteBefore(int dstAddress, int data, AccessMode mode) {
                cpuAction(AccessType.WRITE, dstAddress, data);
            }
          };

          for (int i = 0; i < length; i++) {
              cpu.addWatchPoint(address + i, monitor);
          }
          if (length > 1) {
              context.err.println("Watch set at $" + cpu.getAddressAsString(address) + " - $" + cpu.getAddressAsString(address + length - 1));
          } else {
              context.err.println("Watch set at $" + cpu.getAddressAsString(address));
          }
          return 0;
        }

        public void stopCommand(CommandContext context) {
            for (int i = 0; i < length; i++) {
                cpu.removeWatchPoint(address + i, monitor);
            }
            context.exit(0);
        }
      });

      ch.registerCommand("watchreg",
          new BasicAsyncCommand("add a write watch to a given register", "<register> [int]") {
        int watchMode = 0;
        int register = 0;
        RegisterMonitor monitor;
        public int executeCommand(final CommandContext context) {
          register = context.getArgumentAsRegister(0);
          if (register < 0) {
            return -1;
          }
          if (context.getArgumentCount() > 1) {
            String modeStr = context.getArgument(1);
            if ("int".equals(modeStr)) {
              watchMode = 1;
            } else {
              context.err.println("illegal argument: " + modeStr);
              return -1;
            }
          }
          monitor = new RegisterMonitor.Adapter() {
            @Override
            public void notifyWriteBefore(int register, int data, AccessMode mode) {
                if (watchMode == 0) {
                    int pc = cpu.getPC();
                    String adrStr = getRegisterName(register);
                    String pcStr = getSymOrAddrELF(cpu, getELF(), pc);
                    context.out.println("*** Write from " + pcStr +
                            ": " + adrStr + " = " + data);
                } else {
                    context.out.println(data);
                }
            }
          };
          cpu.addRegisterWriteMonitor(register, monitor);
          context.err.println("Watch set for register " + getRegisterName(register));
          return 0;
        }

        public void stopCommand(CommandContext context) {
          cpu.removeRegisterWriteMonitor(register, monitor);
        }
      });

//      ch.registerCommand("clear", new BasicCommand("clear a breakpoint or watch from a given address or symbol", "<address or symbol>") {
//        public int executeCommand(final CommandContext context) {
//          int baddr = context.getArgumentAsAddress(0);
//          cpu.setBreakPoint(baddr, null);
//          return 0;
//        }
//      });

      ch.registerCommand("symbol", new BasicCommand("list matching symbols", "<regexp>") {
        public int executeCommand(final CommandContext context) {
          String regExp = context.getArgument(0);
          MapEntry[] entries = context.getMapTable().getEntries(regExp);
          if (entries.length == 0) {
              context.err.println("Could not find any symbols matching '" + regExp + '\'');
          } else {
              for (MapEntry mapEntry : entries) {
                  int address = mapEntry.getAddress();
                  String file = mapEntry.getFile();
                  if (file == null) {
                      file = "(unspecified)";
                  }
                  context.out.println(" " + mapEntry.getName() + " at $"
                          + cpu.getAddressAsString(address) + " ($"
                          + Utils.hex8(cpu.getMemory().get(address, AccessMode.BYTE))
                          + ' ' + Utils.hex8(cpu.getMemory().get(address + 1, AccessMode.BYTE)) + ") "
                          + mapEntry.getType() + " in file " + file);
              }
          }
          return 0;
        }
      });

      ch.registerCommand("debug", new BasicCommand("set debug to on or off", "[0/1]") {
          public int executeCommand(final CommandContext context) {
              if (context.getArgumentCount() > 0) {
                  cpu.setDebug(context.getArgumentAsBoolean(0));
              }
              context.out.println("Debug is set to " + cpu.getDebug());
              return 0;
          }
      });

      ch.registerCommand("line", new BasicCommand("print line number of address/symbol", "<address or symbol>") {
        public int executeCommand(final CommandContext context) {
          int adr = context.getArgumentAsAddress(0);
          DebugInfo di = getELF().getDebugInfo(adr);
          if (di == null) {
            /* quick hack to test next address too... - since something seems to be off by one sometimes... */
            di = getELF().getDebugInfo(adr + 1);
          }
          if (di != null) {
            di.getLine();
            context.out.println(di);
          } else {
            context.err.println("No line number found for: " + context.getArgument(0));
          }
          return 0;
        }
      });

      if (node != null) {
        ch.registerCommand("stop", new BasicCommand("stop the CPU", "") {
          public int executeCommand(CommandContext context) {
            if (!cpu.isRunning()) {
                context.err.println("CPU is not running");
                return 1;
            }
            node.stop();
            context.out.println("CPU stopped at: $" + cpu.getAddressAsString(cpu.getPC()));
            return 0;
          }
        });
        ch.registerCommand("start", new BasicCommand("start the CPU", "") {
          public int executeCommand(CommandContext context) {
            if (cpu.isRunning()) {
                context.err.println("cpu already running");
                return 1;
            }
            node.start();
            return 0;
          }
        });
        ch.registerCommand("throw", new BasicCommand("throw an Emulation Exception", "[message]") {
            public int executeCommand(CommandContext context) {
                final String msg = context.getArgumentCount() > 0 ? context.getArgument(0) : "by request";
                cpu.scheduleCycleEvent(new TimeEvent(0, "EmulationException") {
                    @Override public void execute(long t) {
                        throw new EmulationException(msg);
                    }}, cpu.cycles);
                return 0;
            }
        });

        ch.registerCommand("step", new BasicCommand("single step the CPU", "[number of instructions]") {
          public int executeCommand(CommandContext context) {
            int nr = context.getArgumentCount() > 0 ? context.getArgumentAsInt(0) : 1;
            long cyc = cpu.cycles;
            if (cpu.isRunning()) {
                context.err.println("Can not single step when emulation is running.");
                return -1;
            }
            try {
              node.step(nr);
            } catch (Exception e) {
              e.printStackTrace(context.out);
            }
            context.out.println("CPU stepped to: $" + cpu.getAddressAsString(cpu.getPC()) +
                " in " + (cpu.cycles - cyc) + " cycles (" + cpu.cycles + ")");
            return 0;
          }
        });

        ch.registerCommand("stepmicro", new BasicCommand("single the CPU specified no micros", "<micro skip> <micro step>") {
          public int executeCommand(CommandContext context) {
            long cyc = cpu.cycles;
            if (cpu.isRunning()) {
                context.err.println("Can not single step when emulation is running.");
                return -1;
            }
            long nxt = 0;
            try {
              nxt = cpu.stepMicros(context.getArgumentAsLong(0), context.getArgumentAsLong(1));
            } catch (Exception e) {
              e.printStackTrace(context.out);
            }
            context.out.println("CPU stepped to: $" + cpu.getAddressAsString(cpu.getPC()) +
                " in " + (cpu.cycles - cyc) + " cycles (" + cpu.cycles + ") - next exec time: " + nxt);
            return 0;
          }
        });

        ch.registerCommand("stack", new BasicCommand("show stack info", "") {
          public int executeCommand(CommandContext context) {
            int stackEnd = context.getMapTable().heapStartAddress;
            int stackStart = context.getMapTable().stackStartAddress;
            int current = cpu.getSP();
            context.out.println("Current stack: $" + cpu.getAddressAsString(current) + " (" + (stackStart - current) + " used of " + (stackStart - stackEnd) + ')');
            return 0;
          }
        });
        ch.registerCommand("print", new BasicCommand("print value of an address or symbol", "<address or symbol>") {
          public int executeCommand(CommandContext context) {
            int adr = context.getArgumentAsAddress(0);
            if (adr >= 0) {
              int value = cpu.memory[adr];
              if (adr >= 0x100 && adr + 1 < cpu.MAX_MEM) {
                  value |= cpu.memory[adr + 1] << 8;
              }
              context.out.println(context.getArgument(0) + " = $" + Utils.hex16(value));
              return 0;
            }
            context.err.println("unknown symbol: " + context.getArgument(0));
            return 1;
          }
        });
        ch.registerCommand("printreg", new BasicCommand("print value of an register", "[register]") {
          public int executeCommand(CommandContext context) {
              if (context.getArgumentCount() > 0) {
                  for (int i = 0, n = context.getArgumentCount(); i < n; i++) {
                      int register = context.getArgumentAsRegister(i);
                      if (i > 0) {
                          context.out.print((i % 6) == 0 ? "\n" : " ");
                      }
                      if (register >= 0) {
                          context.out.print(getRegisterName(i) + "=$" + Utils.hex(cpu.getRegister(register), 4));
                      } else {
                          context.out.print(context.getArgument(i) + "=<not a register>");
                      }
                  }
              } else {
                  for (int i = 0; i < 16; i++) {
                      if (i > 0) {
                          context.out.print((i % 6) == 0 ? "\n" : " ");
                      }
                      context.out.print(getRegisterName(i) + "=$" + Utils.hex(cpu.getRegister(i), 4));
                  }
              }
              context.out.println();
              return 0;
          }
        });
        ch.registerCommand("reset", new BasicCommand("reset the CPU", "") {
          public int executeCommand(CommandContext context) {
            cpu.reset();
            return 0;
          }
        });

        ch.registerCommand("time", new BasicCommand("print the elapse time and cycles", "") {
          public int executeCommand(CommandContext context) {
            long time = (long)cpu.getTimeMillis();
      long wallDiff = System.currentTimeMillis() - lastWall;
            context.out.println("Emulated time elapsed: " + time + "(ms)  since last: " + (time - lastCall) + " ms" + " wallTime: " +
        wallDiff + " ms speed factor: " +
        (wallDiff == 0 ? "N/A" : "" + (time - lastCall) / wallDiff));
            lastCall = time;
            lastWall = System.currentTimeMillis();
            return 0;
          }
        });

        ch.registerCommand("mem", new BasicCommand("dump memory", "<start address> <num_entries> [type] [hex|char|dis]") {
          public int executeCommand(final CommandContext context) {
            int start = context.getArgumentAsAddress(0);
            if (start < 0) {
              context.err.println("Illegal start address: "
                                  + context.getArgument(0));
              return 1;
            }
            int count = context.getArgumentAsInt(1);
            int mode = Utils.DEC;
            int type = Utils.UBYTE;
            boolean signed = false;
            if (context.getArgumentCount() > 2) {
                int pos = 2;
                int acount = context.getArgumentCount();
                if (acount > 4) acount = 4;
                while (pos < acount) {
                    String tS = context.getArgument(pos++);
                    if ("ubyte".equals(tS)) {
                    } else if ("byte".equals(tS)) {
                        type = Utils.BYTE;
                    } else if ("word".equals(tS)) {
                        type = Utils.WORD;
                    } else if ("uword".equals(tS)) {
                        type = Utils.UWORD;
                    } else if ("hex".equals(tS)) {
                        mode = Utils.HEX;
                    } else if ("char".equals(tS)) {
                        mode = Utils.ASCII;
                        type = Utils.BYTE;
                    } else if ("dis".equals(tS)) {
                        mode = Utils.DIS_ASM;
                        type = Utils.WORD;
                    }
                }
            }
            // Does not yet handle signed data...
            DisAsm disAsm = cpu.getDisAsm();
            for (int i = 0; i < count; i++) {
                if (mode == Utils.DIS_ASM) {
                    DbgInstruction dbg = disAsm.disassemble(start, cpu.memory, cpu.reg, new DbgInstruction(),
                            0);
                    String fkn;
                    if ((fkn = dbg.getFunction()) != null) {
                        context.out.println("//// " + fkn);
                    }
                    context.out.println(dbg.getASMLine(false));
                    start += dbg.getSize();
                } else {
                    int data = 0;
                    data = cpu.memory[start++];
                    if (Utils.size(type) == 2) {
                        data = data  + (cpu.memory[start++] << 8);
                    }
                    context.out.print((mode != Utils.ASCII ? " " : "") +
                            Utils.toString(data, type, mode));
                }
            }
            context.out.println();
            return 0;
          }
        });

        ch.registerCommand("mset", new BasicCommand("set memory", "<address> [type] <value> [value ...]") {
          public int executeCommand(final CommandContext context) {
            int count = context.getArgumentCount();
            int adr = context.getArgumentAsAddress(0);
            String arg2 = context.getArgument(1);
            int type = Utils.BYTE;
            int mode = Utils.DEC;
            boolean typeRead = false;
            if (count > 2) {
              if ("char".equals(arg2)) {
                mode = Utils.ASCII;
                typeRead = true;
              }
              if ("word".equals(arg2)) {
                type = Utils.WORD;
                typeRead = true;
              }
            }
            for (int i = typeRead ? 2 : 1; i < count; i++) {
              if (mode == Utils.DEC) {
                int val = context.getArgumentAsInt(i);
                AccessMode accessMode = Utils.size(type) == 2 || val > 0xff ? AccessMode.WORD : AccessMode.BYTE;
                try {
                  cpu.getMemory().set(adr, val, accessMode);
                  adr += accessMode.bytes;
                } catch (EmulationException e) {
                  e.printStackTrace(context.out);
                }
              } else if (mode == Utils.ASCII) {
                String data = context.getArgument(i);
                Memory mem = cpu.getMemory();
                for (int j = 0; j < data.length(); j++) {
                  mem.set(adr++, data.charAt(j), AccessMode.BYTE);
                }
              }
            }
            return 0;
          }});

        /******************************************************
         * handle external memory (flash, etc).
         ******************************************************/
        ch.registerCommand("xmem", new BasicCommand("dump flash memory", "<start address> <num_entries> [type]") {
          public int executeCommand(final CommandContext context) {
            se.sics.mspsim.chip.Memory xmem = DebugCommands.this.registry.getComponent(se.sics.mspsim.chip.Memory.class, "xmem");
            if (xmem == null) {
              context.err.println("No xmem component registered");
              return 0;
            }
            int start = context.getArgumentAsAddress(0);
            int count = context.getArgumentAsInt(1);
            int size = 1; // unsigned byte
            boolean signed = false;
            if (context.getArgumentCount() > 2) {
              String tS = context.getArgument(2);
              if ("byte".equals(tS)) {
                signed = true;
              } else if ("word".equals(tS)) {
                signed = true;
                size = 2;
              } else if ("uword".equals(tS)) {
                size = 2;
              }
            }
            // Does not yet handle signed data...
            for (int i = 0; i < count; i++) {
              int data = 0;
              data = xmem.readByte(start++);
              if (size == 2) {
                data = data  + (xmem.readByte(start++) << 8);
              }
              context.out.print(" " + data);
            }
            context.out.println();
            return 0;
          }
        });

        ch.registerCommand("xmset", new BasicCommand("set memory", "<address> <value> [type]") {
          public int executeCommand(final CommandContext context) {
            se.sics.mspsim.chip.Memory xmem = DebugCommands.this.registry.getComponent(se.sics.mspsim.chip.Memory.class, "xmem");
            if (xmem == null) {
              context.err.println("No xmem component registered");
              return 0;
            }
            int adr = context.getArgumentAsAddress(0);
            int val = context.getArgumentAsInt(1);
            boolean word = val > 0xff;
            if (word) {
              xmem.writeByte(adr, val >> 8);
              val = val & 0xff;
              adr++;
            }
            xmem.writeByte(adr, val & 0xff);
            return 0;
          }});

        ch.registerCommand("gdbstubs", new BasicCommand("open up a gdb stubs server for GDB remote debugging", "port") {
          private GDBStubs stubs = null;
          public int executeCommand(CommandContext context) {
            if (stubs != null) {
              context.err.println("GDBStubs already open");
            } else {
              int port = context.getArgumentAsInt(0);
              stubs = new GDBStubs();
              stubs.setupServer(cpu, port);
            }
            return 0;
          }
        });

        ch.registerCommand("log", new BasicAsyncCommand("log a loggable object", "[loggable...]" ) {
            private Loggable[] logs;
            private int[] logLevels;
            private LogListener logListener;

            @Override
            public int executeCommand(final CommandContext context) {
                if (context.getArgumentCount() == 0) {
                    Loggable[] loggable = cpu.getLoggables();
                    for (Loggable unit : loggable) {
                        String id = unit.getID();
                        String name = unit.getName();
                        if (id == name) {
                            context.out.println("  " + id);
                        } else {
                            context.out.println("  " + id + " (" + name + ')');
                        }
                    }
                    context.exit(0);
                    return 0;
                }

                final Loggable[] logs = new Loggable[context.getArgumentCount()];
                for(int i = 0, n = context.getArgumentCount(); i < n; i++) {
                    logs[i] = cpu.getLoggable(context.getArgument(i));
                    if (logs[i] == null) {
                        context.err.println("Can not find loggable '" + context.getArgument(i) + '\'');
                        return 1;
                    }
                }
                logListener = new LogListener() {

                    boolean isLogging(Loggable source) {
                        for(Loggable log : logs) {
                            if (source == log) {
                                return true;
                            }
                        }
                        return false;
                    }

                    @Override
                    public void log(Loggable source, String message) {
                        if (isLogging(source)) {
                            context.out.println(source.getID() + ": " + message);
                        }
                    }

                    @Override
                    public void logw(Loggable source, WarningType type,
                            String message) throws EmulationException {
                        if (isLogging(source)) {
                            context.out.println("# " + source.getID() + "[" + type + "]: " + message);
                        }
                    }

                };
                this.logs = logs;
                cpu.getLogger().addLogListener(logListener);
                logLevels = new int[logs.length];
                int i = 0;
                for(Loggable log : logs) {
                    logLevels[i++] = log.getLogLevel();
                    log.setLogLevel(Loggable.DEBUG);
                }
                return 0;
            }

            public void stopCommand(CommandContext context) {
                if (logListener != null) {
                    cpu.getLogger().removeLogListener(logListener);
                    logListener = null;
                }
                if (logs != null) {
                    int i = 0;
                    for(Loggable log : logs) {
                        if (log.getLogLevel() == Loggable.DEBUG) {
                            log.setLogLevel(logLevels[i]);
                        }
                        i++;
                    }
                }
            }
        });

        ch.registerCommand("trace", new BasicCommand("store a trace of execution positions.", "[trace size | show]") {
            @Override
            public int executeCommand(CommandContext context) {
                if (context.getArgumentCount() > 0) {
                    if ("show".equals(context.getArgument(0))) {
                        int size = cpu.getTraceSize();
                        if (size > 0) {
                            DisAsm disAsm = cpu.getDisAsm();
                            for (int i = 0; i < size; i++) {
                                int pc = cpu.getBackTrace(size - 1 - i);
                                DbgInstruction inst = disAsm.getDbgInstruction(pc, cpu);
                                inst.setPos(pc);
                                context.out.println(inst.getASMLine(false));
                            }
                            return 0;
                        }
                    } else {
                        int newSize = context.getArgumentAsInt(0, -1);
                        if (newSize < 0) {
                            return 1;
                        }
                        cpu.setTrace(newSize);
                    }
                }
                context.out.println("Trace size is set to " + cpu.getTraceSize() + " positions.");
                return 0;
            }
        });

        ch.registerCommand("events", new BasicCommand("print event queues", "") {
            @Override
            public int executeCommand(CommandContext context) {
                cpu.printEventQueues(context.out);
              return 0;
            }
          });
      }
    }
View Full Code Here

//     System.out.println("Value: " + val);
//     }
//     System.out.println("T ^ T =>  " + (true ^ true) +
//            " T ^ F => " + (false ^ true));

    MSP430 cpu = new MSP430(0, new ComponentRegistry(), new MSP430f1611Config());
    int[] memory = cpu.memory;
    reader.readFile(memory, args[0]);
    cpu.reset();
    cpu.cpuloop();
  }
View Full Code Here

*
*/
public class ProfilerCommands implements CommandBundle {

  public void setupCommands(final ComponentRegistry registry, CommandHandler ch) {
    final MSP430 cpu = registry.getComponent(MSP430.class);
    if (cpu != null) {
      ch.registerCommand("profile", new BasicCommand("show profile information",
          "[-clear] [-sort column] [-showcallers] [regexp]") {
        public int executeCommand(final CommandContext context) {
          Profiler profiler = cpu.getProfiler();
          if (profiler == null) {
            context.err.println("No profiler found.");
            return 1;
          }
          String namematch = null;
          String sortMode = null;
          String showCaller = null;
          int i;

          for (i = 0; i < context.getArgumentCount(); i++) {
            String value = context.getArgument(i);
            if ("-clear".equals(value)) {
              profiler.clearProfile();
              context.out.println("Cleared profile information.");
              return 0;
            } else if ("-sort".equals(value)) {
              if (context.getArgumentCount() > i + 1) {
                sortMode = context.getArgument(i + 1);
                i++;
              } else {
                context.err.println("Missing mode argument for -sort.");
                return 1;
              }
            } else if ("-showcallers".equals(value)) {
              showCaller = value;
            } else if ("--".equals(value)) {
                /* Done with arguments */
                break;
            } else if (value.startsWith("-")) {
                /* Unknown option */
                context.err.println("Unknown option: " + value);
                return 1;
            } else {
                break;
            }
          }
          if (i < context.getArgumentCount()) {
              namematch = context.getArgument(i++);
              if (i < context.getArgumentCount()) {
                  // Multiple patterns
                  namematch = "(" + namematch;
                  for(; i < context.getArgumentCount(); i++) {
                      namematch += "|" + context.getArgument(i);
                  }
                  namematch += ')';
              }
          }

          Properties params = new Properties();
          if (namematch != null) {
            params.put(Profiler.PARAM_FUNCTION_NAME_REGEXP, namematch);
          }
          if (showCaller != null) {
            params.put(Profiler.PARAM_PROFILE_CALLERS, showCaller);
          }
          if (sortMode != null) {
            params.put(Profiler.PARAM_SORT_MODE, sortMode);
          }
          profiler.printProfile(context.out, params);
          return 0;
        }
      });

      ch.registerCommand("stacktrace", new BasicCommand("show stack trace", "") {
        public int executeCommand(CommandContext context) {
          Profiler profiler = cpu.getProfiler();
          if (profiler == null) {
            context.err.println("No profiler found.");
            return 1;
          }
          profiler.printStackTrace(context.out);
          return 0;
        }
      });

      ch.registerCommand("stackprof", new BasicCommand("Start stack profiler", "") {
          public int executeCommand(CommandContext context) {
              new StackMonitor(cpu);
              return 0;
          }
      });
     
//      ch.registerCommand("irqprofile", new BasicCommand("show interrupt profile", "") {
//        public int executeCommand(CommandContext context) {
//          long[] time = cpu.getInterruptTime();
//          long[] ctr = cpu.getInterruptCount();
//          context.out.println("Interrupt statistics");
//          context.out.println("Vector\tAvg\tCount");
//         
//          for (int i = 0; i < ctr.length; i++) {
//            long avg = ctr[i] != 0 ? (time[i] / ctr[i]) : 0;
//            context.out.println(i + "\t" + avg + "\t" + ctr[i]);
//          }
//          return 0;
//        }
//
//      });
     
      ch.registerCommand("logevents", new BasicAsyncCommand("log events", "[chips...]") {
        Chip[] chips;
        EventListener eventListener;
        public int executeCommand(final CommandContext context) {
            if (context.getArgumentCount() == 0) {
                context.out.println("Available chips:");
                for(Chip chip : cpu.getChips()) {
                    String id = chip.getID();
                    String name = chip.getName();
                    if (id == name) {
                        context.out.println("  " + id);
                    } else {
                        context.out.println("  " + id + " (" + name + ')');
                    }
                }
                context.exit(0);
                return 0;
            }
            chips = new Chip[context.getArgumentCount()];
            for(int i = 0, n = chips.length; i < n; i++) {
                chips[i] = cpu.getChip(context.getArgument(i));
                if (chips[i] == null) {
                    context.err.println("Can not find chip: " + context.getArgument(i));
                    return 1;
                }
            }
            eventListener = new EventListener() {
                public void event(EventSource source, String event, Object data) {
                    context.out.println("Event:" + source.getName() + ":" + event);
                }
            };
            for (Chip chip : chips) {
                chip.addEventListener(eventListener);
            }
            return 0;
        }
        public void stopCommand(CommandContext context) {
            for (Chip chip : chips) {
                chip.removeEventListener(eventListener);
            }
        }
      });

      ch.registerCommand("tagprof", new BasicCommand("profile between two events", "") {
        public int executeCommand(CommandContext context) {
          String event1 = context.getArgument(0);
          String event2 = context.getArgument(1);
          String chip1[] = event1.split("\\.");
          String chip2[] = event2.split("\\.");
          Chip chipE1 = cpu.getChip(chip1[0]);
          if (chipE1 == null) {
            context.err.println("Can not find chip: " + chip1[0]);
            return 1;
          }
          Chip chipE2 = cpu.getChip(chip2[0]);
          if (chipE2 == null) {
            context.err.println("Can not find chip: " + chip2[0]);
            return 1;
          }
          Profiler profiler = cpu.getProfiler();
          SimpleProfiler sprof = (SimpleProfiler) profiler;
          sprof.addProfileTag(context.getArgument(2), chipE1, chip1[1],
              chipE2, chip2[1]);
          return 0;
        }
      });     

      ch.registerCommand("printtags", new BasicCommand("print tags profile", "") {
        public int executeCommand(CommandContext context) {
          Profiler profiler = cpu.getProfiler();
          SimpleProfiler sprof = (SimpleProfiler) profiler;
          sprof.printTagProfile(context.out);
          return 0;
        }
      });     

     
      ch.registerCommand("logcalls", new BasicAsyncCommand("log function calls", "") {
        public int executeCommand(CommandContext context) {
          Profiler profiler = cpu.getProfiler();
          if (profiler == null) {
            context.err.println("No profiler found.");
            return 1;
          }
          profiler.setLogger(context.out);
          return 0;
        }
        public void stopCommand(CommandContext context) {
          Profiler profiler = cpu.getProfiler();
          if (profiler != null) {
            profiler.setLogger(null);
          }
        }
      });
     
      ch.registerCommand("profiler", new BasicCommand("configure profiler",
          "<command> <arguments>") {
            public int executeCommand(CommandContext context) {
              // TODO: add more API's to the Profiler???
              SimpleProfiler profiler = (SimpleProfiler) cpu.getProfiler();
              if (profiler == null) {
                context.err.println("No profiler found.");
                return 1;
              }
              String cmd = context.getArgument(0);
              if ("hide".equals(cmd)) {
                for (int j = 1, n = context.getArgumentCount(); j < n; j++) {
                  profiler.addIgnoreFunction(context.getArgument(j));
                }
              } else if ("hideirq".equals(cmd)) {
                profiler.setHideIRQ(context.getArgumentAsBoolean(1));
              }
              return 0;
            }
      });
     
      ch.registerCommand("readmap", new BasicAsyncCommand("read map", "") {
          private CPUHeatMap hm;

          public int executeCommand(CommandContext context) {
              hm = new CPUHeatMap(cpu, registry.getComponent(WindowManager.class));
              cpu.addGlobalMonitor(hm);
              return 0;
          }

          public void stopCommand(CommandContext context) {
              if (hm != null) {
                  cpu.removeGlobalMonitor(hm);
                  hm.close();
                  hm = null;
              }
          }
      });
View Full Code Here

  protected ELF elf;
  protected OperatingModeStatistics stats;


  public GenericNode(String id, MSP430Config config) {
    super(id, new MSP430(0, new ComponentRegistry(), config));
    this.cpu = (MSP430)super.cpu;
    this.registry = cpu.getRegistry();
  }
View Full Code Here

  private IPStack ipStack;
  public void setupCommands(final ComponentRegistry registry, CommandHandler handler) {
    handler.registerCommand("ipstack", new BasicLineCommand("setup 802.15.4/IP stack", "") {
      CC2420PacketHandler listener;
      public int executeCommand(CommandContext context) {
        MSP430 cpu = registry.getComponent(MSP430.class);
        listener = new CC2420PacketHandler(cpu);
        listener.setOutput(context.out);
        IEEE802154Handler ieeeHandler = new IEEE802154Handler();
        listener.addUpperLayerHandler(0, ieeeHandler);
        ieeeHandler.setLowerLayerHandler(listener);
View Full Code Here

      public void actionPerformed(ActionEvent e) {
        /* XXX Experimental */
        final int max = 10000;
        int count=0;
        int pc, instruction;
        MSP430 cpu = mspMote.getCPU();
        int depth = 0;

        /* Extract function name */
        DebugInfo debugInfo = mspMote.getELF().getDebugInfo(mspMote.getCPU().reg[MSP430.PC]);
        if (debugInfo == null || debugInfo.getFunction() == null) {
          logger.fatal("Unknown function");
          return;
        }
        String functionName = debugInfo.getFunction().split(":")[0];
        logger.info("Function: '" + functionName + "'");

        pc = cpu.readRegister(MSP430Core.PC);
        instruction = cpu.memory[pc] + (cpu.memory[pc + 1] << 8);
        if (instruction == MSP430.RETURN) {
          logger.fatal("Already at return instruction");
          return;
        }

        try {
          while (count++ < max) {
            cpu.step(mspMote.getCPU().cycles+1);
            pc = cpu.readRegister(MSP430Core.PC);
            instruction = cpu.memory[pc] + (cpu.memory[pc + 1] << 8);
            if ((instruction & 0xff80) == MSP430.CALL) {
              depth++;
            } else if (instruction == MSP430.RETURN) {
              depth--;
View Full Code Here

    }

    // Leave control to emulated CPU
    cycleCounter += 1;

    MSP430 cpu = getCPU();
    if (cpu.cycles > cycleCounter) {
      /* CPU already ticked too far - just wait it out */
      return true;
    }
    myMoteInterfaceHandler.doActiveActionsBeforeTick();

    /* Experimental program counter history */
    for (int i=pcHistory.length-1; i > 0; i--) {
      pcHistory[i] = pcHistory[i-1];
    }
    pcHistory[0] = cpu.reg[MSP430.PC];

    try {
      cpu.step(cycleCounter);
    } catch (EmulationException e) {
      if (e.getMessage().startsWith("Bad operation")) {
        /* Experimental: print program counter history */
        LineListener oldListener = commandListener;
        LineListener tmpListener = new LineListener() {
View Full Code Here

TOP

Related Classes of se.sics.mspsim.core.MSP430

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.