Substantial savings
The smallest context will require 17 cycles - 10 to save the context
and seven to restore it. The worst case for the OCG compiler is only 25
cycles. Compared to a conventional compiler, an OCG compiler can reduce
the number of interrupt-related instruction cycles by 40 percent to 60
percent.
Depending on the application, the cycle savings can be substantial.
An interrupt driven serial communication port with a baud rate of
480,600bit/s generates 24,000 interrupts per second. Using a
conventional compiler with 42 instruction cycles per interrupt (168
clock cycles per interrupt) saving and restoring the context will use
up over 4,032,000 CPU cycles per second or 20 percent of the available
cycles on a 20MHz PIC16.
An OCG compiler, averaging 21 instructions cycles per interrupt (84
clock cycles per interrupt), can reduce that number to only 2,016,000
cycles - saving half of the clock cycles otherwise spent on saving and
restoring contexts, and allowing the CPU to be put into sleep mode for
10 percent of its cycles.
Assuming 10mA active and about 1µA sleep mode power
consumption, an OCG compiler could reduce total MCU power consumption
by nearly 1mA—about 10 percent. In an application with an 8mA power
budget, that extra milliamp could be a life saver (Figure 2 below).
 |
|
Figure 2: An OCG compiler can reduce total MCU power consumption by
about 10 percent. |
Bye-bye banked memory
Many 8-bit and 16-bit MCUs have banked memories
that cannot be addressed simultaneously. Switching between the
memory banks requires at least two bank selection instructions.
Thus, if data in one bank must be written to another bank, bank
selection instructions are always necessary. Placing all the variables
accessed by a function in the same memory bank will reduce the number
of bank selection instructions and the total required cycles for the
application.
However, conventional compilers have no way of knowing which
functions call which variables and are unable to optimize their memory
assignment. Nor do these compilers have any way of knowing whether a
particular memory bank will be selected in the code. As a result, these
compilers automatically generate bank selection instructions for every
memory access, whether or not that bank is already selected.
Some compilers have extensions to the C-code that
identify the address of the variable. Programmers may manually assign
variables to memory banks using this non-standard, non-portable code.
The bank qualifiers allow the compiler to see the exact bank an object
resides in and reduces the number of bank selection instructions.
However, this approach does not guarantee that dependent variables
will be placed in the same bank. Every time a variable in one memory
bank needs to be written to another memory bank, bank selection
instructions will still be required.
In addition, trying to track all the memory addresses across
multiple code modules and ensuring that all pointers have the correct
addresses is a time-consuming, tedious process that can introduce
programming errors.
In contrast, an OCG compiler knows every register, stack, pointer,
object and variable declaration from all program modules. It can
optimize every variable, register allocation and the size and scope of
every pointer and every object stored on the compiled stack. It
optimizes memory allocation to minimize or eliminate bank selection
instructions, without any intervention from the programmer.
By placing frequently accessed variables in unbanked memory and by
placing any dependent variables in the same memory bank, an OCG
compiler can radically reduce the number of cycles and power wasted on
bank selection instructions in these MCU architectures.
Since the OCG compiler knows which bank is selected at any point in
the code, it can also eliminate any unnecessary bank selections
instructions when the bank is already selected.
Reducing the number of instructions reduces the number of CPU
cycles by as much as 30 percent to 50 percent. Choosing a low-power
device and exploiting the sleep mode capabilities of the MCU are
important means of minimizing power consumption.
However, the way in which the compiler manages interrupts and
memory usage can also have a significant impact on power consumption.
Newer compilers with OCG technology can make a substantial contribution
to saving cycles and power.
Clyde Stubbs is Founder and CEO of Hi-Tech
Software. He can be contacted at clyde@htsoft.com