File: hardfile.c

package info (click to toggle)
uae 0.8.29-7
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze, wheezy
  • size: 5,396 kB
  • ctags: 10,637
  • sloc: ansic: 74,950; sh: 3,302; asm: 1,064; cpp: 710; objc: 549; makefile: 265; perl: 216
file content (361 lines) | stat: -rw-r--r-- 9,098 bytes parent folder | download
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
 /*
  * UAE - The Un*x Amiga Emulator
  *
  * Hardfile emulation
  *
  * Copyright 1995 Bernd Schmidt
  */

#include "sysconfig.h"
#include "sysdeps.h"

#include "options.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "disk.h"
#include "traps.h"
#include "autoconf.h"
#include "execlib.h"
#include "filesys.h"

#define CMD_INVALID	0
#define CMD_RESET	1
#define CMD_READ	2
#define CMD_WRITE	3
#define CMD_UPDATE	4
#define CMD_CLEAR	5
#define CMD_STOP	6
#define CMD_START	7
#define CMD_FLUSH	8
#define CMD_MOTOR	9
#define CMD_SEEK	10
#define CMD_FORMAT	11
#define CMD_REMOVE	12
#define CMD_CHANGENUM	13
#define CMD_CHANGESTATE	14
#define CMD_PROTSTATUS	15
#define CMD_GETDRIVETYPE 18
#define CMD_GETNUMTRACKS 19
#define CMD_ADDCHANGEINT 20
#define CMD_REMCHANGEINT 21
#define CMD_GETGEOMETRY	22

/* Trackdisk64 support */
#define TD_READ64 24
#define TD_WRITE64 25
#define TD_SEEK64 26
#define TD_FORMAT64 27

/* New Style Devices (NSD) support */
#define DRIVE_NEWSTYLE 0x4E535459L   /* 'NSTY' */
#define NSCMD_DEVICEQUERY 0x4000
#define NSCMD_TD_READ64 0xc000
#define NSCMD_TD_WRITE64 0xc001
#define NSCMD_TD_SEEK64 0xc002
#define NSCMD_TD_FORMAT64 0xc003

static int opencount = 0;

static uae_u32 nscmd_cmd;

int hdf_read (struct hardfiledata *hfd, void *buffer, uae_u64 offset, int len)
{
    int result = 0;

    if (fseek (hfd->fd, offset, SEEK_SET) != 0)
	return 0;
    do {
	int t = fread (buffer, 1, len, hfd->fd);
	result += t;
	len -= t;
	if (t == 0)
	    return result;
    } while (len > 0);
    return result;
}

int hdf_write (struct hardfiledata *hfd, void *buffer, uae_u64 offset, int len)
{
    int result = 0;

    if (fseek (hfd->fd, offset, SEEK_SET) != 0)
	return 0;
    do {
	int t = fwrite (buffer, 1, len, hfd->fd);
	result += t;
	len -= t;
	if (t == 0)
	    return result;
    } while (len > 0);
    return result;
}

static uae_u64 cmd_readx (struct hardfiledata *hfd, uae_u8 *dataptr, uae_u64 offset, uae_u64 len)
{
    return hdf_read (hfd, dataptr, offset, len);
}
static uae_u64 cmd_read (struct hardfiledata *hfd, uaecptr dataptr, uae_u64 offset, uae_u64 len)
{
    addrbank *bank_data = &get_mem_bank (dataptr);
    if (!bank_data || !bank_data->check (dataptr, len))
	return 0;
    return cmd_readx (hfd, bank_data->xlateaddr (dataptr), offset, len);
}
static uae_u64 cmd_writex (struct hardfiledata *hfd, uae_u8 *dataptr, uae_u64 offset, uae_u64 len)
{
    return hdf_write (hfd, dataptr, offset, len);
}

static uae_u64 cmd_write (struct hardfiledata *hfd, uaecptr dataptr, uae_u64 offset, uae_u64 len)
{
    addrbank *bank_data = &get_mem_bank (dataptr);
    if (!bank_data || !bank_data->check (dataptr, len))
	return 0;
    return cmd_writex (hfd, bank_data->xlateaddr (dataptr), offset, len);
}

