/* Fake86: A portable, open-source 8086 PC emulator. Copyright (C)2010-2013 Mike Chambers This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* disk.c: disk emulation routines for Fake86. works at the BIOS interrupt 13h level. */ #include #include #include "disk.h" #include "cpu.h" extern uint8_t RAM[0x100000], cf, hdcount; extern uint16_t segregs[4]; extern union _bytewordregs_ regs; extern uint8_t read86 (uint32_t addr32); extern void write86 (uint32_t addr32, uint8_t value); struct struct_drive disk[256]; uint8_t sectorbuffer[512]; uint8_t insertdisk (uint8_t drivenum, char *filename) { if (disk[drivenum].inserted) fclose (disk[drivenum].diskfile); else disk[drivenum].inserted = 1; disk[drivenum].diskfile = fopen (filename, "r+b"); if (disk[drivenum].diskfile==NULL) { disk[drivenum].inserted = 0; return (1); } fseek (disk[drivenum].diskfile, 0L, SEEK_END); disk[drivenum].filesize = ftell (disk[drivenum].diskfile); fseek (disk[drivenum].diskfile, 0L, SEEK_SET); if (drivenum >= 0x80) { //it's a hard disk image disk[drivenum].sects = 63; disk[drivenum].heads = 16; disk[drivenum].cyls = disk[drivenum].filesize / (disk[drivenum].sects * disk[drivenum].heads * 512); hdcount++; } else { //it's a floppy image disk[drivenum].cyls = 80; disk[drivenum].sects = 18; disk[drivenum].heads = 2; if (disk[drivenum].filesize <= 1228800) disk[drivenum].sects = 15; if (disk[drivenum].filesize <= 737280) disk[drivenum].sects = 9; if (disk[drivenum].filesize <= 368640) { disk[drivenum].cyls = 40; disk[drivenum].sects = 9; } if (disk[drivenum].filesize <= 163840) { disk[drivenum].cyls = 40; disk[drivenum].sects = 8; disk[drivenum].heads = 1; } } return (0); } void ejectdisk (uint8_t drivenum) { disk[drivenum].inserted = 0; if (disk[drivenum].diskfile != NULL) fclose (disk[drivenum].diskfile); } void readdisk (uint8_t drivenum, uint16_t dstseg, uint16_t dstoff, uint16_t cyl, uint16_t sect, uint16_t head, uint16_t sectcount) { uint32_t memdest, lba, fileoffset, cursect, sectoffset; if (!sect || !disk[drivenum].inserted) return; lba = ( (uint32_t) cyl * (uint32_t) disk[drivenum].heads + (uint32_t) head) * (uint32_t) disk[drivenum].sects + (uint32_t) sect - 1; fileoffset = lba * 512; if (fileoffset>disk[drivenum].filesize) return; fseek (disk[drivenum].diskfile, fileoffset, SEEK_SET); memdest = ( (uint32_t) dstseg << 4) + (uint32_t) dstoff; //for the readdisk function, we need to use write86 instead of directly fread'ing into //the RAM array, so that read-only flags are honored. otherwise, a program could load //data from a disk over BIOS or other ROM code that it shouldn't be able to. for (cursect=0; cursectdisk[drivenum].filesize) return; fseek (disk[drivenum].diskfile, fileoffset, SEEK_SET); memdest = ( (uint32_t) dstseg << 4) + (uint32_t) dstoff; for (cursect=0; cursect