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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [stdalone/] [onetask/] [os/] [main.c] - Blame information for rev 262

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

Line No. Rev Author Line
1 18 hellwig
/*
2
 * main.c -- start the ball rolling
3
 */
4
 
5
 
6
#include "stdarg.h"
7
#include "start.h"
8
 
9
 
10
/**************************************************************/
11
 
12
 
13
unsigned char taskCode[] = {
14
  #include "task.dump"
15
};
16
 
17
 
18
/**************************************************************/
19
 
20
 
21
void putchar(char c) {
22
  unsigned int *base;
23
 
24
  if (c == '\n') {
25
    putchar('\r');
26
  }
27
  base = (unsigned int *) 0xF0300000;
28
  while ((*(base + 2) & 1) == 0) ;
29
  *(base + 3) = c;
30
}
31
 
32
 
33
void puts(char *s) {
34
  char c;
35
 
36
  while ((c = *s++) != '\0') {
37
    putchar(c);
38
  }
39
}
40
 
41
 
42
void printn(int n) {
43
  int a;
44
 
45
  if (n < 0) {
46
    putchar('-');
47
    n = -n;
48
  }
49
  a = n / 10;
50
  if (a != 0) {
51
    printn(a);
52
  }
53
  putchar(n % 10 + '0');
54
}
55
 
56
 
57
void printu(unsigned int n, unsigned int b) {
58
  unsigned int a;
59
 
60
  a = n / b;
61
  if (a != 0) {
62
    printu(a, b);
63
  }
64
  putchar("0123456789ABCDEF"[n % b]);
65
}
66
 
67
 
68
void printf(char *fmt, ...) {
69
  va_list ap;
70
  char c;
71
  int n;
72
  unsigned int u;
73
  char *s;
74
 
75
  va_start(ap, fmt);
76
  while (1) {
77
    while ((c = *fmt++) != '%') {
78
      if (c == '\0') {
79
        va_end(ap);
80
        return;
81
      }
82
      putchar(c);
83
    }
84
    c = *fmt++;
85
    if (c == 'd') {
86
      n = va_arg(ap, int);
87
      printn(n);
88
    } else
89
    if (c == 'u' || c == 'o' || c == 'x') {
90
      u = va_arg(ap, int);
91
      printu(u, c == 'o' ? 8 : (c == 'x' ? 16 : 10));
92
    } else
93
    if (c == 's') {
94
      s = va_arg(ap, char *);
95
      puts(s);
96
    } else {
97
      putchar(c);
98
    }
99
  }
100
}
101
 
102
 
103
/**************************************************************/
104
 
105
 
106
static char *exceptionCause[32] = {
107
  /* 00 */  "terminal 0 transmitter interrupt",
108
  /* 01 */  "terminal 0 receiver interrupt",
109
  /* 02 */  "terminal 1 transmitter interrupt",
110
  /* 03 */  "terminal 1 receiver interrupt",
111
  /* 04 */  "keyboard interrupt",
112
  /* 05 */  "unknown interrupt",
113
  /* 06 */  "unknown interrupt",
114
  /* 07 */  "unknown interrupt",
115
  /* 08 */  "disk interrupt",
116
  /* 09 */  "unknown interrupt",
117
  /* 10 */  "unknown interrupt",
118
  /* 11 */  "unknown interrupt",
119
  /* 12 */  "unknown interrupt",
120
  /* 13 */  "unknown interrupt",
121 38 hellwig
  /* 14 */  "timer 0 interrupt",
122
  /* 15 */  "timer 1 interrupt",
123 18 hellwig
  /* 16 */  "bus timeout exception",
124
  /* 17 */  "illegal instruction exception",
125
  /* 18 */  "privileged instruction exception",
126
  /* 19 */  "divide instruction exception",
127
  /* 20 */  "trap instruction exception",
128
  /* 21 */  "TLB miss exception",
129
  /* 22 */  "TLB write exception",
130
  /* 23 */  "TLB invalid exception",
131
  /* 24 */  "illegal address exception",
132
  /* 25 */  "privileged address exception",
133
  /* 26 */  "unknown exception",
134
  /* 27 */  "unknown exception",
135
  /* 28 */  "unknown exception",
136
  /* 29 */  "unknown exception",
137
  /* 30 */  "unknown exception",
138
  /* 31 */  "unknown exception"
139
};
140
 
