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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [stdalone/] [dskchk2/] [dskchk2/] [main.c] - Blame information for rev 318

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

Line No. Rev Author Line
1 198 hellwig
/*
2
 * main.c -- start the ball rolling
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 NUM_SECTORS     262144          /* sync with image generator */
14
#define LINE_SIZE       100
15
 
16
 
17
/**************************************************************/
18
 
19
 
20
void halt(void) {
21
  printf("\nHalting...\n");
22
  while (1) ;
23
}
24
 
25
 
26
void error(char *fmt, ...) {
27
  va_list ap;
28
 
29
  va_start(ap, fmt);
30
  printf("Error: ");
31
  vprintf(fmt, ap);
32
  printf("\n");
33
  va_end(ap);
34
  halt();
35
}
36
 
37
 
38
/**************************************************************/
39
 
40
 
41
static unsigned int randomState = 0;
42
 
43
 
44
void setRandomSeed(unsigned int seed) {
45
  randomState = seed;
46
}
47
 
48
 
49
unsigned int nextRandomNumber(void) {
50
  unsigned int retVal;
51
 
52
  retVal = randomState;
53
  randomState = randomState * 1103515245 + 12345;
54
  return retVal;
55
}
56
 
57
 
58
/**************************************************************/
59
 
60
 
61
static unsigned int randomSector = 0xDEADBEEF;
62
 
63
 
64
unsigned int nextRandomSector(unsigned int numSectors) {
65
  randomSector = randomSector * 1103515245 + 12345;
66
  return randomSector % numSectors;
67
}
68
 
69
 
70
/**************************************************************/
71
 
72
 
73
static char *exceptionCause[32] = {
74
  /* 00 */  "terminal 0 transmitter interrupt",
75
  /* 01 */  "terminal 0 receiver interrupt",
76
  /* 02 */  "terminal 1 transmitter interrupt",
77
  /* 03 */  "terminal 1 receiver interrupt",
78
  /* 04 */  "keyboard interrupt",
79
  /* 05 */  "unknown interrupt",
80
  /* 06 */  "unknown interrupt",
81
  /* 07 */  "unknown interrupt",
82
  /* 08 */  "disk interrupt",
83
  /* 09 */  "unknown interrupt",
84
  /* 10 */  "unknown interrupt",
85
  /* 11 */  "unknown interrupt",
86
  /* 12 */  "unknown interrupt",
87
  /* 13 */  "unknown interrupt",
88
  /* 14 */  "timer 0 interrupt",
89
  /* 15 */  "timer 1 interrupt",
90
  /* 16 */  "bus timeout exception",
91
  /* 17 */  "illegal instruction exception",
92
  /* 18 */  "privileged instruction exception",
93
  /* 19 */  "divide instruction exception",
94
  /* 20 */  "trap instruction exception",
95
  /* 21 */  "TLB miss exception",
96
  /* 22 */  "TLB write exception",
97
  /* 23 */  "TLB invalid exception",
98
  /* 24 */  "illegal address exception",
99
  /* 25 */  "privileged address exception",
100
  /* 26 */  "unknown exception",
101
  /* 27 */  "unknown exception",
102
  /* 28 */  "unknown exception",
103
  /* 29 */  "unknown exception",
104
  /* 30 */  "unknown exception",
105
  /* 31 */  "unknown exception"
106
};
107
 
108
 
109
int defaultISR(int irq) {
110
  printf("\n%s\n", exceptionCause[irq]);
111
  return 0;  /* do not skip any instruction */
112
}
113
 
114
 
115
void initInterrupts(void) {
116
  int i;
117
 
118
  for (i = 0; i < 32; i++) {
119
    setISR(i, defaultISR);
120
  }
121
}
122
 
123
 
124
/**************************************************************/
125
 
126
 
127
#define TIMER_CTRL      ((unsigned *) 0xF0000000)
128
#define TIMER_DIV       ((unsigned *) 0xF0000004)
129
 
130
 
131
int hundredth = 0;
132
 
133
 
134
int clockISR(int irq) {
135
  *TIMER_CTRL = 2;
136
  hundredth++;
137
  return 0;
138
}
139
 
140
 
141
void initClock(void) {
142
  setISR(14, clockISR);
143
  *TIMER_DIV = 500000;
144
  *TIMER_CTRL = 2;
145
  setMask(1 << 14);
146
  enable();
147
}
148
 
149
 
150
/**************************************************************/
151
 
152
 
153
unsigned int buffer[WPS];
154
 
155
 
156
int diskReady(void) {
157
  unsigned int retry;
158
 
159
  retry = READY_RETRIES;
160
  while (retry) {
161
    if (*DISK_CTRL & DISK_CTRL_READY) {
162
      return 1;
163
    }
164
    retry--;
165
  }
166
  return 0;
167
}
168
 
169
 
170
int readSector(unsigned int sector, unsigned int *buffer) {
171
  int i;
172
 
173
  if (!diskReady()) {
174
    return 0;
175
  }
176
  *DISK_SCT = sector;
177
  *DISK_CNT = 1;
178 199 hellwig
  *DISK_CTRL = *DISK_CTRL & ~(DISK_CTRL_DONE | DISK_CTRL_ERR);
179 198 hellwig
  *DISK_CTRL = *DISK_CTRL | DISK_CTRL_STRT;
180
  while ((*DISK_CTRL & DISK_CTRL_DONE) == 0) ;
181
  if (*DISK_CTRL & DISK_CTRL_ERR) {
182
    return 0;
183
  }
184
  for (i = 0; i < WPS; i++) {
185
    buffer[i] = DISK_BUFFER[i];
186
  }
187
  return 1;
188
}
189
 
190
 
191
void check(unsigned int numChecks) {
192
  unsigned int check;
193
  unsigned int sectorRequ;
194
  int i;
195
  unsigned int sectorRead;
196
  unsigned int number;
197
  unsigned int wrong, corrupted;
198
 
199
  wrong = 0;
200
  corrupted = 0;
201
  for (check = 0; check < numChecks; check++) {
202
    sectorRequ = nextRandomSector(NUM_SECTORS);
203
    if (!readSector(sectorRequ, buffer)) {
204
      error("cannot read disk");
205
    }
206
    sectorRead = buffer[0];
207
    if (sectorRead != sectorRequ) {
208
      wrong++;
209
    }
210
    setRandomSeed(sectorRead);
211
    for (i = 0; i < WPS; i++) {
212
      number = buffer[i];
213
      if (number != nextRandomNumber()) {
214
        corrupted++;
215
        break;
216
      }
217
    }
218
    printf("check #%06d: requ 0x%08x, read 0x%08x, sector %s\n",
219
           check, sectorRequ, sectorRead,
220
           i == WPS ? "ok" : "corrupted");
221
  }
222
  printf("\nTotal number of sectors: %u read, %u wrong, %u corrupted\n",
223
         numChecks, wrong, corrupted);
224
}
225
 
226
 
227
/**************************************************************/
228
 
229
 
230
void main(void) {
231
  char line[LINE_SIZE];
232
  char *p;
233
  unsigned int numChecks;
234
 
235
  initInterrupts();
236
  initClock();
237
  printf("\nIDE disk check\n\n");
238
  getLine("Please enter number of sectors to check: ", line, LINE_SIZE);
239
  numChecks = 0;
240
  p = line;
241
  while (*p >= '0' && *p <= '9') {
242
    numChecks = numChecks * 10 + (*p - '0');
243
    p++;
244
  }
245
  check(numChecks);
246
  halt();
247
}

powered by: WebSVN 2.1.0

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