Skip to content

Commit 6f7b4ab

Browse files
committed
添加内部时钟
1 parent 23805e4 commit 6f7b4ab

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

src/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ pub struct Config {
2020
#[arg(short, long, default_value_t = false)]
2121
pub debug: bool,
2222

23-
/// 是否开启外部时钟
23+
/// 是否开启外部时钟(若不开启外部时钟
24+
/// 则使用周期为4ms的内部时钟)
2425
#[arg(short, long, default_value_t = false)]
2526
pub external_clock: bool,
2627
}

src/debug.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn core_hack(cmd: &mut Vec<&str>, debug_ports: &mut Vec<SharedPointer<VdbApi>>)
110110
result.push_str(&format!("scp\t: {:#016x}\n", regs.scp));
111111
result.push_str(&format!("imsg\t: {:#016x}\n", regs.imsg));
112112
result.push_str(&format!("ipdump\t: {:#016x}\n", regs.ipdump));
113-
result.push_str(&format!("flagdump: {:#016x}\n", regs.flagdump));
113+
result.push_str(&format!("flagdump: {:#016x}", regs.flagdump));
114114
result
115115
}
116116
VdbApi::NotRunning => format!("Core{} is not running.", core_id),
@@ -214,11 +214,11 @@ fn core_hack(cmd: &mut Vec<&str>, debug_ports: &mut Vec<SharedPointer<VdbApi>>)
214214
215215
options:
216216
regs <core_id> Print all registers.
217+
write <core_id> <register> <value> Write value to register.
217218
amount Print core amount.
218219
start <core_id> Start core<core_id>.
219220
dbgmod <core_id> [none|step] Set debugger mode.
220221
cont <core_id> Run next instruction when in step mode.
221-
write <core_id> <register> <value> Write value to register.
222222
inst <core_id> Print the id of the instruction which will be executed.
223223
help Print this text."
224224
.to_string(),
@@ -270,6 +270,7 @@ fn memory_hack(cmd: &mut Vec<&str>, memory: &mut Memory) -> String {
270270
result.push_str("\n");
271271
addr += 16;
272272
}
273+
result.pop();
273274
result
274275
}
275276
}

src/lib.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ use std::{
1717
use config::Config;
1818
use debug::VdbApi;
1919
use memory::Memory;
20-
use nix::unistd;
20+
use nix::{
21+
libc::{timeval, timezone},
22+
unistd,
23+
};
2124
use utils::shared::SharedPointer;
2225
use 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

Comments
 (0)