FPGA開発日記

カテゴリ別記事インデックス https://msyksphinz.github.io/github_pages , English Version https://fpgadevdiary.hatenadiary.com/

「30日でできる!OS自作入門」を読み始めた (3. 4日目)

遅まきながら、「30日でできるOS自作入門」を読み始めた。

30日でできる! OS自作入門

30日でできる! OS自作入門

4日目の環境を構築するのがとても疲れた。

あまり著者のツールを使いたくないので、nasmgccを残しつつ、自分でMakefileを作って環境を構築してたのだがこれが疲れる。

edimgとか、よくわからないツールを使わないでほしいな...

結局、以下のようなMakefileを作って環境を構築した。使っているのはnasm, gcc, そして最後はddを使って無理やりイメージを連結している。

github.com

TARGET = haribote


run: $(TARGET).img
  qemu-system-i386 -fda $^

$(TARGET).img: ipl10.bin $(TARGET).sys
  echo $(TARGET).sys > $(TARGET).name
  dd if=ipl10.bin     of=$(TARGET).img count=2880 bs=512 conv=notrunc
  dd if=$(TARGET).name of=$(TARGET).img count=1 bs=512 seek=19 conv=notrunc
  dd if=$(TARGET).sys  of=$(TARGET).img count=4 bs=512 seek=33 conv=notrunc


%.o:%.c
  gcc -m32 -c -fno-pic -nostdlib -o $@ $<
  objdump -D $@ > $@.dmp

%.bin: %.oT
  objcopy -O binary $^ $@ 

asmhead.o: asmhead.nas
  nasm $^ -o $@ -l $^.lst

naskfunc.o: naskfunc.nas
  nasm -felf32 $^ -o $@ -l $^.lst

$(TARGET).bin: bootpack.o naskfunc.o 
  ld -m elf_i386 -e HariMain -n -Thrb.ld -static -o $(TARGET).bin $^
  hexdump -C $(TARGET).bin > $(TARGET).bin.dmp

$(TARGET).sys: asmhead.o $(TARGET).bin
  cat $^ > $@

ipl10.bin: ipl10.nas
  nasm -fbin -o $@ $^ -l ipl10.lst

diff: $(TARGET).diff haribote_reference.diff
  diff -y -W200 $^

%.diff: %.img
  hexdump -C $^ > $@

clean:
  rm -rf *.sys $(TARGET).img *.o *.bin *.lst *.name *~

とりあえず、最初のシマシマが作れた。

f:id:msyksphinz:20180301230756p:plain
図. gas / nasmを使ってHaribote-OSの画面操作プログラムを動作させる。

この時に役に立ったのが、オリジナルの環境 (Windowsでビルド+edimgなどの内製ツールを使用)と、hexdumpを使ってのバイナリの比較だ。 この程度の大きさであれば、どの部分が異なるのかだいたい分かってくる。

