Get MBLogic at SourceForge.net. Fast, secure and Free Open Source 
		software downloads

Help Topics

Topic Details for MBLogicEngine

Help - Soft Logic Instructions


Overview:

This page provides a description of each instruction list (IL) instruction used in the Ck soft logic library. "Ck" is a soft logic library which is modeled closely on the Koyo "Click" PLC. The Ck library is not intended to provide an exact emulation of the "Click". Rather it is intended to provide an instruction set which is similar enough to the "Click" that someone who is already familiar with the "Click" will understand the "Ck" library fairly easily.

The most significant difference between the two systems is the Koyo "Click" does not have a documented IL instruction set. The IL instructions used below have been interpolated from a combination of the ladder instructions used by the "Click", and the IL instructions used in the Koyo DL-205.

This document is not intended as a primer in PLC programming in general or in IL programming. The reader is assumed to be already familiar with PLC programming concepts, conventions, and terminology.


Instruction Summary:

Instruction Instr Type Description
// Program Formatting Comment line
AND Boolean input AND bit with top of logic stack
ANDE Compare AND if parm1 equals param2
ANDGE Compare AND if parm1 >= parm2
ANDGT Compare AND if parm1 > parm2
ANDLE Compare AND if parm1 <= parm2
ANDLT Compare AND if parm1 < parm2
ANDN Boolean input AND NOT bit with top of logic stack
ANDND Edge contact AND negative differential
ANDNE Compare AND if parm1 is not equal to parm2
ANDPD Edge contact AND positive differential
ANDSTR Boolean input AND top two values on logic stack
CALL Program control Call subroutine
CNTD Counter/Timer Count down
CNTU Counter/Timer Count up
COPY Copy Copy a single value to a register
CPYBLK Copy Copy a block of data
END Program control Program end
ENDC Program control Program end conditional
FILL Copy Fill a block of data
FINDEQ Search Search table for equal to
FINDGE Search Search table for >=
FINDGT Search Search table for >
FINDIEQ Search Incremental search table for equal to
FINDIGE Search Incremental search table for >=
FINDIGT Search Incremental search table for >
FINDILE Search Incremental search table for <=
FINDILT Search Incremental search table for <
FINDINE Search Incremental search table for not equal
FINDLE Search Search table for <=
FINDLT Search Search table for <
FINDNE Search Search table for not equal
FOR Program control For/next loop
MATHDEC Math Decimal math
MATHHEX Math Hexadecimal math
NETWORK Program Formatting Network
NEXT Program control Next in For/next loop
OR Boolean input OR bit with top of logic stack
ORE Compare OR if parm1 equals parm2
ORGE Compare OR if parm1 >= parm2
ORGT Compare OR if parm1 > parm2
ORLE Compare OR if parm1 <= parm2
ORLT Compare OR if parm1 < parm2
ORN Boolean input OR NOT bit with top of logic stack
ORND Edge contact OR negative differential
ORNE Compare OR if parm1 is not equal to parm2
ORPD Edge contact OR positive differential
ORSTR Boolean input OR top two values on logic stack
OUT Boolean output Output logic stack to bit
OUT Boolean output Output logic stack to multiple bits
PACK Copy Pack bits into a register
PD Boolean output Output logic stack one shot to multiple bits
PD Boolean output Output logic stack one shot
RST Boolean output Reset multiple bits if logic stack true
RST Boolean output Reset bit if logic stack true
RT Program control Return from subroutine
RTC Program control Return from subroutine conditional
SBR Program control Define a subroutine
SET Boolean output Set multiple bits if logic stack true
SET Boolean output Set bit if logic stack true
SHFRG Shift register Shift register move bits to right
STR Boolean input Store bit onto logic stack
STRE Compare STR if parm1 equals parm2
STRGE Compare STR if parm1 >= parm2
STRGT Compare STR if parm1 > parm2
STRLE Compare STR if parm1 <= parm2
STRLT Compare STR if parm1 < parm2
STRN Boolean input Store NOT bit onto logic stack
STRND Edge contact STORE negative differential
STRNE Compare STR if parm1 is not equal to parm2
STRPD Edge contact STORE positive differential
SUM Math Sum a range of registers
TMR Counter/Timer On delay timer
TMRA Counter/Timer On delay accumulating timer
TMROFF Counter/Timer Off delay timer
UDC Counter/Timer Up/down counter
UNPACK Copy Unpack bits from a register

