LoginSignup
8
8

More than 5 years have passed since last update.

PDP-11(エミュ)上でCで"Hello, World!"

Last updated at Posted at 2014-12-28

PDP-11(エミュ)上で動く、短いHello WorldのCのプログラムを書いてみた。
短いとはいえ、無理に一個関数とかにしない。普通のリーダブルコードとして書く。

ソースコード

hello.c
#define KL11_TTY_REG_ADDRESS 0177564;
#define TXRDY (1<<7)

static volatile struct kl11_tty {
  int out_csr;
  int out_dbr;
} *pkl11 = (struct kl11_tty*)KL11_TTY_REG_ADDRESS;

int putchar(int c)
{
  while(!(pkl11->out_csr & TXRDY))
    ;
  pkl11->out_dbr = c;
}
int puts(unsigned char *s)
{
  while(*s)
    putchar(*s++);
  return 0;
}
int cstart()
{
  puts("Hello, World!\n\r");
  while(1);
}
start.S
    .GLOBAL start
    .GLOBAL _cstart

    .text
start:
    mov     $0x1000, sp
    jsr     pc, _cstart
    .end           
ld.scr
ENTRY(start)
SECTIONS
{
   .  = 01000;
   .text : { *(.text); . = ALIGN(0100); }
   .data : { *(.data); . = ALIGN(0100); }
   .bss :  { *(.bss);  . = ALIGN(0100); }
}
Makefile
all:
    pdp11-aout-gcc -c hello.c -o hello.o
    pdp11-aout-as start.S -o start.o
    pdp11-aout-ld -T ld.scr start.o hello.o -o hello.out
    bin2load -a -f hello.out -o hello.lda

clean:
    rm *.lda
    rm *.out
    rm *.o

ビルド方法

ビルドには下記ツールが必要。

  • pdp-11のgccクロスコンパイラ環境
  • bin2loadというバイナリ変換ツール

ソースは hello.c, start.S, ld.scr の3つある。
Makefileも一緒のディレクトリに入れた上で、以下のようにビルドする。

$ make

実行方法

実行にはsimhというpdp11エミュレータが必要。

$ pdp11

PDP-11 simulator V3.9-0
sim> l hello.lda
sim> g
Hello, World!

プログラムで最後無限ループするようにしているので、終わるにはCtrl-eで実行をぬける。

必要なツール環境

(1) PDP-11のgccクロス環境構築
http://qiita.com/hifistar/items/187fd7ad780c6aa26141

(2) bin2loadは以下から
※簡単に言えば本記事のhello worldのアセンブラ版をCにしてみたのだった。
The Ancient Bits adventure: Writing PDP11 assembly code from Linux (and running it on bare -simulated- metal!) 
上記記事内だと、bin2loadのソースの場所は分かりにくいが、実際はGitHub( https://github.com/jguillaumes/retroutils)にある。

(3) simh
http://simh.trailing-edge.com/

以上。

8
8
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
8
8