Skip to content

Commit

Permalink
clangify
Browse files Browse the repository at this point in the history
  • Loading branch information
jepler committed Mar 20, 2024
1 parent d5a2788 commit ce83b33
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 59 deletions.
14 changes: 9 additions & 5 deletions examples/04_msd_test/04_msd_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ volatile uint32_t flush_time;

volatile uint32_t index_count;
volatile uint32_t index_time, last_index_time;
void count_index() { index_count += 1; last_index_time = index_time; index_time = millis(); }
void count_index() {
index_count += 1;
last_index_time = index_time;
index_time = millis();
}

bool index_delayed, ready_delayed;
uint32_t old_index_count;
Expand All @@ -150,13 +154,13 @@ void loop() {
// ready pin fell or no index for 400ms: media removed
// (the check for nonzero index count is an attempt to future-proof against
// a no-index 3.5" drive)
bool removed = (!ready && ready_delayed)
|| (index_count && time_since_index > 1200);
bool removed =
(!ready && ready_delayed) || (index_count && time_since_index > 1200);

if (removed) {
if (mfm_floppy.sectorCount() != 0) {
Serial.println("removed");
mfm_floppy.removed();
Serial.println("removed");
mfm_floppy.removed();
}
}
if (new_index_count != old_index_count) {
Expand Down
14 changes: 6 additions & 8 deletions src/Adafruit_Floppy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,17 +476,15 @@ static void set_timings(uint32_t sampleFrequency, mfm_io_t &io,
(double density) or 2.0f (high density)
@param clear_validity Whether to clear the validity flag. Set to false if
re-reading a track with errors.
@param logical_track If not NULL, updated with the logical track number of the last sector read. (track & side numbers are not otherwise verified)
@param logical_track If not NULL, updated with the logical track number of
the last sector read. (track & side numbers are not otherwise verified)
@return Number of sectors we actually captured
*/
/**************************************************************************/
size_t Adafruit_FloppyBase::decode_track_mfm(uint8_t *sectors, size_t n_sectors,
uint8_t *sector_validity,
const uint8_t *pulses,
size_t n_pulses,
float nominal_bit_time_us,
bool clear_validity,
uint8_t *logical_track) {
size_t Adafruit_FloppyBase::decode_track_mfm(
uint8_t *sectors, size_t n_sectors, uint8_t *sector_validity,
const uint8_t *pulses, size_t n_pulses, float nominal_bit_time_us,
bool clear_validity, uint8_t *logical_track) {
mfm_io_t io;

if (clear_validity)
Expand Down
3 changes: 1 addition & 2 deletions src/Adafruit_Floppy.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ class Adafruit_FloppyBase {

size_t encode_track_mfm(const uint8_t *sectors, size_t n_sectors,
uint8_t *pulses, size_t max_pulses,
float nominal_bit_time_us,
uint8_t logical_track);
float nominal_bit_time_us, uint8_t logical_track);

size_t capture_track(volatile uint8_t *pulses, size_t max_pulses,
int32_t *falling_index_offset,
Expand Down
34 changes: 19 additions & 15 deletions src/Adafruit_MFM_Floppy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ uint32_t Adafruit_MFM_Floppy::size(void) const {
/**************************************************************************/
/*!
@brief Read one track's worth of data and MFM decode it
@param logical_track the logical track number, 0 to whatever is the max tracks for the given
format during instantiation (e.g. 40 for DD, 80 for HD)
@param logical_track the logical track number, 0 to whatever is the max
tracks for the given format during instantiation (e.g. 40 for DD, 80 for HD)
@param head which side to read, false for side 1, true for side 2
@returns Number of sectors captured, or -1 if we couldn't seek
*/
Expand All @@ -117,7 +117,8 @@ int32_t Adafruit_MFM_Floppy::readTrack(uint8_t logical_track, bool head) {

uint8_t physical_track = _double_step ? 2 * logical_track : logical_track;

Serial.printf("\t[readTrack] Seeking track %d [phys=%d] head %d...\r\n", logical_track, physical_track, head);
Serial.printf("\t[readTrack] Seeking track %d [phys=%d] head %d...\r\n",
logical_track, physical_track, head);
if (!_floppy->goto_track(physical_track)) {
// Serial.println("failed to seek to track");
return -1;
Expand All @@ -141,8 +142,8 @@ int32_t Adafruit_MFM_Floppy::readTrack(uint8_t logical_track, bool head) {

_track_has_errors = (captured_sectors != _sectors_per_track);
if (_track_has_errors) {
Serial.printf("Track %d/%d has errors (%d != %d)\n", logical_track, head, captured_sectors,
_sectors_per_track);
Serial.printf("Track %d/%d has errors (%d != %d)\n", logical_track, head,
captured_sectors, _sectors_per_track);
}
_last_track_read = logical_track * FLOPPY_HEADS + head;
return captured_sectors;
Expand Down Expand Up @@ -309,7 +310,8 @@ bool Adafruit_MFM_Floppy::syncDevice() {
int head = _last_track_read % FLOPPY_HEADS;

uint8_t physical_track = _double_step ? 2 * logical_track : logical_track;
Serial.printf("Flushing track %d [phys %d] side %d\r\n", logical_track, physical_track, head);
Serial.printf("Flushing track %d [phys %d] side %d\r\n", logical_track,
physical_track, head);
// should be a no-op
if (!_floppy->goto_track(physical_track)) {
Serial.println("failed to seek to track");
Expand All @@ -332,7 +334,8 @@ bool Adafruit_MFM_Floppy::syncDevice() {
return false;
}
_n_flux = _floppy->encode_track_mfm(track_data, _sectors_per_track, _flux,
sizeof(_flux), _high_density ? 1.f : 2.f, logical_track);
sizeof(_flux), _high_density ? 1.f : 2.f,
logical_track);

if (!_floppy->write_track(_flux, _n_flux, false)) {
Serial.println("failed to write track");
Expand Down Expand Up @@ -383,20 +386,21 @@ bool Adafruit_MFM_Floppy::autodetect() {

if (_tracks_per_side <= 40) {
_floppy->goto_track(2);
_n_flux = _floppy->capture_track(_flux, sizeof(_flux) / 16, &index_offset,
false, 220);
_n_flux = _floppy->capture_track(_flux, sizeof(_flux) / 16,
&index_offset, false, 220);
uint8_t track_number;
auto captured_sectors =
_floppy->decode_track_mfm(track_data, 1, track_validity, _flux, _n_flux,
flux_rate_ns / 1000.f, true, &track_number);
auto captured_sectors = _floppy->decode_track_mfm(
track_data, 1, track_validity, _flux, _n_flux,
flux_rate_ns / 1000.f, true, &track_number);
if (!captured_sectors) {
Serial.printf("failed to read on physical track 2\r\n");
}
_double_step = (track_number == 1);
Serial.printf("on physical track 2, track_number=%d. _double_step <- %d\r\n",
track_number, _double_step);
Serial.printf(
"on physical track 2, track_number=%d. _double_step <- %d\r\n",
track_number, _double_step);
} else {
_double_step = false;
_double_step = false;
}
Serial.printf("Detected flux rate %dns/bit\r\n%d/%d/%d C/H/S\r\n",
flux_rate_ns, _tracks_per_side, heads, _sectors_per_track);
Expand Down
52 changes: 23 additions & 29 deletions src/mfm_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// SPDX-License-Identifier: MIT

#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>

#if !defined(DEBUG_PRINTF)
#define DEBUG_PRINTF(...) ((void)0)
Expand Down Expand Up @@ -149,12 +149,12 @@ static bool skip_triple_sync_mark(mfm_io_t *io) {
enum { mfm_io_crc_preload_value = 0xcdb4 };

// Copy data into a series of buffers, returning the CRC.
// This must be called right after sync_triple_sync_mark, because an assumption is made
// about the code that's about to be read.
// This must be called right after sync_triple_sync_mark, because an assumption
// is made about the code that's about to be read.
//
// The "..." arguments must be pairs of (uint8_t *buf, size_t n), ending with a NULL buf.
__attribute__((sentinel))
static uint16_t receive_crc(mfm_io_t *io, ...) {
// The "..." arguments must be pairs of (uint8_t *buf, size_t n), ending with a
// NULL buf.
__attribute__((sentinel)) static uint16_t receive_crc(mfm_io_t *io, ...) {
// `tmp` holds up to 9 bits of data, in bits 6..15.
unsigned tmp = 0, weight = 0x8000;
uint16_t crc = mfm_io_crc_preload_value;
Expand Down Expand Up @@ -203,14 +203,15 @@ static uint16_t receive_crc(mfm_io_t *io, ...) {
size_t n = va_arg(ap, size_t);
while (n) {
s = mfm_io_read_symbol(io);
PUT_BIT(state); // 'mfm_io_even' is 1, so record a '1' or '0' as appropriate
PUT_BIT(
state); // 'mfm_io_even' is 1, so record a '1' or '0' as appropriate
if (s == mfm_io_pulse_1000) {
PUT_BIT(0); // the other bit recorded for a 1000 is always a '0'
}
if (s == mfm_io_pulse_100) {
if (state) {
PUT_BIT(0);
} // If 'mfm_io_even', record an additional '0'
} // If 'mfm_io_even', record an additional '0'
state = (mfm_state_t)!state; // the next symbol has opposite parity
}

Expand Down Expand Up @@ -255,20 +256,16 @@ static size_t decode_track_mfm(mfm_io_t *io) {
// floppies which always use 512 byte sectors
while (!mfm_io_eof(io) && io->n_valid < io->n_sectors) {
if (!skip_triple_sync_mark(io)) {
continue;
continue;
}

uint16_t crc = receive_crc(io,
&mark, 1,
idam_buf, sizeof(idam_buf),
crc_buf, sizeof(crc_buf),
NULL);
uint16_t crc = receive_crc(io, &mark, 1, idam_buf, sizeof(idam_buf),
crc_buf, sizeof(crc_buf), NULL);

DEBUG_PRINTF("mark=%02x [expecting IDAM=%02x]\n", mark, MFM_IO_IDAM);
DEBUG_PRINTF("idam=%02x %02x %02x %02x\n",
idam_buf[0], idam_buf[1], idam_buf[2], idam_buf[3]);
DEBUG_PRINTF("crc_buf=%02x %02x\n",
crc_buf[0], crc_buf[1]);
DEBUG_PRINTF("idam=%02x %02x %02x %02x\n", idam_buf[0], idam_buf[1],
idam_buf[2], idam_buf[3]);
DEBUG_PRINTF("crc_buf=%02x %02x\n", crc_buf[0], crc_buf[1]);
DEBUG_PRINTF("crc=%04x [expecting 0]\n", crc);
if (mark != MFM_IO_IDAM) {
continue;
Expand All @@ -288,25 +285,22 @@ static size_t decode_track_mfm(mfm_io_t *io) {
}

if (!skip_triple_sync_mark(io)) {
continue;
continue;
}
crc = receive_crc(io,
&mark, 1,
io->sectors + mfm_io_block_size * r, mfm_io_block_size,
crc_buf, sizeof(crc_buf),
NULL);
crc = receive_crc(io, &mark, 1, io->sectors + mfm_io_block_size * r,
mfm_io_block_size, crc_buf, sizeof(crc_buf), NULL);
DEBUG_PRINTF("mark=%02x [expecting DAM=%02x]\n", mark, MFM_IO_DAM);
DEBUG_PRINTF("crc_buf=%02x %02x\n",
crc_buf[0], crc_buf[1]);
DEBUG_PRINTF("crc_buf=%02x %02x\n", crc_buf[0], crc_buf[1]);
DEBUG_PRINTF("crc=%04x [expecting 0]\n", crc);
if (mark != MFM_IO_DAM) {
continue;
}
if (crc != 0) {
continue;
continue;
}

if (io->cylinder_ptr) *io->cylinder_ptr = idam_buf[0];
if (io->cylinder_ptr)
*io->cylinder_ptr = idam_buf[0];
io->sector_validity[r] = 1;
io->n_valid++;
}
Expand Down Expand Up @@ -444,7 +438,7 @@ static void encode_track_mfm(mfm_io_t *io) {
// sector_validity might end up reused for interleave?
// memset(io->sector_validity, 0, io->n_sectors);

unsigned char buf[mfm_io_idam_size+1];
unsigned char buf[mfm_io_idam_size + 1];

mfm_io_encode_iam(io);

Expand Down

0 comments on commit ce83b33

Please sign in to comment.