Posted: 01:45am 01 Jun 2015 |
Copy link to clipboard |
Print this post |
|
It is cold and wet here in the "fairest Cape" and my solder Station is outside :(
So I have suspended work on the devMite for a bit and turned my attention to MMBasic.
I needed some Bitwise Logic Functions and thought others may find them useful too.
They Complement the PORT command and PORT() Function and are also useful if driving Port Expanders etc.
For more advanced Logic I have also implemented a Bitwise Stack
' LIBRARY - Logic Functions
' C.A.Roper - 2015/06/01
' Version 1.0
' _____________________________________________________________________________
' Bitwise Logic Functions
' _____________________________________________________________________________
' OnesComp%( Value% )
' Returns the BitWise NOT Function of Value% Equivalent to '~' operator in C
' BitGet( Value%, BitNumber )
' Returns the State ( 1 or 0 ) of BitNumber of Value%
' BitSet( Value%, BitNumber )
' Forces BitNumber of Value% to 1
' Returns the new Value%
' BitClr( Value%, BitNumber )
' Forces BitNumber of Value% to 0
' Returns the new Value%
' BitTgl( Value%, BitNumber )
' Toggles the State of BitNumber of Value%
' Returns the new Value%
' BitSet( Value%, BitNumber, BitState )
' Forces BitNumber of Value% to the State of BitState
' Where State is the Boolean Value of BitState,
' 0 or null = FALSE
' any other value = TRUE
' Returns the new Value%
' _____________________________________________________________________________
' Bitwise Stack Functions
' _____________________________________________________________________________
' A "Stack Overflow Error"
' Generated if > 64 Stack Entry's Result
' A "Stack Underflow Error"
' Generated if More Values are pulled than Entries Made
' BitPush( BitState )
' Pushes State into the Bottom of the Stack
' Where State is the Boolean Value of BitState,
' 0 or null = FALSE
' any other value = TRUE
' Increments Stack Pointer
' Updates the Stack
' Returns the New Stack
' BitPull()
' Pulls The State from the Bottom of the Stack
' Decrements the Stack Pointer
' Updates the Stack
' Returns the State Pulled
' BitSwap()
' Reverses the Order of the last two Entries in the Stack
' Stack Pointer is not effected
' Updates the Stack
' Returns the New Stack
' BitDup()
' Duplicates the Bottom Stack Entry
' Increments the Stack Pointer
' Updates the Stack
' Returns the new stack
' _____________________________________________________________________________
' Bitwise Logic Functions
FUNCTION OnesComp%(in%):OnesComp% = in% XOR &HFFFFFFFF:END FUNCTION
FUNCTION BitGet(in%, Bit):BitGet = (in% >> Bit) AND 1:END FUNCTION
FUNCTION BitSet(in%, Bit):BitSet = in% OR (1 << Bit):END FUNCTION
FUNCTION BitClr(in%, Bit):BitClr = in% AND OnesComp%(1 << Bit):END FUNCTION
FUNCTION BitPut(in%, Bit, Val):BitPut = BitClr(in%, Bit) OR ((NOT NOT Val) << Bit):END FUNCTION
FUNCTION BitTgl(in%, Bit):BitTgl = in% XOR (1 << Bit):END FUNCTION
' _____________________________________________________________________________
' Bitwise Stack Functions
DIM BITSTACK% = 0
DIM BITPTR = 0
FUNCTION BitPush(Val)
BitPush = (BITSTACK% << 1) OR NOT NOT Val
BitStack% = BitPush
BITPTR = BITPTR + 1
IF BITPTR > 64 THEN ERROR ("Stack Overflow")
END FUNCTION
FUNCTION BitPull()
BitPull = BitGet(BITSTACK%, 0)
BITSTACK% = (BITSTACK% >> 1)
BITPTR = BITPTR - 1
IF BITPTR < 0 THEN ERROR ("Stack Underflow")
END FUNCTION
FUNCTION BitSwap()
Bit0 = BitPull()
Bit1 = BitPull()
BitSwap = BitPush(Bit0)
BitSwap = BitPush(Bit1)
END FUNCTION
FUNCTION BitDup()
BitDup = BitPush(BitGet(BITSTACK%,0))
END FUNCTION
' _____________________________________________________________________________
The Comment block is far larger than the actual Library so it included at the top to allow easy cropping in the editor before use.
Cheers
Chris
EDIT: Note to Self
Build an Extractor and Move Solder Station indoors for winter :)
Edited by Chris Roper 2015-06-02 http://caroper.blogspot.com/ |