Skip to content
Snippets Groups Projects
Commit 548c65d7 authored by Per Lindgren's avatar Per Lindgren
Browse files

wip (broken)

parent c5d9a4d7
Branches
No related tags found
No related merge requests found
......@@ -67,68 +67,50 @@ use rsim::*;
use std::cell::Cell;
#[derive(Debug, Clone)]
struct S<'a, const N: usize> {
i: &'a [Cell<bool>],
}
impl<'a, const N: usize> S<'a, N> {
fn new(a: &'a mut [bool; N]) -> Self {
let cell_slice: &'a Cell<[bool]> = Cell::from_mut(a);
Self {
i: cell_slice.as_slice_of_cells(),
}
}
}
// two input mux
struct Mux<'a, const N: usize> {
ins: (S<'a, N>, S<'a, N>),
sel: &'a S<'a, 1>,
out: &'a S<'a, N>,
ins: (Signal<'a, N>, Signal<'a, N>),
sel: &'a Signal<'a, 1>,
out: &'a Signal<'a, N>,
}
impl<'a, const N: usize> Eval for Mux<'a, N> {
fn eval(&self) -> () {
match self.sel.i[0].get() {
false => self.out.i[0].set(self.ins.0.i[0].get()),
true => self.out.i[0].set(self.ins.1.i[0].get()),
let sel = self.sel[0].get();
for (i, o) in self.out.iter().enumerate() {
o.set(match sel {
false => self.ins.0[i].get(),
true => self.ins.1[i].get(),
})
}
}
}
fn main() {
let slice = &mut [false, false];
let cell_slice: &Cell<[bool]> = Cell::from_mut(slice);
let slice_cell: &[Cell<bool>] = cell_slice.as_slice_of_cells();
println!("{}", slice_cell[0].get());
slice_cell[0].set(true);
println!("{}", slice_cell[0].get());
let mut sd = [true];
let s = S::new(&mut sd);
println!("{}", s.i[0].get());
s.i[0].set(false);
println!("{}", s.i[0].get());
let mut in1 = [false];
let mut in2 = [true];
let mut in1 = [false, true];
let mut in2 = [true, false];
let mut sel = [false];
let sel = S::new(&mut sel);
let mut out = [false];
let sel = Signal::new(&mut sel);
let mut out = [false, false];
let out = &S::new(&mut out);
let out = &Signal::new(&mut out);
let m = Mux {
ins: (S::new(&mut in1), S::new(&mut in2)),
ins: (Signal::new(&mut in1), Signal::new(&mut in2)),
sel: &sel,
out,
};
println!("m {:?}", m.out);
println!("m.out {:b}", m.out);
m.eval();
sel[0].set(true);
m.eval();
sel.i[0].set(true);
println!("m.out {:b}", m.out);
// sel[0].set(false);
sel.store([false]);
sel.store_slice(&[false]);
m.eval();
println!("m {:?}", m.out);
println!("m.out {:b}", m.out);
}
// #[derive(Debug)]
// struct Mux<'a, const I: usize, const S: usize, const N: usize> {
......
......@@ -5,49 +5,79 @@ use std::ops::{BitAnd, BitOr, Deref, Index};
use std::string::String;
#[derive(Debug, PartialEq, Clone)]
pub struct Signal<const N: usize> {
pub struct Signal<'a, const N: usize> {
// we should probably have set/get instead of exposing store
pub store: Cell<[bool; N]>,
pub store: &'a [Cell<bool>],
}
impl<const N: usize> Signal<N> {
pub fn new() -> Self {
impl<'a, const N: usize> Signal<'a, N> {
pub fn new(arr: &'a mut [bool; N]) -> Self {
let cell_slice: &'a Cell<[bool]> = Cell::from_mut(arr);
Self {
store: Cell::new([false; N]),
store: cell_slice.as_slice_of_cells(),
}
}
// pub fn set(&mut self, i: usize, b: bool) {
// self.store[i] = b;
// }
pub fn load(&self) -> [bool; N] {
let mut a = [false; N];
for (i, v) in self.store.iter().enumerate() {
a[i] = v.get()
}
a
}
pub fn store(&self, a: [bool; N]) {
for (i, v) in self.store.iter().enumerate() {
v.set(a[i])
}
}
pub fn store_slice(&self, a: &[bool]) {
for (i, v) in self.store.iter().enumerate() {
v.set(a[i])
}
}
}
// impl<const N: usize> fmt::Binary for Signal<N> {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// let mut s = String::new();
// for i in self.store.get().iter().rev() {
// s.push(if *i { '1' } else { '0' });
// impl<'a, const N: usize> From<u8> for Signal<'a, N> {
// fn from(u: u8) -> Self {
// let mut bs = [false; N];
// for i in 0..8 {
// bs[i] = 1 << i != 0
// }
// write!(f, "{}", s)
// Signal::new(&mut bs)
// }
// }
// impl<const N: usize> Deref for Signal<N> {
// type Target = [Cell<bool>; N];
impl<'a, const N: usize> Deref for Signal<'a, N> {
type Target = [Cell<bool>];
// fn deref(&self) -> Self::Target {
// (&self.store).as_slice_of_cells()
// }
// }
fn deref(&self) -> &Self::Target {
self.store
}
}
impl<'a, const N: usize> fmt::Binary for &Signal<'a, N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = String::new();
for i in self.store.iter().rev() {
s.push(if i.get() { '1' } else { '0' });
}
write!(f, "{}", s)
}
}
// impl<const N: usize> Index<usize> for Signal<N> {
// type Output = bool;
impl<'a, const N: usize> Index<usize> for Signal<'a, N> {
type Output = Signal<'a, 1>;
// #[inline]
// fn index(self, index: usize) -> Self::Output {
// self.store.get()[index]
// }
// }
#[inline]
fn index(&self, index: usize) -> Self::Output {
Signal {
store: &[self.store[index].clone()],
}
}
}
// operations on signal
// impl<const N: usize> BitAnd for &Signal<N> {
......@@ -80,18 +110,6 @@ impl<const N: usize> Signal<N> {
// }
// }
// impl<const N: usize> From<u8> for Signal<N> {
// fn from(u: u8) -> Self {
// let mut bs = [false; N];
// for i in 0..8 {
// bs[i] = u & (1 << i) != 0
// }
// Signal {
// store: Cell::new(bs),
// }
// }
// }
// impl<const N: usize> From<[bool; N]> for Signal<N> {
// fn from(a: [bool; N]) -> Self {
// Signal {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment