== Modifying the Target ==
=== Background on Setup (XMEGA) === While this tutorial can be performed on any supported target, results will vary between targets. Arm targets, for example, have pipelining, which complicates how long instructions take and when they happen. The sample concepts apply, but the specifics will be different.
The rest of this tutorial will focus on AtXMEGA128D4 (the CW303 XMEGA target), since correlating instructions to power consumption is typically simpler on it. We are comparing the power consumption of two different instructions, the <code>MUL</code> (multiply) instruction and the <code>NOP</code> (no operation) instruction. Some information on these two instructions:
Note that the capture clock is running at 4x the device clock. Thus a single <code>mul</code> instruction should span 8 samples on our output graph, since it takes 4 samples to cover a complete clock cycle.
==== Initial Code ====
The initial code has a power signature something like this (yours will vary based on various physical considerations, and depending if you are using an XMEGA or AVR device):
Note that the 10 <code>mul</code> instructions would be expected to take 80 samples to complete, and the 10 <code>nop</code> instructions should take 40 samples to complete. By modifying the code we can determine exactly which portion of the trace is corresponding to which operations.
==== Increase number of NOPs ====
We will then modify the code to have twenty NOP operations in a row instead of ten. The modified code looks like this:
Pay particular attention to the section between sample number 0 & sample number 180. It is in this section we can compare the two power graphs to see the modified code. We can actually 'see' the change in operation of the device! It would appear the <code>nop</code> is occuring from approximately 10-90, and the <code>mul</code> occuring from 90-170.
==== Add NOP loop after MUL ====
Finally, we will add 10 more NOPs after the 10 MULs. The code should look something like this:
</blockquote>
==== Comparison of All Three ====
The following graph lines the three options up. One can see where adding loops of different operations shows up in the power consumption.
<blockquote>[[File:nop_mul_comparison.png|image]]
</blockquote>
=== Background on Setup (Arm) ===
While this tutorial can be performed on any supported target, results will vary between targets. Arm targets, for example, have pipelining and much smaller differences in power consumption between instructions compared to the XMEGA target. Instead of trying to spot the differences ourselves, we'll be using the persistence feature of ChipWhisperer Capture to compare two traces. The instructions we'll be focusing on here are <code>nop</code> and <code>mul</code>.
First, clear the display of the current trace (if you have one):
[[File:Clear Display.png|frameless|336x336px]]
Next, enable persistence:
[[File:Persistence.png|frameless|336x336px]]
Now hit the ''Run 1'' [[File:Capture One Button.PNG|image]] button and capture a single trace. It's recommended that you only capture a single trace. You may also want to change the colour of the next trace to make differentiating them easier:
[[File:Set Colour.png|frameless|336x336px]]
You should now have something that looks like this:
[[File:B2 cap1.PNG|frameless|1139x1139px]]
==== Swapping NOP and MUL ====
Next, we're going to swap when the nop and mul instructions happen to see if we can identify which is which.:<syntaxhighlight lang="c">
asm volatile(
"mul r0,r1" "\n\t"
"mul r0,r1" "\n\t"
"mul r0,r1" "\n\t"
"mul r0,r1" "\n\t"
"mul r0,r1" "\n\t"
"mul r0,r1" "\n\t"
"mul r0,r1" "\n\t"
"mul r0,r1" "\n\t"
"mul r0,r1" "\n\t"
"mul r0,r1" "\n\t"
::
);
asm volatile(
"nop" "\n\t"
"nop" "\n\t"
"nop" "\n\t"
"nop" "\n\t"
"nop" "\n\t"
"nop" "\n\t"
"nop" "\n\t"
"nop" "\n\t"
"nop" "\n\t"
"nop" "\n\t"
::
);
</syntaxhighlight>Build and program the new firmware onto the device. Then capture another trace. You should have something that looks like:
[[File:B2 cap2.PNG|frameless|1149x1149px]]
We can spot some areas with differences, but it's tough to see where the different instructions are happening. Let's zoom in (click and drag over the area you want to zoom into, right click > View All to go back to the original view):
[[File:B2 cap3.PNG|frameless|1126x1126px]]
As you can see, there's still very little difference between the two, but the differences are there!
== Clock Phase Adjustment ==