thebotslam

How do the cycle, ROM and RAM limits on my bot's code work?

Cycles: how much your bot can do each tick

Every instruction costs a fixed number of cycles, and the CPU gets a new allowance each tick. A program is a loop that does its work, then calls wait() (WAIT in BASIC). That sends your commands, gives up the rest of the tick, and brings in fresh readings. In C++ (loop()) and C# (Update()), each call is one tick, and in Blocks each pass of a loop is one tick, so the waiting is done for you.

If one pass of your loop takes more cycles than a tick has, nothing crashes. The tick simply ends in the middle of your code, which carries on in the next tick, with readings that are now a tick old. Your bot reacts more slowly.

To check, read the cycles left in this tick: cpu.cycles in Python, JavaScript and Lua, cpu.cycles() in C++, cpu.getCycles() in Java, Cpu.Cycles in C#, CYCLES in BASIC. Use it to skip optional work when time is short:

if cpu.cycles > 200:
    log("{} cycles to spare", cpu.cycles)

Some readings cost more than others. The Reference under the editor lists the costly ones, such as reading the damage localizer, and suggests reading them once a tick and keeping the result.

ROM: how big your program can be

Your compiled program must fit the CPU's ROM. The status line under the editor shows how much it uses, for example ✓ Compiled · 170 / 4096 ROM words · 18 / 1024 RAM words. Too big, and you get an error like program is 12053 words; this CPU's ROM holds 4096.

For more room, build your machine with a bigger ROM in the Factory: ROM-M doubles it and ROM-L makes it four times as big. That gives more program space only, not more cycles or RAM. Logs don't count against your ROM.

RAM: variables, lists and the stack

Variables, lists and arrays live in RAM, 1,024 words in the compiled leagues and 256 in assembly. Lists and arrays have a fixed size and never grow. Using an index outside a list faults the CPU: it reboots, stalls for 1 second and starts your program again from the top.

See all three

Open the machine's channels and performance panel under the editor. CPU shows your cycles per tick, I/O per loop what reading and commanding your parts costs each loop, and memory your ROM and RAM.