62ddfa8be021e135d07bb0f88db79c0ead17f63b
1 /* $Id: s100_fif.c 1773 2008-01-11 05:46:19Z hharte $
3 IMSAI FIF Disk Controller by Ernie Price
5 Based on altairz80_dsk.c, Copyright (c) 2002-2008, Peter Schorn
7 Plug-n-Play added by Howard M. Harte
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 PETER SCHORN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 Except as contained in this notice, the name of Peter Schorn shall not
27 be used in advertising or otherwise to promote the sale, use or other dealings
28 in this Software without prior written authorization from Peter Schorn.
32 #include "altairz80_defs.h"
34 #define UNIT_V_DSK_WLK (UNIT_V_UF + 0) /* write locked */
35 #define UNIT_DSK_WLK (1 << UNIT_V_DSK_WLK)
36 #define UNIT_V_DSK_VERBOSE (UNIT_V_UF + 1) /* verbose mode, i.e. show error messages */
37 #define UNIT_DSK_VERBOSE (1 << UNIT_V_DSK_VERBOSE)
38 #define DSK_SECTSIZE 137 /* size of sector */
39 #define DSK_SECT 32 /* sectors per track */
40 #define MAX_TRACKS 254 /* number of tracks, original Altair has 77 tracks only */
41 #define DSK_TRACSIZE (DSK_SECTSIZE * DSK_SECT)
42 #define MAX_DSK_SIZE (DSK_TRACSIZE * MAX_TRACKS)
44 static t_stat
fif_reset(DEVICE
*dptr
);
45 static t_stat
fif_set_verbose(UNIT
*uptr
, int32 value
, char *cptr
, void *desc
);
46 static int32
fif_io(const int32 port
, const int32 io
, const int32 data
);
48 extern t_stat
set_iobase(UNIT
*uptr
, int32 val
, char *cptr
, void *desc
);
49 extern t_stat
show_iobase(FILE *st
, UNIT
*uptr
, int32 val
, void *desc
);
50 extern uint32
sim_map_resource(uint32 baseaddr
, uint32 size
, uint32 resource_type
,
51 int32 (*routine
)(const int32
, const int32
, const int32
), uint8 unmap
);
52 extern uint8
GetBYTEWrapper(const uint32 Addr
);
53 extern void PutBYTEWrapper(const uint32 Addr
, const uint32 Value
);
56 extern char messageBuffer
[];
57 extern void printMessage(void);
59 /* global data on status */
61 /* currently selected drive (values are 0 .. NUM_OF_DSK)
62 current_disk < NUM_OF_DSK implies that the corresponding disk is attached to a file */
63 static int32 current_disk
= NUM_OF_DSK
;
64 static int32 warnLevelDSK
= 3;
65 static int32 warnLock
[NUM_OF_DSK
] = {0, 0, 0, 0, 0, 0, 0, 0};
66 static int32 warnAttached
[NUM_OF_DSK
] = {0, 0, 0, 0, 0, 0, 0, 0};
67 static int32 warnDSK11
= 0;
69 /* 88DSK Standard I/O Data Structures */
72 PNP_INFO pnp
; /* Plug and Play */
75 FIF_INFO fif_info_data
= { { 0x0000, 0, 0xFD, 1 } };
76 FIF_INFO
*fif_info
= &fif_info_data
;
78 static UNIT fif_unit
[] = {
79 { UDATA (NULL
, UNIT_FIX
+ UNIT_ATTABLE
+ UNIT_DISABLE
+ UNIT_ROABLE
, MAX_DSK_SIZE
) },
80 { UDATA (NULL
, UNIT_FIX
+ UNIT_ATTABLE
+ UNIT_DISABLE
+ UNIT_ROABLE
, MAX_DSK_SIZE
) },
81 { UDATA (NULL
, UNIT_FIX
+ UNIT_ATTABLE
+ UNIT_DISABLE
+ UNIT_ROABLE
, MAX_DSK_SIZE
) },
82 { UDATA (NULL
, UNIT_FIX
+ UNIT_ATTABLE
+ UNIT_DISABLE
+ UNIT_ROABLE
, MAX_DSK_SIZE
) },
83 { UDATA (NULL
, UNIT_FIX
+ UNIT_ATTABLE
+ UNIT_DISABLE
+ UNIT_ROABLE
, MAX_DSK_SIZE
) },
84 { UDATA (NULL
, UNIT_FIX
+ UNIT_ATTABLE
+ UNIT_DISABLE
+ UNIT_ROABLE
, MAX_DSK_SIZE
) },
85 { UDATA (NULL
, UNIT_FIX
+ UNIT_ATTABLE
+ UNIT_DISABLE
+ UNIT_ROABLE
, MAX_DSK_SIZE
) },
86 { UDATA (NULL
, UNIT_FIX
+ UNIT_ATTABLE
+ UNIT_DISABLE
+ UNIT_ROABLE
, MAX_DSK_SIZE
) }
89 static REG fif_reg
[] = {
90 { DRDATA (DISK
, current_disk
, 4) },
91 { DRDATA (DSKWL
, warnLevelDSK
, 32) },
92 { BRDATA (WARNLOCK
, warnLock
, 10, 32, NUM_OF_DSK
), REG_CIRC
+ REG_RO
},
93 { BRDATA (WARNATTACHED
, warnAttached
, 10, 32, NUM_OF_DSK
), REG_CIRC
+ REG_RO
},
94 { DRDATA (WARNDSK11
, warnDSK11
, 4), REG_RO
},
98 static MTAB fif_mod
[] = {
99 { MTAB_XTD
|MTAB_VDV
, 0, "IOBASE", "IOBASE", &set_iobase
, &show_iobase
, NULL
},
100 { UNIT_DSK_WLK
, 0, "WRTENB", "WRTENB", NULL
},
101 { UNIT_DSK_WLK
, UNIT_DSK_WLK
, "WRTLCK", "WRTLCK", NULL
},
102 /* quiet, no warning messages */
103 { UNIT_DSK_VERBOSE
, 0, "QUIET", "QUIET", NULL
},
104 /* verbose, show warning messages */
105 { UNIT_DSK_VERBOSE
, UNIT_DSK_VERBOSE
, "VERBOSE", "VERBOSE", &fif_set_verbose
},
110 "FIF", fif_unit
, fif_reg
, fif_mod
,
112 NULL
, NULL
, &fif_reset
,
114 &fif_info_data
, (DEV_DISABLE
| DEV_DIS
), 0,
118 static void resetDSKWarningFlags(void) {
120 for (i
= 0; i
< NUM_OF_DSK
; i
++) {
127 static t_stat
fif_set_verbose(UNIT
*uptr
, int32 value
, char *cptr
, void *desc
) {
128 resetDSKWarningFlags();
132 /* returns TRUE iff there exists a disk with VERBOSE */
133 static int32
hasVerbose(void) {
135 for (i
= 0; i
< NUM_OF_DSK
; i
++) {
136 if (((fif_dev
.units
+ i
) -> flags
) & UNIT_DSK_VERBOSE
) {
143 /* service routines to handle simulator functions */
146 static t_stat
fif_reset(DEVICE
*dptr
)
148 PNP_INFO
*pnp
= (PNP_INFO
*)dptr
->ctxt
;
150 resetDSKWarningFlags();
151 current_disk
= NUM_OF_DSK
;
153 if(dptr
->flags
& DEV_DIS
) {
154 sim_map_resource(pnp
->io_base
, pnp
->io_size
, RESOURCE_TYPE_IO
, &fif_io
, TRUE
);
156 /* Connect HDSK at base address */
157 if(sim_map_resource(pnp
->io_base
, pnp
->io_size
, RESOURCE_TYPE_IO
, &fif_io
, FALSE
) != 0) {
158 printf("%s: error mapping I/O resource at 0x%04x\n", __FUNCTION__
, pnp
->mem_base
);
159 dptr
->flags
|= DEV_DIS
;
166 typedef struct desc_t
169 cmd_unit
, /* (cmd << 4) | unit : 1 = A: */
170 result
, /* result: 0 == busy, 1 = normal completion, */
171 nn
, /* number of secs ? */
174 addr_l
, /* low (transfer address) */
175 addr_h
; /* high (transfer address) */
178 static desc_t mydesc
;
180 enum {NONE
, WRITE_SEC
, READ_SEC
, FMT_TRACK
};
186 static uint8 blanksec
[SEC_SZ
];
187 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
188 static const uint8 utrans
[] = {0,1,2,0,3,0,0,0,4,0,0,0,0,0,0,0};
190 /**************************************************
192 Translate an IMSAI FIF disk request into an access into the harddrive file
195 static int DoDiskOperation(desc_t
*dsc
, uint8 val
)
197 int32 current_disk_flags
;
204 printf("%02x %02x %02x %02x %02x %02x %02x %02x \n",
215 current_disk
= (utrans
[dsc
->cmd_unit
& UMASK
]) - 1; /* 0 <= current_disk < NUM_OF_DSK */
216 if (current_disk
>= NUM_OF_DSK
) {
217 if (hasVerbose() && (warnDSK11
< warnLevelDSK
)) {
219 /*03*/ MESSAGE_2("Attempt disk io on illegal disk %d - ignored.", current_disk
+ 1);
221 return 0; /* no drive selected - can do nothing */
223 current_disk_flags
= (fif_dev
.units
+ current_disk
) -> flags
;
224 if ((current_disk_flags
& UNIT_ATT
) == 0) { /* nothing attached? */
225 if ( (current_disk_flags
& UNIT_DSK_VERBOSE
) && (warnAttached
[current_disk
] < warnLevelDSK
) ) {
226 warnAttached
[current_disk
]++;
227 /*02*/MESSAGE_2("Attempt to select unattached DSK%d - ignored.", current_disk
);
229 current_disk
= NUM_OF_DSK
;
233 uptr
= fif_dev
.units
+ current_disk
;
236 /* decode request: */
237 switch (dsc
->cmd_unit
>> 4) {
239 /*printf("%c", dsc->track % 10 ? '*' : '0' + + dsc->track / 10); */
241 memset(blanksec
, 0, SEC_SZ
);
242 addr
= dsc
->track
* SPT
;
243 fseek(cpx
, addr
* SEC_SZ
, SEEK_SET
);
245 /* write a track worth of sectors */
246 for (kt
=0; kt
< SPT
; kt
++) {
247 fwrite(blanksec
, 1, sizeof(blanksec
), cpx
);
252 addr
= (dsc
->track
* SPT
) + dsc
->sector
- 1;
253 fseek(cpx
, addr
* SEC_SZ
, SEEK_SET
);
254 fread(blanksec
, 1, SEC_SZ
, cpx
);
255 addr
= dsc
->addr_l
+ (dsc
->addr_h
<< 8); /* no assumption on endianness */
256 for (kt
= 0; kt
< SEC_SZ
; kt
++) {
257 PutBYTEWrapper(addr
++, blanksec
[kt
]);
262 addr
= (dsc
->track
* SPT
) + dsc
->sector
- 1;
263 fseek(cpx
, addr
* SEC_SZ
, SEEK_SET
);
264 addr
= dsc
->addr_l
+ (dsc
->addr_h
<< 8); /* no assumption on endianness */
265 for (kt
= 0; kt
< SEC_SZ
; kt
++) {
266 blanksec
[kt
] = GetBYTEWrapper(addr
++);
268 fwrite(blanksec
, 1, SEC_SZ
, cpx
);
277 /**********************************************************************
279 Copy the disk descriptor from target RAM
282 static void getdesc(uint16 addr
) {
284 uint8
*p1
= (uint8
*)&mydesc
;
286 for (x
= 0; x
< sizeof(mydesc
); x
++) {
287 *p1
++ = GetBYTEWrapper(addr
++);
291 /**********************************************************************
293 handle the IMSAI FIF floppy controller
296 static int32
fif_io(const int32 port
, const int32 io
, const int32 data
) {
298 static int32 fdstate
= 0; /* chan 0xfd state */
300 static uint16 fdAdr
[16]; /* disk descriptor address in 8080/z80 RAM */
303 /* cmd == 0x00 do operation */
304 /* cmd == 0x10 next 2 transfers are desc address */
305 /* desc# is one of 16 0x0 - 0xf */
314 if ((data
& 0x10) != 0) { /* prefix 0x10 */
315 fdstate
++; /* means desc address is next 2 out (fd),a */
317 else { /* do what descriptor says */
318 getdesc(fdAdr
[desc
]);
319 PutBYTEWrapper(fdAdr
[desc
] + 1,
320 (uint8
)DoDiskOperation(&mydesc
, (uint8
)data
));
325 /*printf("D1 %02x %02x\n", desc, data); */
326 fdAdr
[desc
] = data
; /* LSB of descriptor address */
331 /*printf("D2 %02x %02x\n", desc, data); */
332 fdAdr
[desc
] |= data
<< 8; /* MSB of descriptor address */
342 #define WRK_BUF_SZ 150
348 /**************************************************
350 static void xfero(int32 addr
, char *src
, int32 lth
)
353 PutBYTEWrapper(addr
++, *src
++);
357 /**************************************************
359 static void xferi(int32 addr
, char *dst
, int32 lth
)
362 *dst
++ = GetBYTEWrapper(addr
++);
366 #if !defined (_WIN32)
367 static void strupr(char *fn
) { /* psco added */
369 if (('a' <= *fn
) && (*fn
<= 'z')) *fn
-= 'a' - 'A';
375 /**************************************************
377 static void initfcb(char *fcb
, char *fn
, int32 flg
)
385 memset (fcb
, 0 , FCB_SIZE
);
386 memset (fcb
+ 1, ' ', NAME_LTH
+ EXT_LTH
);
388 while (*fn
&& (*fn
!= '.'))
396 p1
= fcb
+ NAME_LTH
+ 1;
397 while (*fn
&& (*fn
!= '.'))
403 /**************************************************
405 FTP interface - most of the work is done here
406 The IMDOS/CPM application only does minimal work
410 char message
[WRK_BUF_SZ
];
411 char temp
[WRK_BUF_SZ
];
414 uint8
FTP(int32 BC
, int32 DE
)
419 xferi(DE
, temp
, SEC_SZ
);
424 memcpy(message
, p1
+ 2, *(p1
+ 1));
425 *(message
+ *(p1
+ 1)) = 0;
426 p2
= strtok(message
, " \t");
427 if (!strcmp(p2
, "get"))
429 p2
= strtok(NULL
, " \t");
430 if (myfile
= fopen(p2
, "rb"))
432 initfcb(temp
, p2
, 1);
433 xfero(DE
+ 2, temp
, 32);
438 if (!strcmp(p2
, "era"))
440 p2
= strtok(NULL
, " \t");
441 initfcb(temp
, p2
, 0);
442 xfero(DE
+ 2, temp
, 32);
450 memset(temp
, 0x1a, SEC_SZ
);
451 retval
= fread(temp
, 1, SEC_SZ
, myfile
) ? 0 : 1;
452 xfero( DE
, temp
, SEC_SZ
);
462 #endif /* ERNIES_FTP */
464 /* end of the source */