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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [stdalone/] [wrtmbr/] [main.c] - Blame information for rev 103

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
#define SCT_SIZE        512             /* in bytes */
11
 
12
#define DISK_BASE       0xF0400000
13
 
14
#define DISK_CTRL       0                /* word offset from DISK_BASE */
15
#define DISK_CNT        1               /* ditto */
16
#define DISK_SCT        2               /* ditto */
17
#define DISK_CAP        3               /* ditto */
18
 
19
#define DISK_BUF_STRT   0x00020000      /* word offset from DISK_BASE */
20
#define DISK_BUF_SIZE   0x00000400      /* in words */
21
 
22
#define DISK_CTRL_STRT  0x01
23
#define DISK_CTRL_IEN   0x02
24
#define DISK_CTRL_WRT   0x04
25
#define DISK_CTRL_ERR   0x08
26
#define DISK_CTRL_DONE  0x10
27
#define DISK_CTRL_RDY   0x20
28
 
29
 
30
/**************************************************************/
31
 
32
 
33
void putchar(char c) {
34
  unsigned int *base;
35
 
36
  if (c == '\n') {
37
    putchar('\r');
38
  }
39
  base = (unsigned int *) 0xF0300000;
40
  while ((*(base + 2) & 1) == 0) ;
41
  *(base + 3) = c;
42
}
43
 
44
 
45
void puts(char *s) {
46
  char c;
47
 
48
  while ((c = *s++) != '\0') {
49
    putchar(c);
50
  }
51
}
52
 
53
 
54
void printn(int n) {
55
  int a;
56
 
57
  if (n < 0) {
58
    putchar('-');
59
    n = -n;
60
  }
61
  a = n / 10;
62
  if (a != 0) {
63
    printn(a);
64
  }
65
  putchar(n % 10 + '0');
66
}
67
 
68
 
69
void printu(unsigned int n, unsigned int b) {
70
  unsigned int a;
71
 
72
  a = n / b;
73
  if (a != 0) {
74
    printu(a, b);
75
  }
76
  putchar("0123456789ABCDEF"[n % b]);
77
}
78
 
79
 
80
void printf(char *fmt, ...) {
81
  va_list ap;
82
  char c;
83
  int n;
84
  unsigned int u;
85
  char *s;
86
 
87
  va_start(ap, fmt);
88
  while (1) {
89
    while ((c = *fmt++) != '%') {
90
      if (c == '\0') {
91
        va_end(ap);
92
        return;
93
      }
94
      putchar(c);
95
    }
96
    c = *fmt++;
97
    if (c == 'd') {
98
      n = va_arg(ap, int);
99
      printn(n);
100
    } else
101
    if (c == 'u' || c == 'o' || c == 'x') {
102
      u = va_arg(ap, int);
103
      printu(u, c == 'o' ? 8 : (c == 'x' ? 16 : 10));
104
    } else
105
    if (c == 's') {
106
      s = va_arg(ap, char *);
107
      puts(s);
108
    } else {
109
      putchar(c);
110
    }
111
  }
112
}
113
 
114
 
115
/**************************************************************/
116
 
117
 
118
static char *exceptionCause[32] = {
119
  /* 00 */  "terminal 0 transmitter interrupt",
120
  /* 01 */  "terminal 0 receiver interrupt",
121
  /* 02 */  "terminal 1 transmitter interrupt",
122
  /* 03 */  "terminal 1 receiver interrupt",
123
  /* 04 */  "keyboard interrupt",
124
  /* 05 */  "unknown interrupt",
125
  /* 06 */  "unknown interrupt",
126
  /* 07 */  "unknown interrupt",
127
  /* 08 */  "disk interrupt",
128
  /* 09 */  "unknown interrupt",
129
  /* 10 */  "unknown interrupt",
130
  /* 11 */  "unknown interrupt",
131
  /* 12 */  "unknown interrupt",
132
  /* 13 */  "unknown interrupt",
133 38 hellwig
  /* 14 */  "timer 0 interrupt",
134
  /* 15 */  "timer 1 interrupt",
135 18 hellwig
  /* 16 */  "bus timeout exception",
136
  /* 17 */  "illegal instruction exception",
137
  /* 18 */  "privileged instruction exception",
138
  /* 19 */  "divide instruction exception",
139
  /* 20 */  "trap instruction exception",
140
  /* 21 */  "TLB miss exception",
141
  /* 22 */  "TLB write exception",
142
  /* 23 */  "TLB invalid exception",
143
  /* 24 */  "illegal address exception",
144
  /* 25 */  "privileged address exception",
145
  /* 26 */  "unknown exception",
146
  /* 27 */  "unknown exception",
147
  /* 28 */  "unknown exception",
148
  /* 29 */  "unknown exception",
149
  /* 30 */  "unknown exception",
150
  /* 31 */  "unknown exception"
151
};
152
 
153
 
