This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Constant Generators

Expert 1175 points

Can anybody tell me what is the use of constant generators in msp430.using R2 and R3 registers and what are the situations in which we can use it?

  • A Double-Operand Instruction using Immediate Mode as the Source Operand usually requires an extra word to specify the Source Operand itself (a constant).

    However, for the most commonly used constants, 0, 1, 2, 4, 8, and -1 (0xFFFF), using the "constant generator" can eliminate that extra word in the instruction. This makes the code size smaller and execution faster.

    In most MSP430 tool chains, this is done automatically. That is, when the source code uses one of those commonly used constants, the assembler/compiler generates an object code using the constant generator.

    There are two unfortunate exceptions, they occasionally still generate instructions with extra constant word when they could have used the constant generator. They did that as a work-around for a bug in some of the earlier MSP430. For MSP430 without that bug, these tool chains still work around an imaginary bug.

  • I addition to the above explanations I might add some more in-depth details:

    If an instruction only requires input and output from a register, it can be executed in one MCU cycle. Actually, two cycles are required, one for fetching the instruction from flash, one for executing it, but since the MSP fetches the next isntruction while executing the last, it' sone cycle only.

    Yet registers are used for more than just a data source. Thy can be used as index, absolute or relative address poitners etc. Since this usage makes no sense for the status register, using the status register in one of these addressing modes will instead return a fixed value, a constant. Since there are 4 of these addressing modes, and one are still required for accessing the SR normally /register mode) and one for the absolute mode &xxxx which is done with SR in indexed mode (xxxx(SR)), the R3 register is also used for constant generation. It cannot be used for anything else. So 6 constants are available, which are addressed by addressing R2 or R3 with one of the 4 addressing modes instead of adding an immediate constant value to the code itself. Effectively these 6 values are hardcoded into the instruction word itself. No additional data word to read from flash, one less MCLK cycle to execute. Especially usefule for all those INC and DEC, DINT, EINT instructions one needs so often. They all can be emulated by an OR or ADD instruction using one of the constants (the GIE bit is #8).

    The exception mentioned by OCY is the push/pop instruction which cannot use the SR/R2 for the constant generation when pushing a value on stack. Since this affects only the value #4 and #8 which are really seldom pushed on the stack (0 and -1 are way more common), it's not a big drawback.

    Sicne all this usage of the constant generator is automatically done by the assembler, you usually don't need to think about it. Only that you cannot access R2 other than directly and r3 not at all. At least not with the normal results.

    If one writes
    dint
    this is equal to
    BIC #8,SR
    and the compiler silently generates
    BIC @SR+,SR
    where using SR with the indirect autoincrement addressing mode returns the constant #8 instead of doing some stupid memory reading. And takes only 2 bytes and 1 cycle instead of 4 bytes and 2 cycles.

    Since all instructions are numbered with an orthogonal scheme, in this case 4 bit for the BIC command (0xC), 4 bit for the source register number, 4 bits for the source and destination addressing modes and the operand width (byte/word) and finally 4 bits for the destination register number, you can verify this mechanism by looking at the generated binary value.

     

  • can any one explain me what is use of constant generator in msp430 with simple and clear example.

  • I've just read about it, the way I see it. it's a caching mechanism to speed things up. The most used constants are held in registers. 

  • hi,

     what is meant by zero brown-out reset in msp430 microcontroller .can any one explain me it in deatails.

    with regards

    nanendra

  • narendra babu said:
    zero brown-out reset

    I don't remember this term. However, a brown-out reset is performed when the supply voltage has dropped below (or not yet risen above) a certain threshold. It is a mechanism that keep shte MSP in reset state until a minimum voltage is reached and prevents cpu crashes due to temporarily breakdown of the supply voltage. The device will reset, but for a microcontrolelr this is preferrable over 'crashed and no longer responding'.

    narendra babu said:
    can any one explain me what is use of constant generator in msp430 with simple and clear example.

    Whenever an immediate, constant value of 0,1,2,4,8 or -1 is required for an operation, teh compile rwill generate assembly code that will not generate this value as a stored constant (e.g. MOV #0,R15), but it will generate an instruciton that accesses the R2/R3 register in a way that it will return this constant value instead of the real register value. In case of #0, this would be 'register mode': MOV R3,R15. Whenever R3 is directly used as source operand in register mode, it will return the constant 0. If it is used in indexed mode (R3), it will always return 1. If indirect register mode is used, it will return 2 (so MOV @R3, R15 will move 2 into R15). R2, which is the status register, also provides two constants, +4 and +8, if used in indirect or indirect autoincrement mode.

    Since 1,2,4 and 8 are the most-used bits in teh status register, thsi makes double sense.

    So if you have an instruciton like BIS #GIE,SR - which is equivalent to EINT() or __enable_interrupt() -, it will evaluate to BIS @SR++,SR instead, a single-cycle-instruction.

    The use of the constant generator is normally transparent, so you'll only see the BIS #GIE,SR (or rather BIS #8,R2) instruction in the assembly source or the disassembled code. Assembler/disassembler will automatically use the constant generator if possible.

  • hi,

     tanks for your replay. zero brown-out reset is one of    features of msp430 .

    with regards

    narendra

  • hi,

     can any one help me with small assembly code operation of pc,sp,sr & cg1,cg2   in msp430 cpu.

  • narendra babu said:
    zero brown-out reset is one of    features of msp430 .

    This term is nowhere used. After 6 years of MSP development, I read it for the first time.
    Sure, brown-out reset is a feature of many (but not all!) MSP439 processors (there is no 'the MSP430', it is a processor family). But what has 'zero' to do with it? I just did a search through the users guides and never found the word 'zero' anywhere except if referring to the zero bit in the status register.

    About you assembly question: what exactly is your problem? These a standard cpu instructions and without a context (what do you want to do/know exactly), there is no answer.

  • Perhaps the OP is referring to the 'zero-power BOR'? Maybe that is the origin to the 'zero'.

**Attention** This is a public forum