Time and Addressing Modes
We need to understand how long our program, or a piece of our program,
takes to execute. This is important for such things as, say, turning the
robot for 1/2 of a circle. We would take the speed of the motor and figure
out how long to keep the motor on, then write a program to handle that.
In a digital synchronous system, which a CPU is, time is strictly
controlled by a clock signal. During each clock cycle, something
happens. Clock cycles is what the "t" counter in the simulator is
counting.
Most instructions take more than one clock cycle! Why? Because at the
very least two things must happen:
-
The instruction must be fetched from memory (at least one cycle)
-
The instruction must be executed (at least one cycle)
So even the most basic instructions take two cycles. We can confirm this
by looking at the page for the ABA instruction (p. A-4 in the Reference
Manual). The chart at the bottom shows two cycles of operation. The pocket
programming reference also shows us the number of cycles an instruction
takes. The table beginning on page 19 lists all the instructions, and in
the "cycles" column, tells us the number of cycles. The first instruction
is ABA, which it shows as taking 2 cycles.
If we are loading a value from memory, not only does the instruction
need fetched, but so does the address of the data, and the data itself.
To make things even more complex, it depends on the addressing mode of
the instruction. On page A-63 of the Reference Manual, we can see the timing
charts for the LDAA and LDAB instructions.
-
Immediate addressing, where the data immediately follows the instruction,
only takes 2 cycles, because no other address needs to be fetched.
-
Direct addressing, where only half of an address (one byte) needs to be
fetched (the upper byte is assumed to be $00), takes 3 cycles: One to fetch
the instruction, one to fetch the address byte, and one to fetch the data
byte.
-
Extended addressing, which uses a full 2-byte address, takes 4 cycles,
and some others take even more.
-
Inherent addressing uses in most cases only 1 byte of memory for the
instruction opcode, takes 2 cycles in most cases; however, can take more.
-
Relative addressing, where one byte is used to indicate the change in the PC,
takes 3 cycles (like direct addressing): One to fetch
the instruction, one to fetch the relative offset, and one to change the PC.
-
To really be astounded, look at the IDIV instruction -- it takes 41 cycles!