From: <Saved by Windows Internet Explorer 7>
Subject: Exception and Interrupt Controller
Date: Tue, 30 Dec 2008 11:45:07 -0500
MIME-Version: 1.0
Content-Type: text/html;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.johnloomis.org/NiosII/interrupts/exception.html
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Exception and Interrupt Controller</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<META content=3D"MSHTML 6.00.6000.16788" name=3DGENERATOR></HEAD>
<BODY>
<H1>Exception and Interrupt Controller</H1>
<H2>Control Registers</H2>The Nios II architecture supports up to 32 =
control=20
registers. Those involved in exception and interrupt processing are =
shown below.=20

<P>
<DIR>
<TABLE border=3D1>
  <TBODY>
  <TR>
    <TH>Register
    <TH>Name
    <TH>Register Contents=20
  <TR>
    <TD>0
    <TD><TT><B>status</B></TT>
    <TD><A=20
      =
href=3D"http://www.johnloomis.org/NiosII/interrupts/exception.html#status=
"><TT>status</TT></A>=20
      controls the state of the Nios II processor=20
  <TR>
    <TD>1
    <TD><TT><B>estatus</B></TT>
    <TD>holds a copy of the status register during non-break exception=20
      processing.=20
  <TR>
    <TD>2
    <TD><TT><B>bstatus</B></TT>
    <TD>holds a copy of the status register during break exception =
