1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
use std::fmt::Debug;

use crate::core::ast::VerilogLiteral;
use crate::core::bits::{Bit, Bits};
use crate::core::clock::Clock;
use crate::core::signed::Signed;
use crate::core::type_descriptor::{TypeDescriptor, TypeKind};

#[derive(Clone, PartialEq, Debug)]
pub enum VCDValue {
    Single(vcd::Value),
    Vector(Vec<vcd::Value>),
    String(String),
    Composite(Vec<Box<VCDValue>>),
}

impl From<bool> for VCDValue {
    fn from(x: bool) -> Self {
        if x {
            VCDValue::Single(vcd::Value::V1)
        } else {
            VCDValue::Single(vcd::Value::V0)
        }
    }
}

pub trait Synth: Default + Copy + PartialEq + Debug {
    const BITS: usize;
    fn descriptor() -> TypeDescriptor;
    fn vcd(self) -> VCDValue;
    fn verilog(self) -> VerilogLiteral;
}

impl<const N: usize> Synth for Bits<N> {
    const BITS: usize = N;

    fn descriptor() -> TypeDescriptor {
        TypeDescriptor {
            name: format!("Bits::<{}>", Self::BITS),
            kind: TypeKind::Bits(Self::BITS)
        }
    }

    fn vcd(self) -> VCDValue {
        self.into()
    }

    fn verilog(self) -> VerilogLiteral {
        self.into()
    }
}

impl Synth for Bit {
    const BITS: usize = 1;

    fn descriptor() -> TypeDescriptor {
        TypeDescriptor {
            name: "Bit".to_string(),
            kind: TypeKind::Bits(1)
        }
    }

    fn vcd(self) -> VCDValue {
        if self {
            VCDValue::Single(vcd::Value::V1)
        } else {
            VCDValue::Single(vcd::Value::V0)
        }
    }

    fn verilog(self) -> VerilogLiteral {
        self.into()
    }
}

impl Synth for Clock {
    const BITS: usize = 1;

    fn descriptor() -> TypeDescriptor {
        TypeDescriptor {
            name: "clock".to_string(),
            kind: TypeKind::Bits(1)
        }
    }

    fn vcd(self) -> VCDValue {
        self.0.into()
    }

    fn verilog(self) -> VerilogLiteral {
        self.0.into()
    }
}

impl<const N: usize> Synth for Signed<N> {
    const BITS: usize = N;
    fn descriptor() -> TypeDescriptor {
        TypeDescriptor {
            name: format!("Signed::<{}>", Self::BITS),
            kind: TypeKind::Signed(Self::BITS),
        }
    }
    fn vcd(self) -> VCDValue {
        self.inner().vcd()
    }
    fn verilog(self) -> VerilogLiteral {
        self.inner().into()
    }
}