static uae_u32 hardfile_open (TrapContext *dummy)
{
    uaecptr tmp1 = m68k_areg (regs, 1); /* IOReq */

    /* Check unit number */
    if (get_hardfile_data (m68k_dreg (regs, 0))) {
	opencount++;
	put_word (m68k_areg (regs, 6)+32, get_word (m68k_areg (regs, 6)+32) + 1);
	put_long (tmp1 + 24, m68k_dreg (regs, 0)); /* io_Unit */
	put_byte (tmp1 + 31, 0); /* io_Error */
	put_byte (tmp1 + 8, 7); /* ln_type = NT_REPLYMSG */
	return 0;
    }

    put_long (tmp1 + 20, (uae_u32)-1);
    put_byte (tmp1 + 31, (uae_u8)-1);
    return (uae_u32)-1;
}

static uae_u32 hardfile_close (TrapContext *dummy)
{
    opencount--;
    put_word (m68k_areg (regs, 6) + 32, get_word (m68k_areg (regs, 6) + 32) - 1);

    return 0;
}

static uae_u32 hardfile_expunge (TrapContext *dummy)
{
    return 0; /* Simply ignore this one... */
}

static uae_u32 hardfile_beginio (TrapContext *dummy)
{
    uae_u32 request, len, dataptr, offset, actual = 0, cmd;
    uae_u32 retval = m68k_dreg (regs, 0);
    int unit;
    struct hardfiledata *hfd;

    request = m68k_areg (regs, 1);
    unit = get_long (request + 24);
#undef DEBUGME
#ifdef DEBUGME
    printf ("hardfile: unit = %d\n", unit);
    printf ("hardfile: request = %08lx\n", (unsigned long)request);
    printf ("hardfile: cmd  = %d\n", (int)get_word (request + 28));
#endif
    hfd = get_hardfile_data (unit);

    put_byte (request + 8, NT_MESSAGE);
    put_byte (request + 31, 0); /* no error yet */
    cmd = get_word (request + 28); /* io_Command */
/*    put_byte (request + 30, get_byte (request + 30) & ~1);*/
    dataptr = get_long (request + 40);

    switch (cmd) {
     case CMD_READ:
	if (dataptr & 1)
	    goto bad_command;
	offset = get_long (request + 44);
	if (offset & 511)
	    goto bad_command;
	len = get_long (request + 36); /* io_Length */
	if (len & 511)
	    goto bad_command;
	if (len + offset > (uae_u32)hfd->size)
	    goto bad_command;

	actual = (uae_u32)cmd_read (hfd, dataptr, offset, len);
	put_long (request + 32, actual);
	break;

     case CMD_WRITE:
     case CMD_FORMAT:
	if (dataptr & 1)
	    goto bad_command;
	offset = get_long (request + 44);
	if (offset & 511)
	    goto bad_command;
	len = get_long (request + 36); /* io_Length */
	if (len & 511)
	    goto bad_command;
	if (len + offset > (uae_u32)hfd->size)
	    goto bad_command;

	actual = (uae_u32)cmd_write (hfd, dataptr, offset, len);
	put_long (request + 32, actual); /* set io_Actual */
	break;

	bad_command:
	break;

     case NSCMD_DEVICEQUERY:
	put_long (dataptr + 4, 16); /* size */
	put_word (dataptr + 8, 5); /* NSDEVTYPE_TRACKDISK */
	put_word (dataptr + 10, 0);
	put_long (dataptr + 12, nscmd_cmd);
	put_long (request + 32, 16);
	break;

     case CMD_GETDRIVETYPE:
	printf ("Shouldn't happen\n");
	put_long (request + 32, 1); /* not exactly a 3.5" drive, but... */
	break;

     case CMD_GETNUMTRACKS:
	printf ("Shouldn't happen 2\n");
	put_long (request + 32, 0);
	break;

	/* Some commands that just do nothing and return zero */
     case CMD_UPDATE:
     case CMD_CLEAR:
     case 9: /* Motor */
     case 10: /* Seek */
     case 12: /* Remove */
     case 13: /* ChangeNum */
     case 14: /* ChangeStatus */
     case 15: /* ProtStatus */
     case 20: /* AddChangeInt */
     case 21: /* RemChangeInt */
	put_long (request + 32, 0); /* io_Actual */
	retval = 0;
	break;

     default:
	/* Command not understood. */
	put_byte (request + 31, (uae_u8)-3); /* io_Error */
	retval = 0;
	break;
    }
#if 0
    if ((get_byte (request + 30) & 1) == 0) {
	/* Not IOF_QUICK -- need to ReplyMsg */
	m68k_areg (regs, 1) = request;
	CallLib (get_long (4), -378);
    }
#endif
    return retval;
}

