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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [stdalone/] [dmpmbr/] [main.c] - Blame information for rev 239

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

Line No. Rev Author Line
1 196 hellwig
/*
2
 * main.c -- dump the master boot record of 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
/**************************************************************/
14
 
15
 
16
void error(char *fmt, ...) {
17
  va_list ap;
18
 
19
  va_start(ap, fmt);
20
  printf("Error: ");
21
  vprintf(fmt, ap);
22
  printf(", halting...\n");
23
  va_end(ap);
24
  while (1) ;
25
}
26
 
27
 
28
/**************************************************************/
29
 
30
 
31
static char *exceptionCause[32] = {
32
  /* 00 */  "terminal 0 transmitter interrupt",
33
  /* 01 */  "terminal 0 receiver interrupt",
34
  /* 02 */  "terminal 1 transmitter interrupt",
35
  /* 03 */  "terminal 1 receiver interrupt",
36
  /* 04 */  "keyboard interrupt",
37
  /* 05 */  "unknown interrupt",
38
  /* 06 */  "unknown interrupt",
39
  /* 07 */  "unknown interrupt",
40
  /* 08 */  "disk interrupt",
41
  /* 09 */  "unknown interrupt",
42
  /* 10 */  "unknown interrupt",
43
  /* 11 */  "unknown interrupt",
44
  /* 12 */  "unknown interrupt",
45
  /* 13 */  "unknown interrupt",
46
  /* 14 */  "timer 0 interrupt",
47
  /* 15 */  "timer 1 interrupt",
48
  /* 16 */  "bus timeout exception",
49
  /* 17 */  "illegal instruction exception",
50
  /* 18 */  "privileged instruction exception",
51
  /* 19 */  "divide instruction exception",
52
  /* 20 */  "trap instruction exception",
53
  /* 21 */  "TLB miss exception",
54
  /* 22 */  "TLB write exception",
55
  /* 23 */  "TLB invalid exception",
56
  /* 24 */  "illegal address exception",
57
  /* 25 */  "privileged address exception",
58
  /* 26 */  "unknown exception",
59
  /* 27 */  "unknown exception",
60
  /* 28 */  "unknown exception",
61
  /* 29 */  "unknown exception",
62
  /* 30 */  "unknown exception",
63
  /* 31 */  "unknown exception"
64
};
65
 
66
 
67
int defaultISR(int irq) {
68
  printf("\n%s\n", exceptionCause[irq]);
69
  return 0;  /* do not skip any instruction */
70
}
71
 
72
 
73
void initInterrupts(void) {
74
  int i;
75
 
76
  for (i = 0; i < 32; i++) {
77
    setISR(i, defaultISR);
78
  }
79
}
80
 
81
 
82
/**************************************************************/
83
 
84
 
85
Bool checkDiskReady(void) {
86
  int tries;
87
  int i;
88
 
89
  for (tries = 0; tries < 10; tries++) {
90
    for (i = 0; i < 500000; i++) {
91
      if ((*DISK_CTRL & DISK_CTRL_READY) != 0) {
92
        return TRUE;
93
      }
94
    }
95
    printf(".");
96
  }
97
  return FALSE;
98
}
99
 
100
 
101
unsigned long getDiskSize(void) {
102
  return *DISK_CAP;
103
}
104
 
105
 
106
Bool readDisk(unsigned long sector,
107
              unsigned int count,
108
              unsigned int *addr) {
109
  unsigned int n;
110
  unsigned int *p;
111
  unsigned int i;
112
 
113
  while (count != 0) {
114
    n = count > 8 ? 8 : count;
115
    *DISK_SCT = sector;
116
    *DISK_CNT = n;
117
    *DISK_CTRL = DISK_CTRL_STRT;
118
    while ((*DISK_CTRL & DISK_CTRL_DONE) == 0) ;
119
    if (*DISK_CTRL & DISK_CTRL_ERR) {
120
      return FALSE;
121
    }
122
    p = DISK_BUFFER;
123
    for (i = 0; i < n * SECTOR_SIZE / sizeof(unsigned int); i++) {
124
      *addr++ = *p++;
125
    }
126
    sector += n;
127
    count -= n;
128
  }
129
  return TRUE;
130
}
131
 
132
 
133
/**************************************************************/
134
 
135
 
136
void main(void) {
137
  unsigned long numSectors;
138
  unsigned int buffer[SECTOR_SIZE / sizeof(unsigned int)];
139
  int i, j;
140
  unsigned char *p, *q;
141
  unsigned char c;
142
 
143
  /* init interrupts */
144
  initInterrupts();
145
  /* check disk ready */
146
  if (!checkDiskReady()) {
147
    error("disk not ready");
148
  }
149
  /* determine disk size */
150
  numSectors = getDiskSize();
151
  printf("Disk has %lu (0x%lX) sectors.\n",
152
         numSectors, numSectors);
153
  if (numSectors < 32) {
154
    error("disk is too small");
155
  }
156
  /* read master boot record */
157
  if (!readDisk(0, 1, buffer)) {
158
    error("cannot read master boot record from disk");
159
  }
160
  /* dump master boot record */
161
  p = (unsigned char *) buffer;
162
  for (i = 0; i < 32; i++) {
163
    printf("%08X:  ", i * 16);
164
    q = p;
165
    for (j = 0; j < 16; j++) {
166
      c = *q++;
167
      printf("%02X ", c);
168
    }
169
    printf("  ");
170
    q = p;
171
    for (j = 0; j < 16; j++) {
172
      c = *q++;
173
      if (c >= 32 && c <= 126) {
174
        printf("%c", c);
175
      } else {
176
        printf(".");
177
      }
178
    }
179
    printf("\n");
180
    p = q;
181
  }
182
  /* done */
183
  printf("Halting...\n");
184
}

powered by: WebSVN 2.1.0

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