processing.=20

  <TR>
    <TD>3
    <TD><TT><B>ienable</B></TT>
    <TD>Interrupt-enable bits (see <A=20
      =
href=3D"http://www.johnloomis.org/NiosII/interrupts/exception.html#ienabl=
e"><TT>ienable</TT></B>)=20
      </A>
  <TR>
    <TD>4
    <TD><TT><B>ipending</B></TT>
    <TD>Pending-interrupt bits (see <A=20
      =
href=3D"http://www.johnloomis.org/NiosII/interrupts/exception.html#ipendi=
ng"><TT>ipending</TT></A>=20
  </TR></TBODY></TABLE></DIR>
<H3><A name=3Dstatus>The status Register</H3>
<DIR>
<P>
<TABLE border=3D1>
  <TBODY>
  <TR>
    <TH>Bits
    <TH>Field
    <TH>Description=20
  <TR>
    <TD>31:3
    <TD><BR>
    <TD>Reserved=20
  <TR>
    <TD>2
    <TD><TT><B>EH</B></TT>
    <TD>EH is the exception handler bit. The processor sets EH to one =
when an=20
      exception occurs (including breaks). Software clears EH to zero =
when ready=20
      to handle exceptions again. EH is used by the MMU to determine =
whether a=20
      TLB miss exception is a fast TLB miss or a double TLB miss. In =
systems=20
      without an MMU, EH is always zero.=20
  <TR>
    <TD>1
    <TD><TT><B>U</B></TT>
    <TD>U is the user mode bit. When U =3D 1, the processor operates in =
user=20
      mode. When U =3D 0, the processor operates in supervisor mode. In =
systems=20
      without an MMU, U is always zero.=20
  <TR>
    <TD>0
    <TD><TT><B>PIE</B></TT>
    <TD>PIE is the processor interrupt-enable bit. When PIE =3D 0, =
interrupts=20
      are ignored. When PIE =3D 1, interrupts can be taken, depending on =
the value=20
      of the <TT>ienable</TT> register. </TR></TBODY></TABLE></P></DIR>
<H3><A name=3Dienable>The ienable Register</H3>
<P>The <TT>ienable</TT> register controls the handling of external =
hardware=20
interrupts. Each bit of the ienable register corresponds to one of the =
interrupt=20
inputs, <TT>irq0</TT> through <TT>irq31</TT>. A value of one in bit =
<I>n</I>=20
means that the corresponding irq<I>n</I> interrupt is enabled; a bit =
value of=20
zero means that the corresponding interrupt is disabled. See =
=93Exception=20
Processing=94 on page 3=9634 for more information.=20
<H3><A name=3Dipending>The ipending Register</H3>
<P>The value of the <TT>ipending</TT> register indicates the value of =
the=20
interrupt signals driven into the processor. A value of one in bit =
<I>n</I>=20
means that the corresponding irq<I>n</I> input is asserted. Writing a =
value to=20
the <TT>ipending</TT> register has no effect.=20
<H2>Control Instructions</H2>
<P>
<TABLE border=3D1>
  <TBODY>
  <TR>
    <TH>Instruction
    <TH>Description=20
  <TR>
    <TD><TT><B>trap<BR>eret </B></TT>
    <TD>The <TT>trap</TT> and <TT>eret</TT> instructions generate and =
return=20
      from exceptions. These instructions are similar to the call/ret =
pair, but=20
      are used for exceptions. <TT>trap</TT> saves the <TT>status</TT> =
register=20
      in the <TT>estatus</TT> register, saves the return address in the=20
      <TT>ea</TT> register, and then transfers execution to the general=20
      exception handler. <TT>eret</TT> returns from exception processing =
by=20
      restoring <TT>status</TT> from <TT>estatus</TT>, and executing the =

      instruction specified by the address in <TT>ea</TT>.=20
  <TR>
    <TD><TT><B>break<BR>bret </B></TT>
    <TD>The <TT>break</TT> and <TT>bret</TT> instructions generate and =
return=20
      from breaks. <TT>break</TT> and <TT>bret</TT> are used exclusively =
by=20
      software debugging tools. Programmers never use these instructions =
in=20
      application code.=20
  <TR>
    <TD><TT><B>rdctl<BR>wrctl </B></TT>
    <TD>These instructions read and write control registers, such as the =

      <TT>status</TT> register. The value is read from or stored to a=20
      general-purpose register.=20
  <TR>
    <TD><TT><B>flushd<BR>flushda<BR>flushi<BR>initd<BR>initda<BR>initi=20
      </B></TT>
    <TD>These instructions are used to manage the data and instruction =
cache=20
      memories.=20
  <TR>
    <TD><TT><B>flushp</B></TT>
    <TD>This instruction flushes all pre-fetched instructions from the=20
      pipeline. This is necessary before jumping to recently-modified=20
      instruction memory.=20
  <TR>
    <TD><TT><B>sync </B></TT>
    <TD>This instruction ensures that all previously-issued operations =
have=20
      completed before allowing execution of subsequent load and store=20
      operations. </TR></TBODY></TABLE>
<H2>Exception Controller</H2>
<P>The Nios II architecture provides a simple, non-vectored exception =
controller=20
to handle all exception types. Each exception, including hardware =
interrupts,=20
causes the processor to transfer execution to an exception address. An =
exception=20
handler at this address determines the cause of the exception and =
dispatches an=20
appropriate exception routine.=20
<P>Exception addresses are specified in SOPC Builder at system =
generation time.=20
<P>All exceptions are precise. Precise means that the processor has =
completed=20
execution of all instructions preceding the faulting instruction and not =
started=20
execution of instructions following the faulting instruction. Precise =
exceptions=20
allow the processor to resume program execution once the exception =
handler=20
clears the exception.=20
<H2>Integral Interrupt Controller</H2>
<P>The Nios II architecture supports 32 external hardware interrupts. =
The=20
processor core has 32 level-sensitive interrupt request (IRQ) inputs, =
irq0=20
through irq31, providing a unique input for each interrupt source. IRQ =
priority=20
is determined by software. The architecture supports nested interrupts.=20
<P>The software can enable and disable any interrupt source individually =
through=20
the ienable control register, which contains an interrupt-enable bit for =
each of=20
the IRQ inputs. Software can enable and disable interrupts globally =
using the=20
PIE bit of the status control register. A hardware interrupt is =
generated if and=20
only if all three of these conditions are true:=20
<P>
<UL>
  <LI>The PIE bit of the <TT>status</TT> register is 1=20
  <LI>An interrupt-request input, <TT>irq&lt;n&gt;</TT>, is asserted=20
  <LI>The corresponding bit <I>n</I> of the <TT>ienable</TT> register is =
1=20
</LI></UL>
<H2>Interrupt Vector Custom Instruction</H2>
<P>The Nios II processor core offers an interrupt vector custom =
instruction=20
which accelerates interrupt vector dispatch. Include this custom =
instruction to=20
reduce your program's interrupt latency.=20
<P>The interrupt vector custom instruction is based on a priority =
encoder with=20
one input for each interrupt connected to the Nios II processor. The =
cost of the=20
interrupt vector custom instruction depends on the number of interrupts=20
connected to the Nios II processor. The worst case is a system with 32=20
interrupts. In this case, the interrupt vector custom instruction =
consumes about=20
50 logic elements (LEs).=20
<P>If you have a large number of interrupts connected, adding the =
interrupt=20
vector custom instruction to your system might lower =
<I>f</I><SUB>MAX</SUB>.=20
<H2>Processing Interrupt and Instruction-Related Exceptions</H2>
<P>Except for the break instruction (see =93Processing a Break=94 on =
page 3-38),=20
this section describes the actions the processor takes in response to =
interrupt=20
and instruction-related exceptions. Table 3-32 lists all possible =
non-break=20
exception processing actions performed by hardware. Check marks indicate =
which=20
actions apply to each of the processor scenarios, namely, systems =
without an=20
MMU, systems with an MMU, and systems with an MMU that is currently =
processing=20
an exception. For systems with an MMU, <TT>status.EH</TT> indicates =
whether or=20
not exception processing is already in progress. When <TT>status.EH</TT> =
=3D 1,=20
exception processing is already in progress and the states of the =
exception=20
registers are preserved to retain the original exception states.=20
<H4>Processor Actions (no MMU)</H4>
<UL>
  <P>
  <LI>Copies the contents of the <TT>status</TT> control register to the =

  <TT>estatus</TT> control eregister, saving the processor's =
pre-exception=20
  status.=20
  <P></P>
  <LI>Clears <TT>status.PIE</TT> to zero, disabling external processor=20
  interrupts.=20
  <P></P>
  <LI>Writes the address of the instruction following the exception to =
the=20
  <TT>ea</TT> register (r29).=20
  <P></P>
  <LI>Clears <TT>status.U</TT> to zero, forcing the processor into =
supervisor=20
  mode.=20
  <P></P>
  <LI>Sets <TT>status.EH</TT> to one, indicating the processor is =
handling an=20
  exception.=20
  <P></P>
  <LI>Passes control to the general exception vector, invoking the =
general=20
  exception handler </LI></UL>
<P>
<HR>

<P>Maintained by <A href=3D"http://www.johnloomis.org/">John Loomis</A>, =
last=20
updated <I>10 November 2008 </I></P></BODY></HTML>
