RiscV-Formal/hs/Fetch.hs

25 lines
651 B
Haskell
Raw Normal View History

2025-02-13 04:54:15 +00:00
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE NumericUnderscores #-}
module Fetch(fetchInstruction) where
import Clash.Prelude
2025-02-19 06:21:02 +00:00
import Types(Mem, Addr, Insn)
2025-02-13 04:54:15 +00:00
import Util(endianSwapWord)
2025-02-19 06:21:02 +00:00
data FetchResult = Instruction Insn
| Misaligned Addr
2025-02-13 04:54:15 +00:00
2025-02-19 06:21:02 +00:00
fetchInstruction :: KnownNat n => Mem n -> Addr -> FetchResult
2025-02-13 04:54:15 +00:00
fetchInstruction mem addr =
let
isWordAligned = addr .&. 3 == 0
addrWordAligned = addr `shiftR` 2
insn = mem !! addrWordAligned
-- TODO : check if instruction is word aligned and create type
-- to capture if its not.
in
case isWordAligned of
True -> Instruction insn
False -> Misaligned addr