/
yakc.c
452 lines (396 loc) · 9.61 KB
/
yakc.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* Filename: yakc.c
* Dayton Minor
* Jared Moore
* Contains kernel c code
* Lab 4c
*
* */
#include "clib.h"
#include "yakk.h"
/* Global Variables */
int YKCtxSwCount; /* Global variable tracking context switches */
int YKIdleCount; /* Global variable used by idle task */
int YKTickNum; /* Global variable incremented by tick handler */
int YKISRDepth;
int debug;
int activeTasks;
int started = 0;
TCBptr runningTask; /* the task currently running */
TCBptr YKRdyList; /* a list of TCBs of all ready tasks
in order of decreasing priority */
TCBptr YKSuspList; /* tasks delayed or suspended */
TCBptr YKAvailTCBList; /* a list of available TCBs */
TCB YKTCBArray[MAXTASKS+1]; /* array to allocate all needed TCBs
(extra one is for the idle task) */
YKSEM SStack[MAXSEMAPHORES + MAXMESSAGEQUEUES]; /* Semaphores */
TCBptr YKPendList[MAXSEMAPHORES + MAXMESSAGEQUEUES + MAXEVENTS]; /* One needed for each semaphore */
YKQ QStack[MAXMESSAGEQUEUES]; /* Queues */
YKEVENT EStack[MAXEVENTS]; /* Events */
int IStk[ISTACKSIZE];
/* Function declarations */
void printList(TCBptr list) {
TCBptr tcb = list;
int safety = 0;
// printString("Active tasks: ");
// printInt(activeTasks);
while(tcb != NULL && safety < (activeTasks + 10)) {
printString(" Task P: ");
printInt(tcb->priority);
// printNewLine();
tcb = tcb->next;
safety++;
}
printNewLine();
}
void YKInitialize() { /* Initializes all required kernel data structures */
int i;
TCBptr tempList;
// printString("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
activeTasks = 0;
YKCtxSwCount = 0;
YKIdleCount = 0;
YKTickNum = 0;
/* Set up TCB Lists */
YKRdyList = NULL;
YKSuspList = NULL;
runningTask = NULL;
YKAvailTCBList = YKTCBArray;
YKTCBArray[0].prev = NULL;
for(i = 0; i < MAXTASKS; i++){ /* put the YKTCBArray into the available TCB list */ // this doesn't work
YKTCBArray[i].next = &YKTCBArray[i + 1];
YKTCBArray[i + 1].prev = &YKTCBArray[i];
}
// Creates idle task
YKNewTask(YKIdle, (void*)&IStk[ISTACKSIZE],255);
}
void YKNewTask(void (*task)(void), void*taskStack, unsigned char priority) { /* Creates a new task */
int ip,sp;
TCBptr new_task = &YKTCBArray[activeTasks];
activeTasks++;
new_task->priority = priority;
new_task->state = READY;
new_task->delay = 0;
new_task->next = NULL;
new_task->prev = NULL;
YKRdyList = queue(YKRdyList,new_task);
ip = (int) task & 0xFFFF;
sp = (int) taskStack & 0xFFFF;
sp = initStack(ip,sp);
new_task->sp = (void*)sp;
if(runningTask != NULL){
YKScheduler(0);
}
}
//3
void YKRun() { /* Starts actual execution of user code */
started = 1;
YKScheduler(1);
}
void YKDelayTask(int count) { /* Delays a task for specified number of clock ticks*/
YKEnterMutex();
runningTask->delay=count;
suspendTask(runningTask);
YKExitMutex();
YKScheduler(0);
}
//void YKEnterMutex() { /* Disables interrupts */
//get flags
//0 out interrupt bit
//save flags
//}
//void YKExitMutex() { /* Enables interrupts */
//get flags
//or the interrupt bit
//save flags
//}
//2.5
void YKScheduler(int contextSaved) { /* Determines the highest priority ready task */
TCBptr next;
if(!started)
return;
next = peak(YKRdyList);
if(runningTask != NULL && runningTask->priority != next->priority){
YKEnterMutex();
YKCtxSwCount++;
if(contextSaved == 0) {
YKSaveContext();
}
YKDispatcher(next);
} else if (runningTask == NULL){
YKDispatcher(next);
}
}
//2.5
void YKDispatcher(TCBptr next) { /* Begins or resumes execution of the next task */
void* sp;
// printString("Dispatcher\n\r");
runningTask = next;
sp = next->sp;
// printString("Stack: ");
// printInt((int)sp);
// printNewLine();
next->state = RUNNING;
//YKEnterMutex();
dispatchTask(sp);
//YKExitMutex();
}
void YKTickHandler() { /* The kernel's timer tick interrupt handler */
//print Tick information
/* printNewLine();
printString("Tick ");
printInt(YKTickNum);
printNewLine(); */
//update tick info
YKTickNum++;
// while(YKTickNum > 30);
//decrement top of YKSuspList->delay
if(YKSuspList != NULL) {
YKEnterMutex();
YKSuspList->delay = YKSuspList->delay - 1;
while(YKSuspList!= NULL && YKSuspList->delay == 0){
TCBptr task = dequeue(&YKSuspList); /* get the top of the list */
YKRdyList = queue(YKRdyList,task); /* put the task on the queue */
}
YKExitMutex();
}
}
void YKIdle() { /* Idle task for the kernel */
int i;
while(1) {
YKIdleCount++;
i = 0;
i = 0;
}
}
TCBptr peak(TCBptr head){
return head;
}
TCBptr queue(TCBptr queue_head, TCBptr task){
task->next = NULL;
task->prev = NULL;
if(queue_head == NULL){
return task;
}
if(queue_head->priority > task->priority){
task->next = queue_head;
queue_head->prev = task;
queue_head = task;
} else {
TCBptr temp = queue_head;
while(temp->next != NULL && (temp->next->priority < task->priority)) {
temp = temp->next;
}
if(temp->next==NULL){
task->prev = temp;
temp->next = task;
}
else
{
task->next = temp->next;
task->next->prev = task;
task->prev = temp;
temp->next = task;
}
}
return queue_head;
}
TCBptr dequeue(TCBptr* queue_head){
TCBptr next = *queue_head;
*queue_head = (*queue_head)->next; //does nothing
next->next = NULL;
next->prev = NULL;
(*queue_head)->prev = NULL;
return next;
}
void suspendTask(TCBptr task){
TCBptr temp = NULL;
temp = peak(YKRdyList);
task = dequeue(&YKRdyList);
task->state = DELAYED;
if(YKSuspList == NULL){
YKSuspList = task;//dequeue(&YKRdyList);
}
else if(task->delay <= YKSuspList->delay){
task->next = YKSuspList;
task->prev = NULL;
YKSuspList->prev = task;
YKSuspList->delay -= task->delay;
YKSuspList = task;
} else { /* Needs fixin later*/
temp = YKSuspList;
while(temp->next != NULL && temp->delay < task->delay){
//printString("decrement delay\n");
task->delay -= temp->delay;
temp = temp->next;
}
if(temp->delay < task->delay) { /* end of the list */
task->delay -= temp->delay;
temp->next = task;
task->prev = temp;
} else { /* insert into list */
temp->prev->next = task;
task->prev = temp->prev;
task->next = temp;
temp->prev = task;
temp->delay -= task->delay;
}
}
}
void saveStack(void* sp){
runningTask->sp = sp;
}
YKSEM* YKSemCreate(int initialValue){
static int index = 0;
//create sem at index
YKEnterMutex();
SStack[index].ID = index;
SStack[index].value = initialValue;
YKExitMutex();
//return address and increment index
return &SStack[index++];
}
void YKSemPend(YKSEM* semaphore){
TCBptr task;
YKEnterMutex();
if(semaphore->value-- > 0){
YKExitMutex();
return;
}
task = dequeue(&YKRdyList);
task->state = SUSPENDED;
YKPendList[semaphore->ID] = queue(YKPendList[semaphore->ID],task);
YKScheduler(0);
YKExitMutex();
}
void YKSemPost(YKSEM* semaphore){
TCBptr task;
YKEnterMutex();
if(semaphore->value++ >= 0){extern TCB YKTCBArray[MAXTASKS+1];
YKExitMutex();
return;
}
task = dequeue(&YKPendList[semaphore->ID]);
task->state = READY;
YKRdyList = queue(YKRdyList,task);
//call scheduler if not ISR
if(YKISRDepth == 0){
YKScheduler(0);
}
YKExitMutex();
}
YKQ* YKQCreate(void **start, unsigned size){
static int index = 0;
YKQ *queue;
if(index == MAXMESSAGEQUEUES)
printString("IDIOTS: update max message queues\n");
//find next entry in QStack
queue = &QStack[index++];
//initialize variables
queue->queue = start;
queue->head = -1;
queue->tail = 0;
queue->length = size;
queue->sem = YKSemCreate(0);
return queue;
}
void* YKQPend(YKQ *queue){
void* msg;
//check if head == tail
YKSemPend(queue->sem);
if(queue->head == -1){
printString("null value returned");
return NULL;
}
msg = queue->queue[queue->head++];
//update head(increment)
if(queue->head == queue->length)
queue->head = 0;
if(queue->head == queue->tail) // empty
queue->head = -1;
return msg;
}
int YKQPost(YKQ *queue, void *msg){
//check if head == index
if(queue->head == queue->tail){ // full
//YKSemPost(queue->sem);
return 0;
}else{ //not full
if(queue->head == -1) // empty
queue->head = queue->tail;
queue->queue[queue->tail++] = msg;
if(queue->tail == queue->length)
queue->tail = 0;
YKSemPost(queue->sem);
return 1;
}
}
YKEVENT* YKEventCreate(unsigned init){
static int index = 0;
YKEVENT* e = &EStack[index++];
e->flags = init;
e->sFlags = NULL;
e->tasks = NULL;
return e;
}
void eventBlock(YKEVENT* e){
TCBptr task, list;
task = dequeue(&YKRdyList);
task->state = SUSPENDED;
list = e->tasks;
if(list == NULL)
e->tasks = task;
else{
task->next = list;
e->tasks = task;
}
YKScheduler(0);
}
unsigned YKEventPend(YKEVENT* e, unsigned mask, int mode){
unsigned test;
YKEnterMutex();
if(mode == EVENT_WAIT_ANY){
test = mask & e->flags;
while(!test) {
YKExitMutex();
eventBlock(e); /* Block */
test = mask & e->sFlags; /* test again */
}
} else if (mode == EVENT_WAIT_ALL) {
test = mask & e->flags;
while(test != mask) {
YKExitMutex();
eventBlock(e);
test = mask & e->sFlags;
}
} else {
printString("Invalid event wait mode!\n");
return NULL;
}
YKExitMutex();
return e->flags;
}
void YKEventSet(YKEVENT* e, unsigned mask){
TCBptr head;
TCBptr temp;
int schedule;
YKEnterMutex();
e->flags |= mask; /* Set bits from mask to one (leave others unchanged) */
head = e->tasks;
e->sFlags = e->flags; /* Save flags that caused this pend */
/* unblock all tasks associated with this event*/
schedule = 0;
while( head != NULL){
temp = head;
head = head->next;
e->tasks = head;
temp->next = NULL;
YKRdyList = queue(YKRdyList,temp);
schedule = 1;
}
YKExitMutex();
if(schedule)
YKScheduler(0);
}
void YKEventReset(YKEVENT* e, unsigned mask){
e->flags = e->flags & !mask; /* Set the bits from mask to zero in flags */
}