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

wip working on slices

parent 548c65d7
Branches
No related tags found
No related merge requests found
......@@ -94,6 +94,10 @@ fn main() {
let mut out = [false, false];
let out = &Signal::new(&mut out);
// let s = &(&*out)[0..3]; // .to_slice(0, 1);
let out0: Signal<1> = out.view(0, 1);
let out1: Signal<1> = out.view(1, 2);
// println!("s {:b}", &s);
let m = Mux {
ins: (Signal::new(&mut in1), Signal::new(&mut in2)),
......@@ -111,6 +115,10 @@ fn main() {
sel.store_slice(&[false]);
m.eval();
println!("m.out {:b}", m.out);
println!("out0 {:b}", &out0);
println!("out1 {:b}", &out1);
let p = &m.out[0..1];
let m: Signal<1> = m.out[0..2].into();
}
// #[derive(Debug)]
// struct Mux<'a, const I: usize, const S: usize, const N: usize> {
......
use std::cell::Cell;
use std::convert::{From, Into};
use std::fmt;
use std::ops::{BitAnd, BitOr, Deref, Index};
use std::ops::{BitAnd, BitOr, Deref, Index, Range};
use std::string::String;
#[derive(Debug, PartialEq, Clone)]
pub struct Signal<'a, const N: usize> {
// we should probably have set/get instead of exposing store
pub store: &'a [Cell<bool>],
}
......@@ -38,6 +37,13 @@ impl<'a, const N: usize> Signal<'a, N> {
v.set(a[i])
}
}
pub fn view<const M: usize>(&self, f: usize, t: usize) -> Signal<M> {
assert!(M == t - f);
Signal {
store: &self.store[f..t],
}
}
}
// impl<'a, const N: usize> From<u8> for Signal<'a, N> {
......@@ -50,6 +56,13 @@ impl<'a, const N: usize> Signal<'a, N> {
// }
// }
impl<'a, const N: usize> From<&'a [Cell<bool>]> for Signal<'a, N> {
fn from(slice: &'a [Cell<bool>]) -> Self {
assert!(N == slice.len());
Signal { store: slice }
}
}
impl<'a, const N: usize> Deref for Signal<'a, N> {
type Target = [Cell<bool>];
......@@ -68,16 +81,27 @@ impl<'a, const N: usize> fmt::Binary for &Signal<'a, N> {
}
}
impl<'a, const N: usize> Index<usize> for Signal<'a, N> {
type Output = Signal<'a, 1>;
// almost works
// impl<'a, const N: usize> Index<Range<usize>> for Signal<'a, N> {
// type Output = Signal<'a, N>;
#[inline]
fn index(&self, index: usize) -> Self::Output {
Signal {
store: &[self.store[index].clone()],
}
}
}
// fn index(&self, index: Range<usize>) -> &Signal<'a, N> {
// &Signal {
// store: &self.store[index.start..index.end],
// }
// }
// }
// impl<'a, const N: usize> Index<usize> for Signal<'a, N> {
// type Output = Signal<'a, 1>;
// #[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> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment