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

Arithmetic and logic operations. More...

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

Go to the source code of this file.

Functions

void add (uint8_t d, uint8_t r, struct CORE *core)
 Add without carry.
 
void adc (uint8_t d, uint8_t r, struct CORE *core)
 Add with carry.
 
void adiw (uint8_t d, uint8_t K, struct CORE *core)
 Add immediate to word.
 
void sub (uint8_t d, uint8_t r, struct CORE *core)
 Subtract.
 
void subi (uint8_t d, uint8_t K, struct CORE *core)
 Subtract immediate.
 
void sbc (uint8_t d, uint8_t r, struct CORE *core)
 Subtract with carry.
 
void sbci (uint8_t d, uint8_t K, struct CORE *core)
 Subtract immediate with carry.
 
void sbiw (uint8_t d, uint8_t K, struct CORE *core)
 Subtract immediate from word.
 
void and (uint8_t d, uint8_t r, struct CORE *core)
 Logical AND.
 
void andi (uint8_t d, uint8_t K, struct CORE *core)
 Logical AND with immediate.
 
void or (uint8_t d, uint8_t r, struct CORE *core)
 Logical OR.
 
void ori (uint8_t d, uint8_t K, struct CORE *core)
 Logical OR with immediate.
 
void eor (uint8_t d, uint8_t r, struct CORE *core)
 Perform bitwise exclusive OR between two registers.
 
void com (uint8_t d, struct CORE *core)
 Perform one's complement on a register.
 
void neg (uint8_t d, struct CORE *core)
 Perform two's complement (negation) on a register.
 
void sbr (uint8_t d, uint8_t K, struct CORE *core)
 Set bits in a register.
 
void cbr (uint8_t d, uint8_t K, struct CORE *core)
 Clear bits in a register.
 
void inc (uint8_t d, struct CORE *core)
 Increment the value of a register.
 
void dec (uint8_t d, struct CORE *core)
 Decrement the value of a register.
 
void tst (uint8_t d, struct CORE *core)
 Test for zero or minus after AND operation.
 
void clr (uint8_t d, struct CORE *core)
 Clear a register (set to zero).
 
void ser (uint8_t d, struct CORE *core)
 Set all bits in a register.
 
void mul (uint8_t d, uint8_t r, struct CORE *core)
 Multiplies two unsigned 8-bit integers and stores the result in the CORE structure.
 
void muls (uint8_t d, uint8_t r, struct CORE *core)
 Multiplies two signed 8-bit integers and stores the result in the CORE structure.
 
void mulsu (uint8_t d, uint8_t r, struct CORE *core)
 Multiplies a signed 8-bit integer with an unsigned 8-bit integer and stores the result in the CORE structure.
 
void fmul (uint8_t d, uint8_t r, struct CORE *core)
 Multiplies two unsigned 8-bit integers with fractional representation and stores the result in the CORE structure.
 
void fmuls (uint8_t d, uint8_t r, struct CORE *core)
 Multiplies two signed 8-bit integers with fractional representation and stores the result in the CORE structure.
 
void fmulsu (uint8_t d, uint8_t r, struct CORE *core)
 Multiplies a signed 8-bit integer with an unsigned 8-bit integer with fractional representation and stores the result in the CORE structure.
 

Detailed Description

Arithmetic and logic operations.

Contains all atomic arithmetic and logic operations

Author
Antonin Pivard
Date
2024
Warning
Not tested

Definition in file arithm_logic.h.

Function Documentation

◆ adc()

void adc ( uint8_t  d,
uint8_t  r,
struct CORE core 
)

Add with carry.

Parameters
dDestination register.
rSource register.
corePointer to the CORE structure.

Definition at line 19 of file arithm_logic.c.

19 {
20 // Add with cary
21 // Rd <- Rd + Rr + C
22 // 1 cycle
23
24 uint8_t R_tmp = core->gp.R[d];
25
26 // Execute instruction
27 core->gp.R[d] = core->gp.R[d] + core->gp.R[r] + core->sreg.C;
28
29 // Update SREG
30 update_sreg_arithm(core, R_tmp, core->gp.R[r], core->gp.R[d]);
31
32 inc_pc(core);
33}
void inc_pc(struct CORE *core)
Definition core.c:3
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
union GP gp
Definition core.h:68
union SREG sreg
Definition core.h:71
uint8_t R[32]
Definition core.h:29
bool C
Definition core.h:13

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