The Logic Stack:

A logic stack is one of the fundamental features of all conventional PLCs, although its presence is often not explicitly mentioned. The logic stack is the location where the results of logic operations are stored, and where many instructions implicitly draw one or more of their Boolean parameters.

The logic stack is implemented as a stack. This stack has an undefined maximum size, but the limit may be assumed to be very large, and for all practical purposes unlimited. The minimum size is zero.

Instructions which use the logic stack may push a value onto the stack, modify the top of the stack, read the top of the stack, or read any value below the top of the stack. In the instruction documentation, the top of the logic stack is refered to as "the top of the logic stack", "top of stack", or just "logic stack". Values in locations below the top of the stack are refered to as the logic stack value (top - 1) (the value just below the top of stack), or logic stack value (top - 2)(the value two positions below the top of stack). No instruction reads more than two positions below the top of stack.

Whenever a new network (rung) is started, the logic stack is cleared and the top of the stack is initialised to "false". If an instruction attempts to read a logic stack location which does not exist a value of "false" is returned to the instruction. This means that attempting to read an invalid logic stack location does not result in a run time error, but it does mean the instruction will never become true.

When a subroutine is called, the logic stack is re-initialised. When the subroutine returns, the state of the logic stack is undefined. This means that a subroutine must not rely on the logic stack being in a particular state when called, and the calling location must not rely on the logic stack being either set by the subroutine or being in the same state as before the subroutine call.

Example:

	// Re-initialise the logic stack.
	// Push a value onto the stack. The top of stack is now true.
	NETWORK 1
	STR SC1
	
	// Re-initialise the logic stack again.
	// The previous value is now gone.
	NETWORK 2
	
	// Re-initialise the logic stack again.
	// Push four values onto the stack. The top is true,
	// (top - 1) is false, (top - 2) and (top - 3) are true.
	NETWORK 3
	STR SC1
	STR SC1
	STRN SC1
	STR SC1
	
	// Re-initialise the logic stack again.
	// The previous values are now gone.
	NETWORK 4

Program Formatting Instructions:

Program formatting instructions do not directly affect the program flow or results, but they do act to help document and organise a program.

Instruction Description # Params Parameters
// Comment line Undefined Arbitrary text
NETWORK Network 1 Integer

Anything following a comment token ("//") is considered to be a comment and is ignored. The comment token must be separated from the following comment text by one or more spaces.

Example:


	// This is a comment.

A network is also often known as a "rung". The "NETWORK" token is an instruction that resets the logic stack and begins a new block of code. The NETWORK instruction takes an one integer as a parameter. Network (rung) numbers do not have to be unique or consecutive.

Network numbers are used to report syntax errors when the program is compiled, and are also used to report run time errors.

Example:


	NETWORK 1


Boolean Input Instructions:

Boolean input instructions read a Boolean value from the data table or the logic stack, and output the result to the logic stack.

Instruction Description # Params X Y C T CT SC
AND AND bit with top of logic stack 1 X X X X X X
ANDN AND NOT bit with top of logic stack 1 X X X X X X
ANDSTR AND top two values on logic stack 0
OR OR bit with top of logic stack 1 X X X X X X
ORN OR NOT bit with top of logic stack 1 X X X X X X
ORSTR OR top two values on logic stack 0
STR Store bit onto logic stack 1 X X X X X X
STRN Store NOT bit onto logic stack 1 X X X X X X

Boolean input instructions have two types of input parameters, explicit and implicit. A Boolean input instruction will accept no more than one explicit parameter, and zero, one, or two implicit parameters.

All Boolean input instructions output their result by modifying the logic stack.

Boolean input instructions fall into three categories: AND, OR, and STORE.

All Boolean input instructions have both normal and "NOT" versions.

Example:


	NETWORK 1
	STR X1
	AND X2
	OR X3
	OUT Y1
	
	NETWORK 2
	STRN C10
	ANDN C11
	ORN C12
	OUT C21
	
	NETWORK 3
	STR Y1
	STR Y2
	ANDSTR
	STR Y3
	ORSTR
	OUT Y10


Edge Contact Instructions:

