Jelajahi Sumber

Implemeting ADC, ARC, DIN, POP, SETX

Wilfried Klaas 6 tahun lalu
induk
melakukan
9642418776

+ 10 - 1
SimpleServo.tps

@@ -23,4 +23,13 @@ RJMP 0x06
 RJMP :loop
 JMP 0x08 ;this will cause an syntax error
 PUSH
-POP
+POP
+SETB
+SETC
+SETD
+SETE
+SETF
+DIN
+DIN 0x03
+ADC 1
+ARC 2

+ 23 - 2
src/main/java/de/mcs/tools/sps/mnemonic/ADC.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;
 
 /**
+ * ADC #x: analog input #x to register A. x should be 1 or 2
+ * 
  * @author wklaa_000
  *
  */
 public class ADC extends AbstractMnemonic implements Mnemonic {
 
+  byte value;
+
   public ADC(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..2 for %s.", getArgument(), this.getClass().getSimpleName()));
+    }
+    value = (byte) ((myValue - 1) & 0xFF);
   }
 
   @Override
   public int getByte() {
-    // TODO modifier x (1,2)
-    return 0x69;
+    return 0x69 + value;
   }
 
 }

+ 23 - 2
src/main/java/de/mcs/tools/sps/mnemonic/ARC.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;
 
 /**
+ * ARC #x: rc input #x to register A. x should be 1 or 2
+ * 
  * @author wklaa_000
  *
  */
 public class ARC extends AbstractMnemonic implements Mnemonic {
 
+  byte value;
+
   public ARC(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..2 for %s.", getArgument(), this.getClass().getSimpleName()));
+    }
+    value = (byte) ((myValue - 1) & 0xFF);
   }
 
   @Override
   public int getByte() {
-    // TODO modifier x (1,2 )
-    return 0x6B;
+    return 0x6B + value;
   }
 
 }

+ 25 - 2
src/main/java/de/mcs/tools/sps/mnemonic/DIN.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;
 
 /**
+ * DIN, DIN #x: Input from digital input to register A. If no #x given, all 4
+ * inputs are shown in the register , otherwise the lowest bit of A will show
+ * the input value #x. #x in range 1..4
+ * 
  * @author wklaa_000
  *
  */
 public class DIN extends AbstractMnemonic implements Mnemonic {
 
+  byte value;
+
   public DIN(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 0x64;
+    return 0x64 + value;
   }
 
 }

+ 2 - 0
src/main/java/de/mcs/tools/sps/mnemonic/POP.java

@@ -24,6 +24,8 @@ package de.mcs.tools.sps.mnemonic;
 import de.mcs.tools.sps.exceptions.SyntaxError;
 
 /**
+ * POP: Pop a value from the stack to register A.
+ * 
  * @author wklaa_000
  *
  */

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

@@ -50,7 +50,7 @@ public class PWM extends AbstractMnemonic implements Mnemonic {
     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()));
+          String.format("argument %s is not in range 1..2 for %s.", getArgument(), this.getClass().getSimpleName()));
     }
     value = (byte) ((myValue - 1) & 0xFF);
   }

+ 2 - 0
src/main/java/de/mcs/tools/sps/mnemonic/SETB.java

@@ -24,6 +24,8 @@ package de.mcs.tools.sps.mnemonic;
 import de.mcs.tools.sps.exceptions.SyntaxError;
 
 /**
+ * SETB: copy value of register B to register A
+ * 
  * @author wklaa_000
  *
  */

+ 2 - 0
src/main/java/de/mcs/tools/sps/mnemonic/SETC.java

@@ -24,6 +24,8 @@ package de.mcs.tools.sps.mnemonic;
 import de.mcs.tools.sps.exceptions.SyntaxError;
 
 /**
+ * SETC: copy value of register C to register A
+ * 
  * @author wklaa_000
  *
  */

+ 2 - 0
src/main/java/de/mcs/tools/sps/mnemonic/SETD.java

@@ -24,6 +24,8 @@ package de.mcs.tools.sps.mnemonic;
 import de.mcs.tools.sps.exceptions.SyntaxError;
 
 /**
+ * SETD: copy value of register D to register A
+ * 
  * @author wklaa_000
  *
  */

+ 2 - 0
src/main/java/de/mcs/tools/sps/mnemonic/SETE.java

@@ -24,6 +24,8 @@ package de.mcs.tools.sps.mnemonic;
 import de.mcs.tools.sps.exceptions.SyntaxError;
 
 /**
+ * SETE: copy value of register E to register A
+ * 
  * @author wklaa_000
  *
  */

+ 2 - 0
src/main/java/de/mcs/tools/sps/mnemonic/SETF.java

