blarney
Copyright(c) Matthew Naylor 2019
LicenseMIT
Maintainermattfn@gmail.com
Stabilityexperimental
Safe HaskellSafe-Inferred

Blarney.BitScan

Description

Dynamically typed pattern matching on bit-strings, similar to scanf. Here's an instruction decoder for a tiny subset of RISC-V, written using the match and ==> combinators provided by the module:

-- Semantics of add instruction
add :: Bit 5 -> Bit 5 -> Bit 5 -> Action ()
add rs2 rs1 rd = display "add " rd ", " rs1 ", " rs2

-- Semantics of addi instruction
addi :: Bit 12 -> Bit 5 -> Bit 5 -> Action ()
addi imm rs1 rd = display "addi " rd ", " rs1 ", " imm

-- Semantics of store instruciton
sw :: Bit 12 -> Bit 5 -> Bit 5 -> Action ()
sw imm rs2 rs1 = display "sw " rs2 ", " rs1 "[" imm "]"

top :: Action ()
top = do
  let instr :: Bit 32 = 0b1000000_00001_00010_010_00001_0100011

  match instr
    [
      "0000000   rs2[4:0]  rs1[4:0] 000 rd[4:0]  0110011" ==> add,
      "          imm[11:0] rs1[4:0] 000 rd[4:0]  0010011" ==> addi,
      "imm[11:5] rs2[4:0]  rs1[4:0] 010 imm[4:0] 0100011" ==> sw
    ]

  finish
Synopsis

Documentation

(==>) :: RHS rhs => String -> rhs -> Alt infix 1 Source #

Infix operator to construct a match alternative

data Alt Source #

Match alternative

match :: KnownNat n => Bit n -> [Alt] -> Action () Source #

Match statement, with a subject and a list of alternatives

matchDefault :: KnownNat n => Bit n -> [Alt] -> Action () -> Action () Source #

Match statement, with a default case

matchOpts :: KnownNat n => MatchOpts -> Bit n -> [Alt] -> Action () Source #

General parameterised match statement

data MatchOpts Source #

Match options

matchMap :: (KnownNat n, Ord tag) => Bool -> [(String, tag)] -> Bit n -> (TagMap tag, FieldMap) Source #

Compute tag and field maps given list of pattern/tag pairs. This is a relaxed version which fills field gaps with zero, and sign-extends each field to the length of the longest instance of that field.

type TagMap tag = Map tag (Bit 1) Source #

Mapping from tag names to hot bits

type Tag a = (Ord a, Enum a, Bounded a) Source #

Common constraint of tags in a TagMap

packTagMap :: (KnownNat n, Tag tag) => TagMap tag -> Bit n Source #

Pack the tag map into a bit vector

type FieldMap = Map String (Option BitList) Source #

Mapping from field names to optional bit lists

matchSel :: KnownNat n => [(String, tag)] -> SelMap n Source #

Compute a field selector function for each field that occupies precisely the same bits in every match alternative in which it occurs

type SelMap n = Map String (Bit n -> BitList) Source #

Mapping from field names to field selectors

hasBitField :: FieldMap -> String -> Bit 1 Source #

Check if given field is active.

getBitField :: KnownNat n => FieldMap -> String -> Option (Bit n) Source #

Get field value from a map, and cast to required size using truncation or sign-extension.

getBitFieldStrict :: KnownNat n => FieldMap -> String -> Option (Bit n) Source #

Get field value from a map, and raise an error the size is incorrect

getBitFieldSel :: (KnownNat n, KnownNat m) => SelMap n -> String -> Bit n -> Bit m Source #

Get field selector function from a selector map.

makeFieldSelector :: (KnownNat n, KnownNat m) => [(String, tag)] -> String -> Bit n -> Bit m Source #

Make field selector function from a list of alternatives.