Avr8bit-emulator
An emulator for the Atmel AVR 8-bit microcontroller
Loading...
Searching...
No Matches
bit_test.h File Reference

Bit testings and manipulations. More...

#include <stdint.h>
#include <stdbool.h>
#include "core.h"
Include dependency graph for bit_test.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void lsl (uint8_t d, struct CORE *core)
 Logical Shift Left.
 
void lsr (uint8_t d, struct CORE *core)
 Logical Shift Right.
 
void rol (uint8_t d, struct CORE *core)
 Rotate Left through Carry.
 
void ror (uint8_t d, struct CORE *core)
 Rotate Right through Carry.
 
void asr (uint8_t d, struct CORE *core)
 Arithmetic Shift Right.
 
void swap (uint8_t d, struct CORE *core)
 Swap nibbles in a register.
 
void sbi (uint8_t IO, uint8_t n, struct CORE *core)
 Set Bit in I/O Register.
 
void cbi (uint8_t IO, uint8_t n, struct CORE *core)
 Clear Bit in I/O Register.
 
void bst (uint8_t d, uint8_t b, struct CORE *core)
 Bit Store from Register to T Flag in SREG.
 
void bld (uint8_t d, uint8_t b, struct CORE *core)
 Bit Load from T Flag in SREG to Register.
 
void bset (uint8_t s, struct CORE *core)
 Set Bit in SREG.
 
void bclr (uint8_t s, struct CORE *core)
 Clear Bit in SREG.
 
void sec (struct CORE *core)
 Set Carry Flag.
 
void clc (struct CORE *core)
 Clear Carry Flag.
 
void sen (struct CORE *core)
 Set Negative Flag.
 
void cln (struct CORE *core)
 Clear Negative Flag.
 
void sez (struct CORE *core)
 Set Zero Flag.
 
void clz (struct CORE *core)
 Clear Zero Flag.
 
void sei (struct CORE *core)
 Set Global Interrupt Flag.
 
void cli (struct CORE *core)
 Clear Global Interrupt Flag.
 
void ses (struct CORE *core)
 Set Signed Flag.
 
void cls (struct CORE *core)
 Clear Signed Flag.
 
void sev (struct CORE *core)
 Set Overflow Flag.
 
void clv (struct CORE *core)
 Clear Overflow Flag.
 
void set (struct CORE *core)
 Set T Flag.
 
void clt (struct CORE *core)
 Clear T Flag.
 
void seh (struct CORE *core)
 Set Half Carry Flag.
 
void clh (struct CORE *core)
 Clear Half Carry Flag.
 

Detailed Description

Bit testings and manipulations.

Contains all atomic bit testings and manipulations

Author
Antonin Pivard
Date
2024
Warning
Not tested

Definition in file bit_test.h.

Function Documentation

◆ asr()

void asr ( uint8_t  d,
struct CORE core 
)

Arithmetic Shift Right.

Parameters
dThe register to be shifted.
corePointer to the CORE structure.

Definition at line 80 of file bit_test.c.

80 {
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}
void inc_pc(struct CORE *core)
Definition core.c:3
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
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

References SREG::C, CORE::gp, inc_pc(), SREG::N, GP::R, CORE::sreg, sreg_S_compute_bool(), sreg_Z_compute_bool(), update_sreg_N(), update_sreg_S(), update_sreg_V(), update_sreg_Z(), and SREG::V.

◆ bclr()

void bclr ( uint8_t  s,
struct CORE core 
)

Clear Bit in SREG.

Parameters
sThe bit to be cleared.
corePointer to the CORE structure.

Definition at line 169 of file bit_test.c.

169 {
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}
uint8_t sreg
Definition core.h:11

References inc_pc(), SREG::sreg, and CORE::sreg.

◆ bld()

void bld ( uint8_t  d,
uint8_t  b,
struct CORE core 
)

Bit Load from T Flag in SREG to Register.

Parameters
dThe register.
bThe bit to be loaded.
corePointer to the CORE structure.

Definition at line 145 of file bit_test.c.

145 {
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}
bool T
Definition core.h:19

