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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [sw/] [zipos/] [kernel.c] - Blame information for rev 44

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 22 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    kernel.c
4
//
5
// Project:     CMod S6 System on a Chip, ZipCPU demonstration project
6
//
7
// Purpose:     If you are looking for a main() program associated with the
8
//              ZipOS, this is it.  This is the main program for the supervisor
9
//      task.  It handles interrupt processing, creating tasks, context swaps,
10
//      creating tasks, and ... just about everything else a kernel must handle.
11
//
12
// Creator:     Dan Gisselquist, Ph.D.
13
//              Gisselquist Technology, LLC
14
//
15
////////////////////////////////////////////////////////////////////////////////
16
//
17
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
18
//
19
// This program is free software (firmware): you can redistribute it and/or
20
// modify it under the terms of  the GNU General Public License as published
21
// by the Free Software Foundation, either version 3 of the License, or (at
22
// your option) any later version.
23
//
24
// This program is distributed in the hope that it will be useful, but WITHOUT
25
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
26
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27
// for more details.
28
//
29
// You should have received a copy of the GNU General Public License along
30
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
31
// target there if the PDF file isn't present.)  If not, see
32
// <http://www.gnu.org/licenses/> for a copy.
33
//
34
// License:     GPL, v3, as defined and found on www.gnu.org,
35
//              http://www.gnu.org/licenses/gpl.html
36
//
37
//
38
////////////////////////////////////////////////////////////////////////////////
39
//
40
//
41
 
42
#include "zipsys.h"
43
#include "board.h"
44
#include "ksched.h"
45
#include "kfildes.h"
46
#include "taskp.h"
47
#include "syspipe.h"
48
#include "ktraps.h"
49
#include "errno.h"
50
#include "swint.h"
51
 
52
extern  void    kpanic(void);
53
extern  void    raw_put_uart(int val);
54
 
55
unsigned int    nresets = 0;
56
 
57
extern int      kntasks(void);
58
extern void     kinit(TASKP *tasklist);
59
extern  void    restore_context(int *), save_context(int *);
60
SYSPIPE *rxpipe, *txpipe, *keypipe, *lcdpipe, *pwmpipe, *cmdpipe;
61
KDEVICE *pipedev, *txdev, *pwmdev;
62
void    *heap; //  = _top_of_heap; // Need to wait on startup to set this
63
 
64 37 dgisselq
#define CONTEXT_LENGTH  80000   // 1ms
65 22 dgisselq
#define TICKS_PER_SECOND        1000
66
 
67
void kwrite_audio(TASKP tsk, int dev, int *dst, int len);
68
void kwrite_txuart(TASKP tsk, int dev, int *dst, int len);
69
int     kpost(TASKP *task, unsigned events, int milliseconds);
70
TASKP   kschedule(int LAST_TASK, TASKP *tasklist, TASKP last);
71 27 dgisselq
extern TASKP    *ksetup(void);
72 22 dgisselq
 
73
int     LAST_TASK;
74
 
