''' <summary>
''' Writes the specified value to the buffer.
''' </summary>
''' <param name="Offset">Required. The offset to start writing.</param>
''' <param name="Bit">Required. The bit to start writing.</param>
''' <param name="Length">Required. The number of bits to write.</param>
''' <param name="Value">Required. The value to write.</param>
Public Shared Sub NumberBase(ByVal Offset As Int32, ByVal Bit As Int32, ByVal Length As Int32, ByVal Value As Int32)
Try
Dim b As Int32 = Bit ' Bit currently being manipulated
Dim ofs As Int32 = Offset ' Offset being written
Dim val As Int32 = 0 ' Value to write
Dim State As Boolean ' State of current bit
' Precautionary removal of negative sign.
Length = Math.Abs(Length)
' Modify the bit and offset accordingly.
While b > 8
b -= 8
ofs += 1
End While
' Shift the value to the left.
val = Value << (32 - Length - b)
' Loop through each bit.
For i As Int32 = b To Length + b - 1
State = CBool((val >> (31 - i)) And 1) ' Get the state of the bit.
BitflagBase(ofs, i, State, False) ' Set it in the buffer.
Next
Catch ex As Exception
GetError(ex)
End Try
End Sub
''' <summary>
''' Sets the state of the specified bit relative to the specified offset, inverting if necessary.
''' </summary>
''' <param name="Offset">Required. The offset at which to start.</param>
''' <param name="Bit">Required. The bit whose state is to be set.</param>
''' <param name="State">Required. Whether or not the bit is set.</param>
''' <param name="Invert">Required. Inverts the state.</param>
Public Shared Sub BitflagBase(Offset As Int32, Bit As Int32, State As Boolean, Invert As Boolean)
Try
Dim b As Int32 = Bit
Dim ofs As Int32 = Offset
Dim temp As Int32 = 0
' If the bit we want isn't in the offset provided,
' Move on to the next offset.
While b >= 8
b -= 8
ofs += 1
End While
' Read the byte.
Select Case EditType
Case EditTypeEnum.Buffer
temp = Common.Buffer(ofs)
Case EditTypeEnum.Memory_IRAM
temp = ReadIRAM(ofs, 1)
Case EditTypeEnum.Memory_WRAM
temp = ReadWRAM(ofs, 1)
End Select
' Invert, if necessary.
State = If(Invert, Not State, State)
' Manipulate the bit.
temp = If(State, temp Or (1 << (7 - b)), temp And Not (1 << (7 - b)))
' Write the byte.
Select Case EditType
Case EditTypeEnum.Buffer
Common.Buffer(ofs) = CByte(temp And &HFF)
Case EditTypeEnum.Memory_IRAM
WriteIRAM(ofs, 1, temp And &HFF)
Case EditTypeEnum.Memory_WRAM
WriteWRAM(ofs, 1, temp And &HFF)
End Select
Catch ex As Exception
GetError(ex)
End Try
End Sub