serial clock kinda sux tbh... put it behind a feature flag

This commit is contained in:
Alex Janka 2023-03-17 10:42:27 +11:00
parent 8dc01b59c2
commit 1e01308605
2 changed files with 46 additions and 34 deletions

View file

@ -6,6 +6,7 @@ edition = "2021"
[features]
default = []
async = []
clocked-serial = []
[dependencies]
rand = "0.8.5"

View file

@ -1,11 +1,12 @@
use crate::util::get_bit;
use serde::{Deserialize, Serialize};
use std::{
io::{stdout, Write},
sync::mpsc::{Receiver, Sender},
};
use serde::{Deserialize, Serialize};
use crate::{constants::CLOCK_SPEED, util::get_bit};
#[cfg(feature = "clocked-serial")]
use crate::constants::CLOCK_SPEED;
#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq)]
enum ClockSource {
@ -58,6 +59,7 @@ struct InputByte {
}
const BYTE_DELAY: usize = 2000;
#[cfg(feature = "clocked-serial")]
const CLOCK_DIV: usize = CLOCK_SPEED / 8192;
impl InputByte {
@ -105,6 +107,7 @@ pub struct Serial {
bits_remaining: u8,
control: SerialControl,
target: SerialTarget,
#[cfg(feature = "clocked-serial")]
clock_inc: usize,
}
@ -115,6 +118,7 @@ pub struct SerialSaveState {
input_byte: InputByte,
bits_remaining: u8,
control: SerialControl,
#[cfg(feature = "clocked-serial")]
clock_inc: usize,
}
@ -126,6 +130,7 @@ impl SerialSaveState {
input_byte: serial.input_byte,
bits_remaining: serial.bits_remaining,
control: serial.control,
#[cfg(feature = "clocked-serial")]
clock_inc: serial.clock_inc,
}
}
@ -140,6 +145,7 @@ impl Serial {
bits_remaining: 7,
control: SerialControl::default(),
target,
#[cfg(feature = "clocked-serial")]
clock_inc: 0,
}
}
@ -152,6 +158,7 @@ impl Serial {
bits_remaining: state.bits_remaining,
control: state.control,
target,
#[cfg(feature = "clocked-serial")]
clock_inc: state.clock_inc,
}
}
@ -171,9 +178,14 @@ impl Serial {
&None
};
for _ in 0..steps {
#[cfg(feature = "clocked-serial")]
{
self.clock_inc += 1;
if self.clock_inc >= CLOCK_DIV {
if self.clock_inc < CLOCK_DIV {
continue;
}
self.clock_inc %= CLOCK_DIV;
}
if ime {
self.input_byte.input_delay = self.input_byte.input_delay.saturating_sub(1);
}
@ -207,7 +219,6 @@ impl Serial {
}
}
}
}
will_interrupt
}