package hardcaml

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file parameter.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
module Unstable = struct
  open! Base

  module Std_logic = struct
    type t =
      | U
      | X
      | L0
      | L1
      | Z
      | W
      | L
      | H
      | Don't_care
    [@@deriving compare, enumerate, sexp, variants]
  end

  module Std_logic_vector = struct
    type t = Std_logic.t list [@@deriving compare, sexp]
  end

  module Bit_vector = struct
    type t = bool list [@@deriving compare, sexp]
  end

  module Value = struct
    type t =
      | Bit of bool
      | Bit_vector of Bit_vector.t
      | Bool of bool
      | Int of int
      | Real of float
      | Std_logic of Std_logic.t
      | Std_logic_vector of Std_logic_vector.t
      | Std_ulogic of Std_logic.t
      | Std_ulogic_vector of Std_logic_vector.t
      | String of string
    [@@deriving compare, sexp, variants]
  end

  module T = struct
    type t =
      { name : Parameter_name.t
      ; value : Value.t
      }
    [@@deriving compare, sexp]
  end

  include T
end

open! Import

module Std_logic = struct
  module Unstable = Unstable.Std_logic
  include Unstable

  let equal = [%compare.equal: t]

  let to_char = function
    | U -> 'U'
    | X -> 'X'
    | L0 -> '0'
    | L1 -> '1'
    | Z -> 'Z'
    | W -> 'W'
    | L -> 'L'
    | H -> 'H'
    | Don't_care -> '_'
  ;;

  let sexp_of_t t = [%sexp (to_char t : char)]
  let to_int = Variants.to_rank

  let of_char_exn = function
    | 'U' | 'u' -> U
    | 'X' | 'x' -> X
    | '0' -> L0
    | '1' -> L1
    | 'Z' | 'z' -> Z
    | 'W' | 'w' -> W
    | 'L' | 'l' -> L
    | 'H' | 'h' -> H
    | '_' -> Don't_care
    | _ as char ->
      raise_s [%message "[Std_logic.of_char_exn] got invalid char" (char : char)]
  ;;
end

module Std_logic_vector = struct
  module Unstable = Unstable.Std_logic_vector
  include Unstable

  let equal = [%compare.equal: t]
  let to_string v = v |> List.map ~f:Std_logic.to_char |> String.of_char_list
  let of_string s = s |> String.to_list |> List.map ~f:Std_logic.of_char_exn
  let sexp_of_t t = [%sexp (to_string t : string)]
  let create x = x
  let width x = List.length x
  let of_bits b = Bits.to_bstr b |> of_string
end

module Bit_vector = struct
  module Unstable = Unstable.Bit_vector
  include Unstable

  let equal = [%compare.equal: t]

  let to_string v =
    v
    |> List.map ~f:(function
      | true -> '1'
      | false -> '0')
    |> String.of_char_list
  ;;

  let of_string s =
    s
    |> String.to_list
    |> List.map ~f:(function
      | '0' -> false
      | '1' -> true
      | _ as char ->
        raise_s [%message "[Bit_vector.of_string] got invalid char" (char : char)])
  ;;

  let sexp_of_t t = [%sexp (to_string t : string)]
  let create x = x
  let width x = List.length x
  let of_bits b = Bits.to_bstr b |> of_string
end

module Value = struct
  module Unstable = Unstable.Value
  include Unstable

  let equal = [%compare.equal: t]

  let sexp_of_t = function
    | Bit b -> [%sexp (b : bool)]
    | Bit_vector v -> [%sexp (v : Bit_vector.t)]
    | Bool b -> [%sexp (b : bool)]
    | Int i -> [%sexp (i : int)]
    | Real f -> [%sexp (f : float)]
    | Std_logic x -> [%sexp (x : Std_logic.t)]
    | Std_logic_vector x -> [%sexp (x : Std_logic_vector.t)]
    | Std_ulogic x -> [%sexp (x : Std_logic.t)]
    | Std_ulogic_vector x -> [%sexp (x : Std_logic_vector.t)]
    | String s -> [%sexp (s : string)]
  ;;
end

include Unstable.T

let sexp_of_t { name; value } =
  [%message "" ~_:(name : Parameter_name.t) ~_:(value : Value.t)]
;;

let equal = [%compare.equal: t]
let create ~name ~value = { name = name |> Parameter_name.of_string; value }

let find_name ts name =
  List.find_map ts ~f:(fun t ->
    if Parameter_name.equal t.name name then Some t.value else None)
;;

let find_name_exn ts name =
  match find_name ts name with
  | Some x -> x
  | None ->
    raise_s
      [%message
        "couldn't find parameter" (name : Parameter_name.t) ~parameters:(ts : t list)]
;;

let is_subset ts1 ts2 =
  List.for_all ts1 ~f:(fun t1 ->
    match find_name ts2 t1.name with
    | Some v2 -> Value.equal t1.value v2
    | None -> false)
;;

let sort_by_name ts =
  List.sort ts ~compare:(fun t1 t2 -> Parameter_name.compare t1.name t2.name)
;;
OCaml

Innovation. Community. Security.