blob: a0d0f6cfdcf2ac92516d8ffef92a79a95c974de4 [file] [log] [blame]
Zane Shelley871adec2019-07-30 11:01:39 -05001#pragma once
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -05002
Zane Shelley52cb1a92019-08-21 14:38:31 -05003#include <register/hei_register.hpp>
4
Zane Shelley871adec2019-07-30 11:01:39 -05005namespace libhei
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -05006{
7
Zane Shelleyb0559b92019-12-05 22:18:20 -06008/**
9 * @brief An abstract class for all operator registers.
10 *
11 * Contains member functions and variables that are required for all child
12 * classes.
13 */
14class OperatorRegister : public Register
15{
16 public:
17 /** @brief Pure virtual destructor. */
18 virtual ~OperatorRegister() = 0;
19
20 protected:
21 /**
22 * @brief Constructor from components.
23 * @param i_size Size (in bytes) of this register.
24 */
25 explicit OperatorRegister(size_t i_size) : Register(), iv_result(i_size * 8)
26 {}
27
28 protected: // Instance variables
29 /** When getBitString() is called on an operator, the resulting value of the
30 * operation will be stored in this instance variable. */
31 BitStringBuffer iv_result;
32
33 public:
34 /** @brief Overloaded from parent class. */
35 virtual const BitString* getBitString(const Chip& i_chip) const = 0;
36
37 /** @brief Overloaded from parent class. */
38 size_t getSize() const
39 {
40 return (BitString::getMinBytes(iv_result.getBitLen()));
41 }
42};
43
44// Pure virtual destructor must be defined.
45inline OperatorRegister::~OperatorRegister() {}
46
47class NotRegister : public OperatorRegister
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050048{
49 public:
Zane Shelley48aa8602019-12-05 21:41:21 -060050 /**
51 * @brief Constructor from components.
52 * @param i_arg Target register for operation.
53 */
54 explicit NotRegister(Register& i_arg) :
Zane Shelleyb0559b92019-12-05 22:18:20 -060055 OperatorRegister(i_arg.getSize()), iv_child(&i_arg)
Zane Shelleyd4c0e982019-12-05 21:27:41 -060056 {}
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050057
Zane Shelley48aa8602019-12-05 21:41:21 -060058 /** @brief Default destructor. */
59 ~NotRegister() = default;
60
61 /** @brief Default copy constructor. */
62 NotRegister(const NotRegister&) = default;
63
64 /** @brief Default assignment operator. */
65 NotRegister& operator=(const NotRegister& r) = default;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050066
Zane Shelleyb0559b92019-12-05 22:18:20 -060067 /** @brief Overloaded from parent class. */
Zane Shelleycaee69f2019-12-05 13:42:58 -060068 const BitString* getBitString(const Chip& i_chip) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050069 {
Zane Shelleyd4c0e982019-12-05 21:27:41 -060070 const auto* bs = iv_child->getBitString(i_chip);
71
Zane Shelleyb0559b92019-12-05 22:18:20 -060072 (const_cast<NotRegister*>(this))->iv_result = ~(*bs);
Zane Shelleyd4c0e982019-12-05 21:27:41 -060073
Zane Shelleyb0559b92019-12-05 22:18:20 -060074 return &iv_result;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050075 }
76
Zane Shelleycaee69f2019-12-05 13:42:58 -060077 bool operator==(const NotRegister& r) const
78 {
79 return r.iv_child == iv_child;
80 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050081
Zane Shelleycaee69f2019-12-05 13:42:58 -060082 bool operator<(const NotRegister& r) const
83 {
84 return iv_child < r.iv_child;
85 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050086
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050087 private:
Zane Shelleycaee69f2019-12-05 13:42:58 -060088 Register* iv_child;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050089};
90
Zane Shelleyb0559b92019-12-05 22:18:20 -060091class LeftShiftRegister : public OperatorRegister
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050092{
93 public:
Zane Shelley48aa8602019-12-05 21:41:21 -060094 /**
95 * @brief Constructor from components.
96 * @param i_arg Target register for operation.
97 * @param i_amount The shift value.
98 */
Zane Shelleycaee69f2019-12-05 13:42:58 -060099 LeftShiftRegister(Register& i_arg, uint16_t i_amount) :
Zane Shelleyb0559b92019-12-05 22:18:20 -0600100 OperatorRegister(i_arg.getSize()), iv_child(&i_arg), iv_amount(i_amount)
Zane Shelleyd4c0e982019-12-05 21:27:41 -0600101 {}
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500102
Zane Shelley48aa8602019-12-05 21:41:21 -0600103 /** @brief Default destructor. */
104 ~LeftShiftRegister() = default;
105
106 /** @brief Default copy constructor. */
107 LeftShiftRegister(const LeftShiftRegister&) = default;
108
109 /** @brief Default assignment operator. */
110 LeftShiftRegister& operator=(const LeftShiftRegister& r) = default;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500111
Zane Shelleyb0559b92019-12-05 22:18:20 -0600112 /** @brief Overloaded from parent class. */
Zane Shelleycaee69f2019-12-05 13:42:58 -0600113 const BitString* getBitString(const Chip& i_chip) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500114 {
Zane Shelleyd4c0e982019-12-05 21:27:41 -0600115 const auto* bs = iv_child->getBitString(i_chip);
116
Zane Shelleyb0559b92019-12-05 22:18:20 -0600117 (const_cast<LeftShiftRegister*>(this))->iv_result = (*bs) << iv_amount;
Zane Shelleyd4c0e982019-12-05 21:27:41 -0600118
Zane Shelleyb0559b92019-12-05 22:18:20 -0600119 return &iv_result;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500120 }
121
Zane Shelleycaee69f2019-12-05 13:42:58 -0600122 bool operator==(const LeftShiftRegister& r) const
123 {
124 return (r.iv_child == iv_child) && (r.iv_amount == iv_amount);
125 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500126
Zane Shelleycaee69f2019-12-05 13:42:58 -0600127 bool operator<(const LeftShiftRegister& r) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500128 {
129 if (iv_child == r.iv_child)
130 return iv_amount < r.iv_amount;
131 return iv_child < r.iv_child;
132 }
133
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500134 private:
Zane Shelleycaee69f2019-12-05 13:42:58 -0600135 Register* iv_child;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500136 uint16_t iv_amount;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500137};
138
Zane Shelleyb0559b92019-12-05 22:18:20 -0600139class RightShiftRegister : public OperatorRegister
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500140{
141 public:
Zane Shelley48aa8602019-12-05 21:41:21 -0600142 /**
143 * @brief Constructor from components.
144 * @param i_arg Target register for operation.
145 * @param i_amount The shift value.
146 */
Zane Shelleycaee69f2019-12-05 13:42:58 -0600147 RightShiftRegister(Register& i_arg, uint16_t i_amount) :
Zane Shelleyb0559b92019-12-05 22:18:20 -0600148 OperatorRegister(i_arg.getSize()), iv_child(&i_arg), iv_amount(i_amount)
Zane Shelleyd4c0e982019-12-05 21:27:41 -0600149 {}
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500150
Zane Shelley48aa8602019-12-05 21:41:21 -0600151 /** @brief Default destructor. */
152 ~RightShiftRegister() = default;
153
154 /** @brief Default copy constructor. */
155 RightShiftRegister(const RightShiftRegister&) = default;
156
157 /** @brief Default assignment operator. */
158 RightShiftRegister& operator=(const RightShiftRegister& r) = default;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500159
Zane Shelleyb0559b92019-12-05 22:18:20 -0600160 /** @brief Overloaded from parent class. */
Zane Shelleycaee69f2019-12-05 13:42:58 -0600161 const BitString* getBitString(const Chip& i_chip) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500162 {
Zane Shelleyd4c0e982019-12-05 21:27:41 -0600163 const auto* bs = iv_child->getBitString(i_chip);
164
Zane Shelleyb0559b92019-12-05 22:18:20 -0600165 (const_cast<RightShiftRegister*>(this))->iv_result = (*bs) >> iv_amount;
Zane Shelleyd4c0e982019-12-05 21:27:41 -0600166
Zane Shelleyb0559b92019-12-05 22:18:20 -0600167 return &iv_result;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500168 }
169
Zane Shelleycaee69f2019-12-05 13:42:58 -0600170 bool operator==(const RightShiftRegister& r) const
171 {
172 return (r.iv_child == iv_child) && (r.iv_amount == iv_amount);
173 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500174
Zane Shelleycaee69f2019-12-05 13:42:58 -0600175 bool operator<(const RightShiftRegister& r) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500176 {
177 if (iv_child == r.iv_child)
178 return iv_amount < r.iv_amount;
179 return iv_child < r.iv_child;
180 }
181
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500182 private:
Zane Shelleycaee69f2019-12-05 13:42:58 -0600183 Register* iv_child;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500184 uint16_t iv_amount;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500185};
186
Zane Shelleyb0559b92019-12-05 22:18:20 -0600187class AndRegister : public OperatorRegister
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500188{
189 public:
Zane Shelley48aa8602019-12-05 21:41:21 -0600190 /**
191 * @brief Constructor from components.
192 * @param i_left Target register for operation.
193 * @param i_right Target register for operation.
194 */
Zane Shelleycaee69f2019-12-05 13:42:58 -0600195 AndRegister(Register& i_left, Register& i_right) :
Zane Shelleyb0559b92019-12-05 22:18:20 -0600196 OperatorRegister(std::min(i_left.getSize(), i_right.getSize())),
197 iv_left(&i_left), iv_right(&i_right)
Zane Shelleyd4c0e982019-12-05 21:27:41 -0600198 {}
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500199
Zane Shelley48aa8602019-12-05 21:41:21 -0600200 /** @brief Default destructor. */
201 ~AndRegister() = default;
202
203 /** @brief Default copy constructor. */
204 AndRegister(const AndRegister&) = default;
205
206 /** @brief Default assignment operator. */
207 AndRegister& operator=(const AndRegister& r) = default;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500208
Zane Shelleyb0559b92019-12-05 22:18:20 -0600209 /** @brief Overloaded from parent class. */
Zane Shelleycaee69f2019-12-05 13:42:58 -0600210 const BitString* getBitString(const Chip& i_chip) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500211 {
Zane Shelleyd4c0e982019-12-05 21:27:41 -0600212 const auto* l_bs = iv_left->getBitString(i_chip);
213 const auto* r_bs = iv_right->getBitString(i_chip);
214
Zane Shelleyb0559b92019-12-05 22:18:20 -0600215 (const_cast<AndRegister*>(this))->iv_result = (*l_bs) & (*r_bs);
Zane Shelleyd4c0e982019-12-05 21:27:41 -0600216
Zane Shelleyb0559b92019-12-05 22:18:20 -0600217 return &iv_result;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500218 }
219
Zane Shelleycaee69f2019-12-05 13:42:58 -0600220 bool operator==(const AndRegister& r) const
221 {
222 return (r.iv_left == iv_left) && (r.iv_right == iv_right);
223 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500224
Zane Shelleycaee69f2019-12-05 13:42:58 -0600225 bool operator<(const AndRegister& r) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500226 {
227 if (iv_left == r.iv_left)
228 return iv_right < r.iv_right;
229 return iv_left < r.iv_left;
230 }
231
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500232 private:
Zane Shelleycaee69f2019-12-05 13:42:58 -0600233 Register* iv_left;
234 Register* iv_right;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500235};
236
Zane Shelleyb0559b92019-12-05 22:18:20 -0600237class OrRegister : public OperatorRegister
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500238{
239 public:
Zane Shelley48aa8602019-12-05 21:41:21 -0600240 /**
241 * @brief Constructor from components.
242 * @param i_left Target register for operation.
243 * @param i_right Target register for operation.
244 */
Zane Shelleycaee69f2019-12-05 13:42:58 -0600245 OrRegister(Register& i_left, Register& i_right) :
Zane Shelleyb0559b92019-12-05 22:18:20 -0600246 OperatorRegister(std::min(i_left.getSize(), i_right.getSize())),
247 iv_left(&i_left), iv_right(&i_right)
Zane Shelleyd4c0e982019-12-05 21:27:41 -0600248 {}
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500249
Zane Shelley48aa8602019-12-05 21:41:21 -0600250 /** @brief Default destructor. */
251 ~OrRegister() = default;
252
253 /** @brief Default copy constructor. */
254 OrRegister(const OrRegister&) = default;
255
256 /** @brief Default assignment operator. */
257 OrRegister& operator=(const OrRegister& r) = default;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500258
Zane Shelleyb0559b92019-12-05 22:18:20 -0600259 /** @brief Overloaded from parent class. */
Zane Shelleycaee69f2019-12-05 13:42:58 -0600260 const BitString* getBitString(const Chip& i_chip) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500261 {
Zane Shelleyd4c0e982019-12-05 21:27:41 -0600262 const auto* l_bs = iv_left->getBitString(i_chip);
263 const auto* r_bs = iv_right->getBitString(i_chip);
264
Zane Shelleyb0559b92019-12-05 22:18:20 -0600265 (const_cast<OrRegister*>(this))->iv_result = (*l_bs) | (*r_bs);
Zane Shelleyd4c0e982019-12-05 21:27:41 -0600266
Zane Shelleyb0559b92019-12-05 22:18:20 -0600267 return &iv_result;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500268 }
269
Zane Shelleycaee69f2019-12-05 13:42:58 -0600270 bool operator==(const OrRegister& r) const
271 {
272 return (r.iv_left == iv_left) && (r.iv_right == iv_right);
273 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500274
Zane Shelleycaee69f2019-12-05 13:42:58 -0600275 bool operator<(const OrRegister& r) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500276 {
277 if (iv_left == r.iv_left)
278 return iv_right < r.iv_right;
279 return iv_left < r.iv_left;
280 }
281
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500282 private:
Zane Shelleycaee69f2019-12-05 13:42:58 -0600283 Register* iv_left;
284 Register* iv_right;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500285};
286
Zane Shelleyb0559b92019-12-05 22:18:20 -0600287class ConstantRegister : public OperatorRegister
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500288{
289 public:
Zane Shelley48aa8602019-12-05 21:41:21 -0600290 /**
291 * @brief Constructor from components.
292 * @param i_arg A BitStringBuffer containing the constant value.
293 */
294 explicit ConstantRegister(const BitStringBuffer& i_arg) :
Zane Shelleyb0559b92019-12-05 22:18:20 -0600295 OperatorRegister(BitString::getMinBytes(i_arg.getBitLen()))
296 {
297 iv_result = i_arg;
298 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500299
Zane Shelley48aa8602019-12-05 21:41:21 -0600300 /** @brief Default destructor. */
301 ~ConstantRegister() = default;
302
303 /** @brief Default copy constructor. */
304 ConstantRegister(const ConstantRegister&) = default;
305
306 /** @brief Default assignment operator. */
307 ConstantRegister& operator=(const ConstantRegister& r) = default;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500308
Zane Shelleyb0559b92019-12-05 22:18:20 -0600309 /** @brief Overloaded from parent class. */
Zane Shelleycaee69f2019-12-05 13:42:58 -0600310 const BitString* getBitString(const Chip& i_chip) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500311 {
Zane Shelleyb0559b92019-12-05 22:18:20 -0600312 return &iv_result;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500313 }
314
Zane Shelleycaee69f2019-12-05 13:42:58 -0600315 bool operator==(const ConstantRegister& r) const
316 {
Zane Shelleyb0559b92019-12-05 22:18:20 -0600317 return r.iv_result == iv_result;
Zane Shelleycaee69f2019-12-05 13:42:58 -0600318 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500319};
320
Zane Shelley871adec2019-07-30 11:01:39 -0500321} // end namespace libhei