December 05 Cours Microinformatique I Microinformatique I Instructions DSP et calcul pour traitement de signal Francesco Mondada Laboratoire de systèmes autonomes I2S - STI - EPFL http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 1 December 05 Microinformatique I Introduction Les dsPIC disposent de ressources de calcul très intéressantes, qui visent le calcul temps réel de signaux digitaux. Ces ressources ne sont pas exploitées directement pas le compilateur. Dans ce cadre il est important de comprendre: • Quelles instructions permettent un gain de temps/mémoire • Dans quel cadre de calcul elles sont utilisées • Comment les intégrer avec du code “généraliste” en C. http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 2 December 05 Microinformatique I Introduction Sujets de ce cours: • L’instruction MAC et le cadres d’utilisation DSP • registres / variables utilisés et leur gestion • Le cadre d’utilisation typique de cette instruction Filtrage http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 3 December 05 Microinformatique I Motivation Filtrage numérique (FIR Finite Impulse Response): b1 + b1 + b1 + b1 + Z-1 Z-1 Z-1 Z-1 b1 Cours MicroInfo I http://asl.epfl.ch Autonomous Systems Lab 4 December 05 Microinformatique I Instruction MAC http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 5 December 05 Microinformatique I Exemple: MAC W4*W7, A, [W8]+=2, W4, [W10]+=2, W7, [W13]+=2 W4 A W7 x + A Mémoire W8 W4 Adresse 2 W8 + W8 http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 6 December 05 Microinformatique I Exemple: MAC W4*W7, A, [W8]+=2, W4, [W10]+=2, W7, [W13]+=2 Mémoire W10 W7 Adresse 2 W10 + W10 http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 7 December 05 Microinformatique I Exemple: MAC W4*W7, A, [W8]+=2, W4, [W10]+=2, W7, [W13]+=2 Mémoire W13 B Adresse 2 W13 + W13 http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 8 December 05 Microinformatique I Exemple: MAC W4*W7, A, [W8]+=2, W4, [W10]+=2, W7 Mémoire W8 Adresse x + A W10 Adresse http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 9 December 05 Microinformatique I Exemple: MAC W4*W7, A, [W8]+=2, W4, [W10]+=2, W7 Mémoire W8 Adresse x + A W10 Adresse http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 10 December 05 Microinformatique I Exemple: MAC W4*W7, A, [W8]+=2, W4, [W10]+=2, W7 Mémoire W8 Adresse x W10 + A Adresse http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 11 December 05 Microinformatique I Utilisation de l’instruction MAC Filtre FIR: signal x x x x x x x x + http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 12 December 05 Microinformatique I Utilisation de l’instruction MAC Filtre FIR: signal x x x x x x x x + http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 13 December 05 Microinformatique I Utilisation de l’instruction MAC Filtre FIR: signal x x x x x x x x + http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 14 December 05 Microinformatique I Utilisation de l’instruction MAC Filtre FIR: signal x x x x x x x x Signal filtré + http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 15 December 05 Microinformatique I Instruction MPY http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 16 December 05 Microinformatique I Instruction EDAC http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 17 December 05 Microinformatique I Instruction REPEAT http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 18 December 05 Microinformatique I Instruction DO http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 19 December 05 Microinformatique I Implémentation: #define BUFFLEN 20 #define INOUTLEN 60 unsigned int input[INOUTLEN] __attribute__((space(xmemory), aligned(32))); unsigned int coefficients[BUFFLEN] __attribute__((space(ymemory), aligned(32))); unsigned int output[INOUTLEN]; /* is NOT a circular buffer */ int main( void ) { int i, in_out_index; CORCON |= 0x0001; /* Enable integer arithmetic */ /* Initialize in out and coefficients */ for(i = 0; i < BUFFLEN; i++) { coefficients[i] = 2; } for(i = 0; i < INOUTLEN; i++) { input[i] = i; output[i] = 0; } /* do some filtering */ for(in_out_index = 0; in_out_index < (INOUTLEN-BUFFLEN); in_out_index++) { output[in_out_index] = dot_prod( input+in_out_index, coefficients, BUFFLEN-1 ); } } Cours MicroInfo I http://asl.epfl.ch Autonomous Systems Lab 20 December 05 Microinformatique I Implémentation: unsigned int dot_prod( unsigned int *samples, unsigned int *coeff, unsigned int len) { int i; long out = 0; } for(i=0;i<len+1;i++) { out+=samples[i]*coeff[i]; } return (unsigned int)out; Compilé sans optimisation: 554 cycles par appel de dot_prod Compilé avec optimisation: 227 cycles par appel de dot_prod http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 21 December 05 Microinformatique I Implémentation: .text .global _dot_prod _dot_prod: ; If any of the registers W8 - W15 are used, they should be saved ; W0 - W7 may be used without saving PUSH W8 PUSH W10 ; The 2 pointers were passed in W0 and W1 when function was called ; Transfer pointers to appropriate registers for MAC MOV W0, W8 ; Initializing X pointer MOV W1, W10 ; Initializing Y pointer ; Clear Accumulator and prefetch 1st pair of numbers CLR A, [W8]+=2, W4, [W10]+=2, W7 ; perform product - addition REPEAT W2 MAC W4*W7, A, [W8]+=2, W4, [W10]+=2, W7 ; Return lower 16 bits of A MOV ACCAL, W0 POP POP W10 W8 RETURN Ecrit en assembleur avec MAC: 45 cycles par appel de dot_prod http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 22 December 05 Microinformatique I TP de cette semaine Instructions MAC, REPEAT etc Comparaison de temps d’exécution http://asl.epfl.ch Autonomous Systems Lab Cours MicroInfo I 23