I'm looking for a way to identify CP/M-80 (i.e. 8080 code) program files (.COM) from their content. A process usually known as Magic Number or File Signature detection, intended to identify a file by looking at their first few bytes for certain values and/or structure.
To narrow this down a bit, it will be fine if the distinction is good enough to separate CP/M-80 .COM files from their counterpart under MS-DOS(*1). That means, while a positive detection of a .COM file being 8080 code is great, already a negative detection of it not being 8086 code will do the trick.
I couldn't find any information. In addition, what I could figure out so far is rather limited:
- CP/M 3 file may start with an optional header starting with a C9h (RET) marking a new, optional header used for RSX linking.
Basic CP/M programs often start with
- Loading the stack pointer
LXI SP,const(31 xx xx)
- A variant thereof is
SPHL(2A xx xx F9)
- Some even save SP first with
- Often this is prefixed by a jump around some header
LXI ...(C3 xx xx ...)
But I also found next to any other instruction when scanning files I have at hand - including pushing all registers onto the OS stack first...
The good part is that on a x86
- C3h is a
- 31h is a
- 2Ah is a
The first is rather useless as a program start, but might be used to make prevent data files from execution, while the second may be unusual, but not unheard of to clear a register during setup. The last one is most definitive unusual for a program start, but as a 2...4 byte instruction any combination may show up as 4th byte, so a check for F9h is even weaker.
So either are rather weak indicators.
Question: Is there any (somewhat reliable) fast way for detecting CP/M-80 program files.
From comments it seems that it would be of help to add why I'm looking for that information:
I'm in some NEC V20 nostalgia phase, here about the only useful improvement beside speed and 80186 instruction extension: The 8080 emulation mode. I'm tinkering with first drafts of a modified MS-DOS COMMAND.COM that would - or as of now should - detect when a .COM file is selected for execution if that one is possibly an 8080 one for CP/M-80. If this is the case it would
- prepare the PSP according to CP/M (won't need much change),
- add a BDOS to MS-DOS translation stub to top of memory (right below 64KiB),
- Setup emulation vectors
- invoke emulation mode and
- clean up changed vectors after exit.
All of that not a big deal and already working fine, except for the detection part. And yes, I know, this has been done back then several times in form of loaders (which mine already is), but my attempt is to make it seamless, much like it was possible on CP/M-86.
Oh, and all of that should of course work on a 4.77 MHz XT without much delay - remember kids, grampa had to shovel his data with a toy sized scoop, both ways up hill and in the snow.
*1 - Using the same extension as CP/M-80 was a real stupid decision. CP/M-86 was at least so kind to go with .CMD - and drop memory images totally.