# This is some of my notes from playing with FORTH and Redpower's sortron, trying to create an

# automatic charging system. There's a bit of information on the commands and memory offsets at

# http://integratedredstone.wikispaces.com/Sortron, though I couldn't find any information on the

# words that come on the extended boot disk beyond Direwolf's overview

# Get the item damage in slot number on the top of the stack

# (slot -- damage)

: SLOTDMG@ SORTADDR @ RBP! RP0 2 + ! 2 SORTCMD RP0 8 + @ ;

# Explaination:

# SORTADDR @ RBP! loads the sortron at bus id SORTADDR @ (Default is 4) to the redbus memory area.

# This only lasts to the end of the command since it seems the monitor uses the same method to

# display the screen.

#

# RP0 is the memory location where the redbus memory is stored. To communicate with peripherals you

# write and read to this area. To query the sortron we need to set the slot to query at memory offset

# +0x02. The slot is taken from the top of the stack, we add 2 to RP0 for the memory location then

# write to that location with !.

#

# SORTCMD is the system call to communicate with the sortron. It takes a number from the top of the

# stack which says what command to do, 0x2 queries the item in a slot.

#

# Once the command has run the damage value will be stored in memory offset +0x08, which is queried

# using the @ command. The damage value ends up on top of the stack.

# Pull damaged items from the inventory. If the destination is full the program will hold until it

# can fufil the request. It will go over each inventory slot once, put it in an infinite loop if you

# want to pull continuously

: PULLDAMAGED

SORTADDR @ RBP! 1 SORTCMD RP0 2 + @

0 BEGIN 2DUP > WHILE

DUP SLOTDMG@ 1 > IF 1 RP0 1 + C! 3 SORTCMD THEN

1 +

REPEAT

2DROP

;

# Explaination:

# SORTADDR @ RBP! loads the sortron into redbus memory as before

#

# 1 SORTCMD We will loop over every slot, this command puts the number of slots in the inventory

# into redbus offset 2

#

# RP0 2 + @ Load the stack count onto the stack

#

# 0 Push 0 onto the stack, this will be the current inventory slot.

#

# BEGIN DUP2 > WHILE Start a loop over inventory slots. We want the loop to continue until we reach

# the final slot, so the condition makes sure that we continue the loop while the total number of

# slots is greater than our counter. We want these variables to stay on the stack though so that we

# can continue to use them, so they are copied with the DUP2 command. We have to duplicate the counter

# each time that we access it

#

# DUP SLOTDMG@ Duplicate the counter, then check to see if the item in that slot is damaged

#

# 1 > IF Fully charged electric items appear to have a damage value of 1

#

# 1 RP0 1 + C! Store 1 at redbus offset 0x01. This variable needs to be a single byte (normal FORTH

# variables are 2 bytes) so we use C! to store the value. This will be the maximum stack size to pull,

# we want only one item to charge (though damaged items shouldn't stack)

#

# 3 SORTCMD Pull from the slot designated by offset 2 a number of items designated by offset 1. This

# moves our damaged item into the charger

#

# THEN End the if statement

#

# 1 + Increment the counter

#

# REPEAT End the loop

#