Cortex - read out status register of ALU

Status
Not open for further replies.

Welocs

Well-known member
Hi there,

as the title says, i want to get information about a computation of the ALU. Are these register/s available for teensyduino - or can i find these register/s in the datasheet for teensy 3.x?

Why i want this register/s?
I want to write dimming functions on my own (too learn and so on - do not come with what ever libraries please :) ) and i'd like to check a variable to have an overflow or an underflow.
On simple 8-bit MCUs, like e.g. 16F-family of microchip, the simplest way is to check the Carry Flag in the status register of the ALU.
How this can simply done in C - or how i can access/find this register?

Thanks,
Greetings,
Welocs.
 
Yes, the ARM processor has ALU status bits in its APSR register. You can read it with the MRS instruction.

Here's an example which reads the "Q" status bit.

https://github.com/PaulStoffregen/Audio/blob/master/utility/dspinst.h#L336

The five status bits are N, Z, C, V, Q (negative, zero, carry, overflow and saturation), in bit position 31 to 27. Cortex-M0+ on Teensy LC doesn't have the Q bit, because it lacks the DSP and other special math instructions with saturation capability.

You should probably get Joseph Yiu's book(s). It's by far the best reference for these sorts of ARM processor details. You can find links to it here:

https://www.pjrc.com/teensy/datasheets.html

ARM also publishes this info, but their manuals are much harder to read than Yiu's book. But they can be downloaded for free. The document numbers to search are DDI0403E (for Cortex-M4 on Teensy 3.x) and DDI0419C (for Cortex-M0+ on Teensy LC).

Realistically, the gcc compiler optimizations may reorder instructions in surrounding code in ways you don't expect, which could make using N, Z, C, V problematic. You might need to do the operation you wish to test in the same chunk of inline assembly as the MRS instruction which reads the status bit.

The floating point processor on Teensy 3.5 & 3.6 also has some special floating point status bits. They're also documented in Yiu's book and ARM's reference manual.
 
ok, i found out, that in the cortex-m4 core you cannot access the status register directly. Information about negative, zero, overflow, carry/borrow and dsp overflow flags are available in the PSR register which "contains" the APSR register.
These are accessible through the following assembler code:
Code:
MRS MY_REGISTER, APSR
(more you can find out at this website: http://infocenter.arm.com/help/topic/com.arm.doc.dui0553a/CHDBIBGJ.html)

You also have the possibility to make IT (if-then) branches or something else after or while an instruction. But how it's done in C? Is there a simple way of checking overflows and underflows of unsigned vars?
I know, that i also just could prevent an overflow of one byte e.g. like this:
Code:
if (a + b > 255){
   do...
}
but this might not work or be fast on 8-bit MCUs.
There are also solutions with min() and max() or similar functions to get the borders of vars, but in my opinion it is not simple or "well designed" enough (if you understand what i mean =D ).
 
hi paul,

thank you very much!
yes, i found this online documentation and it too me a few minutes to find everything i wanted to know...(see the link above)

i think i will now just check it with a simple if condition. at the moment i don't have time to get the knowledge of the right asm inline syntax - maybe i'll do this later.
 
yes i was was :)...

Yeah i saw it. I thought there may be a standard function in C like the min(), max() functions or the stio's.

But thank you so far, you helped me a lot!
 
If you're accustomed to programming those old PIC 16F series chips, you may be surprised by how crafty the gcc compiler can be with ARM Cortex-M4 and even Cortex-M0. Often very dramatic results can be achieved by optimizing algorithms and restructuring code to allow the compiler to use its optimizations. Yes, I know this sounds crazy if your world view is only from that ancient accumulator-based PIC architecture and its terrible C compiler, but it's true...

For example, here's a thread from a couple years ago where we optimized the integer printing code for Teensy LC.

https://forum.pjrc.com/threads/28932-LC-is-10-9-times-slower-than-T3-1?p=75893&viewfull=1#post75893

Here you can see my best attempt at coding Stimmer's algorithm in hand optimized assembly. In the end, it turned out mlu came up with a slight restructuring of the C code that allowed the compiler to beat it by 10%.

Still, if you *really* want to directly access status bits, you certainly can. It just takes a small chunk of inline asm, since the C language doesn't have semantics for the status bits.
 
An other, very simple way, if you're used to use 16BIT variables is to let the compiler do its job and just use 32Bit instead. If you really need to know if the result is >16BIT just use "> 65535" ;-)
Yes i know, , this is obvious, but sometimes you do not think of the simplest things ...and the compiler has great possibilities to optimize this.

"int" is 32BIT, by the way.
 
Status
Not open for further replies.
Back
Top