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

sort of wip

parent 55ef00d5
No related branches found
No related tags found
No related merge requests found
......@@ -65,58 +65,124 @@ use rsim::*;
// file.write_all(out.as_bytes()).unwrap();
// }
#[derive(Debug)]
struct Mux<'a, const I: usize, const S: usize, const N: usize> {
inputs: [&'a Signal<N>; I],
sel: &'a Signal<S>,
output: Signal<N>,
}
impl<'a, const I: usize, const S: usize, const N: usize> Eval for Mux<'a, I, S, N> {
fn eval(&self) {
let s: u8 = self.sel.clone().into();
println!("s {}", s);
use std::cell::Cell;
let o = self.inputs[s as usize];
#[derive(Debug, Clone)]
struct S<'a, const N: usize> {
i: &'a [Cell<bool>],
}
self.output.store.set(o.store.get())
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(),
}
}
}
impl<'a, const I: usize, const S: usize, const N: usize> fmt::Binary for Mux<'a, I, S, N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Binary::fmt(&self.output, f)
struct Mux<'a, const N: usize> {
ins: (S<'a, N>, S<'a, N>),
sel: &'a S<'a, 1>,
out: &'a S<'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()),
}
}
}
fn main() {
let s1: Signal<8> = 0b10101u8.into();
println!("s1 {:?}", s1);
println!("s1 {:b}", s1);
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 sel = [false];
let sel = S::new(&mut sel);
let mut out = [false];
let out = &S::new(&mut out);
let m = Mux {
ins: (S::new(&mut in1), S::new(&mut in2)),
sel: &sel,
out,
};
let s2: Signal<8> = 0b00110u8.into();
println!("s2 {:b}", s2);
println!("m {:?}", m.out);
m.eval();
sel.i[0].set(true);
m.eval();
println!("m {:?}", m.out);
}
// #[derive(Debug)]
// struct Mux<'a, const I: usize, const S: usize, const N: usize> {
// inputs: [&'a Signal<N>; I],
// sel: &'a Signal<S>,
// output: Signal<N>,
// }
let s3 = &s1 & &s2;
// impl<'a, const I: usize, const S: usize, const N: usize> Eval for Mux<'a, I, S, N> {
// fn eval(&self) {
// let s: u8 = self.sel.clone().into();
// println!("s {}", s);
println!("s3 {:b}", s3);
// let o = self.inputs[s as usize];
let sel: Signal<1> = [true].into();
// self.output.store.set(o.store.get())
// }
// }
let m: Mux<2, 1, 8> = Mux {
inputs: [&s1, &s2],
sel: &sel,
output: 0u8.into(),
};
// impl<'a, const I: usize, const S: usize, const N: usize> fmt::Binary for Mux<'a, I, S, N> {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// fmt::Binary::fmt(&self.output, f)
// }
// }
println!("m {:?}", m);
// fn main() {
// let s1: Signal<8> = 0b10101u8.into();
// // println!("s1 {:?}", s1);
// println!("s1 {:b}", s1);
println!("m {:b}", m);
// let s2: Signal<8> = 0b00110u8.into();
// println!("s2 {:b}", s2);
m.eval();
println!("m {:?}", m);
println!("m {:b}", m);
}
// let s3 = &s1 & &s2;
// println!("s3 {:b}", s3);
// let mut sel: Signal<1> = [true].into();
// let m: Mux<2, 1, 8> = Mux {
// inputs: [&s1, &s2],
// sel: &sel,
// output: 0u8.into(),
// };
// // println!("m {:?}", m);
// println!("m {:b}", m);
// m.eval();
// println!("eval", m);
// println!("m {:b}", m);
// }
// #[derive(Debug)]
// struct AndIn<'a> {
......
......@@ -18,54 +18,54 @@ impl<const N: usize> Signal<N> {
}
// pub fn set(&mut self, i: usize, b: bool) {
// self.0[i] = b;
// self.store[i] = b;
// }
}
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' });
}
write!(f, "{}", s)
}
}
// 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' });
// }
// write!(f, "{}", s)
// }
// }
// impl<const N: usize> Deref for Signal<N> {
// type Target = [bool; N];
// type Target = [Cell<bool>; N];
// fn deref(&self) -> Self::Target {
// self.store.get()
// (&self.store).as_slice_of_cells()
// }
// }
// impl Index<usize> for Signal {
// impl<const N: usize> Index<usize> for Signal<N> {
// type Output = bool;
// #[inline]
// fn index(&self, index: usize) -> &Self::Output {
// &self.0[index]
// fn index(self, index: usize) -> Self::Output {
// self.store.get()[index]
// }
// }
// operations on signal
impl<const N: usize> BitAnd for &Signal<N> {
type Output = Signal<N>;
// rhs is the "right-hand side" of the expression `a & b`
fn bitand(self, rhs: Self) -> Self::Output {
let mut bs = [false; N];
let a = self.store.get();
let b = rhs.store.get();
for i in 0..self.store.get().len() {
bs[i] = a[i] & b[i]
}
Signal {
store: Cell::new(bs),
}
}
}
// impl<const N: usize> BitAnd for &Signal<N> {
// type Output = Signal<N>;
// // rhs is the "right-hand side" of the expression `a & b`
// fn bitand(self, rhs: Self) -> Self::Output {
// let mut bs = [false; N];
// let a = self.store.get();
// let b = rhs.store.get();
// for i in 0..self.store.get().len() {
// bs[i] = a[i] & b[i]
// }
// Signal {
// store: Cell::new(bs),
// }
// }
// }
// impl<const N: usize> BitOr for &Signal<N> {
// type Output = Signal<N>;
......@@ -80,39 +80,39 @@ impl<const N: usize> BitAnd for &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<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 {
store: Cell::new(a),
}
}
}
// impl<const N: usize> From<[bool; N]> for Signal<N> {
// fn from(a: [bool; N]) -> Self {
// Signal {
// store: Cell::new(a),
// }
// }
// }
impl<const N: usize> From<Signal<N>> for u8 {
fn from(s: Signal<N>) -> u8 {
// check that it fits u8
assert!(N < 8);
let mut u = 0;
let a = s.store.get();
// impl<const N: usize> From<Signal<N>> for u8 {
// fn from(s: Signal<N>) -> u8 {
// // check that it fits u8
// assert!(N < 8);
// let mut u = 0;
// let a = s.store.get();
for i in 0..N {
u |= if a[i] { 1 << i } else { 0 }
}
u
}
}
// for i in 0..N {
// u |= if a[i] { 1 << i } else { 0 }
// }
// u
// }
// }
// impl From<u32> for Signal {
// fn from(u: u32) -> Self {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment