¡Puedes leer este texto en español! MSP430: Ciclos & Instrucciones |
When we're working with microcontrollers or programmable systems, one aspect, we must have very present, is the time that our code, function or piece of code consumes.
Why is it important? For many reasons, for example: Synchronizing with any extern device, managing a communications bus (I2C, SPI, 1-Wire, etc) and finally, let's put a real situation: In any security monitoring systems, we must have quantified the amount of time since the event occurred until it's processed.
It's also a good distintive to know when we're in front of a good designer, for a same task, optimal program will be one that consumes less resources (and therefore, less clock cycles). At present, microcontrollers manufactures incorporate more memory to their devices and improve the structure of the CPU, and this makes that increasingly it's more easier to program in languages of medium-hight level like the C language.
We'll also use the C language, as this family of microcontroller is equiped for this role but, personally, I like to check the assembler code that compiler gives us. Let's move on!
Our family of microcontrollers is based on a Von Neumann architecture, this makes consumption measurements of instructions are a little bit special. One thing is clear: The number of cycles per instruction depends on the instruction format and the addressing mode.
This is very important as, number of cycles doesn't depend on the instruction itself. Below, it shows the number of cycles that are necessary depending on our intentions.
Number of Cycles on Interruptions and Reset.
Nº Cycles on Interruptions & Reset | ||
Operation | Nº Cycles | Length |
RETI | 5 | 1 |
Accept Interruption | 6 | --- |
WDT Reset | 4 | --- |
Reset (#RST/NMI) | 4 | --- |
· NOTE: The symbol: '# it's to indicate that the signal is negated.
Simples Operands. Format-II.
Nº Cycles for Format-II | |||||
Nº Cycles | |||||
Addressing Mode | RRA, RRC, SWPB, SXT | PUSH | CALL | Length | Operation |
Rn | 1 | 3 | 4 | 1 | SWPB R5 |
@Rn | 3 | 4 | 4 | 1 | RRC @R9 |
@Rn+ | 3 | 5 | 5 | 1 | SWPB @R10+ |
#N | Don't use. | 4 | 5 | 2 | CALL #0F000h |
x(Rn) | 4 | 5 | 5 | 2 | CALL 2(R7) |
EDE | 4 | 5 | 5 | 2 | PUSH EDE |
&EDE | 4 | 5 | 5 | 2 | SXT &EDE |
Jumps. Format-III.
All jump instructions, its length is 1-word and they always consume 2 cycles of clock.
Doubles Operands. Format-I.
Nº Cycles for Format-I | ||||
Addressing Mode | ||||
Source | Destination | Nº Cycles | Length | Operation |
Rn | Rm | 1 | 1 | MOV R5, R8 |
PC | 2 | 1 | BR R9 | |
x(Rm) | 4 | 2 | ADD R5, 4(R6) | |
EDE | 4 | 2 | XOR R8 ,EDE | |
&EDE | 4 | 2 | MOV R5, &EDE | |
@Rn | Rm | 2 | 1 | AND @R4, R5 |
PC | 2 | 1 | BR @R8 | |
x(Rm) | 5 | 2 | XOR @R5, 8(R6) | |
EDE | 5 | 2 | MOV @R5, EDE | |
&EDE | 5 | 2 | XOR @R5, &EDE | |
@Rn+ | Rm | 2 | 1 | ADD @R5+, R6 |
PC | 3 | 1 | BR @R9+ | |
x(Rm) | 5 | 2 | XOR @R5, 8(R6) | |
EDE | 5 | 2 | MOV @R9+, EDE | |
&EDE | 5 | 2 | MOV @R9+, &EDE | |
#N | Rm | 2 | 2 | MOV #20, R9 |
PC | 3 | 2 | BR #2AEh | |
x(Rm) | 5 | 3 | MOV #0300h, 0(SP) | |
EDE | 5 | 3 | ADD #33, EDE | |
&EDE | 5 | 3 | ADD #33, &EDE | |
x(Rn) | Rm | 3 | 2 | MOV 2(R5), R7 |
PC | 3 | 2 | BR 2(R6) | |
TONI | 6 | 3 | MOV 4(R7) ,TONI | |
x(Rm) | 6 | 3 | ADD 4(R4), 6(R9) | |
TONI | 6 | 3 | MOV 2(R4), &TONI | |
EDE | Rm | 3 | 2 | AND EDE, R6 |
PC | 3 | 2 | BR EDE | |
TONI | 6 | 3 | CMP EDE, TONI | |
x(Rm) | 6 | 3 | MOV EDE, 0(SP) | |
TONI | 6 | 3 | MOV EDE,&TONI | |
&EDE | Rm | 3 | 2 | MOV &EDE,R8 |
PC | 3 | 2 | BRA &EDE | |
TONI | 6 | 3 | MOV &EDE, TONI | |
x(Rm) | 6 | 3 | MOV &EDE, 0(SP) | |
&TONI | 6 | 3 | MOV &EDE, &TONI |
At this point, we can calculate the cycles of clock that each instruction will consume to make our program or piece of code. But unfortunately, we haven't finished yet, Texas Instruments company has provided to their microcontrollers what they call: Constant Generator Registers, its acronym: CGx (where x, it's the constant generator register appropiate, for our family there are two registers: CG1 and CG2).
CGx Values | |||
Register | ID | Constant | Comments |
R2 | 00 | --- | Register Mode |
R2 | 01 | (0) | Absolute Addressing Mode |
R2 | 10 | 00004h | +4, Bit processing |
R2 | 11 | 00008h | +8, Bit processing |
R3 | 00 | 00000h | +0, Bit processing |
R3 | 01 | 00001h | +1 |
R3 | 10 | 00002h | +2, Bit processing |
R3 | 11 | 0FFFFh | 1, Word processing |
Once submitted the CGx registers, we try to answer some questions about them because they're important at the time of calculating the cycles of clock of our code.
· Why are they important? Well, basically because they save us cycles of clock. They make it automatically, in other words, if we write in assembler language, compiler will look for constants and it will use the appropiate CGx register automatically.
· What?!!! I don't understand anything! Don't worry! Stay Calm! I'll try to explain it to you with an example. Imagine you have a LED conected to P1.1 and you want to change its state (I mean, turn it on and turn it off), you could write the following code:
bic.b.....#002h, &P1OUT
bis.b.....#002h, &P1OUT
bis.b.....#002h, &P1OUT
And now, if we want to calculate the number of cycles that each instruction consumes, we go to the previous tables, specifically the paragraph: Format-I Doubles Operands, and we must look at the immediately mode: #N. And right now,we're concerned the mode: &EDE.
Well, we can check the number of clock cycles required are 5. But, are 5 cycles really? If we simulate it or for example, we check it with an oscilloscope, we'll see that the real consumption are 4 cycles per each instruction.
What's happening? Is the datasheet wrong? No it's not, the reason is that our CGx friends have come into play. If we pay attention to the CGx table, we'll be able to see the constant: 0002h, this constant is contained in the CG2 register. And what does this mean? It's easy, our compiler interprets it and encodes it as follows:
mov.b.....R3, &P1OUT
Then, if we come back to the table of clock cycles consumption, this time, we must pay attention to Rn register mode and now, we pointing at the &EDE mode, voilá! they're our 4 clock cycles which the previous instruction consumes really.
Be careful with this! All constants which appear in the CGx table, they work similary to the previous example.
Have we finished? Not yet! (Don't panic! One lick less). We have 24 additional assembler instructions which their name are: Emulated Instructions.
For example, the instructions have only one operand:
clr.....dst
This instruction resets the destination register then, how many clock cycles does the instruction require? The value: 0000h, is managed by CGx and if we see in the CGx table, this value is contemplated by the R3 register, so our friends (CGx) have come back to appear, in this example concrete, the instruction will be encoded as:
mov.....R3, dst
And this instruction?:
inc.....dst
Same as above, the value of the constant is 00001h, so our friends (CGx) will come back to appear and the instruction will be encoded as:
add.....0(R3), dst
Finally, if we be careful with the CGx registers, we won't have any problem to say how many clock cycles our code will consume.
My advice is: Be careful to read the data sheet, it's very important you have some time to read the data sheet completely.
This save you some neurons.
0 comentarios:
Publicar un comentario