Jelajahi Sumber

implemeting DOUT, PWM, SRV

Wilfried Klaas 6 tahun lalu
induk
melakukan
94218c453b

+ 25 - 2
src/main/java/de/mcs/tools/sps/mnemonic/DOUT.java

@@ -21,22 +21,45 @@
  */
 package de.mcs.tools.sps.mnemonic;
 
+import org.apache.commons.lang3.StringUtils;
+
+import de.mcs.tools.sps.exceptions.IllegalArgument;
 import de.mcs.tools.sps.exceptions.SyntaxError;
 
 /**
+ * DOUT, DOUT #x: Output of register A. If no #x given, register a is output
+ * fully, otherwise the lowest bit of A will be shon on output #x. #x in range
+ * 1..4
+ * 
  * @author wklaa_000
  *
  */
 public class DOUT extends AbstractMnemonic implements Mnemonic {
 
+  byte value;
+
   public DOUT(String line) throws SyntaxError {
     super(line);
+    value = 0;
+  }
+
+  @Override
+  public void checkArgument() throws SyntaxError {
+    if (StringUtils.isEmpty(getArgument())) {
+      value = 0;
+    } else {
+      int myValue = getArgumentAsNumber();
+      if ((myValue < 1) || (myValue > 4)) {
+        throw new IllegalArgument(getLineNumber(),
+            String.format("argument %s is not in range 1..4 for %s.", getArgument(), this.getClass().getSimpleName()));
+      }
+      value = (byte) (myValue & 0xFF);
+    }
   }
 
   @Override
   public int getByte() {
-    // TODO modifier x (0, 1..4)
-    return 0x54;
+    return 0x54 + value;
   }
 
 }

+ 0 - 1
src/main/java/de/mcs/tools/sps/mnemonic/PORT.java

@@ -57,7 +57,6 @@ public class PORT extends AbstractMnemonic implements Mnemonic {
 
   @Override
   public int getByte() {
-    // TODO modifier x (0..15)
     return 0x10 + output;
   }
 

+ 23 - 2
src/main/java/de/mcs/tools/sps/mnemonic/PWM.java

@@ -21,22 +21,43 @@
  */
 package de.mcs.tools.sps.mnemonic;
 
+import org.apache.commons.lang3.StringUtils;
+
+import de.mcs.tools.sps.exceptions.IllegalArgument;
 import de.mcs.tools.sps.exceptions.SyntaxError;
 
 /**
+ * PWM #x: output register A to PWM #x. x should be 1 or 2
+ * 
  * @author wklaa_000
  *
  */
 public class PWM extends AbstractMnemonic implements Mnemonic {
 
+  byte value;
+
   public PWM(String line) throws SyntaxError {
     super(line);
+    value = 0;
+  }
+
+  @Override
+  public void checkArgument() throws SyntaxError {
+    if (StringUtils.isEmpty(getArgument())) {
+      throw new SyntaxError(getLineNumber(),
+          String.format("missing argument for %s.", this.getClass().getSimpleName()));
+    }
+    int myValue = getArgumentAsNumber();
+    if ((myValue < 1) || (myValue > 2)) {
+      throw new IllegalArgument(getLineNumber(),
+          String.format("argument %s is not in range 1..4 for %s.", getArgument(), this.getClass().getSimpleName()));
+    }
+    value = (byte) ((myValue - 1) & 0xFF);
   }
 
   @Override
   public int getByte() {
-    // TODO modifier x (1,2)
-    return 0x59;
+    return 0x59 + value;
   }
 
 }

+ 0 - 1
src/main/java/de/mcs/tools/sps/mnemonic/RJMP.java

@@ -63,7 +63,6 @@ public class RJMP extends AbstractMnemonic implements Mnemonic {
 
   @Override
   public int getByte() {
-    // TODO modifier x (0..15)
     return 0x30 + value;
   }
 

+ 0 - 1
src/main/java/de/mcs/tools/sps/mnemonic/SET.java

@@ -57,7 +57,6 @@ public class SET extends AbstractMnemonic implements Mnemonic {
 
   @Override
   public int getByte() {
-    // TODO modifier x (0..15)
     return 0x40 + value;
   }
 

+ 23 - 2
src/main/java/de/mcs/tools/sps/mnemonic/SRV.java

@@ -21,22 +21,43 @@
  */
 package de.mcs.tools.sps.mnemonic;
 
+import org.apache.commons.lang3.StringUtils;
+
+import de.mcs.tools.sps.exceptions.IllegalArgument;
 import de.mcs.tools.sps.exceptions.SyntaxError;
 
 /**
+ * SRV #x: output register A to SRV #x. x should be 1 or 2
+ * 
  * @author wklaa_000
  *
  */
 public class SRV extends AbstractMnemonic implements Mnemonic {
 
+  byte value;
+
   public SRV(String line) throws SyntaxError {
     super(line);
+    value = 0;
+  }
+
+  @Override
+  public void checkArgument() throws SyntaxError {
+    if (StringUtils.isEmpty(getArgument())) {
+      throw new SyntaxError(getLineNumber(),
+          String.format("missing argument for %s.", this.getClass().getSimpleName()));
+    }
+    int myValue = getArgumentAsNumber();
+    if ((myValue < 1) || (myValue > 2)) {
+      throw new IllegalArgument(getLineNumber(),
+          String.format("argument %s is not in range 1..4 for %s.", getArgument(), this.getClass().getSimpleName()));
+    }
+    value = (byte) ((myValue - 1) & 0xFF);
   }
 
   @Override
   public int getByte() {
-    // TODO modifier x (1,2)
-    return 0x5B;
+    return 0x5B + value;
   }
 
 }

+ 0 - 1
src/main/java/de/mcs/tools/sps/mnemonic/WAIT.java

@@ -102,7 +102,6 @@ public class WAIT extends AbstractMnemonic implements Mnemonic {
 
   @Override
   public int getByte() {
-    // TODO modifier x (0..15)
     return 0x20 + output;
   }
 

+ 64 - 0
src/test/java/de/mcs/tools/sps/mnemonic/TestDOUT.java

@@ -0,0 +1,64 @@
+package de.mcs.tools.sps.mnemonic;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import de.mcs.tools.sps.exceptions.IllegalArgument;
+import de.mcs.tools.sps.exceptions.SyntaxError;
+
+class TestDOUT {
+
+  private static final int BYTE_VALUE = 0x54;
+  private final String MNEMONIC = "DOUT";
+
+  @Test
+  void testNOP() throws SyntaxError {
+    DOUT mno = new DOUT(MNEMONIC);
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE, mno.getByte());
+
+    mno = new DOUT(MNEMONIC + " 0x01");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE + 1, mno.getByte());
+
+    mno = new DOUT(MNEMONIC + " 0x04");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE + 0x04, mno.getByte());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      DOUT mno1 = new DOUT(MNEMONIC + " 0x05");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(IllegalArgument.class, () -> {
+      PORT mno1 = new PORT(MNEMONIC + " 0x10");
+      mno1.checkArgument();
+    });
+  }
+
+  @Test
+  void testMnemonicFactory() throws SyntaxError {
+    Mnemonic mnemonic = MnemonicFactory.getMnemonic(MNEMONIC, 0);
+    assertEquals(DOUT.class, mnemonic.getClass());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      MnemonicFactory.getMnemonic(MNEMONIC + " 0x00", 0);
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      MnemonicFactory.getMnemonic(MNEMONIC + " 5", 0);
+    });
+
+    mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 0x02", 0);
+    assertEquals(BYTE_VALUE + 0x02, mnemonic.getByte());
+
+    mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 3", 0);
+    assertEquals(BYTE_VALUE + 3, mnemonic.getByte());
+
+    mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 0b00000100", 0);
+    assertEquals(BYTE_VALUE + 4, mnemonic.getByte());
+
+  }
+}

