Skip to content

GEMDRIVE: bring in md-drives-emulator's fixes - #29

Merged
diegoparrilla merged 18 commits into
release/v1.2.0from
epic/23-gemdrive-sync
Dec 2, 2026
Merged

diegoparrilla merged 18 commits into
release/v1.2.0from
epic/23-gemdrive-sync

Conversation

@diegoparrilla

Copy link
Copy Markdown
Contributor

md-devops's GEMDRIVE was ported from md-drives-emulator, and the two had drifted. This brings in the 17 GEMDRIVE fixes md-drives-emulator made since, in its order, one commit each. Each commit message names its source (From md-drives-emulator <hash>.). All are hand ports, because md-devops's handlers and dispatch are shaped differently.

What programs on the Atari get

Programs and drives:

  • Pexec is decided by the program's own drive letter, not by the current drive.
  • A directory search leaves the current drive alone.
  • Fforce of a standard handle onto a GEMDRIVE file works, so a shell's > C:\OUT.TXT works.

Program loading:

  • A program started from another program no longer bombs TOS 1.00/1.02 (no mode 6 below GEMDOS $15).
  • p_flags are set from GEMDOS $1700 on.
  • A program gets the memory its header asks for, cleared.
  • A non-program answers EPLFMT.

Errors:

  • A full card gives a short write count.
  • Running out of memory or handles is reported as ENSMEM or ENHNDL.
  • Fattrib set returns the attributes.

Searches:

  • GEMDRIVE answers only the searches it started, using a mark in the DTA.
  • A finished listing leaves the program's buffer alone.
  • A search ends with the process that started it.

Names and machines:

  • Names too long for 8.3 are cut as GEMDOS cuts them.
  • On a 68030, a user-mode call is read where it is, and the instruction cache is cleared after a read into memory. This never runs on a 68000.

Adapted

  • The search mark is written as two 16-bit stores, because its window address is not 4-byte aligned on the RP.
  • The GEMDOS version and the TOS 1.00 PE_GO return address are kept in the relocated GEMDRIVE code.
  • In the debug build, the Fforce handlers and the 8.3 lookup run from flash, to keep RAM within its budget.
  • A source bug is fixed in the Fforce port: md-drives-emulator's .Fclose loses the standard handle in d2. Reported upstream.