◆ add()

void add ( uint8_t  d,
uint8_t  r,
struct CORE core 
)

Add without carry.

Parameters
dDestination register index
rSource register index
corePointer to the CORE structure

Definition at line 3 of file arithm_logic.c.

3 {
4 // Add without carry
5 // Rd <- Rd + Rr
6 // 1 cycle
7
8 uint8_t R_tmp = core->gp.R[d];
9
10 // Execute instruction
11 core->gp.R[d] = core->gp.R[d] + core->gp.R[r];
12
13 // Update SREG
14 update_sreg_arithm(core, R_tmp, core->gp.R[r], core->gp.R[d]);
15
16 inc_pc(core);
17}

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

◆ adiw()

void adiw ( uint8_t  d,
uint8_t  K,
struct CORE core 
)

Add immediate to word.

Parameters
dDestination register.
KImmediate value.
corePointer to the CORE structure.

Definition at line 35 of file arithm_logic.c.

35 {
36 // Add immediate to word
37 // Rd:Rd+1 <- Rd:Rd+1 + K
38 // 2 cycle
39
40 // Execute instruction
41 uint16_t Rd = core->gp.R[d] | (core->gp.R[d + 1] << 8);
42 uint16_t result = Rd + K;
43 core->gp.R[d] = result & 0xFF;
44 core->gp.R[d + 1] = (result >> 8) & 0xFF;
45
46 // Update SREG
47 udpate_sreg_arithm_16bit(core, Rd, result);
48
49 inc_pc(core);
50}
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

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

◆ and()

void and ( uint8_t  d,
uint8_t  r,
struct CORE core 
)

Logical AND.

Parameters
dDestination register.
rSource register.
corePointer to the CORE structure.

Definition at line 133 of file arithm_logic.c.

133 {
134 // Logical AND
135 // Rd <- Rd & Rr
136 // 1 cycle
137
138 // Execute instruction
139 core->gp.R[d] = core->gp.R[d] & core->gp.R[r];
140
141 // Update SREG
142 update_sreg_logic(core, core->gp.R[d]);
143
144 inc_pc(core);
145}
void update_sreg_logic(struct CORE *core, uint8_t result)
Updates the Status Register (SREG) for logical operations.
Definition sreg_utils.c:60

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

◆ andi()

void andi ( uint8_t  d,
uint8_t  K,
struct CORE core 
)

Logical AND with immediate.

Parameters
dDestination register.
KImmediate value.
corePointer to the CORE structure.

Definition at line 147 of file arithm_logic.c.

147 {
148 // Logical AND with immediate
149 // Rd <- Rd & K
150 // 1 cycle
151
152 // Execute instruction
153 core->gp.R[d] = core->gp.R[d] & K;
154
155 // Update SREG
156 update_sreg_logic(core, core->gp.R[d]);
157
158 inc_pc(core);
159}

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

◆ cbr()

void cbr ( uint8_t  d,
uint8_t  K,
struct CORE core 
)

Clear bits in a register.

Parameters
dDestination register.
KConstant value to clear bits.
corePointer to the CORE structure.

Definition at line 238 of file arithm_logic.c.

238 {
239 // Clear Bit in Register
240 // Rd <- Rd & ($FF - K)
241 // 1 cycle
242
243 // Execute instruction
244 core->gp.R[d] = core->gp.R[d] & ~K;
245 inc_pc(core);
246}

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

◆ clr()

void clr ( uint8_t  d,
struct CORE core 
)

Clear a register (set to zero).

Parameters
dDestination register.
corePointer to the CORE structure.

Definition at line 278 of file arithm_logic.c.

278 {
279 // Clear Register
280 // Rd <- 0
281 // 1 cycle
282
283 // Execute instruction
284 core->gp.R[d] = 0;
285 inc_pc(core);
286}

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

◆ com()

void com ( uint8_t  d,
struct CORE core 
)

Perform one's complement on a register.

Parameters
dDestination register.
corePointer to the CORE structure.

Definition at line 203 of file arithm_logic.c.

203 {
204 // Logical Complement
205 // Rd <- $FF - Rd
206 // 1 cycle
207
208 // Execute instruction
209 core->gp.R[d] = ~core->gp.R[d];
210
211 // Update SREG
212 update_sreg_logic(core, core->gp.R[d]);
213 update_sreg_C(core, true);
214
215 inc_pc(core);
216}
void update_sreg_C(struct CORE *core, bool state)
Update the Carry flag (C) in the status register.
Definition sreg_utils.c:75

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

