serial clock @ 8khz

This commit is contained in:
Alex Janka 2023-03-16 18:42:23 +11:00
parent 7fb8b06ff9
commit 8dc01b59c2

View file

@ -5,7 +5,7 @@ use std::{
use serde::{Deserialize, Serialize};
use crate::util::get_bit;
use crate::{constants::CLOCK_SPEED, util::get_bit};
#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq)]
enum ClockSource {
@ -58,6 +58,7 @@ struct InputByte {
}
const BYTE_DELAY: usize = 2000;
const CLOCK_DIV: usize = CLOCK_SPEED / 8192;
impl InputByte {
fn advance(&mut self, rx: &Option<Receiver<u8>>) -> u8 {
@ -104,6 +105,7 @@ pub struct Serial {
bits_remaining: u8,
control: SerialControl,
target: SerialTarget,
clock_inc: usize,
}
#[derive(Serialize, Deserialize)]
@ -113,6 +115,7 @@ pub struct SerialSaveState {
input_byte: InputByte,
bits_remaining: u8,
control: SerialControl,
clock_inc: usize,
}
impl SerialSaveState {
@ -123,6 +126,7 @@ impl SerialSaveState {
input_byte: serial.input_byte,
bits_remaining: serial.bits_remaining,
control: serial.control,
clock_inc: serial.clock_inc,
}
}
}
@ -136,6 +140,7 @@ impl Serial {
bits_remaining: 7,
control: SerialControl::default(),
target,
clock_inc: 0,
}
}
@ -147,6 +152,7 @@ impl Serial {
bits_remaining: state.bits_remaining,
control: state.control,
target,
clock_inc: state.clock_inc,
}
}
@ -165,6 +171,9 @@ impl Serial {
&None
};
for _ in 0..steps {
self.clock_inc += 1;
if self.clock_inc >= CLOCK_DIV {
self.clock_inc %= CLOCK_DIV;
if ime {
self.input_byte.input_delay = self.input_byte.input_delay.saturating_sub(1);
}
@ -198,6 +207,7 @@ impl Serial {
}
}
}
}
will_interrupt
}