Portable CP/M

Post Reply
stefano
Well known member
Posts: 1802
Joined: Mon Jul 16, 2007 7:39 pm

Portable CP/M

Post by stefano »

Someone discovered an interesting C conversion of the CP/M commands in the floppy disk images for the Olivetti M20 CP/M-8000 Operating System.
A "portable CP/M" codename is mentioned in the sources which brought a little bit of enthusiasm to those not very much into the CP/M technicalities.
An article mentions a Pascal conversion, which IMO just brings further confusion.
I understood that at a certain point in the earlier eighties Digital Research got hold of a C compiler for the Motorola 68000 named "Alcyon C Compiler", which was used to port those commands originally written in PL/M to the Motorola CPU.

Both the sample BIOS in C in the CP/M-68K manual and the comments in the sources for the Z8000 refer to the "Alcyon C", thus I tend to agree with this comment:

Code: Select all

The developer of these versions was an experienced UNIX programmer. The
compilers were bought from outside companies for these projects. Since
no PL/M compiers existed, it was faster to translate the utilites. The
Z8000 CP/M is based on the 68K version, and was done because Zilog was
begging them to port CP/M to the Z8000. I don't know if Olivetti was
already lined up as a customer when the project was started.
By the way the programs are *very* clever, they're a great reference for anybody willing to understand what CP/M was and to spot the similarities and differences between the various versions.
They don't need the usual C libraries, they do everything by directly interfacing to BDOS and directly parsing the command line buffer.
Well, it looks like z88dk could be able to support a backport !
Here's an example of a partially working STAT command written for the Olivetti M20 but built and run on a ZX Spectrum +3 with CP/M v3.
portable-stat-cpm3.png
I'm putting the backport here. Beware! It is not working stuff !!
https://github.com/z88dk/z88dk-ext/tree ... ted/CPM/DR
You do not have the required permissions to view the files attached to this post.
stefano
Well known member
Posts: 1802
Joined: Mon Jul 16, 2007 7:39 pm

Re: Portable CP/M

Post by stefano »

Interestingly, the PL/M versions of the STAT command for CP/M 2.2 use the BDOS function number 31 to get the Disk Parameter Block:

Code: Select all

set$dpb: procedure;
    /* set disk parameter block values */
    dpba = mon3(31,0); /* base of dpb */
    end set$dpb;
In many cases the real implementations of CP/M 2.2 missed such function, most of the real world tools had to deal with the BIOS, this is my own attempt to extend the C implementation:

Code: Select all

#define GET_DPB         31                      /* Get disk parameters      */

                                                /* _get_dpb has parameter in*/
                                                /*   some implementations   */
                                                /*   of CP/M but not others */
                                                /* This macro suitable only */
                                                /*   for former             */
#define _get_dpb(a)     (__BDOS(GET_DPB, (int) (a)))
                                                /* This one handles latter  */
#define _get_dpa()      (__BDOS(GET_DPB, (int) 0))

Code: Select all

   if (ver < THREE_X) {
		dpba = biosh( 9,cdisk,0)+5;
   } else {
        if (VOID_GET_DPB(ver))                  /* Get DPB (just how depends*/
        {                                       /*   on CP/M implementation)*/
                _get_dpb(&dpb);
        }
        else
        {
                dpba = (struct dpbs *) _get_dpa();
        }
   }

I think that a backport of "stat.c" to versions previous than CP/M v3 will be challenging, but I'm already surprised that setting/resetting the RO/RW flags works on version 3 !
pjshumphreys
Member
Posts: 30
Joined: Sat Feb 06, 2021 2:32 pm

Re: Portable CP/M

Post by pjshumphreys »

If it's helpful, the Alcyon C compiler came with the Atari ST SDK disks. I think it was made by Digital Research themselves. The Atari ST's TOS operating System is effectively CP/M m68k with GEM sitting on top of it. The SDK disks are available on https://docs.dev-docs.org/ under the "Dev Software" section as "Atari TOS Developer's Kit"
stefano
Well known member
Posts: 1802
Joined: Mon Jul 16, 2007 7:39 pm

Re: Portable CP/M

Post by stefano »

Thank you, I knew already that this compiler was used on the Atari but I didn't know the Atari people had a CP/M + GEM system on it!
I'm not sure I'm able to complete the backport but surely I'm surprised by the quality of the code in those few tools and by the general CP/M structure which was quite basic but evolved into an elegant portable framework.


EDIT: ED.C is a good example of such elegance

https://github.com/z88dk/z88dk-ext/blob ... PM/DR/ed.c

I'm not sure I'll be able to fix it and complete the backport though :)
A question for Dominic, in case he spots it in this corner: is there a way to reference to the free memory space ? e.g. the space between heap and stack

https://github.com/z88dk/z88dk-ext/blob ... ed.c#L3148
stefano
Well known member
Posts: 1802
Joined: Mon Jul 16, 2007 7:39 pm

Re: Portable CP/M

Post by stefano »