◆ dec()

void dec ( uint8_t  d,
struct CORE core 
)

Decrement the value of a register.

Parameters
dDestination register.
corePointer to the CORE structure.

Definition at line 258 of file arithm_logic.c.

258 {
259 // Decrement
260 // Rd <- Rd - 1
261 // 1 cycle
262
263 // Execute instruction
264 core->gp.R[d] = core->gp.R[d] - 1;
265 inc_pc(core);
266}

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

◆ eor()

void eor ( uint8_t  d,
uint8_t  r,
struct CORE core 
)

Perform bitwise exclusive OR between two registers.

Parameters
dDestination register.
rSource register.
corePointer to the CORE structure.

Definition at line 189 of file arithm_logic.c.

189 {
190 // Logical Exclusive OR
191 // Rd <- Rd ^ Rr
192 // 1 cycle
193
194 // Execute instruction
195 core->gp.R[d] = core->gp.R[d] ^ core->gp.R[r];
196
197 // Update SREG
198 update_sreg_logic(core, core->gp.R[d]);
199
200 inc_pc(core);
201}

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

◆ fmul()

void fmul ( uint8_t  d,
uint8_t  r,
struct CORE core 
)

Multiplies two unsigned 8-bit integers with fractional representation and stores the result in the CORE structure.

Parameters
dFirst operand.
rSecond operand.
corePointer to the CORE structure where the result will be stored.

Definition at line 334 of file arithm_logic.c.

334 {
335 // Fractional Multiply Unsigned
336 // R1:R0 <- Rd * Rr (UU)
337 // 2 cycle
338
339 // Execute instruction
340 uint16_t result = (uint16_t)core->gp.R[d] * (uint16_t)(core->gp.R[r] << 1);
341 core->gp.R[0] = result & 0xFF;
342 core->gp.R[1] = (result >> 8) & 0xFF;
343 inc_pc(core);
344}

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

◆ fmuls()

void fmuls ( uint8_t  d,
uint8_t  r,
struct CORE core 
)

Multiplies two signed 8-bit integers with fractional representation and stores the result in the CORE structure.

Parameters
dFirst operand.
rSecond operand.
corePointer to the CORE structure where the result will be stored.

Definition at line 346 of file arithm_logic.c.

346 {
347 // Fractional Multiply Signed
348 // R1:R0 <- Rd * Rr (SS)
349 // 2 cycle
350
351 // Execute instruction
352 int16_t result = (int16_t)core->gp.R[d] * (int16_t)(core->gp.R[r] << 1);
353 core->gp.R[0] = result & 0xFF;
354 core->gp.R[1] = (result >> 8) & 0xFF;
355 inc_pc(core);
356}

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

◆ fmulsu()

void fmulsu ( uint8_t  d,
uint8_t  r,
struct CORE core 
)

Multiplies a signed 8-bit integer with an unsigned 8-bit integer with fractional representation and stores the result in the CORE structure.

Parameters
dFirst operand (signed).
rSecond operand (unsigned).
corePointer to the CORE structure where the result will be stored.

Definition at line 358 of file arithm_logic.c.

358 {
359 // Fractional Multiply Signed with Unsigned
360 // R1:R0 <- Rd * Rr (SU)
361 // 2 cycle
362
363 // Execute instruction
364 int16_t result = (int16_t)core->gp.R[d] * (uint16_t)(core->gp.R[r] << 1);
365 core->gp.R[0] = result & 0xFF;
366 core->gp.R[1] = (result >> 8) & 0xFF;
367 inc_pc(core);
368}

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

◆ inc()

void inc ( uint8_t  d,
struct CORE core 
)

Increment the value of a register.

Parameters
dDestination register.
corePointer to the CORE structure.

Definition at line 248 of file arithm_logic.c.

248 {
249 // Increment
250 // Rd <- Rd + 1
251 // 1 cycle
252
253 // Execute instruction
254 core->gp.R[d] = core->gp.R[d] + 1;
255 inc_pc(core);
256}

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

◆ mul()

void mul ( uint8_t  d,
uint8_t  r,
struct CORE core 
)

Multiplies two unsigned 8-bit integers and stores the result in the CORE structure.

Parameters
dFirst operand.
rSecond operand.
corePointer to the CORE structure where the result will be stored.

