001package jmri.jmrix.loconet.sdf;
002
003/**
004 * Implement the MASK_COMPARE macro from the Digitrax sound definition language
005 *
006 * @author Bob Jacobsen Copyright (C) 2007
007 */
008public class MaskCompare extends SdfMacro {
009
010    public MaskCompare(int byte1, int byte2, int byte3, int byte4) {
011        bytes[0] = (byte) (byte1 & 0xFF);
012        bytes[1] = (byte) (byte2 & 0xFF);
013        bytes[2] = (byte) (byte3 & 0xFF);
014        bytes[3] = (byte) (byte4 & 0xFF);
015
016        src = byte2;
017        immed = byte1 & 0x04;
018        targ = byte3;
019        mask = byte4;
020        skip = byte1 & 0x03;
021    }
022
023    @Override
024    public String name() {
025        return "MASK_COMPARE"; // NOI18N
026    }
027
028    int src;
029    int immed;
030    int targ;
031    int mask;
032    int skip;
033
034    byte[] bytes = new byte[4];
035
036    @Override
037    public int length() {
038        return 4;
039    }
040
041    String srcVal() {
042        return "(src = " + src + ")"; // NOI18N
043    }
044
045    String immedVal() {
046        if (immed != 0) {
047            return "IMMED_DATA"; // NOI18N
048        } else {
049            return "TARGET_DATA"; // NOI18N
050        }
051    }
052
053    String targVal() {
054        return "(target = " + targ + ")"; // NOI18N
055    }
056
057    String maskVal() {
058        return "(mask = " + mask + ")"; // NOI18N
059    }
060
061    String skipVal() {
062        return "(skip = " + skip + ")"; // NOI18N
063    }
064
065    static public SdfMacro match(SdfBuffer buff) {
066        // course match
067        if ((buff.getAtIndex() & 0xF8) != 0xF8) {
068            return null;
069        }
070        return new MaskCompare(buff.getAtIndexAndInc(),
071                buff.getAtIndexAndInc(),
072                buff.getAtIndexAndInc(),
073                buff.getAtIndexAndInc());
074    }
075
076    /**
077     * Store into a buffer.
078     */
079    @Override
080    public void loadByteArray(SdfBuffer buffer) {
081        // data
082        buffer.setAtIndexAndInc(bytes[0]);
083        buffer.setAtIndexAndInc(bytes[1]);
084        buffer.setAtIndexAndInc(bytes[2]);
085        buffer.setAtIndexAndInc(bytes[3]);
086
087        // store children
088        super.loadByteArray(buffer);
089    }
090
091    @Override
092    public String toString() {
093        return "Check Mask\n"; // NOI18N
094    }
095
096    @Override
097    public String oneInstructionString() {
098        return name() + ' ' + jmri.util.StringUtil.hexStringFromBytes(bytes) + '\n';
099    }
100
101    @Override
102    public String allInstructionString(String indent) {
103        return indent + oneInstructionString();
104    }
105}