References CORE::gp, inc_pc(), GP::R, CORE::sreg, and SREG::T.

◆ bset()

void bset ( uint8_t  s,
struct CORE core 
)

Set Bit in SREG.

Parameters
sThe bit to be set.
corePointer to the CORE structure.

Definition at line 157 of file bit_test.c.

157 {
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}

References inc_pc(), SREG::sreg, and CORE::sreg.

◆ bst()

void bst ( uint8_t  d,
uint8_t  b,
struct CORE core 
)

Bit Store from Register to T Flag in SREG.

Parameters
dThe register.
bThe bit to be stored.
corePointer to the CORE structure.

Definition at line 133 of file bit_test.c.

133 {
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}

References CORE::gp, inc_pc(), GP::R, CORE::sreg, and SREG::T.

◆ cbi()

void cbi ( uint8_t  IO,
uint8_t  n,
struct CORE core 
)

Clear Bit in I/O Register.

Parameters
IOThe I/O register.
nThe bit to be cleared.
corePointer to the CORE structure.

Definition at line 121 of file bit_test.c.

121 {
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}

References CORE::gp, inc_pc(), and GP::R.

◆ clc()

void clc ( struct CORE core)

Clear Carry Flag.

Parameters
corePointer to the CORE structure.

Definition at line 193 of file bit_test.c.

193 {
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}

References SREG::C, inc_pc(), and CORE::sreg.

◆ clh()

void clh ( struct CORE core)

Clear Half Carry Flag.

Parameters
corePointer to the CORE structure.

Definition at line 361 of file bit_test.c.

361 {
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}
bool H
Definition core.h:18

References SREG::H, inc_pc(), and CORE::sreg.

◆ cli()

void cli ( struct CORE core)

Clear Global Interrupt Flag.

Parameters
corePointer to the CORE structure.

Definition at line 265 of file bit_test.c.

265 {
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}
bool I
Definition core.h:20

References SREG::I, inc_pc(), and CORE::sreg.

◆ cln()

void cln ( struct CORE core)

Clear Negative Flag.

Parameters
corePointer to the CORE structure.

Definition at line 217 of file bit_test.c.

217 {
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}

References inc_pc(), SREG::N, and CORE::sreg.

◆ cls()

void cls ( struct CORE core)

Clear Signed Flag.

Parameters
corePointer to the CORE structure.

Definition at line 289 of file bit_test.c.

289 {
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}
bool S
Definition core.h:17

References inc_pc(), SREG::S, and CORE::sreg.

◆ clt()

void clt ( struct CORE core)

Clear T Flag.

Parameters
corePointer to the CORE structure.

Definition at line 337 of file bit_test.c.

337 {
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}

References inc_pc(), CORE::sreg, and SREG::T.

◆ clv()

void clv ( struct CORE core)

Clear Overflow Flag.

Parameters
corePointer to the CORE structure.

Definition at line 313 of file bit_test.c.

313 {
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}

References inc_pc(), CORE::sreg, and SREG::V.

◆ clz()

void clz ( struct CORE core)

Clear Zero Flag.

Parameters
corePointer to the CORE structure.

Definition at line 241 of file bit_test.c.

241 {
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}
bool Z
Definition core.h:14

References inc_pc(), CORE::sreg, and SREG::Z.

◆ lsl()

void lsl ( uint8_t  d,
struct CORE core 
)

Logical Shift Left.

Parameters
dThe register to be shifted.
corePointer to the CORE structure.

Definition at line 3 of file bit_test.c.

3 {
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}
void update_sreg_H(struct CORE *core, bool state)
Update the Half Carry flag (H) in the status register.
Definition sreg_utils.c:95

References SREG::C, CORE::gp, inc_pc(), SREG::N, GP::R, CORE::sreg, sreg_S_compute_bool(), sreg_Z_compute_bool(), update_sreg_H(), update_sreg_N(), update_sreg_S(), update_sreg_V(), update_sreg_Z(), and SREG::V.

◆ lsr()

void lsr ( uint8_t  d,
struct CORE core 
)

Logical Shift Right.