Definition at line 298 of file arithm_logic.c.

298 {
299 // Multiply Unsigned
300 // R1:R0 <- Rd * Rr (UU)
301 // 2 cycle
302
303 // Execute instruction
304 uint16_t result = (uint16_t)core->gp.R[d] * (uint16_t)core->gp.R[r];
305 core->gp.R[0] = result & 0xFF;
306 core->gp.R[1] = (result >> 8) & 0xFF;
307 inc_pc(core);
308}

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

◆ muls()

void muls ( uint8_t  d,
uint8_t  r,
struct CORE core 
)

Multiplies two signed 8-bit integers and stores the result in the CORE structure.

Parameters
dFirst operand.
rSecond operand.
corePointer to the CORE structure where the result will be stored.

Definition at line 310 of file arithm_logic.c.

310 {
311 // Multiply Signed
312 // R1:R0 <- Rd * Rr (SS)
313 // 2 cycle
314
315 // Execute instruction
316 int16_t result = (int16_t)core->gp.R[d] * (int16_t)core->gp.R[r];
317 core->gp.R[0] = result & 0xFF;
318 core->gp.R[1] = (result >> 8) & 0xFF;
319 inc_pc(core);
320}

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

◆ mulsu()

void mulsu ( uint8_t  d,
uint8_t  r,
struct CORE core 
)

Multiplies a signed 8-bit integer with an unsigned 8-bit integer and stores the result in the CORE structure.

Parameters
dFirst operand (signed).
rSecond operand (unsigned).
corePointer to the CORE structure where the result will be stored.

Definition at line 322 of file arithm_logic.c.

322 {
323 // Multiply Signed with Unsigned
324 // R1:R0 <- Rd * Rr (SU)
325 // 2 cycle
326
327 // Execute instruction
328 int16_t result = (int16_t)core->gp.R[d] * (uint16_t)core->gp.R[r];
329 core->gp.R[0] = result & 0xFF;
330 core->gp.R[1] = (result >> 8) & 0xFF;
331 inc_pc(core);
332}

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

◆ neg()

void neg ( uint8_t  d,
struct CORE core 
)

Perform two's complement (negation) on a register.

Parameters
dDestination register.
corePointer to the CORE structure.

Definition at line 218 of file arithm_logic.c.

218 {
219 // Two's Complement
220 // Rd <- $00 - Rd
221 // 1 cycle
222
223 // Execute instruction
224 core->gp.R[d] = -core->gp.R[d];
225 inc_pc(core);
226}

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

◆ or()

void or ( uint8_t  d,
uint8_t  r,
struct CORE core 
)

Logical OR.

Parameters
dDestination register.
rSource register.
corePointer to the CORE structure.

Definition at line 161 of file arithm_logic.c.

161 {
162 // Logical OR
163 // Rd <- Rd | Rr
164 // 1 cycle
165
166 // Execute instruction
167 core->gp.R[d] = core->gp.R[d] | core->gp.R[r];
168
169 // Update SREG
170 update_sreg_logic(core, core->gp.R[d]);
171
172 inc_pc(core);
173}

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

◆ ori()

void ori ( uint8_t  d,
uint8_t  K,
struct CORE core 
)

Logical OR with immediate.

Parameters
dDestination register.
KImmediate value.
corePointer to the CORE structure.

Definition at line 175 of file arithm_logic.c.

175 {
176 // Logical OR with immediate
177 // Rd <- Rd | K
178 // 1 cycle
179
180 // Execute instruction
181 core->gp.R[d] = core->gp.R[d] | K;
182
183 // Update SREG
184 update_sreg_logic(core, core->gp.R[d]);
185
186 inc_pc(core);
187}

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

◆ sbc()

void sbc ( uint8_t  d,
uint8_t  r,
struct CORE core 
)

Subtract with carry.

Parameters
dDestination register.
rSource register.
corePointer to the CORE structure.

Definition at line 84 of file arithm_logic.c.

84 {
85 // Subtract with cary
86 // Rd <- Rd - Rr - C
87 // 1 cycle
88
89 uint8_t R_tmp = core->gp.R[d];
90
91 // Execute instruction
92 core->gp.R[d] = core->gp.R[d] - core->gp.R[r] - core->sreg.C;
93
94 // Update SREG
95 update_sreg_arithm(core, R_tmp, core->gp.R[r], core->gp.R[d]);
96
97 inc_pc(core);
98}

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

