示例#1
0
        internal static Bitfield Concat(this Bitfield primaryBitfield, Bitfield followingBitfield)
        {
            var newBitfield = new Bitfield(primaryBitfield.BitWidth + followingBitfield.BitWidth);
            var j           = 0;

            for (var i = 0; i < newBitfield.BitWidth; i++)
            {
                newBitfield.BitArray.Set(i, i < followingBitfield.BitArray.Length ? followingBitfield.BitArray.Get(i) : primaryBitfield.BitArray.Get(j++));
            }
            return(newBitfield);
        }
示例#2
0
        internal static Bitfield InsertAt(this Bitfield primaryBitfield, int primaryIndex, Bitfield insertBitfield)
        {
            var rightToLeftPrimaryIndex = primaryBitfield.BitWidth - primaryIndex;
            var newBitfield             = new Bitfield(primaryBitfield.BitWidth + insertBitfield.BitWidth);
            var primaryBitfieldIterator = 0;
            var insertBitfieldIterator  = 0;

            for (var i = 0; i < newBitfield.BitWidth; i++)
            {
                newBitfield.BitArray.Set(i,
                                         (i < rightToLeftPrimaryIndex || i >= (rightToLeftPrimaryIndex + insertBitfield.BitWidth)) ?
                                         primaryBitfield.BitArray.Get(primaryBitfieldIterator++) :
                                         insertBitfield.BitArray.Get(insertBitfieldIterator++));
            }
            return(newBitfield);
        }
        internal override void ToBitfieldWrapper()
        {
            _bitfieldWrapper = new GlowImmediateBitfieldWrapper();
            _bitfieldWrapper.AddBitfield(_bitfieldWrapper.ColorBitmap);
            _bitfieldWrapper.AddBitfield(_bitfieldWrapper.ActionOpcode);

            // Set action opcode:
            _bitfieldWrapper.ActionOpcode.SetValue((int)ActionOpCode.SetVal);

            // Set color bitmap (using any led in group as they all have same color):
            if (LedColor.Red > -1)
            {
                _bitfieldWrapper.ColorBitmap.SetFlag((int)ColorSpecifier.Red);
                var valueBitfield = new Bitfield(width: 8, name: "red value");
                valueBitfield.SetValue((uint)LedColor.Red);
                _bitfieldWrapper.AddBitfield(valueBitfield);
            }
            if (LedColor.Green > -1)
            {
                _bitfieldWrapper.ColorBitmap.SetFlag((int)ColorSpecifier.Green);
                var valueBitfield = new Bitfield(width: 8, name: "green value");
                valueBitfield.SetValue((uint)LedColor.Green);
                _bitfieldWrapper.AddBitfield(valueBitfield);
            }
            if (LedColor.Blue > -1)
            {
                _bitfieldWrapper.ColorBitmap.SetFlag((int)ColorSpecifier.Blue);
                var valueBitfield = new Bitfield(width: 8, name: "blue value");
                valueBitfield.SetValue((uint)LedColor.Blue);
                _bitfieldWrapper.AddBitfield(valueBitfield);
            }
            if (LedColor.Bright > -1)
            {
                _bitfieldWrapper.ColorBitmap.SetFlag((int)ColorSpecifier.Bright);
                var valueBitfield = new Bitfield(width: 5, name: "bright value");
                valueBitfield.SetValue((uint)LedColor.Bright);
                _bitfieldWrapper.AddBitfield(valueBitfield);
            }

            // Add led bitmap (led index increases from left to right):
            _bitfieldWrapper.LedBitmap = new Bitfield(width: DeviceTable.Dev.LedCount, name: "led bitmap");
            _bitfieldWrapper.AddBitfield(_bitfieldWrapper.LedBitmap);
            LedIdxList.ToList().ForEach(idx => _bitfieldWrapper.LedBitmap.SetFlag((uint)(DeviceTable.Dev.LedCount - idx - 1)));
        }
示例#4
0
        private Bitfield PadBitfieldToPacketSizeMultiple(Bitfield bitfield)
        {
            var packetBitLen = DeviceTable.Dev.UsbPacketByteLen * BitsPerByte;

            if (bitfield.BitWidth % packetBitLen == 0)
            {
                return(bitfield);
            }

            var packetSizeMultiple = bitfield.BitWidth;

            while (packetSizeMultiple % packetBitLen != 0)
            {
                packetSizeMultiple++;
            }
            var paddingBitfield = new Bitfield(width: packetSizeMultiple - bitfield.BitWidth);

            paddingBitfield.BitArray.SetAll(true);  // fill with 1's.
            bitfield = bitfield.Concat(paddingBitfield);

            return(bitfield);
        }
        private Bitfield GenerateControlPacket(MemoryOpcode memoryOpcode, ControlOpcode controlOpcode)
        {
            if (DeviceTable.Dev.UsbPacketByteLen == 0)
            {
                Console.WriteLine("Invalid packet byte length.");
                throw new Exception();
            }
            var packetBitLen = DeviceTable.Dev.UsbPacketByteLen * BitsPerByte;

            // Generate run control bitfield:
            var controlInstrBitfield = new ControlBitfieldWrapper();

            controlInstrBitfield.MemoryOpcode.SetValue((uint)memoryOpcode);
            controlInstrBitfield.ControlOpcode.SetValue((uint)controlOpcode);

            // Add padding to get packet-sized bitfield:
            var padBitfield = new Bitfield(width: packetBitLen - controlInstrBitfield.BitWidth);

            padBitfield.BitArray.SetAll(true);  // pad with 1's.
            controlInstrBitfield.AddBitfield(padBitfield);

            return(controlInstrBitfield.ToAggregatedBitfield());
        }
示例#6
0
        // Populate bitfield values (bitfield widths unchanged).
        private void AddDynamicData(ref List <List <Instruction> > instrSets)
        {
            var tempAggBitfield1 = new Bitfield(width: 0);

            // Populate each instructions's bit address relative to start of first path:
            for (var i = 0; i < instrSets.Count; i++)
            {
                var tempAggBitfield2 = new Bitfield(width: 0);

                foreach (var instr in instrSets[i])
                {
                    // Save instruction bit address:
                    instr.BitAddress            = tempAggBitfield1.BitWidth;
                    instr.CurrentPathBitAddress = tempAggBitfield2.BitWidth;

                    // Append instruction bitfield to aggregated bitfield:
                    tempAggBitfield1 = tempAggBitfield1.Concat(instr.BitfieldWrapper.ToAggregatedBitfield());    // aggBitfield value is incomplete/unused.
                    tempAggBitfield2 = tempAggBitfield2.Concat(instr.BitfieldWrapper.ToAggregatedBitfield());    // aggBitfield value is incomplete/unused.
                }
            }

            // Populate each goto instruction's target bit address relative to start of current path:
            for (var i = 0; i < instrSets.Count; i++)
            {
                foreach (var instr in instrSets[i])
                {
                    if (instr.Type == Instruction.InstrType.Goto)
                    {
                        var gotoTargetName = ((GotoInstruction)instr).TargetName;
                        var matchingHere   = instrSets[i].First(x => x.Type == Instruction.InstrType.Here && ((HereInstruction)x).Name == gotoTargetName);
                        // Set 'here' bit address relative to start of current path:
                        ((GotoBitfieldWrapper)((GotoInstruction)instr).BitfieldWrapper).TargetBitAddress.SetValue(matchingHere.CurrentPathBitAddress);
                    }
                }
            }
        }
示例#7
0
        private Instruction_ContextRegion BuildContextRegionBitfields(List <List <Instruction> > instrSets, out uint contextRegionByteSize, out uint maxInstrPathByteSize)
        {
            var contextRegion = new Instruction_ContextRegion();

            contextRegion.ToBitfieldWrapper();

            // Build common context-region:
            var contextRegionByteLenBitfield = new Bitfield(width: 16, name: "contx-reg byte len");
            var instrRegionByteLenBitfield   = new Bitfield(width: 32, name: "instr-reg byte len");
            var ledCountBitfield             = new Bitfield(width: 16, value: DeviceTable.Dev.LedCount, name: "total led len");
            var timerIntervalMs        = new Bitfield(width: 16, value: DeviceTable.Dev.TickIntervalMs, name: "tick interval ms");
            var simBrightCoeffBitfield = new Bitfield(width: 16, value: DeviceTable.Dev.SimulatorBrightnessCoeff, name: "sim bright coeff");
            var totalPathsBitfield     = new Bitfield(width: 8, value: (uint)instrSets.Count, name: "total paths");
            var pathEndedBitmap        = new Bitfield(width: (uint)instrSets.Count, name: "path end bitmap");

            contextRegion.BitfieldWrapper.AddBitfield(contextRegionByteLenBitfield);
            contextRegion.BitfieldWrapper.AddBitfield(instrRegionByteLenBitfield);
            contextRegion.BitfieldWrapper.AddBitfield(ledCountBitfield);
            contextRegion.BitfieldWrapper.AddBitfield(timerIntervalMs);
            contextRegion.BitfieldWrapper.AddBitfield(simBrightCoeffBitfield);
            contextRegion.BitfieldWrapper.AddBitfield(totalPathsBitfield);
            contextRegion.BitfieldWrapper.AddBitfield(pathEndedBitmap);

            uint totalInstrRegionByteLen = 0;

            maxInstrPathByteSize = 0;

            // Build a context-region block for each path:
            for (var i = 0; i < instrSets.Count; i++)
            {
                // Mark current path as ended (except path 0):
                if (i > 0)
                {
                    pathEndedBitmap.SetFlag((uint)(instrSets.Count - i - 1));
                }

                // Add start of current path byte-address bitfield:
                if ((instrSets[i].First().BitAddress - instrSets[0].First().BitAddress) % BitsPerByte > 0)
                {
                    Console.WriteLine("Byte alignment error");
                    throw new Exception();
                }
                var pathStartByteAddr = (instrSets[i].First().BitAddress - instrSets[0].First().BitAddress) >> BitByteShift;
                var opcode2Bit        = MiscOps.GetOpcode2Bit(pathStartByteAddr, out var valueBitWidth);
                contextRegion.BitfieldWrapper.AddBitfield(new Bitfield(width: 2, value: opcode2Bit, name: "byte width opcode"));
                contextRegion.BitfieldWrapper.AddBitfield(new Bitfield(width: valueBitWidth, value: pathStartByteAddr, name: $"path={i} byte addr"));

                // Add current path byte-length bitfield:
                var instrPathByteLen = (instrSets[i].Last().BitAddress + instrSets[i].Last().BitfieldWrapper.BitWidth - instrSets[i].First().BitAddress) >> BitByteShift;
                opcode2Bit = MiscOps.GetOpcode2Bit(instrPathByteLen, out valueBitWidth);
                contextRegion.BitfieldWrapper.AddBitfield(new Bitfield(width: 2, value: opcode2Bit, name: "byte width opcode"));
                contextRegion.BitfieldWrapper.AddBitfield(new Bitfield(width: valueBitWidth, value: instrPathByteLen, name: $"path={i} byte len"));

                // Keep running total of paths length:
                totalInstrRegionByteLen += instrPathByteLen;
                maxInstrPathByteSize     = Math.Max(maxInstrPathByteSize, instrPathByteLen);

                // Add current path instruction bit address bitfield:
                var instrBitAddr = instrPathByteLen << BitByteShift;    // max possible value is number of bits in path.
                opcode2Bit = MiscOps.GetOpcode2Bit(instrBitAddr, out valueBitWidth);
                contextRegion.BitfieldWrapper.AddBitfield(new Bitfield(width: 2, value: opcode2Bit, name: "byte width opcode"));
                contextRegion.BitfieldWrapper.AddBitfield(new Bitfield(width: valueBitWidth, value: 0, name: $"path={i} instr bit addr"));

                // Add current path extra-value bitfield:
                uint maxValue   = 0;
                var  rampInstrs = instrSets[i].Where(x => x.Type == Instruction.InstrType.GlowRamp);
                if (rampInstrs.Count() > 0)
                {
                    maxValue = rampInstrs.Max(x => ((GlowRampInstruction)x).RampTicks);
                }
                var opcode3Bit = MiscOps.GetOpcode3Bit(maxValue, out valueBitWidth);
                contextRegion.BitfieldWrapper.AddBitfield(new Bitfield(width: 3, value: opcode3Bit, name: "byte width opcode"));
                if (valueBitWidth > 0)
                {
                    contextRegion.BitfieldWrapper.AddBitfield(new Bitfield(width: valueBitWidth, value: 0, name: $"path={i} extra value"));
                }

                // Add current path pause-ticks bitfield:
                maxValue = 0;
                var pauseInstrs = instrSets[i].Where(x => x.Type == Instruction.InstrType.Pause);
                if (pauseInstrs.Count() > 0)
                {
                    maxValue = pauseInstrs.Max(x => ((PauseInstruction)x).Ticks);
                }
                else if (rampInstrs.Count() > 0)
                {
                    maxValue = 1;
                }
                opcode3Bit = MiscOps.GetOpcode3Bit(maxValue, out valueBitWidth);
                contextRegion.BitfieldWrapper.AddBitfield(new Bitfield(width: 3, value: opcode3Bit, name: "byte width opcode"));
                if (valueBitWidth > 0)
                {
                    contextRegion.BitfieldWrapper.AddBitfield(new Bitfield(width: valueBitWidth, value: 0, name: $"path={i} pause ticks"));
                }
            }

            // Set total instruction region byte length:
            instrRegionByteLenBitfield.SetValue(totalInstrRegionByteLen);

            // Add padding to context-region so following instruction path memory is byte-aligned:
            var paddingWidth = BitsPerByte - (contextRegion.BitfieldWrapper.BitWidth % BitsPerByte);

            if (paddingWidth < 8)
            {
                contextRegion.BitfieldWrapper.AddBitfield(new Bitfield(paddingWidth, "byte-align padding"));
            }

            // Set total context-region byte length (convert from bits):
            contextRegionByteSize = contextRegion.BitfieldWrapper.BitWidth >> BitByteShift;
            contextRegionByteLenBitfield.SetValue(contextRegionByteSize);

            return(contextRegion);
        }
