How to use a byte to read variables bit

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

How to use a byte to read variables bit

Post by Doj » Mon Nov 12, 2007 7:54 pm

I have code I have used previouly in PDS that I just can not find a way of emulating in SF.

Code: Select all

dim loop as byte
dim temp_longword as longword

while loop<32'a longword is being accessed a bit at a time
    if temp_longword.loop=1 then
        'do something here
    endif
    inc(loop)
wend
What is the syntax please?

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Mon Nov 12, 2007 8:06 pm

Try the following:

Code: Select all

dim loop as byte 
dim temp_longword as longword 

loop = 0

while loop < 32'a longword is being accessed a bit at a time 
    if temp_longword.Bits(loop)=1 then 
        'do something here 
    endif 
    inc(loop) 
wend
For further info, see the 'Alias and Modifiers' section in the help file.

Hope this helps.

Steve

yo2lio
Posts: 39
Joined: Tue Sep 25, 2007 6:52 pm
Location: Arad, Romania
Contact:

Post by yo2lio » Mon Nov 12, 2007 10:08 pm

Code: Select all

{
****************************************************************
*  Name    : BIT_lib.BAS                                       *
*  Author  : Florin Andrei Medrea                              *
*  Notice  : Copyright (c) 2007 - YO2LIO -                     *
*          : All Rights Reserved                               *
*  Date    : 10/5/2007                                         *
*  Version : 1.0                                               *
*  Notes   : Library for accesing individual bits              *
*          :                                                   *
****************************************************************
}
Module BIT_lib

Include "SYS_lib"

Dim gen_buf As Word

Sub ClearB(bit_pos As Byte)
  Save(FSR2)
  FSR2 = gen_buf
  bit_pos = bit_pos And $07
  bit_pos = GetMask(bit_pos)
  ASM
    comf bit_pos,w
    andwf INDF2,f
  End ASM
  Restore
End Sub

Sub SetB(bit_pos As Byte)
  Save(FSR2)
  FSR2 = gen_buf
  bit_pos = bit_pos And $07
  bit_pos = GetMask(bit_pos)
  ASM
    movf bit_pos,w
    iorwf INDF2,f
  End ASM
  Restore
End Sub

Sub TestB(bit_pos As Byte)
  Save(FSR2)
  FSR2 = gen_buf
  bit_pos = bit_pos And $07
  bit_pos = GetMask(bit_pos)
  ASM
    movf bit_pos,w
    andwf INDF2,w
  End ASM
  Restore
End Sub

Public Sub ClearBit(ByRef data As Byte, bit_pos As Byte)
  gen_buf = AddressOf(data)
  ClearB(bit_pos)
End Sub

Public Sub SetBit(ByRef data As Byte, bit_pos As Byte)
  gen_buf = AddressOf(data)
  SetB(bit_pos)
End Sub

Public Function TestBit(ByRef data As Byte, bit_pos As Byte) As Byte
  result = 0
  gen_buf = AddressOf(data)
  TestB(bit_pos)
  If WREG <> 0 Then Inc(result) End If
End Function

Public Sub ClearBit(ByRef data As ShortInt, bit_pos As Byte)
  gen_buf = AddressOf(data)
  ClearB(bit_pos)
End Sub

Public Sub SetBit(ByRef data As ShortInt, bit_pos As Byte)
  gen_buf = AddressOf(data)
  SetB(bit_pos)
End Sub

Public Function TestBit(ByRef data As ShortInt, bit_pos As Byte) As Byte
  result = 0
  gen_buf = AddressOf(data)
  TestB(bit_pos)
  If WREG <> 0 Then Inc(result) End If
End Function

Public Sub ClearBit(ByRef data As Word, bit_pos As Byte)
  ASM
    rlncf bit_pos,w
    swapf WREG,f
    andlw 3
  End ASM
  gen_buf = AddressOf(data) + WREG
  ClearB(bit_pos)
End Sub

Public Sub SetBit(ByRef data As Word, bit_pos As Byte)
  ASM
    rlncf bit_pos,w
    swapf WREG,f
    andlw 3
  End ASM
  gen_buf = AddressOf(data) + WREG
  SetB(bit_pos)
End Sub

Public Function TestBit(ByRef data As Word, bit_pos As Byte) As Byte
  result = 0
  ASM
    rlncf bit_pos,w
    swapf WREG,f
    andlw 3
  End ASM
  gen_buf = AddressOf(data) + WREG
  TestB(bit_pos)
  If WREG <> 0 Then Inc(result) End If
