#include #include #include #include #include "ssam.h" ssam_cnct_t *rw; ssam_cnct_t *data; ssam_cnct_t *addr; ssam_cnct_t *ack; ssam_cnct_t *s1_s2_sum; ssam_cnct_t *s1_s2_diff; ssam_cnct_t *s2_s3_mul; ssam_cnct_t *s2_s3_quot; ssam_cnct_t *s3_out; #define MEM_SIZE 5 enum memops { MEM_R, MEM_W }; /* MEM */ void* mem() { int tab[MEM_SIZE] = {2, 3, 5, 6, 7}; unsigned int op=-1; unsigned int at; unsigned int res; for(;;ssam_wait()) { res = 0; ssam_read(rw, &op); switch( op ) { case MEM_R: /* Lire dans la mémoire */ ssam_read(addr, &at); #ifdef DEBUG printf("MEM: Read @%d (=%d)!\n", at, tab[at]); #endif ssam_write(data, &tab[at]); res = 1; break; case MEM_W: /* Ecrire dans la memoire */ ssam_read(addr, &at); ssam_read(data, &tab[at]); #ifdef DEBUG printf("MEM: Write %d@%d!\n", tab[at], at); #endif res = 1; break; default: break; } ssam_write(ack, &res); } return NULL; } /* Retourne la valeur à l'adresse donnée */ int get_addr(unsigned int at) { int res; unsigned int my_ack=0; unsigned int op = MEM_R; ssam_write(rw, &op); ssam_write(addr, &at); do { ssam_wait(); ssam_read(ack, &my_ack); } while ( my_ack != 1 ); ssam_read(data, &res); return res; } /* Ecrit la valeur à l'adresse donnée */ void set_addr(unsigned int at, int my_data) { unsigned int my_ack=0; unsigned int op = MEM_W; ssam_write(addr, &at); ssam_write(data, &my_data); ssam_write(rw, &op); do { ssam_wait(); ssam_read(ack, &my_ack); } while ( my_ack != 1 ); } /* Compute sum and diff */ void* stage1() { int i; for(i=0;;i+=2) { int a, b, sum, diff; a = get_addr(i%MEM_SIZE); b = get_addr((i+1)%MEM_SIZE); sum = a + b; diff = a - b; #ifdef DEBUG printf("Stage1 (%d, %d) => (%d, %d)\n", a, b, sum, diff); #endif ssam_write(s1_s2_sum, &sum); ssam_write(s1_s2_diff, &diff); } } void* stage2() { /* Demarrage en cascade */ ssam_wait_duration(3); for(;;) { int a, b, mul, quot; ssam_read(s1_s2_sum, &a); ssam_read(s1_s2_diff, &b); mul = a * b; if ( b == 0 ) quot = 0; else quot = a / b; #ifdef DEBUG printf("Stage2 (%d, %d) => (%d, %d)\n", a, b, mul, quot); #endif ssam_write(s2_s3_mul, &mul); ssam_write(s2_s3_quot, "); ssam_wait(); } return NULL; } void* stage3() { /* Demarrage en cascade */ ssam_wait_duration(4); for(;;) { int a, b, c; ssam_read(s2_s3_mul, &a); ssam_read(s2_s3_quot, &b); c = (a>0 && b>0)? pow(a, b) : 0.; #ifdef DEBUG printf("Stage3 (%d, %d) => %d\n", a, b, c); #endif ssam_write(s3_out, &c); ssam_wait(); } return NULL; } int main() { ssam_init(4); ssam_create_component(mem); ssam_create_component(stage1); ssam_create_component(stage2); ssam_create_component(stage3); rw = ssam_create_connection("rw", SSAM_BOOL_T); data = ssam_create_connection("data", SSAM_UINT_T); addr = ssam_create_connection("addr", SSAM_UINT_T); ack = ssam_create_connection("ack", SSAM_BOOL_T); s1_s2_sum = ssam_create_connection("s1_s2_sum", SSAM_UINT_T); s1_s2_diff = ssam_create_connection("s1_s2_diff", SSAM_UINT_T); s2_s3_mul = ssam_create_connection("s2_s3_mul", SSAM_UINT_T); s2_s3_quot = ssam_create_connection("s2_s3_quot", SSAM_UINT_T); s3_out = ssam_create_connection("s3_out", SSAM_UINT_T); ssam_start(100, "pipelog"); return ssam_exit(); }