OpenASIP 2.2
Loading...
Searching...
No Matches
CUOpcodeGenerator.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2014 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file CUOpcodeGenerator.hh
26 *
27 * Implementation of ProcessorGenerator class.
28 *
29 * @author Henry Linjamäki 2014 (henry.linjamaki-no.spam-tut.fi)
30 * @note rating: red
31 */
32
33#include <set>
34#include <algorithm>
35
36#include "CUOpcodeGenerator.hh"
37
38#include "Machine.hh"
39#include "ControlUnit.hh"
40#include "ProGeTypes.hh"
41#include "MachineInfo.hh"
42#include "Conversion.hh"
43#include "MathTools.hh"
44
45using namespace std;
46
47namespace ProGe {
48
49/*
50 * Constructor for the class.
51 *
52 * @param mach The machine.
53 * @param entityName Prefix name for opcode package
54 */
56 const TTAMachine::Machine& mach, const string& entityName)
57 : mach_(&mach), entityStr_(entityName) {}
58
59CUOpcodeGenerator::CUOpcodeGenerator() : mach_(NULL), entityStr_() {}
60
62
63/*
64 * Returns encodings for GCU as map<operationName, encoding>.
65 */
68 assert(mach_ != NULL);
69
72 size_t encoding = 0;
74
75 MachineInfo::OperationSet::iterator it;
76 for (it = gcuOps.begin(); it != gcuOps.end(); it++) {
77 encMap.insert(make_pair(StringTools::stringToLower(*it), encoding));
78 encoding++;
79 }
80 return encMap;
81}
82
83size_t
84CUOpcodeGenerator::encoding(const std::string& operName) const {
86 if (encMap.count(operName)) {
87 return encMap.at(operName);
88 } else {
90 InstanceNotFound, "CU does not have operation" + operName + ".");
91 }
92}
93
94/*
95 * Return required width for GCU's opcode ports.
96 */
97size_t
101
102/*
103 * Generates RTL package that holds GCU's opcode encoding constants.
104 *
105 * The opcode names are written as IFE_<operName> in upper case. For legacy
106 * support if GCU does not include JUMP or CALL operations then those will
107 * be still added to the package too as dummy operations since ifetch
108 * templates refers to them.
109 *
110 * @param language Target language. VHDL and Verilog are supported.
111 * @param stream File stream.
112 */
113void
115 HDL language, std::ofstream& stream) const {
116 OperationEncodingMapType gcuEncodings = encodings();
117
118 // Add dummy operations for missing CALL and JUMP operations since ifetch
119 // templates refers to them.
120 gcuEncodings.insert(
121 make_pair(OperationType("call"), gcuEncodings.size()));
122 gcuEncodings.insert(
123 make_pair(OperationType("jump"), gcuEncodings.size()));
124
125 if (language == VHDL) {
126 WriteVhdlOpcodePackage(gcuEncodings, stream);
127 } else if (language == Verilog) {
128 WriteVerilogOpcodePackage(gcuEncodings, stream);
129 } else {
130 assert(false && "Unsupported HDL.");
131 }
132}
133
134/*
135 * Return required width for GCU's opcode ports.
136 *
137 * @param mach The machine.
138 */
139size_t
141 const TTAMachine::ControlUnit* gcu = mach.controlUnit();
142 size_t opCount = gcu->operationCount();
143 if (opCount == 0) {
144 return 0;
145 } else {
146 return std::max(MathTools::requiredBits(opCount - 1), 1);
147 }
148}
149
150/*
151 * VHDL version for writing GCU opcode package.
152 */
153void
155 const OperationEncodingMapType& encodings, std::ofstream& stream) const {
156 stream << "library IEEE;" << endl
157 << "use IEEE.std_logic_1164.all;" << endl
158 << endl
159 << "package " + entityStr_ + "_gcu_opcodes is" << endl;
160 OperationEncodingMapType::const_iterator it;
161 for (it = encodings.begin(); it != encodings.end(); it++) {
162 stream << " constant IFE_" << StringTools::stringToUpper(it->first)
163 << " : natural := " << Conversion::toString(it->second) << ";"
164 << endl;
165 }
166 stream << "end " + entityStr_ + "_gcu_opcodes;" << endl;
167}
168
169/*
170 * Verilog version for writing GCU opcode package.
171 */
172void
174 const OperationEncodingMapType& encodings, std::ofstream& stream) const {
175 if (encodings.empty()) {
176 return;
177 }
178
179 OperationEncodingMapType::const_iterator it;
180 for (it = encodings.begin(); it != encodings.end(); it++) {
181 stream << "parameter IFE_" << StringTools::stringToUpper(it->first)
182 << "=" << Conversion::toString(it->second);
183 if (it != --encodings.end()) {
184 stream << "," << endl;
185 } else {
186 stream << endl;
187 }
188 }
189}
190
191} /* namespace ProGe */
#define assert(condition)
#define THROW_EXCEPTION(exceptionType, message)
Exception wrapper macro that automatically includes file name, line number and function name where th...
Definition Exception.hh:39
static std::string toString(const T &source)
TCETools::CIStringSet OperationSet
static OperationSet getOpset(const TTAMachine::Machine &mach)
static int requiredBits(unsigned long int number)
void generateOpcodePackage(HDL language, std::ofstream &stream) const
std::map< OperationType, EncodingType, TCEString::ICLess > OperationEncodingMapType
size_t encoding(const std::string &operName) const
void WriteVhdlOpcodePackage(const OperationEncodingMapType &encodings, std::ofstream &stream) const
const TTAMachine::Machine * mach_
OperationEncodingMapType encodings() const
void WriteVerilogOpcodePackage(const OperationEncodingMapType &encodings, std::ofstream &stream) const
static size_t gcuOpcodeWidth(const TTAMachine::Machine &mach)
static std::string stringToUpper(const std::string &source)
static std::string stringToLower(const std::string &source)
virtual int operationCount() const
virtual ControlUnit * controlUnit() const
Definition Machine.cc:345
Definition FUGen.hh:54
HDL
HDLs supported by ProGe.
Definition ProGeTypes.hh:40
@ Verilog
Verilog.
Definition ProGeTypes.hh:42
@ VHDL
VHDL.
Definition ProGeTypes.hh:41