I noticed that the original PLM source used a very basic logic to determine the free memory, I'm doing the same.
The top of the TPA area is roughly identified by gathering the BDOS entry @ position 6.
I've set the beginning of free memory at a reasonable location determined keeping in consideration the program size and adding a bit of margin.
https://github.com/z88dk/z88dk-ext/blob ... ed.c#L3174

The keyboard scanning is tricky on ED.C, because of the way the different CP/M flavors implement the BIOS and BDOS functions, I had to replace the original BIOS call with 2 workarounds depending on the involved target.

The program is still buggy. Anyway I wasn't hoping to get this far ;)
ed.c.png
You do not have the required permissions to view the files attached to this post.
pjshumphreys
Member
Posts: 30
Joined: Sat Feb 06, 2021 2:32 pm

Re: Portable CP/M

Post by pjshumphreys »

To confirm, here's the header block of STDIO.H from the Atari TOS Developers Kit:

Code: Select all

/*****************************************************************************
*
*		    A T A R I  " C"   H E A D E R   F I L E
*		    ---------------------------------------
*	Copyright 1985 by ATARI CORP.  All rights reserved.
*
*	This is the standard include file for the CP/M C Run Time Library.
*
*****************************************************************************/
stefano
Well known member
Posts: 1802
Joined: Mon Jul 16, 2007 7:39 pm

Re: Portable CP/M

Post by stefano »

Code: Select all


/****************************************************************************/
/*                                                                          */
/*       E D  :   T h e   C P / M   C o n t e x t    E d i t o r            */
/*       -------------------------------------------------------            */
/*                                                                          */
/*      Copyright (c) 1976, 1977, 1978, 1979, 1980, 1981, 1982              */
/*          Digital Research                                                */
/*          Box 579 Pacific Grove                                           */
/*          California 93950                                                */
/*                                                                          */
/*          Revised:                                                        */
/*            07 April 81  by Thomas Rolander                               */
/*            21 July  81  by Doug Huskey                                   */
/*            29 Oct   81  by Doug Huskey                                   */
/*            10 Nov   81  by Doug Huskey                                   */
/*            08 July  82  by Bill Fitler                                   */
/*            26 July  82  by Doug Huskey                                   */
/*             1 Aug   82  by Dave Sallume, Zilog Inc.                      */
/*            12 Sept  82  by Dominic Dunlop, Zilog Inc.                    */
/*                                                                          */
/*                                                                          */
/*              **** this message should be in the header ****              */
/*                                                                          */
/* char copyright[] =                                                       */
/*      " Copyright (c) 1982, Digital Research ";                           */
/*                                                                          */
/****************************************************************************/

char    date[] = "8/82";

/****************************************************************************/
/*                                                                          */
/*              M O D I F I C A T I O N   L O G                             */
/*              -------------------------------                             */
/*                                                                          */
/* Modified for .PRL operation                  May, 1979                   */
/* Modified for operation with CP/M 2.0         August 1979                 */
/* Modified for MP/M 2.0                        June 1981                   */
/* Modified for CP/M 1.1                        October 1981                */
/* Modified for concurrent CP/M 1.0             July 1982                   */
/* Modified for CP/M 3.0                        July 1982                   */
/* Translated to C                              August 1982                 */
/*                                                                          */
/*   Aug 1982 Zilog: Translated to C.                                       */
/*                                                                          */
/*   July 1982 WHF: Some code cleanup (grouped logicals, declared bool);    */
/*        fixed disk full error handling; fixed read from null files;       */
/*        fixed (some) of the dirty fcb handling (shouldn't use settype     */
/*        function on open FCB's!).                                         */
/*                                                                          */
/*   July 1982 DH: Installed patches to change macro abort command from     */
/*        ^C to ^Y and to not print error message when trying to delete     */
/*        a file that doesn't exist.  Added perror: procedure to print      */
/*        error messages in a consistant format and modified error          */
/*        message handler at reset: entry point.  Also corrected invalid    */
/*        filename error to not abort ed if parsing a R or X command.       */
/*        modified start and setdest to prompt for missing                  */
/*        filenames.  Modified parse_fcb & parse_lib to set a global        */
/*        flag and break if it got an invalid filename for X or R commands. */
/*        Start sets page size from the system control block (SCB) if       */
/*        ed is running under CP/M-80 (ver & 0xff00 == 0).                  */
/*        The H command now works with new files. (sets newfile = FALSE)    */
/*                                                                          */
/****************************************************************************/


;)

It could mean that Zilog had an active role as a contributor to the Portable CP/M development (August, 1982)

Code: Select all

/****************************************************************************/
/*                                                                          */
/*                              B D O S . H                                 */
/*                              -----------                                 */
/*                                                                          */
/*              Copyright (c) 1982, Zilog Incorporated                      */
/*                                                                          */
/*      Macros defining the direct BDOS calls used by the standard CP/M     */
/*      utilities (ED, PIP, STAT, SET, SHOW.)  Some necessary data          */
/*      data structures are also defined.                                   */
Post Reply