Edge contact instructions are a type of Boolean input instruction, and operate in a manner similar to that of the normal Boolean input instructions. The difference is that they are one-shot instructions and output a true for one scan only.

Instruction Description # Params X Y C T CT SC
ANDND AND negative differential 1 X X X X X X
ANDPD AND positive differential 1 X X X X X X
ORND OR negative differential 1 X X X X X X
ORPD OR positive differential 1 X X X X X X
STRND STORE negative differential 1 X X X X X X
STRPD STORE positive differential 1 X X X X X X

Edge contact instructions can be categorised as "positive differentiate" and "negative differentiate" instructions. "Positive differentiate" instructions are true for one scan when the logic stack makes a false to true transition. "Negative differentiate" instructions are true for one scan when the logic stack makes a true to false transition.

The address used as a parameter is used for an AND, OR, or STORE the operation. It is not necessary to specify an address to store the one shot state, as the instruction stores this information automatically internally.

Example:


	NETWORK 1
	STR X1
	ANDPD C1
	OUT Y1
	
	NETWORK 2
	STR X2
	ANDND C2
	OUT Y2
	
	NETWORK 3
	STR X3
	ORPD C3
	OUT Y3
	
	NETWORK 4
	STR X4
	ORND C4
	OUT Y4
	
	NETWORK 5
	STRPD X5
	OUT Y5
	
	NETWORK 6
	STRND X6
	OUT Y6


Boolean Output Instructions:

Boolean output instructions output the value on the top of the logic stack to an address given as an explicit parameter. Output instructions do not modify the logic stack. This means that multiple output instructions can be used in series without the result of one instruction affecting the next.

Instruction Description # Params X Y C T CT SC Bit Range
OUT Output logic stack to bit 1 X X
OUT Output logic stack to multiple bits 2 X X X
PD Output logic stack one shot 1 X X
PD Output logic stack one shot to multiple bits 2 X X X
RST Reset bit if logic stack true 1 X X
RST Reset multiple bits if logic stack true 2 X X X
SET Set bit if logic stack true 1 X X
SET Set multiple bits if logic stack true 2 X X X

Boolean output instructions come in two forms: single output and bit range output. The system distinguishes between the two forms by the number of parameters given.

The PD instruction is a one-shot instruction. The output will be turned on for one scan if the logic stack input is true.

Example:


	// Output to a single bit.
	NETWORK 1
	STR X1
	OUT Y1
	RST Y2
	SET Y3
	
	// Output to a range of bits.
	NETWORK 2
	STR X2
	OUT C1 C9
	RST C10 C19
	SET C20 C21
	
	// Output using one-shot.
	NETWORK 3
	STR X3
	PD C30
	PD C40 C60


Comparison Instructions:

Comparison instructions compare two word parameters and output the Boolean result to the logic stack.

Instruction Description # Params
ANDE AND if parm1 equals param2 2
ANDGE AND if parm1 >= parm2 2
ANDGT AND if parm1 > parm2 2
ANDLE AND if parm1 <= parm2 2
ANDLT AND if parm1 < parm2 2
ANDNE AND if parm1 is not equal to parm2 2
ORE OR if parm1 equals parm2 2
ORGE OR if parm1 >= parm2 2
ORGT OR if parm1 > parm2 2
ORLE OR if parm1 <= parm2 2
ORLT OR if parm1 < parm2 2
ORNE OR if parm1 is not equal to parm2 2
STRE STR if parm1 equals parm2 2
STRGE STR if parm1 >= parm2 2
STRGT STR if parm1 > parm2 2
STRLE STR if parm1 <= parm2 2
STRLT STR if parm1 < parm2 2
STRNE STR if parm1 is not equal to parm2 2

All comparison instructions take two explicit parameters and output their result to the logic stack. Each parameter is a word register or constant.

There are three categories of comparison instruction: AND, OR, and STORE. Each of these operates in a manner similar to to the corresponding Boolean input instructions, with the exception that they apply the Boolean result of the comparison operation to to top of the logic stack.

For any comparison operation, both parameters must be compatible with each other. Parameters are compatible if they are both within the same compatibility group. The compatibility groups are defined as follows:

The type and address abbreviations are defined in the section on addresses and constants.

