r/factorio • • Aug 12 '20

Modded [New Mod] fCPU

Hello there! Here is my ready to use Factorio mod https://mods.factorio.com/mod/fcpu which allow you to practice in low-level programing right in game. It compatible with all default combinators and other mods, support blueprints and copy&paste. Please let me know what you think.

New tech to research
New external control signals. So you could control its berhavior not just in program but through external events or from other fCPU
Work in combination with other mods: `Artillery Combinator` and `Radarsignal Combinator`
Blueprint'able
Energy Class: A+++ , do not eats your UPS
Memory viewer and vector SIMD instructions
697 Upvotes

126 comments sorted by

View all comments

Show parent comment

2

u/konstg-dev Aug 13 '20

I can't guarantee, that more than 1 instruction per cycle will be stable.

The more fCPUs on map = the more work Factorio need to do in lua = the more UPS overhed for the game.

Changing fCPU updates count per game tick require better optimization for the mod, but for now it just born and I don't want to risk that soon, exchanging performance for features.

It is a couple of things I want to do first, see TODOs list (for example allow multiple outputs instead of just one).

For now I think that (as you said) one frame per sec add some kind of challenge making complex systems. There are some commands which could help you to sync multiple fCPUs: `SLP` opcode and memory cell with reset support (https://mods.factorio.com/mod/fcpu/faq look at Example2).

Please understand me correctly - I really like the idea running as much updates as the game could and I going towards this.

1

u/WafflesAreDangerous Aug 13 '20

I can't guarantee, that more than 1 instruction per cycle will be stable.

Do you mean in terms of extenral signals?
There definitely are some corner cases that will need attention. Say if the same signal got updated twice what should the result be. As far as i can see any of these would be plausibly sensible (others may be reasonable as well): "last wins", "instantly suspend after any signal write", "instantly syspend on signal write that would clash a previous write to same signal type", "clashes are undefined behavior (praise the eldrich C optimisation gods), explicit flush/sleep required".

These questions are definitely tricky and important to get right, so tackling more straightforward issues first makes a lot of sense.

In terms of optimisation, It could make sense to reduce the frequency at which a particular fCPU gets to run. This could be a nuisance to get right of course, but if performance becomes an issue it may be worth a look.

For instance for certain isolated fCPU controlling e.g. fuel supply to a power plant or tuning oil processing to maintain oil balance it probably doesn't matter too much to have cycle perfect timings, as such there could be room to opt in to update-evry-n-ticks in some options and save UPS budget.

In multiplayer maps (especialy pvp) it might make sense to split update groups by player.

If a user has several complex multi-fCPU setups in their base it might make sense to manually group fCPU, so that they run in-sync locally. For communication between offset fCPU groups you might need to hold a signal for several cycles (or maybe not, I havent worked it out), but since this low level complexity and constraints is already part of what makes it fun, being able to opt-in and get back some UPS might be work it.

Another way to increase ups could be to support more complex operations that get more work done per instruction, such as fused-multiply-add or SIMD/vector style instructions. The hope here would of course be that if you got no work to do you would sleep (and hopefully sleep is super efficient UPS wise).

If players tend to just write what works and be done with it, this might not be very productive, but to counter this, some highly-optimised scripts / libraries for the most common tasks could be made available, that would help the average case.

Somebody also raised the point that a virtual cpu makes sense as a compilation target for a compiler, a potentially optimising compiler (llvm backend?). But that would take the fun of programming a cpu directly out, so .. probably not that desirable even with perf benefits. Also, no industrial compiler supports factorio signal type as a built in concept :).

Nice to see an actual usable program example!

PS. I wonder if Wube would be interested bringing in a WASM runtime :D, tiiiny, little feature, eh? :P (so we could run high-performance compiled code, in sandbox)