SCSIBus.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 
00045 #include "StdAfx.h"
00046 #include "SCSIBus.h"
00047 
00053 CSCSIBus::CSCSIBus(CConfigurator* cfg, CSystem* c) : CSystemComponent(cfg, c)
00054 {
00055   int i;
00056   for(i = 0; i < 16; i++)
00057     targets[i] = 0;
00058 
00059   state.phase = SCSI_PHASE_FREE;
00060   state.target = -1;
00061   state.initiator = -1;
00062 }
00063 
00067 CSCSIBus::~CSCSIBus(void)
00068 { }
00069 
00075 void CSCSIBus::scsi_register(CSCSIDevice* dev, int bus, int target)
00076 {
00077   if(targets[target] && targets[target] != dev)
00078     FAILURE(IllegalState, "More than one SCSI device at the same ID");
00079   targets[target] = dev;
00080   target_bus_no[target] = bus;
00081 }
00082 
00089 void CSCSIBus::scsi_unregister(CSCSIDevice* dev, int target)
00090 {
00091   if(targets[target] != dev)
00092     FAILURE(IllegalState, "Attempt to unregister other SCSI device");
00093   targets[target] = 0;
00094 }
00095 
00102 bool CSCSIBus::arbitrate(int initiator)
00103 {
00104   if(state.phase != SCSI_PHASE_FREE && state.initiator != initiator)
00105   {
00106     printf("Could not arbitrate for the SCSI bus.\n");
00107     return false;
00108   }
00109 
00110   state.initiator = initiator;
00111   state.phase = SCSI_PHASE_ARBITRATION;
00112   return true;
00113 }
00114 
00123 bool CSCSIBus::select(int initiator, int target)
00124 {
00125   if(state.phase != SCSI_PHASE_ARBITRATION || state.initiator != initiator)
00126     FAILURE(IllegalState,
00127             "Attempt to select while the device has not won SCSI arbitration");
00128 
00129   if(!targets[target])
00130     return false;
00131 
00132   state.target = target;
00133   targets[target]->scsi_select_me(target_bus_no[target]);
00134   return(state.phase >= 0);
00135 }
00136 
00142 void CSCSIBus::set_phase(int target, int phase)
00143 {
00144   if(targets[target] != targets[state.target])
00145     FAILURE(IllegalState,
00146             "Attempt to set phase while the device has not been selected");
00147 
00148   state.phase = phase;
00149 }
00150 
00157 void CSCSIBus::free_bus(int initiator)
00158 {
00159 
00160   // nothing to do
00161   if(state.phase == SCSI_PHASE_FREE)
00162     return;
00163 
00164   if(state.phase == SCSI_PHASE_ARBITRATION)
00165   {
00166     if(initiator != state.initiator)
00167       FAILURE(IllegalState, "Attempt to free the scsi bus");
00168   }
00169   else
00170   {
00171     if(targets[initiator] != targets[state.target])
00172       FAILURE(IllegalState, "Attempt to free the scsi bus");
00173   }
00174 
00175   state.phase = SCSI_PHASE_FREE;
00176 }
00177 
00178 static u32  scsi_magic1 = 0x5C510123;
00179 static u32  scsi_magic2 = 0x32105c51;
00180 
00184 int CSCSIBus::SaveState(FILE* f)
00185 {
00186   long  ss = sizeof(state);
00187 
00188   fwrite(&scsi_magic1, sizeof(u32), 1, f);
00189   fwrite(&ss, sizeof(long), 1, f);
00190   fwrite(&state, sizeof(state), 1, f);
00191   fwrite(&scsi_magic2, sizeof(u32), 1, f);
00192   printf("%s: %d bytes saved.\n", devid_string, (int) ss);
00193   return 0;
00194 }
00195 
00199 int CSCSIBus::RestoreState(FILE* f)
00200 {
00201   long    ss;
00202   u32     m1;
00203   u32     m2;
00204   size_t  r;
00205 
00206   r = fread(&m1, sizeof(u32), 1, f);
00207   if(r != 1)
00208   {
00209     printf("%s: unexpected end of file!\n", devid_string);
00210     return -1;
00211   }
00212 
00213   if(m1 != scsi_magic1)
00214   {
00215     printf("%s: MAGIC 1 does not match!\n", devid_string);
00216     return -1;
00217   }
00218 
00219   fread(&ss, sizeof(long), 1, f);
00220   if(r != 1)
00221   {
00222     printf("%s: unexpected end of file!\n", devid_string);
00223     return -1;
00224   }
00225 
00226   if(ss != sizeof(state))
00227   {
00228     printf("%s: STRUCT SIZE does not match!\n", devid_string);
00229     return -1;
00230   }
00231 
00232   fread(&state, sizeof(state), 1, f);
00233   if(r != 1)
00234   {
00235     printf("%s: unexpected end of file!\n", devid_string);
00236     return -1;
00237   }
00238 
00239   r = fread(&m2, sizeof(u32), 1, f);
00240   if(r != 1)
00241   {
00242     printf("%s: unexpected end of file!\n", devid_string);
00243     return -1;
00244   }
00245 
00246   if(m2 != scsi_magic2)
00247   {
00248     printf("%s: MAGIC 1 does not match!\n", devid_string);
00249     return -1;
00250   }
00251 
00252   printf("%s: %d bytes restored.\n", devid_string, (int) ss);
00253   return 0;
00254 }

SourceForge.net Logo
Project space on SourceForge.net