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

Subversion Repositories s6soc

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

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
#define CONTEXT_LENGTH  100000  // 1ms
65
#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
                                milliseconds++;
125
                                if (++ticks >= TICKS_PER_SECOND) {//(pic & SYSINT_PPS)
126
                                        // Toggle the low order LED
127
                                        tickcount++;
128
                                        ticks = 0;
129
                                        sys->io_spio = ((sys->io_spio&1)^1)|0x010;
130
                                        pic |= SWINT_CLOCK;
131
                                }
132 27 dgisselq
                                if (buttonstate)
133
                                        buttonstate--;
134
                                else
135
                                        enableset |= INT_ENABLEV(INT_BUTTON);
136 22 dgisselq
                        }
137
                        // 
138
                        if (pic&INT_BUTTON) {
139
                                // Need to turn the button interrupt off
140
                                enableset &= ~(INT_ENABLEV(INT_BUTTON));
141
                                if ((sys->io_spio&0x0f0)==0x030)
142
                                        kpanic();
143 27 dgisselq
                                buttonstate = 3;
144
                        }
145 22 dgisselq
                        if (pic & INT_UARTRX) {
146
                                int v = sys->io_uart;
147
 
148
                                if ((v & (~0x7f))==0) {
149
                                        kpush_syspipe(rxpipe, v);
150
 
151
                                        // Local Echo
152 27 dgisselq
                                        if (pic & INT_UARTTX) {
153 22 dgisselq
                                                sys->io_uart = v;
154 27 dgisselq
                                                sys->io_pic = INT_UARTTX;
155
                                                pic &= ~INT_UARTTX;
156
                                        }
157 22 dgisselq
                                }
158
                        } if (pic & INT_UARTTX) {
159 27 dgisselq
                                int     v;
160
                                if (kpop_syspipe(txpipe, &v)==0) {
161 22 dgisselq
                                        enableset |= (INT_ENABLEV(INT_UARTTX));
162 27 dgisselq
                                        sys->io_uart= v;
163 22 dgisselq
                                        sys->io_pic = INT_UARTTX;
164 27 dgisselq
                                        // if (v == 'W')
165
                                                // sys->io_timb = 5;
166
                                                // 75k was writing the 'e'
167
                                } else
168 22 dgisselq
                                        enableset &= ~(INT_ENABLEV(INT_UARTTX));
169
                        } if (audiostate) {
170
                                if (pic & INT_AUDIO) {
171
                                int v;
172
                                // States: 
173
                                //      0 -- not in use
174
                                //      1 -- First sample, buffer empty
175
                                //              time to read a new sample
176
                                //      2 -- second sample,  to read new
177
                                //      3 -- Need to turn off
178
                                if ((audiostate & 3)==2) {
179
                                        sys->io_pwm_audio = (audiostate>>2)&0x0ffff;
180
                                        audiostate = 1;
181
                                } else if (kpop_syspipe(pwmpipe, &v)==0) {
182
                                        audiostate = (2|(v<<2))&0x03ffff;
183
                                        sys->io_pwm_audio = (v>>16)&0x0ffff;
184
                                } else {
185
                                        audiostate = 0;
186
                                        // Turn the device off
187
                                        sys->io_pwm_audio = 0x10000;
188
                                        // Turn the interrupts off
189
                                        enableset &= ~(INT_ENABLEV(INT_AUDIO));
190
                                        sys->io_spio = 0x020;
191
                                }
192
 
193
                                // This particular interrupt cannot be cleared
194
                                // until the port has been written to.  Hence,
195
                                // now that we've written to the port, we clear
196 27 dgisselq
                                // it now.  If it needs retriggering, the port
197
                                // will retrigger itself -- despite being
198
                                // cleared here.
199 22 dgisselq
                                sys->io_pic = INT_AUDIO;
200
                        }} else { // if (audiostate == 0)
201 27 dgisselq
                                int     sample;
202
                                if (kpop_syspipe(pwmpipe, &sample)==0) {
203
                                        audiostate = (2|(sample<<2))&0x03ffff;
204
                                        sys->io_pwm_audio = 0x310000 | ((sample>>16)&0x0ffff);
205 22 dgisselq
                                        enableset |= (INT_ENABLEV(INT_AUDIO));
206
                                        sys->io_spio = 0x022;
207
                                        sys->io_pic = INT_AUDIO;
208
                                } // else sys->io_spio = 0x020;
209 29 dgisselq
                        }
210
                        milliseconds = kpost(tasklist, pic, milliseconds);
211 22 dgisselq
 
212
                        // Restart interrupts
213
                        enableset &= (~0x0ffff); // Keep the bottom bits off
214
                        sys->io_pic = INT_ENABLE|enableset;