Checked in EmuMD

  • FSTESTS (md-drives-emulator's GEMDOS suite, run through the Runner):

    Before After Halts
    TOS 1.04 637 OK / 37 FAIL 658 OK / 22 FAIL none
    TOS 1.02 — 659 OK / 22 FAIL none

    The suite completes on both, and nothing that passed now fails. The 22 left are 20 file-date checks (EmuMD's card keeps no FAT dates) and "open many file handles" (md-devops allows 8, and FSTESTS's log takes one).

  • emu_check.py: GDTEST and HELLODBG pass on ST/STE/Mega STE and on TOS 1.00 and 1.02. --mode g passes with programs run through DEVOPS.ACC.

  • Builds:

    • The cartridge is 10,112 of 10,240 bytes. Free per section: main 228, gemdrive 112, runner 128.
    • release and debug build with no new warnings. The heap is unchanged.

For the device

None of this has run on hardware yet:

  • the 68030 cache clear and the user-mode PE_GO hook;
  • the two-word search mark;
  • nested programs on TOS 1.00/1.02;
  • the time the heap clear adds to program starts (about 3 to 4 seconds per MB at 8 MHz, as in md-drives-emulator).

…claiming success

Fwrite stopped with EIO_WRITE on the first chunk that made no progress,
throwing away what earlier chunks had written, and the RP answered 0 both
for a full card and for a failed write, so the two could not be told
apart. The RP now answers each chunk with the bytes it wrote, fewer (0
included) when the card is full, or a negative GEMDOS code when the
write failed: EIHNDL for a handle it does not know, EINTRN when f_write
fails, as md-drives-emulator does. Only a positive count is progress.
As TOS does, Fwrite then returns the short count when something was
written, otherwise the RP's error code, or 0 for a full card.

Pexec of a file that is not a program reported success. The header check
already caught a short or empty file and a missing $601A magic, but its
exit returned the Fclose status, 0, so the desktop behaved as if the
program had run. It now returns EPLFMT (-66), or the read error when the
header could not be read at all.

From md-drives-emulator 19ddb9c.
When FatFs fails for lack of heap or of lock entries, the failure says
nothing about whether the file or path exists, yet GEMDRIVE reported
"file not found" or "path not found" for Fsfirst and Fopen, "path not
found" for Fcreate, "access denied" for Dcreate when the lock table was
full, and "no more files" halfway through a listing. The desktop then
showed an empty folder or claimed the disk was full. One helper now maps
FR_NOT_ENOUGH_CORE to ENSMEM (-39) and FR_TOO_MANY_OPEN_FILES to ENHNDL
(-35) for Fsfirst, Fsnext, Fopen, Fcreate and Dcreate; any other failure
keeps its previous code. Some of these already mapped one of the two;
they now map both.

From md-drives-emulator bf6bdc5.
A shell redirects a program's output with Fforce(1, handle), and TOS
refuses any handle it did not allocate, so redirecting into a file on
the emulated drive failed. GEMDRIVE now keeps the alias itself: the RP
holds the six standard handles with the basepage that forced each one
and publishes the table in the cartridge window, after the basepage
buffer, so the Atari routes Fread, Fwrite and Fseek on a standard handle
without asking the RP, which keeps ordinary console output as cheap as
before. The alias applies to the forcing program and the programs it
starts, and is dropped when the file is closed, when that program ends
(the Pterm cleanup md-devops already has), when the handle is forced
back to a TOS handle, and on every reset. Fdup stays with TOS: it only
duplicates standard handles. Cconws and Cconout do not follow the
alias, as in Hatari's GEMDOS drive.

Fclose of a forced standard handle keeps the standard handle in d6
across the lookup: the lookup uses d2, where md-drives-emulator keeps
it, so there the alias was never dropped that way.

The RP's two new functions run from flash and are never inlined. Every
other function in gemdrive.c runs from RAM, and the initialised data
already ended within 1,188 bytes of 0x2000A000; past it the 8 KB-aligned
command ring, and all of .bss behind it, move up 8 KB and the debug
build no longer links (heap below __MinHeapSize). Nothing here is on a
hot path.

md-drives-emulator's second fix in the same change, CMD_SAVE_BASEPAGE
copying past the 256-byte basepage buffer, does not apply: md-devops
already copies 256 bytes.

From md-drives-emulator 26bf831.
A search was claimed by the drive number GEMDRIVE wrote into the
program's search buffer, at the offset where TOS keeps the directory
position and drive of its own searches. A TOS search on a drive with
the same number, at the start of a directory, was indistinguishable,
so the next Fsnext could be answered with the wrong drive's files.

The RP now marks every buffer it fills with a value no file name can
produce, and the Atari routes Fsnext on that mark. TOS's own search
overwrites it, because the mark sits where TOS keeps the search
pattern.

From md-drives-emulator 46a9758.
At the end of a directory search GEMDRIVE cleared all 44 bytes of the
program's search buffer. TOS 1.00 and 1.02 keep a pointer in it, so a
program that asked for one more file after the end sent TOS through a
null pointer. The buffer now keeps its contents and only carries the
mark that says the search was ours, which is what answers any further
request.

On the RP side md-devops already answers "no more files" for a search
it no longer knows, which is what md-drives-emulator changed its
answers to in the same commit.

From md-drives-emulator 6ff7b7a.
Whether a program being started belonged to GEMDRIVE was decided by the
current drive: the drive-letter check looked at the call's frame instead
of the program's name, so it never saw a letter. A program on another
drive was claimed while the emulated drive was current, and one of
GEMDRIVE's own was left to TOS when it was not. It also claimed the
modes that carry no program name at all, including the ones TOS uses to
start GEM itself.

The call is now routed by the program's name, and only the modes that
carry one are GEMDRIVE's. The frame pointer is taken before the drive
check, because that check sends commands and the sender leaves a0-a3
pointing into the command window.

From md-drives-emulator d4820a4.
After every search result GEMDRIVE set the current drive to its own.
The desktop does not expect opening a window to change it. It was there
because Pexec used to be routed by the current drive, which is no longer
the case. The two Dsetdrv calls, each wrapped in a reentry lock, also
cost 152 bytes of the cartridge.

From md-drives-emulator 95a116d.
Fattrib in set mode answered with the attribute bits FatFs could store
(read-only, hidden, system), where TOS answers with the ones it was
asked to set: its xchmod ends with "return mod & 0xff", and Hatari's
GEMDOS drive does the same. The Compendium says the old ones and
contradicts itself in the same entry, so the ROM and the reference win.

Only the GEMDRIVE change of md-drives-emulator's commit, which measured
FSTESTS against Hatari's GEMDOS drive; its test and tooling parts are
not ported.

From md-drives-emulator 2c45d3a.
TOS 1.00 and 1.02 have no PE_GO_AND_FREE, and md-devops's Pexec always
asked for it: it was ported without md-drives-emulator's PE_GO path for
those versions. It now asks GEMDOS for its version once, when it
installs, and below $1500 runs the child with PE_GO and frees the
child's memory itself: it replaces the return address in the caller's
exception frame, and the trap's RTE lands in .pexec_mshrink_exit.

That is the path in the form md-drives-emulator fixed it to. The RTE
restores the caller's status register, so the code runs in the caller's
mode: supervisor when the desktop or the AES starts a program, user
mode when an ordinary program does, where the first 2 KB of memory is
a bus error. So it sends no command (md-drives-emulator's reentry lock
around the Mfree bombed there), and keeps d0 across the Mfree, so
Pexec answers with the child's exit code. TOS 1.00 does not free a
PE_GO child by itself, so the Mfree stays.

The GEMDOS version and the return address are kept in two cells of the
relocated blob: md-drives-emulator keeps them in shared variables the
Atari writes through the RP, a command md-devops does not have.

From md-drives-emulator cad7956.
Fsetdta took a slot in the table of searches for every address the
current drive's programs pointed their DTA at, whether or not a search
ever began there, and a search abandoned half way kept its slot and its
open directory until the slot was evicted or the device reset.

Fsetdta now only notes the address. The table holds searches, and
Fsfirst is what starts one: it already released whatever was at the
address and took a fresh slot, so nothing was gained by taking one
earlier. Fsnext answers the same for a slot with no open directory as
for no slot at all.

A search also belongs to the process that started it, as its files do.
Fsfirst carries the running basepage where it used to carry the ST
address of the file specification, which was never used since the
path itself travels in the buffer: no extra round trip. Pterm then ends
that process's searches along with its files, closing the directory
each one holds open.

From md-drives-emulator 3385249.
Pexec mode 5 hands back a basepage built without reading the program,
so GEMDOS fills in no flags and clears nothing: that is left to whoever
loads the program. We were doing neither. Every program started from
the emulated drive got a basepage with p_flags zero, so from TOS 1.04
on its Malloc could not know which RAM it had asked for, and it got
only its BSS cleared however its header was built.

The loader now copies PRGFLAGS from the header into p_flags at basepage
offset 0x28, and clears what the flags ask for: only the declared BSS
when fastload is set, everything up to the top of the TPA when it is
not.

Below GEMDOS $1500 it always clears the whole heap. TOS 1.00 and 1.02
ignore PRGFLAGS altogether and clear it whatever the program asked, and
the point is to hand a program the memory its own TOS would have handed
it, not to be faster than it. The version is the one install_entry
reads with Sversion, where md-drives-emulator keeps it in a shared
variable.

From md-drives-emulator be3ae5f.
The loader copied a program's header flags into its basepage's p_flags
on every TOS, on the Compendium's word that TOS 1.04 does. It does not:
loading a program from a floppy, which TOS does itself, TOS 1.00 to
1.06 leave p_flags at 0 and TOS 1.62 and later (GEMDOS $1700) copy the
header's flags, as md-drives-emulator measured under Hatari and on
hardware.

GEMDRIVE now copies the flags from GEMDOS $1700 on and leaves the field
as Pexec mode 5 made it below. The fastload rule of the same loader was
right and is unchanged.

From md-drives-emulator 3152de4.
The GEMDOS hook points a0 at the call: the supervisor stack for a
caller in supervisor mode, the user stack for one in user mode. On a
68010 or later it then added 2 for the longer exception frame in both
cases, but the CPU puts that frame on the supervisor stack only; a
user-mode caller's arguments stay on its own stack as it left them.
TOS 4.04's own dispatcher drops the frame word from the supervisor
stack and takes a user-mode call from the user stack as it is.

So on a TT or a Falcon every call made from user mode was read two
bytes off: GEMDRIVE looked up each GEMDOS call by its first argument
and handed its handlers arguments from the wrong place. The desktop
and most programs call from user mode. The 2 now goes on the
supervisor path only. On an ST and a Mega STE, where the frame has no
extra word, nothing changes. md-devops has no other trap hook that
reads a user-mode call.

From md-drives-emulator 5ab2f02.
… copy

md-drives-emulator's senders copy the loop that waits for the RP's
answer into the disk buffer and run it there, and every module
assembles its own loop, with its own timeout. A TT's or a Falcon's
68030 runs it from its instruction cache, which does not see the copy,
so a send could run the loop another module had left at the same
address; on the Falcon every other GEMDRIVE Fopen and Fcreate reached
the RP twice. The senders now clear the instruction cache after the
copy when the machine is a TT or a Falcon, as TOS's own clrcache does.

md-devops's senders are assembled with COMMAND_SYNC_USE_DSKBUF 0: they
run the wait loop where it is, in the cartridge or in the relocated
blob, and copy nothing, so the two calls after the copy are not
assembled here. The routine is, in everything that includes the
senders (26 bytes each in main.s, gemdrive.s, runner.s and DEVOPS.ACC):
the read path calls it next. It reads the machine where HELLO publishes
it, shared variable 0, which in md-devops's window is 8 bytes past the
seed rather than md-drives-emulator's 4.

From md-drives-emulator b669b59.
In md-drives-emulator the clear took the machine type at
RANDOM_TOKEN_SEED_ADDR + 4, which is the drivers' shared variable 0 in
every module but main.s, whose token lives elsewhere: on a Mega STE the
setup menu's first send read whatever was there, took it for a TT or a
Falcon, and ran movec, which a 68000 does not have. Four bombs.

md-devops's modules all share one token, seed and shared-variable
block, so the address was already right here. The guard is what this
brings: the routine returns at once when _longframe is 0, as it is on
every 68000, before it reads anything, so movec can never run on one
whatever the variable holds. The address now has a name.

From md-drives-emulator 3d28403.
TOS's own floppy driver clears the caches at the end of every Rwabs
read, and TOS's Pexec does not: the driver that read the data is the
one that knows code may have arrived. GEMDRIVE copies what it reads
with the CPU and did not, so on a TT or a Falcon a program loaded where
other code had just run could execute old instructions from the cache.

The instruction cache is now cleared at the end of GEMDRIVE's read
core, Fread and the program load of its own Pexec. The data cache is
write-through, so the CPU's copies are already in it. The clear keeps
every register now. On other machines nothing is done. md-devops has no
floppy or ACSI driver, the other two places md-drives-emulator clears
it.

From md-drives-emulator 1506f05.
GEMDOS reads each part of a name as 8.3 and drops the rest, so a
program can open "dmalogo.imgg" and get DMALOGO.IMG. PP's Gamex
launcher for Lemmings does it in its intro. GEMDRIVE handed the name to
FatFs as it came, got no such file and answered EFILNF, and the game
went on to show a logo it had never loaded.

getLocalFullPathname, which builds the card path for Fopen, Fcreate,
Fdelete, Fattrib, Frename, Dcreate and Ddelete, now takes the 8.3 form
when the name as it came is not on the card and that form is. Only the
program's part of the path is cut, and only when one of its parts is
longer than 8.3, so a name that is on the card as it came - a long name
included - opens as before, with no extra card access. Dsetpath builds
its path elsewhere and is unchanged. In md-devops Fsfirst builds the
folder it lists with getLocalFullPathname too, so a folder named past
8.3 is found the same way there.

The lookup runs from flash and is never inlined, for the RAM reason
given with the Fforce change.

From md-drives-emulator cc0005e.
--machine st100 and st102 boot an ST with TOS 1.00 or 1.02 (linked as
emu/tos/tos100uk.img and tos102uk.img), for GEMDRIVE's program loader,
which takes another path below GEMDOS $15. "all" stays the ST, STE and
Mega STE.
@diegoparrilla
diegoparrilla merged commit b47fb2a into release/v1.2.0 Dec 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant