> For the complete documentation index, see [llms.txt](https://help.qtech.co.nz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/source-editor.md).

# Source editor

Write a DLP as source code: the layout of an ASM file, naming points and labels, comments, and reusing code with macros.

The source editor is where you write a DLP as code, in an ASM (source) file with the extension `.asm`. It has colour syntax highlighting for ASM and DLP code, and multiple ASM code files may be edited simultaneously.

![The source editor with a new ASM file](https://3621580088-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5hMAUl7E1cWeN5Rghg0%2Fuploads%2FU39psA5HwSgNcBxKNpeA%2Fdlp-ide-main-window.png?alt=media)

For what each instruction does, see the [Instruction reference](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/instruction-reference.md).

## Lay out a source file

Any DLP is made up of two sections:

```
PROGINIT
    ; Initialisation: runs once, when the RTU starts, is reset,
    ; or a new DLP is loaded. Name your points and set starting values here.
PROGSTART
    ; Program: runs over and over, from top to bottom, while the RTU is on.
PROGEND
```

Each line of the program may be:

* blank
* a `;` (semicolon), to indicate the following text is documentation only
* an instruction

The parameters may be separated by either a space, tab or comma.

### Start each piece of logic with BEGIN

The `BEGIN` instruction is used at the beginning of a logic rung or procedure. It sets the logic accumulator to true, so that the following logic will operate correctly. The `END` instruction can be used at the end of a logic rung. This is not a compulsory instruction to end a logic rung, but helps with reading the code.

```
BEGIN
    TCE    rdi_Mains_Fail     ; If the mains fail input is on
    EC     tdi_Mains_Fail     ; turn the telemetry input on, otherwise off
END
```

## Name your points

In the initialisation section you would usually make extensive use of `EQU` to give a register a name, so you don't need a table of all the I/O assignments sitting next to you as you write the DLP. For example, to call `RDIN1` "rdi\_Pump1\_Fault":

```
EQU   RDIN1    rdi_Pump1_Fault
```

You can then use `rdi_Pump1_Fault` wherever you'd use `RDIN1`. `EQU` is a compiler directive. It is not sent in the DLP.

Good practice:

* **Don't use dashes** in names. It is advisable not to use dashes/hyphens, as this will cause errors in the compiler.
* **Don't use spaces.**
* **Don't start a name with a number.** It is best practice not to start a variable with a number. However, if you do require a numbered variable it can still be compiled.
* **Prefix each name with a 3 letter code** to indicate what type of IO register it is, such as `rdi_` or `tdi_`. This is not mandatory, but you will find that debugging a complex DLP becomes a lot easier when you can tell what IO type a variable is just by looking at it.
* **Name every point the RTU has.** It is recommended good practice to equate every piece of I/O that the RTU has, even if it is not being used in the DLP. This provides a fundamental level of documentation for the system so that it can be referred to in the future.

`EQULAB` names a label in the same way. See [Jumps and labels](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/instruction-reference/digital-logic.md#jumps-and-labels).

## Write clear code

* Accurate and adequate commenting in your DLP is important. It is particularly important for the next person who comes along after you and wonders what it was you were trying to achieve.
* Put jump labels in the very left hand column of the DLP. This is not necessary for the DLP to compile or function correctly, but it is an established convention that helps other programmers quickly locate and identify jump labels when inspecting the code.
* A gap can be left in the number list in a `NCOSMASK` or `COSMASK` statement, such as `NCOSMASK 1,2,3, ,5,6,7`. This is not mandatory. Leaving a placeholder makes it easier to tell at a glance which points have been intentionally left out.

## Find and replace

Use the ASM editor's search (**Ctrl + F**) functionality to find text. From V4.00, Search and Replace works for single or global actions.

## Reuse code with macros

1. Define the macro:

   ```
   MacroName  DelayInOut
   MacroDig   input,output        ; digital parameters
   MacroAnl   value               ; analogue parameters
   MacroTmr   1                   ; the macro uses one timer
   Timer      input, 1, value
   output := timerd1
   MacroEnd
   ```
2. Use it:

   ```
   Macro DelayInOut, rdin1, rdout1, 1 min
   ```

   or, the same result can be achieved with:

   ```
   DelayInOut(rdin1, rdout1, 1 min)
   ```

* **Timers in macros:** When timers are used in a macro, the compiler uses timer 128 first, then timer 127 and so on. During compilation, a warning message will be generated if the macro uses a timer that has been used in the DLP code. This allows the programmer to use timer 1 followed by timer 2 and so on.
* **Labels in macros:** `MacroLab` informs the compiler that the macro will use one or more labels.
* **Macro libraries:** Once a group of macros have been created they may be stored in a `*.LIB` file for future use. `MacroFile` tells the compiler to look at the file for macros. Several `*.LIB` files may be looked at from the DLP. If a folder is not specified, the `*.LIB` files must be in the same folder as the `*.ASM` file.
* **Inline macros:** `MacroDef` can be used to define an inline macro, with its parameters in brackets.

{% hint style="warning" %}
V4.05 fixes an issue that caused DLP programs using macros to not build and compile properly. The symptom was a build error “An internal exception of type ERangeError occurred with the message: Range check error”.
{% endhint %}

For the syntax of each macro instruction, see [Timers, logging, COS and macros](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/instruction-reference/timers-logging-macros.md#macros).

## Set the editor options

For fonts, colours, tabs and line numbers, see [Set the editor options](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/install.md#set-the-editor-options).

## Next steps

* [Compile and load a DLP into the RTU](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/compile-and-load.md)
* [DLP self-help course](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course.md)
