Avr8bit-emulator
An emulator for the Atmel AVR 8-bit microcontroller
Loading...
Searching...
No Matches
bit_test.c
Go to the documentation of this file.
1#include "bit_test.h"
2
3void lsl(uint8_t d, struct CORE *core) {
4 // Logical Shift Left
5 // r(7) loaded into C
6 // Rd <- Rd << 1
7 // 1 cycle
8
9 core->sreg.C = (core->gp.R[d] & 0x80) >> 7;
10 core->gp.R[d] = core->gp.R[d] << 1;
11
12 // Update SREG
13 // C updated by instruction
14 update_sreg_H(core, (core->gp.R[d] & 0x0F) >> 3);
15 update_sreg_N(core, core->gp.R[d] >> 7);
16 update_sreg_V(core, core->sreg.N ^ core->sreg.C);
17 update_sreg_S(core, sreg_S_compute_bool(core->sreg.N, core->sreg.V));
18 update_sreg_Z(core, sreg_Z_compute_bool(core->gp.R[d]));
19
20 inc_pc(core);
21}
22
23void lsr(uint8_t d, struct CORE *core) {
24 // Logical Shift Right
25 // r(0) loaded into C
26 // Rd <- Rd >> 1
27 // 1 cycle
28
29 core->sreg.C = core->gp.R[d] & 0x01;
30 core->gp.R[d] = core->gp.R[d] >> 1;
31
32 // Update SREG
33 update_sreg_N(core, false);
34 update_sreg_V(core, core->sreg.N ^ core->sreg.C);
35 update_sreg_S(core, sreg_S_compute_bool(core->sreg.N, core->sreg.V));
36 update_sreg_Z(core, sreg_Z_compute_bool(core->gp.R[d]));
37
38 inc_pc(core);
39}
40
41void rol(uint8_t d, struct CORE *core) {
42 // Rotate Left through Cary
43 // r(7) loaded into C
44 // Rd <- Rd << 1 | C
45 // 1 cycle
46 bool temp = core->sreg.C;
47 core->sreg.C = (core->gp.R[d] & 0x80) >> 7;
48 core->gp.R[d] = (core->gp.R[d] << 1) | temp;
49
50 // Update SREG
51 // C updated by instruction
52 update_sreg_H(core, (core->gp.R[d] & 0x0F) >> 3);
53 update_sreg_N(core, core->gp.R[d] >> 7);
54 update_sreg_V(core, core->sreg.N ^ core->sreg.C);
55 update_sreg_S(core, sreg_S_compute_bool(core->sreg.N, core->sreg.V));
56 update_sreg_Z(core, sreg_Z_compute_bool(core->gp.R[d]));
57
58 inc_pc(core);
59}
60
61void ror(uint8_t d, struct CORE *core) {
62 // Rotate Right through Cary
63 // r(0) loaded into C
64 // Rd <- Rd >> 1 | C << 7
65 // 1 cycle
66 bool temp = core->sreg.C;
67 core->sreg.C = core->gp.R[d] & 0x01;
68 core->gp.R[d] = (core->gp.R[d] >> 1) | (temp << 7);
69
70 // Update SREG
71 // C updated by instruction
72 update_sreg_N(core, core->gp.R[d] >> 7);
73 update_sreg_V(core, core->sreg.N ^ core->sreg.C);
74 update_sreg_S(core, sreg_S_compute_bool(core->sreg.N, core->sreg.V));
75 update_sreg_Z(core, sreg_Z_compute_bool(core->gp.R[d]));
76
77 inc_pc(core);
78}
79
80void asr(uint8_t d, struct CORE *core) {
81 // Arithmetic Shift Right
82 // r(0) loaded into C
83 // Rd <- Rd >> 1
84 // 1 cycle
85 core->sreg.C = core->gp.R[d] & 0x01;
86 core->gp.R[d] = (core->gp.R[d] & 0x80) | (core->gp.R[d] >> 1);
87
88 // Update SREG
89 update_sreg_N(core, core->gp.R[d] >> 7);
90 update_sreg_V(core, core->sreg.N ^ core->sreg.C);
91 update_sreg_S(core, sreg_S_compute_bool(core->sreg.N, core->sreg.V));
92 update_sreg_Z(core, sreg_Z_compute_bool(core->gp.R[d]));
93
94 inc_pc(core);
95}
96
97void swap(uint8_t d, struct CORE *core) {
98 // Swap Nibbles
99 // Rd <- Rd(3:0) << 4 | Rd(7:4)
100 // 1 cycle
101 core->gp.R[d] = (core->gp.R[d] & 0x0F) << 4 | (core->gp.R[d] & 0xF0) >> 4;
102
103 // Update SREG
104 // NONE
105
106 inc_pc(core);
107}
108
109void sbi(uint8_t IO, uint8_t n, struct CORE *core) {
110 // Set Bit in I/O Register
111 // I/O(n) <- 1
112 // 2 cycle
113 core->gp.R[IO] = core->gp.R[IO] | (1 << n);
114
115 // Update SREG
116 // NONE
117
118 inc_pc(core);
119}
120
121void cbi(uint8_t IO, uint8_t n, struct CORE *core) {
122 // Clear Bit in I/O Register
123 // I/O(n) <- 0
124 // 2 cycle
125 core->gp.R[IO] = core->gp.R[IO] & ~(1 << n);
126
127 // Update SREG
128 // NONE
129
130 inc_pc(core);
131}
132
133void bst(uint8_t d, uint8_t b, struct CORE *core) {
134 // Bit Store from Register to T Flag
135 // T <- Rd(b)
136 // 1 cycle
137 core->sreg.T = (core->gp.R[d] & (1 << b)) >> b;
138
139 // Update SREG
140 // T updated by instruction
141
142 inc_pc(core);
143}
144
145void bld(uint8_t d, uint8_t b, struct CORE *core) {
146 // Bit Load from T Flag to Register
147 // Rd(b) <- T
148 // 1 cycle
149 core->gp.R[d] = (core->gp.R[d] & ~(1 << b)) | (core->sreg.T << b);
150
151 // Update SREG
152 // NONE
153
154 inc_pc(core);
155}
156
157void bset(uint8_t s, struct CORE *core) {
158 // Bit Set in SREG
159 // S <- 1
160 // 1 cycle
161 core->sreg.sreg = core->sreg.sreg | (1 << s);
162
163 // Update SREG
164 // Updated by instruction
165
166 inc_pc(core);
167}
168
169void bclr(uint8_t s, struct CORE *core) {
170 // Bit Clear in SREG
171 // S <- 0
172 // 1 cycle
173 core->sreg.sreg = core->sreg.sreg & ~(1 << s);
174
175 // Update SREG
176 // Updated by instruction
177
178 inc_pc(core);
179}
180
181void sec(struct CORE *core) {
182 // Set Cary Flag
183 // C <- 1
184 // 1 cycle
185 core->sreg.C = 1;
186
187 // Update SREG
188 // Updated by instruction
189
190 inc_pc(core);
191}
192
193void clc(struct CORE *core) {
194 // Clear Cary Flag
195 // C <- 0
196 // 1 cycle
197 core->sreg.C = 0;
198
199 // Update SREG
200 // Updated by instruction
201
202 inc_pc(core);
203}
204
205void sen(struct CORE *core) {
206 // Set Negative Flag
207 // N <- 1
208 // 1 cycle
209 core->sreg.N = 1;
210
211 // Update SREG
212 // Updated by instruction
213
214 inc_pc(core);
215}
216
217void cln(struct CORE *core) {
218 // Clear Negative Flag
219 // N <- 0
220 // 1 cycle
221 core->sreg.N = 0;
222
223 // Update SREG
224 // Updated by instruction
225
226 inc_pc(core);
227}
228
229void sez(struct CORE *core) {
230 // Set Zero Flag
231 // Z <- 1
232 // 1 cycle
233 core->sreg.Z = 1;
234
235 // Update SREG
236 // Updated by instruction
237
238 inc_pc(core);
239}
240
241void clz(struct CORE *core) {
242 // Clear Zero Flag
243 // Z <- 0
244 // 1 cycle
245 core->sreg.Z = 0;
246
247 // Update SREG
248 // Updated by instruction
249
250 inc_pc(core);
251}
252
253void sei(struct CORE *core) {
254 // Set Global Interupt Enable
255 // I <- 1
256 // 1 cycle
257 core->sreg.I = 1;
258
259 // Update SREG
260 // Updated by instruction
261
262 inc_pc(core);
263}
264
265void cli(struct CORE *core) {
266 // Clear Global Interupt Enable
267 // I <- 0
268 // 1 cycle
269 core->sreg.I = 0;
270
271 // Update SREG
272 // Updated by instruction
273
274 inc_pc(core);
275}
276
277void ses(struct CORE *core) {
278 // Set Signed Flag
279 // S <- 1
280 // 1 cycle
281 core->sreg.S = 1;
282
283 // Update SREG
284 // Updated by instruction
285
286 inc_pc(core);
287}
288
289void cls(struct CORE *core) {
290 // Clear Signed Flag
291 // S <- 0
292 // 1 cycle
293 core->sreg.S = 0;
294
295 // Update SREG
296 // Updated by instruction
297
298 inc_pc(core);
299}
300
301void sev(struct CORE *core) {
302 // Set Two's complement overflow flag
303 // V <- 1
304 // 1 cycle
305 core->sreg.V = 1;
306
307 // Update SREG
308 // Updated by instruction
309
310 inc_pc(core);
311}
312
313void clv(struct CORE *core) {
314 // Clear Two's complement overflow flag
315 // V <- 0
316 // 1 cycle
317 core->sreg.V = 0;
318
319 // Update SREG
320 // Updated by instruction
321
322 inc_pc(core);
323}
324
325void set(struct CORE *core) {
326 // Set T Flag
327 // T <- 1
328 // 1 cycle
329 core->sreg.T = 1;
330
331 // Update SREG
332 // Updated by instruction
333
334 inc_pc(core);
335}
336
337void clt(struct CORE *core) {
338 // Clear T Flag
339 // T <- 0
340 // 1 cycle
341 core->sreg.T = 0;
342
343 // Update SREG
344 // Updated by instruction
345
346 inc_pc(core);
347}
348
349void seh(struct CORE *core) {
350 // Set Half Cary Flag
351 // H <- 1
352 // 1 cycle
353 core->sreg.H = 1;
354
355 // Update SREG
356 // Updated by instruction
357
358 inc_pc(core);
359}
360
361void clh(struct CORE *core) {
362 // Clear Half Cary Flag
363 // H <- 0
364 // 1 cycle
365 core->sreg.H = 0;
366
367 // Update SREG
368 // Updated by instruction
369
370 inc_pc(core);
371}
void cli(struct CORE *core)
Clear Global Interrupt Flag.
Definition bit_test.c:265
void rol(uint8_t d, struct CORE *core)
Rotate Left through Carry.
Definition bit_test.c:41
void seh(struct CORE *core)
Set Half Carry Flag.
Definition bit_test.c:349
void bset(uint8_t s, struct CORE *core)
Set Bit in SREG.
Definition bit_test.c:157
void sei(struct CORE *core)
Set Global Interrupt Flag.
Definition bit_test.c:253
void lsr(uint8_t d, struct CORE *core)
Logical Shift Right.
Definition bit_test.c:23
void cbi(uint8_t IO, uint8_t n, struct CORE *core)
Clear Bit in I/O Register.
Definition bit_test.c:121
void set(struct CORE *core)
Set T Flag.
Definition bit_test.c:325
void sec(struct CORE *core)
Set Carry Flag.
Definition bit_test.c:181
void bld(uint8_t d, uint8_t b, struct CORE *core)
Bit Load from T Flag in SREG to Register.
Definition bit_test.c:145
void clc(struct CORE *core)
Clear Carry Flag.
Definition bit_test.c:193
void swap(uint8_t d, struct CORE *core)
Swap nibbles in a register.
Definition bit_test.c:97
void bclr(uint8_t s, struct CORE *core)
Clear Bit in SREG.
Definition bit_test.c:169
void lsl(uint8_t d, struct CORE *core)
Logical Shift Left.
Definition bit_test.c:3
void cls(struct CORE *core)
Clear Signed Flag.
Definition bit_test.c:289
void ror(uint8_t d, struct CORE *core)
Rotate Right through Carry.
Definition bit_test.c:61
void clh(struct CORE *core)
Clear Half Carry Flag.
Definition bit_test.c:361
void cln(struct CORE *core)
Clear Negative Flag.
Definition bit_test.c:217
void asr(uint8_t d, struct CORE *core)
Arithmetic Shift Right.
Definition bit_test.c:80
void clt(struct CORE *core)
Clear T Flag.
Definition bit_test.c:337
void sez(struct CORE *core)
Set Zero Flag.
Definition bit_test.c:229
void sen(struct CORE *core)
Set Negative Flag.
Definition bit_test.c:205
void clz(struct CORE *core)
Clear Zero Flag.
Definition bit_test.c:241
void clv(struct CORE *core)
Clear Overflow Flag.
Definition bit_test.c:313
void bst(uint8_t d, uint8_t b, struct CORE *core)
Bit Store from Register to T Flag in SREG.
Definition bit_test.c:133
void sbi(uint8_t IO, uint8_t n, struct CORE *core)
Set Bit in I/O Register.
Definition bit_test.c:109
void sev(struct CORE *core)
Set Overflow Flag.
Definition bit_test.c:301
void ses(struct CORE *core)
Set Signed Flag.
Definition bit_test.c:277
Bit testings and manipulations.
void inc_pc(struct CORE *core)
Definition core.c:3
void update_sreg_H(struct CORE *core, bool state)
Update the Half Carry flag (H) in the status register.
Definition sreg_utils.c:95
void update_sreg_S(struct CORE *core, bool state)
Update the Sign flag (S) in the status register.
Definition sreg_utils.c:91
void update_sreg_Z(struct CORE *core, bool state)
Update the Zero flag (Z) in the status register.
Definition sreg_utils.c:79
bool sreg_Z_compute_bool(uint8_t result)
Computes the Z flag as a boolean value.
Definition sreg_utils.c:7
void update_sreg_V(struct CORE *core, bool state)
Update the Overflow flag (V) in the status register.
Definition sreg_utils.c:87
void update_sreg_N(struct CORE *core, bool state)
Update the Negative flag (N) in the status register.
Definition sreg_utils.c:83
bool sreg_S_compute_bool(bool N, bool V)
Computes the S flag as a boolean value.
Definition sreg_utils.c:23
Core structure containing general purpose registers, program counter, stack pointer,...
Definition core.h:67
union GP gp
Definition core.h:68
union SREG sreg
Definition core.h:71
uint8_t R[32]
Definition core.h:29
bool N
Definition core.h:15
bool C
Definition core.h:13
bool V
Definition core.h:16
bool Z
Definition core.h:14
bool I
Definition core.h:20
bool H
Definition core.h:18
bool S
Definition core.h:17
uint8_t sreg
Definition core.h:11
bool T
Definition core.h:19