@@ -17,7 +17,10 @@ use std::{
1717use config:: Config ;
1818use debug:: VdbApi ;
1919use memory:: Memory ;
20- use nix:: unistd;
20+ use nix:: {
21+ libc:: { timeval, timezone} ,
22+ unistd,
23+ } ;
2124use utils:: shared:: SharedPointer ;
2225use vrisc:: vcore:: { DebugMode , InterruptId , Vcore } ;
2326
@@ -61,7 +64,13 @@ pub fn run(config: Config) {
6164 match unsafe { unistd:: fork ( ) . unwrap ( ) } {
6265 unistd:: ForkResult :: Parent { child } => cores. push ( child) ,
6366 unistd:: ForkResult :: Child => {
64- vcore ( config. memory , i, config. cores , config. debug ) ;
67+ vcore (
68+ config. memory ,
69+ i,
70+ config. cores ,
71+ config. debug ,
72+ config. external_clock ,
73+ ) ;
6574 exit ( 0 ) ;
6675 }
6776 }
@@ -107,7 +116,7 @@ pub fn run(config: Config) {
107116 }
108117}
109118
110- fn vcore ( memory_size : usize , id : usize , total_core : usize , debug : bool ) {
119+ fn vcore ( memory_size : usize , id : usize , total_core : usize , debug : bool , external_clock : bool ) {
111120 let mut core_startflg =
112121 SharedPointer :: < bool > :: bind ( format ! ( "VcoreCore{}StartFlg" , id) , 1 ) . unwrap ( ) ;
113122 // 指令计数,计算从a开始运行到现在此核心共运行了多少条指令
@@ -166,9 +175,30 @@ fn vcore(memory_size: usize, id: usize, total_core: usize, debug: bool) {
166175
167176 let mut crossed_page = false ;
168177
178+ let mut tv_clock = timeval {
179+ tv_sec : 0 ,
180+ tv_usec : 0 ,
181+ } ;
182+ let mut usec_buf = 0 ;
183+ let mut clock_count = 0u64 ;
184+
169185 core_instruction_count. write ( 0 , u64:: MAX ) ;
170186
171187 loop {
188+ // 内部时钟
189+ if !debug && !external_clock {
190+ unsafe { nix:: libc:: gettimeofday ( & mut tv_clock, 0 as * mut timezone ) } ;
191+ if tv_clock. tv_usec + tv_clock. tv_sec * 1000_000 - usec_buf >= 4000 {
192+ core. intctler . interrupt ( InterruptId :: Clock ) ;
193+ usec_buf = tv_clock. tv_usec + tv_clock. tv_sec * 1000_000 ;
194+ clock_count += 1 ;
195+ }
196+ if clock_count == 250 {
197+ println ! ( "." ) ;
198+ clock_count = 0 ;
199+ }
200+ }
201+
172202 // 检测中断
173203 if let Some ( intid) = core. intctler . interrupted ( ) {
174204 core. interrupt_jump ( intid) ;
0 commit comments