Actions

Editing Advanced IC10 Programming

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 29: Line 29:
  
 
== Functions ==
 
== Functions ==
 
+
[[File:Function.png|thumb|left]]<br><br><br><br><br><br><br><br>
<div style="background: #000000;>
 
<p style="float: none; white-space: pre; line-height: 1; color: #FFFFFF; background: #000000; font-size: 110%;font-family: Consolas; margin-left: 10px;><span>main:</span><span>
 
</span><span style="color: #FF0000;">jal</span><span> </span><span>MyFunction</span><span>
 
</span><span style="color: #FF0000;">j</span><span> </span><span>main</span><span>
 
 
 
</span><span>MyFunction:</span><span>
 
</span><span>...</span><span>
 
</span><span style="color: #FF0000;">j</span><span> </span><span>ra</span><span>
 
 
 
</span>
 
</p>
 
</div>
 
 
A function is just a label we jump to, but using the -al version of branch instructions (jal, beqzal) as they store the return address into the special '''ra''' register.
 
A function is just a label we jump to, but using the -al version of branch instructions (jal, beqzal) as they store the return address into the special '''ra''' register.
 
When calling a function from another function (or from itself), ra will be overwritten so we have to store it in the stack and take it out when the function completes:
 
When calling a function from another function (or from itself), ra will be overwritten so we have to store it in the stack and take it out when the function completes:
 
+
[[File:EmbeddedFunction.png|thumb|left]]<br><br><br><br><br><br><br><br><br><br><br><br>
<div style="background: #000000; float: none">
+
If we hadn't used the stack, the jump at line 10 would take us to line 9 instead of 4.
<p style="float: none; white-space: pre; line-height: 1; color: #FFFFFF; background: #000000; font-size: 110%;font-family:Consolas; margin-left: 10px;><span>main:</span><span>
 
</span><span style="color: #FF0000;">jal</span><span> </span><span>_MyFunction</span><span>
 
</span><span style="color: #FF0000;">j</span><span> </span><span>main</span><span>
 
 
 
</span><span>_MyFunction:</span><span>
 
</span><span style="color: #6666FF;">push</span><span> </span><span>ra</span><span>
 
</span><span style="color: #FF0000;">jal</span><span> </span><span>_MySecondFunction</span><span>
 
</span><span style="color: #6666FF;">pop</span><span> </span><span>ra</span><span>
 
</span><span style="color: #FF0000;">j</span><span> </span><span>ra</span><span>
 
 
 
</span><span>_MySecondFunction:</span><span>
 
</span><span>...</span><span>
 
</span><span style="color: #FF0000;">j</span><span> </span><span>ra</span><span>
 
 
 
</span>
 
</p>
 
</div>
 
If we hadn't used the stack, the jump at the end of _MyFunction would take us to the call of _MySecondFunction instead of the main section.
 
  
 
== Loops ==
 
== Loops ==
  
<div style="background: #000000; float: none">
+
[[File:Loop.png|thumb|left]]<br><br><br><br><br><br><br><br><br><br>
<p style="float: none; white-space: pre; line-height: 1; color: #FFFFFF; background: #000000; font-size: 110%;font-family:Consolas; margin-left: 10px;><span style="color: #00FFFF;">alias</span><span> </span><span>rIterator</span><span> </span><span>r0</span><span>
 
</span><span>main:</span><span>
 
</span><span style="color: #6666FF;">move</span><span> </span><span>rIterator</span><span> </span><span>0</span><span>
 
</span><span>Loop:</span><span>
 
</span><span>...</span><span>
 
</span><span style="color: #6666FF;">add</span><span> </span><span>rIterator</span><span> </span><span>rIterator</span><span> </span><span>1</span><span>
 
</span><span style="color: #FF0000;">bgt</span><span> </span><span>rIterator</span><span> </span><span>3</span><span> </span><span>Loop_End</span><span>
 
</span><span style="color: #FF0000;">j</span><span> </span><span>Loop</span><span>
 
</span><span>Loop_End:</span><span>
 
 
 
</span><span style="color: #FF0000;">j</span><span> </span><span>main</span><span>
 
 
 
