Comment coder une machine d'états
Bonjour,
je code régulièrement des machines d'états en matériel (en VHDL, je suis développeur matériel) et je dois faire la même chose en C. J'utilise ce code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| #include <stdio.h>
#include "system.h"
int main()
{
int *pmem;
int i=0, j=0, k=0, trans=0;
int current_state=0, next_state=0;
char* tdc(int);
printf("Start!\n");
for (i=0; i<40; i++)
{
pmem=(int *)(ONCHIP_MEMORY2_0_BASE+i);
trans = (*pmem>>1)&1;
printf("i = %u; trans = %u\n", i, trans);
switch(current_state){
case 0 :
if (trans==0)
next_state=8;
else
next_state=0;
case 8 :
if (trans==0)
next_state=8;
else
next_state=1;
case 1 :
if (trans==0)
next_state=2;
else
next_state=9;
case 2 :
if (trans==0)
next_state=3;
else
next_state=4;
case 3 :
if (trans==0)
next_state=3;
else
next_state=4;
case 4 :
if (trans==0)
next_state=5;
else
next_state=7;
case 5 :
if (trans==0)
next_state=5;
else
next_state=6;
case 6 :
if (trans==0)
next_state=3;
else
next_state=7;
case 7 :
if (trans==0)
next_state=8;
else
next_state=1;
case 9 :
if (trans==0)
next_state=10;
else
next_state=0;
case 10 :
if (trans==0)
next_state=11;
else
next_state=12;
case 11 :
if (trans==0)
next_state=11;
else
next_state=12;
case 12 :
if (trans==0)
next_state=13;
else
next_state=15;
case 13 :
if (trans==0)
next_state=13;
else
next_state=14;
case 14 :
if (trans==0)
next_state=11;
else
next_state=15;
default :
if (trans==0)
next_state=8;
else
next_state=1;
}
current_state=next_state;
if (trans==0)
k++;
else
j++;
}
printf("k final = %u 1 et %u 0 sur %u échantillons.\n", j, k, i);
return 0;
} |
Or ça ne marche. J'observe entre autres des transitions de l'état 1 à l'état 8, ce qui ne devrait pas exister. Il est donc clair que j'ai pas écrit ça comme il faudrait et je ne sais pas utiliser le debugger donc je n'arrive pas à savoir où est le problème.
Quelqu'un peut m'aider ?
Merci.