75
void    kernel_entry(void) {
76
        int     nheartbeats= 0, tickcount = 0, milliseconds=0, ticks = 0;
77 27 dgisselq
        int     audiostate = 0, buttonstate = 0;
78 22 dgisselq
        TASKP   *tasklist, current;
79
        int     *last_context;
80
        IOSPACE *sys = (IOSPACE *)IOADDR;
81
 
82 27 dgisselq
        tasklist = ksetup();
83 22 dgisselq
 
84 27 dgisselq
        current = tasklist[0];
85 22 dgisselq
        restore_context(current->context);
86
        last_context = current->context;
87
 
88
        unsigned enableset =
89
                INT_ENABLEV(INT_BUTTON)
90
                |INT_ENABLEV(INT_TIMA)
91
                // |INT_ENABLEV(INT_UARTRX)
92
                // |INT_ENABLEV(INT_UARTTX) // Needs to be turned on by driver
93
                // |INT_ENABLEV(INT_AUDIO // Needs to be turned on by driver)
94
                // |INT_ENABLEV(INT_GPIO)
95
                // |INT_ENABLEV(INT_TIMB);
96
                ;
97
        // Then selectively turn some of them back on
98 27 dgisselq
        sys->io_pic = INT_ENABLE | enableset | 0x07fff;
99 22 dgisselq
 
100
        do {
101
                int need_resched = 0, context_has_been_saved, pic;
102
                nheartbeats++;
103
 
104
                zip_rtu();
105
 
106
                last_context = current->context;
107
                context_has_been_saved = 0;
108
                pic = sys->io_pic;
109
 
110
                if (pic & 0x8000) { // If there's an active interrupt
111
                        // Interrupt processing
112
                        sys->io_spio = 0x44;
113
 
114
                        // First, turn off pending interrupts
115
                        // Although we migt just write 0x7fff7fff to the
116
                        // interrupt controller, how do we know another
117
                        // interrupt hasn't taken place since we read it?
118
                        // Thus we turn off the pending interrupts that we
119
                        // know about.
120
                        pic &= 0x7fff;
121
                        // Acknowledge current ints, and turn off pending ints
122
                        sys->io_pic = INT_DISABLEV(pic)|(INT_CLEAR(pic));
123
                        if(pic&INT_TIMA) {
124
                                if (++ticks >= TICKS_PER_SECOND) {//(pic & SYSINT_PPS)
125
                                        // Toggle the low order LED
126
                                        tickcount++;
127
                                        ticks = 0;
128
                                        sys->io_spio = ((sys->io_spio&1)^1)|0x010;
129
                                        pic |= SWINT_CLOCK;
130
                                }
131 27 dgisselq
                                if (buttonstate)
132
                                        buttonstate--;
133
                                else
134
                                        enableset |= INT_ENABLEV(INT_BUTTON);
135 22 dgisselq
                        }
136
                        // 
137
                        if (pic&INT_BUTTON) {
138
                                // Need to turn the button interrupt off
139
                                enableset &= ~(INT_ENABLEV(INT_BUTTON));
140
                                if ((sys->io_spio&0x0f0)==0x030)
141
                                        kpanic();
142 27 dgisselq
                                buttonstate = 3;
143
                        }
144 22 dgisselq
                        if (pic & INT_UARTRX) {
145
                                int v = sys->io_uart;
146
 
147
                                if ((v & (~0x7f))==0) {
148
                                        kpush_syspipe(rxpipe, v);
149
 
150
                                        // Local Echo
151 27 dgisselq
                                        if (pic & INT_UARTTX) {
152 22 dgisselq
                                                sys->io_uart = v;
153 27 dgisselq
                                                sys->io_pic = INT_UARTTX;
154
                                                pic &= ~INT_UARTTX;
155
                                        }
156 22 dgisselq
                                }
157
                        } if (pic & INT_UARTTX) {
158 27 dgisselq
                                int     v;
159
                                if (kpop_syspipe(txpipe, &v)==0) {
160 22 dgisselq
                                        enableset |= (INT_ENABLEV(INT_UARTTX));
161 27 dgisselq
                                        sys->io_uart= v;
162 22 dgisselq
                                        sys->io_pic = INT_UARTTX;
163 27 dgisselq
                                        // if (v == 'W')
164
                                                // sys->io_timb = 5;
165
                                                // 75k was writing the 'e'
166
                                } else
167 22 dgisselq
                                        enableset &= ~(INT_ENABLEV(INT_UARTTX));
168
                        } if (audiostate) {
169
                                if (pic & INT_AUDIO) {
170
                                int v;
171
                                // States: 
172
                                //      0 -- not in use
173
                                //      1 -- First sample, buffer empty
174
                                //              time to read a new sample
175
                                //      2 -- second sample,  to read new
176
                                //      3 -- Need to turn off
177
                                if ((audiostate & 3)==2) {
178
                                        sys->io_pwm_audio = (audiostate>>2)&0x0ffff;
179
                                        audiostate = 1;
180
                                } else if (kpop_syspipe(pwmpipe, &v)==0) {
181
                                        audiostate = (2|(v<<2))&0x03ffff;
182
                                        sys->io_pwm_audio = (v>>16)&0x0ffff;
183
                                } else {
184
                                        audiostate = 0;
185
                                        // Turn the device off
186
                                        sys->io_pwm_audio = 0x10000;
187
                                        // Turn the interrupts off
188
                                        enableset &= ~(INT_ENABLEV(INT_AUDIO));
189
                                        sys->io_spio = 0x020;
190
                                }
191
 
192
                                // This particular interrupt cannot be cleared
193
                                // until the port has been written to.  Hence,
194
                                // now that we've written to the port, we clear
195 27 dgisselq
                                // it now.  If it needs retriggering, the port
196
                                // will retrigger itself -- despite being
197
                                // cleared here.
198 22 dgisselq
                                sys->io_pic = INT_AUDIO;
199
                        }} else { // if (audiostate == 0)
200 27 dgisselq
                                int     sample;
201
                                if (kpop_syspipe(pwmpipe, &sample)==0) {
202
                                        audiostate = (2|(sample<<2))&0x03ffff;
203
                                        sys->io_pwm_audio = 0x310000 | ((sample>>16)&0x0ffff);
204 22 dgisselq
                                        enableset |= (INT_ENABLEV(INT_AUDIO));
205
                                        sys->io_spio = 0x022;
206
                                        sys->io_pic = INT_AUDIO;
207
                                } // else sys->io_spio = 0x020;
208 29 dgisselq
                        }
209
                        milliseconds = kpost(tasklist, pic, milliseconds);
210 22 dgisselq
 
211
                        // Restart interrupts
212
                        enableset &= (~0x0ffff); // Keep the bottom bits off
213
                        sys->io_pic = INT_ENABLE|enableset;
214
                } else {
215
                        sys->io_pic = INT_ENABLE; // Make sure interrupts are on
216 27 dgisselq
                        int     sample;
217 22 dgisselq
 
218
                        // Check for the beginning of an audio pipe.  If the
219
                        // interrupt is not enabled, we still might need to
220
                        // enable it.
221 27 dgisselq
                        if ((audiostate==0)&&(kpop_syspipe(pwmpipe, &sample)==0)) {
222
                                audiostate = (2|(sample<<2))&0x03ffff;
223
                                sys->io_pwm_audio = 0x310000 | ((sample>>16)&0x0ffff);
224
                                sys->io_pic = INT_AUDIO;
225 22 dgisselq
                                enableset |= (INT_ENABLEV(INT_AUDIO));
226
                                sys->io_spio = 0x022;
227
                        } // else sys->io_spio = 0x020;
228
 
229
                        // Or the beginning of a transmit pipe.  
230
                        if (pic & INT_UARTTX) {
231 27 dgisselq
                                int     v;
232
                                if (kpop_syspipe(txpipe, &v)==0) {
233 22 dgisselq
                                        enableset |= (INT_ENABLEV(INT_UARTTX));
234 27 dgisselq
                                        sys->io_uart = v;
235 22 dgisselq
                                        sys->io_pic = INT_UARTTX;
236 27 dgisselq
                                        // if (v == 'W')
237
                                                // sys->io_timb = 5;
238
                                } else
239 22 dgisselq
                                        enableset &= ~(INT_ENABLEV(INT_UARTTX));
240
                        }
241
 
242
                        // What if someone left interrupts off?
243
                        // This might happen as part of a wait trap call, such
244
                        // as syspipe() accomplishes within uwrite_syspipe()
245
                        // (We also might've just turned them off ... ooops)
246
                        enableset &= (~0x0ffff); // Keep the bottom bits off
247
                        sys->io_pic = INT_ENABLE | enableset;
248
                }
249
                sys->io_spio = 0x40;
250
 
251
                int zcc = zip_ucc();
252
                if (zcc & CC_TRAPBIT) {
253
                        // sys->io_spio = 0x0ea;
254
 
255
                        context_has_been_saved = 1;
256
                        save_context(last_context);
257
                        last_context[14] = zcc & (~CC_TRAPBIT);
258
                        // Do trap handling
259
                        switch(last_context[1]) {
260
                        case TRAPID_WAIT:
261
                                { // The task wishes to wait on an interrupt
262
                                int ilist, timeout;
263
                                ilist = last_context[2];
264
                                timeout= last_context[3];
265 44 dgisselq
                                last_context[1] = ilist & current->pending;
266 22 dgisselq
                                if (current->pending & ilist) {
267
                                        // Clear upon any read
268
                                        current->pending &= (~last_context[1]);
269
                                } else {
270
                                        current->waitsig = ilist;
271
                                        if (timeout != 0) {
272
                                                current->state = SCHED_WAITING;
273
                                                need_resched = 1;
274
                                                if (timeout > 0) {
275
                                                        current->timeout=milliseconds+timeout;
276
                                                        current->waitsig |= SWINT_TIMEOUT;
277
                                                }
278
                                        }
279
                                }} break;
280
                        case TRAPID_CLEAR:
281
                                { unsigned timeout;
282
                                // The task wishes to clear any pending
283
                                // interrupts, in a likely attempt to create
284
                                // them soon.
285
                                last_context[1] = last_context[2] & current->pending;
286
                                // Clear upon any read
287
                                current->pending &= (~last_context[1]);
288
                                timeout = (unsigned)last_context[2];
289
                                if (timeout) {
290
                                        if ((int)timeout < 0)
291 44 dgisselq
                                                // Turn off any pending timeout
292 22 dgisselq
                                                current->pending &= (~SWINT_TIMEOUT);
293
                                        else
294 44 dgisselq
                                                // Otherwise, start a timeout
295
                                                // counter
296 22 dgisselq
                                                current->timeout = milliseconds+timeout;
297
                                }} break;
298
                        case TRAPID_POST:
299
                                kpost(tasklist, last_context[2]&(~0x07fff),
300
                                                milliseconds);
301
                                break;
302
                        case TRAPID_YIELD:
303
                                need_resched = 1;
304
                                break;
305
                        case TRAPID_READ:
306
                                {
307
                                KFILDES *fd = NULL;
308
                                if ((unsigned)last_context[2]
309
                                                < (unsigned)MAX_KFILDES)
310
                                        fd = current->fd[last_context[2]];
311
                                if ((!fd)||(!fd->dev))
312
                                        last_context[1] = -EBADF;
313
                                else
314
                                        fd->dev->read(current, fd->id,
315
                                           (void *)last_context[3], last_context[4]);
316
                                } break;
317
                        case TRAPID_WRITE:
318
                                { KFILDES       *fd = NULL;
319
                                if ((unsigned)last_context[2]
320
                                                < (unsigned)MAX_KFILDES)
321
                                        fd = current->fd[last_context[2]];
322
                                else { kpanic(); zip_halt(); }
323
                                if ((!fd)||(!fd->dev))
324
                                        last_context[1] = -EBADF;
325 29 dgisselq
                                else {
326 22 dgisselq
                                        fd->dev->write(current, fd->id,
327
                                           (void *)last_context[3], last_context[4]);
328 29 dgisselq
                                }}
329
                                break;
330 22 dgisselq
                        case TRAPID_TIME:
331
                                last_context[1] = tickcount;
332
                                break;
333
                        case TRAPID_MALLOC:
334
                                last_context[1] = (int)sys_malloc(last_context[2]);
335
                                break;
336
                        case TRAPID_FREE:
337
                                // Our current malloc cannot free
338
                                // sys_free(last_context[2])
339
                                break;
340
                        case TRAPID_EXIT:
341
                                current->state = SCHED_EXIT;
342
                                need_resched = 1;
343
                                kpanic();
344
                                zip_halt();
345
                                break;
346
                        default:
347
                                current->state = SCHED_ERR;
348
                                need_resched = 1;
349
                                kpanic();
350
                                zip_halt();
351
                                break;
352
                        }
353
 
354
                        restore_context(last_context);
355
                } else if (zcc & (CC_BUSERR|CC_DIVERR|CC_FPUERR|CC_ILL)) {
356
                        current->state = SCHED_ERR;
357
                        // current->errno = -EBUS;
358
                        current->errno = (int)sys->io_buserr;
359
                        save_context(last_context);
360
                        context_has_been_saved = 1;
361
                        kpanic();
362
                        zip_halt();
363
                }
364
 
365
                if ((need_resched)||(current->state != SCHED_READY)
366
                        ||(current == tasklist[LAST_TASK]))
367
                        current = kschedule(LAST_TASK, tasklist, current);
368
 
369
                if (current->context != last_context) {
370
                        // Swap contexts
371
                        if (!context_has_been_saved)
372
                                save_context(last_context);
373
                        restore_context(current->context);
374
                }
375
        } while(1);
376
}
377
 
