Initializing the Vector Table in a source file using a relocatable section

Initializing the vector table in the assembly source file requires that all the entries in the table are initialized. Interrupts, which are not used, must be associated with a standard handler.

The labels or functions that should be inserted in the vector table must be implemented in the assembly source file or an external reference must be available for them. The vector table can be defined in an assembly source file in an additional section containing constant variables. See the following listing:

Listing: Initializing the Vector Table in source code with a relocatable section

          XDEF  ResetFunc
          XDEF  IRQ0Int

DataSec:  SECTION

Data:     DS.W  5 ; Each interrupt increments an element of the table.

CodeSec:  SECTION

; Implementation of the interrupt functions.

IRQ1Func:

          LDA   #0

          BRA   int

SWIFunc:

          LDA   #4

          BRA   int

ResetFunc:

          LDA   #8

          BRA   entry

DummyFunc:

          RTI

int:

          PSHH

          LDHX  #Data  ; Load address of symbol Data in X

          ; X <- address of the appropriate element in the tab

Ofset:    TSTA

          BEQ   Ofset3

Ofset2:

          AIX   #$1

          DECA

          BNE   Ofset2

Ofset3:

          INC   0, X   ; The table element is incremented

          PULH

          RTI

entry:

          LDHX  #$0E00 ; Init Stack Pointer to $E00-$1=$DFF

          TXS

          CLRX

          CLRH

          CLI          ; Enables interrupts

loop:     BRA   loop

VectorTable: SECTION

; Definition of the vector table.

IRQ1Int:  DC.W  IRQ1Func

IRQ0Int:  DC.W  DummyFunc

SWIInt:   DC.W  SWIFunc

ResetInt: DC.W  ResetFunc
Note: Each constant in the VectorTable section is defined as a word (a 2-byte constant), because the entries in the vector table are 16 bits wide.
Note: In the previous example, the constant IRQ1Int is initialized with the address of the label IRQ1Func. The constant IRQ0Int is initialized with the address of the label Dummy Func because this interrupt is not in use.
Note: All the labels specified as initialization value must be defined, published (using XDEF) or imported (using XREF) before the vector table section. No forward reference is allowed in the DC directive.
Note: The constant IRQ0Int is exported so that the section containing the vector table is linked with the application.

The section should now be placed at the expected address. This is performed in the linker parameter file, as listed in the following listing:

Listing: Example linker parameter file

LINK test.abs
NAMES

  test.o+

END

ENTRIES

  IRQ0Int

END

SECTIONS

  MY_ROM   = READ_ONLY  0x0800 TO 0x08FF;

  MY_RAM   = READ_WRITE 0x0B00 TO 0x0CFF;

  MY_STACK = READ_WRITE 0x0D00 TO 0x0DFF;

/* Define the memory range for the vector table */

  Vector   = READ_ONLY  0xFFF8 TO 0xFFFF;

END

PLACEMENT

  DEFAULT_RAM        INTO MY_RAM;

  DEFAULT_ROM        INTO MY_ROM;

  SSTACK             INTO MY_STACK;

/* Place the section 'VectorTable' at the appropriated address. */

  VectorTable        INTO Vector;

END

INIT ResetFunc
Note: The statement Vector = READ_ONLY 0xFFF8 TO 0xFFFF defines the memory range for the vector table.
Note: The statement VectorTable INTO Vector specifies that the vector table should be loaded in the read only memory area Vector. This means, the constant IRQ1Int will be allocated at address 0xFFF8, the constant IRQ0Int will be allocated at address 0xFFFA, the constant SWIInt will be allocated at address 0xFFFC, and the constant ResetInt will be allocated at address 0xFFFE.
Note: The `+' after the object file name switches smart linking off. If this statement is missing in the PRM file, the vector table will not be linked with the application, because it is never referenced. The smart linker only links the referenced objects in the absolute file.