static uae_u32 hardfile_abortio (TrapContext *dummy)
{
    return (uae_u32)-3;
}

void hardfile_install (void)
{
    uae_u32 functable, datatable;
    uae_u32 initcode, openfunc, closefunc, expungefunc;
    uae_u32 beginiofunc, abortiofunc;

    ROM_hardfile_resname = ds ("uaehf.device");
    ROM_hardfile_resid = ds ("UAE hardfile.device 0.2");

    nscmd_cmd = here ();
    dw (CMD_READ);
    dw (CMD_WRITE);
    dw (CMD_FORMAT);
    dw (NSCMD_DEVICEQUERY);
    dw (CMD_GETDRIVETYPE);
    dw (CMD_CLEAR);
    dw (CMD_UPDATE);
    dw (CMD_MOTOR);
    dw (CMD_SEEK);
    dw (CMD_CHANGENUM);
    dw (CMD_CHANGESTATE);
    dw (CMD_PROTSTATUS);
    dw (CMD_ADDCHANGEINT);
    dw (CMD_REMCHANGEINT);
    dw (0);

    /* initcode */
#if 0
    initcode = here ();
    calltrap (deftrap (hardfile_init)); dw (RTS);
#else
    initcode = filesys_initcode;
#endif
    /* Open */
    openfunc = here ();
    calltrap (deftrap (hardfile_open)); dw (RTS);

    /* Close */
    closefunc = here ();
    calltrap (deftrap (hardfile_close)); dw (RTS);

    /* Expunge */
    expungefunc = here ();
    calltrap (deftrap (hardfile_expunge)); dw (RTS);

    /* BeginIO */
    beginiofunc = here ();
    calltrap (deftrap (hardfile_beginio));
    dw (0x48E7); dw (0x8002); /* movem.l d0/a6,-(a7) */
    dw (0x0829); dw (0); dw (30); /* btst #0,30(a1) */
    dw (0x6608); /* bne.b +8 */
    dw (0x2C78); dw (0x0004); /* move.l 4,a6 */
    dw (0x4EAE); dw (-378); /* jsr ReplyMsg(a6) */
    dw (0x4CDF); dw (0x4001); /* movem.l (a7)+,d0/a6 */
    dw (RTS);

    /* AbortIO */
    abortiofunc = here ();
    calltrap (deftrap (hardfile_abortio)); dw (RTS);

    /* FuncTable */
    functable = here ();
    dl (openfunc); /* Open */
    dl (closefunc); /* Close */
    dl (expungefunc); /* Expunge */
    dl (EXPANSION_nullfunc); /* Null */
    dl (beginiofunc); /* BeginIO */
    dl (abortiofunc); /* AbortIO */
    dl (0xFFFFFFFFul); /* end of table */

    /* DataTable */
    datatable = here ();
    dw (0xE000); /* INITBYTE */
    dw (0x0008); /* LN_TYPE */
    dw (0x0300); /* NT_DEVICE */
    dw (0xC000); /* INITLONG */
    dw (0x000A); /* LN_NAME */
    dl (ROM_hardfile_resname);
    dw (0xE000); /* INITBYTE */
    dw (0x000E); /* LIB_FLAGS */
    dw (0x0600); /* LIBF_SUMUSED | LIBF_CHANGED */
    dw (0xD000); /* INITWORD */
    dw (0x0014); /* LIB_VERSION */
    dw (0x0004); /* 0.4 */
    dw (0xD000);
    dw (0x0016); /* LIB_REVISION */
    dw (0x0000);
    dw (0xC000);
    dw (0x0018); /* LIB_IDSTRING */
    dl (ROM_hardfile_resid);
    dw (0x0000); /* end of table */

    ROM_hardfile_init = here ();
    dl (0x00000100); /* ??? */
    dl (functable);
    dl (datatable);
    dl (initcode);
}