Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Move FDD interface into a module

  • Loading branch information...
commit bf19118fd135efc3f2cdcc86411a172ba3bab313 1 parent 99c1787
Steve Conklin authored

Showing 1 changed file with 329 additions and 0 deletions. Show diff stats Hide diff stats

  1. +329 0 experimental/FDif.py
329 experimental/FDif.py
... ... @@ -0,0 +1,329 @@
  1 +#!/usr/bin/env python
  2 +
  3 +# Copyright 2012 Steve Conklin
  4 +# steve at conklinhouse dot com
  5 +#
  6 +# This program is free software; you can redistribute it and/or
  7 +# modify it under the terms of the GNU General Public License
  8 +# as published by the Free Software Foundation; either version 2
  9 +# of the License, or (at your option) any later version.
  10 +#
  11 +# This program is distributed in the hope that it will be useful,
  12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 +# GNU General Public License for more details.
  15 +#
  16 +# You should have received a copy of the GNU General Public License
  17 +# along with this program; if not, write to the Free Software
  18 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19 +
  20 +import sys
  21 +#import os
  22 +#import os.path
  23 +#import string
  24 +import time
  25 +import serial
  26 +
  27 +FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
  28 +
  29 +def dump(src, length=16):
  30 + result=[]
  31 + for i in xrange(0, len(src), length):
  32 + s = src[i:i+length]
  33 + hexa = ' '.join(["%02X"%ord(x) for x in s])
  34 + printable = s.translate(FILTER)
  35 + result.append("%04X %-*s %s\n" % (i, length*3, hexa, printable))
  36 + return ''.join(result)
  37 +
  38 +class PDD1():
  39 +
  40 + def __init__(self):
  41 + self.verbose = False
  42 + self.commport = ''
  43 + self.ser = None
  44 + return
  45 +
  46 + def __del__(self):
  47 + return
  48 +
  49 + def open(self, cport='/dev/ttyUSB0', rate = 9600, par = 'N', stpbts=1, tmout=1, xx=0):
  50 + #self.ser = serial.Serial(port = cport, baudrate = rate, parity = par, stopbits = stpbts, timeout = tmout, xonxoff=0)
  51 + self.ser = serial.Serial(port = cport, baudrate = rate, parity = par, stopbits = stpbts, timeout = tmout, xonxoff=0, rtscts=1, dsrdtr=0)
  52 + if self.ser == None:
  53 + print 'Unable to open serial device %s' % cport
  54 + raise IOError
  55 + self.commtimeout = tmout
  56 + # Sometimes on powerup there are some characters in the buffer, purge them
  57 + self.dumpchars()
  58 + return
  59 +
  60 + def close(self):
  61 + if self.ser is not None:
  62 + self.ser.close()
  63 + self.ser = None
  64 + return
  65 +
  66 + def dumpchars(self):
  67 + num = 1
  68 + while self.ser.inWaiting():
  69 + inc = self.ser.read()
  70 + if len(inc) != 0:
  71 + print 'flushed 0x%02X (%d)' % (ord(inc), num)
  72 + num = num + 1
  73 + else:
  74 + break
  75 + return
  76 +
  77 + def getFDCresponse(self, expected):
  78 + sch = self.ser.read(expected)
  79 + return sch
  80 +
  81 + def isSuccess(self, FDCstatus):
  82 + if self.verbose:
  83 + print "isSuccess checking:"
  84 + print dump(FDCstatus)
  85 + if (FDCstatus[0] == '0') and (FDCstatus[1] == '0'):
  86 + print " SUCCESS"
  87 + print " Physical Sector = %c%c" % (FDCstatus[2], FDCstatus[3])
  88 + print " Logical Sector = %c%c%c%c" % (FDCstatus[4], FDCstatus[5],FDCstatus[6], FDCstatus[7])
  89 + else:
  90 + print " ***** ERROR ***** : %c%c" % (FDCstatus[0], FDCstatus[1])
  91 + print " Physical Sector = %c%c" % (FDCstatus[2], FDCstatus[3])
  92 + print " Logical Sector = %c%c%c%c" % (FDCstatus[4], FDCstatus[5],FDCstatus[6], FDCstatus[7])
  93 + if len(FDCstatus) != 8:
  94 + print "Status Bad Len"
  95 + return False
  96 + for sb in FDCstatus[0:2]:
  97 + if sb != '0':
  98 + return False
  99 + return True
  100 +
  101 +# def readchar(self):
  102 +# inc = ''
  103 +# while len(inc) == 0:
  104 +# inc = self.ser.read()
  105 +# return inc
  106 +
  107 + def writebytes(self, bytes):
  108 + self.ser.write(bytes)
  109 + return
  110 +
  111 + def calcChecksum(self, string):
  112 + sum = 0
  113 + for schr in string:
  114 + sum = sum + ord(schr)
  115 + sum = sum % 0x100
  116 + sum = sum ^ 0xFF
  117 + return chr(sum)
  118 +
  119 +# def __commandResponse(self, command):
  120 +# if self.verbose:
  121 +# pcmd = command.strip()
  122 +# print 'writing command ===> <%s>' % pcmd
  123 +# self.dumpchars()
  124 +# ds_string = command
  125 +# cs = self.calcChecksum(ds_string)
  126 +# ds_string = ds_string + cs
  127 +# print "cR sending . . ."
  128 +# print dump(ds_string)
  129 +# self.writebytes(ds_string)
  130 +# response = self.readsomechars()
  131 +# print 'Command got a response of ', response
  132 +# return response
  133 +
  134 + def __FDCcommandResponse(self, command, expected):
  135 + if self.verbose:
  136 + pcmd = command.strip()
  137 + print '-------------------------------------\nwriting FDC command ===> <%s>' % pcmd
  138 + self.dumpchars()
  139 + self.writebytes(command)
  140 + response = self.getFDCresponse(expected)
  141 + return response
  142 + #
  143 + # Begin Op Mode commands
  144 + #
  145 +
  146 + def EnterFDCMode(self):
  147 + if False:
  148 + command = "ZZ" + chr(0x08) + chr(0)
  149 + cs = self.calcChecksum(command)
  150 + command = command + cs + "\r"
  151 + else:
  152 + command = "ZZ" + chr(0x08) + chr(0) + chr(0xF7) + "\r"
  153 + if self.verbose:
  154 + print "Entering FDC Mode, sending:"
  155 + print dump(command),
  156 + self.writebytes(command)
  157 + # There's no response to this command, so allow time to complete
  158 + time.sleep(.010)
  159 + if self.verbose:
  160 + print "Done entering FDC Mode\n"
  161 + return
  162 +
  163 + #
  164 + # Begin FDC mode commands
  165 + #
  166 +
  167 + def FDCChangeMode(self, mode = '1'):
  168 + """
  169 + Change the disk drive mode. Default to changing from FDC
  170 + emulation mode back to Operational Mode.
  171 + """
  172 + command = "M " + mode
  173 + result = self.__FDCcommandResponse(command, 8)
  174 + if not self.isSuccess(result):
  175 + raise IOError
  176 + return
  177 +
  178 + def FDCcheckDeviceCondition(self):
  179 + """
  180 + Send the 'D' command and return the result
  181 + """
  182 + command = "D\r"
  183 + result = self.__FDCcommandResponse(command, 8)
  184 + if self.verbose:
  185 + # third byte:
  186 + # 30 - Normal
  187 + # 45 - Door open or no disk
  188 + # 32 - write protected
  189 + print "third byte = 0x%X" % ord(result[2])
  190 + return result
  191 +
  192 + def FDCformat(self, sectorSize='5', verify=True):
  193 + """
  194 + Format the floppy with the requested
  195 + """
  196 + if verify:
  197 + command = "F"
  198 + else:
  199 + command = "G"
  200 + command = command + sectorSize + "\r"
  201 + result = self.__FDCcommandResponse(command, 8)
  202 + if not self.isSuccess(result):
  203 + raise IOError
  204 + return
  205 +
  206 + def FDCreadIdSection(self, psn = '0'):
  207 + """
  208 + Read the ID section of a physical sector, and return
  209 + the ID, which should be 12 bytes long
  210 + """
  211 + if self.verbose:
  212 + print "FDCreadIdSection: Enter"
  213 + command = "A " + psn + "\r"
  214 + result = self.__FDCcommandResponse(command, 8)
  215 + if not self.isSuccess(result):
  216 + raise IOError
  217 + result = self.__FDCcommandResponse("\r", 12)
  218 + if self.verbose:
  219 + print "FDCreadIdSection data:"
  220 + print dump(result)
  221 + print "FDCreadIdSection: Exit"
  222 + return result
  223 +
  224 + def FDCreadSector(self, psn = '0', lsn = '1'):
  225 + """
  226 + Read the data from a logical sector.
  227 + psn is Physical sector number, in the range 0-79
  228 + lsn is the logical sector number, in range of 1
  229 + to the max for the physical sector size
  230 + """
  231 + if self.verbose:
  232 + print "FDCreadSector: Enter"
  233 + command = "R " + psn + ","+ lsn + "\r"
  234 + result = self.__FDCcommandResponse(command, 8)
  235 + if not self.isSuccess(result):
  236 + raise IOError
  237 + if self.verbose:
  238 + print "FDCreadSector: Phase two"
  239 + result = self.__FDCcommandResponse("\r", 1024)
  240 + if self.verbose:
  241 + print "FDCreadSector data:"
  242 + print dump(result)
  243 + print "FDCreadSector: Exit"
  244 + return result
  245 +
  246 + def FDCsearchIdSection(self, data, psn = '0'):
  247 + """
  248 + Compare the data to the sector ID
  249 + psn is Physical sector number, in the range 0-79
  250 + Data length must be 12 bytes
  251 + """
  252 + if len(data) != 12:
  253 + raise ValueError("ID data must be 12 characters long")
  254 + if self.verbose:
  255 + print "FDCsearchIdSection: Enter"
  256 + command = "S" + psn + "\r"
  257 + result = self.__FDCcommandResponse(command, 8)
  258 + if not self.isSuccess(result):
  259 + raise IOError
  260 + if self.verbose:
  261 + print "FDCsearchIdSection data:"
  262 + print dump(data)
  263 + result = self.__FDCcommandResponse(data, 12)
  264 + if self.verbose:
  265 + print "FDCsearchIdSection: Exit"
  266 + print "NOT SURE WHAT WE EXPECT HERE"
  267 + print "FDCsearchIdSection status is:"
  268 + print dump(result)
  269 + if not self.isSuccess(result):
  270 + raise IOError
  271 + return
  272 +
  273 + def FDCwriteIdSection(self, data, psn = '0', verify = True):
  274 + """
  275 + Write the data to the sector ID
  276 + psn is Physical sector number, in the range 0-79
  277 + Data length must be 12 bytes
  278 + """
  279 + if len(data) != 12:
  280 + raise ValueError("ID data must be 12 characters long")
  281 + if verify:
  282 + command = "B"
  283 + else:
  284 + command = "C"
  285 + if self.verbose:
  286 + print "FDCwriteIdSection: Enter"
  287 + command = command + psn + "\r"
  288 + result = self.__FDCcommandResponse(command, 8)
  289 + if not self.isSuccess(result):
  290 + raise IOError
  291 + if self.verbose:
  292 + print "FDCwriteIdSection data:"
  293 + print dump(data)
  294 + result = self.__FDCcommandResponse(data, 8)
  295 + if self.verbose:
  296 + print "FDCwriteIdSection: Exit"
  297 + if not self.isSuccess(result):
  298 + raise IOError
  299 + return
  300 +
  301 +#W Write log sector w/verify
  302 +# (two stages - second stage send data to be written)
  303 +#X Write log sector w/o vfy
  304 +# (two stages - second stage send data to be written)
  305 + def FDCwriteSector(self, data, psn = '0', lsn = '1', verify = True):
  306 + """
  307 + Write the data to the sector
  308 + psn is Physical sector number, in the range 0-79, defaults to 0
  309 + lsn is logical sector number, defaults to 1
  310 + """
  311 + if verify:
  312 + command = "W"
  313 + else:
  314 + command = "X"
  315 + if self.verbose:
  316 + print "FDCwriteSector: Enter"
  317 + command = command + psn + "," + lsn + "\r"
  318 + result = self.__FDCcommandResponse(command, 8)
  319 + if not self.isSuccess(result):
  320 + raise IOError
  321 + if self.verbose:
  322 + print "FDCwriteSector data:"
  323 + print dump(data)
  324 + result = self.__FDCcommandResponse(data, 8)
  325 + if self.verbose:
  326 + print "FDCwriteSector: Exit"
  327 + if not self.isSuccess(result):
  328 + raise IOError
  329 + return

0 comments on commit bf19118

Please sign in to comment.
Something went wrong with that request. Please try again.