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

Subversion Repositories eco32

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

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
  *DISK_CTRL = *DISK_CTRL | DISK_CTRL_STRT;
179
  while ((*DISK_CTRL & DISK_CTRL_DONE) == 0) ;
180
  if (*DISK_CTRL & DISK_CTRL_ERR) {
181
    return 0;
182
  }
183
  for (i = 0; i < WPS; i++) {
184
    buffer[i] = DISK_BUFFER[i];
185
  }
186
  return 1;
187
}
188
 
189
 
190
void check(unsigned int numChecks) {
191
  unsigned int check;
192
  unsigned int sectorRequ;
193
  int i;
194
  unsigned int sectorRead;
195
  unsigned int number;
196
  unsigned int wrong, corrupted;
197
 
198
  wrong = 0;
199
  corrupted = 0;
200
  for (check = 0; check < numChecks; check++) {
201
    sectorRequ = nextRandomSector(NUM_SECTORS);
202
//----------------------------------------------
203
    if (numChecks == 1) {
204
      sectorRequ = 0x14001;
205
    }
206
//----------------------------------------------
207
    if (!readSector(sectorRequ, buffer)) {
208
      error("cannot read disk");
209
    }
210
    sectorRead = buffer[0];
211
    if (sectorRead != sectorRequ) {
212
      wrong++;
213
    }
214
    setRandomSeed(sectorRead);
215
    for (i = 0; i < WPS; i++) {
216
      number = buffer[i];
217
      if (number != nextRandomNumber()) {
218
        corrupted++;
219
        break;
220
      }
221
    }
222
    printf("check #%06d: requ 0x%08x, read 0x%08x, sector %s\n",
223
           check, sectorRequ, sectorRead,
224
           i == WPS ? "ok" : "corrupted");
225
  }
226
  printf("\nTotal number of sectors: %u read, %u wrong, %u corrupted\n",
227
         numChecks, wrong, corrupted);
228
}
229
 
230
 
231
/**************************************************************/
232
 
233
 
234
void main(void) {
235
  char line[LINE_SIZE];
236
  char *p;
237
  unsigned int numChecks;
238
 
239
  initInterrupts();
240
  initClock();
241
  printf("\nIDE disk check\n\n");
242
  getLine("Please enter number of sectors to check: ", line, LINE_SIZE);
243
  numChecks = 0;
244
  p = line;
245
  while (*p >= '0' && *p <= '9') {
246
    numChecks = numChecks * 10 + (*p - '0');
247
    p++;
248
  }
249
  check(numChecks);
250
  halt();
251
}

powered by: WebSVN 2.1.0

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