+ 6 - 6
src/test/java/de/mcs/tools/sps/mnemonic/TestNOP.java

@@ -10,18 +10,18 @@ import de.mcs.tools.sps.exceptions.SyntaxError;
 class TestNOP {
 
   @Test
-  void testNOP() throws SyntaxError {
-    NOP nop = new NOP("NOP");
-    assertEquals(0x00, nop.getByte());
+  void testMnemonic() throws SyntaxError {
+    NOP mno = new NOP("NOP");
+    assertEquals(0x00, mno.getByte());
 
     Assertions.assertThrows(SyntaxError.class, () -> {
-      NOP nop1 = new NOP("NOP akfhaskh");
-      nop1.checkArgument();
+      NOP mno1 = new NOP("NOP akfhaskh");
+      mno1.checkArgument();
     });
   }
 
   @Test
-  void testNOPFactory() throws SyntaxError {
+  void testMnemonicFactory() throws SyntaxError {
     Mnemonic mnemonic = MnemonicFactory.getMnemonic("NOP", 0);
     assertEquals(NOP.class, mnemonic.getClass());
 

+ 19 - 18
src/test/java/de/mcs/tools/sps/mnemonic/TestPORT.java

@@ -10,35 +10,36 @@ import de.mcs.tools.sps.exceptions.SyntaxError;
 
 class TestPORT {
 
+  private static final int BYTE_VALUE = 0x10;
   private final String MNEMONIC = "PORT";
 
   @Test
-  void testNOP() throws SyntaxError {
-    PORT port = new PORT(MNEMONIC + " 0x00");
-    port.checkArgument();
-    assertEquals(0x10, port.getByte());
+  void testMnemonic() throws SyntaxError {
+    PORT mno = new PORT(MNEMONIC + " 0x00");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE, mno.getByte());
 
-    port = new PORT(MNEMONIC + " 0x01");
-    port.checkArgument();
-    assertEquals(0x11, port.getByte());
+    mno = new PORT(MNEMONIC + " 0x01");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE + 1, mno.getByte());
 
-    port = new PORT(MNEMONIC + " 0x0f");
-    port.checkArgument();
-    assertEquals(0x1f, port.getByte());
+    mno = new PORT(MNEMONIC + " 0x0f");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE + 0x0f, mno.getByte());
 
     Assertions.assertThrows(SyntaxError.class, () -> {
-      PORT port1 = new PORT(MNEMONIC);
-      port1.checkArgument();
+      PORT mno1 = new PORT(MNEMONIC);
+      mno1.checkArgument();
     });
 
     Assertions.assertThrows(IllegalArgument.class, () -> {
-      PORT port1 = new PORT(MNEMONIC + " 0x10");
-      port1.checkArgument();
+      PORT mno1 = new PORT(MNEMONIC + " 0x10");
+      mno1.checkArgument();
     });
   }
 
   @Test
-  void testNOPFactory() throws SyntaxError {
+  void testMnemonicFactory() throws SyntaxError {
     Mnemonic mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 0x00", 0);
     assertEquals(PORT.class, mnemonic.getClass());
 
@@ -47,13 +48,13 @@ class TestPORT {
     });
 
     mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 0x02", 0);
-    assertEquals(0x12, mnemonic.getByte());
+    assertEquals(BYTE_VALUE + 0x02, mnemonic.getByte());
 
     mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 12", 0);
-    assertEquals(0x1C, mnemonic.getByte());
+    assertEquals(BYTE_VALUE + 12, mnemonic.getByte());
 
     mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 0b00001010", 0);
-    assertEquals(0x1A, mnemonic.getByte());
+    assertEquals(BYTE_VALUE + 10, mnemonic.getByte());
 
   }
 }

+ 64 - 0
src/test/java/de/mcs/tools/sps/mnemonic/TestPWM.java

@@ -0,0 +1,64 @@
+package de.mcs.tools.sps.mnemonic;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import de.mcs.tools.sps.exceptions.IllegalArgument;
+import de.mcs.tools.sps.exceptions.SyntaxError;
+
+class TestPWM {
+
+  private static final int BYTE_VALUE = 0x59;
+  private final String MNEMONIC = "PWM";
+
+  @Test
+  void testNOP() throws SyntaxError {
+    PWM mno = new PWM(MNEMONIC + " 0x01");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE, mno.getByte());
+
+    mno = new PWM(MNEMONIC + " 0x02");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE + 1, mno.getByte());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      PWM mno1 = new PWM(MNEMONIC + " 0x03");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(IllegalArgument.class, () -> {
+      PWM mno1 = new PWM(MNEMONIC + " 0");
+      mno1.checkArgument();
+    });
+  }
+
+  @Test
+  void testMnemonicFactory() throws SyntaxError {
+    Mnemonic mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 1", 0);
+    assertEquals(PWM.class, mnemonic.getClass());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      MnemonicFactory.getMnemonic(MNEMONIC, 0);
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      MnemonicFactory.getMnemonic(MNEMONIC + " 0x00", 0);
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      MnemonicFactory.getMnemonic(MNEMONIC + " 5", 0);
+    });
+
+    mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 0x02", 0);
+    assertEquals(BYTE_VALUE + 0x01, mnemonic.getByte());
+
+    mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 1", 0);
+    assertEquals(BYTE_VALUE, mnemonic.getByte());
+
+    mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 0b00000010", 0);
+    assertEquals(BYTE_VALUE + 1, mnemonic.getByte());
+
+  }
+}

