Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 00:04 05 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MMBasic Logic Function Library

Author Message
Chris Roper
Senior Member

Joined: 19/05/2015
Location: South Africa
Posts: 280
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/
 
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1097
Posted: 01:44pm 01 Jun 2015
Copy link to clipboard 
Print this post

Hi Chris,

Very interesting and clever way of implementing a bit stack using a single 64 bit integer. I kept looking for the array before the penny dropped

panky

... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 09:11pm 01 Jun 2015
Copy link to clipboard 
Print this post

Nice work Chris! Keep it going!
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1328
Posted: 12:22am 02 Jun 2015
Copy link to clipboard 
Print this post

Hi Chris,

You should submit these to Peter Carnegie (G8JCM) for the Micromite library - see the 'Sticky' post at the top of the Forum Topic list.

Greg
 
Chris Roper
Senior Member

Joined: 19/05/2015
Location: South Africa
Posts: 280
Posted: 08:55pm 02 Jun 2015
Copy link to clipboard 
Print this post

  paceman said   Hi Chris,

You should submit these to Peter Carnegie (G8JCM) for the Micromite library - see the 'Sticky' post at the top of the Forum Topic list.

Greg


Thanks Greg, I did think of submitting them but decided to hold off till I had used them in a couple of applications, in case I needed to add or change any functions. Whilst the logic is sound I may change a few functions into subs for better optimisation and ease of use. Several should be viewed as Commands rather than Functions.

At this stage though they appear stable in use, and I have made use of them in another library I am working on.

The more I use MMBasic the more I learn, so the chances are good that both library's will improve with my own skill.

Cheers
ChrisEdited by Chris Roper 2015-06-04
http://caroper.blogspot.com/
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024