154
int defaultISR(int irq) {
155
  printf("\n%s\n", exceptionCause[irq]);
156
  return 0;  /* do not skip any instruction */
157
}
158
 
159
 
160
void initInterrupts(void) {
161
  int i;
162
 
163
  for (i = 0; i < 32; i++) {
164
    setISR(i, defaultISR);
165
  }
166
}
167
 
168
 
169
/**************************************************************/
170
 
171
 
172
/* the MBR buffer need not be word-aligned */
173
unsigned char mbr[SCT_SIZE] = {
174
  #include "mbr/mbr.dump"
175
};
176
 
177
/* the following two buffers must be word-aligned */
178
unsigned int wrBuf[SCT_SIZE / sizeof(unsigned int)];
179
unsigned int rdBuf[SCT_SIZE / sizeof(unsigned int)];
180
 
181
 
182
void copyMBR(void) {
183
  unsigned char *p;
184
  unsigned char *q;
185
  int i;
186
 
187
  p = (unsigned char *) wrBuf;
188
  q = (unsigned char *) mbr;
189
  for (i = 0; i < SCT_SIZE; i++) {
190
    *p++ = *q++;
191
  }
192
}
193
 
194
 
195
int compareMBR(void) {
196
  unsigned char *p;
197
  unsigned char *q;
198
  int i;
199
 
200
  p = (unsigned char *) rdBuf;
201
  q = (unsigned char *) mbr;
202
  for (i = 0; i < SCT_SIZE; i++) {
203
    if (*p++ != *q++) {
204
      return 0;
205
    }
206
  }
207
  return 1;
208
}
209
 
210
 
211
void clearCtrl(void) {
212
  unsigned int *p;
213
  int i;
214
 
215
  p = (unsigned int *) DISK_BASE + DISK_BUF_STRT;
216
  for (i = 0; i < DISK_BUF_SIZE; i++) {
217
    *p++ = 0;
218
  }
219
}
220
 
221
 
222
void copyToCtrl(void) {
223
  unsigned int *p;
224
  unsigned int *q;
225
  int i;
226
 
227
  p = (unsigned int *) DISK_BASE + DISK_BUF_STRT;
228
  q = (unsigned int *) wrBuf;
229
  for (i = 0; i < SCT_SIZE / sizeof(unsigned int); i++) {
230
    *p++ = *q++;
231
  }
232
}
233
 
234
 
235
void copyFromCtrl(void) {
236
  unsigned int *p;
237
  unsigned int *q;
238
  int i;
239
 
240
  p = (unsigned int *) rdBuf;
241
  q = (unsigned int *) DISK_BASE + DISK_BUF_STRT;
242
  for (i = 0; i < SCT_SIZE / sizeof(unsigned int); i++) {
243
    *p++ = *q++;
244
  }
245
}
246
 
247
 
248
int checkDiskReady(void) {
249
  unsigned int *p;
250
  int tries;
251
  int i;
252
 
253
  p = (unsigned int *) DISK_BASE;
254
  for (tries = 0; tries < 10; tries++) {
255
    for (i = 0; i < 500000; i++) {
256
      if ((*(p + DISK_CTRL) & DISK_CTRL_RDY) != 0) {
257
        return 1;
258
      }
259
    }
260
    printf(".");
261
  }
262
  return 0;
263
}
264
 
265
 
266
void writeMBR(void) {
267
  unsigned int *p;
268
 
269
  p = (unsigned int *) DISK_BASE;
270
  *(p + DISK_CNT) = 1;
271
  *(p + DISK_SCT) = 0;
272
  *(p + DISK_CTRL) = DISK_CTRL_WRT | DISK_CTRL_STRT;
273
  while ((*(p + DISK_CTRL) & DISK_CTRL_DONE) == 0) ;
274
}
275
 
276
 
277
void readMBR(void) {
278
  unsigned int *p;
279
 
280
  p = (unsigned int *) DISK_BASE;
281
  *(p + DISK_CNT) = 1;
282
  *(p + DISK_SCT) = 0;
283
  *(p + DISK_CTRL) = DISK_CTRL_STRT;
284
  while ((*(p + DISK_CTRL) & DISK_CTRL_DONE) == 0) ;
285
}
286
 
287
 
288
void main(void) {
289
  initInterrupts();
290
  printf("Checking disk ready...");
291
  if (!checkDiskReady()) {
292
    printf(" disk not ready\n");
293
  } else {
294
    printf(" ok\n");
295
    printf("Writing MBR...\n");
296
    copyMBR();
297
    clearCtrl();
298
    copyToCtrl();
299
    writeMBR();
300
    printf("Reading MBR...\n");
301
    clearCtrl();
302
    readMBR();
303
    copyFromCtrl();
304
    printf("Comparing MBR...");
305
    if (!compareMBR()) {
306
      printf(" error\n");
307
    } else {
308
      printf(" ok\n");
309
    }
310
  }
311
  printf("Halting...\n");
312
}

powered by: WebSVN 2.1.0

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