Pārlūkot izejas kodu

Begin to add code to generate all possible states for macro

Baglio Tabifata 4 gadi atpakaļ
vecāks
revīzija
b13fcf417c

+ 74 - 4
src/net/hkzlab/dupal/boardio/DuPALAnalyzer.java

@@ -1,11 +1,14 @@
 package net.hkzlab.dupal.boardio;
 
+import java.util.ArrayList;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import net.hkzlab.devices.PALSpecs;
 import net.hkzlab.dupal.dupalproto.DuPALProto;
 import net.hkzlab.palanalisys.MacroState;
+import net.hkzlab.palanalisys.SubState;
 
 public class DuPALAnalyzer {
     private final Logger logger = LoggerFactory.getLogger(DuPALAnalyzer.class);
@@ -21,7 +24,8 @@ public class DuPALAnalyzer {
         this.pspecs = pspecs;
         this.IOasOUT_Mask = IOasOUT_Mask;
 
-        this.mStates = new MacroState[2^pspecs.getNumROUTPins()];
+        this.mStates = new MacroState[1 << pspecs.getNumROUTPins()];
+        logger.info("Provisioning for " +this.mStates.length+" possible macro states");
     } 
     
     public DuPALAnalyzer(final DuPALManager dpm, final PALSpecs pspecs) {
@@ -90,11 +94,77 @@ public class DuPALAnalyzer {
         pins = readPINs();
 
         int routstate = pins & pspecs.getROUT_READMask();
-        logger.info("Registered output states at start: " + Integer.toBinaryString(routstate) + "b");
-        logger.info("Output states at start: " + Integer.toBinaryString(pins & IOasOUT_Mask) + "b");
+        logger.info("Registered outputs at start: " + String.format("%02X", routstate));
+        //logger.info("Output states at start: " + String.format("%02X", (pins & IOasOUT_Mask)));
 
         mstate_idx = routstate >> pspecs.getROUT_READMaskShift();
         MacroState ms = new MacroState(buildMSTag(mstate_idx), mstate_idx, pspecs.getNumROUTPins(), pspecs.getNumINPins());
+        logger.info("Generating all possible substates for this macro state...");
+        genAllMSSubStates(ms);
+        mStates[mstate_idx] = ms; // Save it in our Array
+
+        logger.info("Added " + ms + " at index " + mstate_idx);
+        // TODO: Now, we have a starting point
+    }
+
+    private void genAllMSSubStates(MacroState ms) {
+        int idx_mask = (pspecs.getROUT_WRITEMask() | pspecs.getOEPinMask() | pspecs.getCLKPinMask() | (IOasOUT_Mask << 10));
+        int pins_1, pins_2, hiz_pins;
+
+        logger.debug("Input mask " + Integer.toBinaryString(idx_mask) + "b");
+
+        ArrayList<Byte> pinstate = new ArrayList<>();
+        ArrayList<Boolean> instate = new ArrayList<>();
+
+        for(int idx = 0; idx <= 0x387FE; idx+=2) {
+            if((idx & idx_mask) != 0) continue; // Skip this run
+
+            logger.debug("Testing combination 0x" + Integer.toHexString(idx));
+
+            pinstate.clear();
+            instate.clear();
+
+            writePINs(idx);
+            pins_1 = readPINs();
+            
+            writePINs(idx | IOasOUT_Mask);
+            pins_2 = readPINs();
+
+            hiz_pins = (pins_1 ^ pins_2) & IOasOUT_Mask;
+
+            for(int pin_idx = 0; pin_idx < 8; pin_idx++) {
+                if(((IOasOUT_Mask >> pin_idx) & 0x01) == 0) continue; // Not an output pin we're interested in
+
+                if(((hiz_pins >> pin_idx) & 0x01) > 0) pinstate.add((byte)-1);
+                else if (((pins_1 >> pin_idx) & 0x01) > 0) pinstate.add((byte)1);
+                else pinstate.add((byte)0);
+            }
+
+            for(int pin_idx = 0; pin_idx < 18; pin_idx++) {
+                if(((idx_mask >> pin_idx) & 0x01) > 0) continue; // Output pin, not interested
+
+                if(((idx >> pin_idx) & 0x01) > 0) instate.add(true);
+                else instate.add(false);
+            }
+            
+            logger.debug("pinstate len: " + pinstate.size() + " instate len: " + instate.size());
+
+            Byte[] out_state = pinstate.toArray(new Byte[pinstate.size()]);
+            int ss_idx = SubState.calculateSubStateIndex(instate.toArray(new Boolean[instate.size()]));
+            int ss_key = SubState.calculateSubStateKey(out_state);
+            SubState ss = null;
+            
+            logger.debug("substate index: " + ss_idx + " key: " + ss_key);
+
+            ss = ms.ssMap.get(Integer.valueOf(ss_key));
+            if(ss == null) {
+                ss = new SubState(ms.tag, ms, out_state);
+                ms.ssMap.put(Integer.valueOf(ss_key), ss);
+            }
+            ms.substates[ss_idx] = ss;
+        }
+
+        writePINs(0);
     }
 
     private int readPINs() {
@@ -108,6 +178,6 @@ public class DuPALAnalyzer {
     }
 
     static private String buildMSTag(int idx) {
-        return "MS_"+Integer.toHexString(idx);
+        return "TAG_"+Integer.toHexString(idx);
     }
 }

+ 2 - 2
src/net/hkzlab/dupal/boardio/DuPALManager.java

@@ -70,7 +70,7 @@ public class DuPALManager {
     public void writeCommand(String command) {
         if((serport != null) && serport.isOpened()) {
             try {
-                logger.info("Command -> " + command);
+                logger.debug("Command -> " + command);
                 serport.writeBytes(command.getBytes(StandardCharsets.US_ASCII));
                 try { Thread.sleep(25); } catch(InterruptedException e) {}; // Wait a bit for execution and response
             } catch (SerialPortException e) {
@@ -84,7 +84,7 @@ public class DuPALManager {
             try {
                 String resp = serport.readString().trim();
                 
-                logger.info("Response <- " + resp);
+                logger.debug("Response <- " + resp);
                 
                 return resp;
             } catch (SerialPortException e) {

+ 4 - 4
src/net/hkzlab/palanalisys/MacroState.java

@@ -19,21 +19,21 @@ public class MacroState {
         this.rpin_status = rpin_status;
         this.rpins = rpins;
 
-        links = new StateLink[2 ^ inPins]; // Create space for the future links out of this
-        substates = new SubState[2 ^ inPins]; // Create space for substates (each output pin is 3-state, but as they're triggered via input changes, we can have at most 2^inPins)
+        links = new StateLink[1 << inPins]; // Create space for the future links out of this
+        substates = new SubState[1 << inPins]; // Create space for substates (each output pin is 3-state, but as they're triggered via input changes, we can have at most 2^inPins)
         ssMap = new HashMap<>(); // Prepare the hashmap we'll use to avoid substate duplicates
     }
 
     @Override
     public String toString() {
-        return MS_PRE_TAG + tag + " - " + Integer.toBinaryString(rpin_status);
+        return MS_PRE_TAG + tag + " - " + String.format("%02X", rpin_status);
     }
 
     @Override
     public int hashCode() {
         int hash = 0;
 
-        for (int idx = 0; idx < (2^rpins); idx++) {
+        for (int idx = 0; idx < (1 << rpins); idx++) {
             hash ^= (((rpin_status >> idx) & 0x01) << (idx % 32));
         }
 

+ 6 - 6
src/net/hkzlab/palanalisys/SubState.java

@@ -7,9 +7,9 @@ public class SubState {
 
     public final String tag;
     public final MacroState macroState;
-    public final byte[] pin_status;
+    public final Byte[] pin_status;
 
-    public SubState(final String tag, final MacroState macroState, final byte[] pin_status) {
+    public SubState(final String tag, final MacroState macroState, final Byte[] pin_status) {
         this.tag = tag;
         this.macroState = macroState;
         this.pin_status = pin_status;
@@ -47,7 +47,7 @@ public class SubState {
         return true;
     }
 
-    public static int calculateSubStateIndex(final boolean[] inputs) {
+    public static int calculateSubStateIndex(final Boolean[] inputs) {
         int index = 0;
 
         for(int idx = 0; idx < inputs.length; idx++) {
@@ -60,12 +60,12 @@ public class SubState {
     /**
      * 
      */
-    public static int calculateSubStateKey(final byte[] in_comb) {
+    public static int calculateSubStateKey(final Byte[] out_comb) {
         int hash = 0;
 
-        for(int idx = 0; idx < in_comb.length; idx++) {
+        for(int idx = 0; idx < out_comb.length; idx++) {
             int byte_idx = idx % 4;
-            hash ^= (in_comb[idx] & 0xFF) << (8 * byte_idx);
+            hash ^= (out_comb[idx] & 0xFF) << (8 * byte_idx);
         }
 
         return hash;