Avr8bit-emulator
An emulator for the Atmel AVR 8-bit microcontroller
Loading...
Searching...
No Matches
sreg_utils.c File Reference
#include "sreg_utils.h"
Include dependency graph for sreg_utils.c:

Go to the source code of this file.

Functions

bool sreg_H_compute_bool (uint8_t Rd, uint8_t Rr)
 Computes the H flag as a boolean value.
 
bool sreg_Z_compute_bool (uint8_t result)
 Computes the Z flag as a boolean value.
 
bool sreg_C_compute_bool (uint8_t Rd, uint8_t Rr)
 Computes the C flag as a boolean value.
 
bool sreg_N_compute_bool (uint8_t result)
 Computes the N flag as a boolean value.
 
bool sreg_V_compute_bool (uint8_t Rd, uint8_t Rr, uint8_t result)
 Computes the V flag as a boolean value.
 
bool sreg_S_compute_bool (bool N, bool V)
 Computes the S flag as a boolean value.
 
uint8_t sreg_C_compute (uint8_t Rd, uint8_t Rr)
 Computes the C flag as an 8-bit value.
 
uint8_t sreg_Z_compute (uint8_t result)
 Computes the Z flag as an 8-bit value.
 
uint8_t sreg_N_compute (uint8_t result)
 Computes the N flag as an 8-bit value.
 
uint8_t sreg_V_compute (uint8_t Rd, uint8_t Rr, uint8_t result)
 Computes the V flag as an 8-bit value.
 
uint8_t sreg_S_compute (bool N, bool V)
 Computes the S flag as an 8-bit value.
 
uint8_t sreg_H_compute (uint8_t Rd, uint8_t Rr)
 Computes the H flag as an 8-bit value.
 
void update_sreg_arithm (struct CORE *core, uint8_t Rd, uint8_t Rr, uint8_t result)
 Updates the Status Register (SREG) for arithmetic operations.
 
void update_sreg_logic (struct CORE *core, uint8_t result)
 Updates the Status Register (SREG) for logical operations.
 
void udpate_sreg_arithm_16bit (struct CORE *core, uint16_t Rd, uint16_t result)
 Updates the Status Register (SREG) for 16-bit arithmetic operations.
 
void update_sreg_C (struct CORE *core, bool state)
 Update the Carry flag (C) in the status register.
 
void update_sreg_Z (struct CORE *core, bool state)
 Update the Zero flag (Z) in the status register.
 
void update_sreg_N (struct CORE *core, bool state)
 Update the Negative flag (N) in the status register.
 
void update_sreg_V (struct CORE *core, bool state)
 Update the Overflow flag (V) in the status register.
 
void update_sreg_S (struct CORE *core, bool state)
 Update the Sign flag (S) in the status register.
 
void update_sreg_H (struct CORE *core, bool state)
 Update the Half Carry flag (H) in the status register.
 
void update_sreg_T (struct CORE *core, bool state)
 Update the Transfer bit (T) in the status register.
 
void update_sreg_I (struct CORE *core, bool state)
 Update the Interrupt flag (I) in the status register.
 

Function Documentation

◆ sreg_C_compute()

uint8_t sreg_C_compute ( uint8_t  Rd,
uint8_t  Rr 
)

Computes the C flag as an 8-bit value.

Parameters
RdThe value of the destination register.
RrThe value of the source register.
Returns
The computed C flag.

Definition at line 27 of file sreg_utils.c.

27 {
28 return ((uint16_t)Rd + (uint16_t)Rr > 0xFF);
29}

Referenced by update_sreg_arithm().

◆ sreg_C_compute_bool()

bool sreg_C_compute_bool ( uint8_t  Rd,
uint8_t  Rr 
)

Computes the C flag as a boolean value.

Parameters
RdThe value of the destination register.
RrThe value of the source register.
Returns
true if the C flag should be set, false otherwise.

Definition at line 11 of file sreg_utils.c.

11 {
12 return ((uint16_t)Rd + (uint16_t)Rr > 0xFF);
13}

◆ sreg_H_compute()

uint8_t sreg_H_compute ( uint8_t  Rd,
uint8_t  Rr 
)

Computes the H flag as an 8-bit value.

Parameters
RdThe value of the destination register.
RrThe value of the source register.
Returns
The computed H flag.

Definition at line 47 of file sreg_utils.c.

47 {
48 return ((Rd & 0x0F) + (Rr & 0x0F) > 0x0F ? 32 : 0);
49}

Referenced by update_sreg_arithm().

◆ sreg_H_compute_bool()

bool sreg_H_compute_bool ( uint8_t  Rd,
uint8_t  Rr 
)

Computes the H flag as a boolean value.

Parameters
RdThe value of the destination register.
RrThe value of the source register.
Returns
true if the H flag should be set, false otherwise.

Definition at line 3 of file sreg_utils.c.

3 {
4 return ((Rd & 0x0F) + (Rr & 0x0F) > 0x0F);
5}

◆ sreg_N_compute()

uint8_t sreg_N_compute ( uint8_t  result)

Computes the N flag as an 8-bit value.

Parameters
resultThe result of the operation.
Returns
The computed N flag.

Definition at line 35 of file sreg_utils.c.

35 {
36 return (result & 0x80 ? 4 : 0);
37}

Referenced by update_sreg_arithm(), and update_sreg_logic().

◆ sreg_N_compute_bool()

bool sreg_N_compute_bool ( uint8_t  result)

Computes the N flag as a boolean value.

Parameters
resultThe result of the operation.
Returns
true if the N flag should be set, false otherwise.

Definition at line 15 of file sreg_utils.c.

15 {
16 return (result & 0x80);
17}

◆ sreg_S_compute()

uint8_t sreg_S_compute ( bool  N,
bool  V 
)

Computes the S flag as an 8-bit value.

Parameters
NThe value of the N flag.
VThe value of the V flag.
Returns
The computed S flag.

Definition at line 43 of file sreg_utils.c.

43 {
44 return (N ^ V ? 16 : 0);
45}

Referenced by udpate_sreg_arithm_16bit(), update_sreg_arithm(), and update_sreg_logic().

◆ sreg_S_compute_bool()

bool sreg_S_compute_bool ( bool  N,
bool  V 
)

Computes the S flag as a boolean value.

Parameters
NThe value of the N flag.
VThe value of the V flag.
Returns
true if the S flag should be set, false otherwise.

Definition at line 23 of file sreg_utils.c.

23 {
24 return (N ^ V);
25}

Referenced by asr(), lsl(), lsr(), rol(), and ror().

◆ sreg_V_compute()

uint8_t sreg_V_compute ( uint8_t  Rd,
uint8_t  Rr,
uint8_t  result 
)

Computes the V flag as an 8-bit value.

Parameters
RdThe value of the destination register.
RrThe value of the source register.
resultThe result of the operation.
Returns
The computed V flag.

Definition at line 39 of file sreg_utils.c.

39 {
40 return ((Rd & Rr & ~result) | (~Rd & ~Rr & result) ? 8 : 0);
41}

Referenced by update_sreg_arithm().

◆ sreg_V_compute_bool()

bool sreg_V_compute_bool ( uint8_t  Rd,
uint8_t  Rr,
uint8_t  result 
)

Computes the V flag as a boolean value.

Parameters
RdThe value of the destination register.
RrThe value of the source register.
resultThe result of the operation.
Returns
true if the V flag should be set, false otherwise.

Definition at line 19 of file sreg_utils.c.

19 {
20 return ((Rd & Rr & ~result) | (~Rd & ~Rr & result));
21}

◆ sreg_Z_compute()

uint8_t sreg_Z_compute ( uint8_t  result)

Computes the Z flag as an 8-bit value.

Parameters
resultThe result of the operation.
Returns
The computed Z flag.

Definition at line 31 of file sreg_utils.c.

31 {
32 return (result == 0 ? 2 : 0);
33}

Referenced by update_sreg_arithm(), and update_sreg_logic().

◆ sreg_Z_compute_bool()

bool sreg_Z_compute_bool ( uint8_t  result)

Computes the Z flag as a boolean value.

Parameters
resultThe result of the operation.
Returns
true if the Z flag should be set, false otherwise.

Definition at line 7 of file sreg_utils.c.

7 {
8 return (result == 0);
9}

Referenced by asr(), lsl(), lsr(), rol(), and ror().

◆ udpate_sreg_arithm_16bit()

void udpate_sreg_arithm_16bit ( struct CORE core,
uint16_t  Rd,
uint16_t  result 
)

Updates the Status Register (SREG) for 16-bit arithmetic operations.

This function updates the SREG based on the result of a 16-bit arithmetic operation.

Parameters
corePointer to the CORE structure.
RdThe destination register value.
resultThe result of the 16-bit arithmetic operation.

Definition at line 67 of file sreg_utils.c.

67 {
68 core->sreg.C = (!(Rd>>15) & (result>>15));
69 core->sreg.Z = result == 0 ? 1 : 0;
70 core->sreg.N = result >> 15;
71 core->sreg.V = (!(Rd>>15) & (result>>15));
72 core->sreg.S = sreg_S_compute(core->sreg.N, core->sreg.V);
73}
uint8_t sreg_S_compute(bool N, bool V)
Computes the S flag as an 8-bit value.
Definition sreg_utils.c:43
union SREG sreg
Definition core.h:71
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 S
Definition core.h:17

References SREG::C, SREG::N, SREG::S, CORE::sreg, sreg_S_compute(), SREG::V, and SREG::Z.

Referenced by adiw(), and sbiw().

◆ update_sreg_arithm()

void update_sreg_arithm ( struct CORE core,
uint8_t  Rd,
uint8_t  Rr,
uint8_t  result 
)

Updates the Status Register (SREG) for arithmetic operations.

This function updates the SREG based on the result of an arithmetic operation.

Parameters
corePointer to the CORE structure.
RdThe destination register value.
RrThe source register value.
resultThe result of the arithmetic operation.

Definition at line 51 of file sreg_utils.c.

51 {
52 core->sreg.C = sreg_C_compute(Rd, Rr);
53 core->sreg.Z = sreg_Z_compute(result);
54 core->sreg.N = sreg_N_compute(result);
55 core->sreg.V = sreg_V_compute(Rd, Rr, result);
56 core->sreg.S = sreg_S_compute(core->sreg.N, core->sreg.V);
57 core->sreg.H = sreg_H_compute(Rd, Rr);
58}
uint8_t sreg_Z_compute(uint8_t result)
Computes the Z flag as an 8-bit value.
Definition sreg_utils.c:31
uint8_t sreg_V_compute(uint8_t Rd, uint8_t Rr, uint8_t result)
Computes the V flag as an 8-bit value.
Definition sreg_utils.c:39
uint8_t sreg_H_compute(uint8_t Rd, uint8_t Rr)
Computes the H flag as an 8-bit value.
Definition sreg_utils.c:47
uint8_t sreg_N_compute(uint8_t result)
Computes the N flag as an 8-bit value.
Definition sreg_utils.c:35
uint8_t sreg_C_compute(uint8_t Rd, uint8_t Rr)
Computes the C flag as an 8-bit value.
Definition sreg_utils.c:27
bool H
Definition core.h:18

References SREG::C, SREG::H, SREG::N, SREG::S, CORE::sreg, sreg_C_compute(), sreg_H_compute(), sreg_N_compute(), sreg_S_compute(), sreg_V_compute(), sreg_Z_compute(), SREG::V, and SREG::Z.

Referenced by adc(), add(), cp(), cpc(), cpi(), sbc(), sbci(), sub(), and subi().

◆ update_sreg_C()

void update_sreg_C ( struct CORE core,
bool  state 
)

Update the Carry flag (C) in the status register.

Parameters
corePointer to the CORE structure.
stateBoolean state to set the Carry flag.

Definition at line 75 of file sreg_utils.c.

