internal override void ToBitfieldWrapper() { _bitfieldWrapper = new PauseBitfieldWrapper(); // Set byte width opcode: _bitfieldWrapper.TickOpcode.SetValue(MiscOps.GetOpcode2Bit(Ticks, out var tickValueBitWidth)); var valueBitfield = new Bitfield(width: tickValueBitWidth, value: Ticks, name: "tick value"); _bitfieldWrapper.AddBitfield(valueBitfield); }
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); }
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); }