</span>
 
</p>
 
</div>
 
 
The challenge with loops is to keep them clean. Relative jumps are a great way to save lines but they can quickly turn code messy and adding lines might break them if you overlook them. If code length is not an issue, stick to labels where possible.
 
The challenge with loops is to keep them clean. Relative jumps are a great way to save lines but they can quickly turn code messy and adding lines might break them if you overlook them. If code length is not an issue, stick to labels where possible.
  
 
== Indexing With Relative Jump ==
 
== Indexing With Relative Jump ==
  
<div style="background: #000000; float: none">
+
[[File:Selector.png|thumb|left]]<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<p style="float: none; white-space: pre; line-height: 1; color: #FFFFFF; background: #000000; font-size: 110%;font-family:Consolas; margin-left: 10px;><span style="color: #00FFFF;">alias</span><span> </span><span>rIndex</span><span> </span><span>r0</span><span>
 
</span><span style="color: #00FFFF;">alias</span><span> </span><span>rTemp</span><span> </span><span>r1</span><span>
 
</span><span style="color: #00FFFF;">alias</span><span> </span><span>rValue</span><span> </span><span>r2</span><span>
 
 
 
</span><span>main:</span><span>
 
</span><span style="color: #6666FF;">move</span><span> </span><span>rIndex</span><span> </span><span>1</span><span>
 
</span><span style="color: #6666FF;">mul</span><span> </span><span>rTemp</span><span> </span><span>rIndex</span><span> </span><span>2</span><span>
 
</span><span style="color: #6666FF;">add</span><span> </span><span>rTemp</span><span> </span><span>rTemp</span><span> </span><span>1</span><span>
 
</span><span style="color: #FF0000;">jr</span><span> </span><span>rTemp</span><span>
 
</span><span style="color: #6666FF;">move</span><span> </span><span>rValue</span><span> </span><span>123</span><span> </span><span style="color: #FF8000;">#Index 0
 
</span><span style="color: #FF0000;">j</span><span> </span><span>Select_end</span><span>
 
</span><span style="color: #6666FF;">move</span><span> </span><span>rValue</span><span> </span><span>456</span><span> </span><span style="color: #FF8000;">#Index 1
 
</span><span style="color: #FF0000;">j</span><span> </span><span>Select_end</span><span>
 
</span><span style="color: #6666FF;">move</span><span> </span><span>rValue</span><span> </span><span>789</span><span> </span><span style="color: #FF8000;">#Index 2
 
</span><span style="color: #FF0000;">j</span><span> </span><span>Select_end</span><span>
 
</span><span>...</span><span>
 
</span><span>Select_end:</span><span>
 
</span><span style="color: #FF0000;">j</span><span> </span><span>main</span><span>
 
 
 
</span>
 
</p>
 
</div>
 
 
Use this to set one or more parameters or call functions using an index number you calculated (or read from a dial button). We multiply it by 2 because we need space for a jump after each move command so we don't run the subsequent ones and we add 1 because for a 0 index "jr 0" would lock up our IC.
 
Use this to set one or more parameters or call functions using an index number you calculated (or read from a dial button). We multiply it by 2 because we need space for a jump after each move command so we don't run the subsequent ones and we add 1 because for a 0 index "jr 0" would lock up our IC.
  
Line 116: Line 49:
 
Say you have a complex function that manipulates a device (reads/writes multiple parameters, controls timing etc) and you want to reuse it for multiple devices. You could copy paste the whole function, or you could redefine the alias you used and point it to another device, like this:
 
Say you have a complex function that manipulates a device (reads/writes multiple parameters, controls timing etc) and you want to reuse it for multiple devices. You could copy paste the whole function, or you could redefine the alias you used and point it to another device, like this:
  
<div style="background: #000000; float: none">
+
[[File:Aliases.png|thumb|left]]<br><br><br><br><br><br>
<p style="float: none; white-space: pre; line-height: 1; color: #FFFFFF; background: #000000; font-size: 110%;font-family:Consolas; margin-left: 10px;><span>main:</span><span>
 
