1+ #include "intN_t.h"
2+ #include "uintN_t.h"
3+
4+ #define N_THREADS 10
5+
6+ // Each thread is providing these values to the increment handler module simultaneously
7+ uint32_t increment_input_value ;
8+ uint32_t increment_input_tid ;
9+ // And expects an output of the sum after the thread's addition (same cycle)
10+ uint32_t increment_output_sum ;
11+ // A 'thread'/FSM instance definition trying to increment some accumulator this clock cycle
12+ uint32_t incrementer_thread (uint8_t tid )
13+ {
14+ while (1 )
15+ {
16+ increment_input_value = tid + 1 ;
17+ increment_input_tid = tid ;
18+ // increment_output_sum ready same cycle
19+ printf ("Thread %d trying to increment by %d, got new total %d.\n" ,
20+ tid , increment_input_value , increment_output_sum );
21+ __out (increment_output_sum ); // "return" that continues
22+ //return increment_output_sum;
23+ }
24+ }
25+ // Derive FSM from above function
26+ #include "incrementer_thread_FSM.h"
27+
28+ // The module doing the incrementing/accumulating
29+ // sees an array of values, one from each thread
30+ uint32_t increment_input_values [N_THREADS ];
31+ uint32_t increment_input_tids [N_THREADS ];
32+ #pragma INST_ARRAY increment_input_value increment_input_values
33+ #pragma INST_ARRAY increment_input_tid increment_input_tids
34+ // And drives output totals back to each thread as an array
35+ uint32_t increment_output_sums [N_THREADS ];
36+ #pragma INST_ARRAY increment_output_sum increment_output_sums
37+ #pragma MAIN increment_handler
38+ void increment_handler ()
39+ {
40+ // Accumulator reg
41+ static uint32_t total ;
42+
43+ // In one cycle accumulate from each thread with chained adders
44+ uint32_t i ;
45+ for (i = 0 ; i < N_THREADS ; i += 1 )
46+ {
47+ increment_output_sums [i ] = total + increment_input_values [i ];
48+ printf ("Thread %d incrementing total %d by %d -> new total %d.\n" ,
49+ increment_input_tids [i ], total , increment_input_values [i ], increment_output_sums [i ]);
50+ total = increment_output_sums [i ]; // accumulate
51+ }
52+
53+ return total ;
54+ }
55+
56+ // Top level wrapper for N parallel instances of incrementer_thread
57+
58+ // Connect FSM outputs to top level output
59+ // So doesnt synthesize away
60+ typedef struct n_thread_outputs_t
61+ {
62+ incrementer_thread_OUTPUT_t data [N_THREADS ];
63+ }n_thread_outputs_t ;
64+ #pragma MAIN main
65+ n_thread_outputs_t main ()
66+ {
67+ // Registers keeping time count and knowning when done
68+ static uint32_t clk_cycle_count ;
69+ printf ("Clock Cycle %d:\n" , clk_cycle_count );
70+
71+ // N parallel instances of incrementer_thread
72+ uint8_t tid ;
73+ incrementer_thread_INPUT_t inputs [N_THREADS ];
74+ n_thread_outputs_t outputs ;
75+ for (tid = 0 ; tid < N_THREADS ; tid += 1 )
76+ {
77+ inputs [tid ].tid = tid ;
78+ inputs [tid ].input_valid = 1 ;
79+ inputs [tid ].output_ready = 1 ;
80+ outputs .data [tid ] = incrementer_thread_FSM (inputs [tid ]);
81+ //if(outputs.data[tid].input_ready)
82+ //{
83+ // printf("Clock# %d: Thread %d starting.\n",
84+ // clk_cycle_count, tid);
85+ //}
86+ //if(outputs.data[tid].output_valid)
87+ //{
88+ // printf("Clock# %d: Thread %d output value = %d.\n",
89+ // clk_cycle_count, tid, outputs.data[tid].return_output);
90+ //}
91+ }
92+
93+ // Func occuring each clk cycle
94+ clk_cycle_count += 1 ;
95+
96+ // Wire up outputs
97+ return outputs ;
98+ }
0 commit comments