| 1 |
27 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
//
|
| 3 |
|
|
// Filename: ksetup.c
|
| 4 |
|
|
//
|
| 5 |
|
|
// Project: CMod S6 System on a Chip, ZipCPU demonstration project
|
| 6 |
|
|
//
|
| 7 |
|
|
// Purpose: These are the routines from kernel.c that didn't need to be
|
| 8 |
|
|
// in RAM memory. They are specifically the pre-run setup
|
| 9 |
|
|
// routines.
|
| 10 |
|
|
//
|
| 11 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
| 12 |
|
|
// Gisselquist Technology, LLC
|
| 13 |
|
|
//
|
| 14 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
| 15 |
|
|
//
|
| 16 |
|
|
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
|
| 17 |
|
|
//
|
| 18 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
| 19 |
|
|
// modify it under the terms of the GNU General Public License as published
|
| 20 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
| 21 |
|
|
// your option) any later version.
|
| 22 |
|
|
//
|
| 23 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
| 24 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
| 25 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 26 |
|
|
// for more details.
|
| 27 |
|
|
//
|
| 28 |
|
|
// You should have received a copy of the GNU General Public License along
|
| 29 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
| 30 |
|
|
// target there if the PDF file isn't present.) If not, see
|
| 31 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
| 32 |
|
|
//
|
| 33 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
| 34 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
| 35 |
|
|
//
|
| 36 |
|
|
//
|
| 37 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
| 38 |
|
|
//
|
| 39 |
|
|
//
|
| 40 |
|
|
#include "zipsys.h"
|
| 41 |
|
|
#include "board.h"
|
| 42 |
|
|
#include "ksched.h"
|
| 43 |
|
|
#include "kfildes.h"
|
| 44 |
|
|
#include "taskp.h"
|
| 45 |
|
|
#include "syspipe.h"
|
| 46 |
|
|
#include "ktraps.h"
|
| 47 |
|
|
#include "errno.h"
|
| 48 |
|
|
#include "swint.h"
|
| 49 |
|
|
|
| 50 |
|
|
extern int kntasks(void);
|
| 51 |
|
|
extern void kinit(TASKP *tasklist);
|
| 52 |
|
|
extern void restore_context(int *), save_context(int *);
|
| 53 |
|
|
SYSPIPE *rxpipe, *txpipe, *keypipe, *lcdpipe, *pwmpipe, *cmdpipe;
|
| 54 |
|
|
KDEVICE *pipedev, *txdev, *pwmdev;
|
| 55 |
|
|
void *heap; // = _top_of_heap; // Need to wait on startup to set this
|
| 56 |
|
|
|
| 57 |
|
|
#define CONTEXT_LENGTH 100000 // 1ms
|
| 58 |
|
|
|
| 59 |
|
|
int LAST_TASK;
|
| 60 |
|
|
|
| 61 |
|
|
TASKP *ksetup(void) {
|
| 62 |
|
|
TASKP *tasklist;
|
| 63 |
|
|
IOSPACE *sys = (IOSPACE *)IOADDR;
|
| 64 |
|
|
|
| 65 |
|
|
sys->io_spio = 0x0f0;
|
| 66 |
|
|
sys->io_timb = 0; // Turn off the watchdog timer
|
| 67 |
|
|
LAST_TASK = kntasks();
|
| 68 |
|
|
heap = _top_of_heap;
|
| 69 |
|
|
|
| 70 |
|
|
pipedev = sys_malloc(sizeof(KDEVICE));
|
| 71 |
|
|
pipedev->write = (RWFDFUN)kwrite_syspipe;
|
| 72 |
|
|
pipedev->read = (RWFDFUN)kread_syspipe;
|
| 73 |
|
|
pipedev->close = NULL;
|
| 74 |
|
|
|
| 75 |
|
|
txdev = pwmdev = pipedev;
|
| 76 |
|
|
|
| 77 |
|
|
rxpipe = new_syspipe(16); //rxpipe->m_wrtask=INTERRUPT_WRITE_TASK;
|
| 78 |
|
|
txpipe = new_syspipe(8); txpipe->m_rdtask = INTERRUPT_READ_TASK;
|
| 79 |
|
|
keypipe = new_syspipe(16);
|
| 80 |
|
|
lcdpipe = new_syspipe(16);
|
| 81 |
|
|
pwmpipe = new_syspipe(256); pwmpipe->m_rdtask= INTERRUPT_READ_TASK;
|
| 82 |
|
|
cmdpipe = new_syspipe(16);
|
| 83 |
|
|
|
| 84 |
|
|
tasklist = sys_malloc(sizeof(TASKP)*(1+LAST_TASK));
|
| 85 |
|
|
kinit(tasklist);
|
| 86 |
|
|
tasklist[LAST_TASK] = new_task(2, idle_task);
|
| 87 |
|
|
|
| 88 |
|
|
// Turn all interrupts off, acknowledge all at the same time
|
| 89 |
|
|
sys->io_pic = 0x7fff7fff;
|
| 90 |
|
|
|
| 91 |
|
|
sys->io_tima = CONTEXT_LENGTH | TM_REPEAT;
|
| 92 |
|
|
|
| 93 |
|
|
{
|
| 94 |
|
|
// Reset our wishbone scope for debug later
|
| 95 |
|
|
SCOPE *scope = (SCOPE *)SCOPEADDR;
|
| 96 |
|
|
scope->s_control = 2;
|
| 97 |
|
|
}
|
| 98 |
|
|
sys->io_spio = 0x0f1;
|
| 99 |
|
|
|
| 100 |
|
|
return tasklist;
|
| 101 |
|
|
}
|
| 102 |
|
|
|
| 103 |
|
|
void kwait_on_buttonpress(void) {
|
| 104 |
|
|
IOSPACE *sys = (IOSPACE *)IOADDR;
|
| 105 |
|
|
|
| 106 |
|
|
// Wait on a button press before starting
|
| 107 |
|
|
while((sys->io_spio & 0x0f0)==0)
|
| 108 |
|
|
;
|
| 109 |
|
|
sys->io_spio = 0x0f3;
|
| 110 |
|
|
for(int i=0; i<40000; i++)
|
| 111 |
|
|
sys->io_spio = ((i>>14)&2)|0x020;
|
| 112 |
|
|
sys->io_spio = 0x0f7;
|
| 113 |
|
|
}
|
| 114 |
|
|
|
| 115 |
|
|
void kuserexit(int a) {
|
| 116 |
|
|
syscall(TRAPID_EXIT, a, 0, 0);
|
| 117 |
|
|
}
|
| 118 |
|
|
|
| 119 |
|
|
void *sys_malloc(int sz) {
|
| 120 |
|
|
{
|
| 121 |
|
|
SCOPE *s = (SCOPE *)SCOPEADDR;
|
| 122 |
|
|
s->s_control = TRIGGER_SCOPE_NOW | (s->s_control & 0x0ffff);
|
| 123 |
|
|
}
|
| 124 |
|
|
|
| 125 |
|
|
void *res = heap;
|
| 126 |
|
|
heap += sz;
|
| 127 |
|
|
if ((int)heap > ((int)&res)-32) {
|
| 128 |
|
|
IOSPACE *sys = (IOSPACE *)IOADDR;
|
| 129 |
|
|
sys->io_spio = 0xf3;
|
| 130 |
|
|
asm("break 0");
|
| 131 |
|
|
}
|
| 132 |
|
|
return res;
|
| 133 |
|
|
}
|
| 134 |
|
|
|