Esempio n. 1
0
  /**
   * If the argument is the <i>numberOfBits</i> parameter, then set the production rate of the
   * output port.
   *
   * @param attribute The attribute that has changed.
   * @exception IllegalActionException If the parameter is out of range.
   */
  public void attributeChanged(Attribute attribute) throws IllegalActionException {
    if (attribute == numberOfBits) {
      int rate = ((IntToken) numberOfBits.getToken()).intValue();

      if ((rate < 1) || (rate > 32)) {
        throw new IllegalActionException(this, "Invalid number of bits: " + rate);
      }
    } else {
      super.attributeChanged(attribute);
    }
  }
Esempio n. 2
0
  /**
   * Consume a single IntToken on the input. Produce <i>numberOfBits</i> BooleanTokens on the output
   * port which is the bitwise representation of the input IntToken. The most significant bit (the
   * sign bit) is the first boolean token send out. The least significant bit is the last boolean
   * token send out.
   *
   * @exception IllegalActionException If there is no director. or if the input integer is out of
   *     range.
   */
  public final void fire() throws IllegalActionException {
    super.fire();

    int rate = ((IntToken) numberOfBits.getToken()).intValue();
    BooleanToken[] bits = new BooleanToken[rate];
    IntToken token = (IntToken) (input.get(0));
    int integer = token.intValue();

    if (integer < 0) {
      if (integer < -(1 << (rate - 1))) {
        throw new IllegalActionException(this, "integer is out of range.");
      }

      bits[0] = new BooleanToken(true);

      // integer = (int)(2147483648L + integer);
      integer = ((1 << (rate - 1)) + integer);
    } else {
      if (integer > ((1 << (rate - 1)) - 1)) {
        throw new IllegalActionException(this, "integer is out of range.");
      }

      bits[0] = new BooleanToken(false);
    }

    for (int i = rate - 1; i > 0; i--) {
      int remainder = integer % 2;
      integer = integer / 2;

      if (remainder == 0) {
        bits[i] = new BooleanToken(false);
      } else {
        bits[i] = new BooleanToken(true);
      }
    }

    output.send(0, bits, bits.length);
  }