public static void writeStateSignals(
      EDComponent comp, StringBuilder sb, String name, String parentName) {
    for (Iterator<EDState> i = comp.state.iterator(); i.hasNext(); ) {
      EDState state = i.next();
      sb.append(
          "signal "
              + parentName
              + "_statevariable_"
              + state.type
              + "_"
              + name
              + state.name
              + "_out_int : sfixed ("
              + state.integer
              + " downto "
              + state.fraction
              + ");\r\n");
    }
    for (Iterator<EDDerivedVariable> i = comp.derivedvariables.iterator(); i.hasNext(); ) {
      EDDerivedVariable state = i.next();
      sb.append(
          "signal "
              + parentName
              + "_statevariable_"
              + state.type
              + "_"
              + name
              + state.name
              + "_out_int : sfixed ("
              + state.integer
              + " downto "
              + state.fraction
              + ");\r\n");
    }
    for (Iterator<EDConditionalDerivedVariable> i = comp.conditionalderivedvariables.iterator();
        i.hasNext(); ) {
      EDConditionalDerivedVariable state = i.next();
      sb.append(
          "signal "
              + parentName
              + "_statevariable_"
              + state.type
              + "_"
              + name
              + state.name
              + "_out_int : sfixed ("
              + state.integer
              + " downto "
              + state.fraction
              + ");\r\n");
    }

    for (Iterator<EDComponent> i = comp.Children.iterator(); i.hasNext(); ) {
      EDComponent item = i.next();
      String newName = name + item.name + "_";
      writeStateSignals(item, sb, newName, parentName);
    }
  }
  public static void writeConnectivityMap(
      EDComponent comp, StringBuilder sb, String name, String parentName) {
    for (Iterator<EDParameter> i = comp.parameters.iterator(); i.hasNext(); ) {
      EDParameter item = i.next();
      updateCurrentBits((item.integer) - (item.fraction) + 1, item, true);
      sb.append(
          "  "
              + parentName
              + "_param_"
              + item.type
              + "_"
              + name
              + item.name
              + " => resize("
              + "to_sfixed ( mega_bus_in("
              + currentTop
              + " downto "
              + currentBit
              + "),"
              + item.integer
              + ","
              + (item.integer - (item.busLength - 1))
              + "),"
              + item.integer
              + " , "
              + item.fraction
              + "),\r\n");
    }

    for (Iterator<EDState> i = comp.state.iterator(); i.hasNext(); ) {
      EDState state = i.next();
      updateCurrentBits((state.integer) - (state.fraction) + 1, state, true);
      sb.append(
          "  "
              + parentName
              + "_stateCURRENT_"
              + state.type
              + "_"
              + name
              + state.name
              + " => "
              + parentName
              + "_stateCURRENT_"
              + state.type
              + "_"
              + name
              + state.name
              + "_int,\r\n");
      sb.append(
          "  "
              + parentName
              + "_stateRESTORE_"
              + state.type
              + "_"
              + name
              + state.name
              + " => resize(to_sfixed (mega_bus_in("
              + currentTop
              + " downto "
              + currentBit
              + "),"
              + state.integer
              + ","
              + (state.integer - (state.busLength - 1))
              + "),"
              + state.integer
              + " , "
              + state.fraction
              + "),\r\n");
    }
    for (Iterator<EDDerivedVariable> i = comp.derivedvariables.iterator(); i.hasNext(); ) {
      EDDerivedVariable state = i.next();
      if (state.IsUsedForOtherDerivedVariables || state.ExposureIsUsed) {
        updateCurrentBits((state.integer) - (state.fraction) + 1, state, true);
        sb.append(
            "  "
                + parentName
                + "_stateCURRENT_"
                + state.type
                + "_"
                + name
                + state.name
                + " => "
                + parentName
                + "_stateCURRENT_"
                + state.type
                + "_"
                + name
                + state.name
                + "_int,\r\n");
        sb.append(
            "  "
                + parentName
                + "_stateRESTORE_"
                + state.type
                + "_"
                + name
                + state.name
                + " => resize(to_sfixed (mega_bus_in("
                + currentTop
                + " downto "
                + currentBit
                + "),"
                + state.integer
                + ","
                + (state.integer - (state.busLength - 1))
                + "),"
                + state.integer
                + " , "
                + state.fraction
                + "),\r\n");
      }
    }
    for (Iterator<EDConditionalDerivedVariable> i = comp.conditionalderivedvariables.iterator();
        i.hasNext(); ) {
      EDConditionalDerivedVariable state = i.next();
      if (state.IsUsedForOtherDerivedVariables || state.ExposureIsUsed) {
        updateCurrentBits((state.integer) - (state.fraction) + 1, state, true);
        sb.append(
            "  "
                + parentName
                + "_stateCURRENT_"
                + state.type
                + "_"
                + name
                + state.name
                + " => "
                + parentName
                + "_stateCURRENT_"
                + state.type
                + "_"
                + name
                + state.name
                + "_int,\r\n");
        sb.append(
            "  "
                + parentName
                + "_stateRESTORE_"
                + state.type
                + "_"
                + name
                + state.name
                + " => resize(to_sfixed (mega_bus_in("
                + currentTop
                + " downto "
                + currentBit
                + "),"
                + state.integer
                + ","
                + (state.integer - (state.busLength - 1))
                + "),"
                + state.integer
                + " , "
                + state.fraction
                + "),\r\n");
      }
    }

    for (Iterator<EDComponent> i = comp.Children.iterator(); i.hasNext(); ) {
      EDComponent item = i.next();
      String newName = name + item.name + "_";
      writeConnectivityMap(item, sb, newName, parentName);
    }
  }
  public static void writeStateToBusSignals(
      EDComponent comp, StringBuilder sb, String name, String parentName) {
    for (Iterator<EDState> i = comp.state.iterator(); i.hasNext(); ) {
      EDState state = i.next();
      updateCurrentBits((state.integer) - (state.fraction) + 1, state, false);
      sb.append(
          "mega_bus_out("
              + currentTop
              + " downto "
              + currentBit
              + ") <= to_slv(resize("
              + parentName
              + "_stateCURRENT_"
              + state.type
              + "_"
              + name
              + state.name
              + "_int,"
              + state.integer
              + ","
              + (state.integer - (state.busLength - 1))
              + "));\r\n");
    }
    for (Iterator<EDDerivedVariable> i = comp.derivedvariables.iterator(); i.hasNext(); ) {
      EDDerivedVariable state = i.next();
      if (state.IsUsedForOtherDerivedVariables || state.ExposureIsUsed) {
        updateCurrentBits((state.integer) - (state.fraction) + 1, state, false);
        sb.append(
            "mega_bus_out("
                + currentTop
                + " downto "
                + currentBit
                + ") <= to_slv(resize("
                + parentName
                + "_stateCURRENT_"
                + state.type
                + "_"
                + name
                + state.name
                + "_int,"
                + state.integer
                + ","
                + (state.integer - (state.busLength - 1))
                + "));\r\n");
      }
    }
    for (Iterator<EDConditionalDerivedVariable> i = comp.conditionalderivedvariables.iterator();
        i.hasNext(); ) {
      EDConditionalDerivedVariable state = i.next();
      if (state.IsUsedForOtherDerivedVariables || state.ExposureIsUsed) {
        updateCurrentBits((state.integer) - (state.fraction) + 1, state, false);
        sb.append(
            "mega_bus_out("
                + currentTop
                + " downto "
                + currentBit
                + ") <= to_slv(resize("
                + parentName
                + "_stateCURRENT_"
                + state.type
                + "_"
                + name
                + state.name
                + "_int,"
                + state.integer
                + ","
                + (state.integer - (state.busLength - 1))
                + "));\r\n");
      }
    }

    for (Iterator<EDComponent> i = comp.Children.iterator(); i.hasNext(); ) {
      EDComponent item = i.next();
      String newName = name + item.name + "_";
      writeStateToBusSignals(item, sb, newName, parentName);
    }
  }
  public static void writeConnectivityMapPar(
      EDComponent comp, StringBuilder sb, String name, String parentName, JsonGenerator g)
      throws JsonGenerationException, IOException {
    for (Iterator<EDParameter> i = comp.parameters.iterator(); i.hasNext(); ) {
      EDParameter item = i.next();
      currentBit = currentBit + currentLength + 1;
      while (currentBit % 8 != 0) currentBit++;
      currentLength = (item.integer) - (item.fraction);
      int currentTop = currentBit + currentLength;
      writeSignal(
          parentName + "_param_" + item.type + "_" + name + item.name,
          currentBit,
          currentTop,
          item.integer,
          item.fraction,
          g);
      sb.append(
          parentName
              + "_param_"
              + item.type
              + "_"
              + name
              + item.name
              + " => to_sfixed ( mega_bus_in("
              + currentTop
              + " downto "
              + currentBit
              + "),"
              + item.integer
              + " , "
              + item.fraction
              + "),\r\n");
    }

    for (Iterator<EDComponent> i = comp.Children.iterator(); i.hasNext(); ) {
      EDComponent item = i.next();
      if (!item.isSynapse) {
        String newName = name + item.name + "_";
        writeConnectivityMapPar(item, sb, newName, parentName, g);
      }
    }

    boolean hasSynapses = false;
    for (Iterator<EDComponent> i = comp.Children.iterator(); i.hasNext(); ) {
      EDComponent item = i.next();
      if (item.isSynapse) {
        hasSynapses = true;
      }
    }
    if (hasSynapses == true) {
      int count = 0;
      for (Iterator<EDComponent> i = comp.Children.iterator(); i.hasNext(); ) {
        EDComponent item = i.next();
        if (item.isSynapse) {
          g.writeEndArray();
          g.writeArrayFieldStart("SynapseParameters_" + item.name);
          String newName = name + item.name + "_";
          writeConnectivityMapVar(item, sb, newName, parentName, g);
        }
      }
    }
  }
  public static void writeConnectivityMapVar(
      EDComponent comp, StringBuilder sb, String name, String parentName, JsonGenerator g)
      throws JsonGenerationException, IOException {

    for (Iterator<EDState> i = comp.state.iterator(); i.hasNext(); ) {
      EDState state = i.next();
      currentBit = currentBit + currentLength + 1;
      while (currentBit % 8 != 0) currentBit++;
      currentLength = (state.integer) - (state.fraction);
      int currentTop = currentBit + currentLength;
      writeSignal(
          parentName + "_statevariable_" + state.type + "_" + name + state.name + "_in",
          currentBit,
          currentTop,
          state.integer,
          state.fraction,
          g);
      sb.append(
          parentName
              + "_statevariable_"
              + state.type
              + "_"
              + name
              + state.name
              + "_out => "
              + parentName
              + "_statevariable_"
              + state.type
              + "_"
              + name
              + state.name
              + "_out_int,\r\n");
      sb.append(
          parentName
              + "_statevariable_"
              + state.type
              + "_"
              + name
              + state.name
              + "_in => to_sfixed (mega_bus_in("
              + currentTop
              + " downto "
              + currentBit
              + "),"
              + state.integer
              + " , "
              + state.fraction
              + "),\r\n");
    }
    for (Iterator<EDDerivedVariable> i = comp.derivedvariables.iterator(); i.hasNext(); ) {
      EDDerivedVariable state = i.next();
      currentBit = currentBit + currentLength + 1;
      while (currentBit % 8 != 0) currentBit++;
      currentLength = (state.integer) - (state.fraction);
      int currentTop = currentBit + currentLength;
      writeSignal(
          parentName + "_statevariable_" + state.type + "_" + name + state.name + "_in",
          currentBit,
          currentTop,
          state.integer,
          state.fraction,
          g);
      sb.append(
          parentName
              + "_statevariable_"
              + state.type
              + "_"
              + name
              + state.name
              + "_out => "
              + parentName
              + "_statevariable_"
              + state.type
              + "_"
              + name
              + state.name
              + "_out_int,\r\n");
      sb.append(
          parentName
              + "_statevariable_"
              + state.type
              + "_"
              + name
              + state.name
              + "_in => to_sfixed (mega_bus_in("
              + currentTop
              + " downto "
              + currentBit
              + "),"
              + state.integer
              + " , "
              + state.fraction
              + "),\r\n");
    }
    for (Iterator<EDConditionalDerivedVariable> i = comp.conditionalderivedvariables.iterator();
        i.hasNext(); ) {
      EDConditionalDerivedVariable state = i.next();
      currentBit = currentBit + currentLength + 1;
      while (currentBit % 8 != 0) currentBit++;
      currentLength = (state.integer) - (state.fraction);
      int currentTop = currentBit + currentLength;
      writeSignal(
          parentName + "_statevariable_" + state.type + "_" + name + state.name + "_in",
          currentBit,
          currentTop,
          state.integer,
          state.fraction,
          g);
      sb.append(
          parentName
              + "_statevariable_"
              + state.type
              + "_"
              + name
              + state.name
              + "_out => "
              + parentName
              + "_statevariable_"
              + state.type
              + "_"
              + name
              + state.name
              + "_out_int,\r\n");
      sb.append(
          parentName
              + "_statevariable_"
              + state.type
              + "_"
              + name
              + state.name
              + "_in => to_sfixed (mega_bus_in("
              + currentTop
              + " downto "
              + currentBit
              + "),"
              + state.integer
              + " , "
              + state.fraction
              + "),\r\n");
    }

    for (Iterator<EDComponent> i = comp.Children.iterator(); i.hasNext(); ) {
      EDComponent item = i.next();
      if (!item.isSynapse) {
        String newName = name + item.name + "_";
        writeConnectivityMapVar(item, sb, newName, parentName, g);
      }
    }

    boolean hasSynapses = false;
    for (Iterator<EDComponent> i = comp.Children.iterator(); i.hasNext(); ) {
      EDComponent item = i.next();
      if (item.isSynapse) {
        hasSynapses = true;
      }
    }
    if (hasSynapses == true) {
      for (Iterator<EDComponent> i = comp.Children.iterator(); i.hasNext(); ) {
        EDComponent item = i.next();
        if (item.isSynapse) {
          g.writeEndArray();
          g.writeArrayFieldStart("SynapseVariablesIn_" + item.name);
          String newName = name + item.name + "_";
          writeConnectivityMapVar(item, sb, newName, parentName, g);
        }
      }
    }
  }