If one parameter is a string constant (e.g. "abc123"), and the other is a text register, the string constant will be compared to the series of text registers beginning at the register specified. The comparison will be on a character by character basis, such that the comparison will fail at the first character that does not meet the comparison criteria.

For example, if the string "hijklanop" is compared to to see if it is greater that a series of registers containing "abcdefghi", the comparison will fail on the sixth character because "a" is not greater than "f".

Example:


	// Compare signed numbers.
	NETWORK 1
	STRE DS1 DS2
	ANDGT 123 DD10
	ORLE DF20 123.876
	ORGE DF5 DS3
	ANDLT 93 CTD3
	OUT Y1
	
	// Compare unsigned (hex) numbers.
	NETWORK 2
	STNE 123fh DH52
	ANDGE YD4 DH2
	SET C20
	
	// Compare text.
	NETWORK 3
	STR X3
	ANDE TXT5 TXT10
	ORGE "A" TXT12
	ANDE "pass" TXT50 
	OUT C35


Program Control Instructions:

Program control instructions control the flow of the PLC program.

Instruction Description # Params Subr DS KInt One Shot
END Program end 0
ENDC Program end conditional 0
FOR For/next loop 1 or 2 X X X
NEXT Next in For/next loop 0
SBR Define a subroutine 1 X
CALL Call subroutine 1 X
RT Return from subroutine 0
RTC Return from subroutine conditional 0

Program END

END and ENDC will terminate a program scan. END terminates the scan unconditionally, while ENDC will terminate the scan if the logic stack is true.

Example:


	// END.
	NETWORK 100
	STR SC1
	AND X5
	ENDC
	STRN SC1
	END

Subroutines

SBR, CALL, RT, and RTC are subroutine instructions. SBR is used to define a subroutine. CALL will call a subroutine. SBR and CALL expect a valid subroutine name as a parameter. RT will return from a subroutine unconditionally. RTC will return from a subroutine if the logic stack is true.

Example:


	// Subroutines.
	NETWORK 1
	STR SC1
	OUT Y1
	CALL SubTest
	
	NETWORK 2
	STR C47
	OUT Y19
	
	END
	
	// Define subroutine.
	SBR SubTest
	NETWORK 1
	STR SC1
	RST Y10
	AND X21
	RTC
	SET C47
	RT

For/Next

FOR and NEXT are used to define a loop which executes a specified number of times. FOR begins the loop, and NEXT defines the end of the loop. FOR expects an integer parameter specifying the number of loops to perform. This may be a register or constant. FOR also accepts an optional parameter specifying whether the instruction should execute as a one-shot. FOR/NEXT loops may be nested.

Example:


	// FOR/NEXT with constant.
	NETWORK 1
	STR SC1
	FOR 10
	OUT Y1
	NEXT
	
	// FOR/NEXT with register.
	NETWORK 2
	STR SC1
	FOR DS1
	OUT Y2
	NEXT
	
	// FOR/NEXT with one-shot.
	NETWORK 2
	STR SC1
	AND C1
	FOR 100 1
	OUT Y2
	NEXT

Errors

Using RT or RTC in the main program will cause a run-time error. Using NEXT without FOR may cause a run-time error. Using FOR without NEXT may cause unpredictable run-time operation.

Subroutines may call other subroutines, and they may even call themselves (recursion is permitted). However, if if the nesting level reaches an excessive level, a run time error will occur and the program will exit. The maximum nesting level is undefined, but is typically about 1000.


Counter and Timer Instructions:

Instruction Description # Params T CT DS DD KInt KDInt Time Base
CNTU Count up 2 X X X X X
CNTD Count down 2 X X X X X
UDC Up/down counter 2 X X X X X
TMR On delay timer 3 X X X X
TMRA On delay accumulating timer 3 X X X X
TMROFF Off delay timer 3 X X X X

Counters

Counter instructions come in three types:

All counter instructions take two parameters. The first is a counter number, and the second is the counter preset. The counter preset may be either a constant or a register address. When the present value of the counter is equal to the preset, the counter status bit turns on. The address of the counter status bit is the same as the counter number. The present value of the counter is stored in the counter data register (CTD) of the same number as the counter number. E.g. for counter CT5, the counter status bit is also CT5, and the present count is in CTD5.

