Difference between revisions of "Native MIPS Systems"

From Free Pascal wiki
Jump to navigationJump to search
 
(64 intermediate revisions by 6 users not shown)
Line 1: Line 1:
The MIPS architecture is unusual in that it was designed for desktop computers, is now overwhelmingly used for embedded systems, but can still occasionally be found in desktops or small servers. The only comparable architecture is [[Native ARM Systems|ARM]], which is the platform of choice for a multitude of "bratphones", tablets etc.
+
The MIPS architecture is unusual in that it was designed for desktop computers, is now overwhelmingly used for embedded systems, but can still occasionally be found in desktops or small servers. The only comparable architecture is [[Native ARM Systems|ARM]], which is the platform of choice for a multitude of mobile phones, tablets etc.
  
Like almost all computers, MIPS-based systems will contain an internal loader, usually in Flash memory. However there are three basic categories of system:
+
Like almost all computers, MIPS-based systems will contain an internal loader, usually in flash memory. However there are three basic categories of system:
  
*  Those that boot an operating system from internal Flash, such as the Linksys/Cisco WRT54G [http://wiki.openwrt.org/].
+
*  Those that boot an operating system from internal flash, such as the Linksys/Cisco WRT54G [http://wiki.openwrt.org/].
  
 
*  Those that boot an operating system from a conventional disc, connected via IDE, SCSI or USB, such as an SGI server or workstation, or a Cobalt/Sun Qube or Raq.
 
*  Those that boot an operating system from a conventional disc, connected via IDE, SCSI or USB, such as an SGI server or workstation, or a Cobalt/Sun Qube or Raq.
Line 9: Line 9:
 
*  Those running as guests using an emulator such as [[Qemu and other emulators|Qemu]] on a general-purpose computer.
 
*  Those running as guests using an emulator such as [[Qemu and other emulators|Qemu]] on a general-purpose computer.
  
In any of these cases it is necessary to be able to install a general-purpose operating system such as Linux in order to be able to run development tools. This writer (MarkMLl) favours Debian, since he finds that using it results in a very similar system on a wide range of hardware (x86, SPARC, PPC, MIPS, ARM, zSeries).
+
In any of these cases it is necessary to be able to install a general-purpose operating system such as Linux in order to be able to run development tools. This writer (MarkMLl) favours Debian, since he finds that using it results in a very similar system on a wide range of hardware (x86, SPARC, PPC, MIPS, ARM, zSeries, 68K).
  
 
'''If you intend to build Lazarus, make sure you have at least 512Mb memory (RAM + swap) available.'''
 
'''If you intend to build Lazarus, make sure you have at least 512Mb memory (RAM + swap) available.'''
  
== Interim Notes ==
+
__TOC__
  
The "unofficial" MIPS compiler at [http://sourceforge.net/projects/fpc-mips/develop] was written by David Zhang (possibly [http://www4.comp.polyu.edu.hk/~csdzhang/]) and is licensed under GPL Version 2, it cites but does not include the COPYING.FPC file.
+
== Microchip microcontrollers ==
  
There are .zip files of both sources and a minimal executable at Sourceforge (link above). The sources are basically modified contents of fpcsrc/compiler/mips based on FPC 2.0.0, the compiler targets mipsel and is described as being operational on Qemu but not yet supporting shared libraries.
+
In 2013, Microchip enhanced their series of "PIC24" and "PIC33" chips with the "PIC32" variants, providing compatible (to a certain extent even pin-compatible) alternatives to the 16 bit chips (PIC32MX..."), as well as much more powerful ones ("PIC32MZ..."). The PIC32 chips are both cheaper and more versatile than the corresponding PIC24s. Hence they include both cheap and powerful controllers. The PIC32 chips feature a "M4K" MIPS CPU and so they could run software done with the FPC MIPS cross compiler. As usually these chips are used for doing "deeply embedded" systems: in most cases there will be no formal OS and the user code programmer will need to adapt the RTL to fit his needs. But as the C-based "MPLAB-X" IDE by Microchip comes with a huge library that provides basic functions (such as heap management), the "standard" RTL for such purpose should take this into account.
  
Since this is specifically a little-endian compiler it should presumably be compatible with Linux on a MIPS-based Qube or Raq, or with the Chinese "Loongson" processor of fable. It will not be compatible with SGI MIPS systems, which are big-endian.
+
== FPC Implementations for MIPS ==
  
There is a system library bundled with the compiler, but no indication of how far the compiler is capable of handling the remainder of the standard libraries.
+
As of 2011 there are two implementations of FPC for the MIPS architecture, both targeting Linux.
  
== Messy Working Notes ==
+
* A fork of the mainline compiler at 2.0.0 by David Zhang. ''Now deprecated due to mainline integration.''
  
Anybody: please feel free to improve/delete all of this, they're basically stream-of-consciousness.
+
* An ongoing effort to reconcile this with the mainline project, in SVN trunk and partially completed in 2.7.x.
  
I've now got mipsel working as a [[Qemu and other emulators|Qemu]] guest, but have reservations about the performance; I've put test results on that page. I'd very much prefer having some real MIPS hardware, but right now that's not going to happen.
+
=== David Zhang's Compiler ===
  
I don't know how good something like a Raq would be for compilation. They've not got much memory but take standard EDO, if we're lucky they'd work with Compaq DIMMs of which we have a lot. We missed an SGI Origin a few months ago but only spotted it a couple of hours before the eBay auction closed- went for £10 :-(
+
The "unofficial" MIPS compiler at [http://sourceforge.net/projects/fpc-mips/develop] was written by David Zhang (possibly [http://www4.comp.polyu.edu.hk/~csdzhang/]) and is licensed under GPL Version 2, it cites but does not include the COPYING.FPC file. The compiler appears to have been originally based on the SPARC port, and there are significant differences between it and the mainline sources. It is discussed for historical interest, and as reference until the mainline compiler is at least as complete and robust.
  
The compiler is for mipsel (i.e. not immediately usable on SGI etc.). Comparing with arm/armel I've found minimal difference in the compiler and system unit between big- and little-endian.
+
There are .zip files of both sources and a minimal executable at Sourceforge (link above). The sources are basically a complete reimplementation of fpcsrc/compiler/mips based on FPC 2.0.0, the compiler targets mipsel and is described as being operational on Qemu but not yet supporting shared libraries.
 +
 
 +
Since this is specifically a little-endian compiler it should presumably be compatible with Linux on a MIPS-based Qube or Raq, or with the Chinese "Loongson" processor. It will not be compatible with SGI MIPS systems, which are big-endian.
 +
 
 +
There is a system library bundled with the compiler, but no indication of how far the compiler is capable of handling the remainder of the standard libraries. The released files may be downloaded from:
  
 
* http://sourceforge.net/projects/fpc-mips/files/fpc-mips/0.90/fpc_mips_bin_090.zip/download
 
* http://sourceforge.net/projects/fpc-mips/files/fpc-mips/0.90/fpc_mips_bin_090.zip/download
Line 39: Line 43:
 
* http://sourceforge.net/projects/fpc-mips/files/fpc-mips/0.90/fpc_mips_all_090a.zip/download
 
* http://sourceforge.net/projects/fpc-mips/files/fpc-mips/0.90/fpc_mips_all_090a.zip/download
  
This is both source and binary, dumped into a single directory which appears to correspond to the standard fpc_200/compiler, i.e. there's no separate compiler/mips directory.
+
This is both source and binary, dumped into a single directory which appears to correspond to the standard fpc_200/compiler, i.e. there's no separate compiler/mips directory; it also partially reimplements some standard libraries etc.
 +
 
 +
=== Mainline MIPS Port ===
 +
 
 +
This is part of the trunk development files, but does not yet appear in standard releases; i.e. it's only available via an svn download.
 +
 
 +
The host is assumed to be a non-MIPS system, for example x86 running Linux (e.g. Debian "Squeeze"). It needs a copy of binutils, and it is probably expedient to make this the same version as is running natively, i.e. 2.20.1 in the above case, downloaded from [http://ftp.gnu.org/gnu/binutils/binutils-2.20.1.tar.bz2]. In any event, no version older than 2.18 will work since symbolic register names are a prerequisite.
 +
 
 +
Build and install cross-binutils:
 +
<syntaxhighlight lang="bash">
 +
tar xjf binutils-2.20.1.tar.bz2
 +
cd binutils-2.20.1
 +
./config.sub mipsel-linux-gnu
 +
./configure --prefix=/usr/local/mipsel-linux mipsel-linux-gnu
 +
make
 +
sudo make install
 +
sudo ln -s /usr/local/mipsel-linux/bin/as /usr/local/bin/mipsel-linux-as
 +
sudo ln -s /usr/local/mipsel-linux/bin/ld /usr/local/bin/mipsel-linux-ld
 +
sudo ln -s /usr/local/mipsel-linux/bin/ar /usr/local/bin/mipsel-linux-ar
 +
sudo ln -s /usr/local/mipsel-linux/bin/objdump /usr/local/bin/mipsel-linux-objdump
 +
sudo ln -s /usr/local/mipsel-linux/bin/objcopy /usr/local/bin/mipsel-linux-objcopy
 +
sudo ln -s /usr/local/mipsel-linux/bin/strip /usr/local/bin/mipsel-linux-strip
 +
mipsel-linux-ld -V
 +
</syntaxhighlight>
 +
 
 +
Particularly if working with the trunk sources, consider doing the same for a different CPU (e.g. powerpc-linux-gnu) so as not to be caught out if they won't build due to a temporary inconsistency.
 +
 
 +
Build a compiler which runs on the host but targets mipsel, note debug options which are needed later:
 +
<syntaxhighlight lang="bash">
 +
cd /usr/local/src/fpc-trunk
 +
export PP=
 +
make "OPT=-O- -g" -C compiler mipsel
 +
mv compiler/ppcmipsel compiler/ppcXmipsel
 +
</syntaxhighlight>
 +
 
 +
Renaming the compiler prevents it from being deleted by make clean etc., consider extending that OPT setting with -dEXTDEBUG.
 +
 
 +
<syntaxhighlight lang="bash">
 +
export PP=/usr/local/src/fpc-trunk/fpc/compiler/ppcXmipsel
 +
make CPU_TARGET=mipsel OS_TARGET=linux rtl
 +
make CPU_TARGET=mipsel OS_TARGET=linux compiler
 +
</syntaxhighlight>
 +
 
 +
Move the newly-generated compiler and the RTL units to the target system, which should now be able to do a full build.
 +
 
 +
==== Stall point ====
 +
 
 +
As of October 2012 there are still some code-generation tests failing.
 +
 
 +
To be continued (hopefully :-)
 +
 
 +
As a general point, there's some useful thoughts on binary disassembly at [http://chdk.wikia.com/wiki/GPL_Disassembling] for situations where IDA or equivalent aren't available.
 +
 
 +
=== Big-endian subarchitecture ===
 +
 
 +
''Working notes, no permanent links.''
 +
 
 +
This is a highly-experimental investigation of how much work it would take to port the existing little-endian compiler (mipsel) to big-endian architectures. Note that the GNU triplet is mips-linux-gnu, there is no such thing as mipseb-linux-gnu etc.
 +
 
 +
Build and install cross-binutils:
 +
<syntaxhighlight lang="bash">
 +
tar xjf binutils-2.20.1.tar.bz2
 +
cd binutils-2.20.1
 +
./config.sub mips-linux-gnu
 +
./configure --prefix=/usr/local/mips-linux mips-linux-gnu
 +
make
 +
sudo make install
 +
sudo ln -s /usr/local/mips-linux/bin/as /usr/local/bin/mips-linux-as
 +
sudo ln -s /usr/local/mips-linux/bin/ld /usr/local/bin/mips-linux-ld
 +
sudo ln -s /usr/local/mips-linux/bin/ar /usr/local/bin/mips-linux-ar
 +
sudo ln -s /usr/local/mips-linux/bin/objdump /usr/local/bin/mips-linux-objdump
 +
sudo ln -s /usr/local/mips-linux/bin/objcopy /usr/local/bin/mips-linux-objcopy
 +
sudo ln -s /usr/local/mips-linux/bin/strip /usr/local/bin/mips-linux-strip
 +
mips-linux-ld -V
 +
</syntaxhighlight>
 +
 
 +
Build a compiler which runs on the host but targets mipseb, note debug options which are needed later:
 +
<syntaxhighlight lang="bash">
 +
cd /usr/local/src/fpc-trunk
 +
export PP=
 +
make "OPT=-O- -g" -C compiler mips
 +
mv compiler/ppcmips compiler/ppcXmips
 +
</syntaxhighlight>
 +
 
 +
For my own reference, actually using /usr/local/src/fpc/zhang/fpc-trunk/fpc
 +
 
 +
<syntaxhighlight lang="bash">
 +
export PP=/usr/local/src/fpc-trunk/fpc/compiler/ppcXmips
 +
make CPU_TARGET=mips OS_TARGET=linux rtl
 +
# make CPU_TARGET=mips OS_TARGET=linux compiler
 +
</syntaxhighlight>
 +
 
 +
Build a compiler which runs on the target:
 +
<syntaxhighlight lang="bash">
 +
export PP=/usr/local/src/fpc-trunk/fpc/compiler/ppcXmips
 +
make "OPT=-O- -g" -C compiler mips
 +
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="bash">
 +
 
 +
$PP -i |head
 +
Free Pascal Compiler version 2.7.1
 +
 
 +
Compiler Date      : 2012/11/02
 +
Compiler CPU Target: mipseb
 +
 
 +
Supported targets:
 +
  Linux for MIPSEB
 +
 
 +
file compiler/ppcmips
 +
compiler/ppcmips: ELF 32-bit MSB executable, MIPS, MIPS-II version 1 (SYSV), statically linked, stripped
  
I'm going to start off by trying to merge the source into the standard fpc_200/compiler and compiler/mips directories, eliminating identical files, and identifying changed ones. I could be some time.
+
file rtl/units/mips-linux/system.o
 +
rtl/units/mips-linux/system.o: ELF 32-bit MSB relocatable, MIPS, MIPS-II version 1 (SYSV), not stripped
 +
</syntaxhighlight>
  
Using cksum, these files are identical in the Chinese compiler and in fpc_200/compiler, so may be removed from the former:
+
== See also ==
 +
*[[TARGET Embedded Mipsel]]
  
rabase.pas switches.pas ncal.pas tokens.pas defutil.pas msgidx.inc
+
[[Category:FPC]]
ogcoff.pas nstate.pas comprsrc.pas nutils.pas cutils.pas nbas.pas
+
[[Category:Operating Systems and Platforms]]
verbose.pas gendef.pas msgtxt.inc defcmp.pas aopt.pas nmem.pas
+
[[Category:Microcontrollers]]
script.pas gdb.pas symutil.pas rautils.pas ncnv.pas pmodules.pas
+
[[Category:PIC32]]
pass_2.pas catch.pas pass_1.pas ncgset.pas symbase.pas psub.pas
+
[[Category:MIPS]]
impdef.pas oglx.pas aoptbase.pas symconst.pas ncgmem.pas raatt.pas
 
rgobj.pas regvars.pas fmodule.pas cp8859_1.pas import.pas cp437.pas
 
symnot.pas pbase.pas ppheap.pas aasmbase.pas pdecl.pas aoptcs.pas
 
charset.pas cgutils.pas pexpr.pas ncgcon.pas rasm.pas dwarf.pas
 
nopt.pas tgobj.pas procinfo.pas aasmtai.pas ppu.pas cg64f32.pas
 
pexports.pas ncgadd.pas pdecvar.pas browcol.pas nadd.pas compinnr.inc
 
ncgflw.pas ncgmat.pas nld.pas aggas.pas ncginl.pas ncgbas.pas
 
export.pas finput.pas owbase.pas symtype.pas owar.pas pdecsub.pas
 
cstreams.pas cp850.pas aoptda.pas ogbase.pas aoptobj.pas nflw.pas
 
browlog.pas symtable.pas crc.pas globtype.pas ptconst.pas widestr.pas
 
scandir.pas nobj.pas COPYING nset.pas node.pas ogmap.pas symsym.pas
 
ncon.pas link.pas cclasses.pas htypechk.pas ncgopt.pas comphook.pas
 
rgbase.pas nmat.pas pinline.pas ncgcnv.pas parabase.pas pdecobj.pas
 
cresstr.pas ogelf.pas cgbase.pas ptype.pas fppu.pas scanner.pas
 
parser.pas cmsgs.pas
 

Latest revision as of 07:30, 26 January 2020

The MIPS architecture is unusual in that it was designed for desktop computers, is now overwhelmingly used for embedded systems, but can still occasionally be found in desktops or small servers. The only comparable architecture is ARM, which is the platform of choice for a multitude of mobile phones, tablets etc.

Like almost all computers, MIPS-based systems will contain an internal loader, usually in flash memory. However there are three basic categories of system:

  • Those that boot an operating system from internal flash, such as the Linksys/Cisco WRT54G [1].
  • Those that boot an operating system from a conventional disc, connected via IDE, SCSI or USB, such as an SGI server or workstation, or a Cobalt/Sun Qube or Raq.
  • Those running as guests using an emulator such as Qemu on a general-purpose computer.

In any of these cases it is necessary to be able to install a general-purpose operating system such as Linux in order to be able to run development tools. This writer (MarkMLl) favours Debian, since he finds that using it results in a very similar system on a wide range of hardware (x86, SPARC, PPC, MIPS, ARM, zSeries, 68K).

If you intend to build Lazarus, make sure you have at least 512Mb memory (RAM + swap) available.

Microchip microcontrollers

In 2013, Microchip enhanced their series of "PIC24" and "PIC33" chips with the "PIC32" variants, providing compatible (to a certain extent even pin-compatible) alternatives to the 16 bit chips (PIC32MX..."), as well as much more powerful ones ("PIC32MZ..."). The PIC32 chips are both cheaper and more versatile than the corresponding PIC24s. Hence they include both cheap and powerful controllers. The PIC32 chips feature a "M4K" MIPS CPU and so they could run software done with the FPC MIPS cross compiler. As usually these chips are used for doing "deeply embedded" systems: in most cases there will be no formal OS and the user code programmer will need to adapt the RTL to fit his needs. But as the C-based "MPLAB-X" IDE by Microchip comes with a huge library that provides basic functions (such as heap management), the "standard" RTL for such purpose should take this into account.

FPC Implementations for MIPS

As of 2011 there are two implementations of FPC for the MIPS architecture, both targeting Linux.

  • A fork of the mainline compiler at 2.0.0 by David Zhang. Now deprecated due to mainline integration.
  • An ongoing effort to reconcile this with the mainline project, in SVN trunk and partially completed in 2.7.x.

David Zhang's Compiler

The "unofficial" MIPS compiler at [2] was written by David Zhang (possibly [3]) and is licensed under GPL Version 2, it cites but does not include the COPYING.FPC file. The compiler appears to have been originally based on the SPARC port, and there are significant differences between it and the mainline sources. It is discussed for historical interest, and as reference until the mainline compiler is at least as complete and robust.

There are .zip files of both sources and a minimal executable at Sourceforge (link above). The sources are basically a complete reimplementation of fpcsrc/compiler/mips based on FPC 2.0.0, the compiler targets mipsel and is described as being operational on Qemu but not yet supporting shared libraries.

Since this is specifically a little-endian compiler it should presumably be compatible with Linux on a MIPS-based Qube or Raq, or with the Chinese "Loongson" processor. It will not be compatible with SGI MIPS systems, which are big-endian.

There is a system library bundled with the compiler, but no indication of how far the compiler is capable of handling the remainder of the standard libraries. The released files may be downloaded from:

This comprises the binary compiler pp_mipsel32. It successfully compiles a minimal test program which appears to run.

This is both source and binary, dumped into a single directory which appears to correspond to the standard fpc_200/compiler, i.e. there's no separate compiler/mips directory; it also partially reimplements some standard libraries etc.

Mainline MIPS Port

This is part of the trunk development files, but does not yet appear in standard releases; i.e. it's only available via an svn download.

The host is assumed to be a non-MIPS system, for example x86 running Linux (e.g. Debian "Squeeze"). It needs a copy of binutils, and it is probably expedient to make this the same version as is running natively, i.e. 2.20.1 in the above case, downloaded from [4]. In any event, no version older than 2.18 will work since symbolic register names are a prerequisite.

Build and install cross-binutils:

tar xjf binutils-2.20.1.tar.bz2
cd binutils-2.20.1
./config.sub mipsel-linux-gnu
./configure --prefix=/usr/local/mipsel-linux mipsel-linux-gnu
make
sudo make install
sudo ln -s /usr/local/mipsel-linux/bin/as /usr/local/bin/mipsel-linux-as
sudo ln -s /usr/local/mipsel-linux/bin/ld /usr/local/bin/mipsel-linux-ld
sudo ln -s /usr/local/mipsel-linux/bin/ar /usr/local/bin/mipsel-linux-ar
sudo ln -s /usr/local/mipsel-linux/bin/objdump /usr/local/bin/mipsel-linux-objdump
sudo ln -s /usr/local/mipsel-linux/bin/objcopy /usr/local/bin/mipsel-linux-objcopy
sudo ln -s /usr/local/mipsel-linux/bin/strip /usr/local/bin/mipsel-linux-strip
mipsel-linux-ld -V

Particularly if working with the trunk sources, consider doing the same for a different CPU (e.g. powerpc-linux-gnu) so as not to be caught out if they won't build due to a temporary inconsistency.

Build a compiler which runs on the host but targets mipsel, note debug options which are needed later:

cd /usr/local/src/fpc-trunk
export PP=
make "OPT=-O- -g" -C compiler mipsel
mv compiler/ppcmipsel compiler/ppcXmipsel

Renaming the compiler prevents it from being deleted by make clean etc., consider extending that OPT setting with -dEXTDEBUG.

export PP=/usr/local/src/fpc-trunk/fpc/compiler/ppcXmipsel
make CPU_TARGET=mipsel OS_TARGET=linux rtl
make CPU_TARGET=mipsel OS_TARGET=linux compiler

Move the newly-generated compiler and the RTL units to the target system, which should now be able to do a full build.

Stall point

As of October 2012 there are still some code-generation tests failing.

To be continued (hopefully :-)

As a general point, there's some useful thoughts on binary disassembly at [5] for situations where IDA or equivalent aren't available.

Big-endian subarchitecture

Working notes, no permanent links.

This is a highly-experimental investigation of how much work it would take to port the existing little-endian compiler (mipsel) to big-endian architectures. Note that the GNU triplet is mips-linux-gnu, there is no such thing as mipseb-linux-gnu etc.

Build and install cross-binutils:

tar xjf binutils-2.20.1.tar.bz2
cd binutils-2.20.1
./config.sub mips-linux-gnu
./configure --prefix=/usr/local/mips-linux mips-linux-gnu
make
sudo make install
sudo ln -s /usr/local/mips-linux/bin/as /usr/local/bin/mips-linux-as
sudo ln -s /usr/local/mips-linux/bin/ld /usr/local/bin/mips-linux-ld
sudo ln -s /usr/local/mips-linux/bin/ar /usr/local/bin/mips-linux-ar
sudo ln -s /usr/local/mips-linux/bin/objdump /usr/local/bin/mips-linux-objdump
sudo ln -s /usr/local/mips-linux/bin/objcopy /usr/local/bin/mips-linux-objcopy
sudo ln -s /usr/local/mips-linux/bin/strip /usr/local/bin/mips-linux-strip
mips-linux-ld -V

Build a compiler which runs on the host but targets mipseb, note debug options which are needed later:

cd /usr/local/src/fpc-trunk
export PP=
make "OPT=-O- -g" -C compiler mips
mv compiler/ppcmips compiler/ppcXmips

For my own reference, actually using /usr/local/src/fpc/zhang/fpc-trunk/fpc

export PP=/usr/local/src/fpc-trunk/fpc/compiler/ppcXmips
make CPU_TARGET=mips OS_TARGET=linux rtl
# make CPU_TARGET=mips OS_TARGET=linux compiler

Build a compiler which runs on the target:

export PP=/usr/local/src/fpc-trunk/fpc/compiler/ppcXmips
make "OPT=-O- -g" -C compiler mips
$PP -i |head
Free Pascal Compiler version 2.7.1

Compiler Date      : 2012/11/02
Compiler CPU Target: mipseb

Supported targets:
  Linux for MIPSEB

file compiler/ppcmips
compiler/ppcmips: ELF 32-bit MSB executable, MIPS, MIPS-II version 1 (SYSV), statically linked, stripped

file rtl/units/mips-linux/system.o
rtl/units/mips-linux/system.o: ELF 32-bit MSB relocatable, MIPS, MIPS-II version 1 (SYSV), not stripped

See also