End Function

Public Sub ClearBit(ByRef data As Integer, bit_pos As Byte)
  ASM
    rlncf bit_pos,w
    swapf WREG,f
    andlw 3
  End ASM
  gen_buf = AddressOf(data) + WREG
  ClearB(bit_pos)
End Sub

Public Sub SetBit(ByRef data As Integer, bit_pos As Byte)
  ASM
    rlncf bit_pos,w
    swapf WREG,f
    andlw 3
  End ASM
  gen_buf = AddressOf(data) + WREG
  SetB(bit_pos)
End Sub

Public Function TestBit(ByRef data As Integer, bit_pos As Byte) As Byte
  result = 0
  ASM
    rlncf bit_pos,w
    swapf WREG,f
    andlw 3
  End ASM
  gen_buf = AddressOf(data) + WREG
  TestB(bit_pos)
  If WREG <> 0 Then Inc(result) End If
End Function

Public Sub ClearBit(ByRef data As LongWord, bit_pos As Byte)
  ASM
    rlncf bit_pos,w
    swapf WREG,f
    andlw 3
  End ASM
  gen_buf = AddressOf(data) + WREG
  ClearB(bit_pos)
End Sub

Public Sub SetBit(ByRef data As LongWord, bit_pos As Byte)
  ASM
    rlncf bit_pos,w
    swapf WREG,f
    andlw 3
  End ASM
  gen_buf = AddressOf(data) + WREG
  SetB(bit_pos)
End Sub

Public Function TestBit(ByRef data As LongWord, bit_pos As Byte) As Byte
  result = 0
  ASM
    rlncf bit_pos,w
    swapf WREG,f
    andlw 3
  End ASM
  gen_buf = AddressOf(data) + WREG
  TestB(bit_pos)
  If WREG <> 0 Then Inc(result) End If
End Function

Public Sub ClearBit(ByRef data As LongInt, bit_pos As Byte)
  ASM
    rlncf bit_pos,w
    swapf WREG,f
    andlw 3
  End ASM
  gen_buf = AddressOf(data) + WREG
  ClearB(bit_pos)
End Sub

Public Sub SetBit(ByRef data As LongInt, bit_pos As Byte)
  ASM
    rlncf bit_pos,w
    swapf WREG,f
    andlw 3
  End ASM
  gen_buf = AddressOf(data) + WREG
  SetB(bit_pos)
End Sub

Public Function TestBit(ByRef data As LongInt, bit_pos As Byte) As Byte
  result = 0
  ASM
    rlncf bit_pos,w
    swapf WREG,f
    andlw 3
  End ASM
  gen_buf = AddressOf(data) + WREG
  TestB(bit_pos)
  If WREG <> 0 Then Inc(result) End If
End Function

Public Sub ClearBitArray(ByRef data() As Byte, byte_pos As Word, bit_pos As Byte)
  gen_buf = AddressOf(data) + byte_pos
  ClearB(bit_pos)
End Sub

Public Sub SetBitArray(ByRef data() As Byte, byte_pos As Word, bit_pos As Byte)
  gen_buf = AddressOf(data) + byte_pos
  SetB(bit_pos)
End Sub

Public Function TestBitArray(ByRef data() As Byte, byte_pos As Word, bit_pos As Byte) As Byte
  result = 0
  gen_buf = AddressOf(data) + byte_pos
  TestB(bit_pos)
  If WREG <> 0 Then Inc(result) End If
End Function

Public Sub ClearBitArray(ByRef data As String, byte_pos As Word, bit_pos As Byte)
  gen_buf = AddressOf(data) + byte_pos
  ClearB(bit_pos)
End Sub

Public Sub SetBitArray(ByRef data As String, byte_pos As Word, bit_pos As Byte)
  gen_buf = AddressOf(data) + byte_pos
  SetB(bit_pos)
End Sub

Public Function TestBitArray(ByRef data As String, byte_pos As Word, bit_pos As Byte) As Byte
  result = 0
  gen_buf = AddressOf(data) + byte_pos
  TestB(bit_pos)
  If WREG <> 0 Then Inc(result) End If
End Function

Code: Select all

