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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [stdalone/] [shpart/] [main.c] - Blame information for rev 230

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

Line No. Rev Author Line
1 18 hellwig
/*
2
 * main.c -- show partitions on a disk
3
 */
4
 
5
 
6
#include "types.h"
7
#include "stdarg.h"
8
#include "iolib.h"
9
#include "start.h"
10
#include "idedsk.h"
11
 
12
 
13
#define NPE             (SECTOR_SIZE / sizeof(PartEntry))
14
#define DESCR_SIZE      20
15
 
16
 
17
typedef struct {
18
  unsigned long type;
19
  unsigned long start;
20
  unsigned long size;
21
  char descr[DESCR_SIZE];
22
} PartEntry;
23
 
24
PartEntry ptr[NPE];
25
 
26
 
27
/**************************************************************/
28
 
29
 
30
void error(char *fmt, ...) {
31
  va_list ap;
32
 
33
  va_start(ap, fmt);
34
  printf("Error: ");
35
  vprintf(fmt, ap);
36
  printf(", halting...\n");
37
  va_end(ap);
38
  while (1) ;
39
}
40
 
41
 
42
/**************************************************************/
43
 
44
 
45
unsigned long getNumber(unsigned char *p) {
46
  return (unsigned long) *(p + 0) << 24 |
47
         (unsigned long) *(p + 1) << 16 |
48
         (unsigned long) *(p + 2) <<  8 |
49
         (unsigned long) *(p + 3) <<  0;
50
}
51
 
52
 
53
void convertPartitionTable(PartEntry *e, int n) {
54
  int i;
55
  unsigned char *p;
56
 
57
  for (i = 0; i < n; i++) {
58
    p = (unsigned char *) &e[i];
59
    e[i].type = getNumber(p + 0);
60
    e[i].start = getNumber(p + 4);
61
    e[i].size = getNumber(p + 8);
62
  }
63
}
64
 
65
 
66
/**************************************************************/
67
 
68
 
69
static char *exceptionCause[32] = {
70
  /* 00 */  "terminal 0 transmitter interrupt",
71
  /* 01 */  "terminal 0 receiver interrupt",
72
  /* 02 */  "terminal 1 transmitter interrupt",
73
  /* 03 */  "terminal 1 receiver interrupt",
74
  /* 04 */  "keyboard interrupt",
75
  /* 05 */  "unknown interrupt",
76
  /* 06 */  "unknown interrupt",
77
  /* 07 */  "unknown interrupt",
78
  /* 08 */  "disk interrupt",
79
  /* 09 */  "unknown interrupt",
80
  /* 10 */  "unknown interrupt",
81
  /* 11 */  "unknown interrupt",
82
  /* 12 */  "unknown interrupt",
83
  /* 13 */  "unknown interrupt",
84 38 hellwig
  /* 14 */  "timer 0 interrupt",
85
  /* 15 */  "timer 1 interrupt",
86 18 hellwig
  /* 16 */  "bus timeout exception",
87
  /* 17 */  "illegal instruction exception",
88
  /* 18 */  "privileged instruction exception",
89
  /* 19 */  "divide instruction exception",
90
  /* 20 */  "trap instruction exception",
91
  /* 21 */  "TLB miss exception",
92
  /* 22 */  "TLB write exception",
93
  /* 23 */  "TLB invalid exception",
94
  /* 24 */  "illegal address exception",
95
  /* 25 */  "privileged address exception",
96
  /* 26 */  "unknown exception",
97
  /* 27 */  "unknown exception",
98
  /* 28 */  "unknown exception",
99
  /* 29 */  "unknown exception",
100
  /* 30 */  "unknown exception",
101
  /* 31 */  "unknown exception"
102
};
103
 
104
 
105
int defaultISR(int irq) {
106
  printf("\n%s\n", exceptionCause[irq]);
107
  return 0;  /* do not skip any instruction */
108
}
109
 
110
 
111
void initInterrupts(void) {
112
  int i;
113
 
114
  for (i = 0; i < 32; i++) {
115
    setISR(i, defaultISR);
116
  }
117
}
118
 
119
 
120
/**************************************************************/
121
 
122
 
123
Bool checkDiskReady(void) {
124
  int tries;
125
  int i;
126
 
127
  for (tries = 0; tries < 10; tries++) {
128
    for (i = 0; i < 500000; i++) {
129
      if ((*DISK_CTRL & DISK_CTRL_READY) != 0) {
130
        return TRUE;
131
      }
132
    }
133
    printf(".");
134
  }
135
  return FALSE;
136
}
137
 
138
 
139
unsigned long getDiskSize(void) {
140
  return *DISK_CAP;
141
}
142
 
143
 
144
Bool readDisk(unsigned long sector,
145
              unsigned int count,
146
              unsigned int *addr) {
147
  unsigned int n;
148
  unsigned int *p;
149
  unsigned int i;
150
 
151
  while (count != 0) {
152
    n = count > 8 ? 8 : count;
153
    *DISK_SCT = sector;
154
    *DISK_CNT = n;
155
    *DISK_CTRL = DISK_CTRL_STRT;
156
    while ((*DISK_CTRL & DISK_CTRL_DONE) == 0) ;
157
    if (*DISK_CTRL & DISK_CTRL_ERR) {
158
      return FALSE;
159
    }
160
    p = DISK_BUFFER;
161
    for (i = 0; i < n * SECTOR_SIZE / sizeof(unsigned int); i++) {
162
      *addr++ = *p++;
163
    }
164
    sector += n;
165
    count -= n;
166
  }
167
  return TRUE;
168
}
169
 
170
 
171
/**************************************************************/
172
 
173
 
174
void main(void) {
175
  unsigned long numSectors;
176
  unsigned long partLast;
177
  int i, j;
178
  char c;
179
 
180
  /* init interrupts */
181
  initInterrupts();
182
  /* check disk ready */
183
  if (!checkDiskReady()) {
184
    error("disk not ready");
185
  }
186
  /* determine disk size */
187
  numSectors = getDiskSize();
188
  printf("Disk has %lu (0x%lX) sectors.\n",
189
         numSectors, numSectors);
190
  if (numSectors < 32) {
191
    error("disk is too small");
192
  }
193
  /* read partition table record */
194
  if (!readDisk(1, 1, (unsigned int *) ptr)) {
195
    error("cannot read partition table from disk");
196
  }
197
  convertPartitionTable(ptr, NPE);
198
  /* show partition table */
199
  printf("Partitions:\n");
200
  printf(" # b type       start      last       size       description\n");
201
  for (i = 0; i < NPE; i++) {
202
    if (ptr[i].type != 0) {
203
      partLast = ptr[i].start + ptr[i].size - 1;
204
    } else {
205
      partLast = 0;
206
    }
207
    printf("%2d %s 0x%08lX 0x%08lX 0x%08lX 0x%08lX ",
208
           i,
209
           ptr[i].type & 0x80000000 ? "*" : " ",
210
           ptr[i].type & 0x7FFFFFFF,
211
           ptr[i].start,
212
           partLast,
213
           ptr[i].size);
214
    for (j = 0; j < DESCR_SIZE; j++) {
215
      c = ptr[i].descr[j];
216
      if (c == '\0') {
217
        break;
218
      }
219
      if (c >= 0x20 && c < 0x7F) {
220
        printf("%c", c);
221
      } else {
222
        printf(".");
223
      }
224
    }
225
    printf("\n");
226
  }
227
  /* done */
228
  printf("Halting...\n");
229
}

powered by: WebSVN 2.1.0

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