NEON - Store 128bit values - neon

I am newbie to Neon assembly. I am trying to save 128bit values in "q" registers to memory using (vst3.16). I am getting only 64bit "d" saved.
Code is:
__asm__ __volatile__
(
"vld1.u16 {q0}, [%[bay0]] \n\t"
"vld1.u16 {q1}, [%[bay1]] \n\t"
"vld1.u16 {q2}, [%[bay2]] \n\t"
"vst3.16 {d0,d2,d4}, [%[dst]:64] \n\t"
: [bay0]"+r"(bay0), [bay1]"+r"(bay1), [bay2]"+r"(bay2), [dst]"+r"(dst)
);
Any suggestions? Thanks.

Related

What is the most efficient way to pass a local variable address to extended inline ASM for ARM-v7?

Consider this code snippet:
__asm volatile (
" MOVW R0, #0x0000 \n\t"
" MOVT R0, #0x3004 \n\t"
" LDRSH R1, [R0, #12] \n\t"
⋮
)
This is the hard-coded way for loading what's stored at address 0x30040000 + 12 into R1.
I understand I can use a list of input and output variables and a clobber list. So if I use something like this:
int16_t localVar = 10;
__asm volatile (
" <some code with %[lv]> \n\t"
" LDRSH R1, ??? \n\t"
⋮
: [lv] "r" (localVar)
⋮
)
What do I need to replace <some code with %[lv]> and/or the question marks with so that I end up with value 10 in register R1? I generally don't know how to do this. It would be great to learn the most efficient way for ARM-v7 in terms of execution time needed.

Variable exporting between asm files

In asm file1 I try to export a variable and use it in another.
I've tried to find how to do that from manuals & tutorials, but no success.
So, how can I share a global variable between asm files?
// File 1
// Here is saved value of a register (r10) to a variable.
.section .data
.global r10_save
r10_save_addr: .word r10_save
.section .text
ldr r13, =r10_save_addr // Load address for the global variable to some reg (r13)
str r13, [r10] // Save r13 to global variable
// File 2
// Here the intention is to use the variable (that should have r10 value stored).
.section .data
str_r10:
.asciz "r10 = 0x"
strlen_r10 = .-str_r10
.section .text
/* Here I want to use the reference of a variable
which has got its value in other file.
*/
mov r0, $1 //
ldr r1, =str_r10 // address of text string
ldr r2, =strlen_r10 // number of bytes to write
mov r7, $4 //
swi 0
You can use extern to get the value of global varibles:
// File 2
// Here the intention is to use the variable (that should have r10 value stored).
.section .data
str_r10:
.asciz "r10 = 0x"
strlen_r10 = .-str_r10
.section .text
/* Here I want to use the reference of a variable
which has got its value in other file.
*/
.extern r10_save // or EXTERN DATA(r10_save)
mov r0, $1 //
ldr r1, =str_r10 // address of text string
ldr r2, =strlen_r10 // number of bytes to write
mov r7, $4 //
swi 0
Then you can access r10_save in the second file too.

How to create inline assembly functions in C?