@@ -24,6 +24,8 @@ package de.mcs.tools.sps.mnemonic;
 import de.mcs.tools.sps.exceptions.SyntaxError;
 
 /**
+ * SETF: copy value of register F to register A
+ * 
  * @author wklaa_000
  *
  */

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

@@ -50,7 +50,7 @@ public class SRV extends AbstractMnemonic implements Mnemonic {
     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()));
+          String.format("argument %s is not in range 1..2 for %s.", getArgument(), this.getClass().getSimpleName()));
     }
     value = (byte) ((myValue - 1) & 0xFF);
   }

+ 64 - 0
src/test/java/de/mcs/tools/sps/mnemonic/TestADC.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 TestADC {
+
+  private static final int BYTE_VALUE = 0x69;
+  private final String MNEMONIC = "ADC";
+
+  @Test
+  void testMnemonic() throws SyntaxError {
+    ADC mno = new ADC(MNEMONIC + " 0x01");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE, mno.getByte());
+
+    mno = new ADC(MNEMONIC + " 0x02");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE + 1, mno.getByte());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      ADC mno1 = new ADC(MNEMONIC + " 0x03");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(IllegalArgument.class, () -> {
+      ADC mno1 = new ADC(MNEMONIC + " 0");
+      mno1.checkArgument();
+    });
+  }
+
+  @Test
+  void testMnemonicFactory() throws SyntaxError {
+    Mnemonic mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 1", 0);
+    assertEquals(ADC.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/TestARC.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 TestARC {
+
+  private static final int BYTE_VALUE = 0x6b;
+  private final String MNEMONIC = "ARC";
+
+  @Test
+  void testMnemonic() throws SyntaxError {
+    ARC mno = new ARC(MNEMONIC + " 0x01");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE, mno.getByte());
+
+    mno = new ARC(MNEMONIC + " 0x02");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE + 1, mno.getByte());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      ARC mno1 = new ARC(MNEMONIC + " 0x03");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(IllegalArgument.class, () -> {
+      ARC mno1 = new ARC(MNEMONIC + " 0");
+      mno1.checkArgument();
+    });
+  }
+
+  @Test
+  void testMnemonicFactory() throws SyntaxError {
+    Mnemonic mnemonic = MnemonicFactory.getMnemonic(MNEMONIC + " 1", 0);
+    assertEquals(ARC.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/TestDIN.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 TestDIN {
+
+  private static final int BYTE_VALUE = 0x64;
+  private final String MNEMONIC = "DIN";
+
+  @Test
+  void testMnemonic() throws SyntaxError {
+    DIN mno = new DIN(MNEMONIC);
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE, mno.getByte());
+
+    mno = new DIN(MNEMONIC + " 0x01");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE + 1, mno.getByte());
+
+    mno = new DIN(MNEMONIC + " 0x04");
+    mno.checkArgument();
+    assertEquals(BYTE_VALUE + 0x04, mno.getByte());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      DIN mno1 = new DIN(MNEMONIC + " 0x05");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(IllegalArgument.class, () -> {
+      DIN mno1 = new DIN(MNEMONIC + " 0x10");
+      mno1.checkArgument();
+    });
+  }
+
+  @Test
+  void testMnemonicFactory() throws SyntaxError {
+    Mnemonic mnemonic = MnemonicFactory.getMnemonic(MNEMONIC, 0);
+    assertEquals(DIN.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());
+
+  }
+}

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

@@ -14,7 +14,7 @@ class TestDOUT {
   private final String MNEMONIC = "DOUT";
 
   @Test
-  void testNOP() throws SyntaxError {
+  void testMnemonic() throws SyntaxError {
     DOUT mno = new DOUT(MNEMONIC);
     mno.checkArgument();
     assertEquals(BYTE_VALUE, mno.getByte());
@@ -33,7 +33,7 @@ class TestDOUT {
     });
 
     Assertions.assertThrows(IllegalArgument.class, () -> {
-      PORT mno1 = new PORT(MNEMONIC + " 0x10");
+      DOUT mno1 = new DOUT(MNEMONIC + " 0x10");
       mno1.checkArgument();
     });
   }

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

@@ -14,7 +14,7 @@ class TestPWM {
   private final String MNEMONIC = "PWM";
 
   @Test
-  void testNOP() throws SyntaxError {
+  void testMnemonic() throws SyntaxError {
     PWM mno = new PWM(MNEMONIC + " 0x01");
     mno.checkArgument();
     assertEquals(BYTE_VALUE, mno.getByte());

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

@@ -14,7 +14,7 @@ class TestSRV {
   private final String MNEMONIC = "SRV";
 
   @Test
-  void testNOP() throws SyntaxError {
+  void testMnemonic() throws SyntaxError {
     SRV mno = new SRV(MNEMONIC + " 0x01");
     mno.checkArgument();
     assertEquals(BYTE_VALUE, mno.getByte());

+ 141 - 0
src/test/java/de/mcs/tools/sps/mnemonic/TestSingleMnemonics.java

@@ -51,6 +51,27 @@ class TestSingleMnemonics {
     });
   }
 
+  @Test
+  void testSETB() throws SyntaxError {
+    SETB mno = new SETB("SETB");
+    assertEquals(0x61, mno.getByte());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETB mno1 = new SETB("SETB akfhaskh");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETB mno1 = new SETB("SETB 12");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETB mno1 = new SETB("SETB :label");
+      mno1.checkArgument();
+    });
+  }
+
   @Test
   void testCSET() throws SyntaxError {
     CSET mno = new CSET("CSET");
@@ -72,6 +93,27 @@ class TestSingleMnemonics {
     });
   }
 
+  @Test
+  void testSETC() throws SyntaxError {
+    SETC mno = new SETC("SETC");
+    assertEquals(0x62, mno.getByte());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETC mno1 = new SETC("SETC akfhaskh");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETC mno1 = new SETC("SETC 12");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETC mno1 = new SETC("SETC :label");
+      mno1.checkArgument();
+    });
+  }
+
   @Test
   void testDSET() throws SyntaxError {
     DSET mno = new DSET("DSET");
@@ -93,6 +135,27 @@ class TestSingleMnemonics {
     });
   }
 