Parameters
dThe register to be shifted.
corePointer to the CORE structure.

Definition at line 23 of file bit_test.c.

23 {
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}

References SREG::C, CORE::gp, inc_pc(), SREG::N, GP::R, CORE::sreg, sreg_S_compute_bool(), sreg_Z_compute_bool(), update_sreg_N(), update_sreg_S(), update_sreg_V(), update_sreg_Z(), and SREG::V.

◆ rol()

void rol ( uint8_t  d,
struct CORE core 
)

Rotate Left through Carry.

Parameters
dThe register to be rotated.
corePointer to the CORE structure.

Definition at line 41 of file bit_test.c.

41 {
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}

References SREG::C, CORE::gp, inc_pc(), SREG::N, GP::R, CORE::sreg, sreg_S_compute_bool(), sreg_Z_compute_bool(), update_sreg_H(), update_sreg_N(), update_sreg_S(), update_sreg_V(), update_sreg_Z(), and SREG::V.

◆ ror()

void ror ( uint8_t  d,
struct CORE core 
)

Rotate Right through Carry.

Parameters
dThe register to be rotated.
corePointer to the CORE structure.

Definition at line 61 of file bit_test.c.

61 {
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}

References SREG::C, CORE::gp, inc_pc(), SREG::N, GP::R, CORE::sreg, sreg_S_compute_bool(), sreg_Z_compute_bool(), update_sreg_N(), update_sreg_S(), update_sreg_V(), update_sreg_Z(), and SREG::V.

◆ sbi()

void sbi ( uint8_t  IO,
uint8_t  n,
struct CORE core 
)

Set Bit in I/O Register.

Parameters
IOThe I/O register.
nThe bit to be set.
corePointer to the CORE structure.

Definition at line 109 of file bit_test.c.

109 {
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}

References CORE::gp, inc_pc(), and GP::R.

◆ sec()

void sec ( struct CORE core)

Set Carry Flag.

Parameters
corePointer to the CORE structure.

Definition at line 181 of file bit_test.c.

181 {
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}

References SREG::C, inc_pc(), and CORE::sreg.

◆ seh()

void seh ( struct CORE core)

Set Half Carry Flag.

Parameters
corePointer to the CORE structure.

Definition at line 349 of file bit_test.c.

349 {
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}

References SREG::H, inc_pc(), and CORE::sreg.

◆ sei()

void sei ( struct CORE core)

Set Global Interrupt Flag.

Parameters
corePointer to the CORE structure.

Definition at line 253 of file bit_test.c.

253 {
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}

References SREG::I, inc_pc(), and CORE::sreg.

◆ sen()

void sen ( struct CORE core)

Set Negative Flag.

Parameters
corePointer to the CORE structure.

Definition at line 205 of file bit_test.c.

205 {
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}

References inc_pc(), SREG::N, and CORE::sreg.

◆ ses()

void ses ( struct CORE core)

Set Signed Flag.

Parameters
corePointer to the CORE structure.

Definition at line 277 of file bit_test.c.

277 {
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}

References inc_pc(), SREG::S, and CORE::sreg.

◆ set()

void set ( struct CORE core)

Set T Flag.

Parameters
corePointer to the CORE structure.

Definition at line 325 of file bit_test.c.

325 {
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}

References inc_pc(), CORE::sreg, and SREG::T.

◆ sev()

void sev ( struct CORE core)

Set Overflow Flag.

Parameters
corePointer to the CORE structure.

Definition at line 301 of file bit_test.c.

301 {
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}

References inc_pc(), CORE::sreg, and SREG::V.

◆ sez()

void sez ( struct CORE core)

Set Zero Flag.

Parameters
corePointer to the CORE structure.

Definition at line 229 of file bit_test.c.

229 {
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}

References inc_pc(), CORE::sreg, and SREG::Z.

◆ swap()

void swap ( uint8_t  d,
struct CORE core 
)

Swap nibbles in a register.

Parameters
dThe register to be swapped.
corePointer to the CORE structure.

Definition at line 97 of file bit_test.c.

97 {
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}

References CORE::gp, inc_pc(), and GP::R.