Document fait avec Nvu Document made with Nvu
acceuilnouveautesintroductioncoeurmini_systemepages_techniquesrevue_de_presselienscourriel



Technical pages
Conditional Interpretation/Compilation

Instructions
Use
Example

Principe

It is a question of having an instruction set allowing to interpret or of compiling a program or some according to conditions which can depend on the configuration of the material environmen of a system.

To save in memory space, it is sensible to compile, tha the specific instructions of peripherals used by a system. Le us take for example the case of the FORTH core, it is not necessary to arrange the VGA interface in an embeded application. It is enough then to indicate to the compiler to avoid creating the graphic instruction set if this interface is not present in the FPGA. One wins so the memory space corresponding to these instructions become then useless. This same memory space can then contain another specific instruction set of the application.

For it, it is necessary to have instructions which, on one hand, are feasible in interpretation mode AND in compilation mode and, on the other hand, do not consume supplementary resources. For example, it is necessary to be able to create variable one indicating the availability of the various peripherals withou this one is integrated into the code knowing result that it serves only for the making.

Everything this is taken into account in the following instruction set.


Instructions

n [IF] - instructions

- [THEN] -

The instructions between "[IF]" and "[THEN]" are interpreted/compiled if n is not 0

n [IF] - instructions 1

- [ELSE] - instructions 2

- [THEN] -

If n is not 0, only the instructions 1 are interpreted/compiled, else there are the instructions 2

- [LABEL] "nom" -

- [GOTO] "nom" -

"[GOTO] nom" forces the interprétation/compilation of the instructions wich are following "[LABEL] nom"

- [VARIABLE] "nom" adresse

"[VARIABLE]" creates, if it does not yet exist, and/or gives the address of the 32 bits variable "nom"


Use

If the use of [IF], [ ELSE] and [ THEN] instructions do not create difficulties for one accustomed by the language FORTH, it is necessary a little to detail [VARIABLE], [LABEL] and [ GOTO] instructions.

[VARIABLE] create a variable which will be available only during the interpretation/compilation of the program. This one will no be so integrated into the FORTH dictionary of the system.

[LABEL] indicates to the interpreter/compiler that following word is a label which will not be integrated into the FORTH dictionary.

[ GOTO] asks the interpreter/compiler to jump to the label ( following word) which has to meet itself following an instruction [LABEL].

Here is a small example of conditional interpretation:

ECHO" Debut du programme." CR

10 [VARIABLE] numero_boucle !

[LABEL] boucle
 [VARIABLE] numero_boucle @ 1- ?DUP
 [IF]
  DUP [VARIABLE] numero_boucle ! ECHO" Execution d'un rebouclage, numero_boucle = "  . CR [GOTO] boucle
 [ELSE]
  ECHO" Fin du programme." CR
 [THEN]

This example has to produce following result:

Debut du programme.
Execution d'un rebouclage, numero_boucle = 9
Execution d'un rebouclage, numero_boucle = 8
Execution d'un rebouclage, numero_boucle = 7
Execution d'un rebouclage, numero_boucle = 6
Execution d'un rebouclage, numero_boucle = 5
Execution d'un rebouclage, numero_boucle = 4
Execution d'un rebouclage, numero_boucle = 3
Execution d'un rebouclage, numero_boucle = 2
Execution d'un rebouclage, numero_boucle = 1
Fin du programme.
OK
>


Example

Here is an example of program containing the drivers of several peripherals which, through variable associated, will be integrated or not into the FORTH dictionary of the system:

( Exemple de structure de programme utilisant les instructions
  d'interpretation/compilation conditionnelle.
  Version 0.10 du 5 Decembre 2004.
  Ecrit par jpb.forth .
)

TELECHARGEMENT

( Declaration des peripheriques existants:
  ----------------------------------------
)

1 [VARIABLE] VGA !
0 [VARIABLE] I2C !
0 [VARIABLE] USB !
( ... )

( Saut en fin du programme si aucun peripherique n'est present:
  -------------------------------------------------------------
)

[VARIABLE] VGA @ [VARIABLE] I2C @ OR [VARIABLE] USB @ OR 0=
[IF]
 [GOTO] fin_du_programme
[THEN]

( Instructions d'acces aux differents peripheriques:
  --------------------------------------------------
)

[VARIABLE] VGA @
[IF]

ECHO" Installation des instructions du peripherique VGA.
"

: VGA_INIT
 ( ... )
;

: VGA_LIRE ( - --> n )
 ( ... ) 1234
;

: VGA_ECRIRE ( n --> - )
 ( ... ) DROP
;

[THEN]

[VARIABLE] I2C @
[IF]

ECHO" Installation des instructions du peripherique I2C.
"

: I2C_INIT
 ( ... )
;

: I2C_LIRE ( - --> n )
 ( ... ) 5678
;

: I2C_ECRIRE ( n --> - )
 ( ... ) DROP
;

[THEN]

[VARIABLE] USB @
[IF]

ECHO" Installation des instructions du peripherique USB.
"

: USB_INIT
 ( ... )
;

: USB_LIRE ( - --> n )
 ( ... ) 3456
;

: USB_ECRIRE ( n --> - )
 ( ... ) DROP
;

[THEN]

( Instructions de test des differents peripheriques:
  --------------------------------------------------
)

[VARIABLE] VGA @
[IF]

: VGA_TEST ( - --> 0 si OK )
 ." Test du peripherique VGA en cours..." CR
 ( ... ) 0
;

[THEN]

[VARIABLE] I2C @
[IF]

: I2C_TEST ( - --> 0 si OK )
 ." Test du peripherique I2C en cours..." CR
 ( ... ) 0
;

[THEN]

[VARIABLE] USB @
[IF]

: USB_TEST ( - --> 0 si OK )
 ." Test du peripherique USB en cours..." CR
 ( ... ) 0
;

[THEN]

( Sequence de test des differents peripheriques:
  ----------------------------------------------
)

[VARIABLE] VGA @
[IF]
 VGA_TEST
 [IF]
  ECHO" Le peripherique VGA est en PANNE!" CR
 [ELSE]
  ECHO" Le peripherique VGA est operationnel." CR
 [THEN]
[THEN]

[VARIABLE] I2C @
[IF]
 I2C_TEST
 [IF]
  ECHO" Le peripherique I2C est en PANNE!" CR
 [ELSE]
  ECHO" Le peripherique I2C est operationnel." CR
 [THEN]
[THEN]

[VARIABLE] USB @
[IF]
 USB_TEST
 [IF]
  ECHO" Le peripherique USB est en PANNE!" CR
 [ELSE]
  ECHO" Le peripherique USB est operationnel." CR
 [THEN]
[THEN]

( Retrait des programmes de test devenus inutiles:
  ------------------------------------------------
)

FIND VGA_TEST
[IF]
 FORGET VGA_TEST
[ELSE]
 FIND I2C_TEST
 [IF]
  FORGET I2C_TEST
 [ELSE]
  FORGET USB_TEST
 [THEN]
[THEN]

[LABEL] fin_du_programme

( FIN
)

The behavior of this example can be modified with the values of variable VGA, I2C and USB.