215
                } else {
216
                        sys->io_pic = INT_ENABLE; // Make sure interrupts are on
217 27 dgisselq
                        int     sample;
218 22 dgisselq
 
219
                        // Check for the beginning of an audio pipe.  If the
220
                        // interrupt is not enabled, we still might need to
221
                        // enable it.
222 27 dgisselq
                        if ((audiostate==0)&&(kpop_syspipe(pwmpipe, &sample)==0)) {
223
                                audiostate = (2|(sample<<2))&0x03ffff;
224
                                sys->io_pwm_audio = 0x310000 | ((sample>>16)&0x0ffff);
225
                                sys->io_pic = INT_AUDIO;
226 22 dgisselq
                                enableset |= (INT_ENABLEV(INT_AUDIO));
227
                                sys->io_spio = 0x022;
228
                        } // else sys->io_spio = 0x020;
229
 
230
                        // Or the beginning of a transmit pipe.  
231
                        if (pic & INT_UARTTX) {
232 27 dgisselq
                                int     v;
233
                                if (kpop_syspipe(txpipe, &v)==0) {
234 22 dgisselq
                                        enableset |= (INT_ENABLEV(INT_UARTTX));
235 27 dgisselq
                                        sys->io_uart = v;
236 22 dgisselq
                                        sys->io_pic = INT_UARTTX;
237 27 dgisselq
                                        // if (v == 'W')
238
                                                // sys->io_timb = 5;
239
                                } else
240 22 dgisselq
                                        enableset &= ~(INT_ENABLEV(INT_UARTTX));
241
                        }
242
 
243
                        // What if someone left interrupts off?
244
                        // This might happen as part of a wait trap call, such
245
                        // as syspipe() accomplishes within uwrite_syspipe()
246
                        // (We also might've just turned them off ... ooops)
247
                        enableset &= (~0x0ffff); // Keep the bottom bits off
248
                        sys->io_pic = INT_ENABLE | enableset;
249
                }
250
                sys->io_spio = 0x40;
251
 
252
                int zcc = zip_ucc();
253
                if (zcc & CC_TRAPBIT) {
254
                        // sys->io_spio = 0x0ea;
255
 
256
                        context_has_been_saved = 1;
257
                        save_context(last_context);
258
                        last_context[14] = zcc & (~CC_TRAPBIT);
259
                        // Do trap handling
260
                        switch(last_context[1]) {
261
                        case TRAPID_WAIT:
262
                                { // The task wishes to wait on an interrupt
263
                                int ilist, timeout;
264
                                ilist = last_context[2];
265
                                timeout= last_context[3];
266
                                if (current->pending & ilist) {
267
                                        last_context[1] = ilist & current->pending;
268
                                        // Clear upon any read
269
                                        current->pending &= (~last_context[1]);
270
                                } else {
271
                                        current->waitsig = ilist;
272
                                        if (timeout != 0) {
273
                                                current->state = SCHED_WAITING;
274
                                                need_resched = 1;
275
                                                if (timeout > 0) {
276
                                                        current->timeout=milliseconds+timeout;
277
                                                        current->waitsig |= SWINT_TIMEOUT;
278
                                                }
279
                                        }
280
                                }} break;
281
                        case TRAPID_CLEAR:
282
                                { unsigned timeout;
283
                                // The task wishes to clear any pending
284
                                // interrupts, in a likely attempt to create
285
                                // them soon.
286
                                last_context[1] = last_context[2] & current->pending;
287
                                // Clear upon any read
288
                                current->pending &= (~last_context[1]);
289
                                timeout = (unsigned)last_context[2];
290
                                if (timeout) {
291
                                        if ((int)timeout < 0)
292
                                                current->pending &= (~SWINT_TIMEOUT);
293
                                        else
294
                                                current->timeout = milliseconds+timeout;
295
                                }} break;
296
                        case TRAPID_POST:
297
                                kpost(tasklist, last_context[2]&(~0x07fff),
298
                                                milliseconds);
299
                                break;
300
                        case TRAPID_YIELD:
301
                                need_resched = 1;
302
                                break;
303
                        case TRAPID_READ:
304
                                {
305
                                KFILDES *fd = NULL;
306
                                if ((unsigned)last_context[2]
307
                                                < (unsigned)MAX_KFILDES)
308
                                        fd = current->fd[last_context[2]];
309
                                if ((!fd)||(!fd->dev))
310
                                        last_context[1] = -EBADF;
311
                                else
312
                                        fd->dev->read(current, fd->id,
313
                                           (void *)last_context[3], last_context[4]);
314
                                } break;
315
                        case TRAPID_WRITE:
316
                                { KFILDES       *fd = NULL;
317
                                if ((unsigned)last_context[2]
318
                                                < (unsigned)MAX_KFILDES)
319
                                        fd = current->fd[last_context[2]];
320
                                else { kpanic(); zip_halt(); }
321
                                if ((!fd)||(!fd->dev))
322
                                        last_context[1] = -EBADF;
323 29 dgisselq
                                else {
324 22 dgisselq
                                        fd->dev->write(current, fd->id,
325
                                           (void *)last_context[3], last_context[4]);
326 29 dgisselq
                                }}
327
                                break;
328 22 dgisselq
                        case TRAPID_TIME:
329
                                last_context[1] = tickcount;
330
                                break;
331
                        case TRAPID_MALLOC:
332
                                last_context[1] = (int)sys_malloc(last_context[2]);
333
                                break;
334
                        case TRAPID_FREE:
335
                                // Our current malloc cannot free
336
                                // sys_free(last_context[2])
337
                                break;
338
                        case TRAPID_EXIT:
339
                                current->state = SCHED_EXIT;
340
                                need_resched = 1;
341
                                kpanic();
342
                                zip_halt();
343
                                break;
344
                        default:
345
                                current->state = SCHED_ERR;
346
                                need_resched = 1;
347
                                kpanic();
348
                                zip_halt();
349
                                break;
350
                        }
351
 
352
                        restore_context(last_context);
353
                } else if (zcc & (CC_BUSERR|CC_DIVERR|CC_FPUERR|CC_ILL)) {
354
                        current->state = SCHED_ERR;
355
                        // current->errno = -EBUS;
356
                        current->errno = (int)sys->io_buserr;
357
                        save_context(last_context);
358
                        context_has_been_saved = 1;
359
                        kpanic();
360
                        zip_halt();
361
                }
362
 
363
                if ((need_resched)||(current->state != SCHED_READY)
364
                        ||(current == tasklist[LAST_TASK]))
365
                        current = kschedule(LAST_TASK, tasklist, current);
366
 
367
                if (current->context != last_context) {
368
                        // Swap contexts
369
                        if (!context_has_been_saved)
370
                                save_context(last_context);
371
                        restore_context(current->context);
372
                }
373
        } while(1);
374
}
375
 
376
TASKP   kschedule(int LAST_TASK, TASKP *tasklist, TASKP last) {
377
        TASKP   current = tasklist[LAST_TASK];
378
        int nxtid = 0, i;
379
 
380
        // What task were we just running?
381
        for(i=0; i<=LAST_TASK; i++) {
382
                if (last == tasklist[i]) {
383
                        // If we found it, then let's run the next one
384
                        nxtid = i+1;
385
                        break;
386
                }
387
        }
388
 
389
        // Now let's see if we can find the next ready task to run
390 29 dgisselq
        for(; nxtid<LAST_TASK; nxtid++) {
391 22 dgisselq
                if (tasklist[nxtid]->state == SCHED_READY) {
392
                        current=tasklist[nxtid];
393
                        break;
394
                }
395 29 dgisselq
        }
396 22 dgisselq
        // The last task (the idle task) doesn't count
397
        if (nxtid >= LAST_TASK) {
398
                nxtid = 0; // Don't automatically run idle task
399
                for(; nxtid<LAST_TASK; nxtid++)
400
                        if (tasklist[nxtid]->state == SCHED_READY) {
401
                                break;
402
                        }
403
                // Now we stop at the idle task, if nothing else is ready
404
                current = tasklist[nxtid];
405 29 dgisselq
        } return current;
406 22 dgisselq
}
407
 
408
int     kpost(TASKP *tasklist, unsigned events, int milliseconds) {
409
        int     i;
410
        if (events & INT_TIMA)
411
                milliseconds++;
412
        if (milliseconds<0) {
413
                milliseconds -= 0x80000000;
414
                for(i=0; i<=LAST_TASK; i++) {
415
                        if(tasklist[i]->timeout) {
416
                                tasklist[i]->timeout -= 0x80000000;
417
                                if (tasklist[i]->timeout==0)
418
                                        tasklist[i]->timeout++;
419
                                if ((int)tasklist[i]->timeout < milliseconds) {
420
                                        tasklist[i]->pending |= SWINT_TIMEOUT;
421
                                        tasklist[i]->timeout = 0;
422
                                }
423
                        }
424
                }
425
        } else {
426
                for(i=0; i<=LAST_TASK; i++) {
427
                        if(tasklist[i]->timeout) {
428
                                if (tasklist[i]->timeout < (unsigned)milliseconds) {
429
                                        tasklist[i]->pending |= SWINT_TIMEOUT;
430
                                        tasklist[i]->timeout = 0;
431
                                }
432
                        }
433
                }
434
        } for(i=0; i<=LAST_TASK; i++) {
435
                tasklist[i]->pending |= events;
436
                if ((tasklist[i]->state == SCHED_WAITING)
437
                                &&(tasklist[i]->waitsig&tasklist[i]->pending)) {
438
                        tasklist[i]->state = SCHED_READY;
439
                        tasklist[i]->context[1] = tasklist[i]->waitsig & tasklist[i]->pending;
440
                        tasklist[i]->pending &= (~tasklist[i]->context[1]);
441
                        tasklist[i]->waitsig = 0;
442
                }
443
        } return milliseconds;
444
}
445
 
446
 

powered by: WebSVN 2.1.0

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