Skip to content

Instantly share code, notes, and snippets.

@nmarley
Last active April 23, 2022 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmarley/230bc7911957298c809d02f142424269 to your computer and use it in GitHub Desktop.
Save nmarley/230bc7911957298c809d02f142424269 to your computer and use it in GitHub Desktop.
Example of sending interrupt using Rust mpsc channel
use std::sync::mpsc::{channel, Sender};
pub struct PPU {
// data...
cycles: u8,
nmi_channel: Sender<bool>,
}
impl PPU {
pub fn new(chan: Sender<bool>) -> Self {
PPU {
cycles: 0,
nmi_channel: chan,
}
}
pub fn fire(&self) {
self.nmi_channel.send(true).unwrap();
}
pub fn tick(&mut self) {
self.cycles += 1;
// send an interrupt every 3 cycles...
if self.cycles % 3 == 0 {
self.fire();
}
}
}
fn main() {
let (tx, rx) = channel();
let mut ppu = PPU::new(tx);
for _ in 1..=10 {
ppu.tick();
match rx.try_recv() {
Err(err) => println!("... no such luck: {}", err),
Ok(_) => println!("Got an interrupt from PPU!"),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment