0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

懐かしい日立 SH-4 CPU の開発環境をUbuntu上に用意する

Last updated at Posted at 2025-04-25

準備

  • Ubuntu環境がない場合は、WSL もしくは VMWare WorkStation / Player のようなものに Ubuntu24.04 LTSを入れます。(入れ方の詳細は割愛。ぐぐってね)
  • Ubuntu環境がある場合は、そのまま以下をどうぞ。

sh4用の gccを入れます。

# apt install gcc-13-sh4-linux-gnu

もしかしたら環境によっては gcc-sh4-linux-gnu でも入るかも。
(gccのVersionは 13もしくは14の好きなほうで。)

sh4 CPUのエミュレータを入れます。

# apt install qemu-user-static
  • これはsh4 のみならず、以下のCPUエミュレータを全部含みます。
    arm64/32
    alpha
    cris
    hexagon
    hppa
    loongarch64
    m68k
    mips64/32
    microblaze
    ppc
    riscv64/32
    s390x
    sh4/sh4eb
    sparc
    xtensa

適当なソースコードを用意します。

hello.c
#include <stdio.h>
int main(){
  printf("hello \n");return 0;
}

コンパイルと実行

$ sh4-linux-gnu-gcc hello.c -static 
$ ./a.out
hello
  • Linux上でsh4のelfがそのまま実行できるのか、というと、実はbinfmt_misc のおかげなんですね。
  • 実際には qemu-sh4-static (ビッグエンディアンの場合は、qemu-sh4eb-static) というエミュレータが sh4 elfを読み込んでエミュレートしています。
  • つまり、実際には、Linux側で、 ./a.out のELFヘッダーを解釈したのち、
$ qemu-sh4-static ./a.out
もしくは
$ qemu-sh4eb-static ./a.out (ビッグエンディアンの場合)

が実行されているわけです。
(PythonやPerlで書かれたスクリプトが、先頭の #! の後ろに書かれたインタプリタを起動して間接的に実行可能になるのに少し似ていますが、ELFヘッダーを解釈して判別している、というのが肝です。)

  • 実行ファイルのファイル名は、gccの出力オプションを省略した場合 a.out というファイル名になりますが、中身は sh4 elf32 形式です。

  • QEMUは、バイナリートランスレータ方式とは言え、一種のエミュレータなので、実行速度は遅いです。(簡単なベンチマークで、おおよそ 1:12 程度でした。)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?