Example:


	// Up counter.
	NETWORK 1
	STR C1
	STR C2
	CNTU CT5 155793
	STR CT5
	OUT Y1
	
	// Down counter.
	NETWORK 2
	STR C11
	STR C12
	CNTD CT15 DD11
	STR CT15
	OUT Y11
	
	// Up/down counter.
	NETWORK 3
	STR C21
	STR C22
	STR C23
	UDC CT25 DS5
	STR CT25
	OUT Y21
	
	// Examine counter present values.
	NETWORK 7
	STRE CTD15 157
	OUT Y112

Timers

Timers come in three types:

All timer instructions take three parameters. The first is the timer address. The second is the timer preset. The timer preset may be either a constant or a register address. The third parameter is the time base. This may be either "ms" (milliseconds), "sec" (seconds), "min" (minutes), "hour" (hours), or "day" (days).

Example:


	// On delay timer.
	NETWORK 4
	STR X1
	TMR T5 25000 ms
	STR T5
	OUT Y51
	
	// Accumulating on delay timer.
	NETWORK 5
	STR X11
	STR X12
	TMRA T6 DS9 sec
	STR T6
	OUT Y52
	
	// Off delay timer.
	NETWORK 6
	STR X62
	TMROFF T192 DS412 min
	STR T192
	OUT Y57
	
	// Examine timer present values.
	NETWORK 7
	STRE TD6 97
	OUT Y120


Copy Instructions:

Copy instructions are used to copy data between registers, convert register data between types, and copy data between registers and Boolean (bit) addresses. Copy instructions are conditional output instructions which execute only if the logic stack is true. They also have an option to execute in a one-shot manner. Errors are indicated in the system control relays (SC43, SC44).

Instruction Description # Params One Shot Pointers SC43 SC44
COPY Copy a single value to a register 2 or 3 X X X X
CPYBLK Copy a block of data 3 or 4 X X
FILL Fill a block of data 3 or 4 X X
PACK Pack bits into a register 3 or 4 X
UNPACK Unpack bits from a register 3 or 4 X

Copy Single

The Copy Single (COPY) instruction is used to copy a single register, or constant (including a text string) to another register (or series of registers for text string constants). It is also the only instruction which will accept pointers as a source or destination.

The COPY instruction expects the following parameters:

COPY source destination (one-shot)

The COPY instruction will convert types as necessary when copying between otherwise incompatible registers. If a copy is made from a numeric register or constant to a text register, the digits will be converted to a text string, and one character being placed in each consecutive register. As many registers will be used as are necessary to hold the resulting string.

The error flags are set under the following conditions:

Example:


	// Copy a constant to a register.
	NETWORK 1
	STR SC1
	COPY 100 DS1
	
	// Copy a register to a register.
	NETWORK 2
	STR SC1
	COPY DS1 DD101
	
	// Copy a character to a text register.
	NETWORK 3
	STR SC1
	COPY "A" TXT100
	
	// Copy a character string to six consecutive text registers.
	NETWORK 4
	STR SC1
	COPY "abc123" TXT200
	
	// Copy a number to a text register, converting to text.
	NETWORK 5
	STR SC1
	COPY 123 TXT300
	
	// Copy with a one shot.
	NETWORK 6
	STR X1
	COPY 100 DS200 1

Copy Block

The Copy Block (CPYBLK) instruction is used to copy a block of consecutive registers to another block of consecutive registers.

The Copy Block instruction expects the following parameters:

CPYBLK sourcestart sourceend destinationstart (one-shot)

The error flags are set under the following conditions:

Example:


	// Copy a set of registers.
	NETWORK 1
	STR Y3
	CPYBLK DS1 DS10 DD1
	
	// Copy with one shot.
	NETWORK 2
	STR Y3
	CPYBLK DS1 DS10 DD1 1

Copy Fill

The Copy Fill (FILL) instruction is used to copy a single register or constant to a series of consecutive registers.

The Copy Fill instruction expects the following parameters:

FILL source destinationstart destinationend (one-shot)

The error flags are set under the following conditions:

Example:


	// Fill a set of registers with a numeric value.
	NETWORK 1
	STR X1
	FILL 1 DS1 DS100
	
	// Fill a set of registers with a value from another register.
	NETWORK 2
	STR X2
	FILL DS1 DD100 DD150
	
	// Fill using a one-shot.
	NETWORK 3
	STR X3
	FILL "A" TXT300 TXT 321 1