141
 
142
void defaultISR(int irq, unsigned int *registers) {
143
  printf("\n%s\n", exceptionCause[irq]);
144
}
145
 
146
 
147
void initInterrupts(void) {
148
  int i;
149
 
150
  for (i = 0; i < 32; i++) {
151
    setISR(i, defaultISR);
152
  }
153
}
154
 
155
 
156
/**************************************************************/
157
 
158
 
159
void loadTask(unsigned char *code,
160
              int csize, unsigned int physCodeAddr,
161
              int dsize, unsigned int physDataAddr,
162
              int bsize) {
163
  unsigned char *virtLoadAddr;
164
  int i;
165
 
166
  virtLoadAddr = (unsigned char *) (0xC0000000 | physCodeAddr);
167
  for (i = 0; i < csize; i++) {
168
    *virtLoadAddr++ = *code++;
169
  }
170
  virtLoadAddr = (unsigned char *) (0xC0000000 | physDataAddr);
171
  for (i = 0; i < dsize; i++) {
172
    *virtLoadAddr++ = *code++;
173
  }
174
  for (i = 0; i < bsize; i++) {
175
    *virtLoadAddr++ = '\0';
176
  }
177
}
178
 
179
 
180
/**************************************************************/
181
 
182
 
183
void trapISR(int irq, unsigned int *registers) {
184
  /* 'putchar' is the only system call yet */
185
  putchar(registers[4]);
186
  /* skip the trap instruction */
187
  registers[30] += 4;
188
}
189
 
190
 
191
/**************************************************************/
192
 
193
 
194
void flushTLB(void) {
195
  unsigned int invalPage;
196
  int i;
197
 
198
  invalPage = 0xC0000000;
199
  for (i = 0; i < 32; i++) {
200
    setTLB(i, invalPage, 0);
201
    invalPage += (1 << 12);
202
  }
203
}
204
 
205
 
206
/**************************************************************/
207
 
208
 
209
unsigned int getNumber(unsigned char *p) {
210
  return (unsigned int) *(p + 0) << 24 |
211
         (unsigned int) *(p + 1) << 16 |
212
         (unsigned int) *(p + 2) <<  8 |
213
         (unsigned int) *(p + 3) <<  0;
214
}
215
 
216
 
217
void main(void) {
218
  unsigned int magic;
219
  unsigned int csize;
220
  unsigned int dsize;
221
  unsigned int bsize;
222
 
223
  printf("\n");
224
  printf("OS: initializing interrupts\n");
225
  initInterrupts();
226
  setISR(20, trapISR);
227
  /* load code at 256 k, data at (256 + 16) k */
228
  printf("OS: loading task\n");
229
  magic = getNumber(taskCode + 0);
230
  csize = getNumber(taskCode + 4);
231
  dsize = getNumber(taskCode + 8);
232
  bsize = getNumber(taskCode + 12);
233
  if (magic != 0x1AA09232) {
234
    printf("Error: Load module is not executable!\n");
235
    while (1) ;
236
  }
237
  printf("(csize = 0x%x, dsize = 0x%x, bsize = 0x%x)\n",
238
         csize, dsize, bsize);
239
  loadTask(taskCode + 8 * sizeof(unsigned int),
240
           csize, 64 << 12,
241
           dsize, 68 << 12,
242
           bsize);
243
  printf("OS: presetting TLB\n");
244
  flushTLB();
245
  setTLB(5, 0x00000000, 64 << 12 | 0x01);
246
  setTLB(27, (csize + 0x00000FFF) & 0xFFFFF000, 68 << 12 | 0x03);
247
  setTLB(22, 0x7FFFF000, 80 << 12 | 0x03);
248
  printf("OS: starting task\n");
249
  startTask(80 << 12);  /* stack at (256 + 64) k */
250
}

powered by: WebSVN 2.1.0

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