書籍「はじめて読む486」のLinux環境構築
TLDR
QEMUの設定
sudo apt update
sudo apt install qemu-system-i386
sudo wget https://archive.org/download/FD12CD/FD12CD.iso
qemu-img create -f raw freedos.img 300M
qemu-system-i386 -cpu 486 -drive file=freedos.img,format=raw -cdrom FD12CD.iso -boot d -nographic
openwatcomの設定
wget https://openwatcom.org/ftp/install/open-watcom-c-dos-1.9.exe
sudo mkdir /mnt/freedos20250424
sudo mount -t msdos -o loop,offset=32256 freedos.img /mnt/freedos20250424
sudo qemu-system-i386 -cpu 486 -drive file=freedos.img,format=raw -display curses
QEMU内でのowccのインストール
owcc
QEMU内でのowccの設定
call C:\WATCOM\OWSETENV.BAT
はじめに
「はじめて読む486」を最近読んでいるのですが、Linuxの環境構築をしてみることにしました。このLinuxマシンには自分のホストマシンからSSHして入って操作しています。
参考:
インストールの手順
環境
Ubuntu
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.2 LTS
Release: 24.04
Codename: noble
CPU
$ uname -a
Linux vm 6.8.0-1024-aws #26-Ubuntu SMP Tue Feb 18 17:22:37 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Qemuのインストール
sudo apt update
sudo apt install qemu-system-i386
FreeDOSのインストール
ここに直接アクセスして、ダウンロードしました。
以下のコマンドでもいけます
sudo wget https://archive.org/download/FD12CD/FD12CD.iso
オリジナル記事のこのリンクはもうなくなっていました
イメージの作成
qemu-img create -f raw freedos.img 300M
FreeDOS 1.2のインストール
qemu-system-i386 -cpu 486 -drive file=freedos.img,format=raw -cdrom FD12CD.iso -boot d -nographic
以下だとエラーが起きます
$ qemu-system-x86_64 -cpu 486 -drive file=freedos.img,format=raw -cdrom FD12CD.iso -boot d
[1] 37244 segmentation fault qemu-system-x86_64 -cpu 486 -drive file=freedos.img,format=raw -cdrom -boot
以下のように質問がどんどん来ます。オリジナル記事に書いてあるようにこちらで進みます。
"Welcome to FreeDOS 1.2" で [Install to harddisk] を選択
"What is your preferred language?" で [English] を選択
"Do you want to proceed?" で [Yes - Continue with the installation] を選択
"Do you want to partition your drive?" で [Yes - Partition drive C:] を選択
"Do you want to use large disk (FAT32) support (Y/N)?" で [Y] を選択
"Enter choice" で [1] を選択 (Create DOS partition or Logical DOS Drive)
"Enter choice" で [1] を選択 (Create Primary DOS Partition)
"Do you wish to use the maximum available size for a Primary DOS Partition and make the partition active (Y/N)?" で [Y] を選択
Esc を 3 回押下
"Do you want to reboot now?" で [Yes - Please reboot now] を選択
"Welcome to FreeDOS 1.2" で [Install to harddisk now] を選択
"What is your preferred language?" で [English] を選択
"Welcome to the FreeDOS 1.2 installation program" で [Yes 1. Continue with the installation] を選択
"Drive C: does not appear to be formated." で [Yes - Please erase and format drive C:] を選択
"Formatting drive C:..." で Enter を押下
"Please select your keyboard layout." で [US English (Default)] を選択
"What FreeDOS packages do you want to install?" で [Full installation] を選択
"Do you want to install now?" で [Yes - Please install FreeDOS 1.2] を選択
"Installation of FreeDOS 1.2 is now complete." が出ればインストール成功
一旦、ウィンドウを閉じる (マウスカーソルが表示されていなくなっていたら、Ctrl + Alt + G で復活します。)
FreeDOS を起動
そこからはひたすら待ちます。(自分の画面が黒いからかちょっと読みづらい。。。)
ようやく終わりました。
Installation of FreeDOS is not complete
これで毎回以下のコマンドで入ることができます
sudo qemu-system-i386 -cpu 486 -drive file=freedos.img,format=raw -display curses
-no-graphicを設定するとowccのインストールがうまくいきません。なのでcursesを指定しています。
FreeDOSであることは以下のように確認できる
> ver
FreeCom version 0.84-pre2 XMS_Swap [Aug 28 2006 00:29:00]
openwatcomのインストール
wget https://openwatcom.org/ftp/install/open-watcom-c-dos-1.9.exe
sudo mkdir /mnt/freedos20250424
sudo mount -t msdos -o loop,offset=32256 freedos.img /mnt/freedos20250424
これで見てみると共有されていることがわかる。
$ ls /mnt/freedos20250424
autoexec.bat command.com fdconfig.sys fdos kernel.sys
sudo cp open-watcom-c-dos-1.9.exe /mnt/freedos20250424/owcc.exe
# Unmount and detach loop device
sudo umount /mnt/freedos20250424
どうして32256かというと:
1 セクタのサイズが 512 bytes, デバイスの開始セクタが 63 なので 63 * 512 = 32256 ということになります。
$ fdisk -l freedos.img
Disk freedos.img: 200 MiB, 209715200 bytes, 409600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000
Device Boot Start End Sectors Size Id Type
freedos.img1 * 63 409247 409185 199.8M 6 FAT16
これでもう一回QEMUを起動し、owccを入力します。
tab + spaceで基本操作できます。
こちらのYoutube動画が非常に役に立ちます。
owcc の設定
> call C:\WATCOM\OWSETENV.BAT
Open Watcom Build Environment
これでwmake, wclなどのコマンドを使えるようになります。
C:\>wcl hello.c
C:\>wasm proto_a.asm
アウトプット
Open Watcom C/C++16 Compile and Link Utility Version 1.9
Portions Copyright (c) 1988-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
wcc HELLO.C
DOS/4GW Protected Mode Run-time Version 1.97
Copyright (c) Rational Systems, Inc. 1990-1994
Open Watcom C16 Optimizing Compiler Version 1.9
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
HELLO.C: 7 lines, included 771, 0 warnings, 0 errors
Code size: 17
wlink @__wcl__.lnk
DOS/4GW Protected Mode Run-time Version 1.97
Copyright (c) Rational Systems, Inc. 1990-1994
Open Watcom Linker Version 1.9
Portions Copyright (c) 1985-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
loading object files
searching libraries
creating a DOS executable
ここまで長かった。。。
C:\>hello
hello world
Cコードを走らせてみる。
Sieveコード
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define NUM 10000
void main(int argc, char *argv[]) {
unsigned char *tbl;
unsigned int m, i, snum;
void sieve(unsigned char *tbl, unsigned int num);
int isprime(unsigned char *tbl, unsigned int num);
if (argc > 1) {
m = atol(argv[1]);
} else {
m = NUM;
}
printf("%d", m);
tbl = (char *) malloc(m + 1);
if (tbl == NULL){
fprintf(stderr, "cannot allocate memory\n");
exit(1);
}
sieve(tbl, m);
snum = 0;
for (i=2; i<=m; i++) {
if (isprime(tbl, i)) {
snum++;
printf("%u ", i);
}
}
putchar('\n');
printf("%u prime numbers in 2..%u\n", snum, m);
}
void sieve(unsigned char *tbl, unsigned int num) {
unsigned long i, j;
for (i = 0; i <= num; i++) {
tbl[i] = 1;
}
for (i = 2; i <= num; i++) {
if (tbl[i] == 1) {
for (j = i + i; j <= num; j += i) {
tbl[j] = 0;
}
}
}
}
int isprime(unsigned char *tbl, unsigned int num) {
return tbl[num];
}
転送
sudo mount -t msdos -o loop,offset=32256 freedos.img /mnt/freedos20250424
sudo cp sieve_c.c /mnt/freedos20250424
sudo umount /mnt/freedos20250424
MS DOS内で走らせる
61701まではうまくいく。
C:\>sieve_c 10
2 3 5 7
4 prime numbers in 2..10
64000以上だと失敗
C:\>sieve_c 64000
cannot allocate memory
-1536
M1 Macではなぜかうまくいかなかった
QEMUのイメージ作成、構築段階で失敗。Segmentationエラーが出まくった。
Discussion