cpprisc16  June 16, 2020
cppis2.hpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 
3 /** \file cpprisc16/cppis2.hpp (March 15, 2017)
4  * \brief Additional Instructions Set 2 IS[2]:
5  * 8 new instructions is2_* and 1 instruction modified is2_add.
6  *
7  * Piece of cpprisc16.
8  * https://bitbucket.org/OPiMedia/cpprisc16
9  *
10  * GPLv3 --- Copyright (C) 2017 Olivier Pirson
11  * http://www.opimedia.be/
12  */
13 
14 #ifndef CPPRISC16_CPPRISC16_CPPIS2_HPP_
15 #define CPPRISC16_CPPRISC16_CPPIS2_HPP_
16 
17 #include "cpprisc16.hpp"
18 
19 
20 /* *******************************
21  * Macros for IS[2] instructions *
22  *********************************/
23 
24 /** \brief (Add Branch if Overflow)
25  * R[result] <-- R[a] + R[b]
26  * and if overflow
27  * then jump to label.
28  *
29  * Count for 1 instruction.
30  */
31 #define is2_add_bo(result, a, b, label) { \
32  assert(a < cpprisc16::nb_registers); \
33  assert(b < cpprisc16::nb_registers); \
34  \
35  if (is2_add(result, a, b)) { goto label; } \
36  }
37 
38 
39 /** \brief (Branch if Lower)
40  * If R[a] < R[b]
41  * then jump to label.
42  *
43  * Count for 1 instruction.
44  */
45 #define is2_bl(a, b, label) { \
46  assert(a < cpprisc16::nb_registers); \
47  assert(b < cpprisc16::nb_registers); \
48  \
49  ++cpprisc16::nb_executed; \
50  if (cpprisc16::registers[a] < cpprisc16::registers[b]) { goto label; } \
51  }
52 
53 
54 /** \brief (SHift Arithmetic)
55  * R[result] <-- (R[a] << R[b]) or (R[a] >> -R[b])
56  *
57  * If R[b] >= 0
58  * then shift to the left,
59  * else shift to the right with *duplication of the sign bit*.
60  *
61  * And if overflow
62  * then jump to label.
63  *
64  * Count for 1 instruction.
65  *
66  * @return true iff overflow
67  */
68 #define is2_sha_bo(result, a, b, label) { \
69  assert(a < cpprisc16::nb_registers); \
70  assert(b < cpprisc16::nb_registers); \
71  \
72  if (is2_sha(result, a, b)) { goto label; } \
73  }
74 
75 
76 /** \brief (SHift Logic if Overflow)
77  * R[result] <-- (R[a] << R[b]) or (R[a] >> -R[b])
78  *
79  * If R[b] >= 0
80  * then shift to the left,
81  * else shift to the right.
82  *
83  * And if overflow
84  * then jump to label.
85  *
86  * Count for 1 instruction.
87  */
88 #define is2_shl_bo(result, a, b, label) { \
89  assert(a < cpprisc16::nb_registers); \
90  assert(b < cpprisc16::nb_registers); \
91  \
92  if (is2_shl(result, a, b)) { goto label; } \
93  }
94 
95 
96 /** \brief (Sub Branch if Overflow)
97  * R[result] <-- R[a] - R[b]
98  * and if overflow
99  * then jump to label.
100  *
101  * Count for 1 instruction.
102  */
103 #define is2_sub_bo(result, a, b, label) { \
104  assert(a < cpprisc16::nb_registers); \
105  assert(b < cpprisc16::nb_registers); \
106  \
107  if (is2_sub(result, a, b)) { goto label; } \
108  }
109 
110 
111 
112 namespace cpprisc16 {
113 
114  /* ***********************************
115  * Prototypes for IS[2] instructions *
116  *************************************/
117  /** \brief
118  * R[result] <-- R[a] + R[b]
119  *
120  * See macro is2_add_bo() for jump if overflow.
121  *
122  * Count for 1 instruction.
123  *
124  * @return true iff overflow
125  */
126  bool
127  is2_add(unsigned int result, unsigned int a, unsigned int b);
128 
129 
130  /** \brief
131  * R[result - 1]:R[result] <-- R[a]*R[b]
132  *
133  * Count for 1 instruction.
134  *
135  * @param result >= 1
136  * @param a
137  * @param b
138  */
139  void
140  is2_mul(unsigned int result, unsigned int a, unsigned int b);
141 
142 
143  /** \brief
144  * R[result] <-- R[a] NOR R[b] (== ~(a | b))
145  *
146  * Count for 1 instruction.
147  */
148  void
149  is2_nor(unsigned int result, unsigned int a, unsigned int b);
150 
151 
152  /** \brief (SHift Arithmetic)
153  * R[result] <-- (R[a] << R[b]) or (R[a] >> -R[b])
154  *
155  * If R[b] >= 0
156  * then shift to the left,
157  * else shift to the right with *duplication of the sign bit*.
158  *
159  * See macro is2_sha_bo() for jump if overflow.
160  *
161  * Count for 1 instruction.
162  *
163  * @return true iff overflow
164  */
165  bool
166  is2_sha(unsigned int result, unsigned int a, unsigned int b);
167 
168 
169  /** \brief (Shift Immediate)
170  * R[result] <-- (R[a] << immed5) or (R[a] >> -immed5)
171  *
172  * 6 5 43210
173  * immed7 is decomposed in x:M:immed5
174  * where: x is 1 bit unused
175  * M is 1 bit: 0 for logic mode, 1 for arithmetic mode
176  * immed5 is 5 signed bits
177  *
178  * If immed5 >= 0
179  * then shift to the left,
180  * else shift to the right
181  * (with *duplication of the sign bit* if arithmetic mode).
182  *
183  * Count for 1 instruction.
184  */
185  void
186  is2_shifti(unsigned int result, unsigned int a, immed_t immed7);
187 
188 
189  /** \brief (SHift Logic)
190  * R[result] <-- (R[a] << R[b]) or (R[a] >> -R[b])
191  *
192  * If R[b] >= 0
193  * then shift to the left,
194  * else shift to the right.
195  *
196  * See macro is2_shl_bo() for jump if overflow.
197  *
198  * Count for 1 instruction.
199  *
200  * @return true iff overflow
201  */
202  bool
203  is2_shl(unsigned int result, unsigned int a, unsigned int b);
204 
205 
206  /** \brief
207  * R[result] <-- R[a] - R[b]
208  *
209  * See macro is2_sub_bo() for jump if overflow.
210  *
211  * Count for 1 instruction.
212  *
213  * @return true iff overflow
214  */
215  bool
216  is2_sub(unsigned int result, unsigned int a, unsigned int b);
217 
218 
219  /** \brief
220  * R[result] <-- R[a] XOR R[b] (== ~(a ^ b))
221  *
222  * Count for 1 instruction.
223  */
224  void
225  is2_xor(unsigned int result, unsigned int a, unsigned int b);
226 
227 } // namespace cpprisc16
228 
229 
230 #endif // CPPRISC16_CPPRISC16_CPPIS2_HPP_
void is2_xor(unsigned int result, unsigned int a, unsigned int b)
R[result] <– R[a] XOR R[b] (== ~(a ^ b))
Definition: cppis2.cpp:103
bool is2_shl(unsigned int result, unsigned int a, unsigned int b)
(SHift Logic) R[result] <– (R[a] << R[b]) or (R[a] >> -R[b])
Definition: cppis2.cpp:73
void is2_shifti(unsigned int result, unsigned int a, immed_t immed7)
(Shift Immediate) R[result] <– (R[a] << immed5) or (R[a] >> -immed5)
Definition: cppis2.cpp:83
bool is2_sha(unsigned int result, unsigned int a, unsigned int b)
(SHift Arithmetic) R[result] <– (R[a] << R[b]) or (R[a] >> -R[b])
Definition: cppis2.cpp:63
std::uint16_t immed_t
Type for immediate value.
Definition: cpprisc16.hpp:48
void is2_mul(unsigned int result, unsigned int a, unsigned int b)
R[result - 1]:R[result] <– R[a]*R[b].
Definition: cppis2.cpp:34
Instructions set of RiSC16: 8 instructions i_* and 4 pseudo-instructions p_*.
bool is2_add(unsigned int result, unsigned int a, unsigned int b)
R[result] <– R[a] + R[b].
Definition: cppis2.cpp:24
bool is2_sub(unsigned int result, unsigned int a, unsigned int b)
R[result] <– R[a] - R[b].
Definition: cppis2.cpp:93
void is2_nor(unsigned int result, unsigned int a, unsigned int b)
R[result] <– R[a] NOR R[b] (== ~(a | b))
Definition: cppis2.cpp:53