66// SPDX-License-Identifier: Apache-2.0
77
88use core:: panic:: PanicInfo ;
9+ use itertools:: izip;
910
1011use bittide_hal:: manual_additions:: timer:: Duration ;
1112use bittide_hal:: manual_additions:: timer:: Instant ;
@@ -37,19 +38,6 @@ fn main() -> ! {
3738 uwriteln ! ( uart, "Starting sync out generator.." ) . unwrap ( ) ;
3839 sync_out_generator. set_active ( true ) ;
3940
40- uwriteln ! ( uart, "Waiting for at least one pulse.." ) . unwrap ( ) ;
41- while freeze. number_of_sync_pulses_seen ( ) == 0 {
42- // TOOD: Memory map sync pulse counter -- no need to involve freeze here
43- freeze. set_freeze ( ( ) ) ;
44- }
45-
46- // XXX: CPU is booted after all links are available, so we can blindly enable
47- // all counters.
48- uwriteln ! ( uart, "Starting domain diff counters.." ) . unwrap ( ) ;
49- for i in 0 ..DomainDiffCounters :: ENABLE_LEN {
50- domain_diff_counters. set_enable ( i, true ) ;
51- }
52-
5341 uwriteln ! ( uart, "Starting clock control.." ) . unwrap ( ) ;
5442 let mut callisto = Callisto :: new ( cc. config ( ) . callisto ) ;
5543
@@ -67,16 +55,34 @@ fn main() -> ! {
6755 let mut prev_all_stable = false ;
6856
6957 loop {
70- // Do clock control on "frozen" counters
58+ // Store frozen elastic buffer counters
7159 freeze. set_freeze ( ( ) ) ;
72- cc. set_change_speed ( callisto. update ( & cc, freeze. eb_counters_volatile_iter ( ) ) ) ;
60+
61+ // Do clock control update
62+ cc. set_change_speed (
63+ callisto. update (
64+ & cc,
65+ izip ! (
66+ 0 ..DomainDiffCounters :: ENABLE_LEN ,
67+ freeze. eb_counters_volatile_iter( )
68+ )
69+ . map ( |( i, counter) | {
70+ if domain_diff_counters. enable ( i) . unwrap_or ( false ) {
71+ Some ( counter)
72+ } else {
73+ None
74+ }
75+ } ) ,
76+ ) ,
77+ ) ;
7378
7479 // Detect stability
7580 let stability = stability_detector. update ( & cc, timer. now ( ) ) ;
7681
7782 // Store debug information. Stop capturing samples if we are stable to
7883 // reduce plot sizes.
79- if !prev_all_stable {
84+ let has_sense_of_global_time = freeze. number_of_sync_pulses_seen ( ) != 0 ;
85+ if !prev_all_stable && has_sense_of_global_time {
8086 sample_store. store ( & freeze, stability, callisto. accumulated_speed_requests ) ;
8187 }
8288
@@ -90,6 +96,13 @@ fn main() -> ! {
9096 }
9197 prev_all_stable = all_stable;
9298
99+ // Update active domain difference counters based on which links are
100+ // enabled.
101+ let link_mask_rev = cc. link_mask_rev ( ) ;
102+ for i in 0 ..DomainDiffCounters :: ENABLE_LEN {
103+ domain_diff_counters. set_enable ( i, test_bit ( link_mask_rev, i) ) ;
104+ }
105+
93106 // Wait for next update
94107 let timer_result = timer. wait_until_stall ( next_update) ;
95108 panic_on_missed_deadline ( & mut uart, & timer, next_update, timer_result) ;
@@ -115,6 +128,11 @@ fn panic_on_missed_deadline(
115128 } ;
116129}
117130
131+ /// Test whether the `i`-th bit in `bv` is set.
132+ fn test_bit ( bv : u8 , i : usize ) -> bool {
133+ ( bv & ( 1 << i) ) != 0
134+ }
135+
118136#[ panic_handler]
119137fn panic_handler ( _info : & PanicInfo ) -> ! {
120138 loop {
0 commit comments