OpenCores
URL https://opencores.org/ocsvn/thor/thor/trunk

Subversion Repositories thor

[/] [thor/] [trunk/] [software/] [FMTK/] [source/] [kernel/] [TCB.c] - Blame information for rev 23

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 23 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2012-2015  Robert Finch, Stratford
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
// TCB.c
9
// Task Control Block related functions.
10
//
11
// This source file is free software: you can redistribute it and/or modify 
12
// it under the terms of the GNU Lesser General Public License as published 
13
// by the Free Software Foundation, either version 3 of the License, or     
14
// (at your option) any later version.                                      
15
//                                                                          
16
// This source file is distributed in the hope that it will be useful,      
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
19
// GNU General Public License for more details.                             
20
//                                                                          
21
// You should have received a copy of the GNU General Public License        
22
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
23
//                                                                          
24
// ============================================================================
25
//
26
#include "config.h"
27
#include "const.h"
28
#include "types.h"
29
#include "proto.h"
30
 
31
extern char hasUltraHighPriorityTasks;
32
extern pascal prtdbl(double);
33
 
34
TCB tcbs[NR_TCB];
35
hTCB freeTCB;
36
hTCB TimeoutList;
37
hTCB readyQ[8];
38
 
39
 
40
pascal int chkTCB(TCB *p)
41
{
42
    asm {
43
        lw    r1,32[bp]
44
        //chk   r1,r1,b48
45
    }
46
}
47
 
48
naked TCB *GetRunningTCBPtr()
49
{
50
    asm {
51
        mov   r1,tr
52
        rts
53
    }
54
}
55
 
56
naked hTCB GetRunningTCB()
57
{
58
    asm {
59
        subui  r1,tr,#tcbs_
60
        shrui  r1,r1,#12
61
        rts
62
    }
63
}
64
 
65
pascal void SetRunningTCB(hTCB ht)
66
{
67
     asm {
68
         lw      tr,32[bp]
69
         shli    tr,tr,#12
70
         addui   tr,tr,#tcbs_
71
     }
72
}
73
 
74
// ----------------------------------------------------------------------------
75
// ----------------------------------------------------------------------------
76
 
77
pascal int InsertIntoReadyList(hTCB ht)
78
{
79
    hTCB hq;
80
    TCB *p, *q;
81
 
82
        asm {
83
                ldi   r1,#70
84
                sc    r1,hs:LEDS
85
        }
86
        if (ht < 0 || ht >= NR_TCB)
87
                throw E_BadHandle;
88
    p = &tcbs[ht];
89
        if (p->priority > 077 || p->priority < 000)
90
                throw E_BadPriority;
91
        asm {
92
                ldi   r1,#71
93
                sc    r1,hs:LEDS
94
        }
95
        if (p->priority < 003)
96
           hasUltraHighPriorityTasks |= (1 << p->priority);
97
        p->status = TS_READY;
98
        hq = readyQ[p->priority>>3];
99
        asm {
100
                ldi   r1,#72
101
                sc    r1,hs:LEDS
102
        }
103
        // Ready list empty ?
104
        if (hq<0) {
105
                p->next = ht;
106
                p->prev = ht;
107
                readyQ[p->priority>>3] = ht;
108
                return E_Ok;
109
        }
110
        asm {
111
                ldi   r1,#75
112
                sc    r1,hs:LEDS
113
        }
114
        // Insert at tail of list
115
        q = &tcbs[hq];
116
        p->next = hq;
117
        p->prev = q->prev;
118
        tcbs[q->prev].next = ht;
119
        q->prev = ht;
120
        asm {
121
                ldi   r1,#77
122
                sc    r1,hs:LEDS
123
        }
124
        return E_Ok;
125
}
126
 
127
// ----------------------------------------------------------------------------
128
// ----------------------------------------------------------------------------
129
 
130
pascal int RemoveFromReadyList(hTCB ht)
131
{
132
    TCB *t;
133
 
134
        if (ht < 0 || ht >= NR_TCB)
135
                throw E_BadHandle;
136
    t = &tcbs[ht];
137
        if (t->priority > 077 || t->priority < 000)
138
                throw E_BadPriority;
139
    if (ht==readyQ[t->priority>>3])
140
       readyQ[t->priority>>3] = t->next;
141
    if (ht==readyQ[t->priority>>3])
142
       readyQ[t->priority>>3] = -1;
143
    tcbs[t->next].prev = t->prev;
144
    tcbs[t->prev].next = t->next;
145
    t->next = -1;
146
    t->prev = -1;
147
    t->status = TS_NONE;
148
    return E_Ok;
149
}
150
 