Copy Pack

The Copy Pack (PACK) instruction is used to pack a series of boolean (bit) values into a single register. Unused bits will be be set to zero.

The Copy Pack instruction expects the following parameters:

PACK sourcestart sourceend destination (one-shot)

The PACK instruction does not affect the error relays.

Example:


	// Pack some inputs into a register.
	NETWORK 1
	STR C2
	PACK X1 X16 DH1
	PACK X20 X25 DH2
	
	// Pack, with a one shot.
	NETWORK 2
	STR C5
	PACK X100 X116 DH10 1

Copy Unpack

The Copy Unpack (UNPACK) instruction is used to unpack a single register into a series of boolean (bit) addresses.

The Copy Unpack instruction expects the following parameters:

UNPACK source destinationstart destinationend (one-shot)

The UNPACK instruction does not affect the error relays.

Example:


	// Unpack a register.
	NETWORK 1
	STR C2
	UNPACK DH1 C1 C16
	UNPACK DH2 C25 C28
	
	// Unpack, with a one shot.
	NETWORK 2
	STR C5
	UNPACK DH5 Y1 Y16 1

Error Flags

When any copy instruction which uses either error flag is executed, both error flags (SC43, SC44) are reset. If an error is encountered, the appropriate error flag is set and the operation is aborted.


Search Instructions:

Search instructions are used to search a range of registers to find a value which matches the search criteria. The search value may be a register, constant, or string constant.

Instruction Description # Params One Shot
FINDEQ Search table for equal to 5 or 6 X
FINDGE Search table for >= 5 or 6 X
FINDGT Search table for > 5 or 6 X
FINDIEQ Incremental search table for equal to 5 or 6 X
FINDIGE Incremental search table for >= 5 or 6 X
FINDIGT Incremental search table for > 5 or 6 X
FINDILE Incremental search table for <= 5 or 6 X
FINDILT Incremental search table for < 5 or 6 X
FINDINE Incremental search table for not equal 5 or 6 X
FINDLE Search table for <= 5 or 6 X
FINDLT Search table for < 5 or 6 X
FINDNE Search table for not equal 5 or 6 X

Search instructions each come in two forms - continuous and incremental. A continuous search will begin at the start of the search register range each time it is invoked. An incremental search will begin at the next position after the last match. If the last incremental search did not result in a match before it reached the end of the range, the next search will start at the beginning again.

The search instructions expect the following parameters:

FINDEQ searchvalue start end result resultflag (one-shot)

The result of a search is placed in the specified register. If the search was successful (a match was found), the register is set to an integer value equal to the index (number of elements) from the beginning of the search range to where the match was found. If the search was not successful, the result register is set to -1.

When a string constant is used as the search value, the search is conducted by comparing the entire string to a number of registers equal to the length of the string. If a match is not found, the search position increments by one register positionand repeats until a match is found or the end of the search range is reached.

Example:


	// Search for constant.
	NETWORK 1
	STR X1
	FINDEQ 1 DS100 DS110 DS111 C111
	
	// Search for register.
	NETWORK 2
	STR X2
	FINDGT DD112 DD100 DD110 DS112 C112
	
	// Search for character.
	NETWORK 3
	STR X3
	FINDEQ "c" TXT100 TXT110 DS113 C113
	
	// Search for character string.
	NETWORK 3
	STR X3
	FINDEQ "cde" TXT100 TXT110 DS114 C114
	
	// Search with one shot.
	NETWORK 5
	STR X5
	FINDIEQ 15 DS100 DS110 DS115 C115 1


Shift Register Instructions:

The shift register instruction implements a shift register which may be of any arbitrary length. It takes two parameters which define the start and end of the shift register. Both must be "C" Boolean addresses. The first parameter may be lower or higher than the second parameter. The shift register will always shift from the lower address to the higher address.

Instruction Description # Params X Y C T CT SC One Shot
SHFRG Shift register 2 X

The value on the top of the logic stack is used as an input to the shift register the first location in the shift register will always be equal to the top of the logic stack. When the logic stack value (top - 1) transitions from off to on, the shift register will shift one position from the lower address to the higher address. When the logic stack value (top - 2) is on, the shift register is cleared and all bits are turned off.