378
TASKP   kschedule(int LAST_TASK, TASKP *tasklist, TASKP last) {
379
        TASKP   current = tasklist[LAST_TASK];
380
        int nxtid = 0, i;
381
 
382
        // What task were we just running?
383
        for(i=0; i<=LAST_TASK; i++) {
384
                if (last == tasklist[i]) {
385
                        // If we found it, then let's run the next one
386
                        nxtid = i+1;
387
                        break;
388
                }
389
        }
390
 
391
        // Now let's see if we can find the next ready task to run
392 29 dgisselq
        for(; nxtid<LAST_TASK; nxtid++) {
393 22 dgisselq
                if (tasklist[nxtid]->state == SCHED_READY) {
394
                        current=tasklist[nxtid];
395
                        break;
396
                }
397 29 dgisselq
        }
398 22 dgisselq
        // The last task (the idle task) doesn't count
399
        if (nxtid >= LAST_TASK) {
400
                nxtid = 0; // Don't automatically run idle task
401
                for(; nxtid<LAST_TASK; nxtid++)
402
                        if (tasklist[nxtid]->state == SCHED_READY) {
403
                                break;
404
                        }
405
                // Now we stop at the idle task, if nothing else is ready
406
                current = tasklist[nxtid];
407 29 dgisselq
        } return current;
408 22 dgisselq
}
409
 
