MIXAL
registers.cpp
1 #include <stdexcept>
2 #include <string>
3 #include "registers.h"
4 
5 namespace mixal {
6 
7 Register2::Register2() : negative(), byte1(), byte2() {
8 }
9 
10 Register2::Register2(int16_t value) : negative(), byte1(), byte2() {
11  set(value);
12 }
13 
14 Register2::Register2(bool _negative, uint8_t _byte1, uint8_t _byte2) :
15  negative(_negative), byte1(_byte1), byte2(_byte2) {
16 }
17 
18 Register2::Register2(char sign, uint8_t _byte1, uint8_t _byte2) :
19  negative(sign == '-'), byte1(_byte1), byte2(_byte2) {
20  if (sign != '+' && sign != '-') {
21  throw std::runtime_error("Invalid sign: " + std::string(1, sign));
22  }
23 }
24 
25 uint8_t Register2::operator[](int index) const {
26  if (index == 1) {
27  return byte1;
28  }
29  if (index == 2) {
30  return byte2;
31  }
32  throw std::runtime_error("Invalid index for a two bytes register: " + std::to_string(index));
33 }
34 
35 uint16_t Register2::bytes12() const {
36  int16_t high = static_cast<int16_t>(static_cast<uint8_t>(byte1));
37  int16_t low = static_cast<int16_t>(static_cast<uint8_t>(byte2));
38  return high * 64 + low;
39 }
40 
41 int16_t Register2::value() const {
42  int16_t val = static_cast<int16_t>(bytes12());
43  return negative ? -val : val;
44 }
45 
46 void Register2::set(int16_t value) {
47  if (value > 0) {
48  negative = false;
49  } else if (value < 0) {
50  negative = true;
51  value = -value;
52  }
53  byte1 = static_cast<uint8_t>(value / (1 << 6));
54  byte2 = static_cast<uint8_t>(value % (1 << 6));
55 }
56 
57 void Register2::set(int index, int8_t val) {
58  if (index == 1) {
59  byte1 = val;
60  } else if (index == 2) {
61  byte2 = val;
62  } else {
63  throw std::runtime_error("Invalid index for a two bytes register: " + std::to_string(index));
64  }
65 }
66 
67 void Register2::set(bool negative, uint8_t byte1, uint8_t byte2) {
68  this->negative = negative;
69  this->byte1 = byte1;
70  this->byte2 = byte2;
71 }
72 
73 void Register2::set(char sign, uint8_t byte1, uint8_t byte2) {
74  if (sign != '+' && sign != '-') {
75  throw std::runtime_error("Invalid sign: " + std::string(1, sign));
76  }
77  this->negative = sign == '-';
78  this->byte1 = byte1;
79  this->byte2 = byte2;
80 }
81 
82 }; // namespace mixal
mixal::Register2::set
void set(int16_t value)
Definition: registers.cpp:46
mixal::Register2::operator[]
uint8_t operator[](int index) const
Definition: registers.cpp:25
mixal::Register2::Register2
Register2()
Definition: registers.cpp:7
mixal::Register2::bytes12
uint16_t bytes12() const
Definition: registers.cpp:35
mixal::Register2::value
int16_t value() const
Definition: registers.cpp:41
registers.h
The definition of registers.