I am using a MSP 432 and have to create an assembly functions in C. I tried creating the functions using __asm void PendSV_Handler{}. But that does not work and says Expected an Identifier.
Also, I am trying to run this assembler command cpsid i but it says CPSID is undefined but CPSIE i works. I am a bit confused at this point. I am fairly new to this and I am still learning.
Below is the code I am trying to make assembly. I try making the function assembly by doing __asm void PendSV_handler.
I was not sure if it would be easier to just create a asm. file with these instructions.
OSThread *volatile OS_curr;
OSThread *volatile OS_next;
void PendSV_Handler(void){
__asm__("cpsid i"
//if(OS_curr != (OSThread *)0)
"ldr r0, =OS_curr"
"ldr r0, [r0]"
"cbz r0, PendSV_restore");
// Push r4 - r11
__asm__("PUSH {r4-r11}"
"ldr r1, =OS_curr"
"ldr r1, [r1]"
// OS_curr -> sp = sp;
"str sp, [r1]");
PendSV_restore
// sp=OS_next -> sp;
__asm__("ldr r0, =OS_next;"
"ldr r0, [r0]"
"ldr r0, [r0]"
"str sp, [r13]");
// OS_curr = OS_next;
__asm__("ldr r0, =OS_next"
"ldr r1, [pc, 0xc]"
"ldr r0, =OS_curr"
"str r0, [r1]"
//Pop r4-r11
"POP {r4-r11}"
// __enable_interrupts();
"cpsie i"
//return to next thread
"bx r14");
}
Inline assembler syntax is not redefined in the C programming language, and its support and syntax is compiler specific. In GCC:
void PendSV_Handler(void)
{
__asm__("cpsid i");
//if(OS_curr != (OSThread *)0)
__asm__("ldr r0, =OS_curr");
__asm__("ldr r0, [r0]");
__asm__("cbz r0, PendSV_restore");
// Push r4 - r11
__asm__("PUSH {r4-r11}");
__asm__("ldr r1, =OS_curr");
__asm__("ldr r1, [r1]");
// OS_curr -> sp = sp;
__asm__("str sp, [r1]");
PendSV_restore:
// sp=OS_next -> sp;
__asm__("ldr r0, =OS_next;");
__asm__("ldr r0, [r0]");
__asm__("ldr r0, [r0]");
__asm__("str sp, [r13]");
// OS_curr = OS_next;
__asm__("ldr r0, =OS_next");
__asm__("ldr r1, [pc, 0xc]");
__asm__("ldr r0, =OS_curr");
__asm__("str r0, [r1]");
//Pop r4-r11
__asm__("POP {r4-r11}");
// __enable_interrupts();
__asm__("cpsie i");
//return to next thread
__asm__("bx r14");
}
Referencing
Inline assembler syntax is not redefined in the C programming language
I did in the past for a university project
inline void function(param1,param2)
{
asm volatile ("param1");
asm volatile ("param2");
}
But, if u are working on ARM, look at the instruction set to see whitch commands are possible
As an example:
If you want to write some timing critical stuff, that needs to be in the written order you could do something like this
inline void COLOUR_GLCD_write_address_data(uint8_t address, uint16_t data)
{
asm volatile ("DMB");
*(volatile uint16_t*)LCD_CMD_ADDRESS = address;
asm volatile ("DMB");
*(volatile uint16_t*)LCD_DATA_ADDRESS = data;
}
This was for sending data to an external lcd via the BUS Interface on an Atmel SAME70.
Hope that helped =)

Assembly variables are deleted (16 bit x86 assembly)