{
****************************************************************
*  Name    : SYS_lib.bas                                       *
*  Author  : Florin Andrei Medrea                              *
*  Notice  : Copyright (c) 20067 - YO2LIO -                    *
*          : All Rights Reserved                               *
*  Date    : 28/10/2007                                        *
*  Version : 1.1                                               *
*  Notes   :                                                   *
*          :                                                   *
****************************************************************
}
Module SYS_lib
{
****************************************************************************
* Name    : ClrWDT                                                         *
* Purpose : Clear the watch dog timer                                      *
****************************************************************************
}
Public Inline Sub ClrWDT()
   ASM-
      ClrWDT
   End ASM
End Sub
{
****************************************************************************
* Name    : Nop                                                            *
* Purpose : Single NOP instruction                                         *
****************************************************************************
}
Public Inline Sub Nop()
   ASM-
      Nop
   End ASM
End Sub      
{
****************************************************************************
* Name    : Reset                                                          *
* Purpose : Reset instruction                                              *
****************************************************************************
}
Public Inline Sub Reset()
   ASM-
      Reset
   End ASM
End Sub
{
****************************************************************************
* Name    : Sleep                                                          *
* Purpose : Sleep instruction                                              *
****************************************************************************
}
Public Inline Sub Sleep()
   ASM-
      Sleep
   End ASM
End Sub  
{
****************************************************************************
* Name    : Swap_F                                                         *
* Purpose : Swapf instruction (exchange the upper and lower                *
*           nibbles of register ‘f’)                                       *
****************************************************************************
}
Public Inline Function Swap_F(data_b As Byte) As Byte
   ASM-
      Swapf data_b,w
   End ASM
   result = WREG
End Function
{
****************************************************************************
* Name    : Neg_F                                                          * 
* Purpose : Negf instruction ('f' is negated using two’s complement        *
****************************************************************************
}
Public Inline Function Neg_F(data_b As Byte) As Byte
   ASM-
      Negf data_b,w
   End ASM
   result = WREG
End Function
{
****************************************************************************
* Name    : SetBank                                                        * 
* Purpose : Set memmory bank                                               *
****************************************************************************
}
Public Inline Sub SetBank(data_b As Byte)
   ASM-
      Movlb data_b
   End ASM
End Sub
{
****************************************************************************
* Name    : Daw_F                                                          * 
* Purpose : Adjusts the eight-bit value in W                               *
*           resulting from the earlier addition of two                     *
*           variables (each in packed BCD format)                          *
*           and produces a correct packed BCD                              *
****************************************************************************
}
Public Inline Function Daw_F(data_b As Byte) As Byte
   ASM-
      movf data_b,w
      daw
   End ASM
   result = WREG
End Function
{
****************************************************************************
* Name    : GetMask                                                        *
* Purpose : Get mask of bit 0 - 7                                          *
****************************************************************************
}
Public Function GetMask(data As Byte) As Byte
Dim i As Byte
  result = 1
  i = 0
  While i < data
    ASM
      Rlncf result,f
    End ASM
    Inc(i)
  Wend
End Function
You have Test_Bit function .........

Sorry, I don't have time right now to put my UserLibrary in WIKI.

Till then : http://www.microelemente.ro/Swordfish/UserLibrary/
Best regards, Florin Medrea
My UserLibrary Folder http://www.microelemente.ro/Swordfish/UserLibrary/

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Mon Nov 12, 2007 10:17 pm

Just a quick look and this one can be rewritten a tad:

Code: Select all

Public Inline Function Swap_F(data_b As Byte) As Byte
   ASM-
      Swapf data_b,w
   End ASM
   result = WREG
End Function 
To this:

Code: Select all

Public Inline Function Swap_F(data_b As Byte) As WREG
   ASM-
      Swapf data_b,w
   End ASM
End Function
Swordfish is a really slick tool!
Last edited by xor on Mon Nov 12, 2007 10:21 pm, edited 1 time in total.

yo2lio
Posts: 39
Joined: Tue Sep 25, 2007 6:52 pm
Location: Arad, Romania
Contact:

Post by yo2lio » Mon Nov 12, 2007 10:21 pm

xor wrote:Swordfish is really a slick tool!
Thanks XOR :!: :!: :!: :D
Best regards, Florin Medrea
My UserLibrary Folder http://www.microelemente.ro/Swordfish/UserLibrary/

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Mon Nov 12, 2007 11:10 pm

You could do that in a few places actually... good work!

Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Tue Nov 13, 2007 7:14 pm

Thanks everyone, sorted me out no problem.

Post Reply