performance improvement

This commit is contained in:
Mwa
2026-03-20 11:08:18 +01:00
parent c72e133cde
commit ff6427b020
2 changed files with 297 additions and 282 deletions

View File

@@ -396,7 +396,7 @@ impl Computer {
new new
} }
#[inline(always)] #[inline(always)]
pub fn step(&mut self) { pub fn step(&mut self, s: usize) {
match self.interupts { match self.interupts {
InteruptState::Disabled => {} InteruptState::Disabled => {}
InteruptState::Enabled => { InteruptState::Enabled => {
@@ -415,7 +415,7 @@ impl Computer {
} }
InteruptState::Serving(..) => {} InteruptState::Serving(..) => {}
} }
for _ in 0..s {
//potentially just changed by interupt. //potentially just changed by interupt.
let next_opcode = self.ram[self.pc]; let next_opcode = self.ram[self.pc];
@@ -541,23 +541,33 @@ impl Computer {
} else { } else {
match addr as isize - 0x0120_0000 { match addr as isize - 0x0120_0000 {
#[cfg(feature = "rich_keyboard")] #[cfg(feature = "rich_keyboard")]
-12 => { -12 => SHARED.keyboard[0]
SHARED.keyboard[0].load(std::sync::atomic::Ordering::Relaxed) .load(std::sync::atomic::Ordering::Relaxed),
}
#[cfg(feature = "rich_keyboard")] #[cfg(feature = "rich_keyboard")]
-8 => SHARED.keyboard[1].load(std::sync::atomic::Ordering::Relaxed), -8 => SHARED.keyboard[1]
.load(std::sync::atomic::Ordering::Relaxed),
#[cfg(feature = "rich_keyboard")] #[cfg(feature = "rich_keyboard")]
-4 => SHARED.keyboard[2].load(std::sync::atomic::Ordering::Relaxed), -4 => SHARED.keyboard[2]
0 => SHARED.keyboard[3].load(std::sync::atomic::Ordering::Relaxed), .load(std::sync::atomic::Ordering::Relaxed),
0 => SHARED.keyboard[3]
.load(std::sync::atomic::Ordering::Relaxed),
4 => time::Instant::now() 4 => time::Instant::now()
.duration_since(self.creation) .duration_since(self.creation)
.as_millis() as u32, .as_millis()
as u32,
8 => SHARED.mouse[0].load(std::sync::atomic::Ordering::Relaxed), 8 => SHARED.mouse[0].load(std::sync::atomic::Ordering::Relaxed),
12 => SHARED.mouse[1].load(std::sync::atomic::Ordering::Relaxed), 12 => {
16 => SHARED.mouse[2].load(std::sync::atomic::Ordering::Relaxed), SHARED.mouse[1].load(std::sync::atomic::Ordering::Relaxed)
}
16 => {
SHARED.mouse[2].load(std::sync::atomic::Ordering::Relaxed)
}
//guaranted by the inequality and is multiple of 4 //guaranted by the inequality and is multiple of 4
_ => { _ => {
self.serve_interupt(InteruptKind::IllegalOpcode, [next_opcode]); self.serve_interupt(
InteruptKind::IllegalOpcode,
[next_opcode],
);
return; return;
} }
} }
@@ -652,7 +662,8 @@ impl Computer {
self.interupts = InteruptState::Enabled self.interupts = InteruptState::Enabled
} }
InteruptKind::Swi => {} InteruptKind::Swi => {}
InteruptKind::DivByZero | InteruptKind::UnsupportedOpcode => { InteruptKind::DivByZero
| InteruptKind::UnsupportedOpcode => {
ret_index = Some(self.regs[0]); ret_index = Some(self.regs[0]);
ret_value = self.regs[1]; ret_value = self.regs[1];
} }
@@ -691,7 +702,8 @@ impl Computer {
(&SHARED.external_enabled_interupts) (&SHARED.external_enabled_interupts)
.store(0, std::sync::atomic::Ordering::Relaxed); .store(0, std::sync::atomic::Ordering::Relaxed);
(&SHARED.external_interupts).store(0, std::sync::atomic::Ordering::Relaxed); (&SHARED.external_interupts)
.store(0, std::sync::atomic::Ordering::Relaxed);
SHARED.external_interupts.signal(); SHARED.external_interupts.signal();
} }
Instruction::Swi() => { Instruction::Swi() => {
@@ -716,14 +728,17 @@ impl Computer {
Err((kind, rx, ry, op2, opcode)) => { Err((kind, rx, ry, op2, opcode)) => {
self.pc += 1; self.pc += 1;
match kind { match kind {
InteruptKind::UnsupportedOpcode => self InteruptKind::UnsupportedOpcode => self.serve_interupt(
.serve_interupt(kind, [rx.0.into(), self[ry], self.resolve(op2), opcode]), kind,
[rx.0.into(), self[ry], self.resolve(op2), opcode],
),
InteruptKind::IllegalOpcode => self.serve_interupt(kind, [next_opcode]), InteruptKind::IllegalOpcode => self.serve_interupt(kind, [next_opcode]),
_ => unsafe { unreachable_unchecked() }, _ => unsafe { unreachable_unchecked() },
} }
} }
} }
} }
}
fn resolve(&self, op2: Op2) -> u32 { fn resolve(&self, op2: Op2) -> u32 {
match op2 { match op2 {
Op2::Direct(v) => v, Op2::Direct(v) => v,

View File

@@ -303,7 +303,7 @@ fn main() -> Result<(), Error> {
let mut simulation = Computer::new(program); let mut simulation = Computer::new(program);
#[cfg(not(feature = "debug"))] #[cfg(not(feature = "debug"))]
loop { loop {
simulation.step(); simulation.step(64);
} }
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
{ {
@@ -371,13 +371,13 @@ fn main() -> Result<(), Error> {
println!("cannot step, cpu killed"); println!("cannot step, cpu killed");
break; break;
} }
simulation.step(); simulation.step(1);
} }
false false
} }
"r" | "run" => { "r" | "run" => {
while !simulation.error { while !simulation.error {
simulation.step(); simulation.step(64);
} }
false false
} }
@@ -408,7 +408,7 @@ fn main() -> Result<(), Error> {
while !simulation.error while !simulation.error
&& simulation.ram[simulation.pc] != 0x8800_0000 && simulation.ram[simulation.pc] != 0x8800_0000
{ {
simulation.step(); simulation.step(1);
} }
false false
} }
@@ -419,7 +419,7 @@ fn main() -> Result<(), Error> {
while !simulation.error while !simulation.error
&& simulation.pc != (v as usize / 4) && simulation.pc != (v as usize / 4)
{ {
simulation.step(); simulation.step(1);
} }
false false
} }