410
int     kpost(TASKP *tasklist, unsigned events, int milliseconds) {
411
        int     i;
412
        if (events & INT_TIMA)
413
                milliseconds++;
414
        if (milliseconds<0) {
415
                milliseconds -= 0x80000000;
416
                for(i=0; i<=LAST_TASK; i++) {
417
                        if(tasklist[i]->timeout) {
418
                                tasklist[i]->timeout -= 0x80000000;
419
                                if (tasklist[i]->timeout==0)
420
                                        tasklist[i]->timeout++;
421
                                if ((int)tasklist[i]->timeout < milliseconds) {
422
                                        tasklist[i]->pending |= SWINT_TIMEOUT;
423
                                        tasklist[i]->timeout = 0;
424
                                }
425
                        }
426
                }
427
        } else {
428
                for(i=0; i<=LAST_TASK; i++) {
429
                        if(tasklist[i]->timeout) {
430
                                if (tasklist[i]->timeout < (unsigned)milliseconds) {
431
                                        tasklist[i]->pending |= SWINT_TIMEOUT;
432
                                        tasklist[i]->timeout = 0;
433
                                }
434
                        }
435
                }
436
        } for(i=0; i<=LAST_TASK; i++) {
437
                tasklist[i]->pending |= events;
438
                if ((tasklist[i]->state == SCHED_WAITING)
439
                                &&(tasklist[i]->waitsig&tasklist[i]->pending)) {
440
                        tasklist[i]->state = SCHED_READY;
441
                        tasklist[i]->context[1] = tasklist[i]->waitsig & tasklist[i]->pending;
442
                        tasklist[i]->pending &= (~tasklist[i]->context[1]);
443
                        tasklist[i]->waitsig = 0;
444
                }
445
        } return milliseconds;
446
}
447
 
448
 

powered by: WebSVN 2.1.0

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