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

Subversion Repositories s6soc

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

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
                                if (current->pending & ilist) {
266
                                        last_context[1] = ilist & current->pending;
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
                                                current->pending &= (~SWINT_TIMEOUT);
292
                                        else
293
                                                current->timeout = milliseconds+timeout;
294
                                }} break;
295
                        case TRAPID_POST:
296
                                kpost(tasklist, last_context[2]&(~0x07fff),
297
                                                milliseconds);
298
                                break;
299
                        case TRAPID_YIELD:
300
                                need_resched = 1;
301
                                break;
302
                        case TRAPID_READ:
303
                                {
304
                                KFILDES *fd = NULL;
305
                                if ((unsigned)last_context[2]
306
                                                < (unsigned)MAX_KFILDES)
307
                                        fd = current->fd[last_context[2]];
308
                                if ((!fd)||(!fd->dev))
309
                                        last_context[1] = -EBADF;
310
                                else
311
                                        fd->dev->read(current, fd->id,
312
                                           (void *)last_context[3], last_context[4]);
313
                                } break;
314
                        case TRAPID_WRITE:
315
                                { KFILDES       *fd = NULL;
316
                                if ((unsigned)last_context[2]
317
                                                < (unsigned)MAX_KFILDES)
318
                                        fd = current->fd[last_context[2]];
319
                                else { kpanic(); zip_halt(); }
320
                                if ((!fd)||(!fd->dev))
321
                                        last_context[1] = -EBADF;
322 29 dgisselq
                                else {
323 22 dgisselq
                                        fd->dev->write(current, fd->id,
324
                                           (void *)last_context[3], last_context[4]);
325 29 dgisselq
                                }}
326
                                break;
327 22 dgisselq
                        case TRAPID_TIME:
328
                                last_context[1] = tickcount;
329
                                break;
330
                        case TRAPID_MALLOC:
331
                                last_context[1] = (int)sys_malloc(last_context[2]);
332
                                break;
333
                        case TRAPID_FREE:
334
                                // Our current malloc cannot free
335
                                // sys_free(last_context[2])
336
                                break;
337
                        case TRAPID_EXIT:
338
                                current->state = SCHED_EXIT;
339
                                need_resched = 1;
340
                                kpanic();
341
                                zip_halt();
342
                                break;
343
                        default:
344
                                current->state = SCHED_ERR;
345
                                need_resched = 1;
346
                                kpanic();
347
                                zip_halt();
348
                                break;
349
                        }
350
 
351
                        restore_context(last_context);
352
                } else if (zcc & (CC_BUSERR|CC_DIVERR|CC_FPUERR|CC_ILL)) {
353
                        current->state = SCHED_ERR;
354
                        // current->errno = -EBUS;
355
                        current->errno = (int)sys->io_buserr;
356
                        save_context(last_context);
357
                        context_has_been_saved = 1;
358
                        kpanic();
359
                        zip_halt();
360
                }
361
 
362
                if ((need_resched)||(current->state != SCHED_READY)
363
                        ||(current == tasklist[LAST_TASK]))
364
                        current = kschedule(LAST_TASK, tasklist, current);
365
 
366
                if (current->context != last_context) {
367
                        // Swap contexts
368
                        if (!context_has_been_saved)
369
                                save_context(last_context);
370
                        restore_context(current->context);
371
                }
372
        } while(1);
373
}
374
 
375
TASKP   kschedule(int LAST_TASK, TASKP *tasklist, TASKP last) {
376
        TASKP   current = tasklist[LAST_TASK];
377
        int nxtid = 0, i;
378
 
379
        // What task were we just running?
380
        for(i=0; i<=LAST_TASK; i++) {
381
                if (last == tasklist[i]) {
382
                        // If we found it, then let's run the next one
383
                        nxtid = i+1;
384
                        break;
385
                }
386
        }
387
 
388
        // Now let's see if we can find the next ready task to run
389 29 dgisselq
        for(; nxtid<LAST_TASK; nxtid++) {
390 22 dgisselq
                if (tasklist[nxtid]->state == SCHED_READY) {
391
                        current=tasklist[nxtid];
392
                        break;
393
                }
394 29 dgisselq
        }
395 22 dgisselq
        // The last task (the idle task) doesn't count
396
        if (nxtid >= LAST_TASK) {
397
                nxtid = 0; // Don't automatically run idle task
398
                for(; nxtid<LAST_TASK; nxtid++)
399
                        if (tasklist[nxtid]->state == SCHED_READY) {
400
                                break;
401
                        }
402
                // Now we stop at the idle task, if nothing else is ready
403
                current = tasklist[nxtid];
404 29 dgisselq
        } return current;
405 22 dgisselq
}
406
 
407
int     kpost(TASKP *tasklist, unsigned events, int milliseconds) {
408
        int     i;
409
        if (events & INT_TIMA)
410
                milliseconds++;
411
        if (milliseconds<0) {
412
                milliseconds -= 0x80000000;
413
                for(i=0; i<=LAST_TASK; i++) {
414
                        if(tasklist[i]->timeout) {
415
                                tasklist[i]->timeout -= 0x80000000;
416
                                if (tasklist[i]->timeout==0)
417
                                        tasklist[i]->timeout++;
418
                                if ((int)tasklist[i]->timeout < milliseconds) {
419
                                        tasklist[i]->pending |= SWINT_TIMEOUT;
420
                                        tasklist[i]->timeout = 0;
421
                                }
422
                        }
423
                }
424
        } else {
425
                for(i=0; i<=LAST_TASK; i++) {
426
                        if(tasklist[i]->timeout) {
427
                                if (tasklist[i]->timeout < (unsigned)milliseconds) {
428
                                        tasklist[i]->pending |= SWINT_TIMEOUT;
429
                                        tasklist[i]->timeout = 0;
430
                                }
431
                        }
432
                }
433
        } for(i=0; i<=LAST_TASK; i++) {
434
                tasklist[i]->pending |= events;
435
                if ((tasklist[i]->state == SCHED_WAITING)
436
                                &&(tasklist[i]->waitsig&tasklist[i]->pending)) {
437
                        tasklist[i]->state = SCHED_READY;
438
                        tasklist[i]->context[1] = tasklist[i]->waitsig & tasklist[i]->pending;
439
                        tasklist[i]->pending &= (~tasklist[i]->context[1]);
440
                        tasklist[i]->waitsig = 0;
441
                }
442
        } return milliseconds;
443
}
444
 
445
 

powered by: WebSVN 2.1.0

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