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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [simtest/] [disk/] [mkdisk/] [mkdisk.c] - Blame information for rev 244

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

Line No. Rev Author Line
1 9 hellwig
/*
2
 * mkdisk.c -- make an empty physical disk
3
 */
4
 
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdarg.h>
10
 
11
 
12
#define SECTOR_SIZE             512
13
#define MIN_NUMBER_SECTORS      100
14
#define SECTORS_PER_MB          ((1 << 20) / SECTOR_SIZE)
15
#define DATA_BYTE               0xE5
16
 
17
 
18
void error(char *fmt, ...) {
19
  va_list ap;
20
 
21
  va_start(ap, fmt);
22
  fprintf(stderr, "Error: ");
23
  vfprintf(stderr, fmt, ap);
24
  fprintf(stderr, "\n");
25
  va_end(ap);
26
  exit(1);
27
}
28
 
29
 
30
void usage(void) {
31
  fprintf(stderr, "Usage: mkdisk <file name> <n>[M]\n");
32
  fprintf(stderr, "       <n>: decimal number of sectors\n");
33
  fprintf(stderr, "       if 'M' appended: megabytes instead of sectors\n");
34
  fprintf(stderr, "       (sector size is always %d bytes)\n", SECTOR_SIZE);
35
  exit(1);
36
}
37
 
38
 
39
int main(int argc, char *argv[]) {
40
  FILE *dskFile;
41
  int numSectors;
42
  unsigned char sectorBuffer[SECTOR_SIZE];
43
  int i;
44
 
45
  if (argc != 3) {
46
    usage();
47
  }
48
  numSectors = atoi(argv[2]);
49
  i = strlen(argv[2]) - 1;
50
  if (argv[2][i] == 'M') {
51
    numSectors *= SECTORS_PER_MB;
52
  }
53
  if (numSectors < MIN_NUMBER_SECTORS) {
54
    error("this disk is too small to be useful (minimum size is %d sectors)",
55
          MIN_NUMBER_SECTORS);
56
  }
57
  dskFile = fopen(argv[1], "wb");
58
  if (dskFile == NULL) {
59
    error("cannot open file '%s' for write", argv[1]);
60
  }
61
  fprintf(stdout,
62
          "Creating disk '%s' with %d sectors (around %d MB)...\n",
63
          argv[1], numSectors,
64
          (numSectors + SECTORS_PER_MB / 2) / SECTORS_PER_MB);
65
  for (i = 0; i < SECTOR_SIZE; i++) {
66
    sectorBuffer[i] = DATA_BYTE;
67
  }
68
  for (i = 0; i < numSectors; i++) {
69
    if (fwrite(sectorBuffer, SECTOR_SIZE, 1, dskFile) != 1) {
70
      error("write error on file '%s', sector %d", argv[1], i);
71
    }
72
  }
73
  fclose(dskFile);
74
  return 0;
75
}

powered by: WebSVN 2.1.0

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