The Ember CPU — Initial Design Part 2: Registers
Now that we have the basic design philosophy and some fundamental requirements for our new CPU, we should consider the types and number of registers we need. First, let’s look at how registers are used, their benefits, and some limitations due to hardware and instruction architecture.
One of the primary uses of internal registers is to hold values for operations that are being computed or actively worked on. Registers can represent CPU settings, flags used in logic operations, and loop counters so the program knows which item in a loop it is working on, and when to stop the current algorithm.
It is very common to have loops in code that “iterate over” or go to each entry or value in a list and perform some operation. Nearly as common is a 2-dimensional, or double loop, which iterates over two lists or dimensions at the same time. For example, when we want to do something to a 2-dimensional array like an image, we have a number of rows in the image, and then each row has a number of pixels. In this case, we need two “iterators” or index registers, one for X and one for the Y direction. Higher-dimensional arrays are increasingly less common, but still desirable, though one can often split those into smaller arrays if needed.