-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
101 lines (94 loc) · 2.96 KB
/
stack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/********************************************************************************
* stack.c: Contains function definitions for implementation of 1 kB stack.
********************************************************************************/
#include "stack.h"
/* Static variables: */
static uint32_t stack[STACK_ADDRESS_WIDTH]; /* 1 kB stack. */
static uint16_t sp; /* Stack pointer, points to last added value. */
static bool stack_empty; /* Indicates if the stack is empty. */
/********************************************************************************
* stack_reset: Clears content on the entire stack and sets the stack pointer
* to the top of the stack.
********************************************************************************/
void stack_reset(void)
{
for (uint16_t i = 0; i < STACK_ADDRESS_WIDTH; ++i)
{
stack[i] = 0x00;
}
sp = STACK_ADDRESS_WIDTH - 1;
stack_empty = true;
return;
}
/********************************************************************************
* stack_push: Pushes 8 bit value to the stack, unless the stack is full.
* Success code 0 is returned after successful push, otherwise
* error code 1 is returned if the stack is already full.
*
* - value: 8 bit value to push to the stack.
********************************************************************************/
int stack_push(const uint32_t value)
{
if (sp == 0)
{
return 1;
}
else
{
if (stack_empty)
{
stack[sp] = value;
stack_empty = false;
}
else
{
stack[--sp] = value;
}
return 0;
}
}
/********************************************************************************
* stack_pop: Returns 8 bit value popped from the stack. If the stack is empty,
* the value 0x00 is returned.
********************************************************************************/
uint32_t stack_pop(void)
{
if (stack_empty)
{
return 0x00;
}
else
{
if (sp < STACK_ADDRESS_WIDTH - 1)
{
return stack[sp++];
}
else
{
stack_empty = true;
return stack[sp];
}
}
}
/********************************************************************************
* stack_pointer: Returns the 16 bit address of the stack pointer.
********************************************************************************/
uint16_t stack_pointer(void)
{
return sp;
}
/********************************************************************************
* stack_last_added_value: Returns the last added value to the stack. If the
* stack is empty, the value 0x00 is returned.
********************************************************************************/
uint32_t stack_last_added_value(void)
{
if (stack_empty)
{
return 0x00;
}
else
{
return stack[sp];
}
}