76ba9df5 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <unistd.h> |
4 | #include <sys/io.h> |
5 | #include <signal.h> |
6 | |
7 | /* |
8 | * PC: |
9 | * BASE+0 ~ data |
10 | * BASE+1 ~ status: BAOSEI?? |
11 | * /BUSY, ACK, PE, SLCT, ERROR, IRQ |
12 | * BASE+2 ~ control: ??MISIAS |
13 | * bidirrectMODE, IRQ_EN, /SLCT_IN, INIT, /AUTO_FD_XT, /STROBE |
14 | * |
15 | * SEGA |
16 | * ?HRL3210 |
17 | * TH, TR, TL, D3, D2, D1, D0 |
18 | * |
19 | * SEGA PC |
20 | * 1 D0 <-> 2 D0 |
21 | * 2 D1 <-> 3 D1 |
22 | * 3 D2 <-> 4 D2 |
23 | * 4 D3 <-> 5 D3 |
24 | * 5 +5V |
25 | * 6 TL <-- 14 /AUTO_FD_XT |
26 | * 7 TH --> 10 ACK |
27 | * 8 GND --- 21 GND |
28 | * 9 TR <-- 17 /SLCT_IN |
29 | */ |
30 | |
31 | static void inthandler(int u) |
32 | { |
33 | /* switch TL back to high */ |
34 | outb(0xe0, 890); |
35 | printf("\n"); |
36 | exit(1); |
37 | } |
38 | |
39 | int main(int argc, char *argv[]) |
40 | { |
41 | int size, byte, ret, i; |
42 | unsigned char *data; |
43 | char *p = NULL; |
44 | FILE *file; |
45 | |
46 | if (argc != 4 || argv[1][0] != '-' || (argv[1][1] != 'r' && argv[1][1] != 'w')) { |
47 | printf("usage:\n%s {-r,-w} <file> <size_hex>\n", argv[0]); |
48 | return 1; |
49 | } |
50 | |
51 | file = fopen(argv[2], argv[1][1] == 'r' ? "wb" : "rb"); |
52 | if (file == NULL) { |
53 | fprintf(stderr, "can't open file: %s\n", argv[2]); |
54 | return 1; |
55 | } |
56 | |
57 | size = (int)strtoul(argv[3], &p, 16); |
58 | if (p == NULL || *p != 0) { |
59 | fprintf(stderr, "can't convert size %s\n", argv[3]); |
60 | return 1; |
61 | } |
62 | |
63 | data = malloc(size); |
64 | if (data == NULL) { |
65 | fprintf(stderr, "can't alloc %d bytes\n", size); |
66 | return 1; |
67 | } |
68 | |
69 | ret = ioperm(888, 3, 1); |
70 | if (ret != 0) { |
71 | perror("ioperm"); |
72 | return 1; |
73 | } |
74 | |
75 | signal(SIGINT, inthandler); |
76 | |
77 | printf("regs: %02x %02x %02x\n", inb(888), inb(889), inb(890)); |
78 | outb(0xe0, 890); |
79 | |
80 | while (!(inb(889) & 0x40)) { |
81 | printf("waiting for TH..\n"); |
82 | sleep(5); |
83 | } |
84 | |
85 | if (argv[1][1] == 'r') |
86 | { |
87 | for (i = 0; i < size; i++) |
88 | { |
89 | if ((i & 0xff) == 0) { |
90 | printf("\b\b\b\b\b\b\b\b\b\b\b\b\b"); |
91 | printf("%06x/%06x", i, size); |
92 | fflush(stdout); |
93 | } |
94 | |
95 | outb(0xe2, 890); /* TL low */ |
96 | |
97 | /* wait for TH low */ |
98 | while (inb(889) & 0x40) ; |
99 | |
100 | byte = inb(888) & 0x0f; |
101 | |
102 | outb(0xe0, 890); /* TL high */ |
103 | |
104 | /* wait for TH high */ |
105 | while (!(inb(889) & 0x40)) ; |
106 | |
107 | byte |= inb(888) << 4; |
108 | data[i] = byte; |
109 | } |
110 | |
111 | fwrite(data, 1, size, file); |
112 | } |
113 | else |
114 | { |
115 | ret = fread(data, 1, size, file); |
116 | if (ret < size) |
117 | printf("warning: read only %d/%d\n", ret, size); |
118 | |
119 | outb(0xc0, 890); /* out mode, TL hi */ |
120 | outb(data[0] & 0x0f, 888); |
121 | outb(0xc2, 890); /* out mode, TL low (start condition) */ |
122 | |
123 | for (i = 0; i < size; i++) |
124 | { |
125 | if ((i & 0xff) == 0) { |
126 | printf("\b\b\b\b\b\b\b\b\b\b\b\b\b"); |
127 | printf("%06x/%06x", i, size); |
128 | fflush(stdout); |
129 | } |
130 | |
131 | /* wait for TH low */ |
132 | while (inb(889) & 0x40) ; |
133 | |
134 | byte = data[i]; |
135 | |
136 | outb(byte & 0x0f, 888); |
137 | outb(0xc2, 890); /* TL low */ |
138 | |
139 | /* wait for TH high */ |
140 | while (!(inb(889) & 0x40)) ; |
141 | |
142 | outb(byte >> 4, 888); |
143 | outb(0xc0, 890); /* TL high */ |
144 | } |
145 | } |
146 | printf("\b\b\b\b\b\b\b\b\b\b\b\b\b"); |
147 | printf("%06x/%06x\n", i, size); |
148 | fclose(file); |
149 | |
150 | return 0; |
151 | } |
152 | |