</span><span style="color: #00FFFF;">alias</span><span> </span><span>dCurrent</span><span> </span><span>d0</span><span>
 
</span><span style="color: #FF0000;">jal</span><span> </span><span>_MyFunction</span><span>
 
</span><span style="color: #00FFFF;">alias</span><span> </span><span>dCurrent</span><span> </span><span>d1</span><span>
 
</span><span style="color: #FF0000;">jal</span><span> </span><span>_MyFunction</span><span>
 
</span><span style="color: #FF0000;">j</span><span> </span><span>main</span><span>
 
 
 
</span>
 
</p>
 
</div>
 
 
''NOTE: This wouldn't work in real languages since aliases and defines are just directives for the (pre)compiler, they're not evaluated during runtime by the CPU''
 
''NOTE: This wouldn't work in real languages since aliases and defines are just directives for the (pre)compiler, they're not evaluated during runtime by the CPU''
  
 
You can combine this with a loop and indexing to run your function on multiple devices automatically.
 
You can combine this with a loop and indexing to run your function on multiple devices automatically.
<div style="background: #000000; float: none">
+
[[File:LoopingAlias.png|thumb|left]]<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<p style="float: none; white-space: pre; line-height: 1; color: #FFFFFF; background: #000000; font-size: 110%;font-family:Consolas; margin-left: 10px;><span style="color: #00FFFF;">alias</span><span> </span><span>rIterator</span><span> </span><span>r0</span><span>
 
</span><span style="color: #00FFFF;">alias</span><span> </span><span>rTemp</span><span> </span><span>r1</span><span>
 
</span><span>main:</span><span>
 
</span><span style="color: #6666FF;">move</span><span> </span><span>rIterator</span><span> </span><span>0</span><span>
 
</span><span>Loop:</span><span>
 
</span><span style="color: #FF0000;">jal</span><span> </span><span>_UpdateReference</span><span>
 
</span><span style="color: #FF0000;">jal</span><span> </span><span>_FunctionForDevice</span><span>
 
</span><span style="color: #6666FF;">add</span><span> </span><span>rIterator</span><span> </span><span>rIterator</span><span> </span><span>1</span><span>
 
</span><span style="color: #FF0000;">bgt</span><span> </span><span>rIterator</span><span> </span><span>3</span><span> </span><span>Loop_End</span><span>
 
</span><span style="color: #FF0000;">j</span><span> </span><span>Loop</span><span>
 
</span><span>Loop_End:</span><span>
 
</span><span style="color: #FF0000;">j</span><span> </span><span>main</span><span>
 
 
 
</span><span>_UpdateReference:</span><span>
 
</span><span style="color: #6666FF;">mul</span><span> </span><span>rTemp</span><span> </span><span>rIterator</span><span> </span><span>2</span><span>
 
</span><span style="color: #6666FF;">add</span><span> </span><span>rTemp</span><span> </span><span>rTemp</span><span> </span><span>1</span><span>
 
</span><span style="color: #FF0000;">jr</span><span> </span><span>rTemp</span><span>
 
</span><span style="color: #00FFFF;">alias</span><span> </span><span>dCurrentDevice</span><span> </span><span>d0</span><span> </span><span style="color: #FF8000;">#Index 0
 
</span><span style="color: #FF0000;">j</span><span> </span><span>Select_end</span><span>
 
</span><span style="color: #00FFFF;">alias</span><span> </span><span>dCurrentDevice</span><span> </span><span>d1</span><span> </span><span style="color: #FF8000;">#Index 1
 
</span><span style="color: #FF0000;">j</span><span> </span><span>Select_end</span><span>
 
</span><span style="color: #00FFFF;">alias</span><span> </span><span>dCurrentDevice</span><span> </span><span>d2</span><span> </span><span style="color: #FF8000;">#Index 2
 
</span><span style="color: #FF0000;">j</span><span> </span><span>Select_end</span><span>
 
</span><span style="color: #00FFFF;">alias</span><span> </span><span>dCurrentDevice</span><span> </span><span>d3</span><span> </span><span style="color: #FF8000;">#Index 3
 
