Jelajahi Sumber

implementing feature A=some Input

Wilfried Klaas 6 tahun lalu
induk
melakukan
5efb3c283d

+ 10 - 0
src/main/java/de/mcs/tools/sps/EmulatorInput.java

@@ -2,4 +2,14 @@ package de.mcs.tools.sps;
 
 public interface EmulatorInput {
 
+  byte getInput();
+
+  void setInput(byte data);
+
+  void reset();
+
+  void setFeature(String name, byte register);
+
+  byte getFeature(String name);
+
 }

+ 24 - 10
src/main/java/de/mcs/tools/sps/holtec/HoltekEmulator.java

@@ -216,80 +216,94 @@ public class HoltekEmulator extends AbstractEmulator implements SPSEmulator {
     datas.add(new SPSCommandDataImpl() {
       @Override
       public EmulatorOutput doWork(EmulatorInput input, EmulatorInternals internals, EmulatorOutput output) {
+        internals.setRegister("A", internals.getRegister("B"));
         ((HoltekEmulatorInternals) internals).address++;
-        return HoltekEmulator.getInstance().getEmulatorOutput();
+        return output;
       }
     }.setName("A=B").setMnemonic("AEB").setCommandByte((byte) (SPS_A_INPUT.getCommandByte() + 0x01))
         .setCommand(SPS_A_INPUT));
     datas.add(new SPSCommandDataImpl() {
       @Override
       public EmulatorOutput doWork(EmulatorInput input, EmulatorInternals internals, EmulatorOutput output) {
+        internals.setRegister("A", internals.getRegister("C"));
         ((HoltekEmulatorInternals) internals).address++;
-        return HoltekEmulator.getInstance().getEmulatorOutput();
+        return output;
       }
     }.setName("A=C").setMnemonic("AEC").setCommandByte((byte) (SPS_A_INPUT.getCommandByte() + 0x02))
         .setCommand(SPS_A_INPUT));
     datas.add(new SPSCommandDataImpl() {
       @Override
       public EmulatorOutput doWork(EmulatorInput input, EmulatorInternals internals, EmulatorOutput output) {
+        internals.setRegister("A", internals.getRegister("D"));
         ((HoltekEmulatorInternals) internals).address++;
-        return HoltekEmulator.getInstance().getEmulatorOutput();
+        return output;
       }
     }.setName("A=D").setMnemonic("AED").setCommandByte((byte) (SPS_A_INPUT.getCommandByte() + 0x03))
         .setCommand(SPS_A_INPUT));
     datas.add(new SPSCommandDataImpl() {
       @Override
       public EmulatorOutput doWork(EmulatorInput input, EmulatorInternals internals, EmulatorOutput output) {
+        internals.setRegister("A", input.getInput());
         ((HoltekEmulatorInternals) internals).address++;
-        return HoltekEmulator.getInstance().getEmulatorOutput();
+        return output;
       }
     }.setName("A=Input").setMnemonic("AEI").setCommandByte((byte) (SPS_A_INPUT.getCommandByte() + 0x04))
         .setCommand(SPS_A_INPUT));
     datas.add(new SPSCommandDataImpl() {
       @Override
       public EmulatorOutput doWork(EmulatorInput input, EmulatorInternals internals, EmulatorOutput output) {
+        boolean value = (input.getInput() & 0x01) > 0;
+        internals.setRegister("A", (byte) (value ? 1 : 0));
         ((HoltekEmulatorInternals) internals).address++;
-        return HoltekEmulator.getInstance().getEmulatorOutput();
+        return output;
       }
     }.setName("A=Input.1").setMnemonic("AEI1").setCommandByte((byte) (SPS_A_INPUT.getCommandByte() + 0x05))
         .setCommand(SPS_A_INPUT));
     datas.add(new SPSCommandDataImpl() {
       @Override
       public EmulatorOutput doWork(EmulatorInput input, EmulatorInternals internals, EmulatorOutput output) {
+        boolean value = (input.getInput() & 0x02) > 0;
+        internals.setRegister("A", (byte) (value ? 1 : 0));
         ((HoltekEmulatorInternals) internals).address++;
-        return HoltekEmulator.getInstance().getEmulatorOutput();
+        return output;
       }
     }.setName("A=Input.2").setMnemonic("AEI2").setCommandByte((byte) (SPS_A_INPUT.getCommandByte() + 0x06))
         .setCommand(SPS_A_INPUT));
     datas.add(new SPSCommandDataImpl() {
       @Override
       public EmulatorOutput doWork(EmulatorInput input, EmulatorInternals internals, EmulatorOutput output) {
+        boolean value = (input.getInput() & 0x04) > 0;
+        internals.setRegister("A", (byte) (value ? 1 : 0));
         ((HoltekEmulatorInternals) internals).address++;
-        return HoltekEmulator.getInstance().getEmulatorOutput();
+        return output;
       }
     }.setName("A=Input.3").setMnemonic("AEI3").setCommandByte((byte) (SPS_A_INPUT.getCommandByte() + 0x07))
         .setCommand(SPS_A_INPUT));
     datas.add(new SPSCommandDataImpl() {
       @Override
       public EmulatorOutput doWork(EmulatorInput input, EmulatorInternals internals, EmulatorOutput output) {
+        boolean value = (input.getInput() & 0x08) > 0;
+        internals.setRegister("A", (byte) (value ? 1 : 0));
         ((HoltekEmulatorInternals) internals).address++;
-        return HoltekEmulator.getInstance().getEmulatorOutput();
+        return output;
       }
     }.setName("A=Input.4").setMnemonic("AEI4").setCommandByte((byte) (SPS_A_INPUT.getCommandByte() + 0x08))
         .setCommand(SPS_A_INPUT));
     datas.add(new SPSCommandDataImpl() {
       @Override
       public EmulatorOutput doWork(EmulatorInput input, EmulatorInternals internals, EmulatorOutput output) {
+        internals.setRegister("A", input.getFeature("AD1"));
         ((HoltekEmulatorInternals) internals).address++;
-        return HoltekEmulator.getInstance().getEmulatorOutput();
+        return output;
       }
     }.setName("A=AD.1").setMnemonic("AEA1").setCommandByte((byte) (SPS_A_INPUT.getCommandByte() + 0x09))
         .setCommand(SPS_A_INPUT));
     datas.add(new SPSCommandDataImpl() {
       @Override
       public EmulatorOutput doWork(EmulatorInput input, EmulatorInternals internals, EmulatorOutput output) {
+        internals.setRegister("A", input.getFeature("AD2"));
         ((HoltekEmulatorInternals) internals).address++;
-        return HoltekEmulator.getInstance().getEmulatorOutput();
+        return output;
       }
     }.setName("A=AD.2").setMnemonic("AEA2").setCommandByte((byte) (SPS_A_INPUT.getCommandByte() + 0x0A))
         .setCommand(SPS_A_INPUT));

+ 46 - 0
src/main/java/de/mcs/tools/sps/holtec/HoltekEmulatorInput.java

@@ -11,4 +11,50 @@ import de.mcs.tools.sps.EmulatorInput;
  */
 public class HoltekEmulatorInput implements EmulatorInput {
 
+  boolean i0, i1, i2, i3;
+  int ad1, ad2;
+
+  @Override
+  public void reset() {
+    i0 = false;
+    i1 = false;
+    i2 = false;
+    i3 = false;
+    ad1 = 0;
+    ad2 = 0;
+  }
+
+  @Override
+  public void setInput(byte data) {
+    i0 = (data & (byte) 0x01) > 0;
+    i1 = (data & (byte) 0x02) > 0;
+    i2 = (data & (byte) 0x04) > 0;
+    i3 = (data & (byte) 0x08) > 0;
+  }
+
+  public byte getInput() {
+    return (byte) ((i0 ? 1 : 0) + (i1 ? 2 : 0) + (i2 ? 4 : 0) + (i3 ? 8 : 0));
+  }
+
+  @Override
+  public void setFeature(String name, byte value) {
+    if ("AD1".equals(name.toUpperCase())) {
+      ad1 = value;
+    }
+    if ("AD2".equals(name.toUpperCase())) {
+      ad2 = value;
+    }
+  }
+
+  @Override
+  public byte getFeature(String name) {
+    if ("AD1".equals(name.toUpperCase())) {
+      return (byte) ad1;
+    }
+    if ("AD2".equals(name.toUpperCase())) {
+      return (byte) ad2;
+    }
+    return 0;
+  }
+
 }

+ 6 - 0
src/main/java/de/mcs/tools/sps/holtec/HoltekEmulatorOutput.java

@@ -40,4 +40,10 @@ public class HoltekEmulatorOutput implements EmulatorOutput {
     }
   }
 