75 {
76 core->sreg.C = state;
77}

References SREG::C, and CORE::sreg.

Referenced by com().

◆ update_sreg_H()

void update_sreg_H ( struct CORE core,
bool  state 
)

Update the Half Carry flag (H) in the status register.

Parameters
corePointer to the CORE structure.
stateBoolean state to set the Half Carry flag.

Definition at line 95 of file sreg_utils.c.

95 {
96 core->sreg.H = state;
97}

References SREG::H, and CORE::sreg.

Referenced by lsl(), and rol().

◆ update_sreg_I()

void update_sreg_I ( struct CORE core,
bool  state 
)

Update the Interrupt flag (I) in the status register.

Parameters
corePointer to the CORE structure.
stateBoolean state to set the Interrupt flag.

Definition at line 103 of file sreg_utils.c.

103 {
104 core->sreg.I = state;
105}
bool I
Definition core.h:20

References SREG::I, and CORE::sreg.

Referenced by reti().

◆ update_sreg_logic()

void update_sreg_logic ( struct CORE core,
uint8_t  result 
)

Updates the Status Register (SREG) for logical operations.

This function updates the SREG based on the result of a logical operation.

Parameters
corePointer to the CORE structure.
resultThe result of the logical operation.

Definition at line 60 of file sreg_utils.c.

60 {
61 core->sreg.Z = sreg_Z_compute(result);
62 core->sreg.N = sreg_N_compute(result);
63 core->sreg.V = 0;
64 core->sreg.S = sreg_S_compute(core->sreg.N, core->sreg.V);
65}

References SREG::N, SREG::S, CORE::sreg, sreg_N_compute(), sreg_S_compute(), sreg_Z_compute(), SREG::V, and SREG::Z.

Referenced by and(), andi(), com(), eor(), or(), and ori().

◆ update_sreg_N()

void update_sreg_N ( struct CORE core,
bool  state 
)

Update the Negative flag (N) in the status register.

Parameters
corePointer to the CORE structure.
stateBoolean state to set the Negative flag.

Definition at line 83 of file sreg_utils.c.

83 {
84 core->sreg.N = state;
85}

References SREG::N, and CORE::sreg.

Referenced by asr(), lsl(), lsr(), rol(), and ror().

◆ update_sreg_S()

void update_sreg_S ( struct CORE core,
bool  state 
)

Update the Sign flag (S) in the status register.

Parameters
corePointer to the CORE structure.
stateBoolean state to set the Sign flag.

Definition at line 91 of file sreg_utils.c.

91 {
92 core->sreg.S = state;
93}

References SREG::S, and CORE::sreg.

Referenced by asr(), lsl(), lsr(), rol(), and ror().

◆ update_sreg_T()

void update_sreg_T ( struct CORE core,
bool  state 
)

Update the Transfer bit (T) in the status register.

Parameters
corePointer to the CORE structure.
stateBoolean state to set the Transfer bit.

Definition at line 99 of file sreg_utils.c.

99 {
100 core->sreg.T = state;
101}
bool T
Definition core.h:19

References CORE::sreg, and SREG::T.

◆ update_sreg_V()

void update_sreg_V ( struct CORE core,
bool  state 
)

Update the Overflow flag (V) in the status register.

Parameters
corePointer to the CORE structure.
stateBoolean state to set the Overflow flag.

Definition at line 87 of file sreg_utils.c.

87 {
88 core->sreg.V = state;
89}

References CORE::sreg, and SREG::V.

Referenced by asr(), lsl(), lsr(), rol(), and ror().

◆ update_sreg_Z()

void update_sreg_Z ( struct CORE core,
bool  state 
)

Update the Zero flag (Z) in the status register.

Parameters
corePointer to the CORE structure.
stateBoolean state to set the Zero flag.

Definition at line 79 of file sreg_utils.c.

79 {
80 core->sreg.Z = state;
81}

References CORE::sreg, and SREG::Z.

Referenced by asr(), lsl(), lsr(), rol(), and ror().