示例#8
0
 internal void AddBitfield(Bitfield bitfield)
 {
     _bitfieldList.Add(bitfield);
 }
        internal override void ToBitfieldWrapper()
        {
            var isCrossPath = CrossPathLedList.Any(x => LedIdxList.Contains(x));

            _bitfieldWrapper = new GlowRampBitfieldWrapper();

            // Add ramp tick opcode:
            var opcode2Bit = MiscOps.GetOpcode2Bit(RampTicks, out var valueBitWidth);

            _bitfieldWrapper.RampTickOpcode.SetValue(opcode2Bit);
            _bitfieldWrapper.AddBitfield(_bitfieldWrapper.RampTickOpcode);

            // Add ramp tick value:
            _bitfieldWrapper.RampTickValue = new Bitfield(width: valueBitWidth, value: RampTicks, name: "ramp tick value");
            _bitfieldWrapper.AddBitfield(_bitfieldWrapper.RampTickValue);

            // Add color bitmap (flags set below):
            _bitfieldWrapper.AddBitfield(_bitfieldWrapper.ColorBitmap);

            if (LedColorDiff.Red != 0 || isCrossPath)
            {
                // Add color to bitmap:
                _bitfieldWrapper.ColorBitmap.SetFlag((uint)ColorSpecifier.Red);

                // Add initial color value:
                var bitfield = new Bitfield(width: 8, value: (uint)LedColorFrom.Red, name: "red init value");
                _bitfieldWrapper.AddBitfield(bitfield);

                // Set color inc/dec opcode:
                opcode2Bit = (uint)(LedColorDiff.Red == 0 ? 0 : LedColorDiff.Red > 0 ? 1 : 2);
                bitfield   = new Bitfield(width: 2, value: opcode2Bit, name: "inc/dec opcode");
                _bitfieldWrapper.AddBitfield(bitfield);

                if (LedColorDiff.Red != 0)
                {
                    // Set color byte width opcode:
                    opcode2Bit = MiscOps.GetOpcode2Bit((uint)TickStep.Red, out valueBitWidth);
                    bitfield   = new Bitfield(width: 2, value: opcode2Bit, name: "byte width opcode");
                    _bitfieldWrapper.AddBitfield(bitfield);

                    // Set tick step value:
                    bitfield = new Bitfield(width: valueBitWidth, value: (uint)TickStep.Red, name: "red tick step value");
                    _bitfieldWrapper.AddBitfield(bitfield);

                    // Set color step value:
                    bitfield = new Bitfield(width: 8, value: (uint)ColorStep.Red, name: "red color step value");
                    _bitfieldWrapper.AddBitfield(bitfield);
                }
            }

            if (LedColorDiff.Green != 0 || isCrossPath)
            {
                // Add color to bitmap:
                _bitfieldWrapper.ColorBitmap.SetFlag((uint)ColorSpecifier.Green);

                // Add initial color value:
                var bitfield = new Bitfield(width: 8, value: (uint)LedColorFrom.Green, name: "green init value");
                _bitfieldWrapper.AddBitfield(bitfield);

                // Set color inc/dec opcode:
                opcode2Bit = (uint)(LedColorDiff.Green == 0 ? 0 : LedColorDiff.Green > 0 ? 1 : 2);
                bitfield   = new Bitfield(width: 2, value: opcode2Bit, name: "inc/dec opcode");
                _bitfieldWrapper.AddBitfield(bitfield);

                if (LedColorDiff.Green != 0)
                {
                    // Set color byte width opcode:
                    opcode2Bit = MiscOps.GetOpcode2Bit((uint)TickStep.Green, out valueBitWidth);
                    bitfield   = new Bitfield(width: 2, value: opcode2Bit, name: "byte width opcode");
                    _bitfieldWrapper.AddBitfield(bitfield);

                    // Set tick step value:
                    bitfield = new Bitfield(width: valueBitWidth, value: (uint)TickStep.Green, name: "green tick step value");
                    _bitfieldWrapper.AddBitfield(bitfield);

                    // Set color step value:
                    bitfield = new Bitfield(width: 8, value: (uint)ColorStep.Green, name: "green color step value");
                    _bitfieldWrapper.AddBitfield(bitfield);
                }
            }

            if (LedColorDiff.Blue != 0 || isCrossPath)
            {
                // Add color to bitmap:
                _bitfieldWrapper.ColorBitmap.SetFlag((uint)ColorSpecifier.Blue);

                // Add initial color value:
                var bitfield = new Bitfield(width: 8, value: (uint)LedColorFrom.Blue, name: "blue init value");
                _bitfieldWrapper.AddBitfield(bitfield);

                // Set color inc/dec opcode:
                opcode2Bit = (uint)(LedColorDiff.Blue == 0 ? 0 : LedColorDiff.Blue > 0 ? 1 : 2);
                bitfield   = new Bitfield(width: 2, value: opcode2Bit, name: "inc/dec opcode");
                _bitfieldWrapper.AddBitfield(bitfield);

                if (LedColorDiff.Blue != 0)
                {
                    // Set color byte width opcode:
                    opcode2Bit = MiscOps.GetOpcode2Bit((uint)TickStep.Blue, out valueBitWidth);
                    bitfield   = new Bitfield(width: 2, value: opcode2Bit, name: "byte width opcode");
                    _bitfieldWrapper.AddBitfield(bitfield);

                    // Set tick step value:
                    bitfield = new Bitfield(width: valueBitWidth, value: (uint)TickStep.Blue, name: "blue tick step value");
                    _bitfieldWrapper.AddBitfield(bitfield);

                    // Set color step value:
                    bitfield = new Bitfield(width: 8, value: (uint)ColorStep.Blue, name: "blue color step value");
                    _bitfieldWrapper.AddBitfield(bitfield);
                }
            }

            if (LedColorDiff.Bright != 0 || isCrossPath)
            {
                // Add color to bitmap:
                _bitfieldWrapper.ColorBitmap.SetFlag((uint)ColorSpecifier.Bright);

                // Add initial color value:
                var bitfield = new Bitfield(width: 8, value: (uint)LedColorFrom.Bright, name: "bright init value");
                _bitfieldWrapper.AddBitfield(bitfield);

                // Set color inc/dec opcode:
                opcode2Bit = (uint)(LedColorDiff.Bright == 0 ? 0 : LedColorDiff.Bright > 0 ? 1 : 2);
                bitfield   = new Bitfield(width: 2, value: opcode2Bit, name: "inc/dec opcode");
                _bitfieldWrapper.AddBitfield(bitfield);

                if (LedColorDiff.Bright != 0)
                {
                    // Set color byte width opcode:
                    opcode2Bit = MiscOps.GetOpcode2Bit((uint)TickStep.Bright, out valueBitWidth);
                    bitfield   = new Bitfield(width: 2, value: opcode2Bit, name: "byte width opcode");
                    _bitfieldWrapper.AddBitfield(bitfield);

                    // Set tick step value:
                    bitfield = new Bitfield(width: valueBitWidth, value: (uint)TickStep.Bright, name: "bright tick step value");
                    _bitfieldWrapper.AddBitfield(bitfield);

                    // Set color step value:
                    bitfield = new Bitfield(width: 8, value: (uint)ColorStep.Bright, name: "bright color step value");
                    _bitfieldWrapper.AddBitfield(bitfield);
                }
            }

            // Add led bitmap (led index increases from left to right):
            _bitfieldWrapper.LedBitmap = new Bitfield(width: DeviceTable.Dev.LedCount, name: "led bitmap");
            LedIdxList.ToList().ForEach(idx => _bitfieldWrapper.LedBitmap.SetFlag((uint)(DeviceTable.Dev.LedCount - idx - 1)));
            _bitfieldWrapper.AddBitfield(_bitfieldWrapper.LedBitmap);
        }