+  public byte getFeature(String name) {
+    if ("PWM".equals(name.toUpperCase())) {
+      return (byte) pwm;
+    }
+    return 0;
+  }
 }

+ 155 - 1
src/test/java/de/mcs/tools/sps/holtec/TestHoltekEmulator.java

@@ -174,7 +174,7 @@ class TestHoltekEmulator implements TOutputCallback {
     start = System.currentTimeMillis();
     holtekEmulator.nextStep();
     delta = System.currentTimeMillis() - start;
-    assertTrue(delta > 9000 && delta < 10100);
+    assertTrue(delta > 9000 && delta < 11000);
   }
 
   @Test
@@ -198,4 +198,158 @@ class TestHoltekEmulator implements TOutputCallback {
     assertEquals(0x03, holtekEmulator.getEmulatorInternals().getAddress());
   }
 
+  @Test
+  void testAEquals() throws WrongProgramSizeException {
+    byte[] prg = new byte[16];
+    for (int i = 0; i < prg.length; i++) {
+      prg[i] = (byte) (0x40 + i);
+    }
+    ;
+    holtekEmulator.loadProgram(prg);
+    holtekEmulator.startProgram(true);
+    for (int i = 0; i < prg.length; i++) {
+      holtekEmulator.nextStep();
+      assertEquals(i, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    }
+  }
+
+  @Test
+  void testEqualsA() throws WrongProgramSizeException {
+    byte[] prg = new byte[] { 0x48, 0x51, 0x52, 0x53, 0x40, 0x54, 0x4F, 0x54, 0x55 };
+    holtekEmulator.loadProgram(prg);
+    holtekEmulator.startProgram(true);
+    holtekEmulator.nextStep();
+    holtekEmulator.nextStep();
+    holtekEmulator.nextStep();
+    holtekEmulator.nextStep();
+    assertEquals(0x08, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    assertEquals(0x08, holtekEmulator.getEmulatorInternals().getRegister("B"));
+    assertEquals(0x08, holtekEmulator.getEmulatorInternals().getRegister("C"));
+    assertEquals(0x08, holtekEmulator.getEmulatorInternals().getRegister("D"));
+
+    holtekEmulator.nextStep();
+    holtekEmulator.nextStep();
+    assertEquals(0x00, ((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).getOutput());
+
+    holtekEmulator.nextStep();
+    holtekEmulator.nextStep();
+    assertEquals(0x0F, ((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).getOutput());
+    assertTrue(((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).d0);
+    assertTrue(((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).d1);
+    assertTrue(((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).d2);
+    assertTrue(((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).d3);
+
+    holtekEmulator.stop();
+
+    prg = new byte[] { 0x41, 0x55, 0x56, 0x57, 0x58 };
+
+    holtekEmulator.loadProgram(prg);
+    holtekEmulator.startProgram(true);
+    holtekEmulator.nextStep();
+    holtekEmulator.nextStep();
+    assertEquals(0x01, ((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).getOutput());
+    holtekEmulator.nextStep();
+    assertEquals(0x03, ((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).getOutput());
+    assertTrue(((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).d0);
+    assertTrue(((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).d1);
+    assertFalse(((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).d2);
+    assertFalse(((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).d3);
+    holtekEmulator.nextStep();
+    assertEquals(0x07, ((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).getOutput());
+    holtekEmulator.nextStep();
+    assertEquals(0x0f, ((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).getOutput());
+
+    holtekEmulator.stop();
+
+    prg = new byte[] { 0x41, 0x59, 0x4F, 0x59 };
+
+    holtekEmulator.loadProgram(prg);
+    holtekEmulator.startProgram(true);
+    holtekEmulator.nextStep();
+    holtekEmulator.nextStep();
+    assertEquals(0x01, ((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).getFeature("PWM"));
+    holtekEmulator.nextStep();
+    holtekEmulator.nextStep();
+    assertEquals(0x0F, ((HoltekEmulatorOutput) holtekEmulator.getEmulatorOutput()).getFeature("PWM"));
+  }
+
+  @Test
+  void testAInput() throws WrongProgramSizeException {
+    byte[] prg = new byte[] { 0x4F, 0x51, 0x52, 0x53, 0x40, 0x61, 0x40, 0x62, 0x40, 0x63 };
+    holtekEmulator.loadProgram(prg);
+    holtekEmulator.startProgram(true);
+    for (int i = 0; i < 5; i++) {
+      holtekEmulator.nextStep();
+    }
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.nextStep();
+    assertEquals(0x0f, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.nextStep();
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.nextStep();
+    assertEquals(0x0f, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.nextStep();
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.nextStep();
+    assertEquals(0x0f, holtekEmulator.getEmulatorInternals().getRegister("A"));
+
+    holtekEmulator.stop();
+
+    prg = new byte[] { 0x64, 0x64, 0x40, 0x65, 0x40, 0x66, 0x40, 0x67, 0x40, 0x68 };
+    holtekEmulator.loadProgram(prg);
+    holtekEmulator.startProgram(true);
+    holtekEmulator.getEmulatorInput().setInput((byte) 0x00);
+    holtekEmulator.nextStep();
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.getEmulatorInput().setInput((byte) 0x0f);
+    holtekEmulator.nextStep();
+    assertEquals(0x0f, holtekEmulator.getEmulatorInternals().getRegister("A"));
+
+    holtekEmulator.nextStep();
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.nextStep();
+    assertEquals(0x01, holtekEmulator.getEmulatorInternals().getRegister("A"));
+
+    holtekEmulator.nextStep();
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.nextStep();
+    assertEquals(0x01, holtekEmulator.getEmulatorInternals().getRegister("A"));
+
+    holtekEmulator.nextStep();
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.nextStep();
+    assertEquals(0x01, holtekEmulator.getEmulatorInternals().getRegister("A"));
+
+    holtekEmulator.nextStep();
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.nextStep();
+    assertEquals(0x01, holtekEmulator.getEmulatorInternals().getRegister("A"));
+
+    holtekEmulator.stop();
+
+    prg = new byte[] { 0x40, 0x69, 0x40, 0x69, 0x40, 0x6A, 0x40, 0x6A };
+    holtekEmulator.loadProgram(prg);
+    holtekEmulator.startProgram(true);
+    holtekEmulator.nextStep();
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.getEmulatorInput().setFeature("AD1", (byte) 0x00);
+    holtekEmulator.nextStep();
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.nextStep();
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.getEmulatorInput().setFeature("AD1", (byte) 0x0F);
+    holtekEmulator.nextStep();
+    assertEquals(0x0f, holtekEmulator.getEmulatorInternals().getRegister("A"));
+
+    holtekEmulator.nextStep();
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.getEmulatorInput().setFeature("AD2", (byte) 0x00);
+    holtekEmulator.nextStep();
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.nextStep();
+    assertEquals(0x00, holtekEmulator.getEmulatorInternals().getRegister("A"));
+    holtekEmulator.getEmulatorInput().setFeature("AD2", (byte) 0x0F);
+    holtekEmulator.nextStep();
+    assertEquals(0x0f, holtekEmulator.getEmulatorInternals().getRegister("A"));
+  }
 }