DiskRam.cpp

Go to the documentation of this file.
00001 /* ES40 emulator.
00002  * Copyright (C) 2007-2008 by the ES40 Emulator Project
00003  *
00004  * WWW    : http://sourceforge.net/projects/es40
00005  * E-mail : camiel@camicom.com
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  * 
00021  * Although this is not required, the author would appreciate being notified of, 
00022  * and receiving any modifications you may make to the source code that might serve
00023  * the general public.
00024  */
00025 
00081 #include "StdAfx.h"
00082 #include "DiskRam.h"
00083 
00084 CDiskRam::CDiskRam(CConfigurator*  cfg, CSystem*  sys, CDiskController*  c,
00085                    int idebus, int idedev) : CDisk(cfg, sys, c, idebus, idedev)
00086 {
00087   byte_size = myCfg->get_num_value("size", false, 512 * 1024 * 1024);
00088 
00089   CHECK_ALLOCATION(ramdisk = malloc((size_t) byte_size));
00090 
00091   state.byte_pos = 0;
00092 
00093   sectors = 32;
00094   heads = 8;
00095 
00096   //calc_cylinders();
00097   determine_layout();
00098 
00099   model_number = myCfg->get_text_value("model_number", "ES40RAMDISK");
00100 
00101   printf("%s: Mounted RAMDISK, %"LL "d %d-byte blocks, %"LL "d/%d/%d.\n",
00102          devid_string, byte_size / state.block_size, state.block_size,
00103          cylinders, heads, sectors);
00104 }
00105 
00106 CDiskRam::~CDiskRam(void)
00107 {
00108   if(ramdisk)
00109   {
00110     printf("%s: RAMDISK freed.\n", devid_string);
00111     free(ramdisk);
00112     ramdisk = 0;
00113   }
00114 }
00115 
00116 bool CDiskRam::seek_byte(off_t_large byte)
00117 {
00118   if(byte >= byte_size)
00119   {
00120     FAILURE_1(InvalidArgument, "%s: Seek beyond end of file!\n", devid_string);
00121   }
00122 
00123   state.byte_pos = byte;
00124   return true;
00125 }
00126 
00127 size_t CDiskRam::read_bytes(void* dest, size_t bytes)
00128 {
00129   if(state.byte_pos >= byte_size)
00130     return 0;
00131 
00132   while(state.byte_pos + bytes >= byte_size)
00133     bytes--;
00134 
00135   memcpy(dest, &(((char*) ramdisk)[state.byte_pos]), bytes);
00136   state.byte_pos += (unsigned long) bytes;
00137   return bytes;
00138 }
00139 
00140 size_t CDiskRam::write_bytes(void* src, size_t bytes)
00141 {
00142   if(state.byte_pos >= byte_size)
00143     return 0;
00144 
00145   while(state.byte_pos + bytes >= byte_size)
00146     bytes--;
00147 
00148   memcpy(&(((char*) ramdisk)[state.byte_pos]), src, bytes);
00149   state.byte_pos += (unsigned long) bytes;
00150   return bytes;
00151 }

SourceForge.net Logo
Project space on SourceForge.net