UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

AR# 53823

14.4 - MicroBlaze - Using Software Break in MicroBlaze

Description

This Answer Record describes how software can use the MicroBlaze user vector exception functionality, including simple code examples to illustrate typical use cases, and references to the documentation where the feature is described.


Solution

The MicroBlaze processor provides a user exception that can be invoked by calling the user exception vector. 

This article describes how to declare and call a user exception handler from software.

The User Vector, by default at address 0x8, is defined in the GNU Compiler Tool crt0.o file, where it calls the function named _exception_handler

This function is defined in the newlib library, but not used anywhere in the BSP, so you can directly implement the function in your code.

It can be written in a high level language (C, C++) or assembler.


There are two ways to call a user exception from software, using the normal subroutine call instruction bralid, or using the brki instruction. 

The brki instruction has the advantage of automatically disabling breaks, and thus also interrupts.

When a user exception handler coded in C is called with brki, breaks must be enabled in the handler before returning.

This can be done with an msrclr instruction, provided that the MicroBlaze parameter C_USE_MSR_INSTR is set to 1. 

Also, the return from the user exception handler normally assumes that the call was made with an instruction using a delay slot. 

This can be handled by adding a nop instruction after the brki.

When the MMU is enabled, the user exception handler is executed in real privileged mode, which makes it suitable as an operating system call entry point.


Examples

Here are two simple code examples written in C:

  • Using bralid to call the user exception handler
  • Using brki to call the user exception handler

 

Note that although a message is printed in the handler in these examples, this is generally not a good practice.

 

Figure 1            Calling handler from bralid

#include <stdio.h>

 

#define CALL_USER_VECTOR asm("bralid r15,0x8 ; nop")

 

void _exception_handler ()

{

       xil_printf("In user exception!\r\n");

}

 

)

{

       xil_printf("Main: Calling user exception...\r\n");

       CALL_USER_VECTOR;

       xil_printf("Main: Done.\r\n");

       return 0;

}

 

Figure 2            Calling handler from brki

 

#include <stdio.h>

 

#define CALL_USER_VECTOR asm("brki r15,0x8 ; nop")

#define CLEAR_BIP asm(msrclr r0,8)

 

void _exception_handler ()

{

       xil_printf("In user exception!\r\n");

       CLEAR_BIP;

}

 

int main(

)

{

       xil_printf("Main: Calling user exception using brki...\r\n");

       CALL_USER_VECTOR;

       xil_printf("Main: Done.\r\n");

       return 0;

}

AR# 53823
Date Created 01/16/2013
Last Updated 10/14/2014
Status Active
Type General Article
Tools
  • EDK