</span><span style="color: #FF0000;">j</span><span> </span><span>Select_end</span><span>
 
</span><span>Select_end:</span><span>
 
</span><span style="color: #FF0000;">j</span><span> </span><span>ra</span><span>
 
 
 
</span><span>_FunctionForDevice:</span><span>
 
</span><span style="color: #FF8000;">#Manipulate dCurrentDevice
 
</span><span style="color: #FF0000;">j</span><span> </span><span>ra</span><span>
 
 
 
</span>
 
</p>
 
</div>
 
  
 
= Device Manipulation =
 
= Device Manipulation =
Line 172: Line 60:
  
 
Some devices don't react well to having parameters changed while they're performing an action such as import or export. Notable mentions are the stacker, sorter and the vending machine. You can avoid them locking up (or getting jammed with items) either by adding delays (yield, sleep) or by actively waiting for their slots to become unoccupied, or their Activation to become 0.
 
Some devices don't react well to having parameters changed while they're performing an action such as import or export. Notable mentions are the stacker, sorter and the vending machine. You can avoid them locking up (or getting jammed with items) either by adding delays (yield, sleep) or by actively waiting for their slots to become unoccupied, or their Activation to become 0.
<div style="background: #000000; float: none">
+
[[File:Busy.png|thumb|left]]<br><br><br><br><br><br>
<p style="float: none; white-space: pre; line-height: 1; color: #FFFFFF; background: #000000; font-size: 110%;font-family:Consolas; margin-left: 10px;><span>WaitForDevices:</span><span>
 
</span><span>yield</span><span>
 
</span><span style="color: #00FFFF;">ls</span><span> </span><span>rBusy</span><span> </span><span>dSorter</span><span> </span><span>0</span><span> </span><span>Occupied</span><span>
 
</span><span style="color: #FF0000;">bgtz</span><span> </span><span>rBusy</span><span> </span><span>WaitForDevices</span><span>
 
</span><span style="color: #00FFFF;">ls</span><span> </span><span>rBusy</span><span> </span><span>dSorter</span><span> </span><span>1</span><span> </span><span>Occupied</span><span>
 
</span><span style="color: #FF0000;">bgtz</span><span> </span><span>rBusy</span><span> </span><span>WaitForDevices</span><span>
 
</span><span style="color: #00FFFF;">ls</span><span> </span><span>rBusy</span><span> </span><span>dSorter</span><span> </span><span>2</span><span> </span><span>Occupied</span><span>
 
</span><span style="color: #FF0000;">bgtz</span><span> </span><span>rBusy</span><span> </span><span>WaitForDevices</span><span>
 
</span><span style="color: #00FFFF;">ls</span><span> </span><span>rBusy</span><span> </span><span>dVender</span><span> </span><span>0</span><span> </span><span>Occupied</span><span>
 
</span><span style="color: #FF0000;">bgtz</span><span> </span><span>rBusy</span><span> </span><span>WaitForDevices</span><span>
 
</span><span style="color: #00FFFF;">ls</span><span> </span><span>rBusy</span><span> </span><span>dVender</span><span> </span><span>1</span><span> </span><span>Occupied</span><span>
 
</span><span style="color: #FF0000;">bgtz</span><span> </span><span>rBusy</span><span> </span><span>WaitForDevices</span><span>
 
</span><span style="color: #FF0000;">j</span><span> </span><span>ra</span><span>
 
</span>
 
</p>
 
</div>
 
 
 
 
'''NOTE:''' The slot reader labels slots from 1 but in the IC they start at 0. It is also a good idea to check any device involved in your solution with both a logical and slot reader as the current databases may not list all existing slots or parameters. You should always make use of all data available from devices as they may greatly simplify your project.
 
'''NOTE:''' The slot reader labels slots from 1 but in the IC they start at 0. It is also a good idea to check any device involved in your solution with both a logical and slot reader as the current databases may not list all existing slots or parameters. You should always make use of all data available from devices as they may greatly simplify your project.
  

Please note that all contributions to Unofficial Stationeers Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Unofficial Stationeers Wiki:Copyrights for details). Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel | Editing help (opens in new window)