make diff
diff -y -W200 haribote.diff haribote_reference.diff
00000000  eb 4e 90 48 41 52 49 42  4f 54 45 00 02 01 01 00  |.N.HARIBOTE.....|                          00000000  eb 4e 90 48 41 52 49 42  4f 54 45 00 02 01 01 00  |.N.HARIBOTE.....|
00000010  02 e0 00 40 0b f0 09 00  12 00 02 00 00 00 00 00  |...@............|                          00000010  02 e0 00 40 0b f0 09 00  12 00 02 00 00 00 00 00  |...@............|
00000020  40 0b 00 00 00 00 29 ff  ff ff ff 48 41 52 49 42  |@.....)....HARIB|                          00000020  40 0b 00 00 00 00 29 ff  ff ff ff 48 41 52 49 42  |@.....)....HARIB|
00000030  4f 54 45 4f 53 20 46 41  54 31 32 20 20 20 00 00  |OTEOS FAT12   ..|                          00000030  4f 54 45 4f 53 20 46 41  54 31 32 20 20 20 00 00  |OTEOS FAT12   ..|
00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|                          00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000050  b8 00 00 8e d0 bc 00 7c  8e d8 b8 20 08 8e c0 b5  |.......|... ....|                          00000050  b8 00 00 8e d0 bc 00 7c  8e d8 b8 20 08 8e c0 b5  |.......|... ....|
00000060  00 b6 00 b1 02 be 00 00  b4 02 b0 01 bb 00 00 b2  |................|                          00000060  00 b6 00 b1 02 be 00 00  b4 02 b0 01 bb 00 00 b2  |................|
00000070  00 cd 13 73 10 83 c6 01  83 fe 05 73 32 b4 00 b2  |...s.......s2...|                          00000070  00 cd 13 73 10 83 c6 01  83 fe 05 73 32 b4 00 b2  |...s.......s2...|
00000080  00 cd 13 eb e3 8c c0 83  c0 20 8e c0 80 c1 01 80  |......... ......|                     |    00000080  00 cd 13 eb e3 8c c0 05  20 00 8e c0 80 c1 01 80  |........ .......|
00000090  f9 12 76 d1 b1 01 80 c6  01 80 fe 02 72 c7 b6 00  |..v.........r...|                          00000090  f9 12 76 d1 b1 01 80 c6  01 80 fe 02 72 c7 b6 00  |..v.........r...|
000000a0  80 c5 01 80 fd 0a 72 bd  88 2e f0 0f e9 51 45 be  |......r......QE.|                          000000a0  80 c5 01 80 fd 0a 72 bd  88 2e f0 0f e9 51 45 be  |......r......QE.|
000000b0  c7 7c 8a 04 83 c6 01 3c  00 74 09 b4 0e bb 0f 00  |.|.....<.t......|                          000000b0  c7 7c 8a 04 83 c6 01 3c  00 74 09 b4 0e bb 0f 00  |.|.....<.t......|
000000c0  cd 10 eb ee f4 eb fd 0a  0a 6c 6f 61 64 20 65 72  |.........load er|                          000000c0  cd 10 eb ee f4 eb fd 0a  0a 6c 6f 61 64 20 65 72  |.........load er|
000000d0  72 6f 72 0a 00 00 00 00  00 00 00 00 00 00 00 00  |ror.............|                          000000d0  72 6f 72 0a 00 00 00 00  00 00 00 00 00 00 00 00  |ror.............|
000000e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|                          000000e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*                                                                                                       *
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|                          000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200  f0 ff ff ff 0f 00 00 00  00 00 00 00 00 00 00 00  |................|                          00000200  f0 ff ff ff 0f 00 00 00  00 00 00 00 00 00 00 00  |................|
00000210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|                          00000210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*                                                                                                       *
00001400  f0 ff ff ff 0f 00 00 00  00 00 00 00 00 00 00 00  |................|                          00001400  f0 ff ff ff 0f 00 00 00  00 00 00 00 00 00 00 00  |................|
00001410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|                          00001410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*                                                                                                       *
00002600  68 61 72 69 62 6f 74 65  2e 73 79 73 0a 00 00 00  |haribote.sys....|                     |    00002600  48 41 52 49 42 4f 54 45  53 59 53 20 00 00 00 00  |HARIBOTESYS ....|
00002610  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|                     |    00002610  00 00 00 00 00 00 a5 6b  5c 4c 02 00 90 01 00 00  |.......k\L......|
                                                                                                   >    00002620  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*                                                                                                       *
00004200  b0 13 b4 00 cd 10 c6 06  f2 0f 08 c7 06 f4 0f 40  |...............@|                          00004200  b0 13 b4 00 cd 10 c6 06  f2 0f 08 c7 06 f4 0f 40  |...............@|
00004210  01 c7 06 f6 0f c8 00 66  c7 06 f8 0f 00 00 0a 00  |.......f........|                          00004210  01 c7 06 f6 0f c8 00 66  c7 06 f8 0f 00 00 0a 00  |.......f........|
00004220  b4 02 cd 16 a2 f1 0f b0  ff e6 21 90 e6 a1 fa e8  |..........!.....|                          00004220  b4 02 cd 16 a2 f1 0f b0  ff e6 21 90 e6 a1 fa e8  |..........!.....|
00004230  b5 00 b0 d1 e6 64 e8 ae  00 b0 df e6 60 e8 a7 00  |.....d......`...|                          00004230  b5 00 b0 d1 e6 64 e8 ae  00 b0 df e6 60 e8 a7 00  |.....d......`...|
00004240  0f 01 16 2a c3 0f 20 c0  66 25 ff ff ff 7f 66 83  |...*.. .f%....f.|                          00004240  0f 01 16 2a c3 0f 20 c0  66 25 ff ff ff 7f 66 83  |...*.. .f%....f.|
00004250  c8 01 0f 22 c0 eb 00 b8  08 00 8e d8 8e c0 8e e0  |..."............|                          00004250  c8 01 0f 22 c0 eb 00 b8  08 00 8e d8 8e c0 8e e0  |..."............|
00004260  8e e8 8e d0 66 be 30 c3  00 00 66 bf 00 00 28 00  |....f.0...f...(.|                          00004260  8e e8 8e d0 66 be 30 c3  00 00 66 bf 00 00 28 00  |....f.0...f...(.|
00004270  66 b9 00 00 02 00 e8 75  00 66 be 00 7c 00 00 66  |f......u.f..|..f|                          00004270  66 b9 00 00 02 00 e8 75  00 66 be 00 7c 00 00 66  |f......u.f..|..f|
00004280  bf 00 00 10 00 66 b9 80  00 00 00 e8 60 00 66 be  |.....f......`.f.|                          00004280  bf 00 00 10 00 66 b9 80  00 00 00 e8 60 00 66 be  |.....f......`.f.|

さらに進めていく。色テーブルを使って画面を表示させた。

  • day04/haribote-001
f:id:msyksphinz:20180301233922p:plain
図. カラーパレットを使ってシマシマ模様を描く
  • day04/haribote-002
f:id:msyksphinz:20180301232427p:plain
図. 四角形を書いてみる。
  • day04/haribote-003
f:id:msyksphinz:20180301233828p:plain
図. ウィンドウを書いてみる