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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::element::Element;
use crate::vhdl::keywords::*;
use crate::vhdl::design_unit::DesignUnit;
use crate::vhdl::block_declarative_item::BlockDeclarativeItem;
use crate::vhdl::constant_declaration::ConstantDeclaration;
use crate::vhdl::signal_declaration::SignalDeclaraion;
use crate::vhdl::concurrent_statement::ConcurrentStatement;
use crate::vhdl::signal_assignment::SignalAssignment;
use crate::vhdl::process::Process;
pub struct Architecture {
name : String,
entity : String,
declarations : Vec< Box< dyn BlockDeclarativeItem > >,
statements : Vec< Box< dyn ConcurrentStatement > >
}
impl Architecture {
pub fn new( name : & str, entity : & str ) -> Architecture {
Architecture { name : name.to_string(), entity : entity.to_string(),
declarations : Vec::new(), statements : Vec::new() }
}
pub fn add_constant_declaration( & mut self, constant_declaration : ConstantDeclaration ) {
self.declarations.push( Box::< ConstantDeclaration >::new( constant_declaration ) );
}
pub fn add_signal_declaration( & mut self, signal_declaration : SignalDeclaraion ) {
self.declarations.push( Box::< SignalDeclaraion >::new( signal_declaration ) );
}
pub fn add_signal_assignment( & mut self, signal_assignment : SignalAssignment ) {
self.statements.push( Box::< SignalAssignment >::new( signal_assignment ) );
}
pub fn add_process( & mut self, process : Process ) {
self.statements.push( Box::< Process >::new( process ) );
}
}
impl Element for Architecture {
fn to_source_code( & self, indent : usize ) -> String {
let mut source = String::new();
let indent_str = crate::util::indent( indent );
source.push_str( & format!( "{}{} {} {} {} {}\n", indent_str, ARCHITECTURE, self.name, OF,
self.entity, IS ) );
for declaration in & self.declarations {
source.push_str( & declaration.to_source_code( indent + 1 ) );
}
source.push_str( & format!( "{}{}\n", indent_str, BEGIN ) );
for statement in & self.statements {
source.push_str( & statement.to_source_code( indent + 1 ) );
}
source.push_str( & format!( "{}{} {} {};\n", indent_str, END, ARCHITECTURE, self.name ) );
return source;
}
}
impl DesignUnit for Architecture {
}
#[cfg(test)]
mod tests {
use super::*;
const NAME : &'static str = "rtl";
const ENTITY : &'static str = "test";
const HEADER : &'static str = "architecture rtl of test is\n";
const BEGIN : &'static str = "begin\n";
const END : &'static str = "end architecture rtl;\n";
const SIGNAL_DECLARATION : &'static str = " signal signal_1 : boolean;\n";
const SIGNAL_ASSIGNMENT : &'static str = " s1: signal_1 <= true;\n";
const CONSTANT_DECLARATION : &'static str = " constant const_1 : integer := 12;\n";
#[test]
fn architecture_frame() {
let architecture = Architecture::new( NAME, ENTITY );
assert_eq!(
architecture.to_source_code( 0 ),
format!( "{}{}{}", HEADER, BEGIN, END )
);
}
#[test]
fn architecture_with_signal_declaration() {
let mut architecture = Architecture::new( NAME, ENTITY );
architecture.add_signal_declaration( SignalDeclaraion::new( "signal_1", "boolean" ) );
assert_eq!(
architecture.to_source_code( 0 ),
format!( "{}{}{}{}", HEADER, SIGNAL_DECLARATION, BEGIN, END )
);
}
#[test]
fn architecture_with_signal_declaration_and_assignment() {
let mut architecture = Architecture::new( NAME, ENTITY );
architecture.add_signal_declaration( SignalDeclaraion::new( "signal_1", "boolean" ) );
architecture.add_signal_assignment( SignalAssignment::new_with_label( "s1", "signal_1", "true" ) );
assert_eq!(
architecture.to_source_code( 0 ),
format!( "{}{}{}{}{}", HEADER, SIGNAL_DECLARATION, BEGIN, SIGNAL_ASSIGNMENT, END )
);
}
#[test]
fn architecture_with_constant_declaration_and_assignment() {
let mut architecture = Architecture::new( NAME, ENTITY );
architecture.add_constant_declaration( ConstantDeclaration::new( "const_1", "integer", "12" ) );
assert_eq!(
architecture.to_source_code( 0 ),
format!( "{}{}{}{}", HEADER, CONSTANT_DECLARATION, BEGIN, END )
);
}
}