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

Subversion Repositories s6soc

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

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

powered by: WebSVN 2.1.0

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