File: disk.c

package info (click to toggle)
gxemul 0.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,152 kB
  • sloc: ansic: 111,065; sh: 972; exp: 354; makefile: 118
file content (113 lines) | stat: -rw-r--r-- 2,231 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 *  GXemul demo:  Disk image access
 *
 *  This file is in the Public Domain.
 */

#include "dev_cons.h"
#include "dev_disk.h"


#ifdef MIPS
/*  Note: The ugly cast to a signed int (32-bit) causes the address to be
	sign-extended correctly on MIPS when compiled in 64-bit mode  */ 
#define PHYSADDR_OFFSET         ((signed int)0xa0000000)
#else
#define PHYSADDR_OFFSET         0
#endif


#define PUTCHAR_ADDRESS		(PHYSADDR_OFFSET +              \
				DEV_CONS_ADDRESS + DEV_CONS_PUTGETCHAR)
#define HALT_ADDRESS            (PHYSADDR_OFFSET +              \
				DEV_CONS_ADDRESS + DEV_CONS_HALT)
#define DISK_ADDRESS            (PHYSADDR_OFFSET + DEV_DISK_ADDRESS)


void printchar(char ch)
{
	*((volatile unsigned char *) PUTCHAR_ADDRESS) = ch;
}


void printstr(char *s)
{
	while (*s)
		printchar(*s++);
}


void halt(void)
{
	*((volatile unsigned char *) HALT_ADDRESS) = 0;
}


void printhex2(int i)
{
	printchar("0123456789abcdef"[i >> 4]);
	printchar("0123456789abcdef"[i & 15]);
}


void printhex4(int i)
{
	printhex2(i >> 8);
	printhex2(i & 255);
}


void f(void)
{
	int ofs, ide_id = 0, status, i;
	unsigned char ch;

	printstr("Testing dev_disk.\n");
	printstr("Assuming that IDE ID 0 (primary master) is available.\n");

	for (ofs = 0; ofs < 1024; ofs += 512) {
		printstr("\n");

		*((volatile int *) (DISK_ADDRESS + DEV_DISK_OFFSET)) = ofs;
		*((volatile int *) (DISK_ADDRESS + DEV_DISK_ID)) = ide_id;

		*((volatile int *) (DISK_ADDRESS + DEV_DISK_START_OPERATION)) =
		    DEV_DISK_OPERATION_READ;

		/*  Get status:  */
		status = *((volatile int *) (DISK_ADDRESS + DEV_DISK_STATUS));

		if (status == 0) {
			printstr("Read failed.\n");
			halt();
		}

		printstr("Sector dump:\n");
		for (i = 0; i < 512; i++) {
			if ((i % 16) == 0) {
				printhex4(i);
				printstr(" ");
			}
			printstr(" ");
			ch = *((volatile unsigned char *) DISK_ADDRESS
			    + DEV_DISK_BUFFER + i);
			printhex2(ch);
			if ((i % 16) == 15) {
				int j;
				printstr("  ");
				for (j = i-15; j <= i; j++) {
					ch = *((volatile unsigned char *)
					    DISK_ADDRESS + DEV_DISK_BUFFER + j);
					if (ch < 32 || ch >= 127)
						ch = '.';
					printchar(ch);
				}
				printstr("\n");
			}
		}
	}

	printstr("\nDone.\n");
	halt();
}