SANDBOX — this is a test environment. No servers are actually deployed and no real payments are charged.
Back to NxLabs

NxLabs / Factorio

fCPU

EBy extermeon

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.

fCPU

Description

Specs

  • supports blueprints
  • supports copy & paste
  • supports multiplayer
  • supports quality
  • supports Informatron and Booktorio in-game wiki
  • in-game debugger with breakpoints
  • 256 instructions for whole program
  • 64 general purpose registers
  • 4096 LIFO stack for subprogram calls and local variables
  • 4 memory channels for vector processing
  • integrated access to the logistic network
  • 50+ opcodes
  • rich math instructions, trigonometry, rounding
  • SIMD instructions, min, max, filter, comparison, etc...
  • two input wires (Red, Green)
  • two output wires (Red, Green) have same output signals and values
  • parallel output, allows output multiple signals simultaneously
  • could be controlled through special input signals (interruptions)
  • one tick = one instruction (except for SIMD ones)
  • made for geeks

Description

fCPU is a combinator that includes:

  • program text
  • a set of registers (for storing signals or\and numbers)
  • couple of memory channels (for storing not zero signals and numbers)
  • processor (command processor and vector coprocessor)

Program

Programs for fCPU are entered in plain text in simplified assembly language (this guide is enough for a quick study) and consists of lines. Each line represents one instruction. An instruction consists of mnemonics and operands. For example: mov out1 123[item=copper-ore], here mov is a mnemonic, out1 is the first operand, 123[item=copper-ore] is the second operand. This instruction tells the processor to send signal [item=copper-ore] with number 123 on to wires connected to the output.

Mnemonics are abbreviated names of operations that the processor understands and knows how to execute. Operands are arguments to operations. They are used to indicate the values ​​on which an operation will be performed.

The following can be used as operands:

  • Signal: each signal consists of a type and a value (123[item=copper-ore])

123 - signal value represented by number [item=copper-ore] - type can be represented by pictogram or text

  • Register: this is a special cell that store the transmitted signal indefinitely (reg1, r2, ...)
  • Local: this is a special local cell that store the transmitted signal inside procedure call (var1, v2, ...)
  • Memory channel: one memory channel consists of multiple cells (array) that store the signal indefinitely (mem1, m2, ...)
  • LogNet channel: receives content of a logistic network this fCPU is placed in (lgn[1], lgn@2, logi[34], ...)
  • Input wire: you can receive signals on wires connected to a combinator's input (red, green, red1, green@3, ...)
  • Output wire: sets the values ​​at the output of a combinator (out1, out2, ..., out256)
  • Address: instruction address (line number 34)
  • Label in the code: written in text with a colon in front (:label, :anyname, ...)

The processor executes instructions from a written program in turn, line by line.

Registers

There are 8 generic purpose read/write registers, named reg1, ... reg8 or alias r1, ... r8. Each register store signal type and numeric value (floating point numbers are supported). For example mov reg2 10[item=iron-plate], this instruction assigns to reg2 value of 10 and type of \[item=iron-plate].

Besides general purpose registers there are some read only registers:

  • ipt: current instruction line numer
  • clk: clock, value increases every tick
  • cnr, cng: signals number on red cnr or green cng input wire
  • cnl: count of a various items in lognet, not a sum of its values
  • cnm1, ..., cnm4: signals number in memory
  • sp, bp: Stack Pointer, Base Pointer

Output registers (write only):

  • out1, ..., out256: output registers (only integer values with signal id, all untyped or zero-valued signals will be deleted)

Stack

The stack grows downwards in memory (from higher addresses to lower addresses). If you want to know how much space left, use sp register (mov r1 sp).

  • Type: LIFO (Last In, First Out)
  • Size: 4096

Use push and pop mnemonics to write and read from stack. push r1 r2 r3 shorthand for

push r1
push r2
push r3

Procedures

Procedure support through the call, ret, enter, and leave mnemonics, with semantics closely resembling x86. bp stores the address of the beginning of the current function's stack frame, enabling access to local variables.

call saves the return address by pushing it onto the stack, then transfers control to the target label or address. This allows nested and recursive procedure calls. Same as:

  push ipt
  jmp addr offset

ret completes a procedure by popping the return address from the stack and resuming execution at that location. Same as:

  pop <temp>
  jmp <temp>

enter initializes a new stack frame for a procedure. It pushes the current base pointer onto the stack and sets a new base pointer, optionally reserving space for local variables. Same as:

  push bp        ; save previous base pointer `bp`
  mov  bp, sp    ; set new stack frame base `sp`
  sub  sp, size  ; allocate space for local variables

leave reverses the effect of enter by restoring the previous base pointer and adjusting the stack pointer, effectively destroying the current stack frame before returning. Same as:

  mov  sp, bp    ; discard local variables
  pop  bp        ; restore previous base pointer `bp`

Stack diagram after some procedure calls.

4096
| ...         |
| older  ipt  | `call :fn1`
|             | `enter 3`
| old    bp   |
|        var1 | <-- old bp - 1
| ...         |
|        var3 | <-- old bp - 3
| ...         |
Read the full description on Factorio Mod Portal →

Ratings & reviews

No ratings yet

Sign in to leave a rating or comment.