Avr8bit-emulator
An emulator for the Atmel AVR 8-bit microcontroller
Loading...
Searching...
No Matches
sreg_utils.c
Go to the documentation of this file.
1#include "sreg_utils.h"
2
3bool sreg_H_compute_bool(uint8_t Rd, uint8_t Rr) {
4 return ((Rd & 0x0F) + (Rr & 0x0F) > 0x0F);
5}
6
7bool sreg_Z_compute_bool(uint8_t result) {
8 return (result == 0);
9}
10
11bool sreg_C_compute_bool(uint8_t Rd, uint8_t Rr) {
12 return ((uint16_t)Rd + (uint16_t)Rr > 0xFF);
13}
14
15bool sreg_N_compute_bool(uint8_t result) {
16 return (result & 0x80);
17}
18
19bool sreg_V_compute_bool(uint8_t Rd, uint8_t Rr, uint8_t result) {
20 return ((Rd & Rr & ~result) | (~Rd & ~Rr & result));
21}
22
23bool sreg_S_compute_bool(bool N, bool V) {
24 return (N ^ V);
25}
26
27uint8_t sreg_C_compute(uint8_t Rd, uint8_t Rr) {
28 return ((uint16_t)Rd + (uint16_t)Rr > 0xFF);
29}
30
31uint8_t sreg_Z_compute(uint8_t result) {
32 return (result == 0 ? 2 : 0);
33}
34
35uint8_t sreg_N_compute(uint8_t result) {
36 return (result & 0x80 ? 4 : 0);
37}
38
39uint8_t sreg_V_compute(uint8_t Rd, uint8_t Rr, uint8_t result) {
40 return ((Rd & Rr & ~result) | (~Rd & ~Rr & result) ? 8 : 0);
41}
42
43uint8_t sreg_S_compute(bool N, bool V) {
44 return (N ^ V ? 16 : 0);
45}
46
47uint8_t sreg_H_compute(uint8_t Rd, uint8_t Rr) {
48 return ((Rd & 0x0F) + (Rr & 0x0F) > 0x0F ? 32 : 0);
49}
50
51void update_sreg_arithm(struct CORE *core, uint8_t Rd, uint8_t Rr, uint8_t result) {
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}
59
60void update_sreg_logic(struct CORE *core, uint8_t result) {
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}
66
67void udpate_sreg_arithm_16bit(struct CORE *core, uint16_t Rd, uint16_t result) {
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}
74
75void update_sreg_C(struct CORE *core, bool state) {
76 core->sreg.C = state;
77}
78
79void update_sreg_Z(struct CORE *core, bool state) {
80 core->sreg.Z = state;
81}
82
83void update_sreg_N(struct CORE *core, bool state) {
84 core->sreg.N = state;
85}
86
87void update_sreg_V(struct CORE *core, bool state) {
88 core->sreg.V = state;
89}
90
91void update_sreg_S(struct CORE *core, bool state) {
92 core->sreg.S = state;
93}
94
95void update_sreg_H(struct CORE *core, bool state) {
96 core->sreg.H = state;
97}
98
99void update_sreg_T(struct CORE *core, bool state) {
100 core->sreg.T = state;
101}
102
103void update_sreg_I(struct CORE *core, bool state) {
104 core->sreg.I = state;
105}
bool sreg_V_compute_bool(uint8_t Rd, uint8_t Rr, uint8_t result)
Computes the V flag as a boolean value.
Definition sreg_utils.c:19
bool sreg_N_compute_bool(uint8_t result)
Computes the N flag as a boolean value.
Definition sreg_utils.c:15
uint8_t sreg_Z_compute(uint8_t result)
Computes the Z flag as an 8-bit value.
Definition sreg_utils.c:31
bool sreg_H_compute_bool(uint8_t Rd, uint8_t Rr)
Computes the H flag as a boolean value.
Definition sreg_utils.c:3
bool sreg_C_compute_bool(uint8_t Rd, uint8_t Rr)
Computes the C flag as a boolean value.
Definition sreg_utils.c:11
void update_sreg_T(struct CORE *core, bool state)
Update the Transfer bit (T) in the status register.
Definition sreg_utils.c:99
void update_sreg_I(struct CORE *core, bool state)
Update the Interrupt flag (I) in the status register.
Definition sreg_utils.c:103
void update_sreg_arithm(struct CORE *core, uint8_t Rd, uint8_t Rr, uint8_t result)
Updates the Status Register (SREG) for arithmetic operations.
Definition sreg_utils.c:51
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_logic(struct CORE *core, uint8_t result)
Updates the Status Register (SREG) for logical operations.
Definition sreg_utils.c:60
void udpate_sreg_arithm_16bit(struct CORE *core, uint16_t Rd, uint16_t result)
Updates the Status Register (SREG) for 16-bit arithmetic operations.
Definition sreg_utils.c:67
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
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
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
uint8_t sreg_S_compute(bool N, bool V)
Computes the S flag as an 8-bit value.
Definition sreg_utils.c:43
bool sreg_S_compute_bool(bool N, bool V)
Computes the S flag as a boolean value.
Definition sreg_utils.c:23
uint8_t sreg_N_compute(uint8_t result)
Computes the N flag as an 8-bit value.
Definition sreg_utils.c:35
void update_sreg_C(struct CORE *core, bool state)
Update the Carry flag (C) in the status register.
Definition sreg_utils.c:75
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
Functions for manipulating the Status Register (SREG)
Core structure containing general purpose registers, program counter, stack pointer,...
Definition core.h:67
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 I
Definition core.h:20
bool H
Definition core.h:18
bool S
Definition core.h:17
bool T
Definition core.h:19