Chapter 3. Advanced Techniques

Table of Contents
3.1. Interrupt Handling
3.2. Using Optimizations
3.3. Using a Custom Start-file
3.4. Generating PROM Programming Files
3.5. Using the Debugger
3.6. Working with the Target
3.7. Expanded Memory

Once you have mastered writing and running a small program, you'll want to check out some of the more advanced techniques required to write a real application program. In this chapter, we cover the following topics:

3.1. Interrupt Handling

Interrupt handling is made relatively simple by the header file <intrrpt.h>. This file declares the interrupt numbers, and provides a function to connect any interrupt to a user handler. The header file is installed as /opt/xgc/m1750-coff/include/intrrpt.h.

The run-time system contains a declaration of the interrupt vector, and provides the functions to connect a C function to an interrupt so that when the interrupt is raised, the C function is called. You should note that the stack used during an interrupt is not the main stack. The first interrupt switches from the main stack to the interrupt stack, and then any nested interrupts continue to use the interrupt stack. You should therefore ensure that the interrupt stack is big enough to accommodate interrupts nested up to the maximum depth your hardware is capable of.

Where an interrupt handler accesses a data area shared between the handler and some base level code, then objects in the data area should be marked as 'volatile'. This ensures that the compiler generates all the loads and all the stores and does not work on copies in registers.

This is the file hello8.cpp from the examples directory.

////////////////////////////////////////////////////////////////////////
//
// Filename:
//
//   hello8.cpp
//
// Description:
//
//   Example of how to connect an interrupt to a handler.
//

#include <stdio.h>
#include <intrrpt.h>

static volatile int cnt;

void
timer_handler (int sig)
{
  //
  // Reset timer B to interrupt after a further 100 mSec
  //
  asm volatile ("xio    %0,otb" : : "r" (-1000));

  printf ("Hello world\n");
  cnt++;
}

int
main ()
{
  //
  // Connect our interrupt handler to the timer B interrupt.
  //
  handler (INTTIMERB, timer_handler);

  //
  // Set timer B to interrupt after 100 mSec
  //
  asm volatile ("xio    %0,otb" : : "r" (-1000));

  //
  // Wait until we've had 10 clock interrupts
  //
  while (cnt < 10)
    ;
}