The target interface system uses CrossStudio’s JavaScript (ECMAScript) interpreter to support board and target-specific behaviour.

The main use for this is to support non-standard target and board reset schemes and also to configure the target after reset, see Reset Script for more information.

The target script system can also be used to carry out target specific operations when the debugger attaches, stops or starts the target. This can be useful when debugging with caches enabled as it provides a mechanism for the debugger to flush and disable caches when the processor enters debug state and then re-enable the caches when the processor is released into run state. See Attach Script, Stop Script, and Run Script for more information.

In order to reduce script duplication, when the target interface runs a reset, attach, run or stop script it first looks in the current active project for a file marked with a project property File Type set to Reset Script. If a file of this type is found it will be loaded prior to executing the scripts, each of the scripts can then call functions within this script file.

## Reset Script

The Reset Script property held in the Target project property group is used to define a script to execute to reset and configure the target.

The aim of the reset script is to get the processor into a known state. When the script has executed the processor should be reset, stopped on the first instruction and configured appropriately.

As an example, the following script demonstrates the reset script for an Evaluator 7T target board with a memory configuration that re-maps SRAM to start from 0x00000000. The Evaluator7T_Reset function carries out the standard ARM reset and stops the processor prior to executing the first instruction. The Evaluator7T_ResetWithRamAtZero function calls this reset function and then configures the target memory by accessing the configuration registers directly. See TargetInterface Object for a description of the TargetInterface object which is used by the reset script to access the target hardware.

    function Evaluator7T_Reset()
    {
      TargetInterface.setNSRST(0);
      TargetInterface.setICEBreakerBreakpoint(0, 0x00000000, 0xFFFFFFFF,
                                              0x00000000, 0xFFFFFFFF, 0x100, 0xF7);
      TargetInterface.setNSRST(1);
      TargetInterface.waitForDebugState(1000);
      TargetInterface.trst();
    }

    function Evaluator7T_ResetWithRamAtZero()
    {
      Evaluator7T_Reset();

      /***************************************************************************
      * Register settings for the following memory configuration:
      *
      *	+----------------------+
      * | ROMCON0 - 512K FLASH | 0x01800000 - 0x0187FFFF
      * +----------------------+
      * | ROMCON2 - 256K SRAM  | 0x00040000 - 0x0007FFFF
      *	+----------------------+
      * | ROMCON1 - 256K SRAM  | 0x00000000 - 0x0003FFFF
      *	+----------------------+
      *
      ***************************************************************************/

      TargetInterface.pokeWord(0x03FF0000, 0x07FFFFA0); // SYSCFG
      TargetInterface.pokeWord(0x03FF3000, 0x00000000); // CLKCON
      TargetInterface.pokeWord(0x03FF3008, 0x00000000); // EXTACON0
      TargetInterface.pokeWord(0x03FF300C, 0x00000000); // EXTACON1
      TargetInterface.pokeWord(0x03FF3010, 0x0000003E); // EXTDBWIDTH
      TargetInterface.pokeWord(0x03FF3014, 0x18860030); // ROMCON0
      TargetInterface.pokeWord(0x03FF3018, 0x00400010); // ROMCON1
      TargetInterface.pokeWord(0x03FF301C, 0x00801010); // ROMCON2
      TargetInterface.pokeWord(0x03FF3020, 0x08018020); // ROMCON3
      TargetInterface.pokeWord(0x03FF3024, 0x0A020040); // ROMCON4
      TargetInterface.pokeWord(0x03FF3028, 0x0C028040); // ROMCON5
      TargetInterface.pokeWord(0x03FF302C, 0x00000000); // DRAMCON0
      TargetInterface.pokeWord(0x03FF3030, 0x00000000); // DRAMCON1
      TargetInterface.pokeWord(0x03FF3034, 0x00000000); // DRAMCON2
      TargetInterface.pokeWord(0x03FF3038, 0x00000000); // DRAMCON3
      TargetInterface.pokeWord(0x03FF303C, 0x9C218360); // REFEXTCON

## Attach Script

The Attach Script property held in the Target project property group is used to define a script that is executed when the debugger first attaches to an application. This can be after a download or reset before the program is run or after an attach to a running application. The aim of the attach script is to carry out any target specific configuration before the debugger first attaches to the application being debugged.

See TargetInterface Object for a description of the TargetInterface object which is used by the attach script to access the target hardware.

## Stop Script

The Stop Script property held in the Target project property groups is used to define a script that is executed when the target enters debug/stopped state. This can be after the application hits a breakpoint or when the Debug | Break operation has been carried out. The aim of the stop script is to carry out any target specific operations before the debugger starts accessing target memory. This is particularly useful when debugging applications that have caches enabled as the script can disable and flush the caches enabling the debugger to access the current memory state.

See TargetInterface Object for a description of the TargetInterface object which is used by the stop script to access the target hardware.

## Run Script

This Run Script property held in the Target project property group is used to define a script that is executed when the target enters run state. This can be when the application is run for the first time or when the Debug | Go operation has been carried out after the application has hit a breakpoint or been stopped using the Debug | Break operation. The aim of the run script is to carry out any target specific operations after the debugger has finished accessing target memory. This can be useful to re-enable caches previously disabled by the stop script.

See TargetInterface Object for a description of the TargetInterface object which is used by the run script to access the target hardware.

## TargetInterface Object

The TargetInterface object is used to access the currently connected target interface. The following section describes the TargetInterface object's member functions.

### TargetInterface.beginDebugAccess

##### Synopsis

    TargetInterface.beginDebugAccess()

##### Description

Put target into debug state if it is not already in order to carry out a number of debug operations. The idea behind beginDebugAccess and endDebugAccess is to minimize the number of times the target enters and exits debug state when carrying out a number of debug operations. Target interface functions that require the target to be in debug state (such as peek and poke) also use beginDebugAccess and endDebugAccess to get the target into the correct state. A nesting count is maintained, incremented by beginDebugAccess and decremented by endDebugAccess. The initial processor state is recorded on the first nested call to beginDebugAccess and this state is restored when the final endDebugAccess is called causing the count to return to it initial state.

### TargetInterface.delay

##### Synopsis

    TargetInterface.delay(milliseconds)

##### Description

TargetInterface.delay waits for milliseconds milliseconds

### TargetInterface.endDebugAccess

##### Synopsis

    TargetInterface.endDebugAccess(alwaysRun)

##### Description

Restore the target run state recorded at the first nested call to beginDebugAccess. See beginDebugAccess for more information. If alwaysRun is non-zero the processor will exit debug state on the last nested call to endDebugAccess.

### TargetInterface.eraseBytes

##### Synopsis

    TargetInterface.eraseBytes(address, length)

##### Description

TargetInterface.eraseBytes erases a block of erasable memory. address is the start address of the block to erase and length is the number of bytes to erase.

### TargetInterface.executeFunction

##### Synopsis

    TargetInterface.executeFunction(address, r0, timeout)

##### Description

TargetInterface.executeFunction executes a function on the target.address is the address of the function entry point, r0 is the value to set register r0 on entry to the function (in effect the first parameter to the function), and timeout is the timeout value in milliseconds to wait for the function to complete.

### TargetInterface.getRegister

##### Synopsis

    TargetInterface.getRegister(register)

##### Description

TargetInterface.getRegister gets the value of a CPU register. Note that the set of register values are only updated when the CPU stops. register is a string specifying the register to get and must be one of r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, sp, lr, pc, cpsr, r8_fiq, r9_fiq, r10_fiq, r11_fiq, r12_fiq, r13_fiq, r14_fiq, spsr_fiq, r13_svc, r14_svc, spsr_svc, r13_abt, r14_abt, spsr_abt, r13_irq, r14_irq, spsr_irq, r13_und, r14_und, spsr_und. TargetInterface.getRegister returns the register's value.

### TargetInterface.peekByte

##### Synopsis

    TargetInterface.peekByte(address)

##### Description

TargetInterface.peekByte reads a byte of target memory from address and returns it.

### TargetInterface.peekBytes

##### Synopsis

    TargetInterface.peekBytes(address, length)

##### Description

TargetInterface.peekBytes reads a block of bytes from target memory starting at >address for length bytes and returns the result as an array containing the bytes read.

### TargetInterface.peekUint16

##### Synopsis

    TargetInterface.peekUint16(address)

##### Description

TargetInterface.peekUint16 reads a 16-bit unsigned integer from target memory from address and returns it.

### TargetInterface.peekUint32

##### Synopsis

    TargetInterface.peekUint32(address)

##### Description

TargetInterface.peekUint32 reads a 32-bit unsigned integer from target memory from address and returns it.

### TargetInterface.peekWord

##### Synopsis

    TargetInterface.peekWord(address)

##### Description

TargetInterface.peekWord reads a word as an unsigned integer from target memory from address and returns it.

### TargetInterface.pokeByte

##### Synopsis

    TargetInterface.pokeByte(address, data)

##### Description

TargetInterface.pokeByte writes the byte data to address in target memory.

##### Synopsis

### TargetInterface.pokeUint16

    TargetInterface.pokeUint16(address, data)

##### Description

TargetInterface.pokeUint16 writes data as a 16-bit value to address in target memory.

##### Synopsis

### TargetInterface.pokeUint32

    TargetInterface.pokeUint32(address, data)

##### Description

TargetInterface.pokeUint32 writes data as a 32-bit value to address in target memory.

##### Synopsis

### TargetInterface.pokeWord

    TargetInterface.pokeWord(address, data)

##### Description

TargetInterface.pokeWord writes data as a word value to address in target memory.

### TargetInterface.pokeBytes

##### Synopsis

### TargetInterface.pokeBytes

    TargetInterface.pokeBytes(address, data)

##### Description

TargetInterface.pokeBytes writes the array data containing 8-bit data to target memory at address.

### TargetInterface.peekMultUint16

##### Synopsis

    TargetInterface.peekMultUint16(address, length)

##### Description

TargetInterface.peekMultUint16 reads length unsigned 16-bit integers from target memory starting at address and returns them as an array.

### TargetInterface.peekMultUint32

##### Synopsis

    TargetInterface.peekMultUint32(address, length)

##### Description

TargetInterface.peekMultUint32 reads length unsigned 32-bit integers from target memory starting at address and returns them as an array.

### TargetInterface.pokeMultUint16

##### Synopsis

    TargetInterface.pokeMultUint16(address, data)

##### Description

TargetInterface.pokeBytes writes the array data containing 16-bit data to target memory at address.

### TargetInterface.pokeMultUint32

##### Synopsis

    TargetInterface.pokeMultUint32(address, data)

##### Description

TargetInterface.pokeBytes writes the array data containing 32-bit data to target memory at address.

### TargetInterface.setICEBreakerBreakpoint

##### Synopsis

    TargetInterface.setICEBreakerBreakpoint(n, addressValue, addressMask, dataValue, dataMask controlValue, controlMask)

##### Description

TargetInterface.setICEBreakerBreakpoint sets an ICEBreaker breakpoint. n is the number of the watchpoint unit to use, addressValue is the address value, addressMask is the address mask, dataValue is the data value, dataMask is the data mask, controlValue is the control value, and controlMask is the control mask.

### TargetInterface.resetAndStop

##### Synopsis

    TargetInterface.resetAndStop(delay)

##### Description

TargetInterface.resetAndStop resets the target by cycling nSRST and then stops the processor. The delay parameter specifies the number of milliseconds to hold the nSRST signal. This function allows the processor to run for a period of time before the processor is stopped which may cause unpredictable behaviour as the processor may be left in an undefined state if it has been allowed to start up from reset.

### TargetInterface.stopAndReset

##### Synopsis

    TargetInterface.stopAndReset(delay)

##### Description

TargetInterface.stopAndReset resets the target by stopping the processor and then cycling nSRST. The delay parameter specifies the number of milliseconds to hold the nSRST signal. The difference between this function and TargetInterface.resetAndStop is that this function attempts to keep the processor stopped whilst it is reset and therefore allows you to start your program with the processor in a known state. Some ARM targets however can't do this as the reset also resets the debug interface which allows the processor to run when it is released from reset. On these targets this function behaves in the same way as TargetInterface.resetAndStop.

### TargetInterface.setNSRST

##### Synopsis

    TargetInterface.setNSRST(state)

##### Description

TargetInterface.setNSRST sets the level of the target's nSRST reset signal high if state is non-zero.

### TargetInterface.setRegister

##### Synopsis

    TargetInterface.setRegister(register, value)

##### Description

TargetInterface.setRegister sets the CPU regsister register to value.

register is a string describing the register to set and must be one of r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13 or sp, r14 or lr, r15 or pc, cpsr, r8_fiq, r9_fiq, r10_fiq, r11_fiq, r12_fiq, r13_fiq, r14_fiq, spsr_fiq, r13_svc, r14_svc, spsr_svc, r13_abt, r14_abt, spsr_abt, r13_irq, r14_irq, spsr_irq, r13_und, r14_und, spsr_und.

Note that this function will only change the CPU register state if the CPU is stopped.

### TargetInterface.trst

##### Synopsis

    TargetInterface.trst()

##### Description

TargetInterface.trst performs a JTAG TAP reset.

### TargetInterface.waitForDebugState

##### Synopsis

    TargetInterface.waitForDebugState(timeout)

##### Description

TargetInterface.waitForDebugState waits for the target to enter debug state with a timeout of timeout milliseconds. If the timeout period expires and exception is thrown which is caught by the debugger.