FPGA開発日記

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

Privileged命令の実装 (命令フェッチミスアラインの動作)

かなりのパタンが動作するようになってきた。

github.com

ma_fetchというパタンは、フェッチ時にミスアラインが発生したときの対処をチェックするテスト項目だ。

RISC-Vのアーキテクチャでは、JALRのジャンプ先アドレスのLSBは無視するような仕様になっている。これはハードウェアを簡単化するための仕様だそうだ。

The JALR instruction ignores the lowest bit of the calculated target address. This both
simplies the hardware slightly and allows the low bit of function pointers to be used to store
auxiliary information. Although there is potentially a slight loss of error checking in this case,
in practice jumps to an incorrect instruction address will usually quickly raise an exception.

そして、JALRでのジャンプ先アドレスミスアラインが発生したときには、mepcに発生した命令のアドレス(つまり、JALRが置かれているアドレス)そして、mbadaddrに、例外を起こした飛び先アドレスが配置されるようになっている。

この修正を実施して、再度リグレッションを実施してみた。

$ for hex in `ls -1 rv32si-p-*.hex`;do echo -n ${hex}; swimmer_riscv --hexfile ${hex} --debug  --max 1000 | grep tohost; done
rv32si-p-csr.hex       156 : [00000184] 780e1073 : csrrw      r00,0x780,r28        mtohost=>00000000 r28=>00000019 mtohost<=00000019
rv32si-p-illegal.hex        48 : [00000184] 780e1073 : csrrw      r00,0x780,r28        mtohost=>00000000 r28=>00000001 mtohost<=00000001
rv32si-p-ma_addr.hex       245 : [00000184] 780e1073 : csrrw      r00,0x780,r28        mtohost=>00000000 r28=>00000001 mtohost<=00000001
rv32si-p-ma_fetch.hex        94 : [00000184] 780e1073 : csrrw      r00,0x780,r28        mtohost=>00000000 r28=>00000001 mtohost<=00000001
rv32si-p-sbreak.hex        29 : [00000184] 780e1073 : csrrw      r00,0x780,r28        mtohost=>00000000 r28=>00000005 mtohost<=00000005
rv32si-p-scall.hex        46 : [00000184] 780e1073 : csrrw      r00,0x780,r28        mtohost=>00000000 r28=>00000001 mtohost<=00000001
rv32si-p-shamt.hex        53 : [00000184] 780e1073 : csrrw      r00,0x780,r28        mtohost=>00000000 r28=>00000001 mtohost<=00000001

かなり通るようになってきた!