Got a question that the wiki doesn't answer? Ask on the forum (preferred), or join us on IRC.
CraftBookPerlstone32
Contents
Description
Perlstone32, (a 32-bit descendant of Perlstone) is a scripting language that can be used in CraftBook to program programmable logic chips.
The language was written by olegbl, based on Lymia's Perlstone. It has been integrated into the main release of CraftBook since version 3.
Features
- Integer Stack
- 32-cell Persistent Variable Table of Integers
- Indexed with 0-9 and a-v.
- Stored via software.
- May be Public, Private or None,
- If line 4 of IC sign is blank, persistent variable table will not be available (to save memory).
- If line 4 of IC sign is "private", a private persistent variable table will be created.
- Private tables are not accessible to any other ICs.
- If line 4 of IC sign is any other name, a public persistent variable table identified by that name will be accessed.
- This table may be shared between different Perlstone32-based ICs.
- It may also be accessed from other Perlstone32-based ICs via the L and S opcodes.
- 32-cell Temporary Variable Table of Integers
- Indexed with 0-9 and a-v.
- Used across all functions for the duration of a state change.
- 32-cell Local Variable Table of Integers
- Indexed with 0-9 and a-v.
- Local to a single function.
- Three Input Registers
- Boolean
- Variable Operand Source
- Operands may be explicitely defined.
- Otherwise, Perlstone32 will attempt to pop the required operands off the stack.
- Mathematical Operations
- Compile-time Syntax Checking
- When you place the IC sign, it will check the code for you.
- If your code is wrong, it will tell you exactly where the error is.
Usage
Perlstone32 uses the MC5032 IC. It is used in the same way as MC5000.
However, the fourth line of the MC5032 sign must contain a name of permanent storage to be used.
Two MC5032 ICs with the same 4th line will share their permanent storage.
This can also be used as wireless transfer of large amounts of data.
General structure of Perlstone32 is very similar to that of Perlstone.
A script is grouped into up to one hundred functions. Functions are strings containing an arbitrary number of opcodes, and are separated by the character ':'. Functions are also indexed with numbers 0 though 99. Functions may take arguments (which are pushed onto the function's stack when it is called) and return a single integer value. When a function is called, a new function local variable table is created, pre-filled with all 'zero' values and used for the duration of the function.
Functions 0 through 2 are run on state update. Functions 3 through 99 must be called manually.
Opcodes
Each opcode is formatted as follows: [opcode][operands].
All opcodes must be separated by either a space or a semicolon (user's preferance).
Opcode | Operands | Pops | Pushes | Version Required |
Description |
---|---|---|---|---|---|
Stack Functions | |||||
<int> | int | 1.0 | Pushes integer <int> onto the stack | ||
d | [n=pop] | n to n+1 | int, ... | 1.1.2 | Duplicates the top value of the stack n number of times. If n is omitted, pops the top value off the stack, and uses it as n, |
p | [n=pop] | n to n+1 | 1.1.2 | Pops the top value off the stack and discards it n number of times. If n is omitted, pops the top value off the stack, and uses it as n, | |
v | [n=pop] | 0 to 1 | int | 1.1.2 | Copies the value n values down (0 = top, 1 = pretop, ...) in the stack onto the top. If n is omitted, pops the top value off the stack, and uses it as n, |
Variable Functions | |||||
A | bool | 1.0 | Pushes contents the A input register onto the stack | ||
B | bool | 1.0 | Pushes contents the B input register onto the stack | ||
C | bool | 1.0 | Pushes contents the C input register onto the stack | ||
At | bool | 1.0 | Pushes true unto the stack if the state of the A input was toggled, false otherwise | ||
Bt | bool | 1.0 | Pushes true unto the stack if the state of the B input was toggled, false otherwise | ||
Ct | bool | 1.0 | Pushes true unto the stack if the state of the C input was toggled, false otherwise | ||
S | [r=pop][e][n=pop] | 1 to 3 | 1.1 | Pops the top value off the stack and stores it to slot n in the table r.
Operand r can be p for persistent, t for temp, l for local or e for external persistent. | |
L | [r=pop][e][n=pop] | 0 to 2 | int | 1.1 | Pushes the value n in the table r onto the stack (see above) |
Mutator Functions | |||||
+ | 2 | int | 1.0 | Pops the two top values off the stack, adds them together and pushes the result on the stack. | |
- | 2 | int | 1.0 | Pops the two top values off the stack, subtracts the top value from the pre-top value and pushes the result on the stack. | |
* | 2 | int | 1.0 | Pops the two top values off the stack, multiplies them together and pushes the result on the stack. | |
/ | 2 | int | 1.0 | Pops the two top values off the stack, divides the pre-top value by the top value and pushes the result on the stack. | |
% | 2 | int | 1.0 | Pops the two top values off the stack, divides the pre-top value by the top value and pushes the remainder on the stack. | |
^ | 2 | int | 1.0 | Pops the two top values off the stack, raises the pre-top value to the top value power and pushes the result on the stack. | |
>> | 2 | int | 1.0 | Pops the two top values off the stack, logically shifts the pre-top value to the right by the top value and pushes the result on the stack. | |
<< | 2 | int | 1.0 | Pops the two top values off the stack, logically shifts the pre-top value to the left by the top value and pushes the result on the stack. | |
++ | 1 | int | 1.0 | Pops the top value off the stack, increments it by 1 and pushes the result on the stack. | |
-- | 1 | int | 1.0 | Pops the top value off the stack, decrements it by 1 and pushes the result on the stack. | |
Comparator Functions | |||||
== | 2 | bool | 1.0 | Pops the top two values off the stack. If they are equal, pushes 1 on the stack. Otherwise, pushes 0 on the stack. | |
!= | 2 | bool | 1.0 | Pops the top two values off the stack. If they are not equal, pushes 1 on the stack. Otherwise, pushes 0 on the stack. | |
> | 2 | bool | 1.0 | Pops the top two values off the stack. If the pre-top value is greater than the top value, pushes 1 on the stack. Otherwise, pushes 0 on the stack. | |
< | 2 | bool | 1.0 | Pops the top two values off the stack. If the pre-top value is less than the top value, pushes 1 on the stack. Otherwise, pushes 0 on the stack. | |
>= | 2 | bool | 1.0 | Pops the top two values off the stack. If the pre-top value is greater than or equal to the top value, pushes 1 on the stack. Otherwise, pushes 0 on the stack. | |
<= | 2 | bool | 1.0 | Pops the top two values off the stack. If the pre-top value is less than or equal to the top value, pushes 1 on the stack. Otherwise, pushes 0 on the stack. | |
Logical Functions | |||||
! | 1 | bool | 1.1.1 | Pops the top value off the stack. If it is false (equal to 0), pushes 1 on the stack. If it is true (not equal to 0), pushes 0 on the stack. (NOT) | |
& | 2 | bool | 1.1.1 | Pops the top two values off the stack. If they are both true (not equal to 0), pushes 1 on the stack. Otherwise, pushes 0 on the stack. (AND) | |
| | 2 | bool | 1.1.1 | Pops the top two values off the stack. If at least one of them is true (not equal to 0), pushes 1 on the stack. Otherwise, pushes 0 on the stack. (OR) | |
x | 2 | bool | 1.1.1 | Pops the top two values off the stack. If exactly one of them is true (not equal to 0), pushes 1 on the stack. Otherwise, pushes 0 on the stack. (XOR) | |
Flow Control Functions | |||||
R | 1.0 | Returns 0. | |||
r | 1 | 1.0 | Pops the top value off the stack and returns it. | ||
f | [i][n] | n to n+2 | int | 1.0 | Calls function i (referenced by a 2-digit number) with n arguments from the stack.
Both i and n may come from the stack. Operand i would be popped first, n second, followed by any arguments. |
[ | 1 | 1.0 | Pops the top value off the stack; if it is false (equal to 0), jump to the opcode after the corresponding ] opcode (as defined by a system of nested '[' and ']' instructions) | ||
] | 1 | 1.0 | Pops the top value off the stack; if it is true (not equal to 0), jump to the opcode after the corresponding [ opcode (as defined in the [ opcode) |
Examples
10-bit combination lock
Description
- Input A: Reset
- Input B: Input Button B
- Input C: Input Button C
- The combination is BCBBCCBCBC (meaning inputs B and C have to toggle high in that order).
Human Readable Code
A [ 0 Sp0 0 Sp1 R ] # If A, reset p0 (unlocked) and p1 (button sequence) and return false. Lp0 [ 1 r ] # If p0 (unlocked) is set, return true. B [ Lp1 10 * 1 + Sp1 0 ] # If B, multiply p1 by 10 and add 1 C [ Lp1 10 * 2 + Sp1 0 ] # If C, multiply p1 by 10 and add 2 Lp1 1211221212 == [ 1 Sp0 1 r ] R # If p1 equals the set code, set p0 to 1 and return true.
Sign Formatted Code
[Code Block]
A [ 0 Sp0 0 Sp1 R ] Lp0 [ 1 r ] B [ Lp1 10 * 1 + Sp1 0 ] C [
Lp1 10 * 2 + S p1 0 ] Lp1 1211 221212 == [ 1 S p0 1 r ] R
[MC5032] private