-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
180 lines (123 loc) · 2.68 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// SYS86 project
// Main program
#include "arch.h"
#include "int.h"
#include "task.h"
#include "timer.h"
#include "queue.h"
#include "serial.h"
#include "lib.h"
#include "heap.h"
// Test code
static struct task_s task_loop;
static struct task_s task_stub;
//------------------------------------------------------------------------------
#ifdef CONFIG_TRACE
static void trace_dump (void)
{
char_t str [5];
byte_t len;
trace_on = 0;
for (word_t i = 0; i < trace_count; i++)
{
word_t num = traces [i].num;
len = word_to_hex (num, str);
for (int s = 0; s < len; s++)
serial_put (str [s]);
serial_put (' ');
word_t msec = traces [i].msec;
len = word_to_dec (msec, str);
for (int s = 0; s < len; s++)
serial_put (str [s]);
serial_put ('.');
word_t usec = traces [i].usec / 5;
len = word_to_dec (usec, str);
for (int s = 0; s < len; s++)
serial_put (str [s]);
serial_put (13);
serial_put (10);
}
serial_put (13);
serial_put (10);
trace_count = 0;
trace_on = 1;
}
#endif // CONFIG_TRACE
#ifdef HEAP_DEBUG
void heap_dump (heap_s * h)
{
char_t str [5];
byte_t len;
len = word_to_hex ((word_t) h, str);
for (int s = 0; s < len; s++)
serial_put (str [s]);
serial_put (' ');
len = word_to_hex (h->tag, str);
for (int s = 0; s < len; s++)
serial_put (str [s]);
serial_put (' ');
len = word_to_dec (h->size, str);
for (int s = 0; s < len; s++)
serial_put (str [s]);
serial_put (13);
serial_put (10);
}
#endif // HEAP_DEBUG
static struct task_s task_dump;
static void main_dump (void)
{
while (1)
{
// Wait for timer
timer_get ();
// Dump traces
#ifdef CONFIG_TRACE
trace_dump ();
#endif
// Dump heap
#ifdef HEAP_DEBUG
heap_iterate (heap_dump);
serial_put (13);
serial_put (10);
#endif
}
}
//------------------------------------------------------------------------------
static void main_loop (void)
{
while (1)
{
// Read from serial port
// Blocking operation
byte_t c = serial_get ();
//trace_near (0x100);
// Write to serial port
// Blocking operation
serial_put (c);
//trace_near (0x200);
}
}
extern int _free_begin; // linker symbol
void main (void)
{
// System initialization
heap_init ();
heap_add (&_free_begin, 0x8000);
int_init ();
task_init ();
timer_init ();
serial_init ();
task_init_near (0, &task_loop, main_loop, STACK_SIZE);
task_init_near (2, &task_dump, main_dump, STACK_SIZE);
// TEST: idle task in user space
task_init_far (4, &task_stub, 0x2000, STACK_SIZE);
// Switch to root task
// Initial stack not needed any more
#ifdef CONFIG_TRACE
trace_on = 1;
#endif // CONFIG_TRACE
word_t flags;
int_save (flags);
task_next = &task_loop;
task_switch ();
}