aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/lib/adafruit_floppy/src
diff options
context:
space:
mode:
Diffstat (limited to 'circuitpython/lib/adafruit_floppy/src')
-rw-r--r--circuitpython/lib/adafruit_floppy/src/Adafruit_Floppy.cpp524
-rw-r--r--circuitpython/lib/adafruit_floppy/src/Adafruit_Floppy.h150
-rw-r--r--circuitpython/lib/adafruit_floppy/src/Adafruit_MFM_Floppy.cpp206
-rw-r--r--circuitpython/lib/adafruit_floppy/src/mfm_impl.h266
4 files changed, 1146 insertions, 0 deletions
diff --git a/circuitpython/lib/adafruit_floppy/src/Adafruit_Floppy.cpp b/circuitpython/lib/adafruit_floppy/src/Adafruit_Floppy.cpp
new file mode 100644
index 0000000..eef1aa0
--- /dev/null
+++ b/circuitpython/lib/adafruit_floppy/src/Adafruit_Floppy.cpp
@@ -0,0 +1,524 @@
+#include "Adafruit_Floppy.h"
+
+#define DEBUG_FLOPPY (0)
+
+// We need to read and write some pins at optimized speeds - use raw registers
+// or native SDK API!
+#ifdef BUSIO_USE_FAST_PINIO
+#define read_index() (*indexPort & indexMask)
+#define read_data() (*dataPort & dataMask)
+#define set_debug_led() (*ledPort |= ledMask)
+#define clr_debug_led() (*ledPort &= ~ledMask)
+#elif defined(ARDUINO_ARCH_RP2040)
+#define read_index() gpio_get(_indexpin)
+#define read_data() gpio_get(_rddatapin)
+#define set_debug_led() gpio_put(led_pin, 1)
+#define clr_debug_led() gpio_put(led_pin, 0)
+#endif
+
+uint32_t T2_5 = T2_5_IBMPC_HD;
+uint32_t T3_5 = T3_5_IBMPC_HD;
+
+#if !DEBUG_FLOPPY
+#undef set_debug_led
+#undef clr_debug_led
+#define set_debug_led() ((void)0)
+#define clr_debug_led() ((void)0)
+#endif
+
+#define MFM_IO_MMIO (1)
+#include "mfm_impl.h"
+
+/**************************************************************************/
+/*!
+ @brief Create a hardware interface to a floppy drive
+ @param densitypin A pin connected to the floppy Density Select input
+ @param indexpin A pin connected to the floppy Index Sensor output
+ @param selectpin A pin connected to the floppy Drive Select input
+ @param motorpin A pin connected to the floppy Motor Enable input
+ @param directionpin A pin connected to the floppy Stepper Direction input
+ @param steppin A pin connected to the floppy Stepper input
+ @param wrdatapin A pin connected to the floppy Write Data input
+ @param wrgatepin A pin connected to the floppy Write Gate input
+ @param track0pin A pin connected to the floppy Track 00 Sensor output
+ @param protectpin A pin connected to the floppy Write Protect Sensor output
+ @param rddatapin A pin connected to the floppy Read Data output
+ @param sidepin A pin connected to the floppy Side Select input
+ @param readypin A pin connected to the floppy Ready/Disk Change output
+
+*/
+/**************************************************************************/
+
+Adafruit_Floppy::Adafruit_Floppy(int8_t densitypin, int8_t indexpin,
+ int8_t selectpin, int8_t motorpin,
+ int8_t directionpin, int8_t steppin,
+ int8_t wrdatapin, int8_t wrgatepin,
+ int8_t track0pin, int8_t protectpin,
+ int8_t rddatapin, int8_t sidepin,
+ int8_t readypin) {
+ _densitypin = densitypin;
+ _indexpin = indexpin;
+ _selectpin = selectpin;
+ _motorpin = motorpin;
+ _directionpin = directionpin;
+ _steppin = steppin;
+ _wrdatapin = wrdatapin;
+ _wrgatepin = wrgatepin;
+ _track0pin = track0pin;
+ _protectpin = protectpin;
+ _rddatapin = rddatapin;
+ _sidepin = sidepin;
+ _readypin = readypin;
+}
+
+/**************************************************************************/
+/*!
+ @brief Initializes the GPIO pins but do not start the motor or anything
+*/
+/**************************************************************************/
+void Adafruit_Floppy::begin(void) { soft_reset(); }
+
+/**************************************************************************/
+/*!
+ @brief Set back the object and pins to initial state
+*/
+/**************************************************************************/
+void Adafruit_Floppy::soft_reset(void) {
+ // deselect drive
+ pinMode(_selectpin, OUTPUT);
+ digitalWrite(_selectpin, HIGH);
+
+ // motor enable pin, drive low to turn on motor
+ pinMode(_motorpin, OUTPUT);
+ digitalWrite(_motorpin, HIGH);
+
+ // set motor direction (low is in, high is out)
+ pinMode(_directionpin, OUTPUT);
+ digitalWrite(_directionpin, LOW); // move inwards to start
+
+ // step track pin, pulse low for 3us min, 3ms max per pulse
+ pinMode(_steppin, OUTPUT);
+ digitalWrite(_steppin, HIGH);
+
+ // side selector
+ pinMode(_sidepin, OUTPUT);
+ digitalWrite(_sidepin, HIGH); // side 0 to start
+
+ pinMode(_indexpin, INPUT_PULLUP);
+ pinMode(_track0pin, INPUT_PULLUP);
+ pinMode(_protectpin, INPUT_PULLUP);
+ pinMode(_readypin, INPUT_PULLUP);
+ pinMode(_rddatapin, INPUT_PULLUP);
+
+ // set high density
+ pinMode(_densitypin, OUTPUT);
+ digitalWrite(_densitypin, LOW);
+
+ // set write OFF
+ if (_wrdatapin >= 0) {
+ pinMode(_wrdatapin, OUTPUT);
+ digitalWrite(_wrdatapin, HIGH);
+ }
+ if (_wrgatepin >= 0) {
+ pinMode(_wrgatepin, OUTPUT);
+ digitalWrite(_wrgatepin, HIGH);
+ }
+
+#ifdef BUSIO_USE_FAST_PINIO
+ indexPort = (BusIO_PortReg *)portInputRegister(digitalPinToPort(_indexpin));
+ indexMask = digitalPinToBitMask(_indexpin);
+#endif
+
+ select_delay_us = 10;
+ step_delay_us = 10000;
+ settle_delay_ms = 15;
+ motor_delay_ms = 1000;
+ watchdog_delay_ms = 1000;
+ bus_type = BUSTYPE_IBMPC;
+
+ if (led_pin >= 0) {
+ pinMode(led_pin, OUTPUT);
+ digitalWrite(led_pin, LOW);
+ }
+}
+
+/**************************************************************************/
+/*!
+ @brief Whether to select this drive
+ @param selected True to select/enable
+*/
+/**************************************************************************/
+void Adafruit_Floppy::select(bool selected) {
+ digitalWrite(_selectpin, !selected); // Selected logic level 0!
+ // Select drive
+ delayMicroseconds(select_delay_us);
+}
+
+/**************************************************************************/
+/*!
+ @brief Which head/side to read from
+ @param head Head 0 or 1
+*/
+/**************************************************************************/
+void Adafruit_Floppy::side(uint8_t head) {
+ digitalWrite(_sidepin, !head); // Head 0 is logic level 1, head 1 is logic 0!
+}
+
+/**************************************************************************/
+/*!
+ @brief Turn on or off the floppy motor, if on we wait till we get an index
+ pulse!
+ @param motor_on True to turn on motor, False to turn it off
+ @returns False if turning motor on and no index pulse found, true otherwise
+*/
+/**************************************************************************/
+bool Adafruit_Floppy::spin_motor(bool motor_on) {
+ digitalWrite(_motorpin, !motor_on); // Motor on is logic level 0!
+ if (!motor_on)
+ return true; // we're done, easy!
+
+ delay(motor_delay_ms); // Main motor turn on
+
+ uint32_t index_stamp = millis();
+ bool timedout = false;
+
+ if (debug_serial)
+ debug_serial->print("Waiting for index pulse...");
+
+ while (digitalRead(_indexpin)) {
+ if ((millis() - index_stamp) > 10000) {
+ timedout = true; // its been 10 seconds?
+ break;
+ }
+ }
+
+ if (timedout) {
+ if (debug_serial)
+ debug_serial->println("Didn't find an index pulse!");
+ return false;
+ }
+ if (debug_serial)
+ debug_serial->println("Found!");
+ return true;
+}
+
+/**************************************************************************/
+/*!
+ @brief Seek to the desired track, requires the motor to be spun up!
+ @param track_num The track to step to
+ @return True If we were able to get to the track location
+*/
+/**************************************************************************/
+bool Adafruit_Floppy::goto_track(uint8_t track_num) {
+ // track 0 is a very special case because its the only one we actually know we
+ // got to. if we dont know where we are, or we're going to track zero, step
+ // back till we get there.
+ if ((_track < 0) || track_num == 0) {
+ if (debug_serial)
+ debug_serial->println("Going to track 0");
+
+ // step back a lil more than expected just in case we really seeked out
+ uint8_t max_steps = 100;
+ while (max_steps--) {
+ if (!digitalRead(_track0pin)) {
+ _track = 0;
+ break;
+ }
+ step(STEP_OUT, 1);
+ }
+
+ if (digitalRead(_track0pin)) {
+ // we never got a track 0 indicator :(
+ // what if we try stepping in a bit??
+
+ max_steps = 20;
+ while (max_steps--) {
+ if (!digitalRead(_track0pin)) {
+ _track = 0;
+ break;
+ }
+ step(STEP_IN, 1);
+ }
+
+ if (digitalRead(_track0pin)) {
+ // STILL not found!
+ if (debug_serial)
+ debug_serial->println("Could not find track 0");
+ return false; // we 'timed' out, were not able to locate track 0
+ }
+ }
+ }
+ delay(settle_delay_ms);
+
+ // ok its a non-track 0 step, first, we cant go past 79 ok?
+ track_num = min(track_num, FLOPPY_IBMPC_HD_TRACKS - 1);
+ if (debug_serial)
+ debug_serial->printf("Going to track %d\n\r", track_num);
+
+ if (_track == track_num) { // we are there already
+ return true;
+ }
+
+ int8_t steps = (int8_t)track_num - (int8_t)_track;
+ if (steps > 0) {
+ if (debug_serial)
+ debug_serial->printf("Step in %d times\n\r", steps);
+ step(STEP_IN, steps);
+ } else {
+ steps = abs(steps);
+ if (debug_serial)
+ debug_serial->printf("Step out %d times\n\r", steps);
+ step(STEP_OUT, steps);
+ }
+ delay(settle_delay_ms);
+ _track = track_num;
+
+ return true;
+}
+
+/**************************************************************************/
+/*!
+ @brief Step the track motor
+ @param dir STEP_OUT or STEP_IN depending on desired direction
+ @param times How many steps to take
+*/
+/**************************************************************************/
+void Adafruit_Floppy::step(bool dir, uint8_t times) {
+ digitalWrite(_directionpin, dir);
+ delayMicroseconds(10); // 1 microsecond, but we're generous
+
+ while (times--) {
+ digitalWrite(_steppin, HIGH);
+ delay((step_delay_us / 1000UL) + 1); // round up to at least 1ms
+ digitalWrite(_steppin, LOW);
+ delay((step_delay_us / 1000UL) + 1);
+ digitalWrite(_steppin, HIGH); // end high
+ yield();
+ }
+ // one more for good measure (5.25" drives seemed to like this)
+ delay((step_delay_us / 1000UL) + 1);
+}
+
+/**************************************************************************/
+/*!
+ @brief The current track location, based on internal caching
+ @return The cached track location
+*/
+/**************************************************************************/
+int8_t Adafruit_Floppy::track(void) { return _track; }
+
+/**************************************************************************/
+/*!
+ @brief Capture and decode one track of MFM data
+ @param sectors A pointer to an array of memory we can use to store into,
+ 512*n_sectors bytes
+ @param n_sectors The number of sectors (e.g., 18 for a
+ standard 3.5", 1.44MB format)
+ @param sector_validity An array of values set to 1 if the sector was
+ captured, 0 if not captured (no IDAM, CRC error, etc)
+ @return Number of sectors we actually captured
+*/
+/**************************************************************************/
+uint32_t Adafruit_Floppy::read_track_mfm(uint8_t *sectors, size_t n_sectors,
+ uint8_t *sector_validity) {
+ mfm_io_t io;
+#ifdef BUSIO_USE_FAST_PINIO
+ BusIO_PortReg *dataPort, *ledPort;
+ BusIO_PortMask dataMask, ledMask;
+ dataPort = (BusIO_PortReg *)portInputRegister(digitalPinToPort(_rddatapin));
+ dataMask = digitalPinToBitMask(_rddatapin);
+ ledPort = (BusIO_PortReg *)portOutputRegister(digitalPinToPort(led_pin));
+ ledMask = digitalPinToBitMask(led_pin);
+ (void)ledPort;
+ (void)ledMask;
+ io.index_port = indexPort;
+ io.index_mask = indexMask;
+ io.data_port = dataPort;
+ io.data_mask = dataMask;
+#elif defined(ARDUINO_ARCH_RP2040)
+ io.index_port = &sio_hw->gpio_in;
+ io.index_mask = 1u << _indexpin;
+ io.data_port = &sio_hw->gpio_in;
+ io.data_mask = 1u << _rddatapin;
+#endif
+
+ noInterrupts();
+ int result = read_track(io, n_sectors, sectors, sector_validity);
+ interrupts();
+
+ return result;
+}
+
+/**************************************************************************/
+/*!
+ @brief Capture one track's worth of flux transitions, between two falling
+ index pulses
+ @param pulses A pointer to an array of memory we can use to store into
+ @param max_pulses The size of the allocated pulses array
+ @return Number of pulses we actually captured
+*/
+/**************************************************************************/
+uint32_t Adafruit_Floppy::capture_track(uint8_t *pulses, uint32_t max_pulses) {
+ unsigned pulse_count;
+ uint8_t *pulses_ptr = pulses;
+ uint8_t *pulses_end = pulses + max_pulses;
+
+#ifdef BUSIO_USE_FAST_PINIO
+ BusIO_PortReg *dataPort, *ledPort;
+ BusIO_PortMask dataMask, ledMask;
+ dataPort = (BusIO_PortReg *)portInputRegister(digitalPinToPort(_rddatapin));
+ dataMask = digitalPinToBitMask(_rddatapin);
+ ledPort = (BusIO_PortReg *)portOutputRegister(digitalPinToPort(led_pin));
+ ledMask = digitalPinToBitMask(led_pin);
+ (void)ledPort;
+ (void)ledMask;
+#endif
+
+ memset(pulses, 0, max_pulses); // zero zem out
+
+ noInterrupts();
+ wait_for_index_pulse_low();
+
+ // wait for one clean flux pulse so we dont get cut off.
+ // don't worry about losing this pulse, we'll get it on our
+ // overlap run!
+
+ // ok we have a h-to-l transition so...
+ bool last_index_state = read_index();
+ uint8_t index_transitions = 0;
+
+ // if data line is low, wait till it rises
+ if (!read_data()) {
+ while (!read_data())
+ ;
+ }
+ // if data line is high, wait till it drops down
+ if (read_data()) {
+ while (read_data())
+ ;
+ }
+
+ while (true) {
+ bool index_state = read_index();
+ // ahh a L to H transition
+ if (!last_index_state && index_state) {
+ index_transitions++;
+ if (index_transitions ==
+ 2) // and its the second one, so we're done with this track!
+ break;
+ }
+ last_index_state = index_state;
+
+ // muahaha, now we can read track data!
+ // Don't start counting at zero because we lost some time checking for
+ // index. Empirically, at 180MHz and -O3 on M4, this gives the most 'even'
+ // timings, moving the bins from 41/63/83 to 44/66/89
+ pulse_count = 3;
+
+ // while pulse is in the low pulse, count up
+ while (!read_data()) {
+ pulse_count++;
+ }
+ set_debug_led();
+
+ // while pulse is high, keep counting up
+ while (read_data())
+ pulse_count++;
+ clr_debug_led();
+
+ pulses_ptr[0] = min(255u, pulse_count);
+ pulses_ptr++;
+ if (pulses_ptr == pulses_end) {
+ break;
+ }
+ }
+ // whew done
+ interrupts();
+ return pulses_ptr - pulses;
+}
+
+/**************************************************************************/
+/*!
+ @brief Busy wait until the index line goes from high to low
+*/
+/**************************************************************************/
+void Adafruit_Floppy::wait_for_index_pulse_low(void) {
+ // initial state
+ bool index_state = read_index();
+ bool last_index_state = index_state;
+
+ // wait until last index state is H and current state is L
+ while (true) {
+ index_state = read_index();
+ if (last_index_state && !index_state) {
+ return;
+ }
+ last_index_state = index_state;
+ }
+}
+
+/**************************************************************************/
+/*!
+ @brief Pretty print the counts in a list of flux transitions
+ @param pulses A pointer to an array of memory containing pulse counts
+ @param num_pulses The size of the pulses in the array
+*/
+/**************************************************************************/
+void Adafruit_Floppy::print_pulses(uint8_t *pulses, uint32_t num_pulses) {
+ if (!debug_serial)
+ return;
+
+ for (uint32_t i = 0; i < num_pulses; i++) {
+ debug_serial->print(pulses[i]);
+ debug_serial->print(", ");
+ }
+ debug_serial->println();
+}
+/**************************************************************************/
+/*!
+ @brief Pretty print a simple histogram of flux transitions
+ @param pulses A pointer to an array of memory containing pulse counts
+ @param num_pulses The size of the pulses in the array
+ @param max_bins The maximum number of histogram bins to use (default 64)
+*/
+/**************************************************************************/
+void Adafruit_Floppy::print_pulse_bins(uint8_t *pulses, uint32_t num_pulses,
+ uint8_t max_bins) {
+ if (!debug_serial)
+ return;
+
+ // lets bin em!
+ uint32_t bins[max_bins][2];
+ memset(bins, 0, max_bins * 2 * sizeof(uint32_t));
+ // we'll add each pulse to a bin so we can figure out the 3 buckets
+ for (uint32_t i = 0; i < num_pulses; i++) {
+ uint8_t p = pulses[i];
+ // find a bin for this pulse
+ uint8_t bin = 0;
+ for (bin = 0; bin < max_bins; bin++) {
+ // bin already exists? increment the count!
+ if (bins[bin][0] == p) {
+ bins[bin][1]++;
+ break;
+ }
+ if (bins[bin][0] == 0) {
+ // ok we never found the bin, so lets make it this one!
+ bins[bin][0] = p;
+ bins[bin][1] = 1;
+ break;
+ }
+ }
+ if (bin == max_bins)
+ debug_serial->println("oof we ran out of bins but we'll keep going");
+ }
+ // this is a very lazy way to print the bins sorted
+ for (uint8_t pulse_w = 1; pulse_w < 255; pulse_w++) {
+ for (uint8_t b = 0; b < max_bins; b++) {
+ if (bins[b][0] == pulse_w) {
+ debug_serial->print(bins[b][0]);
+ debug_serial->print(": ");
+ debug_serial->println(bins[b][1]);
+ }
+ }
+ }
+}
diff --git a/circuitpython/lib/adafruit_floppy/src/Adafruit_Floppy.h b/circuitpython/lib/adafruit_floppy/src/Adafruit_Floppy.h
new file mode 100644
index 0000000..3e519eb
--- /dev/null
+++ b/circuitpython/lib/adafruit_floppy/src/Adafruit_Floppy.h
@@ -0,0 +1,150 @@
+#ifndef ADAFRUIT_FLOPPY_H
+#define ADAFRUIT_FLOPPY_H
+
+#include "Arduino.h"
+#include <Adafruit_SPIDevice.h>
+// to implement SdFat Block Driver
+#include "SdFat.h"
+#include "SdFatConfig.h"
+
+#define FLOPPY_IBMPC_HD_TRACKS 80
+#define FLOPPY_IBMPC_DD_TRACKS 40
+#define FLOPPY_HEADS 2
+
+#define MFM_IBMPC1440K_SECTORS_PER_TRACK 18
+#define MFM_IBMPC360K_SECTORS_PER_TRACK 9
+#define MFM_BYTES_PER_SECTOR 512UL
+
+#ifdef BUSIO_USE_FAST_PINIO
+#define FLOPPYIO_SAMPLERATE (F_CPU * 11u / 90u) // empirical on SAM D51 @ 120MHz
+#endif
+
+#if defined(ARDUINO_ARCH_RP2040)
+#undef FLOPPYIO_SAMPLERATE
+#define FLOPPYIO_SAMPLERATE (F_CPU * 13u / 100u) // empirical on RP2040 @ 200MHz
+#endif
+
+#define T2_5_IBMPC_HD (FLOPPYIO_SAMPLERATE * 5 / 2 / 1000000)
+#define T3_5_IBMPC_HD (FLOPPYIO_SAMPLERATE * 7 / 2 / 1000000)
+#define T2_5_IBMPC_DD (T2_5_IBMPC_HD * 2)
+#define T3_5_IBMPC_DD (T3_5_IBMPC_HD * 2)
+
+#define STEP_OUT HIGH
+#define STEP_IN LOW
+#define MAX_FLUX_PULSE_PER_TRACK \
+ (uint32_t)(500000UL / 5 * \
+ 1.5) // 500khz / 5 hz per track rotation, 1.5 rotations
+
+#define BUSTYPE_IBMPC 1
+#define BUSTYPE_SHUGART 2
+
+typedef enum {
+ IBMPC1440K,
+ IBMPC360K,
+} adafruit_floppy_disk_t;
+
+/**************************************************************************/
+/*!
+ @brief A helper class for chattin with floppy drives
+*/
+/**************************************************************************/
+class Adafruit_Floppy {
+public:
+ Adafruit_Floppy(int8_t densitypin, int8_t indexpin, int8_t selectpin,
+ int8_t motorpin, int8_t directionpin, int8_t steppin,
+ int8_t wrdatapin, int8_t wrgatepin, int8_t track0pin,
+ int8_t protectpin, int8_t rddatapin, int8_t sidepin,
+ int8_t readypin);
+ void begin(void);
+ void soft_reset(void);
+
+ void select(bool selected);
+ bool spin_motor(bool motor_on);
+ bool goto_track(uint8_t track);
+ void side(uint8_t head);
+ int8_t track(void);
+ void step(bool dir, uint8_t times);
+
+ uint32_t read_track_mfm(uint8_t *sectors, size_t n_sectors,
+ uint8_t *sector_validity);
+ uint32_t capture_track(uint8_t *pulses, uint32_t max_pulses)
+ __attribute__((optimize("O3")));
+ void print_pulse_bins(uint8_t *pulses, uint32_t num_pulses,
+ uint8_t max_bins = 64);
+ void print_pulses(uint8_t *pulses, uint32_t num_pulses);
+
+ int8_t led_pin = LED_BUILTIN; ///< Debug LED output for tracing
+
+ uint16_t select_delay_us = 10; ///< delay after drive select (usecs)
+ uint16_t step_delay_us = 10000; ///< delay between head steps (usecs)
+ uint16_t settle_delay_ms = 15; ///< settle delay after seek (msecs)
+ uint16_t motor_delay_ms = 1000; ///< delay after motor on (msecs)
+ uint16_t watchdog_delay_ms =
+ 1000; ///< quiescent time until drives reset (msecs)
+ uint8_t bus_type = BUSTYPE_IBMPC; ///< what kind of floppy drive we're using
+
+ Stream *debug_serial = NULL; ///< optional debug stream for serial output
+
+private:
+ void wait_for_index_pulse_low(void);
+
+ // theres a lot of GPIO!
+ int8_t _densitypin, _indexpin, _selectpin, _motorpin, _directionpin, _steppin,
+ _wrdatapin, _wrgatepin, _track0pin, _protectpin, _rddatapin, _sidepin,
+ _readypin;
+
+ int8_t _track = -1;
+
+#ifdef BUSIO_USE_FAST_PINIO
+ BusIO_PortReg *indexPort;
+ BusIO_PortMask indexMask;
+#endif
+};
+
+/**************************************************************************/
+/*!
+ This class adds support for the BaseBlockDriver interface to an MFM
+ encoded floppy disk. This allows it to be used with SdFat's FatFileSystem
+ class. or for a mass storage device
+*/
+/**************************************************************************/
+class Adafruit_MFM_Floppy : public BaseBlockDriver {
+public:
+ Adafruit_MFM_Floppy(Adafruit_Floppy *floppy,
+ adafruit_floppy_disk_t format = IBMPC1440K);
+
+ bool begin(void);
+ bool end(void);
+
+ uint32_t size(void);
+ int32_t readTrack(uint8_t track, bool head);
+
+ /**! @brief The expected number of sectors per track in this format
+ @returns The number of sectors per track */
+ uint8_t sectors_per_track(void) { return _sectors_per_track; }
+ /**! @brief The expected number of tracks per side in this format
+ @returns The number of tracks per side */
+ uint8_t tracks_per_side(void) { return _tracks_per_side; }
+
+ //------------- SdFat BaseBlockDRiver API -------------//
+ virtual bool readBlock(uint32_t block, uint8_t *dst);
+ virtual bool writeBlock(uint32_t block, const uint8_t *src);
+ virtual bool syncBlocks();
+ virtual bool readBlocks(uint32_t block, uint8_t *dst, size_t nb);
+ virtual bool writeBlocks(uint32_t block, const uint8_t *src, size_t nb);
+
+ /**! The raw byte decoded data from the last track read */
+ uint8_t track_data[MFM_IBMPC1440K_SECTORS_PER_TRACK * MFM_BYTES_PER_SECTOR];
+
+ /**! Which tracks from the last track-read were valid MFM/CRC! */
+ uint8_t track_validity[MFM_IBMPC1440K_SECTORS_PER_TRACK];
+
+private:
+ uint8_t _sectors_per_track = 0;
+ uint8_t _tracks_per_side = 0;
+ int8_t _last_track_read = -1; // last cached track
+ Adafruit_Floppy *_floppy = NULL;
+ adafruit_floppy_disk_t _format = IBMPC1440K;
+};
+
+#endif
diff --git a/circuitpython/lib/adafruit_floppy/src/Adafruit_MFM_Floppy.cpp b/circuitpython/lib/adafruit_floppy/src/Adafruit_MFM_Floppy.cpp
new file mode 100644
index 0000000..6128a20
--- /dev/null
+++ b/circuitpython/lib/adafruit_floppy/src/Adafruit_MFM_Floppy.cpp
@@ -0,0 +1,206 @@
+#include <Adafruit_Floppy.h>
+
+extern uint32_t T2_5, T3_5;
+
+/**************************************************************************/
+/*!
+ @brief Instantiate an MFM-formatted floppy
+ @param floppy An Adafruit_Floppy object that has the pins defined
+ @param format What kind of format we will be parsing out - we DO NOT
+ autodetect!
+*/
+/**************************************************************************/
+Adafruit_MFM_Floppy::Adafruit_MFM_Floppy(Adafruit_Floppy *floppy,
+ adafruit_floppy_disk_t format) {
+ _floppy = floppy;
+ _format = format;
+
+ // different formats have different 'hardcoded' sectors and tracks
+ if (_format == IBMPC1440K) {
+ _sectors_per_track = MFM_IBMPC1440K_SECTORS_PER_TRACK;
+ _tracks_per_side = FLOPPY_IBMPC_HD_TRACKS;
+ T2_5 = T2_5_IBMPC_HD;
+ T3_5 = T3_5_IBMPC_HD;
+ } else if (_format == IBMPC360K) {
+ _sectors_per_track = MFM_IBMPC360K_SECTORS_PER_TRACK;
+ _tracks_per_side = FLOPPY_IBMPC_DD_TRACKS;
+ T2_5 = T2_5_IBMPC_DD;
+ T3_5 = T3_5_IBMPC_DD;
+ }
+}
+
+/**************************************************************************/
+/*!
+ @brief Initialize and spin up the floppy drive
+ @returns True if we were able to spin up and detect an index track
+*/
+/**************************************************************************/
+bool Adafruit_MFM_Floppy::begin(void) {
+ if (!_floppy)
+ return false;
+ _floppy->begin();
+
+ // now's the time to tweak settings
+ if (_format == IBMPC360K) {
+ _floppy->step_delay_us = 65000UL; // lets make it max 65ms not 10ms?
+ _floppy->settle_delay_ms = 50; // 50ms not 15
+ }
+
+ _floppy->select(true);
+
+ return _floppy->spin_motor(true);
+}
+
+/**************************************************************************/
+/*!
+ @brief Spin down and deselect the motor and drive
+ @returns True always
+*/
+/**************************************************************************/
+bool Adafruit_MFM_Floppy::end(void) {
+ _floppy->spin_motor(false);
+ _floppy->select(false);
+ return true;
+}
+
+/**************************************************************************/
+/*!
+ @brief Quick calculator for expected max capacity
+ @returns Size of the drive in bytes
+*/
+/**************************************************************************/
+uint32_t Adafruit_MFM_Floppy::size(void) {
+ return (uint32_t)_tracks_per_side * FLOPPY_HEADS * _sectors_per_track *
+ MFM_BYTES_PER_SECTOR;
+}
+
+/**************************************************************************/
+/*!
+ @brief Read one track's worth of data and MFM decode it
+ @param track track number, 0 to whatever is the max tracks for the given
+ @param head which side to read, false for side 1, true for side 2
+ format during instantiation (e.g. 40 for DD, 80 for HD)
+ @returns Number of sectors captured, or -1 if we couldn't seek
+*/
+/**************************************************************************/
+int32_t Adafruit_MFM_Floppy::readTrack(uint8_t track, bool head) {
+
+ // Serial.printf("\tSeeking track %d head %d...", track, head);
+ if (!_floppy->goto_track(track)) {
+ // Serial.println("failed to seek to track");
+ return -1;
+ }
+ _floppy->side(head);
+ // Serial.println("done!");
+ uint32_t captured_sectors =
+ _floppy->read_track_mfm(track_data, _sectors_per_track, track_validity);
+ /*
+ Serial.print("Captured %d sectors", captured_sectors);
+
+ Serial.print("Validity: ");
+ for(size_t i=0; i<MFM_SECTORS_PER_TRACK; i++) {
+ Serial.print(track_validity[i] ? "V" : "?");
+ }
+ */
+ return captured_sectors;
+}
+
+//--------------------------------------------------------------------+
+// SdFat BaseBlockDriver API
+// A block is 512 bytes
+//--------------------------------------------------------------------+
+
+/**************************************************************************/
+/*!
+ @brief Read a 512 byte block of data, may used cached data
+ @param block Block number, will be split into head and track based on
+ expected formatting
+ @param dst Destination buffer
+ @returns True on success
+*/
+/**************************************************************************/
+bool Adafruit_MFM_Floppy::readBlock(uint32_t block, uint8_t *dst) {
+ uint8_t track = block / (FLOPPY_HEADS * _sectors_per_track);
+ uint8_t head = (block / _sectors_per_track) % FLOPPY_HEADS;
+ uint8_t subsector = block % _sectors_per_track;
+
+ // Serial.printf("\tRead request block %d\n", block);
+ if ((track * FLOPPY_HEADS + head) != _last_track_read) {
+ // oof it is not cached!
+
+ if (readTrack(track, head) == -1) {
+ return false;
+ }
+
+ _last_track_read = track * FLOPPY_HEADS + head;
+ }
+
+ if (!track_validity[subsector]) {
+ // Serial.println("subsector invalid");
+ return false;
+ }
+ // Serial.println("OK!");
+ memcpy(dst, track_data + (subsector * MFM_BYTES_PER_SECTOR),
+ MFM_BYTES_PER_SECTOR);
+
+ return true;
+}
+
+/**************************************************************************/
+/*!
+ @brief Read multiple 512 byte block of data, may used cached data
+ @param block Starting block number, will be split into head and track based
+ on expected formatting
+ @param dst Destination buffer
+ @param nb Number of blocks to read
+ @returns True on success
+*/
+/**************************************************************************/
+bool Adafruit_MFM_Floppy::readBlocks(uint32_t block, uint8_t *dst, size_t nb) {
+ // read each block one by one
+ for (size_t blocknum = 0; blocknum < nb; blocknum++) {
+ if (!readBlock(block + blocknum, dst + (blocknum * MFM_BYTES_PER_SECTOR)))
+ return false;
+ }
+ return true;
+}
+
+/**************************************************************************/
+/*!
+ @brief Write a 512 byte block of data NOT IMPLEMENTED YET
+ @param block Block number, will be split into head and track based on
+ expected formatting
+ @param src Source buffer
+ @returns True on success, false if failed or unimplemented
+*/
+/**************************************************************************/
+bool Adafruit_MFM_Floppy::writeBlock(uint32_t block, const uint8_t *src) {
+ Serial.printf("Writing block %d\n", block);
+ (void *)src;
+ return false;
+}
+
+/**************************************************************************/
+/*!
+ @brief Write multiple 512 byte blocks of data NOT IMPLEMENTED YET
+ @param block Starting lock number, will be split into head and track based
+ on expected formatting
+ @param src Source buffer
+ @param nb Number of consecutive blocks to write
+ @returns True on success, false if failed or unimplemented
+*/
+/**************************************************************************/
+bool Adafruit_MFM_Floppy::writeBlocks(uint32_t block, const uint8_t *src,
+ size_t nb) {
+ Serial.printf("Writing %d blocks %d\n", nb, block);
+ (void *)src;
+ return false;
+}
+
+/**************************************************************************/
+/*!
+ @brief Sync written blocks NOT IMPLEMENTED YET
+ @returns True on success, false if failed or unimplemented
+*/
+/**************************************************************************/
+bool Adafruit_MFM_Floppy::syncBlocks() { return false; }
diff --git a/circuitpython/lib/adafruit_floppy/src/mfm_impl.h b/circuitpython/lib/adafruit_floppy/src/mfm_impl.h
new file mode 100644
index 0000000..bfbe7ba
--- /dev/null
+++ b/circuitpython/lib/adafruit_floppy/src/mfm_impl.h
@@ -0,0 +1,266 @@
+// SPDX-FileCopyrightText: 2022 Jeff Epler for Adafruit Industries
+//
+// SPDX-License-Identifier: MIT
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+#pragma GCC push_options
+#pragma GCC optimize("-O3")
+typedef struct mfm_io mfm_io_t;
+
+#ifndef MFM_IO_MMIO
+#define MFM_IO_MMIO (0)
+#endif
+
+// If you have a memory mapped peripheral, define MFM_IO_MMIO to get an
+// implementation of the mfm_io functions. then, just populate the fields with
+// the actual registers to use and define T2_5 and T3_5 to the empirical values
+// dividing between T2/3 and T3/4 pulses.
+#if MFM_IO_MMIO
+struct mfm_io {
+ const volatile uint32_t *index_port;
+ uint32_t index_mask;
+ const volatile uint32_t *data_port;
+ uint32_t data_mask;
+ unsigned index_state;
+ unsigned index_count;
+};
+#endif
+
+typedef enum { pulse_10, pulse_100, pulse_1000 } mfm_io_symbol_t;
+
+typedef enum { odd = 0, even = 1 } mfm_state_t;
+
+enum { IDAM = 0xfe, DAM = 0xfb };
+
+enum { blocksize = 512, overhead = 3, metadata_size = 7 };
+__attribute__((always_inline)) static inline mfm_io_symbol_t
+mfm_io_read_symbol(mfm_io_t *io);
+static void mfm_io_reset_sync_count(mfm_io_t *io);
+__attribute__((always_inline)) static int mfm_io_get_sync_count(mfm_io_t *io);
+
+// Automatically generated CRC function
+// polynomial: 0x11021
+static uint16_t crc16(uint8_t *data, int len, uint16_t crc) {
+ static const uint16_t table[256] = {
+ 0x0000U, 0x1021U, 0x2042U, 0x3063U, 0x4084U, 0x50A5U, 0x60C6U, 0x70E7U,
+ 0x8108U, 0x9129U, 0xA14AU, 0xB16BU, 0xC18CU, 0xD1ADU, 0xE1CEU, 0xF1EFU,
+ 0x1231U, 0x0210U, 0x3273U, 0x2252U, 0x52B5U, 0x4294U, 0x72F7U, 0x62D6U,
+ 0x9339U, 0x8318U, 0xB37BU, 0xA35AU, 0xD3BDU, 0xC39CU, 0xF3FFU, 0xE3DEU,
+ 0x2462U, 0x3443U, 0x0420U, 0x1401U, 0x64E6U, 0x74C7U, 0x44A4U, 0x5485U,
+ 0xA56AU, 0xB54BU, 0x8528U, 0x9509U, 0xE5EEU, 0xF5CFU, 0xC5ACU, 0xD58DU,
+ 0x3653U, 0x2672U, 0x1611U, 0x0630U, 0x76D7U, 0x66F6U, 0x5695U, 0x46B4U,
+ 0xB75BU, 0xA77AU, 0x9719U, 0x8738U, 0xF7DFU, 0xE7FEU, 0xD79DU, 0xC7BCU,
+ 0x48C4U, 0x58E5U, 0x6886U, 0x78A7U, 0x0840U, 0x1861U, 0x2802U, 0x3823U,
+ 0xC9CCU, 0xD9EDU, 0xE98EU, 0xF9AFU, 0x8948U, 0x9969U, 0xA90AU, 0xB92BU,
+ 0x5AF5U, 0x4AD4U, 0x7AB7U, 0x6A96U, 0x1A71U, 0x0A50U, 0x3A33U, 0x2A12U,
+ 0xDBFDU, 0xCBDCU, 0xFBBFU, 0xEB9EU, 0x9B79U, 0x8B58U, 0xBB3BU, 0xAB1AU,
+ 0x6CA6U, 0x7C87U, 0x4CE4U, 0x5CC5U, 0x2C22U, 0x3C03U, 0x0C60U, 0x1C41U,
+ 0xEDAEU, 0xFD8FU, 0xCDECU, 0xDDCDU, 0xAD2AU, 0xBD0BU, 0x8D68U, 0x9D49U,
+ 0x7E97U, 0x6EB6U, 0x5ED5U, 0x4EF4U, 0x3E13U, 0x2E32U, 0x1E51U, 0x0E70U,
+ 0xFF9FU, 0xEFBEU, 0xDFDDU, 0xCFFCU, 0xBF1BU, 0xAF3AU, 0x9F59U, 0x8F78U,
+ 0x9188U, 0x81A9U, 0xB1CAU, 0xA1EBU, 0xD10CU, 0xC12DU, 0xF14EU, 0xE16FU,
+ 0x1080U, 0x00A1U, 0x30C2U, 0x20E3U, 0x5004U, 0x4025U, 0x7046U, 0x6067U,
+ 0x83B9U, 0x9398U, 0xA3FBU, 0xB3DAU, 0xC33DU, 0xD31CU, 0xE37FU, 0xF35EU,
+ 0x02B1U, 0x1290U, 0x22F3U, 0x32D2U, 0x4235U, 0x5214U, 0x6277U, 0x7256U,
+ 0xB5EAU, 0xA5CBU, 0x95A8U, 0x8589U, 0xF56EU, 0xE54FU, 0xD52CU, 0xC50DU,
+ 0x34E2U, 0x24C3U, 0x14A0U, 0x0481U, 0x7466U, 0x6447U, 0x5424U, 0x4405U,
+ 0xA7DBU, 0xB7FAU, 0x8799U, 0x97B8U, 0xE75FU, 0xF77EU, 0xC71DU, 0xD73CU,
+ 0x26D3U, 0x36F2U, 0x0691U, 0x16B0U, 0x6657U, 0x7676U, 0x4615U, 0x5634U,
+ 0xD94CU, 0xC96DU, 0xF90EU, 0xE92FU, 0x99C8U, 0x89E9U, 0xB98AU, 0xA9ABU,
+ 0x5844U, 0x4865U, 0x7806U, 0x6827U, 0x18C0U, 0x08E1U, 0x3882U, 0x28A3U,
+ 0xCB7DU, 0xDB5CU, 0xEB3FU, 0xFB1EU, 0x8BF9U, 0x9BD8U, 0xABBBU, 0xBB9AU,
+ 0x4A75U, 0x5A54U, 0x6A37U, 0x7A16U, 0x0AF1U, 0x1AD0U, 0x2AB3U, 0x3A92U,
+ 0xFD2EU, 0xED0FU, 0xDD6CU, 0xCD4DU, 0xBDAAU, 0xAD8BU, 0x9DE8U, 0x8DC9U,
+ 0x7C26U, 0x6C07U, 0x5C64U, 0x4C45U, 0x3CA2U, 0x2C83U, 0x1CE0U, 0x0CC1U,
+ 0xEF1FU, 0xFF3EU, 0xCF5DU, 0xDF7CU, 0xAF9BU, 0xBFBAU, 0x8FD9U, 0x9FF8U,
+ 0x6E17U, 0x7E36U, 0x4E55U, 0x5E74U, 0x2E93U, 0x3EB2U, 0x0ED1U, 0x1EF0U,
+ };
+
+ while (len > 0) {
+ crc = table[*data ^ (uint8_t)(crc >> 8)] ^ (crc << 8);
+ data++;
+ len--;
+ }
+ return crc;
+}
+
+enum { triple_mark_magic = 0x09926499, triple_mark_mask = 0x0fffffff };
+
+__attribute__((always_inline)) inline static bool
+wait_triple_sync_mark(mfm_io_t *io) {
+ uint32_t state = 0;
+ while (mfm_io_get_sync_count(io) < 3 && state != triple_mark_magic) {
+ state = ((state << 2) | mfm_io_read_symbol(io)) & triple_mark_mask;
+ }
+ return state == triple_mark_magic;
+}
+
+// Compute the MFM CRC of the data, _assuming it was preceded by three 0xa1 sync
+// bytes
+static int crc16_preloaded(unsigned char *buf, size_t n) {
+ return crc16((uint8_t *)buf, n, 0xcdb4);
+}
+
+// Copy 'n' bytes of data into 'buf'
+__attribute__((always_inline)) inline static void
+receive(mfm_io_t *io, unsigned char *buf, size_t n) {
+ // `tmp` holds up to 9 bits of data, in bits 6..15.
+ unsigned tmp = 0, weight = 0x8000;
+
+#define PUT_BIT(x) \
+ do { \
+ if (x) \
+ tmp |= weight; \
+ weight >>= 1; \
+ } while (0)
+
+ // In MFM, flux marks can be 2, 3, or 4 "T" apart. These three signals
+ // stand for the bit sequences 10, 100, and 1000. However, half of the
+ // bits are data bits, and half are 'clock' bits. We have to keep track of
+ // whether [in the next symbol] we want the "even" bit(s) or the "odd" bit(s):
+ //
+ // 10 - leaves even/odd (parity) unchanged
+ // 100 - inverts even/odd (parity)
+ // 1000 - leaves even/odd (parity) unchanged
+ // ^ ^ data bits if state is even
+ // ^ ^ data bits if state is odd
+
+ // We do this by knowing that when we arrive, we are waiting to parse the
+ // final '1' data bit of the MFM sync mark. This means we apply a special rule
+ // to the first word, starting as though in the 'even' state but not recording
+ // the '1' bit.
+ mfm_io_symbol_t s = mfm_io_read_symbol(io);
+ mfm_state_t state = even;
+ switch (s) {
+ case pulse_100: // first data bit is a 0, and we start in the ODD state
+ state = odd;
+ /* fallthrough */
+ case pulse_1000: // first data bit is a 0, and we start in EVEN state
+ PUT_BIT(0);
+ break;
+ default:
+ break;
+ }
+
+ while (n) {
+ s = mfm_io_read_symbol(io);
+ PUT_BIT(state); // 'even' is 1, so record a '1' or '0' as appropriate
+ if (s == pulse_1000) {
+ PUT_BIT(0); // the other bit recorded for a 1000 is always a '0'
+ }
+ if (s == pulse_100) {
+ if (state) {
+ PUT_BIT(0);
+ } // If 'even', record an additional '0'
+ state = (mfm_state_t)!state; // the next symbol has opposite parity
+ }
+
+ *buf = tmp >> 8; // store every time to make timing more even
+ if (weight <= 0x80) {
+ tmp <<= 8;
+ weight <<= 8;
+ buf++;
+ n--;
+ }
+ }
+}
+
+// Perform all the steps of receiving the next IDAM, DAM (or DDAM, but we don't
+// use them)
+__attribute__((always_inline)) inline static bool
+wait_triple_sync_mark_receive_crc(mfm_io_t *io, void *buf, size_t n) {
+ if (!wait_triple_sync_mark(io)) {
+ return false;
+ }
+ receive(io, (uint8_t *)buf, n);
+ unsigned crc = crc16_preloaded((uint8_t *)buf, n);
+ return crc == 0;
+}
+
+// Read a whole track, setting validity[] for each sector actually read, up to
+// n_sectors indexing of validity & data is 0-based, even though IDAMs store
+// sectors as 1-based
+static int read_track(mfm_io_t io, int n_sectors, void *data,
+ uint8_t *validity) {
+ memset(validity, 0, n_sectors);
+
+ int n_valid = 0;
+
+ mfm_io_reset_sync_count(&io);
+
+ unsigned char buf[512 + 3];
+ while (mfm_io_get_sync_count(&io) < 3 && n_valid < n_sectors) {
+ if (!wait_triple_sync_mark_receive_crc(&io, buf, metadata_size)) {
+ continue;
+ }
+ if (buf[0] != IDAM) {
+ continue;
+ }
+
+ int r = (uint8_t)buf[3] - 1;
+ if (r >= n_sectors) {
+ continue;
+ }
+
+ if (validity[r]) {
+ continue;
+ }
+
+ if (!wait_triple_sync_mark_receive_crc(&io, buf, sizeof(buf))) {
+ continue;
+ }
+ if (buf[0] != DAM) {
+ continue;
+ }
+
+ memcpy((char *)data + blocksize * r, buf + 1, blocksize);
+ validity[r] = 1;
+ n_valid++;
+ }
+ return n_valid;
+}
+
+#if MFM_IO_MMIO
+#define READ_DATA() (!!(*io->data_port & io->data_mask))
+#define READ_INDEX() (!!(*io->index_port & io->index_mask))
+__attribute__((optimize("O3"), always_inline)) static inline mfm_io_symbol_t
+mfm_io_read_symbol(mfm_io_t *io) {
+ unsigned pulse_count = 3;
+ while (!READ_DATA()) {
+ pulse_count++;
+ }
+
+ unsigned index_state = (io->index_state << 1) | READ_INDEX();
+ if ((index_state & 3) == 2) { // a zero-to-one transition
+ io->index_count++;
+ }
+ io->index_state = index_state;
+
+ while (READ_DATA()) {
+ pulse_count++;
+ }
+
+ int result = pulse_10;
+ if (pulse_count > T2_5) {
+ result++;
+ }
+ if (pulse_count > T3_5) {
+ result++;
+ }
+
+ return (mfm_io_symbol_t)result;
+}
+
+static void mfm_io_reset_sync_count(mfm_io_t *io) { io->index_count = 0; }
+
+__attribute__((optimize("O3"), always_inline)) inline static int
+mfm_io_get_sync_count(mfm_io_t *io) {
+ return io->index_count;
+}
+#endif
+
+#pragma GCC pop_options