+  @Test
+  void testSETD() throws SyntaxError {
+    SETD mno = new SETD("SETD");
+    assertEquals(0x63, mno.getByte());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETD mno1 = new SETD("SETD akfhaskh");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETD mno1 = new SETD("SETD 12");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETD mno1 = new SETD("SETD :label");
+      mno1.checkArgument();
+    });
+  }
+
   @Test
   void testESET() throws SyntaxError {
     ESET mno = new ESET("ESET");
@@ -114,6 +177,27 @@ class TestSingleMnemonics {
     });
   }
 
+  @Test
+  void testSETE() throws SyntaxError {
+    SETE mno = new SETE("SETE");
+    assertEquals(0x6D, mno.getByte());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETE mno1 = new SETE("SETE akfhaskh");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETE mno1 = new SETE("SETE 12");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETE mno1 = new SETE("SETE :label");
+      mno1.checkArgument();
+    });
+  }
+
   @Test
   void testFSET() throws SyntaxError {
     FSET mno = new FSET("FSET");
@@ -135,6 +219,27 @@ class TestSingleMnemonics {
     });
   }
 
+  @Test
+  void testSETF() throws SyntaxError {
+    SETF mno = new SETF("SETF");
+    assertEquals(0x6E, mno.getByte());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETF mno1 = new SETF("SETF akfhaskh");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETF mno1 = new SETF("SETF 12");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      SETF mno1 = new SETF("SETF :label");
+      mno1.checkArgument();
+    });
+  }
+
   @Test
   void testPUSH() throws SyntaxError {
     PUSH mno = new PUSH("PUSH");
@@ -156,6 +261,27 @@ class TestSingleMnemonics {
     });
   }
 
+  @Test
+  void testPOP() throws SyntaxError {
+    POP mno = new POP("POP");
+    assertEquals(0x6f, mno.getByte());
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      POP mno1 = new POP("POP akfhaskh");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      POP mno1 = new POP("POP 12");
+      mno1.checkArgument();
+    });
+
+    Assertions.assertThrows(SyntaxError.class, () -> {
+      POP mno1 = new POP("POP :label");
+      mno1.checkArgument();
+    });
+  }
+
   @Test
   void testMnemonicFactory() throws SyntaxError {
     Mnemonic mnemonic = MnemonicFactory.getMnemonic("SWAP", 0);
@@ -178,5 +304,20 @@ class TestSingleMnemonics {
 
     mnemonic = MnemonicFactory.getMnemonic("PUSH", 0);
     assertEquals(PUSH.class, mnemonic.getClass());
+
+    mnemonic = MnemonicFactory.getMnemonic("SETB", 0);
+    assertEquals(SETB.class, mnemonic.getClass());
+
+    mnemonic = MnemonicFactory.getMnemonic("SETC", 0);
+    assertEquals(SETC.class, mnemonic.getClass());
+
+    mnemonic = MnemonicFactory.getMnemonic("SETD", 0);
+    assertEquals(SETD.class, mnemonic.getClass());
+
+    mnemonic = MnemonicFactory.getMnemonic("SETE", 0);
+    assertEquals(SETE.class, mnemonic.getClass());
+
+    mnemonic = MnemonicFactory.getMnemonic("SETF", 0);
+    assertEquals(SETF.class, mnemonic.getClass());
   }
 }