-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMARTS77.CPP
581 lines (499 loc) · 12.9 KB
/
SMARTS77.CPP
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/****************************************************************/
/* A Small Real Time System for the Real-Time laboratory */
/* built by: A.Teitelbaum on an idea of H.G.Mendelbaum */
/* Jerusalem College of Technology, 5759-64 (1999) */
/* update Tishrey 5777 */
/* SMARTS77.CPP, SMARTS class body */
/****************************************************************/
#include "smarts77.h"
/********** Function **********/
unsigned getTimerClocks( )
// Gets the remaining clocks of the timer register
{
unsigned clocks;
/* latch counter #0 */
outportb(0x43,0x00);
/* read counter #0 low byte */
clocks=inportb(0x40);
/* read counter #0 high byte */
clocks += inportb(0x40)<<8;
return clocks;
}
////////////////////////////////////////////////////
/********** class body: Parallelism **********/
Parallelism::Parallelism()
{
currentTask = 0;
sleepTasks = 0;
activeTasks = 0;
totalTasks = 0;
deadlock = false;
contextSwitchFlag = true;
endOfTimeSlice = true;
}
void Parallelism::externalFunctions(void interrupt ( *timerInterruptHandler)(...),
void far *scheduler, void far *userTaskEnd,
int far (*algorithm)( ))
// Sets the external functions
{
this->timerInterruptHandler = timerInterruptHandler;
this->scheduler = scheduler;
this->userTaskEnd = userTaskEnd;
this->algorithm = algorithm;
contextSched.declare(scheduler, userTaskEnd, 'S'); // prepare the stack of the scheduler task
for (int i=MaxStack-1; i >= (MaxStack-14); i--)
schedCopy[i]=contextSched.stack[i];
}
int Parallelism::declareTask(void far *code, char name, int periodTime, int cycles, int priority)
// Insert a new task entry in SMARTS context array [ ]
{
if (totalTasks < MaxTask-1)
{
context[totalTasks++].declare(code, userTaskEnd, name, periodTime, cycles, priority);
++activeTasks;
return true;
}
else
return false;
}
void Parallelism::runTheTasks()
// Start running all tasks declared in SMARTS.
{
context[totalTasks].status = READY; //for task 'runTheTasks' (IDLE task)
context[totalTasks].priority = MAXINT;
context[totalTasks].currentPriority = MAXINT;
currentTask = totalTasks;
asm cli; // forbids interrupts (clear interrupts) while changing the interrupt vect
// saves the original BIOS userInt in our variable 'userIntAddress' to be restored at the end
userIntAddress = getvect(userInt); // BIOS userInt 0x60 (unused by PC)
// puts the normal BIOS timerInt into the unused userInt address
setvect(userInt,getvect(timerInt)); // timerInt 0x08
// sets our SMARTS external function 'timerInterruptHandler' as the new PC hard interrupt time handler
setvect(timerInt,timerInterruptHandler);
asm sti; // allows back interrupts (set interrupts)
// waits for end of runTheTasks (end of all the tasks)
while(true)
{
if (deadlock)
{
textcolor(RED);
cprintf("\n\n\rExit : deadlock");
break;
}
if (activeTasks==0)
{
cprintf("\n\n\rExit : finish");
break;
}
if (timeOutTasks > 0)
{
cprintf("\n\n\rExit : time error");
break;
}
}
// restore the original BIOS 'interrupt vector' at the end before returning to regular DOS
asm cli; // no interrupts
setvect(timerInt, getvect(userInt)); // restore original BIOS time handler
setvect(userInt, userIntAddress); // restore original BIOS userInt
asm sti; // yes interrupts
}
void Parallelism::callScheduler( )
// Return the control to the scheduler, this sets ON the software interrupt ProgInt flag
{
setProgInt( );
asm int timerInt;
}
void Parallelism::restoreSchedStack( )
// Restore the scheduler stack
{
for (int i=MaxStack-1; i >= (MaxStack-14); i--)
contextSched.stack[i] = schedCopy[i];
}
int Parallelism::getCurrentTask( )
{
return currentTask;
}
void Parallelism::setCurrentTask(int taskNum)
// Sets the next task to be run
{
if (taskNum <= totalTasks)
currentTask = taskNum;
}
int Parallelism::getTotalTasks( )
// Gets total tasks declared
{
return totalTasks;
}
int Parallelism::getDeadlock( )
{
return deadlock;
}
void Parallelism::setDeadlock( )
{
deadlock = true;
}
int Parallelism::contextSwitchOn( )
// flag which enables context switch
{
if (endOfTimeSlice) //is current time slice finished ?
{
endOfTimeSlice = false;
//contextSwitchFlag = true;
context[currentTask].isContextSwitch = true;
callScheduler(); // return control to the scheduler
return 1;
}
//contextSwitchFlag = true;
context[currentTask].isContextSwitch = true;
return 0;
}
void Parallelism::contextSwitchOff( )
// Disable context switch
{
//contextSwitchFlag = false;
context[currentTask].isContextSwitch = false;
}
int Parallelism::getContextSwitch( )
{
//return contextSwitchFlag;
return context[currentTask].isContextSwitch;
}
void Parallelism::setProgInt( )
// flag indicates to the extern function 'timerInterruptHandler'
// that this is an internal SMARTS software interrupt call,
// and the original BIOS function has no to be called.
{
progInt = true;
}
void Parallelism::resetProgInt()
{
progInt = false;
}
int Parallelism::getProgInt( )
{
return progInt;
}
void Parallelism::setEndOfTimeSlice( )
// flag indicates that when 'context switch' will be enabled,
// it must also return the control to the scheduler.
{
endOfTimeSlice = true;
}
char Parallelism::getName(int taskNum) // returns name found or ' ' if not
{
return (taskNum <= totalTasks)? context[taskNum].name : ' ';
}
int Parallelism::getPeriodTime(int taskNum) // returns periodTime found or ' ' if not
{
return (taskNum <= totalTasks) ? context[taskNum].periodTime : -1;
}
int Parallelism::getPeriodTimeLast(int taskNum) // returns periodTime found or ' ' if not
{
return (taskNum <= totalTasks) ? context[taskNum].periodTimeLast : -1;
}
int Parallelism::decPeriodTime(int taskNum)
{
//cprintf("in decPeriodTime() ");
//cout << context[taskNum].periodTimeLast << endl;
if (taskNum <= totalTasks - 1) // without scheduler.
{
context[taskNum].periodTimeLast--;
if (context[taskNum].periodTimeLast == 0 && context[taskNum].cycles > 0)
{
context[taskNum].reDeclareTask();
}
}
}
int Parallelism::getCycles(int taskNum) // returns Cycles found or ' ' if not
{
return (taskNum <= totalTasks) ? context[taskNum].cycles : ' ';
}
char Parallelism::getCurrentName( )
{
return context[currentTask].name;
}
int Parallelism::getCurrentPriority()
{
return context[currentTask].currentPriority;
}
taskStatus Parallelism::getStatus(int taskNum)
// returns status or undefined if not found
{
return (taskNum <= totalTasks)? context[taskNum].status : UNDEFINED;
}
taskStatus Parallelism::getCurrentStatus( )
{
return context[currentTask].status;
}
void Parallelism::resume(int taskNum)
{
if (taskNum < totalTasks)
context[taskNum].status = READY;
}
void Parallelism::resume(char taskName)
{
for(int i=0;i<totalTasks;++i)
if (context[i].name == taskName)
context[i].status = READY;
}
void Parallelism::setCurrentNotActive()
{
context[currentTask].status = NOT_ACTIVE;
if(context[currentTask].cycles <= 1)
--activeTasks;
}
void Parallelism::suspended()
{
context[currentTask].status = SUSPENDED;
callScheduler();
}
void Parallelism::incrPriority(int taskNum )
{
if (taskNum < totalTasks)
context[taskNum].incrPriority();
}
void Parallelism::setOriginalPriority(int taskNum )
{
if (taskNum < totalTasks)
context[taskNum].setOriginalPriority();
}
void Parallelism::setCurrentOriginalPriority()
{
context[currentTask].setOriginalPriority();
}
Event *Parallelism::getExpectedEvent(int taskNum)
// returns *Event or NULL if not found
{
return (taskNum <= totalTasks)? context[taskNum].expectedEvent : NULL;
}
Event *Parallelism::getCurrentExpectedEvent( )
{
return context[currentTask].expectedEvent;
}
void Parallelism::setCurrentExpectedEvent(Event *expectedEvent)
{
context[currentTask].expectedEvent = expectedEvent;
}
void Parallelism::sleep(int t)
// Current task sleeps for 't' milliseconds
{
if (t < MAXINT)
{
context[currentTask].sleepCount = t/55+1;
context[currentTask].status = SLEEP;
++sleepTasks;
callScheduler(); // return control to scheduler
}
}
void Parallelism::sleepDecr(int taskNum)
{
if (taskNum < totalTasks)
context[taskNum].sleepDecr();
}
void Parallelism::getCurrentStack(unsigned &StackSeg, unsigned &StackPtr)
// Load current task stack pointer
{
if (context[currentTask].is_restore != 0) // if
{
for (int i = MaxStack - 1; i >= (MaxStack - 14); i--)
context[currentTask].stack[i] = context[currentTask].stack_copy[i];
StackSeg = context[currentTask].stackSeg_copy;
StackPtr = context[currentTask].stackPtr_copy;
context[currentTask].is_restore = 0;
}
else
{
StackSeg = context[currentTask].stackSeg;
StackPtr = context[currentTask].stackPtr;
}
}
void Parallelism::setCurrentStack(unsigned StackSeg, unsigned StackPtr)
// Save current task stack pointer
{
context[currentTask].stackSeg = StackSeg;
context[currentTask].stackPtr = StackPtr;
}
void Parallelism::getSchedStack(unsigned &StackSeg, unsigned &StackPtr)
// Load scheduler stack pointer
{
StackSeg = contextSched.stackSeg;
StackPtr = contextSched.stackPtr;
}
void Parallelism::handleTimers()
// handling of the sleep status mode
{
for (int i=totalTasks-1; i >=0 ; --i)
{
if(getStatus(i)==SLEEP)
{
sleepDecr(i);
if (getStatus(i) == READY)
--sleepTasks;
}
}
}
void Parallelism::handleClockPeriods()
{
for (int i = totalTasks - 1; i >= 0; --i)
{
decPeriodTime(i);
}
}
void Parallelism::taskEnd( )
// This function is called after the last operation of a task, instead of function return
{
SMARTS.setCurrentNotActive();
SMARTS.callScheduler(); // return the control to the scheduler to run a next task
}
/********** class body: Task **********/
Task::Task()
{
stack[MaxStack-14]=_BP;
stack[MaxStack-13]=_DI;
stack[MaxStack-12]=_SI;
stack[MaxStack-11]=_DS;
stack[MaxStack-10]=_ES;
stack[MaxStack-9]=_DX;
stack[MaxStack-8]=_CX;
stack[MaxStack-7]=_BX;
stack[MaxStack-6]=_AX;
stackSeg=FP_SEG(&stack[MaxStack-14]);
stackPtr=FP_OFF(&stack[MaxStack-14]);
status = NOT_ACTIVE;
sleepCount = 0;
currentPriority=priority=0;
isContextSwitch = true;
}
//-----------------------------------------------------
void Task::declare(void far *code, void far *userTaskEnd, char name, int periodTime, int cycles, int priority)
{
stack[MaxStack-5]=FP_OFF(code);
stack[MaxStack-4]=FP_SEG(code);
stack[MaxStack-3]=_FLAGS;
stack[MaxStack-2]=FP_OFF(userTaskEnd);
stack[MaxStack-1]=FP_SEG(userTaskEnd);
for (int i = MaxStack - 1; i >= (MaxStack - 14); i--) //Save the initial stack values for future recovery
stack_copy[i] = stack[i];
stackSeg_copy = stackSeg;
stackPtr_copy = stackPtr;
is_restore = 0;
this->periodTime = periodTime; // the task should be complete every 'periodTime'
this->cycles = cycles; // the task should be run 'cycles' times
this->name= name;
periodTimeLast = this->periodTime;
currentPriority = priority;
this->priority = priority;
status = READY;
}
//----------------------------------------------------
void Task::incrPriority( )
{
--currentPriority;
}
//----------------------------------------------------
void Task::setOriginalPriority( )
{
currentPriority = priority;
}
//----------------------------------------------------
void Task::sleepDecr( )
// Decrements the sleep counter and update the task status accordingly
{
if (status==SLEEP)
{
if (sleepCount > 0)
--sleepCount;
if (!sleepCount)
status = READY;
}
}
void Task::reDeclareTask()
{
periodTimeLast = periodTime;
cycles--;
if (status == NOT_ACTIVE)
{
if (cycles > 0)
{
//MUTEX.acquire();MUTEX.acquire();
status = READY;
is_restore = 1;
//MUTEX.release();
}
else
status = NOT_ACTIVE;
}
else
{
MUTEX.acquire();
cprintf("Time error %c ", name);
status = UNDEFINED;
SMARTS.timeOutTasks++;
MUTEX.release();
}
}
void Mutex::Mutex()
{
_owner = -1;
_level = 0;
_s = 1;
_waitingList = new priorityQueue();
_numberSuspended = 0;
}
void Mutex::acquire()
{
if (_s == 1 || _owner == SMARTS.getCurrentTask())
_s = 0;
else
{
_numberSuspended++;
_waitingList->enqueue(SMARTS.getCurrentTask(), SMARTS.getCurrentPriority());
SMARTS.suspended(); // suspending the current task.
}
_owner = SMARTS.getCurrentTask();
_level++;
}
void Mutex::release()
{
if (_owner == SMARTS.getCurrentTask())
if (--_level)
return;
else {
_owner = -1;
if (_numberSuspended > 0) {
_numberSuspended--;
int task = _waitingList->peek();
SMARTS.resume(task);
}
else
_s = 1;
}
}
priorityQueue::priorityQueue()
{
size = 0;
}
void priorityQueue::enqueue(int value, int priority)
{
pr[size].value = value;
pr[size].priority = priority;
size++;
}
int priorityQueue::peek()
{
int highestPriority = -999;
int ind = -1;
for (int i = 0; i < size; i++)
{
if (highestPriority < pr[i].priority)
{
highestPriority = pr[i].priority;
ind = i;
}
}
int ret = pr[ind].value;
for (i = ind; i < size; i++)
pr[i] = pr[i + 1];
size--;
return ret;
}