Main Page | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

libd64copy/s2.c

00001 /*
00002  *      This program is free software; you can redistribute it and/or
00003  *      modify it under the terms of the GNU General Public License
00004  *      as published by the Free Software Foundation; either version
00005  *      2 of the License, or (at your option) any later version.
00006  *
00007  *  Copyright 1999 Michael Klein <michael(dot)klein(at)puffin(dot)lb(dot)shuttle(dot)de>
00008 */
00009 
00010 #ifdef SAVE_RCSID
00011 static char *rcsid =
00012     "@(#) $Id: s2.c,v 1.13 2006/03/10 15:20:24 trikalio Exp $";
00013 #endif
00014 
00015 #include "opencbm.h"
00016 #include "d64copy_int.h"
00017 
00018 #include <stdlib.h>
00019 
00020 #include "arch.h"
00021 
00022 static const unsigned char s2_drive_prog[] = {
00023 #include "s2.inc"
00024 };
00025 
00026 static CBM_FILE fd_cbm;
00027 static int two_sided;
00028 
00029 static int s2_read_byte(CBM_FILE fd, unsigned char *c)
00030 {
00031     int i;
00032     *c = 0;
00033     for(i=4; i>0; i--) {
00034 #ifndef USE_CBM_IEC_WAIT
00035         while(cbm_iec_get(fd, IEC_CLOCK));
00036         *c = (*c>>1) | (cbm_iec_get(fd, IEC_DATA) ? 0x80 : 0);
00037 #else
00038         *c = (*c>>1) | ((cbm_iec_wait(fd, IEC_CLOCK, 0) & IEC_DATA) ? 0x80 : 0);
00039 #endif
00040         cbm_iec_release(fd, IEC_ATN);
00041 #ifndef USE_CBM_IEC_WAIT
00042         while(!cbm_iec_get(fd,IEC_CLOCK));
00043         *c = (*c>>1) | (cbm_iec_get(fd, IEC_DATA) ? 0x80 : 0);
00044 #else
00045         *c = (*c>>1) | ((cbm_iec_wait(fd, IEC_CLOCK, 1) & IEC_DATA) ? 0x80 : 0);
00046 #endif
00047         cbm_iec_set(fd, IEC_ATN);
00048     }
00049     return 0;
00050 }
00051 
00052 static int s2_write_byte_nohs(CBM_FILE fd, unsigned char c)
00053 {
00054     int i;
00055     for(i=4; ; i--) {
00056         c & 1 ? cbm_iec_set(fd, IEC_DATA) : cbm_iec_release(fd, IEC_DATA);
00057         c >>= 1;
00058         cbm_iec_release(fd, IEC_ATN);
00059 #ifndef USE_CBM_IEC_WAIT
00060         while(cbm_iec_get(fd, IEC_CLOCK));
00061 #else
00062         cbm_iec_wait(fd, IEC_CLOCK, 0);
00063 #endif
00064         c & 1 ? cbm_iec_set(fd, IEC_DATA) : cbm_iec_release(fd, IEC_DATA);
00065         c >>= 1;
00066         cbm_iec_set(fd, IEC_ATN);
00067 
00068         if(i<=1) return 0;
00069 
00070 #ifndef USE_CBM_IEC_WAIT
00071         while(!cbm_iec_get(fd, IEC_CLOCK));
00072 #else
00073         cbm_iec_wait(fd, IEC_CLOCK, 1);
00074 #endif
00075     }
00076 }
00077 
00078 static int s2_write_byte(CBM_FILE fd, unsigned char c)
00079 {
00080     s2_write_byte_nohs(fd, c);
00081 #ifndef USE_CBM_IEC_WAIT
00082     while(!cbm_iec_get(fd, IEC_CLOCK));
00083 #else
00084     cbm_iec_wait(fd, IEC_CLOCK, 1);
00085 #endif
00086     cbm_iec_release(fd, IEC_DATA);
00087     return 0;
00088 }
00089 
00090 static int read_block(unsigned char tr, unsigned char se, unsigned char *block)
00091 {
00092     int  i;
00093     unsigned char status;
00094 
00095     s2_write_byte(fd_cbm, tr);
00096     s2_write_byte(fd_cbm, se);
00097 #ifndef USE_CBM_IEC_WAIT
00098     arch_usleep(20000);
00099 #endif
00100     s2_read_byte(fd_cbm, &status);
00101     for(i=0;i<BLOCKSIZE;i++) s2_read_byte(fd_cbm, &block[i]);
00102 
00103     return status;
00104 }
00105 
00106 static int write_block(unsigned char tr, unsigned char se, const unsigned char *blk, int size, int read_status)
00107 {
00108     int  i;
00109     unsigned char status;
00110 
00111     s2_write_byte(fd_cbm, tr);
00112     s2_write_byte(fd_cbm, se);
00113     for(i=0;i<size;i++) s2_write_byte(fd_cbm, blk[i]);
00114 
00115 #ifndef USE_CBM_IEC_WAIT
00116     if(size == BLOCKSIZE) {
00117         arch_usleep(20000);
00118     }
00119 #endif
00120     s2_read_byte(fd_cbm, &status);
00121 
00122     return status;
00123 }
00124 
00125 static int open_disk(CBM_FILE fd, d64copy_settings *settings,
00126                      const void *arg, int for_writing,
00127                      turbo_start start, d64copy_message_cb message_cb)
00128 {
00129     unsigned char d = (unsigned char)(ULONG_PTR)arg;
00130 
00131     fd_cbm = fd;
00132     two_sided = settings->two_sided;
00133 
00134     cbm_upload(fd_cbm, d, 0x700, s2_drive_prog, sizeof(s2_drive_prog));
00135     start(fd, d);
00136     cbm_iec_release(fd_cbm, IEC_CLOCK);
00137     while(!cbm_iec_get(fd_cbm, IEC_CLOCK));
00138     cbm_iec_set(fd_cbm, IEC_ATN);
00139     arch_usleep(20000);
00140     
00141     return 0;
00142 }
00143 
00144 static void close_disk(void)
00145 {
00146     s2_write_byte(fd_cbm, 0);
00147     s2_write_byte_nohs(fd_cbm, 0);
00148     arch_usleep(100);
00149     cbm_iec_release(fd_cbm, IEC_DATA);
00150     cbm_iec_release(fd_cbm, IEC_ATN);
00151     cbm_iec_set(fd_cbm, IEC_CLOCK);
00152 }
00153 
00154 static int send_track_map(unsigned char tr, const char *trackmap, unsigned char count)
00155 {
00156     int i;
00157     s2_write_byte(fd_cbm, tr);
00158     s2_write_byte(fd_cbm, count);
00159     for(i = 0; i < d64copy_sector_count(two_sided, tr); i++)
00160     {
00161         s2_write_byte(fd_cbm, (unsigned char) !NEED_SECTOR(trackmap[i]));
00162     }
00163     return 0;
00164 }
00165 
00166 static int read_gcr_block(unsigned char *se, unsigned char *gcrbuf)
00167 {
00168     int i;
00169     unsigned char s;
00170 
00171     s2_read_byte(fd_cbm, &s);
00172     *se = s;
00173     s2_read_byte(fd_cbm,  &s);
00174 
00175     if(s) {
00176         return s;
00177     }
00178     for(i = 0; i < GCRBUFSIZE; i++) {
00179         s2_read_byte(fd_cbm, &gcrbuf[i]);
00180     }
00181 
00182     return 0;
00183 }
00184 
00185 DECLARE_TRANSFER_FUNCS_EX(s2_transfer, 1, 1);

Generated on Sun Apr 30 18:45:58 2006 for opencbm by  doxygen 1.4.2