151
 
152
// ----------------------------------------------------------------------------
153
// ----------------------------------------------------------------------------
154
 
155
pascal int InsertIntoTimeoutList(hTCB ht, int to)
156
{
157
    TCB *p, *q, *t;
158
 
159
        if (ht < 0 || ht >= NR_TCB)
160
                throw E_BadHandle;
161
    t = &tcbs[ht];
162
    if (TimeoutList<0) {
163
        t->timeout = to;
164
        TimeoutList = ht;
165
        t->next = -1;
166
        t->prev = -1;
167
        return E_Ok;
168
    }
169
 
170
    q = null;
171
    p = &tcbs[TimeoutList];
172
 
173
    while (to > p->timeout) {
174
 
175
        to -= p->timeout;
176
        q = p;
177
        p = &tcbs[p->next];
178
 
179
    }
180
 
181
    t->next = p - tcbs;
182
    t->prev = q - tcbs;
183
    if (p) {
184
        p->timeout -= to;
185
        p->prev = ht;
186
    }
187
    if (q)
188
        q->next = ht;
189
    else
190
        TimeoutList = ht;
191
    t->status |= TS_TIMEOUT;
192
    return E_Ok;
193
 
194
};
195
 
196
// ----------------------------------------------------------------------------
197
// ----------------------------------------------------------------------------
198
 
199
pascal int RemoveFromTimeoutList(hTCB ht)
200
{
201
    TCB *t;
202
 
203
        if (ht < 0 || ht >= NR_TCB)
204
                throw E_BadHandle;
205
    t = &tcbs[ht];
206
    if (t->next) {
207
       tcbs[t->next].prev = t->prev;
208
       tcbs[t->next].timeout += t->timeout;
209
    }
210
    if (t->prev >= 0)
211
       tcbs[t->prev].next = t->next;
212
    t->status = TS_NONE;
213
    t->next = -1;
214
    t->prev = -1;
215
}
216
 
217
// ----------------------------------------------------------------------------
218
// Pop the top entry from the timeout list.
219
// ----------------------------------------------------------------------------
220
 
221
hTCB PopTimeoutList()
222
{
223
    TCB *p;
224
    hTCB h;
225
 
226
    h = TimeoutList;
227
    if (TimeoutList >= 0 && TimeoutList < NR_TCB) {
228
        TimeoutList = tcbs[TimeoutList].next;
229
        if (TimeoutList >= 0 && TimeoutList < NR_TCB)
230
            tcbs[TimeoutList].prev = -1;
231
    }
232
    return h;
233
}
234
 
235
 
236
// ----------------------------------------------------------------------------
237
// ----------------------------------------------------------------------------
238
 
239
void DumpTaskList()
240
{
241
     TCB *p, *q;
242
     int n;
243
     int kk;
244
     hTCB h, j;
245
 
246
//     printf("pi is ");
247
//     prtdbl(3.141592653589793238,10,6,'E');
248
     printf("CPU Pri Stat Task Prev Next Timeout\r\n");
249
     for (n = 0; n < 8; n++) {
250
         h = readyQ[n];
251
         if (h >= 0 && h < NR_TCB) {
252
             q = &tcbs[h];
253
             p = q;
254
             kk = 0;
255
             do {
256
//                 if (!chkTCB(p)) {
257
//                     printf("Bad TCB (%X)\r\n", p);
258
//                     break;
259
//                 }
260
                   j = p - tcbs;
261
                 printf("%3d %3d  %02X  %04X %04X %04X %08X %08X\r\n", p->affinity, p->priority, p->status, (int)j, p->prev, p->next, p->timeout, p->ticks);
262
                 if (p->next < 0 || p->next >= NR_TCB)
263
                     break;
264
                 p = &tcbs[p->next];
265
                 if (getcharNoWait()==3)
266
                    goto j1;
267
                 kk = kk + 1;
268
             } while (p != q && kk < 10);
269
         }
270
     }
271
     printf("Waiting tasks\r\n");
272
     h = TimeoutList;
273
     while (h >= 0 && h < NR_TCB) {
274
         p = &tcbs[h];
275
         printf("%3d %3d  %02X  %04X %04X %04X %08X %08X\r\n", p->affinity, p->priority, p->status, (int)j, p->prev, p->next, p->timeout, p->ticks);
276
         h = p->next;
277
         if (getcharNoWait()==3)
278
            goto j1;
279
     }
280
j1:  ;
281
}
282
 
283
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.