Skip to contentSkip to conversation

hw/m68k: add working floppy controller for q800

PATCH Superseded Matt Jacobson 4 participants 18 messages Jun 17 – Jun 28 Arm/Amlogic Meson SoC support

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover hw/m68k: add working floppy controller for q800

Implement the SWIM2 floppy controller and SuperDrive for the q800 machine.

Reading and writing work, and booting a system disk works. Both GCR and MFM paths work.
Tested with the Quadra 800 ROM running System 7 and A/UX 3.x. Whole-disk formatting/duplication does not yet work.

Example usage:

$ qemu-system-m68k \
   -machine q800 \
   -m 128 \
   -bios "Quadra800.ROM" \
   -nodefaults \
   -drive if=mtd,file="pram.img",format=raw \
   -drive if=floppy \
   -drive if=floppy \
   -drive if=scsi,media=disk,file="harddisk.img",format=raw \
   -drive if=scsi,media=cdrom \
   -nic user,model=dp83932 \
   -audio none \
   -display cocoa \
   -name "Macintosh Quadra 800"

(To populate the floppy drive at boot, use
-drive if=floppy,file="floppy.img",format=raw. However, note that the Macintosh ROM will eject non-bootable floppies on boot.)

The Sony drive emulator takes raw images and will generate the GCR/MFM encoding on the fly. Here are a couple sample images (HFS) to play with:

<https://mjacobson.net/files/mac-disks/floppy-800.img>
<https://mjacobson.net/files/mac-disks/floppy-1440.img>

Matt Jacobson (5):
hw/block: add Sony/Apple "SuperDrive" diskette drive
fifo32: add fifo32_peek()
hw/block: add SWIM2 floppy controller for q800 hw/m68k: add working floppy controller for q800 hw/block: remove unused SWIM1 floppy controller stub

 MAINTAINERS                        |   2 -
 hw/block/meson.build               |   2 +-
 hw/block/sony_superdrive.c         | 994 +++++++++++++++++++++++++++++
 hw/block/swim.c                    | 577 -----------------
 hw/block/swim2.c                   | 722 +++++++++++++++++++++
 hw/block/trace-events              |  25 +-
 hw/m68k/q800.c                     |   5 +-
 hw/misc/mac_via.c                  |  16 +
 include/hw/block/sony_superdrive.h |  56 ++
 include/hw/block/swim.h            |  72 ---
 include/hw/block/swim2.h           |  60 ++
 include/hw/m68k/q800.h             |   4 +-
 include/hw/misc/mac_via.h          |   4 +
 include/qemu/fifo32.h              |  25 +
 14 files changed, 1901 insertions(+), 663 deletions(-)
 create mode 100644 hw/block/sony_superdrive.c
 delete mode 100644 hw/block/swim.c
 create mode 100644 hw/block/swim2.c
 create mode 100644 include/hw/block/sony_superdrive.h
 delete mode 100644 include/hw/block/swim.h
 create mode 100644 include/hw/block/swim2.h
Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/5 hw/block: add Sony/Apple "SuperDrive" diskette drive

The Mac Quadra 800 contains a "SuperDrive" floppy drive that supports both GCR (400 kB and 800 kB) and MFM (1440 kB) diskettes. Implement an emulated version, to be controlled by a forthcoming SWIM2 device.

---
 hw/block/meson.build               |   2 +-
 hw/block/sony_superdrive.c         | 994 +++++++++++++++++++++++++++++
 hw/block/trace-events              |  10 +
 include/hw/block/sony_superdrive.h |  56 ++
 4 files changed, 1061 insertions(+), 1 deletion(-)
 create mode 100644 hw/block/sony_superdrive.c
 create mode 100644 include/hw/block/sony_superdrive.h
hw/block/meson.build +1 -1
index d646323b82..c4b873a906 100644
@@ -10,7 +10,7 @@ system_ss.add(when: 'CONFIG_PFLASH_CFI01', if_true: files('pflash_cfi01.c'))
 system_ss.add(when: 'CONFIG_PFLASH_CFI02', if_true: files('pflash_cfi02.c'))
 system_ss.add(when: 'CONFIG_SSI_M25P80', if_true: files('m25p80.c'))
 system_ss.add(when: 'CONFIG_SSI_M25P80', if_true: files('m25p80_sfdp.c'))
-system_ss.add(when: 'CONFIG_SWIM', if_true: files('swim.c'))
+system_ss.add(when: 'CONFIG_SWIM', if_true: files('swim.c', 'sony_superdrive.c'))
 system_ss.add(when: 'CONFIG_XEN_BUS', if_true: files('xen-block.c'))
 
 system_ss.add(when: 'CONFIG_VIRTIO_BLK', if_true: files('virtio-blk.c'))
hw/block/sony_superdrive.c new file +994 -0

994 lines not on this page · raw message

hw/block/trace-events +10 -0
index cc9a9f2460..c9eb62bcaa 100644
@@ -101,3 +101,13 @@ swim_iwmctrl_read(int reg, const char *name, unsigned size, uint64_t value) "reg
 swim_iwmctrl_write(int reg, const char *name, unsigned size, uint64_t value) "reg=%d [%s] size=%u value=0x%"PRIx64
 swim_switch_to_ism(void) "switch from IWM to ISM mode"
 swim_switch_to_iwm(void) "switch from ISM to IWM mode"