◆ sbci()

void sbci ( uint8_t  d,
uint8_t  K,
struct CORE core 
)

Subtract immediate with carry.

Parameters
dDestination register.
KImmediate value.
corePointer to the CORE structure.

Definition at line 100 of file arithm_logic.c.

100 {
101 // Subtract immediate with cary
102 // Rd <- Rd - K - C
103 // 1 cycle
104
105 uint8_t R_tmp = core->gp.R[d];
106
107 // Execute instruction
108 core->gp.R[d] = core->gp.R[d] - K - core->sreg.C;
109
110 // Update SREG
111 update_sreg_arithm(core, R_tmp, K, core->gp.R[d]);
112
113 inc_pc(core);
114}

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

◆ sbiw()

void sbiw ( uint8_t  d,
uint8_t  K,
struct CORE core 
)

Subtract immediate from word.

Parameters
dDestination register.
KImmediate value.
corePointer to the CORE structure.

Definition at line 116 of file arithm_logic.c.

116 {
117 // Subtract immediate from word
118 // Rd:Rd+1 <- Rd:Rd+1 - K
119 // 2 cycle
120
121 // Execute instruction
122 uint16_t Rd = core->gp.R[d] | (core->gp.R[d + 1] << 8);
123 uint16_t result = Rd - K;
124 core->gp.R[d] = result & 0xFF;
125 core->gp.R[d + 1] = (result >> 8) & 0xFF;
126
127 // Update SREG
128 udpate_sreg_arithm_16bit(core, Rd, result);
129
130 inc_pc(core);
131}

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

◆ sbr()

void sbr ( uint8_t  d,
uint8_t  K,
struct CORE core 
)

Set bits in a register.

Parameters
dDestination register.
KConstant value to set bits.
corePointer to the CORE structure.

Definition at line 228 of file arithm_logic.c.

228 {
229 // Set Bit in Register
230 // Rd <- Rd | K
231 // 1 cycle
232
233 // Execute instruction
234 core->gp.R[d] = core->gp.R[d] | K;
235 inc_pc(core);
236}

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

◆ ser()

void ser ( uint8_t  d,
struct CORE core 
)

Set all bits in a register.

Parameters
dDestination register.
corePointer to the CORE structure.

Definition at line 288 of file arithm_logic.c.

288 {
289 // Set Register
290 // Rd <- $FF
291 // 1 cycle
292
293 // Execute instruction
294 core->gp.R[d] = 0xFF;
295 inc_pc(core);
296}

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

◆ sub()

void sub ( uint8_t  d,
uint8_t  r,
struct CORE core 
)

Subtract.

Parameters
dDestination register.
rSource register.
corePointer to the CORE structure.

Definition at line 52 of file arithm_logic.c.

52 {
53 // Subtract without cary
54 // Rd <- Rd - Rr
55 // 1 cycle
56
57 uint8_t R_tmp = core->gp.R[d];
58
59 // Execute instruction
60 core->gp.R[d] = core->gp.R[d] - core->gp.R[r];
61
62 // Update SREG
63 update_sreg_arithm(core, R_tmp, core->gp.R[r], core->gp.R[d]);
64
65 inc_pc(core);
66}

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

◆ subi()

void subi ( uint8_t  d,
uint8_t  K,
struct CORE core 
)

Subtract immediate.

Parameters
dDestination register.
KImmediate value.
corePointer to the CORE structure.

Definition at line 68 of file arithm_logic.c.

68 {
69 // Subtract immediate
70 // Rd <- Rd - K
71 // 1 cycle
72
73 uint8_t R_tmp = core->gp.R[d];
74
75 // Execute instruction
76 core->gp.R[d] = core->gp.R[d] - K;
77
78 // Update SREG
79 update_sreg_arithm(core, R_tmp, K, core->gp.R[d]);
80
81 inc_pc(core);
82}

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

◆ tst()

void tst ( uint8_t  d,
struct CORE core 
)

Test for zero or minus after AND operation.

Parameters
dDestination register.
corePointer to the CORE structure.

Definition at line 268 of file arithm_logic.c.

268 {
269 // Test for Zero or Minus
270 // Rd <- Rd & Rd
271 // 1 cycle
272
273 // Execute instruction
274 core->gp.R[d] = core->gp.R[d] & core->gp.R[d];
275 inc_pc(core);
276}

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