+ 64 - 0
src/test/java/de/mcs/tools/sps/mnemonic/TestSRV.java

@@ -0,0 +1,64 @@
+package de.mcs.tools.sps.mnemonic;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import de.mcs.tools.sps.exceptions.IllegalArgument;
+import de.mcs.tools.sps.exceptions.SyntaxError;
+
+class TestSRV {
+
+  private static final int BYTE_VALUE = 0x5b;
+  private final String MNEMONIC = "SRV";
+
+  @Test
+  void testNOP() throws SyntaxError {
+    SRV mno = new SRV(MNEMONIC + " 0x01");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE, mno.getByte());
+
+    mno = new SRV(MNEMONIC + " 0x02");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE + 1, mno.getByte());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SRV mno1 = new SRV(MNEMONIC + " 0x03");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(IllegalArgument.class, () -> {
+      SRV mno1 = new SRV(MNEMONIC + " 0");
+      mno1.checkArgument();
+    });
+  }
+
+  @Test
+  void testMnemonicFactory() throws SyntaxError {
+    Mnemonic mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 1", 0);
+    assertEquals(SRV.class, mnemonic.getClass());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      MnemonicFactory.getMnemonic(MNEMONIC, 0);
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      MnemonicFactory.getMnemonic(MNEMONIC + " 0x00", 0);
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      MnemonicFactory.getMnemonic(MNEMONIC + " 5", 0);
+    });
+
+    mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 0x02", 0);
+    assertEquals(BYTE_VALUE + 0x01, mnemonic.getByte());
+
+    mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 1", 0);
+    assertEquals(BYTE_VALUE, mnemonic.getByte());
+
+    mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 0b00000010", 0);
+    assertEquals(BYTE_VALUE + 1, mnemonic.getByte());
+
+  }
+}