+
+# sony_superdrive.c
+sony_drive_prepare(void *drive, uint8_t track, uint8_t head, uint8_t sector, bool gcr, uint16_t payload_len) "drive=%p track=%"PRIu8" head=%"PRIu8" sector=%"PRIu8" gcr=%d length=%"PRIu16
+sony_drive_persist(void *drive, uint64_t lba) "drive=%p lba=0x%"PRIx64
+sony_drive_strobe(void *drive, uint8_t command, const char *name) "drive=%p cmd=0x%02"PRIx8" [%s]"
+sony_drive_read_sense(void *drive, uint8_t reg, const char *name, bool value) "drive=%p reg=0x%02"PRIx8" [%s] value=%d"
+sony_drive_set_inputs(void *drive, uint8_t phases, bool sel, bool enabled) "drive=%p phases=0x%"PRIx8" sel=%d enabled=%d"
+sony_drive_read_byte(void *drive, uint8_t value, bool is_mark, uint64_t lba, uint16_t position) "drive=%p value=0x%02"PRIx8", is_mark=%d, lba=0x%"PRIx64", position=0x%"PRIx16
+sony_drive_write_byte(void *drive, uint8_t value, uint64_t lba, uint16_t position) "drive=%p value=0x%02"PRIx8", lba=0x%"PRIx64", position=0x%"PRIx16
+sony_drive_step(void *drive, uint8_t seek_direction, uint8_t current_track) "drive=%p direction=%"PRId8", track=%"PRId8
include/hw/block/sony_superdrive.h new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v2 1/5] hw/block: add Sony/Apple "SuperDrive" diskette drive

Quoted 3 lines · Matt Jacobson

The Mac Quadra 800 contains a "SuperDrive" floppy drive that supports both GCR (400 kB and 800 kB) and MFM (1440 kB) diskettes. Implement an emulated version, to be controlled by a forthcoming SWIM2 device.

A bit more explanation could be useful here. In the cover letter it's mentioned that GCR/MFM encoding is generated by this emulation. Maybe that should be mentioned here. But I don't see what it's needed for. I thought maybe for single bit reads but those are not emulated. So is encoded data needed at all or could be added later when emulating single bit reads making this a simpler patch?

My original question that lead to to above was if the asserts in GCR encoding and elsewhere could be caused by the user supplying invalid data (i.e. bad disk image file) and thus crashing QEMU but I think if the disk images contain decoded data not raw GCR/MFM encoded images then these asserts would only be caused by errors in encoding functions in this file but I'm not completely sure.

Regards,
BALATON Zoltan

Quoted 7 lines

---

hw/block/meson.build               |   2 +-
hw/block/sony_superdrive.c         | 994 +++++++++++++++++++++++++++++
hw/block/trace-events              |  10 +
include/hw/block/sony_superdrive.h |  56 ++
4 files changed, 1061 insertions(+), 1 deletion(-)
#3 lore raw

Re: [PATCH v2 1/5] hw/block: add Sony/Apple "SuperDrive" diskette drive

Hi Zoltan,

Many thanks for taking the time to review.

Quoted 8 lines · BALATON Zoltan
Quoted 3 lines · Matt Jacobson

The Mac Quadra 800 contains a "SuperDrive" floppy drive that supports both GCR (400 kB and 800 kB) and MFM (1440 kB) diskettes. Implement an emulated version, to be controlled by a forthcoming SWIM2 device.

A bit more explanation could be useful here. In the cover letter it's mentioned that GCR/MFM encoding is generated by this emulation. Maybe that should be mentioned here. But I don't see what it's needed for. I thought maybe for single bit reads but those are not emulated. So is encoded data needed at all or could be added later when emulating single bit reads making this a simpler patch?

Apologies for the misunderstanding.

The GCR-encoded data is filled into xfer_buffer, and from there it's accessed through sony_drive_read_byte(). In fact, the Macintosh does the GCR decoding *in software* (in the ".Sony" driver), so the encoding is definitely required for the I/O to work correctly.

In real hardware, the SWIM2 floppy controller reads the data bit-by-bit via the RDDATA0/RDDATA1 sense bits, but it seemed like overkill to force the emulated controller and emulated drive to deal in individual bits. So that's why I left that unimplemented (though it could be added later if needed).

Quoted 1 line

My original question that lead to to above was if the asserts in GCR encoding and elsewhere could be caused by the user supplying invalid data (i.e. bad disk image file) and thus crashing QEMU but I think if the disk images contain decoded data not raw GCR/MFM encoded images then these asserts would only be caused by errors in encoding functions in this file but I'm not completely sure.

I don't think there are any situations where a bad disk image would cause an assert. The asserts are for internal consistency only.

However, your comment made me realize that there are cases where bad input *from the guest* (on the write path) could cause a crash. That's a bug, and I will amend the patch to defend against that.

Happy to answer any other questions. Thanks again,

Matt

#4 lore raw

Re: [PATCH v2 1/5] hw/block: add Sony/Apple "SuperDrive" diskette drive

Hello Matt,

Quoted 3 lines · Matt Jacobson

Hi Zoltan,

Many thanks for taking the time to review.

Thanks for taking the time to implement this and improve QEMU.

Quoted 7 lines
Quoted 7 lines · BALATON Zoltan
Quoted 3 lines · Matt Jacobson

The Mac Quadra 800 contains a "SuperDrive" floppy drive that supports both GCR (400 kB and 800 kB) and MFM (1440 kB) diskettes. Implement an emulated version, to be controlled by a forthcoming SWIM2 device.

Quoted 5 lines
Quoted 1 line

A bit more explanation could be useful here. In the cover letter it's mentioned that GCR/MFM encoding is generated by this emulation. Maybe that should be mentioned here. But I don't see what it's needed for. I thought maybe for single bit reads but those are not emulated. So is encoded data needed at all or could be added later when emulating single bit reads making this a simpler patch?

Apologies for the misunderstanding.

The GCR-encoded data is filled into xfer_buffer, and from there it's accessed through sony_drive_read_byte(). In fact, the Macintosh does the GCR decoding *in software* (in the ".Sony" driver), so the encoding is definitely required for the I/O to work correctly.

