fCPU


Factorio Customizable Processing Unit. Allow to write any logic on low level machine code. The fCPU acts like a programmable microcontroller with a vector coprocessor that supports many useful instructions.

Content
18 days ago
0.18 - 2.0
10.2K
Logistic network Circuit network

g Example program - nuclear control

4 years ago

hey, I thought to add to the programs shown here, (and following Nassiel's example of showing of some code)

This program was written to control my nuclear outpost,
I'm playing with the apm nuclear mod, so I had multiple fuels to choose from. I choose to make a program that selects the highes amount type, and then check if steam is below a certain level (and going down)

setup:
green input = steam of boiler tanks & logistics network content
red input = constant combinator with fuels as content, this is the 'filter' to only check required items from the logistics network
output = a 1 pulse signals routed to filter inserters, they start the fuel cycle

! this program was way shorter (it fitted in <32 fCPU), but I added some comments to give context
I'm not sure if comment in the same line as the instruction works, so it might not be directly copy/past-able

clr ;cleanup for first run
:start
;cleanup counter, and max register
;r2=counter register, r3=max reg
clr r2 r3

:filterloop
inc r2
mov r1 red@2 ;get inputsignal from filter input @ counter(r2)
:inputloop
fig r1 r1 ;find this in green
tlt r3 r1 ;bigger than max reg? (first run of loop it always will)
swp r1 r3
tge r2 cnr ;counter > #signals in red(filter input)?
jmp :checkNext
jmp :filterloop

:checkNext
fig r1 [fluid=steam] ; get steam from input
tlt r1 r8 ; check steam with priv steam value (r8)
jmp :checkSteam
:endCheckSteam ;return label for steam check.
mov r8 r1 ;move current steam level to r8, and restart.
jmp :start

:checkSteam ; only active if steam is going down.
tlt r1 30000 ; my steam level setup is between 0-100k, so 30% was a good level
jmp :pulse
jmp :endCheckSteam

:pulse
ssv r3 1 ;set value of max register to 1 (external counting register counts how many time fcpu triggerd a fuel cycle)
mov out1 r3 ;'emmit fuel type'
clr out1 ;stop it directly, making 1 tick pulse
slp 3600 ;sleep some time to give the reactor time to heat back up and create steam.
jmp :endCheckSteam

Hope this helps picking some code out for your own projects. :)

4 years ago

Thank you for the example.
I created Discord channels for enthusiasts to discuss their programs and blueprints.
So there is a good place to share your designs and get advice.

4 years ago

I'm still don't fully understand how @ works.
Does it allow you to iterate over the different fuel types in the red wire?

4 years ago
(updated 4 years ago)

Let me describe on an example.

Having instruction mov r8 r@1, means following:
- r8 is the destination register
- r@1 is a source
- @1 tells the fCPU that it should take value from the r1 register and use it as index for source register r#.
- if a value in the r1 is 5, then the source will be r5)
- if a value in the r1 is 2, then the source will be r2)
- ...
- move value from source register r@r1 to destination register r8 (so the result operation depends on a value in r8 register)

You may use even red\green input wire: red@1, green@1:
mov r7 red@2 and fCPU will take r2 as index to picking signal from red inpuit wire (there may be multiple signals simultaneously) and then move it in r7.

4 years ago
(updated 4 years ago)

So you could iterate with @ syntax.

clr
:loop
inc r1
bgt r1 cnr :end
mov out1 red@1
jmp :loop
:end
4 years ago

So Inc r1 also changed the type?

4 years ago

No, inc r1 only increments it's value.

Another example:

mov r1 1
mov r8 red@1 ; same as mov r8 red1

mov r1 2
mov r8 red@1 ; same as mov r8 red2

mov r1 3
mov r8 red@1 ; same as mov r8 red3

mov r1 4
mov r8 red@1 ; same as mov r8 red4
4 years ago

ah, so I can do red1, red2 etc...
How does it select what each one is? is it the same order it appears on a power pole?

4 years ago

Power poles show signals in sorted order (from max to min). But actually signals appear in the order it was emmited on the wire (assume that it is a random order). For selecting signal with specific type you may use fir or fig mnemonics (Find In Red or Find In Green)

New response