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() { ...@@ -94,6 +94,10 @@ fn main() {
let mut out = [false, false]; let mut out = [false, false];
let out = &Signal::new(&mut out); 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 { let m = Mux {
ins: (Signal::new(&mut in1), Signal::new(&mut in2)), ins: (Signal::new(&mut in1), Signal::new(&mut in2)),
...@@ -111,6 +115,10 @@ fn main() { ...@@ -111,6 +115,10 @@ fn main() {
sel.store_slice(&[false]); sel.store_slice(&[false]);
m.eval(); m.eval();
println!("m.out {:b}", m.out); 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)] // #[derive(Debug)]
// struct Mux<'a, const I: usize, const S: usize, const N: usize> { // struct Mux<'a, const I: usize, const S: usize, const N: usize> {
......
use std::cell::Cell; use std::cell::Cell;
use std::convert::{From, Into}; use std::convert::{From, Into};
use std::fmt; use std::fmt;
use std::ops::{BitAnd, BitOr, Deref, Index}; use std::ops::{BitAnd, BitOr, Deref, Index, Range};
use std::string::String; use std::string::String;
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub struct Signal<'a, const N: usize> { pub struct Signal<'a, const N: usize> {
// we should probably have set/get instead of exposing store
pub store: &'a [Cell<bool>], pub store: &'a [Cell<bool>],
} }
...@@ -38,6 +37,13 @@ impl<'a, const N: usize> Signal<'a, N> { ...@@ -38,6 +37,13 @@ impl<'a, const N: usize> Signal<'a, N> {
v.set(a[i]) 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> { // impl<'a, const N: usize> From<u8> for Signal<'a, N> {
...@@ -50,6 +56,13 @@ impl<'a, const N: usize> 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> { impl<'a, const N: usize> Deref for Signal<'a, N> {
type Target = [Cell<bool>]; type Target = [Cell<bool>];
...@@ -68,16 +81,27 @@ impl<'a, const N: usize> fmt::Binary for &Signal<'a, N> { ...@@ -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> { // almost works
type Output = Signal<'a, 1>; // impl<'a, const N: usize> Index<Range<usize>> for Signal<'a, N> {
// type Output = Signal<'a, N>;
#[inline] // fn index(&self, index: Range<usize>) -> &Signal<'a, N> {
fn index(&self, index: usize) -> Self::Output { // &Signal {
Signal { // store: &self.store[index.start..index.end],
store: &[self.store[index].clone()], // }
} // }
} // }
}
// 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 // operations on signal
// impl<const N: usize> BitAnd for &Signal<N> { // 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