summaryrefslogtreecommitdiffstats
path: root/src/uscxml/transform/ChartToVHDL.h
blob: f3bcd3baed423b6e85f5156195537114243aa429 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
 *  @file
 *  @author     2015-2016 Jens Heuschkel (heuschkel@tk.tu-darmstadt.de)
 *  @copyright  Simplified BSD
 *
 *  @cond
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the FreeBSD license as published by the FreeBSD
 *  project.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 *  You should have received a copy of the FreeBSD license along with this
 *  program. If not, see <http://www.opensource.org/licenses/bsd-license>.
 *  @endcond
 */

#ifndef CHARTOVHDL_H
#define CHARTOVHDL_H

#include "uscxml/interpreter/InterpreterDraft6.h"
#include "uscxml/dom/DOMUtils.h"
#include "uscxml/util/Trie.h"
#include "Transformer.h"
#include "ChartToC.h"

#include <DOM/Document.hpp>
#include <DOM/Node.hpp>
#include <XPath/XPath.hpp>
#include <ostream>
#include <vector>

namespace uscxml {

class USCXML_API ChartToVHDL : public ChartToC {
public:

	virtual ~ChartToVHDL();
	static Transformer transform(const Interpreter& other);

	void writeTo(std::ostream& stream);


	struct VNode {
		virtual void print(std::ostream& stream, const std::string padding = "") = 0;
		virtual ~VNode() {};
	};
	struct VBranch : VNode {
		std::vector< VNode* > v;
		virtual ~VBranch() {
			for(unsigned i = 0; i < v.size(); i++)
				delete v[i];
		}

		VBranch& operator +=(VNode* p ) {
			v.push_back(p);
			return *this;
		}
	};

	struct VPointer {
		VNode* ptr;

		operator VNode*() {
			return ptr;
		}

		VPointer& operator /( VNode* p ) {
			ptr = p;
			return *this;
		}
	};

	struct VContainer {
		VBranch* ptr;

		operator VBranch*() {
			return ptr;
		}
		VContainer& operator /( VBranch* p ) {
			ptr = p;
			return *this;
		}
		VContainer& operator , ( VPointer p ) {
			if(ptr) ptr->v.push_back(p.ptr);
			return *this;
		}
		VContainer& operator , ( VContainer c ) {
			if(ptr) ptr->v.push_back(c.ptr);
			return *this;
		}
	};

	struct VLine : VNode {
		VLine(const std::string& name) : name(name) {}
		virtual void print(std::ostream& stream, const std::string padding = "") {
			stream << " " << name;
		}

		std::string name;
	};

	struct VAssign : VBranch {
		virtual void print(std::ostream& stream, const std::string padding = "") {
			v[0]->print(stream, padding);
			stream << padding << " <=";
			v[1]->print(stream, padding + "  ");
		}
	};

	struct VAnd : VBranch {
		virtual void print(std::ostream& stream, const std::string padding = "") {
			stream << std::endl << padding << "( '1' ";
			for(unsigned i = 0; i < v.size(); i++) {
				stream << std::endl << padding << "  and";
				v[i]->print(stream, padding + "    ");
			}
			stream << padding << ")" << std::endl;
		}
	};

	struct VOr : VBranch {
		virtual void print(std::ostream& stream, const std::string padding = "") {
			stream << std::endl << padding << "( '0' ";
			for(unsigned i = 0; i < v.size(); i++) {
				stream << std::endl << padding << "  or";
				v[i]->print(stream, padding + "    ");
			}
			stream << std::endl << padding << ")" << std::endl;
		}
	};

	struct VNot : VBranch {
		virtual void print(std::ostream& stream, const std::string padding = "") {
			stream << " ( not";
			v[0]->print(stream, padding + "  ");
			stream << " )";
		}
	};

	struct VNop : VBranch {
		virtual void print(std::ostream& stream, const std::string padding = "") {
			v[0]->print(stream, padding);
		}
	};


#define VLINE   VPointer()/new VLine
#define VASSIGN VContainer()/new VAssign
#define VOR     VContainer()/new VOr
#define VAND    VContainer()/new VAnd
#define VNOT    VContainer()/new VNot
#define VNOP    VContainer()/new VNop



protected:
	ChartToVHDL(const Interpreter& other);

	void checkDocument();
	void findEvents();

	void writeTypes(std::ostream& stream);
	void writeIncludes(std::ostream& stream);
        
        // top layer components
	void writeFiFo(std::ostream& stream);
	void writeEventController(std::ostream & stream);
	void writeMicroStepper(std::ostream& stream);
	void writeTestbench(std::ostream & stream);
        
        // system
	void writeSignalsAndComponents(std::ostream& stream);
	void writeSystemSignalMapping(std::ostream& stream);
	void writeModuleInstantiation(std::ostream& stream);

        // combinatorial logic
	void writeOptimalTransitionSetSelection(std::ostream& stream);
	void writeExitSet(std::ostream & stream);
	void writeEntrySet(std::ostream & stream);
	void writeTransitionSet(std::ostream & stream);
	void writeDefaultCompletions(std::ostream & stream);
	void writeCompleteEntrySet(std::ostream & stream);
	void writeActiveStateNplusOne(std::ostream & stream);

        // handler
	void writeStateHandler(std::ostream& stream);
	void writeResetHandler(std::ostream & stream);
	void writeSpontaneousHandler(std::ostream & stream);
	void writeInternalEventHandler(std::ostream & stream);
	void writeErrorHandler(std::ostream& stream);
        
        // event generation
	void writeExContentBlock(std::ostream & stream, std::string index, 
            std::list< Arabica::DOM::Element<std::string> > commandSequence);
        
	Trie _eventTrie;
    Arabica::XPath::NodeSet<std::string> _execContent;

private:
	std::string eventNameEscape(const std::string& eventName);

    std::string getLineForExecContent(const Arabica::DOM::Node<std::string>& elem);

};

}

#endif /* end of include guard: FSMTOCPP_H_201672B0 */