You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
292 lines
7.5 KiB
292 lines
7.5 KiB
2 years ago
|
//! Wires and diodes
|
||
|
|
||
|
use super::*;
|
||
|
|
||
|
#[derive(Clone, Debug)]
|
||
|
pub struct Wire(Orientation);
|
||
|
|
||
2 years ago
|
impl Wire {
|
||
|
pub fn new(orientation: Orientation) -> Self {
|
||
|
Self(orientation)
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
impl Tile for Wire {
|
||
2 years ago
|
fn update<'b>(&'b mut self, mut context: UpdateContext<'b>) {
|
||
|
if let Some(signal) = context.take_signal() {
|
||
|
for &direction in self.0.into_directions() {
|
||
|
if direction == signal.direction().opposite() {
|
||
|
continue;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
if let Some(pos) = context.accepts_direction(direction) {
|
||
|
context.send(pos, signal.clone_move(direction));
|
||
2 years ago
|
}
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
if context.state() != State::Idle {
|
||
|
context.next_state();
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
|
fn accepts_signal(&self, direction: Direction) -> bool {
|
||
|
self.0.contains(direction)
|
||
|
}
|
||
2 years ago
|
|
||
|
fn draw(&self, x: usize, y: usize, state: State, surface: &mut TextSurface) {
|
||
|
let ch = match self.0 {
|
||
|
Orientation::Horizontal => '-',
|
||
|
Orientation::Vertical => '|',
|
||
2 years ago
|
Orientation::Any => '+',
|
||
2 years ago
|
};
|
||
|
|
||
|
surface.set(x, y, TextChar::from_state(ch, state));
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
|
#[derive(Clone, Debug)]
|
||
|
pub struct Diode(Direction);
|
||
|
|
||
2 years ago
|
impl Diode {
|
||
|
pub fn new(direction: Direction) -> Self {
|
||
|
Self(direction)
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
impl Tile for Diode {
|
||
2 years ago
|
fn update<'b>(&'b mut self, mut context: UpdateContext<'b>) {
|
||
|
if let Some(signal) = context.take_signal() {
|
||
|
// Block signals coming from where the diode is looking
|
||
|
if signal.direction().opposite() == self.0 {
|
||
|
return;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
if let Some(pos) = context.accepts_direction(self.0) {
|
||
|
context.send(pos, signal.moved(self.0));
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
if context.state() != State::Idle {
|
||
|
context.next_state();
|
||
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
fn draw(&self, x: usize, y: usize, state: State, surface: &mut TextSurface) {
|
||
|
let ch = match self.0 {
|
||
|
Direction::Up => '^',
|
||
|
Direction::Down => 'v',
|
||
|
Direction::Left => '<',
|
||
|
Direction::Right => '>',
|
||
|
};
|
||
|
|
||
|
surface.set(x, y, TextChar::from_state(ch, state));
|
||
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
#[derive(Clone, Debug)]
|
||
|
pub struct Resistor {
|
||
|
direction: Direction,
|
||
|
signal: Option<Signal>,
|
||
|
}
|
||
|
|
||
|
impl Resistor {
|
||
|
pub fn new(direction: Direction) -> Self {
|
||
|
Self {
|
||
|
direction,
|
||
2 years ago
|
signal: None,
|
||
2 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Tile for Resistor {
|
||
|
fn update<'b>(&'b mut self, mut context: UpdateContext<'b>) {
|
||
|
if let Some(signal) = std::mem::take(&mut self.signal) {
|
||
2 years ago
|
if let Some(pos) = context.accepts_direction(self.direction) {
|
||
|
context.send(pos, signal.moved(self.direction));
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if let Some(signal) = context.take_signal() {
|
||
|
self.signal = Some(signal);
|
||
|
context.set_state(State::Active);
|
||
|
} else {
|
||
|
if context.state() != State::Idle {
|
||
|
context.next_state();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
fn draw(&self, x: usize, y: usize, state: State, surface: &mut TextSurface) {
|
||
|
let ch = match self.direction {
|
||
2 years ago
|
Direction::Up => '\u{219f}', // Upwards Two Headed Arrow
|
||
|
Direction::Down => '\u{21a1}', // Downwards Two Headed Arrow
|
||
|
Direction::Left => '\u{219e}', // Leftwards Two Headed Arrow
|
||
2 years ago
|
Direction::Right => '\u{21a0}', // Rightwards Two Headed Arrow
|
||
|
};
|
||
|
|
||
|
surface.set(x, y, TextChar::from_state(ch, state));
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
#[cfg(test)]
|
||
|
mod test {
|
||
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
|
fn test_wire_transmit() {
|
||
|
use crate::Orientation::*;
|
||
|
|
||
2 years ago
|
let mut pane = test_tile_setup!(
|
||
|
3,
|
||
|
2,
|
||
|
[
|
||
|
Wire::new(Horizontal),
|
||
|
Wire::new(Any),
|
||
|
Wire::new(Horizontal),
|
||
|
(),
|
||
|
Wire::new(Vertical),
|
||
|
()
|
||
|
]
|
||
|
);
|
||
2 years ago
|
|
||
|
// Test the signal going from left to right
|
||
|
test_set_signal!(pane, (0, 0), Direction::Right);
|
||
|
|
||
2 years ago
|
pane.step();
|
||
2 years ago
|
|
||
2 years ago
|
println!("{:#?}", pane);
|
||
|
|
||
2 years ago
|
assert_signal!(pane, (1, 0));
|
||
|
assert_no_signal!(pane, (0, 0));
|
||
|
assert_no_signal!(pane, (2, 0));
|
||
|
assert_no_signal!(pane, (1, 1));
|
||
|
|
||
2 years ago
|
pane.step();
|
||
2 years ago
|
|
||
|
assert_signal!(pane, (2, 0));
|
||
|
assert_signal!(pane, (1, 1));
|
||
|
assert_no_signal!(pane, (0, 0));
|
||
|
assert_no_signal!(pane, (1, 0));
|
||
|
|
||
2 years ago
|
pane.step();
|
||
2 years ago
|
for (_, _, tile) in pane.tiles() {
|
||
2 years ago
|
assert!(tile.signal().is_none());
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
// Let the simulation cool down
|
||
2 years ago
|
pane.step();
|
||
|
pane.step();
|
||
2 years ago
|
|
||
2 years ago
|
// Test the signal going from right to left
|
||
|
test_set_signal!(pane, (2, 0), Direction::Left);
|
||
|
|
||
2 years ago
|
pane.step();
|
||
2 years ago
|
|
||
|
assert_signal!(pane, (1, 0));
|
||
|
assert_no_signal!(pane, (0, 0));
|
||
|
assert_no_signal!(pane, (2, 0));
|
||
|
assert_no_signal!(pane, (1, 1));
|
||
|
|
||
2 years ago
|
pane.step();
|
||
2 years ago
|
|
||
|
assert_signal!(pane, (0, 0));
|
||
|
assert_signal!(pane, (1, 1));
|
||
|
assert_no_signal!(pane, (2, 0));
|
||
|
assert_no_signal!(pane, (1, 0));
|
||
|
}
|
||
2 years ago
|
|
||
|
#[test]
|
||
|
fn test_diode_transmit() {
|
||
|
use crate::Direction::*;
|
||
|
|
||
2 years ago
|
let mut pane = test_tile_setup!(
|
||
|
3,
|
||
|
2,
|
||
|
[
|
||
|
Diode::new(Right),
|
||
|
Diode::new(Right),
|
||
|
Diode::new(Down),
|
||
|
(),
|
||
|
Diode::new(Up),
|
||
|
Diode::new(Left)
|
||
|
]
|
||
|
);
|
||
2 years ago
|
|
||
|
// Test the signal going from left to right
|
||
|
test_set_signal!(pane, (0, 0), Direction::Right);
|
||
|
|
||
|
pane.step();
|
||
|
assert_signal!(pane, (1, 0));
|
||
|
assert_no_signal!(pane, (0, 0));
|
||
|
|
||
2 years ago
|
let positions = [(2, 0), (2, 1), (1, 1), (1, 0)];
|
||
2 years ago
|
|
||
|
for &pos in positions.iter().cycle().take(16) {
|
||
|
pane.step();
|
||
2 years ago
|
println!("{:#?}", pane);
|
||
2 years ago
|
assert_signal!(pane, pos);
|
||
|
for &pos2 in positions.iter() {
|
||
|
if pos == pos2 {
|
||
2 years ago
|
continue;
|
||
2 years ago
|
}
|
||
|
assert_no_signal!(pane, pos2);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
#[test]
|
||
|
fn test_resistor_transmit() {
|
||
|
use crate::Direction::*;
|
||
|
|
||
2 years ago
|
let mut pane = test_tile_setup!(
|
||
|
4,
|
||
|
1,
|
||
|
[
|
||
|
Diode::new(Right),
|
||
|
Resistor::new(Right),
|
||
|
Resistor::new(Right),
|
||
|
Diode::new(Right)
|
||
|
]
|
||
|
);
|
||
2 years ago
|
|
||
|
test_set_signal!(pane, (0, 0), Direction::Right);
|
||
|
|
||
|
pane.step();
|
||
|
assert_no_signal!(pane, (0, 0));
|
||
|
assert_signal!(pane, (1, 0));
|
||
|
assert_no_signal!(pane, (2, 0));
|
||
|
assert_no_signal!(pane, (3, 0));
|
||
|
|
||
|
pane.step();
|
||
|
assert_no_signal!(pane, (0, 0));
|
||
|
assert_no_signal!(pane, (1, 0));
|
||
|
assert_no_signal!(pane, (2, 0));
|
||
|
assert_no_signal!(pane, (3, 0));
|
||
|
|
||
|
pane.step();
|
||
|
assert_no_signal!(pane, (0, 0));
|
||
|
assert_no_signal!(pane, (1, 0));
|
||
|
assert_signal!(pane, (2, 0));
|
||
|
assert_no_signal!(pane, (3, 0));
|
||
|
|
||
|
pane.step();
|
||
|
assert_no_signal!(pane, (0, 0));
|
||
|
assert_no_signal!(pane, (1, 0));
|
||
|
assert_no_signal!(pane, (2, 0));
|
||
|
assert_no_signal!(pane, (3, 0));
|
||
|
|
||
|
pane.step();
|
||
|
assert_no_signal!(pane, (0, 0));
|
||
|
assert_no_signal!(pane, (1, 0));
|
||
|
assert_no_signal!(pane, (2, 0));
|
||
|
assert_signal!(pane, (3, 0));
|
||
|
|
||
|
pane.step();
|
||
|
assert_no_signal!(pane, (0, 0));
|
||
|
assert_no_signal!(pane, (1, 0));
|
||
|
assert_no_signal!(pane, (2, 0));
|
||
|
assert_no_signal!(pane, (3, 0));
|
||
|
}
|
||
2 years ago
|
}
|