I'm still playing with retro programming in turbo C for MS-DOS, and I found some trounble using variables.
If I define some variables at the start of the assembly code (in BSS or DATA), and try to use them inside the assembly function, most of the time these variables are deleted, or end up containing random data.
I learned a bit of assembly for the game boy :) and variables always worked well and never were deleted or modified, I guess x86 asm is different.
Then I tried this using inline assembly and it was a bit better, there is just one variable (width) not working.
void draw_map_column(MAP map, TILE *t){
word *tiledata = &t->data;
int *mapdata = map.data;
int width = map.width<<1;
word tile_offset = 0;
word map_offset = 0;
word screen_offset = 0;
asm{
push ds
push di
push si
mov dx,12 //column
lds bx,[tiledata]
lds si,ds:[bx] //ds:si data address
mov [tile_offset],ds
mov [tile_offset+2],si
les bx,[mapdata]
mov ax,es:[bx]
mov cl,8
shl ax,cl
add si,ax
mov di,screen_offset //es:di screen address
}
loop_tile:
asm{
mov ax,0A000h
mov es,ax
mov ax,16
}
copy_tile:
asm{
mov cx,8
rep movsw
add di,320-16
dec ax
jnz copy_tile
mov ds,[tile_offset]
mov si,[tile_offset+2]
mov ax,map_offset
add ax,[width] //"width" does never contain the value stored at the start
mov map_offset,ax
les bx,[mapdata]
add bx,ax
mov ax,es:[bx]
mov cl,8
shl ax,cl
add si,ax
dec dx
jnz loop_tile
pop si
pop di
pop ds
}
}
Just note the "witdh" variable which is not working at all, if I replace it with a number (40), the code just works as expected (this draws a column of tiles using a map array, and some tiles stored in ram).
I guess it has something to do with the push/pop etc, and something is not set as it should.
Also what happens in pure assembly? none of the variables were working. I defined them as DW and also added:
push bp
mov bp,sp
;function
mov sp,bp
pop bp
Thanks.
Well once again thanks a lot, next time I'll be more patient before asking.
Just in case this is useful for someone, I had defined a variable using the wrong size.
There are other things that can be improved, but that's another question.
Variable "tileoffset" holds a 32 bit address, so it must be a "dword", not a "word". Then the function should be like this:
void draw_map_column(MAP map, TILE *t){
word *tiledata = &t->data;
int *mapdata = map.data;
int width = map.width<<1;
dword tile_offset = 0; //changed to dword to store 32 bit address
word map_offset = 0;
word screen_offset = 0;
asm{
push ds
push di
push si
mov dx,12 //column
lds bx,[tiledata]
lds si,ds:[bx] //ds:si data address
mov word ptr[tile_offset],ds //store a word
mov word ptr[tile_offset+2],si
les bx,[mapdata]
mov ax,es:[bx]
mov cl,8
shl ax,cl
add si,ax
mov di,screen_offset //es:di screen address
}
loop_tile:
asm{
mov ax,0A000h
mov es,ax
mov ax,16
}
copy_tile:
asm{
mov cx,8
rep movsw
add di,320-16
dec ax
jnz copy_tile
mov ds,word ptr[tile_offset] //read a word to the register
mov si,word ptr[tile_offset+2]
mov ax,map_offset
add ax,[width]
mov map_offset,ax
les bx,[mapdata]
add bx,ax
mov ax,es:[bx]
mov cl,8
shl ax,cl
add si,ax
dec dx
jnz loop_tile
pop si
pop di
pop ds
}

How do I get the current interrupt state (enabled, disabled or current level) on a MC9S12ZVM processor

I'm working on a project using a MC9S12ZVM family processor and need to be able to get, save and restore the current interrupt enabled state. This is needed to access variables from the main line code that may be modified by the interrupt handler that are larger than word in size and therefore not atomic.
pseudo code: (variable is 32bits and -= isn't atomic anyhow)
state_save = current_interrupt_state();
DisableInterrupt();
variable -= x;
RestoreInterrupts(state_save);
Edit: I found something that works, but has the issue of modifying the stack.
asm(PSH CCW);
asm(SEI);
Variable++;
asm(PUL CCW);
This is ok as long as I don't need to do anything other than a simple variable++, but I don't like exiting a block with the stack modified.
It seems you are referring to the global interrupt mask. If so, then this is one way to disable it and then restore it to previous state:
static const uint8_t CCR_I_MASK = 0x10;
static uint8_t ccr;
void disable_interrupts (void)
{
__asm PSHA;
__asm TPA; // transfer CCR to A
__asm STA ccr; // store CCR in RAM variable
__asm PULA;
__asm SEI;
}
void restore_interrupts (void)
{
if((ccr & CCR_I_MASK) == 0)
{
__asm CLI; // i was not set, clear it
}
else
{
; // i was set, do nothing
}
}
__asm is specific to the Codewarrior compiler, with or without "strict ANSI" option set.
Ok, I've found an answer to my problem, with thanks to those who commented.
static volatile uint16_t v = 0u;
void testfunction(void);
void testfunction(void)
{
static uint16_t L_CCR;
asm( PSH D2 );
asm( TFR CCW, D2);
asm( ST D2, L_CCR );
asm( PUL D2 );
asm( SEI );
v++;
asm( PSH D2 );
asm( LD D2, L_CCR );
asm( TFR D2, CCW);
asm( PUL D2 );
}