|
|
Writing 32-bit softwarewith the new Acorn C/C++ development suite |
| by Paul Skirrow, from an article produced for Archive magazine | |
|
see also: Acorn Publisher, Autumn 2002 & My RISC OS portal for your continued discussion of this item. Non-programmers may prefer to read the shorter version of this article in Acorn Publisher magazine which gives the background to the subject but with much of the programming technicalities removed. |
|
|
The Acorn C/C++ Development Suite from Castle Technology Ltd is a major update to Acorn C/C++ that Acorn released in 1994. It includes new 32-bit versions of the C compiler, ARM assembler, linker and all of the other tools that were in the original C/C++ pack. More importantly it enables software to be ported to Castles new 32-bit IYONIX PC ahead of its launch. Acorn C/C++ cost 250 in 1994 and was supplied with four printed manuals totalling nearly 1400 pages and several floppy discs. Castle supply a single CD for 99 which contains the new software, the updated manuals in PDF format, and some useful public domain software including StrongEd, Zap, Perl and 32-bit IYONIX versions of Colin Granvilles PDF reader and FTPc for transferring files to or from FTP sites. All of these are installed automatically by the supplied installer and work with RISC OS 3.10 onwards. 32-bit ModeWhen Acorn designed the ARM processor back in the mid-80s it was widely regarded as an innovative and elegant design. One key feature was to combine the Program Counter (PC) with the Processor Status Register (PSR) into a single 32-bit register. This had the benefit that the PSR could be saved automatically with the PC to achieve fast subroutine calls and quick interrupt response. The disadvantage was that 26-bit mode limited the PC to a maximum range of 64MByte.When the Risc PC was launched in 1994 it used the ARM6 processor which overcame this problem by introducing a 32-bit mode. However, for compatibility with existing software, most of RISC OS and all of the RISC OS applications continued to use the old 26-bit mode and the new 32-bit mode was largely ignored. The latest generation of ARM processors, including the Intel XScale used in Castles IYONIX PC, only operate in 32-bit mode and no longer provide the old 26-bit compatibility mode. The new Acorn C/C++ Development Suite enables programmers to produce 32-bit compatible software suitable for anything from a humble ARM2-based Archimedes running RISC OS 3.10 to the new XScale-based IYONIX PC running a 32-bit version of RISC OS. Converting Programs to 32-bitMost RISC OS applications are written in BASIC, C or assembler. How should these programs be updated to work on a 32-bit computer?Anything written in BASIC should work without any problem since it is an interpreted high level language which isnt translated into ARM machine code. BASIC programs that include assembler code will need the assembler sections updating as described below. The vast majority of the commercial RISC OS applications are written in C. The good news is that C programs can easily be converted to run in a 32-bit environment re-compiling them using the new C compiler and linking them with new 32-bit compatible libraries on Castles CD. The new version will work on existing 26-bit versions of RISC OS as well as 32-bit versions of RISC OS. Programs that use other compiled languages or libraries will also need to be re-compiled and linked against the new libraries. New 32-bit versions of the ABC BASIC compiler and UnixLib libraries are expected to be available soon. Assembler programs are more complicated as they need to be modified by hand. In particular, instructions that manipulate the Processor Status Register (PSR) in 26-bit mode (such as MOVS PC, TEQP, LDM ^ etc) will not work in 32-bit mode. If the code is only intended for ARM6 and later processors (ie RISC OS 3.50 onwards) then these instructions may simply be replaced by the new MSR and MRS instructions. If compatibility with ARM2 and ARM3 computers is required than more cunning code sequences are needed to achieve compatibility across the whole range of processors. There is no easy way of automating the conversion process, although the ARM Clubs free ARMalyser tool is helpful in identifying what needs converting. This may sound difficult but the changes are actually simpler than the changes needed to make software run on the StrongaRM when it was launched in 1996 and Ill cover this in more detail in part 2 next month. The Pace BranchSome history is necessary to explain the background of the new C/C++ Development Suite:RISCOS Ltd obtained a source code licence from Acorn in 1999 and took over development of RISC OS 4 and this version is what we see in modern RISC OS computers. Meanwhile Acorn (which became Element 14) and then Pace continued to develop RISC OS 4 internally for their Set Top Box products and the Bush Internet television. These two branches developed in different ways RISCOS Ltd added enhancements for desktop users while Pace made other improvements and converted RISC OS and its various modules to be 32-bit compatible. The C compiler, tools, libraries and modules that Castle are now distributing are from the Pace branch and not from RISCOS Ltd. This is necessary to get 32-bit compatibility but it does mean that some of the extra features that RISCOS Ltd added to the toolbox modules, for example, may not be available in Castles 32-bit versions. Developers need to be aware of this and ensure that their code does not use any of the features that are unique to the RISCOS Ltd modules. What Else is New?You might be wondering how the new tools differ from the ones that RISCOS Ltd and Pace released 18 months ago. Although they are similar, the new tools have evolved considerably since that release and offer several new features as well as fixing a lot of problems, some of which affected the generation of 32-bit code.The ARM assembler supports all of the new ARM processors including the Intel XScale. A cpu assembler option allows the target processor to be specified and the assembler will warn of any instructions which are unavailable on the specified processor. This is useful as you might want to specify the ARM2 as the target processor but use MSR and MRS instructions after testing for 32-bit mode. The assembler will warn when you do this, but still allow it, thus making it easy to write code which runs on old and new processors. Many of the other tools have been updated. The AMU make utility has been improved significantly and now provides much of the functionality of the popular Gnu make tool including string manipulation operators. It also allows makefiles to include other makefiles and this is especially useful for managing large projects (indeed this feature is used to build RISC OS itself). The C Module Header Generator (CMHG) now uses the C pre-processor. This allows C header files to be included so that symbolic names can be used rather than hexadecimal numbers. For example: #include window.h #include services.h swi-chunk-base-number: Window_SWIChunkBase service-call-handler: Window_services Service_ModeChange CMHG also sets the new 32-bit compatible flag which is used by RISC OS 5 to identify and reject 26-bit modules. The new C compiler generates faster code and implements many features from the new C99 standard. C99The original Acorn C/C++ software released in 1994 complied with the 1989 ANSI C (American National Standards Institute) standard which is usually referred to as C90 since it was adopted by ISO (International Standards Organisation) in 1990.ISO have now produced a new standard for the C programming language which is commonly referred to as C99. It adds several new features which will be widely welcomed by C programmers, while maintaining backwards compatibility as far as possible. C99 adds some new keywords and tightens some restrictions, so there is a small chance that old programs will generate some errors or warning in which case a convenient C90 flag can be used to make the compiler behave in accordance to the C90 standard. The new Acorn C/C++ compiler does not fully comply with the C99 standard and in particular it doesnt implement all of the new library functions, but it does implement most of the more useful features. The following should give a taste of a few of the improvements which are described fully in the new Acorn C/C++ manual included on the CD: 1. 64-bit integers are implemented via the long long type. Long long literals use the LL suffix and they can be printed using the new ll format specifier: #include <stdlib.h> #include <stdio.h> unsigned long long int MSB_set = 1ULL <<63; printf(MSB_set is %lld in decimal\n, MSB_set); printf(or %llx in hexadecimal\n, MSB_set); 2. Statements and declarations may be interleaved, allowing declarations to be positioned to make code more readable: foo();int b = 5;bar(); 3. Loop variables may be declared in the for statement: for (int i=0; i<10; i++) Variable length arrays, varying in multiple dimensions may be passed as function parameters: void fred(int w, int h, char d[h][w]) { for (int y=0; y } 4. The last element of a structure may be an array of unspecified size and this is referred to as a flexible array member: struct flex { int len; char data[]; }; struct flex *p=malloc(sizeof(struct flex)+10*sizeof(p->data[0])); for (int i=0; i<10; i++) p->data[i] = i; The object pointed to by p behaves as if it had been declared as: struct flex{int len; char data[10]} It is still the programmers responsibility to allocate the necessary memory space correctly. 5. snprintf prints a formatted string to a buffer like sprintf but take an extra parameter which specifies the size of the buffer: char buffer[20]; len = snprintf(buffer, sizeof buffer, Result is %s, s); if (len >= sizeof buffer) printf(Buffer too small\n); These are just a few examples there are plenty of other improvements. New ModulesNew 32-bit versions of the shared C library, floating point emulator and other modules for distribution are supplied by Castle in a !System directory on the CD. The new shared C library is needed for any programs built to run in 32-mode (this is the compiler default) and any programs that use 64-bit integers or any of the other new C99 features or C99 library functions. If none of these are being used then there is no need to distribute the new C library with your application (as a general rule the new modules should only be distributed and installed if they are really needed).DocumentationThe original Acorn C/C++ pack included four manuals: Acorn C/C++, Desktop Tools, Acorn Assembler and User Interface Toolbox. The first three have been updated significantly with details of the new features, but the toolbox manual hasnt been updated for the first release.The CD also includes the RISC OS 3 Programmers Reference Manual, volumes 1-5a which describes RISC OS 3.60, but not later versions. All of the manuals are provided in Adobe PDF (Portable Document Format) and may be read using the latest version of Colin Granvilles public domain PDF reader which is included on the CD. This works extremely well especially as it has been enhanced specifically for this project. In particular, PDF version 1.01.1.08.15 renders thin lines correctly and it even has the option to highlight cross-references in blue. This is a very welcome feature as you can easily see the cross-reference links and click on them to jump to the relevant section. Even Adobes own Acrobat reader cannot highlight cross-references in this way (unless the author has changed the style in the document, but then it cannot be turned off for printing). Clicking with adjust causes a new PDF window to open allowing you to view several pages simultaneously. The manuals can be printed single-sided or double-sided using !PDF but it is unlikely that many ink-jet users will print them in their entirety due to the cost. Castle may publish printed manuals in the future (at extra cost) if there is enough demand. ConclusionThe Acorn C/C++ Development Suite is available now at the special launch price of 99* inc VAT from Castle. It enables software writers to produce 32-bit applications and it has already been used by several major developers to produce 32-bit versions of their software which run on the prototype IYONIX PCs.* The full RRP is 199 inc VAT the special 99 introductory price expires on 31st December 2002. More details and on-line ordering form are on http://www.castle.uk.co/castle/software.htm |
|
| This article is reproduced from a series in Archive magazine and a shorter version for the general reader appeared in the Autumn 2002 issue of the Acorn Publisher magazine. | |
| Initially published October 2002 | |
|
|
The contents of this page are copyright the author. This article must not be reproduced without the express permission of the publishers. Whilst we take care to ensure that the facts described in AP On-line are correct we appreciate that errors will occur and will try to ensure that such errors are amended as soon as possible after they are notified to us. |