From 271e0fd67582633b00b8fd0377fed43b2031f56f Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Sat, 28 Nov 2026 10:47:24 -0800 Subject: [PATCH 1/2] amiga: keep the 68000 builds off unaligned long accesses The 68000 builds stop with an address error (80000003) on a 68000 as soon as they list an ISO 9660 directory or mount an audio CD, as carlosbragatto found and analysed in #61. memcpy only asks exec whether the CPU allows unaligned long moves when __mc68020__ is not defined, and iso_read_le32() loads both-byte-order fields with one move.l when it is. GCC's __mc680x0__ macros follow -mtune, though, not the -m option: the 68000 builds use -m68000 -mtune=68020-60 and get __mc68020__ to __mc68060__ as well. Their predefined macros are the same as those of -m68020 -mtune=68020-60, so no compiler macro can tell the two apart. So the 68000 and ROM builds used long moves for every misaligned copy of more than 16 bytes, and iso_read_le32() read fields with move.l where a Rock Ridge entry can put them at an odd address: SUSP entries are not padded, so the PX entry after mkisofs' five-byte RR entry starts at an odd offset in every ".." record. Let the Makefile say it instead: the 68020 build defines ODFS_M68K_UNALIGNED_OK, and both places test that. A build without it takes the safe path, so a new variant that forgets it gets slower code, not an address error. The 68020 build stays byte for byte the same. The 68000 build grows by 592 bytes and the ROM build by 48: iso_read_le32() reads bytes now. --- Makefile | 6 +++++- backends/iso9660/iso9660.h | 7 ++++--- platform/amiga/os3/string.S | 6 ++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index f80b067..cf9aaac 100644 --- a/Makefile +++ b/Makefile @@ -208,7 +208,11 @@ AMIGA_020_BUILD = build/amiga-020 # 68020 release variant: hardware mul/div and 020 addressing modes make # the handler both faster and smaller than the 68000 build. -AMIGA_020_CPUFLAGS ?= -m68020 -msoft-float +# ODFS_M68K_UNALIGNED_OK lets the code use word and long accesses at odd +# addresses. GCC cannot tell us: its __mc680x0__ macros follow -mtune, +# not -m, so the 68000 build (-m68000 -mtune=68020-60) defines +# __mc68020__ too. Leave it out of any build that may run on a 68000. +AMIGA_020_CPUFLAGS ?= -m68020 -msoft-float -DODFS_M68K_UNALIGNED_OK # ---- shared source lists ---- diff --git a/backends/iso9660/iso9660.h b/backends/iso9660/iso9660.h index a8ee9bf..6006815 100644 --- a/backends/iso9660/iso9660.h +++ b/backends/iso9660/iso9660.h @@ -32,10 +32,11 @@ /* read little-endian 32 from a both-byte-order 8-byte field */ static inline uint32_t iso_read_le32(const uint8_t *p) { -#if defined(__mc68020__) || defined(__mc68030__) || \ - defined(__mc68040__) || defined(__mc68060__) +#if defined(ODFS_M68K_UNALIGNED_OK) /* GCC keeps to aligned accesses on every m68k and would assemble - * the value byte by byte; the 68020 and later load it unaligned */ + * the value byte by byte; the 68020 and later load it unaligned. + * Rock Ridge fields can sit at odd addresses, so only a build that + * never runs on a 68000 may do this (see the Makefile). */ uint32_t v; __asm__("move.l %1,%0" : "=d" (v) : "m" (*(const uint8_t (*)[4])p)); diff --git a/platform/amiga/os3/string.S b/platform/amiga/os3/string.S index d137692..cb1fc52 100644 --- a/platform/amiga/os3/string.S +++ b/platform/amiga/os3/string.S @@ -77,9 +77,11 @@ _memcpy: 1: move.l 4(sp),d0 rts -/* Source and destination differ in alignment; n > 16. */ +/* Source and destination differ in alignment; n > 16. A build that + * may run on a 68000 asks exec whether the CPU allows unaligned long + * moves; see ODFS_M68K_UNALIGNED_OK in the Makefile. */ .Lunaligned: -#ifndef __mc68020__ +#ifndef ODFS_M68K_UNALIGNED_OK move.l 4.w,d1 exg d1,a0 btst #AFB_68020,AttnFlags+1(a0) From a0b8d9aec94b255da8e1a069a65368a4ef97afbf Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Sat, 28 Nov 2026 13:54:29 -0800 Subject: [PATCH 2/2] amiga: tune the 68000 builds for the 68000 The 68000 disk and ROM builds were tuned for the 68020 to 68060 (-mtune=68020-60). That dates from when the 68000 build was the only one. The 68020 build serves those CPUs now, and the tuning costs the 68000: GCC replaces moveq with clr.l and cmpi.l, swap with long shifts, and shift-and-add with mulu, all of which take a 68000 more cycles. Keep the 68020-60 tuning for handler_main.c alone. It holds the packet loop and the streaming read path, and tuned for the 68000 they cost a 68040 one E-clock tick per Read(): GCC keeps the file handle in a data register and moves it to an address register for each access, and builds shifts from swap. That is -0.85% sequential read in CDBench, the same under four different free-memory layouts. GCC has no per-function tuning on m68k, so the Makefile sets it per object. The 68020 build and the LTO-linked ROM profile leave it out; the ROM reads as fast either way. CDBench, 68000 disk build, all-68020-60 -> this: 68040 rig seq read 209126 -> 208998 KiB/s (same tick) deep lock 148 -> 139 us small o+r 318 -> 298 us Discovery 9432 -> 9668 entries/s A600, 68000 deep lock 8485 -> 7714 us open/lock 10549 -> 10120 us small o+r 43913 -> 42800 us and for the ROM build, all-68020-60 -> all-68000: 68040 rig deep lock 619 -> 575 us small o+r 1970 -> 1934 us A600, 68000 deep lock 35856 -> 32921 us small o+r 123633 -> 118545 us The A600 runs use a cycle-exact 68000 with KS 3.2.3 and an ATAPI CD on Gayle IDE; sequential read there is limited by the PIO transfers and stays at 90 KiB/s. The 68000 build shrinks by 384 bytes and the ROM build by 216. The 68020 build stays byte for byte the same. --- Makefile | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index cf9aaac..46921ef 100644 --- a/Makefile +++ b/Makefile @@ -169,7 +169,16 @@ LIBS = -lc -lgcc HANDLER_LDFLAGS = -nostartfiles -static -Wl,-u,_start HANDLER_LIBS = -nostdlib -lgcc else -AMIGA_CPUFLAGS ?= -m68000 -mtune=68020-60 -msoft-float +# Tuned for the 68000 itself: the 68020 build serves the faster CPUs, and +# -mtune=68020-60 picks clr.l, cmpi.l, long shifts and mulu that cost a +# 68000 more. +AMIGA_CPUFLAGS ?= -m68000 -msoft-float +# Except handler_main.c, which holds the packet loop and the streaming +# read path. Tuned for the 68000, they cost a 68040 one E-clock tick per +# Read() (-0.85% sequential read in CDBench); tuned for the 68020-60, +# they cost a 68000 nothing measurable. The 68020 build and the +# LTO-linked ROM profile leave this empty. +AMIGA_LOOP_CPUFLAGS ?= -mtune=68020-60 AMIGA_SYSFLAGS ?= -noixemul # Use register arguments inside the OS3 handler, including the ROM profile. # Keep this on both compilation and linking for LTO. OS entry points use @@ -210,8 +219,8 @@ AMIGA_020_BUILD = build/amiga-020 # the handler both faster and smaller than the 68000 build. # ODFS_M68K_UNALIGNED_OK lets the code use word and long accesses at odd # addresses. GCC cannot tell us: its __mc680x0__ macros follow -mtune, -# not -m, so the 68000 build (-m68000 -mtune=68020-60) defines -# __mc68020__ too. Leave it out of any build that may run on a 68000. +# not -m, so -m68000 -mtune=68020-60 defines __mc68020__ too. Leave it +# out of any build that may run on a 68000. AMIGA_020_CPUFLAGS ?= -m68020 -msoft-float -DODFS_M68K_UNALIGNED_OK # ---- shared source lists ---- @@ -365,6 +374,7 @@ amiga-020: @$(MAKE) --no-print-directory \ AMIGA_BUILD=$(AMIGA_020_BUILD) \ AMIGA_CPUFLAGS="$(AMIGA_020_CPUFLAGS)" \ + AMIGA_LOOP_CPUFLAGS= \ SIZE_LIMIT_DESC="release Amiga 68020 handler" \ amiga @@ -465,6 +475,7 @@ rom: AMIGA_BUILD=$(ROM_BUILD) \ AMIGA_OPT=-Os \ LTO="$(ROM_LTO)" \ + AMIGA_LOOP_CPUFLAGS= \ AMIGA_AR=$(AMIGA_TOOL_PREFIX)-gcc-ar \ CPPFLAGS="$(CPPFLAGS) -DODFS_PROFILE_ROM" \ AMIGA_SIZE_LIMIT=$(ROM_SIZE_LIMIT) \ @@ -483,6 +494,7 @@ rom-test: AMIGA_BUILD=$(ROM_TEST_BUILD) \ AMIGA_OPT=-Os \ LTO="$(ROM_LTO)" \ + AMIGA_LOOP_CPUFLAGS= \ AMIGA_AR=$(AMIGA_TOOL_PREFIX)-gcc-ar \ CPPFLAGS="$(CPPFLAGS) -DODFS_PROFILE_ROM" \ ENFORCE_SIZE_LIMITS=0 \ @@ -616,7 +628,9 @@ $(AMIGA_BUILD)/libodfs.a: $(AMIGA_LIB_OBJS) $(AMIGA_BUILD)/%.o: %.c Makefile @mkdir -p $(@D) @echo " CC $<" - @$(CC) $(CPPFLAGS) $(AMIGA_INCLUDES) $(CFLAGS) $(AMIGA_ABIFLAGS) -c -o $@ $< + @$(CC) $(CPPFLAGS) $(AMIGA_INCLUDES) $(CFLAGS) $(AMIGA_ABIFLAGS) $(OBJ_CPUFLAGS) -c -o $@ $< + +$(AMIGA_BUILD)/platform/amiga/handler_main.o: OBJ_CPUFLAGS = $(AMIGA_LOOP_CPUFLAGS) # ---- Amiga assembly ----