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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [stdalone/] [mkpart/] [main.c] - Blame information for rev 42

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

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

powered by: WebSVN 2.1.0

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