This could be explained in the commit message so those not familiar with it (like me) get the basic theory of operation. That's what I meant by more explanation. It should not need to be too detailed, something like the above paragraph is enough detail just to note the tricky parts that are not obvious from first look at the code.

Quoted 1 line

In real hardware, the SWIM2 floppy controller reads the data bit-by-bit via the RDDATA0/RDDATA1 sense bits, but it seemed like overkill to force the emulated controller and emulated drive to deal in individual bits. So that's why I left that unimplemented (though it could be added later if needed).

I see, yes bit-by-bit is not needed unless something in the guest wants to use that but that can be added later if needed. Generally the approach in QEMU is to emulate things at a level that the guest code needs and parts can skipped that are not used by the guest to keep emulation simple.

Quoted 3 lines
Quoted 1 line

My original question that lead to to above was if the asserts in GCR encoding and elsewhere could be caused by the user supplying invalid data (i.e. bad disk image file) and thus crashing QEMU but I think if the disk images contain decoded data not raw GCR/MFM encoded images then these asserts would only be caused by errors in encoding functions in this file but I'm not completely sure.

I don't think there are any situations where a bad disk image would cause an assert. The asserts are for internal consistency only.

Asserts for internal consistency are OK to catch programming errors. I'm not sure if we now prefer g_assert which gives more detailed log when it fires, we still have a mix of older asserts so it may depend on the maintainer's preference. To me either is fine.

Quoted 3 lines

However, your comment made me realize that there are cases where bad input *from the guest* (on the write path) could cause a crash. That's a bug, and I will amend the patch to defend against that.

Happy to answer any other questions. Thanks again,

I'm not familiar with the rest of the series so maybe somebody else can review those.

Regards,
BALATON Zoltan

#5 lore raw

PATCH v2 2/5 fifo32: add fifo32_peek()

In reply to Matt Jacobson

Add fifo32_peek(), which, like the other Fifo32 functions, ultimately just delegates to the Fifo8 code.

---
 include/qemu/fifo32.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
include/qemu/fifo32.h +25 -0
index 4e9fd1b5ef..7601bcfbfe 100644
@@ -117,6 +117,31 @@ static inline void fifo32_push_all(Fifo32 *fifo, const uint32_t *data,
     }
 }
 
+/**
+ * fifo32_peek:
+ * @fifo: fifo to peek from
+ *
+ * Peek the data word at the current head of the FIFO. Clients are responsible
+ * for checking for emptiness using fifo32_is_empty().
+ *
+ * Returns: The peeked 32 bits data word.
+ */
+
+static inline uint32_t fifo32_peek(Fifo32 *fifo)
+{
+    uint8_t bytes[sizeof(uint32_t)];
+    uint32_t ret = 0;
+    int i;
+
+    fifo8_peek_buf(&fifo->fifo, bytes, sizeof bytes);
+
+    for (i = 0; i < sizeof(uint32_t); i++) {
+        ret |= (uint32_t)bytes[i] << (i * 8);
+    }
+
+    return ret;
+}
+
 /**
  * fifo32_pop:
  * @fifo: fifo to pop from
Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH v2 2/5] fifo32: add fifo32_peek()

Hi Matt,

Quoted 23 lines · Matt Jacobson

Add fifo32_peek(), which, like the other Fifo32 functions, ultimately just delegates to the Fifo8 code.

---
  include/qemu/fifo32.h | 25 +++++++++++++++++++++++++
  1 file changed, 25 insertions(+)
include/qemu/fifo32.h +7 -0
index 4e9fd1b5ef..7601bcfbfe 100644
@@ -117,6 +117,31 @@ static inline void fifo32_push_all(Fifo32 *fifo, const uint32_t *data,
      }
  }
 
+/**
+ * fifo32_peek:
+ * @fifo: fifo to peek from
+ *
+ * Peek the data word at the current head of the FIFO. Clients are responsible
+ * for checking for emptiness using fifo32_is_empty().
+ *

include/qemu/fifo32.h static inline void fifo32_push_all(Fifo32 *fifo, const uint32_t *data, · line 136

Quoted 10 lines
include/qemu/fifo32.h +10 -0
+ * Returns: The peeked 32 bits data word.
+ */
+
+static inline uint32_t fifo32_peek(Fifo32 *fifo)
+{
+    uint8_t bytes[sizeof(uint32_t)];
+    uint32_t ret = 0;
+    int i;
+
+    fifo8_peek_buf(&fifo->fifo, bytes, sizeof bytes);

Could this work?

       fifo8_peek_buf(&fifo->fifo, (uint8_t *)&ret, sizeof(ret));
Quoted 11 lines
include/qemu/fifo32.h +8 -0
+
+    for (i = 0; i < sizeof(uint32_t); i++) {
+        ret |= (uint32_t)bytes[i] << (i * 8);
+    }
+
+    return ret;
+}
+
  /**
   * fifo32_pop:
   * @fifo: fifo to pop from
#7 lore raw

Re: [PATCH v2 2/5] fifo32: add fifo32_peek()

Hi Philippe,

Thanks for reviewing.

Quoted 14 lines · Philippe Mathieu-Daudé

include/qemu/fifo32.h static inline void fifo32_push_all(Fifo32 *fifo, const uint32_t *data, · line 136

Quoted 7 lines · Matt Jacobson
include/qemu/fifo32.h +7 -0
+static inline uint32_t fifo32_peek(Fifo32 *fifo)
+{
+    uint8_t bytes[sizeof(uint32_t)];
+    uint32_t ret = 0;
+    int i;
+
+    fifo8_peek_buf(&fifo->fifo, bytes, sizeof bytes);

Could this work?

Quoted 1 line
      fifo8_peek_buf(&fifo->fifo, (uint8_t *)&ret, sizeof(ret));

I don't think it would, at least not without other matching changes. The existing fifo32_push() and fifo32_pop() routines explicitly store/load the bytes in little-endian order, so a single fifo8_peek_buf would not work on big-endian hosts.

The rest of your comments are well-taken; I'll include them in v3.

Thanks again,

Matt

#8 lore raw

Re: [PATCH v2 2/5] fifo32: add fifo32_peek()

Quoted 18 lines · Matt Jacobson

Hi Philippe,

Thanks for reviewing.

Quoted 14 lines · Philippe Mathieu-Daudé

include/qemu/fifo32.h static inline void fifo32_push_all(Fifo32 *fifo, const uint32_t *data, · line 136

Quoted 7 lines · Matt Jacobson
include/qemu/fifo32.h +7 -0
+static inline uint32_t fifo32_peek(Fifo32 *fifo)
+{
+    uint8_t bytes[sizeof(uint32_t)];
+    uint32_t ret = 0;
+    int i;
+
+    fifo8_peek_buf(&fifo->fifo, bytes, sizeof bytes);

Could this work?

Quoted 3 lines
Quoted 1 line
       fifo8_peek_buf(&fifo->fifo, (uint8_t *)&ret, sizeof(ret));

I don't think it would, at least not without other matching changes. The existing fifo32_push() and fifo32_pop() routines explicitly store/load the bytes in little-endian order, so a single fifo8_peek_buf would not work on big-endian hosts.

I briefly wondered about endianness then forgot. What about:

          return le32_to_cpu(ret);
Quoted 6 lines

The rest of your comments are well-taken; I'll include them in v3.

Thanks again,

Matt

#9 lore raw

Re: [PATCH v2 2/5] fifo32: add fifo32_peek()

Quoted 19 lines · Philippe Mathieu-Daudé
Quoted 18 lines · Matt Jacobson

Hi Philippe,

Thanks for reviewing.

Quoted 14 lines · Philippe Mathieu-Daudé

include/qemu/fifo32.h static inline void fifo32_push_all(Fifo32 *fifo, const uint32_t *data, · line 136

Quoted 7 lines · Matt Jacobson
include/qemu/fifo32.h +7 -0
+static inline uint32_t fifo32_peek(Fifo32 *fifo)
+{
+    uint8_t bytes[sizeof(uint32_t)];
+    uint32_t ret = 0;
+    int i;
+
+    fifo8_peek_buf(&fifo->fifo, bytes, sizeof bytes);

Could this work?

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

       fifo8_peek_buf(&fifo->fifo, (uint8_t *)&ret, sizeof(ret));

I don't think it would, at least not without other matching changes. The existing fifo32_push() and fifo32_pop() routines explicitly store/ load the bytes in little-endian order, so a single fifo8_peek_buf would not work on big-endian hosts.

I briefly wondered about endianness then forgot. What about:

          return le32_to_cpu(ret);

Nevermind, I just noticed Peter review on this patch.

Quoted 9 lines
Quoted 7 lines

The rest of your comments are well-taken; I'll include them in v3.

Thanks again,

Matt

#10 lore raw

Re: [PATCH v2 2/5] fifo32: add fifo32_peek()

In reply to Matt Jacobson
Quoted 24 lines · Matt Jacobson

Add fifo32_peek(), which, like the other Fifo32 functions, ultimately just delegates to the Fifo8 code.

---
 include/qemu/fifo32.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
include/qemu/fifo32.h +7 -0
index 4e9fd1b5ef..7601bcfbfe 100644
@@ -117,6 +117,31 @@ static inline void fifo32_push_all(Fifo32 *fifo, const uint32_t *data,
     }
 }
 
+/**
+ * fifo32_peek:
+ * @fifo: fifo to peek from
+ *
+ * Peek the data word at the current head of the FIFO. Clients are responsible
+ * for checking for emptiness using fifo32_is_empty().
+ *

include/qemu/fifo32.h static inline void fifo32_push_all(Fifo32 *fifo, const uint32_t *data, · line 136

Quoted 10 lines
include/qemu/fifo32.h +10 -0
+ * Returns: The peeked 32 bits data word.
+ */
+
+static inline uint32_t fifo32_peek(Fifo32 *fifo)
+{
+    uint8_t bytes[sizeof(uint32_t)];
+    uint32_t ret = 0;
+    int i;
+
+    fifo8_peek_buf(&fifo->fifo, bytes, sizeof bytes);

fifo8_peek() assert()s that we have enough data in the FIFO to peek it. fifo8_peek_buf() doesn't assert anything, it instead returns you the number of bytes actually read. I think to make fifo32_peek() consistent with fifo8_peek() we should here have

    uint32_t read = fifo8_peek_buf(...);
    assert(read == sizeof(bytes));

This will also avoid code analyzers like Coverity complaining that we failed to check the return value of fifo8_peek_buf().

include/qemu/fifo32.h static inline void fifo32_push_all(Fifo32 *fifo, const uint32_t *data, · line 140

Quoted 4 lines
include/qemu/fifo32.h +4 -0
+
+    for (i = 0; i < sizeof(uint32_t); i++) {
+        ret |= (uint32_t)bytes[i] << (i * 8);
+    }

This bit can be more simply written as

       return ldl_le_p(bytes);

(which says "load a little-endian 32 bit value from this addr").

thanks
-- PMM

#11 lore raw

Re: [PATCH v2 2/5] fifo32: add fifo32_peek()

In reply to Matt Jacobson

include/qemu/fifo32.h static inline void fifo32_push_all(Fifo32 *fifo, const uint32_t *data, · line 136

Quoted 13 lines · Matt Jacobson

Add fifo32_peek(), which, like the other Fifo32 functions, ultimately just delegates to the Fifo8 code.

include/qemu/fifo32.h +7 -0
+static inline uint32_t fifo32_peek(Fifo32 *fifo)
+{
+    uint8_t bytes[sizeof(uint32_t)];
+    uint32_t ret = 0;
+    int i;
+
+    fifo8_peek_buf(&fifo->fifo, bytes, sizeof bytes);

Oh, a trivial style nit since this needs respinning anyway:
we generally prefer[*] "sizeof(foo)" over "sizeof foo", which also matches the other two uses in the file.

include/qemu/fifo32.h static inline void fifo32_push_all(Fifo32 *fifo, const uint32_t *data, · line 140

Quoted 4 lines
include/qemu/fifo32.h +4 -0
+
+    for (i = 0; i < sizeof(uint32_t); i++) {
+        ret |= (uint32_t)bytes[i] << (i * 8);
+    }

[*] empirically:

$ git grep 'sizeof ' |wc -l
295
$ git grep 'sizeof(' |wc -l
13591

thanks
-- PMM

#12 lore raw

Re: [PATCH v2 2/5] fifo32: add fifo32_peek()

Hi Peter,

Quoted 20 lines · Peter Maydell

fifo8_peek() assert()s that we have enough data in the FIFO to peek it. fifo8_peek_buf() doesn't assert anything, it instead returns you the number of bytes actually read. I think to make fifo32_peek() consistent with fifo8_peek() we should here have

   uint32_t read = fifo8_peek_buf(...);
   assert(read == sizeof(bytes));

This will also avoid code analyzers like Coverity complaining that we failed to check the return value of fifo8_peek_buf().

<...>

This bit can be more simply written as

      return ldl_le_p(bytes);
Quoted 7 lines

(which says "load a little-endian 32 bit value from this addr").

On Jun 23, 2026, at 4:54 AM, Peter Maydell <[email protected]> wrote:

Oh, a trivial style nit since this needs respinning anyway:
we generally prefer[*] "sizeof(foo)" over "sizeof foo", which also matches the other two uses in the file.

Agreed on all points. I'll include these fixes in v3.

Thanks for the review.

Matt

#13 lore raw

PATCH v2 3/5 hw/block: add SWIM2 floppy controller for q800

In reply to Matt Jacobson

The Quadra 800 contains a SWIM2 floppy controller, which is a simplified revision of the original SWIM controller. Implement it based on publicly available specs; a future commit will connect it to q800.

---
 hw/block/meson.build     |   2 +-
 hw/block/swim2.c         | 722 +++++++++++++++++++++++++++++++++++++++
 hw/block/trace-events    |   9 +
 include/hw/block/swim2.h |  60 ++++
 4 files changed, 792 insertions(+), 1 deletion(-)
 create mode 100644 hw/block/swim2.c
 create mode 100644 include/hw/block/swim2.h
hw/block/meson.build +1 -1
index c4b873a906..733e9ffb47 100644
@@ -10,7 +10,7 @@ system_ss.add(when: 'CONFIG_PFLASH_CFI01', if_true: files('pflash_cfi01.c'))
 system_ss.add(when: 'CONFIG_PFLASH_CFI02', if_true: files('pflash_cfi02.c'))
 system_ss.add(when: 'CONFIG_SSI_M25P80', if_true: files('m25p80.c'))
 system_ss.add(when: 'CONFIG_SSI_M25P80', if_true: files('m25p80_sfdp.c'))
-system_ss.add(when: 'CONFIG_SWIM', if_true: files('swim.c', 'sony_superdrive.c'))
+system_ss.add(when: 'CONFIG_SWIM', if_true: files('swim.c', 'swim2.c', 'sony_superdrive.c'))
 system_ss.add(when: 'CONFIG_XEN_BUS', if_true: files('xen-block.c'))
 
 system_ss.add(when: 'CONFIG_VIRTIO_BLK', if_true: files('virtio-blk.c'))
hw/block/swim2.c new file +722 -0

722 lines not on this page · raw message

hw/block/trace-events +9 -0
index c9eb62bcaa..92d4bd9f17 100644
@@ -102,6 +102,15 @@ swim_iwmctrl_write(int reg, const char *name, unsigned size, uint64_t value) "re
 swim_switch_to_ism(void) "switch from IWM to ISM mode"
 swim_switch_to_iwm(void) "switch from ISM to IWM mode"
 
+# swim2.c
+swim2_mmio_read(uint64_t addr, unsigned size, int reg, const char *name, uint64_t value, uint8_t mode, uint8_t setup, uint8_t phase, uint32_t fifo_count) "addr=0x%"PRIx64" size=%u reg=%d [%s] value=0x%"PRIx64" mode=0x%02x setup=0x%02x phase=0x%02x fifo=%u"
+swim2_mmio_write(uint64_t addr, unsigned size, int reg, const char *name, uint64_t value, uint8_t mode, uint8_t setup, uint8_t phase, uint32_t fifo_count) "addr=0x%"PRIx64" size=%u reg=%d [%s] value=0x%"PRIx64" mode=0x%02x setup=0x%02x phase=0x%02x fifo=%u"
+swim2_fifo_push(uint32_t count, uint8_t data, uint8_t flags) "fifo=%u data=0x%02x flags=0x%02x"
+swim2_fifo_pop(uint32_t count, uint8_t data, uint8_t flags, bool success) "fifo=%u data=0x%02x flags=0x%02x success=%d"
+swim2_fifo_clear(void) "clear FIFO"
+swim2_set_sel(bool sel) "set sel=%d"
+swim2_error_set(uint8_t prev, uint8_t curr, uint8_t new_err, uint8_t mode, bool write_mode, uint32_t fifo_count) "prev=0x%02x curr=0x%02x new=0x%02x mode=0x%02x write=%d fifo=%u"
+
 # sony_superdrive.c
 sony_drive_prepare(void *drive, uint8_t track, uint8_t head, uint8_t sector, bool gcr, uint16_t payload_len) "drive=%p track=%"PRIu8" head=%"PRIu8" sector=%"PRIu8" gcr=%d length=%"PRIu16
 sony_drive_persist(void *drive, uint64_t lba) "drive=%p lba=0x%"PRIx64
include/hw/block/swim2.h new file +60 -0

60 lines not on this page · raw message

Signature · 1 line
2.53.0
#14 lore raw

Re: [PATCH v2 3/5] hw/block: add SWIM2 floppy controller for q800

Quoted 23 lines · Matt Jacobson

The Quadra 800 contains a SWIM2 floppy controller, which is a simplified revision of the original SWIM controller. Implement it based on publicly available specs; a future commit will connect it to q800.

---
  hw/block/meson.build     |   2 +-
  hw/block/swim2.c         | 722 +++++++++++++++++++++++++++++++++++++++
  hw/block/trace-events    |   9 +
  include/hw/block/swim2.h |  60 ++++
  4 files changed, 792 insertions(+), 1 deletion(-)

create mode 100644 hw/block/swim2.c
create mode 100644 include/hw/block/swim2.h

hw/block/swim2.c new file +2 -0
new file mode 100644
index 0000000000..f10be40678
@@ -0,0 +1,722 @@
+/*
+ * QEMU Macintosh floppy disk controller emulator (SWIM2)

hw/block/swim2.c line 12

Quoted 10 lines
hw/block/swim2.c new file +10 -0
+ *
+ * Copyright (c) 2025 Matt Jacobson <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include <assert.h>
+#include <stdbool.h>
+#include <stdint.h>

These 3 system headers are already provided by "qemu/osdep.h".

Quoted 31 lines
hw/block/swim2.c new file +29 -0
+#include "qemu/log.h"
+#include "qapi/error.h"
+#include "hw/core/qdev-properties.h"
+#include "hw/core/qdev.h"
+#include "hw/core/sysbus.h"
+#include "hw/block/sony_superdrive.h"
+#include "hw/block/swim2.h"
+#include "system/block-backend.h"
+#include "system/blockdev.h"
+#include "trace.h"
 
 
+static const TypeInfo swim2_info = {
+    .name = TYPE_SWIM2,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(SWIM2State),
+    .instance_init = swim2_init,
+    .instance_finalize = swim2_finalize,
+    .class_init = swim2_class_init,
+};
+
+static const TypeInfo swim2_bus_info = {
+    .name = TYPE_SWIM2_BUS,
+    .parent = TYPE_BUS,
+    .instance_size = 0,
+    .class_init = swim2_bus_class_init,
+};
+
+static const TypeInfo swim2_drive_info = {
+    .name = TYPE_SWIM2_DRIVE,
+    .parent = TYPE_DEVICE,

hw/block/swim2.c

Quoted 10 lines
hw/block/swim2.c new file +10 -0
+    .instance_size = sizeof(SWIM2Drive),
+    .class_init = swim2_drive_class_init,
+};
+
+static void swim2_register_types(void)
+{
+    type_register_static(&swim2_info);
+    type_register_static(&swim2_bus_info);
+    type_register_static(&swim2_drive_info);
+}

Preferably DEFINE_TYPES().

Quoted 13 lines
hw/block/swim2.c new file +2 -0
+
+type_init(swim2_register_types)
include/hw/block/swim2.h new file +3 -0
new file mode 100644
index 0000000000..72be21ea4e
@@ -0,0 +1,60 @@
+/*
+ * QEMU Macintosh floppy disk controller emulator (SWIM2)
+ *

include/hw/block/swim2.h line 13

Quoted 10 lines
include/hw/block/swim2.h new file +10 -0
+ * Copyright (c) 2025 Matt Jacobson <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_BLOCK_SWIM2_H
+#define HW_BLOCK_SWIM2_H
+
+#include <stdbool.h>
+#include <stdint.h>

No need to include these system headers.

Quoted 47 lines
include/hw/block/swim2.h new file +47 -0
+#include "hw/core/qdev.h"
+#include "hw/core/sysbus.h"
+#include "hw/block/block.h"
+#include "hw/block/sony_superdrive.h"
+#include "qemu/fifo32.h"
+#include "qom/object.h"
+
+#define TYPE_SWIM2 "swim2"
+#define TYPE_SWIM2_BUS "swim2-bus"
+#define TYPE_SWIM2_DRIVE "swim2-drive"
+
+OBJECT_DECLARE_SIMPLE_TYPE(SWIM2State, SWIM2)
+OBJECT_DECLARE_SIMPLE_TYPE(SWIM2Drive, SWIM2_DRIVE)
+
+#define SWIM2_MAX_FD 2
+
+struct SWIM2Drive {
+    DeviceState parent_obj;
+
+    BlockConf conf;
+    SonyDrive sony;
+    int32_t unit;
+};
+
+struct SWIM2State {
+    SysBusDevice parent_obj;
+
+    MemoryRegion mmio;
+    BusState *bus;
+
+    SWIM2Drive *drives[SWIM2_MAX_FD];
+
+    uint8_t parameter_data[4];
+    uint8_t parameter_index;
+    uint8_t phase_reg;
+    uint8_t setup_reg;
+    uint8_t mode_reg;
+    uint8_t error_reg;
+    bool did_handshake;
+    bool wait_for_mark;
+
+    Fifo32 fifo;
+};
+
+void swim2_set_sel(SWIM2State *ctrl, bool sel);
+
+#endif /* HW_BLOCK_SWIM2_H */
#15 lore raw

PATCH v2 4/5 hw/m68k: add working floppy controller for q800

In reply to Matt Jacobson

Switch the Quadra 800 machine to the working SWIM2 floppy controller. Hook up the HeadSel bit on the VIA to control the head selection (the IWM does not handle this part itself).

---
 hw/m68k/q800.c            |  5 +++--
 hw/misc/mac_via.c         | 16 ++++++++++++++++
 include/hw/m68k/q800.h    |  4 ++--
 include/hw/misc/mac_via.h |  4 ++++
 4 files changed, 25 insertions(+), 4 deletions(-)
hw/m68k/q800.c +3 -2
index ab64250c47..a6997ef802 100644
@@ -47,7 +47,7 @@
 #include "hw/audio/asc.h"
 #include "hw/nubus/mac-nubus-bridge.h"
 #include "hw/display/macfb.h"
-#include "hw/block/swim.h"
+#include "hw/block/swim2.h"
 #include "net/net.h"
 #include "net/util.h"
 #include "qapi/error.h"
@@ -338,6 +338,7 @@ static void q800_machine_init(MachineState *machine)
     /* VIA 1 */
     object_initialize_child(OBJECT(machine), "via1", &m->via1,
                             TYPE_MOS6522_Q800_VIA1);
+    m->via1.swim = &m->swim;
     dinfo = drive_get(IF_MTD, 0, 0);
     if (dinfo) {
         qdev_prop_set_drive(DEVICE(&m->via1), "drive",
@@ -511,7 +512,7 @@ static void q800_machine_init(MachineState *machine)
     /* SWIM floppy controller */
 
     object_initialize_child(OBJECT(machine), "swim", &m->swim,
-                            TYPE_SWIM);
+                            TYPE_SWIM2);
     sysbus = SYS_BUS_DEVICE(&m->swim);
     sysbus_realize(sysbus, &error_fatal);
     memory_region_add_subregion(&m->macio, SWIM_BASE - IO_BASE,
hw/misc/mac_via.c +16 -0
index 26ee230dcc..09197fe519 100644
@@ -31,6 +31,7 @@
 #include "hw/core/qdev-properties-system.h"
 #include "system/block-backend.h"
 #include "system/rtc.h"
+#include "hw/block/swim2.h"
 #include "trace.h"
 #include "qemu/log.h"
 
@@ -1080,6 +1081,17 @@ static void mos6522_q800_via1_write(void *opaque, hwaddr addr, uint64_t val,
     mos6522_write(ms, addr, val, size);
 
     switch (addr) {
+    case VIA_REG_A:
+    case VIA_REG_ANH: {
+        const bool sel = (ms->a & VIA1A_vHeadSel) != 0;
+
+        if (v1s->swim) {
+            swim2_set_sel(v1s->swim, sel);
+        }
+
+        break;
+    }
+
     case VIA_REG_B:
         via1_rtc_update(v1s);
         via1_adb_update(v1s);
@@ -1224,6 +1236,10 @@ static void mos6522_q800_via1_reset_hold(Object *obj, ResetType type)
 
     /* Timer calibration hack */
     v1s->timer_hack_state = 0;
+
+    if (v1s->swim) {
+        swim2_set_sel(v1s->swim, false);
+    }
 }
 
 static void mos6522_q800_via1_realize(DeviceState *dev, Error **errp)
include/hw/m68k/q800.h +2 -2
index 77551b9592..2a20314d57 100644
@@ -33,7 +33,7 @@
 #include "hw/char/escc.h"
 #include "hw/core/or-irq.h"
 #include "hw/scsi/esp.h"
-#include "hw/block/swim.h"
+#include "hw/block/swim2.h"
 #include "hw/nubus/mac-nubus-bridge.h"
 #include "hw/display/macfb.h"
 #include "hw/misc/djmemc.h"
@@ -59,7 +59,7 @@ struct Q800MachineState {
     ESCCState escc;
     OrIRQState escc_orgate;
     SysBusESPState esp;
-    Swim swim;
+    SWIM2State swim;
     MacNubusBridge mac_nubus_bridge;
     MacfbNubusState macfb;
     DJMEMCState djmemc;
include/hw/misc/mac_via.h +4 -0
index 114f41db4c..a25f219a25 100644
@@ -35,6 +35,8 @@
 #define TYPE_MOS6522_Q800_VIA1 "mos6522-q800-via1"
 OBJECT_DECLARE_SIMPLE_TYPE(MOS6522Q800VIA1State, MOS6522_Q800_VIA1)
 
+struct SWIM2State;
+
 struct MOS6522Q800VIA1State {
     /*< private >*/
     MOS6522State parent_obj;
@@ -77,6 +79,8 @@ struct MOS6522Q800VIA1State {
 
     /* SETUPTIMEK hack */
     int timer_hack_state;
+
+    struct SWIM2State *swim;
 };
 
 
Signature · 1 line
2.53.0
#16 lore raw
Quoted 18 lines · Matt Jacobson

Switch the Quadra 800 machine to the working SWIM2 floppy controller. Hook up the HeadSel bit on the VIA to control the head selection (the IWM does not handle this part itself).

---
  hw/m68k/q800.c            |  5 +++--
  hw/misc/mac_via.c         | 16 ++++++++++++++++
  include/hw/m68k/q800.h    |  4 ++--
  include/hw/misc/mac_via.h |  4 ++++
  4 files changed, 25 insertions(+), 4 deletions(-)
include/hw/misc/mac_via.h +0 -0
index 114f41db4c..a25f219a25 100644

include/hw/misc/mac_via.h line 38

Quoted 4 lines
include/hw/misc/mac_via.h +1 -0
@@ -35,6 +35,8 @@
  #define TYPE_MOS6522_Q800_VIA1 "mos6522-q800-via1"
  OBJECT_DECLARE_SIMPLE_TYPE(MOS6522Q800VIA1State, MOS6522_Q800_VIA1)
 
+struct SWIM2State;

Why not include "hw/block/swim2.h"?

include/hw/misc/mac_via.h struct MOS6522Q800VIA1State { · line 83

Quoted 9 lines
include/hw/misc/mac_via.h +2 -0
  struct MOS6522Q800VIA1State {
      /*< private >*/
      MOS6522State parent_obj;
@@ -77,6 +79,8 @@ struct MOS6522Q800VIA1State {
 
      /* SETUPTIMEK hack */
      int timer_hack_state;
+
+    struct SWIM2State *swim;

QEMU coding style is:

       SWIM2State *swim;
Quoted 3 lines
include/hw/misc/mac_via.h +0 -0
  };
 
 
#17 lore raw

PATCH v2 5/5 hw/block: remove unused SWIM1 floppy controller stub

In reply to Matt Jacobson

This was formerly used in the q800 machine; it's been replaced by a functional SWIM2.

---
 MAINTAINERS             |   2 -
 hw/block/meson.build    |   2 +-
 hw/block/swim.c         | 577 ----------------------------------------
 hw/block/trace-events   |   8 -
 include/hw/block/swim.h |  72 -----
 5 files changed, 1 insertion(+), 660 deletions(-)
 delete mode 100644 hw/block/swim.c
 delete mode 100644 include/hw/block/swim.h
MAINTAINERS +0 -2
index 2b5b581e17..e24c7734d8 100644
@@ -1394,7 +1394,6 @@ F: hw/m68k/q800-glue.c
 F: hw/misc/mac_via.c
 F: hw/nubus/*
 F: hw/display/macfb.c
-F: hw/block/swim.c
 F: hw/misc/djmemc.c
 F: hw/misc/iosb.c
 F: hw/audio/asc.c
@@ -1404,7 +1403,6 @@ F: include/standard-headers/asm-m68k/bootinfo-mac.h
 F: include/hw/misc/mac_via.h
 F: include/hw/nubus/*
 F: include/hw/display/macfb.h
-F: include/hw/block/swim.h
 F: include/hw/m68k/q800.h
 F: include/hw/m68k/q800-glue.h
 F: include/hw/misc/djmemc.h
hw/block/meson.build +1 -1
index 733e9ffb47..2a9a0b9b13 100644
@@ -10,7 +10,7 @@ system_ss.add(when: 'CONFIG_PFLASH_CFI01', if_true: files('pflash_cfi01.c'))
 system_ss.add(when: 'CONFIG_PFLASH_CFI02', if_true: files('pflash_cfi02.c'))
 system_ss.add(when: 'CONFIG_SSI_M25P80', if_true: files('m25p80.c'))
 system_ss.add(when: 'CONFIG_SSI_M25P80', if_true: files('m25p80_sfdp.c'))
-system_ss.add(when: 'CONFIG_SWIM', if_true: files('swim.c', 'swim2.c', 'sony_superdrive.c'))
+system_ss.add(when: 'CONFIG_SWIM', if_true: files('swim2.c', 'sony_superdrive.c'))
 system_ss.add(when: 'CONFIG_XEN_BUS', if_true: files('xen-block.c'))
 
 system_ss.add(when: 'CONFIG_VIRTIO_BLK', if_true: files('virtio-blk.c'))
hw/block/swim.c deleted +0 -577

577 lines not on this page · raw message

hw/block/trace-events +0 -8
index 92d4bd9f17..be3915f201 100644
@@ -94,14 +94,6 @@ m25p80_read_sfdp(void *s, uint32_t addr, uint8_t v) "[%p] Read SFDP 0x%"PRIx32"=
 m25p80_binding(void *s) "[%p] Binding to IF_MTD drive"
 m25p80_binding_no_bdrv(void *s) "[%p] No BDRV - binding to RAM"
 
-# swim.c
-swim_ismctrl_read(int reg, const char *name, unsigned size, uint64_t value) "reg=%d [%s] size=%u value=0x%"PRIx64
-swim_ismctrl_write(int reg, const char *name, unsigned size, uint64_t value) "reg=%d [%s] size=%u value=0x%"PRIx64
-swim_iwmctrl_read(int reg, const char *name, unsigned size, uint64_t value) "reg=%d [%s] size=%u value=0x%"PRIx64
-swim_iwmctrl_write(int reg, const char *name, unsigned size, uint64_t value) "reg=%d [%s] size=%u value=0x%"PRIx64
-swim_switch_to_ism(void) "switch from IWM to ISM mode"
-swim_switch_to_iwm(void) "switch from ISM to IWM mode"
-
 # swim2.c
 swim2_mmio_read(uint64_t addr, unsigned size, int reg, const char *name, uint64_t value, uint8_t mode, uint8_t setup, uint8_t phase, uint32_t fifo_count) "addr=0x%"PRIx64" size=%u reg=%d [%s] value=0x%"PRIx64" mode=0x%02x setup=0x%02x phase=0x%02x fifo=%u"
 swim2_mmio_write(uint64_t addr, unsigned size, int reg, const char *name, uint64_t value, uint8_t mode, uint8_t setup, uint8_t phase, uint32_t fifo_count) "addr=0x%"PRIx64" size=%u reg=%d [%s] value=0x%"PRIx64" mode=0x%02x setup=0x%02x phase=0x%02x fifo=%u"
include/hw/block/swim.h deleted +0 -72

72 lines not on this page · raw message

Signature · 1 line
2.53.0