The shift register instruction expects the following parameters.

SHFRG start end

Example:


	// Shift register.
	NETWORK 1
	STR X1
	STR X2
	STR X3
	SHFRG C10 C20


Math Instructions:

Math instructions perform mathematical operations on registers and constants, and output the results to registers.

Instruction Description # Params One Shot SC40 SC43 SC46
MATHDEC Decimal math 3 X X X X
MATHHEX Hexadecimal math 3 X X X X
SUM Sum a range of registers 3 or 4 X X

There are only three math instructions. However, they accept complete equations as input parameters allowing them to perform many different operations.

The decimal and hexadecimal math instructions are similar except for the functions and operators they offer, and for the type of registers they operate on. The general operation of both is desribed here with the details of the functions and operators listed separately below.

The MATHDEC and MATHHEX instruction expects the following parameters:

MATHDEC destination one-shot equationparameters

MATHHEX destination one-shot equationparameters

The error flags are set under the following conditions:

Example:


	// Decimal math. Will resolve to 10.77245
	NETWORK 1
	STR X1
	COPY 2 DS1
	MATHDEC DF1 0 (1 + DS1) ^ 2 + SQRT(PI)
	
	// Hexadecimal math. Will resolve to 22h (in hexadecimal)
	NETWORK 2
	STR X2
	COPY 4 DH2
	MATHHEX DH1 0 (LSH(DH2, 2h) + 1h) * 2h

Decimal Math Operations

Decimal math operations are conducted by functions and operators. The input values may be DS, DD, or DF registers, or decimal constants. The destination may be a DS, DD, or DF register.

Angles for transcendental functions (SIN, COS, etc.) are in radians.

Using a function that operates in floating point (transcendental, logmarithic, square root), or using any floating point number (including the PI constant) will cause the entire equation to be conducted in floating point. The result will then be converted to a type that is compatible with the destination register. If a floating point number is copied to an integer register, the decimal part of the result is truncated, not rounded.

Operation Description Example
SIN Sine SIN(DF1)
COS Cosine COS(DF2)
TAN Tangent TAN(DF3)
ASIN Arcsine ASIN(DF4)
ACOS Arccosine ACOS(DF5)
ATAN Arctagent ATAN(DF6)
LOG Log (base 10) LOG(DF7)
LN Natural log LN(DF8)
SQRT Square root SQRT(DD3)
RAD Convert degrees to radians RAD(DF21)
DEG Convert radians to degrees DEG(DF22)
+ Add DS1 + 7
- Subtract DS3 - DS4
* Multiply DS5 * 10
/ Divide DF7 / DS1
MOD Modulus (remainder) DS22 MOD DS21
^ Exponentiate DF52 ^ 3
PI The constant PI PI * DF102
() Parentheses (group operations) (DS2 + 71) * DF9

Hexadecimal Math Operations

Hexadecimal math operations are conducted by functions and operators. The input values may be DH registers, or hexadecimal constants. The destination must be a DH register.

Shifts and rotates take place within a 16 bit register size.

Operation Description Example
LSH Shift left a specified amount LSH(DH1, 2h)
RSH Shift right a specified amount RSH(DH2, 2h)
LRO Rotate left a specified amount LRO(DH3, 10h)
RRO Rotate right a specified amount RRO(DH4, 10h)
AND AND words DH5 AND Fh
OR OR words DH6 OR 7Fh
XOR XOR words DH7 XOR 1Ah
+ Add DH1 + 7h
- Subtract DH3 - DH4
* Multiply DH5 * 1fh
/ Divide DH7 / DH1
MOD Modulus (remainder) DH22 MOD DH21
() Parentheses (group operations) (DH2 + a1h) * DH9

Sum

The SUM function is provided as a separate instruction instead of being called from within the other math instructions.

The Sum instruction expects the following parameters:

SUM sourcestart sourceend destination (one-shot)

The error flags are set under the following conditions:

Example:


	// Sum a series of registers.
	NETWORK 1
	STR X1
	SUM DS10 DS19 DF20
	
	// Sum with a one shot.
	NETWORK 2
	STR X2
	SUM DH10 DH19 DH20 1