FANDOM


Programming

Setting up

2013-01-13 12.53.51

basic setup

To set up your basic computer you'll need:

You may also need :

All components must form a connected network. If you want them to be far away from each other, use Ribbon Cable. If you use the IO Expander, take care on the Ribbon Cable and Bundled Cable sides.

Starting up

To start up your computer, insert the FORTH Boot Disk in the Disk Drive (RedPower), then right click the CPU and click start. Now you may use the monitor.

Programming

Redpower computers use FORTH, a stack based programming language, meaning it primarily operates on the most recent values in memory, unlike modern languages like Java with a more organized structure. For example, typing

1 2 3 4 5 +

pushes 1-5 into memory. Typing '.S' will display

.S 1 2 3 9

because the last two values (4 and 5) are added into 9. Entering '+' further eventually results in a single value of 15.

This is a list of the commands available :

Environment commands
Command Effect Example Result Comment
WORDS List all knows functions (called words in FORTH) Your functions will also appear there
COLD Reboot computer
PAGE Clear current page
IOXADDR Default IO expander's variable See varaibles section for more details
EXIT Exit current function
FORGET <function> Remove <function> from known functions FORGET foo
SAVE" <name>" Save current environment as <name> in a floppy disk SAVE" MyOS"
DISKNAME" Rename your floppy disk DISKNAME" NewNamedOS"
DISKID Return uniqu id of the Floppy disk <hexadecimal value> Can be use to find the file in the redpower folder (in your world save)
Printing commands
Command Effect Example Result Comment
." <text> " Print <text> ." Hello" ." world" Helloworld Space character are mandatory after quotes
. Print last value and consume it 1 2 3 . 3
CR Do a carriage return ( = new line)
SPACE Insert a space ." Hello" SPACE ." world" Hello world
Stack commands
Command Effect Example Result Comment
.S Print all the stack of values 1 2 3 .S 1 2 3
DROP Remove last value 1 2 3 DROP .S 1 2
DUP Duplicate last value on the stack 1 2 3 DUP .S 1 2 3 3
+ - / * Add substract divide or multiply two last values 1 42 - 41
TICKS Wait for a defined number of ticks 5 TICKS 20 TICKS wait for 1 sec
Loop/condition commands
Command Effect Example Result Comment
<cond> IF <code> THEN Runs <code> only if <cond> is true 0 1 > IF ." smaller" THEN
<cond> IF <code1> ELSE <code2> THEN Runs <code1> if <cond> is true else runs <code2> 0 1 > IF ." smaller" ELSE ." bigger " THEN bigger
DO LOOP Repeat the same code n times 4 0 DO <code> LOOP Runs <code> four times Iterator value are stored in I and J
BEGIN AGAIN Infinite loop BEGIN <code> AGAIN Runs <code> infinitely EXIT must be used
IO commands
Command Effect Example Result Comment
IOX! Set default IO expander's value 3 IOX! Turns on only white and orange signals
IOX@ Read default IO expander's value IOX@ .S 3

(because we did 3 IOX!)

Result depends on the redstone signal in input
Memory commands
Command Effect Example Result Comment
VARIABLE Create a global varaible in words list VARIABLE bar
<var> @ Get the value of a variable bar @ 0
<value> <variable> ! Store <value> in <variable> 42 bar !
<n> ALLOT Allocate n Byte of memory and return the address 32 ALLOT
<addr> <size> ACCEPT Store <size> character of the next input at <addr> bar 32 ACCEPT 'enter' Hello world 'enter' Printing tokens will not work here
<addr> TYPE Print the content stored at <addr> bar TYPE Hello world Because of the previous ACCEPT

If you want to make a program you type ': *name of program*' and if you're done type ';'. So for example :

: hello ." Hello World" ;

Redstone outputs

if you want a redstone output use '*value* IOX!'

Insulated wire color Decimal value Binary value
White 1 0000 0000 0000 0001
Orange 2 0000 0000 0000 0010
Magenta 4 0000 0000 0000 0100
Light blue 8 0000 0000 0000 1000
Yellow 16 0000 0000 0001 0000
Lime 32 0000 0000 0010 0000
Pink 64 0000 0000 0100 0000
Grey 128 0000 0000 1000 0000
Light Grey 256 0000 0001 0000 0000
Cyan 512 0000 0010 0000 0000
Purple 1024 0000 0100 0000 0000
Blue 2048 0000 1000 0000 0000
Brown 4096 0001 0000 0000 0000
Green 8192 0010 0000 0000 0000
Red 16384 0100 0000 0000 0000
Black 32768 1000 0000 0000 0000

Multiple colors can be chosen by adding their values together. For example, to select white and orange, use 3, since 1 + 2 = 3.  To do yellow and lime you use 48, since 16 + 32 = 48.

You can also use binary conversion - search for the positions of 1's in the binary value. The position, starting from the right, corresponds to the color. For example, 48 is 110000 in binary. Because the 5th and 6th position corresponds to yellow and lime, 48 is yellow and lime.

Looping

When you're able to turn on lights or open and close doors using a redpower computer, you may want learn how to create loops.

If you wanted to make a light flash on and off repeatedly, you could do the following:

  1. Create a new program ': Flash' for example
  2. Type 'Number of times you want it to flash' '0' 'DO'
  3. Then type 'number of ticks you want to wait until it turns off' 'TICKS'
  4. 'value of your wire as seen above' 'IOX!'
  5. Then type 'number of ticks you want to wait until it turns on' 'TICKS'
  6. Then type '0 IOX!' so lights will turn off
  7. Finally, type 'LOOP ;' to end both your loop and function

For *1 it should look like this

: Flash
10 0 DO
10 TICKS
1 IOX!
10 TICKS
0 IOX!
LOOP ;

This program will flash 10 times for the output 1 (White cable). But because splitting line does not matter in FORTH this could also be written as follows :

: Flash 10 0 DO 10 TICKS 1 IOX! 10 TICKS 0 IOX! LOOP ;

Now if you do not know how many times you want the flash, try this :

: Flash 0 DO 10 TICKS 1 IOX! 10 TICKS 0 IOX! LOOP ;

This will use the last value on the stack as the missing parameter for DO and will flash the number of times you type before flash - '2 Flash' will flash 2 times.

More information

You may want to read more about FORTH :

http://technicpack.wikia.com/wiki/Basic_Forth_Programming

http://technicpack.wikia.com/wiki/Forth_language

Creating your own OS :

http://wishstudio.blogspot.fr/2012/10/building-red-power-operating-system.html

http://www.eloraam.com/nonwp/redcpu.php

An example of another OS :

https://github.com/vadixidav/minebos

Tutorials

RedPower Control Computer - Strings Input IO Variables & Stack Massive Tutorial01:38:38

RedPower Control Computer - Strings Input IO Variables & Stack Massive Tutorial

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.