forked from retrofw/dingux-msx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatch.c
431 lines (367 loc) · 14 KB
/
Patch.c
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/** fMSX: portable MSX emulator ******************************/
/** **/
/** Patch.c **/
/** **/
/** This file contains implementation for the PatchZ80() **/
/** function necessary for emulating MSX disk and tape IO. **/
/** **/
/** Copyright (C) Marat Fayzullin 1994-2003 **/
/** You are not allowed to distribute this software **/
/** commercially. Please, notify me, if you make any **/
/** changes to this file. **/
/*************************************************************/
#include "MSX.h"
#include "Boot.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#ifndef SEEK_CUR
#define SEEK_CUR 1
#endif
void SSlot(byte Value); /* Used to switch secondary slots */
#ifdef FMSX
extern byte *RAM[],PSL[],SSLReg;
static byte loc_RdZ80(word A)
{
if(A!=0xFFFF) return(RAM[A>>13][A&0x1FFF]);
else return((PSL[3]==3)? ~SSLReg:RAM[7][0x1FFF]);
}
#endif
/** PatchZ80() ***********************************************/
/** Emulate BIOS calls. This function is called on an ED FE **/
/** instruction to emulate disk/tape access, etc. **/
/*************************************************************/
extern Z80 CPU;
void PatchZ80()
{
static const byte TapeHeader[8] = { 0x1F,0xA6,0xDE,0xBA,0xCC,0x13,0x7D,0x74 };
static const struct
{ int Sectors;byte Heads,Names,PerTrack,PerFAT,PerCluster; }
Info[8] =
{
{ 720,1,112,9,2,2 },
{ 1440,2,112,9,3,2 },
{ 640,1,112,8,1,2 },
{ 1280,2,112,8,2,2 },
{ 360,1, 64,9,2,1 },
{ 720,2,112,9,2,2 },
{ 320,1, 64,8,1,1 },
{ 640,2,112,8,1,2 }
};
byte Buf[512],Count,PS,SS,N,*P;
int J,I,Sector;
word Addr;
switch(CPU.PC.W-2)
{
case 0x4010:
/** PHYDIO: Read/write sectors to disk **************************
*** Input: ***
*** [F] CARRY=WRITE [A] Drive number (0=A:) ***
*** [B] Number of sectors to write [C] Media descriptor ***
*** [DE] Logical sector number (starts at 0) ***
*** [HL] Transfer address ***
*** Output: ***
*** [F] CARRY=ERROR [A] If error: errorcode ***
*** [B] Number of sectors remaining (not read/written) ***
*** Error codes in [A] can be: ***
*** 0 Write protected 8 Record not found ***
*** 2 Not ready 10 Write fault ***
*** 4 Data (CRC) error 12 Other errors ***
*** 6 Seek error ***
****************************************************************/
{
if(Verbose&0x04)
printf
(
"%s DISK %c: %d sectors starting from %04Xh [buffer at %04Xh]\n",
CPU.AF.B.l&C_FLAG? "WRITE":"READ",CPU.AF.B.h+'A',CPU.BC.B.h,
CPU.DE.W,CPU.HL.W
);
CPU.IFF|=1;
Addr = CPU.HL.W;
Count = CPU.BC.B.h;
if(!DiskPresent(CPU.AF.B.h))
{ CPU.AF.W=0x0201;return; } /* No disk -> "Not ready" */
if((int)(CPU.DE.W)+Count>Info[CPU.BC.B.l-0xF8].Sectors)
{ CPU.AF.W=0x0801;return; } /* Wrong sector -> "Record not found" */
/* If data does not fit into 64kB address space, trim it */
if((int)(CPU.HL.W)+Count*512>0x10000) Count=(0x10000-CPU.HL.W)/512;
/* Save slot states */
PS=PSLReg;SS=SSLReg;
/* Turn on RAM in all slots */
OutZ80(0xA8,0xFF);
SSlot(0xAA);
if(CPU.AF.B.l&C_FLAG)
for(Sector=CPU.DE.W;Count--;Sector++) /* WRITE */
{
for(J=0;J<512;J++) Buf[J]=loc_RdZ80(Addr++);
if(DiskWrite(CPU.AF.B.h,Buf,Sector)) CPU.BC.B.h--;
else
{
CPU.AF.W=0x0A01;
SSlot(SS);
OutZ80(0xA8,PS);
return;
}
}
else
for(Sector=CPU.DE.W;Count--;Sector++) /* READ */
{
if(DiskRead(CPU.AF.B.h,Buf,Sector)) CPU.BC.B.h--;
else
{
CPU.AF.W=0x0401;
SSlot(SS);
OutZ80(0xA8,PS);
return;
}
for(J=0;J<512;J++) WrZ80(Addr++,Buf[J]);
}
/* Restore slot states */
SSlot(SS);
OutZ80(0xA8,PS);
/* Return "Success" */
CPU.AF.B.l&=~C_FLAG;
return;
}
case 0x4013:
/** DSKCHG: Check if the disk was changed ***********************
*** Input: ***
*** [A] Drive number (0=A:) [B] Media descriptor ***
*** [C] Media descriptor [HL] Base address of DPB ***
*** Output: ***
*** [F] CARRY=ERROR [A] If error: errorcode (see DSKIO) ***
*** [B] If success: 1=Unchanged, 0=Unknown, -1=Changed ***
*** Note: ***
*** If the disk has been changed or may have been changed ***
*** (unknown) read the boot sector or the FAT sector for disk ***
*** media descriptor and transfer a new DPB as with GETDPB. ***
****************************************************************/
{
if(Verbose&0x04) printf("CHECK DISK %c\n",CPU.AF.B.h+'A');
CPU.IFF|=1;
/* If no disk, return "Not ready": */
if(!DiskPresent(CPU.AF.B.h)) { CPU.AF.W=0x0201;return; }
/* This requires some major work to be done: */
CPU.BC.B.h=0;CPU.AF.B.l&=~C_FLAG;
/* We continue with GETDPB now... */
}
case 0x4016:
/** GETDPB: Disk format *****************************************
*** Input: ***
*** [A] Drive number [B] 1st byte of FAT (media descriptor) ***
*** [C] Media descriptor [HL] Base address of DPB ***
*** Output: ***
*** [HL+1] .. [HL+18] = DPB for specified drive ***
*** DPB consists of: ***
*** Name Offset Size Description ***
*** MEDIA 0 1 Media type (F8..FF) ***
*** SECSIZ 1 2 Sector size (must be 2^n) ***
*** DIRMSK 3 1 (SECSIZE/32)-1 ***
*** DIRSHFT 4 1 Number of one bits in DIRMSK ***
*** CLUSMSK 5 1 (Sectors per cluster)-1 ***
*** CLUSSHFT 6 1 (Number of one bits in CLUSMSK)+1 ***
*** FIRFAT 7 2 Logical sector number of first FAT ***
*** FATCNT 8 1 Number of FATs ***
*** MAXENT A 1 Number of directory entries (max 254) ***
*** FIRREC B 2 Logical sector number of first data ***
*** MAXCLUS D 2 Number of clusters (not including ***
*** reserved, FAT and directory sectors)+1 ***
*** FATSIZ F 1 Number of sectors used ***
*** FIRDIR 10 2 FAT logical sector number of start of ***
*** directory ***
****************************************************************/
{
int BytesPerSector,SectorsPerDisk,SectorsPerFAT,ReservedSectors;
/* If no disk, return "Not ready": */
if(!DiskPresent(CPU.AF.B.h)) { CPU.AF.W=0x0201;return; }
/* If can't read, return "Other error": */
if(!DiskRead(CPU.AF.B.h,Buf,0)) { CPU.AF.W=0x0C01;return; }
BytesPerSector = (int)Buf[0x0C]*256+Buf[0x0B];
SectorsPerDisk = (int)Buf[0x14]*256+Buf[0x13];
SectorsPerFAT = (int)Buf[0x17]*256+Buf[0x16];
ReservedSectors = (int)Buf[0x0F]*256+Buf[0x0E];
Addr=CPU.HL.W+1;
WrZ80(Addr++,Buf[0x15]); /* Format ID [F8h-FFh] */
WrZ80(Addr++,Buf[0x0B]); /* Sector size */
WrZ80(Addr++,Buf[0x0C]);
J=(BytesPerSector>>5)-1;
for(I=0;J&(1<<I);I++);
WrZ80(Addr++,J); /* Directory mask/shft */
WrZ80(Addr++,I);
J=Buf[0x0D]-1;
for(I=0;J&(1<<I);I++);
WrZ80(Addr++,J); /* Cluster mask/shift */
WrZ80(Addr++,I+1);
WrZ80(Addr++,Buf[0x0E]); /* Sector # of 1st FAT */
WrZ80(Addr++,Buf[0x0F]);
WrZ80(Addr++,Buf[0x10]); /* Number of FATs */
WrZ80(Addr++,Buf[0x11]); /* Number of dirent-s */
J=ReservedSectors+Buf[0x10]*SectorsPerFAT;
J+=32*Buf[0x11]/BytesPerSector;
WrZ80(Addr++,J&0xFF); /* Sector # of data */
WrZ80(Addr++,(J>>8)&0xFF);
J=(SectorsPerDisk-J)/Buf[0x0D];
WrZ80(Addr++,J&0xFF); /* Number of clusters */
WrZ80(Addr++,(J>>8)&0xFF);
WrZ80(Addr++,Buf[0x16]); /* Sectors per FAT */
J=ReservedSectors+Buf[0x10]*SectorsPerFAT;
WrZ80(Addr++,J&0xFF); /* Sector # of dir. */
WrZ80(Addr,(J>>8)&0xFF);
/* Return success */
CPU.AF.B.l&=~C_FLAG;
return;
}
case 0x401C:
/** DSKFMT: Disk format *****************************************
*** Input: ***
*** [A] Specified choice (1-9) [D] Drive number (0=A:) ***
*** [HL] Begin address of work area [BC] Length of work area ***
*** Output: ***
*** [F] CARRY=ERROR ***
*** Notes: ***
*** 1) Also writes a MSX boot sector at sector 0, clears all ***
*** FATs (media descriptor at first byte, 0FFh at second/ ***
*** third byte and rest zero) and clears the directory ***
*** filling it with zeros. ***
*** 2) Error codes are: ***
*** 0 Write protected 10 Write fault ***
*** 2 Not ready 12 Bad parameter ***
*** 4 Data (CRC) error 14 Insufficient memory ***
*** 6 Seek error 16 Other errors ***
*** 8 Record not found ***
****************************************************************/
{
CPU.IFF|=1;
/* If invalid choice, return "Bad parameter": */
if(!CPU.AF.B.h||(CPU.AF.B.h>2)) { CPU.AF.W=0x0C01;return; }
/* If no disk, return "Not ready": */
if(!DiskPresent(CPU.DE.B.h)) { CPU.AF.W=0x0201;return; }
/* Fill bootblock with data: */
P=BootBlock+3;
N=2-CPU.AF.B.h;
memcpy(P,"fMSXdisk",8);P+=10; /* Manufacturer's ID */
*P=Info[N].PerCluster;P+=4; /* Sectors per cluster */
*P++=Info[N].Names;*P++=0x00; /* Number of names */
*P++=Info[N].Sectors&0xFF; /* Number of sectors */
*P++=(Info[N].Sectors>>8)&0xFF;
*P++=N+0xF8; /* Format ID [F8h-FFh] */
*P++=Info[N].PerFAT;*P++=0x00; /* Sectors per FAT */
*P++=Info[N].PerTrack;*P++=0x00; /* Sectors per track */
*P++=Info[N].Heads;*P=0x00; /* Number of heads */
/* If can't write bootblock, return "Write protected": */
if(!DiskWrite(CPU.DE.B.h,BootBlock,0)) { CPU.AF.W=0x0001;return; };
/* Writing FATs: */
for(Sector=1,J=0;J<2;J++)
{
Buf[0]=N+0xF8;
Buf[1]=Buf[2]=0xFF;
memset(Buf+3,0x00,509);
if(!DiskWrite(CPU.DE.B.h,Buf,Sector++)) { CPU.AF.W=0x0A01;return; }
memset(Buf,0x00,512);
for(I=Info[N].PerFAT;I>1;I--)
if(!DiskWrite(CPU.DE.B.h,Buf,Sector++)) { CPU.AF.W=0x0A01;return; }
}
J=Info[N].Names/16; /* Directory size */
I=Info[N].Sectors-2*Info[N].PerFAT-J-1; /* Data size */
for(memset(Buf,0x00,512);J;J--)
if(!DiskWrite(CPU.DE.B.h,Buf,Sector++)) { CPU.AF.W=0x0A01;return; }
for(memset(Buf,0xFF,512);I;I--)
if(!DiskWrite(CPU.DE.B.h,Buf,Sector++)) { CPU.AF.W=0x0A01;return; }
/* Return success */
CPU.AF.B.l&=~C_FLAG;
return;
}
case 0x401F:
/** DRVOFF: Stop drives *****************************************
*** Input: None ***
*** Output: None ***
****************************************************************/
return;
case 0x00E1:
/** TAPION: Open for read and read header ***********************
****************************************************************/
{
long Pos;
if(Verbose&0x04) printf("TAPE: Looking for header...");
CPU.AF.B.l|=C_FLAG;
if(CasStream)
{
Pos=ftell(CasStream);
if(Pos&7)
if(fseek(CasStream,8-(Pos&7),SEEK_CUR))
{
if(Verbose&0x04) puts("FAILED");
rewind(CasStream);return;
}
while(fread(Buf,1,8,CasStream)==8)
if(!memcmp(Buf,TapeHeader,8))
{
if(Verbose&0x04) puts("OK");
CPU.AF.B.l&=~C_FLAG;return;
}
rewind(CasStream);
}
if(Verbose&0x04) puts("FAILED");
return;
}
case 0x00E4:
/** TAPIN: Read tape ********************************************
****************************************************************/
{
CPU.AF.B.l|=C_FLAG;
if(CasStream)
{
J=fgetc(CasStream);
if(J<0) rewind(CasStream);
else { CPU.AF.B.h=J;CPU.AF.B.l&=~C_FLAG; }
}
return;
}
case 0x00E7:
/** TAPIOF: *****************************************************
****************************************************************/
CPU.AF.B.l&=~C_FLAG;
return;
case 0x00EA:
/** TAPOON: *****************************************************
****************************************************************/
{
long Pos;
CPU.AF.B.l|=C_FLAG;
if(CasStream)
{
Pos=ftell(CasStream);
if(Pos&7)
if(fseek(CasStream,8-(Pos&7),SEEK_CUR))
{ CPU.AF.B.l|=C_FLAG;return; }
fwrite(TapeHeader,1,8,CasStream);
CPU.AF.B.l&=~C_FLAG;
}
return;
}
case 0x00ED:
/** TAPOUT: Write tape ******************************************
****************************************************************/
CPU.AF.B.l|=C_FLAG;
if(CasStream)
{
fputc(CPU.AF.B.h,CasStream);
CPU.AF.B.l&=~C_FLAG;
}
return;
case 0x00F0:
/** TAPOOF: *****************************************************
****************************************************************/
CPU.AF.B.l&=~C_FLAG;
return;
case 0x00F3:
/** STMOTR: *****************************************************
****************************************************************/
CPU.AF.B.l&=~C_FLAG;
return;
default:
printf("Unknown BIOS trap called at PC=%04Xh\n",CPU.PC.W-2);
}
}