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

signal in separate module

parent e861cd1f
No related branches found
No related tags found
No related merge requests found
use std::convert::{From, Into};
use std::fmt;
use std::ops::{BitAnd, Index};
use std::string::String;
#[derive(Debug, PartialEq, Clone)]
pub struct Signal(Vec<bool>);
impl Signal {
pub fn new() -> Self {
Self { 0: Vec::new() }
}
pub fn new_zeroed(n: usize) -> Self {
let mut bs = Signal::new();
for i in 0..n {
bs.push(false)
}
bs
}
pub fn push(&mut self, b: bool) {
self.0.push(b)
}
pub fn pop(&mut self) -> Option<bool> {
self.0.pop()
}
pub fn set(&mut self, i: usize, b: bool) {
self.0[i] = b;
}
}
impl fmt::Binary for Signal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = String::new();
for i in self.0.iter().rev() {
s.push(if *i { '1' } else { '0' });
}
write!(f, "{}", s)
}
}
impl Index<usize> for Signal {
type Output = bool;
#[inline]
fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}
// operations on signal
impl BitAnd for &Signal {
type Output = Signal;
// rhs is the "right-hand side" of the expression `a & b`
fn bitand(self, rhs: Self) -> Self::Output {
let mut bs = Signal::new();
for (a, b) in self.0.iter().zip(&rhs.0) {
bs.push(a & b);
}
bs
}
}
impl From<u8> for Signal {
fn from(u: u8) -> Self {
let mut bs = Signal::new();
for i in 0..8 {
bs.push(u & (1 << i) != 0)
}
bs
}
}
impl From<Signal> for u8 {
fn from(b: Signal) -> u8 {
let mut u = 0;
for (n, b) in b.0.iter().enumerate() {
u |= if *b { 1 << n } else { 0 }
}
u
}
}
impl From<u32> for Signal {
fn from(u: u32) -> Self {
let mut bs = Signal::new();
for i in 0..32 {
bs.push(u & (1 << i) != 0)
}
bs
}
}
impl From<Signal> for u32 {
fn from(b: Signal) -> u32 {
let mut u = 0;
for (n, b) in b.0.iter().enumerate() {
u |= if *b { 1 << n } else { 0 }
}
u
}
}
#[test]
fn test_partial_eq() {
let b: Signal = 12345u32.into();
let c: Signal = 1245u32.into();
assert!(b != c);
assert!(b == b);
}
#[test]
fn test_and() {
let a = 0x15u8;
let b = 0x51u8;
let a_bs: Signal = a.into();
let b_bs: Signal = b.into();
let c_bs = &a_bs & &b_bs;
assert!(a & b == c_bs.into());
}
mod signal;
pub use signal::*;
use std::convert::{From, Into};
use std::fmt;
use std::ops::{BitAnd, Index};
use std::string::String;
#[derive(Debug, PartialEq, Clone)]
pub struct Signal(Vec<bool>);
impl Signal {
pub fn new() -> Self {
Self { 0: Vec::new() }
}
pub fn new_zeroed(n: usize) -> Self {
let mut bs = Signal::new();
for i in 0..n {
bs.push(false)
}
bs
}
pub fn push(&mut self, b: bool) {
self.0.push(b)
}
pub fn pop(&mut self) -> Option<bool> {
self.0.pop()
}
pub fn set(&mut self, i: usize, b: bool) {
self.0[i] = b;
}
}
impl fmt::Binary for Signal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = String::new();
for i in self.0.iter().rev() {
s.push(if *i { '1' } else { '0' });
}
write!(f, "{}", s)
}
}
impl Index<usize> for Signal {
type Output = bool;
#[inline]
fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}
// operations on signal
impl BitAnd for &Signal {
type Output = Signal;
// rhs is the "right-hand side" of the expression `a & b`
fn bitand(self, rhs: Self) -> Self::Output {
let mut bs = Signal::new();
for (a, b) in self.0.iter().zip(&rhs.0) {
bs.push(a & b);
}
bs
}
}
impl From<u8> for Signal {
fn from(u: u8) -> Self {
let mut bs = Signal::new();
for i in 0..8 {
bs.push(u & (1 << i) != 0)
}
bs
}
}
impl From<Signal> for u8 {
fn from(b: Signal) -> u8 {
let mut u = 0;
for (n, b) in b.0.iter().enumerate() {
u |= if *b { 1 << n } else { 0 }
}
u
}
}
impl From<u32> for Signal {
fn from(u: u32) -> Self {
let mut bs = Signal::new();
for i in 0..32 {
bs.push(u & (1 << i) != 0)
}
bs
}
}
impl From<Signal> for u32 {
fn from(b: Signal) -> u32 {
let mut u = 0;
for (n, b) in b.0.iter().enumerate() {
u |= if *b { 1 << n } else { 0 }
}
u
}
}
#[test]
fn test_partial_eq() {
let b: Signal = 12345u32.into();
let c: Signal = 1245u32.into();
assert!(b != c);
assert!(b == b);
}
#[test]
fn test_and() {
let a = 0x15u8;
let b = 0x51u8;
let a_bs: Signal = a.into();
let b_bs: Signal = b.into();
let c_bs = &a_bs & &b_bs;
assert!(a & b == c_bs.into());
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment