package hardcaml_step_testbench

  1. Overview
  2. Docs

Source file component.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
open! Import
include Component_intf

type ('i, 'o) t_module = (module S with type Input.t = 'i and type Output.t = 'o)
type ('i, 'o) t_ = ('i, 'o) t_module
type ('i, 'o) t = T of ('i, 'o) t_

let create t = T t

let create_combinational (type i o) ((module T) : (i, o) Combinational.t) =
  create
    (module struct
      include T

      let update_state _ _ = ()
    end)
;;

let input_module (type i o) (T (module T) : (i, o) t) : i Data.t = (module T.Input)
let output_module (type i o) (T (module T) : (i, o) t) : o Data.t = (module T.Output)
let sexp_of_input (type i o) (T (module T) : (i, o) t) = T.Input.sexp_of_t
let sexp_of_output (type i o) (T (module T) : (i, o) t) = T.Output.sexp_of_t

let sexp_of_t (type i o) _ _ (T (module T) : (i, o) t) =
  [%message "" ~_:(T.created_at : Source_code_position.t) ~_:(T.t : T.t)]
;;

let output (type i o) (T (module T) : (i, o) t) input = T.(output t) input
let update_state (type i o) (T (module T) : (i, o) t) input = T.(update_state t) input

let run_with_inputs t is =
  List.fold is ~init:[] ~f:(fun os i ->
    update_state t i;
    (i, output t i) :: os)
  |> List.rev
;;

module Next_input = struct
  type 'i t =
    | Finished
    | Input of 'i
  [@@deriving sexp_of]
end

let run_until_finished
      ?(show_steps = false)
      t
      ~first_input
      ~(next_input : _ -> _ Next_input.t)
  =
  let step_number = ref 0 in
  let rec loop input =
    if show_steps then print_s [%message "" ~step_number:(!step_number : int)];
    incr step_number;
    update_state t input;
    let output = output t input in
    match next_input output with
    | Finished -> ()
    | Input i -> loop i
  in
  loop first_input
;;

let sequence
      (type a b c)
      (T (module T1) as t1 : (a, b) t)
      (T (module T2) as t2 : (b, c) t)
  : (a, c) t
  =
  T
    (module struct
      module Input = T1.Input
      module Output = T2.Output

      type nonrec t = (T1.Input.t, T1.Output.t) t * (T2.Input.t, T2.Output.t) t
      [@@deriving sexp_of]

      let t = t1, t2
      let created_at = [%here]

      let update_state ((t1, t2) : t) input =
        let b = output t1 input in
        update_state t1 input;
        update_state t2 b
      ;;

      let output (t1, t2) input = output t2 (output t1 input)
    end)
;;

let map_input
      (type i1 i2 o)
      (T (module T) : (i2, o) t)
      (module Input : Data.S with type t = i1)
      ~f
  =
  T
    (module struct
      module Input = Input
      module Output = T.Output

      type t = T.t [@@deriving sexp_of]

      let t = T.t
      let created_at = [%here]
      let output t i1 = T.output t (f i1)
      let update_state t i1 = T.update_state t (f i1)
    end)
;;

let map_output
      (type i o1 o2)
      (T (module T) : (i, o1) t)
      (module Output : Data.S with type t = o2)
      ~f
  =
  T
    (module struct
      module Input = T.Input
      module Output = Output

      type t = T.t [@@deriving sexp_of]

      let t = T.t
      let created_at = [%here]
      let output t i = f (T.output t i)
      let update_state t i = T.update_state t i
    end)
;;

let create_binary_bool sexp f =
  create_combinational
    (module struct
      module Input = Data.Pair (Data.Bool) (Data.Bool)
      module Output = Data.Bool

      type t = unit

      let sexp_of_t () = sexp
      let created_at = [%here]
      let t = ()
      let output () (b1, b2) = f b1 b2
    end)
;;

let and_ = create_binary_bool [%message "and"] (fun b1 b2 -> b1 && b2)
let or_ = create_binary_bool [%message "or"] (fun b1 b2 -> b1 || b2)

let create_unary_bool sexp f =
  create_combinational
    (module struct
      module Input = Data.Bool
      module Output = Data.Bool

      type t = unit

      let sexp_of_t () = sexp
      let created_at = [%here]
      let t = ()
      let output () b = f b
    end)
;;

let not_ = create_unary_bool [%message "not"] (fun b -> not b)

let flip_flop () =
  create
    (module struct
      module Input = Data.Bool
      module Output = Data.Bool

      type t = bool ref [@@deriving sexp_of]

      let sexp_of_t t = [%message "Flip_flop" ~_:(t : t)]
      let t = ref Output.undefined
      let created_at = [%here]
      let output t _ = !t
      let update_state t b = t := b
    end)
;;

module Flip_flop_with_load_enable = struct
  module Input = struct
    type t =
      { input : bool
      ; load_enable : bool
      }
    [@@deriving compare, sexp_of]

    let equal = [%compare.equal: t]
    let undefined = { input = Data.Bool.undefined; load_enable = Data.Bool.undefined }
  end

  module Output = Data.Bool

  let create () =
    create
      (module struct
        module Input = Input
        module Output = Output

        type t = bool ref [@@deriving sexp_of]

        let t = ref Output.undefined
        let sexp_of_t t = [%message "Flip_flop_with_load_enable" ~_:(t : t)]
        let created_at = [%here]
        let output t _ = !t
        let update_state t { Input.input; load_enable } = if load_enable then t := input
      end)
  ;;
end

module Flip_flop_with_load_enable_and_reset = struct
  module Input = struct
    type t =
      { input : bool
      ; load_enable : bool
      ; reset : bool
      }
    [@@deriving compare, sexp_of]

    let equal = [%compare.equal: t]

    let undefined =
      { input = Data.Bool.undefined
      ; load_enable = Data.Bool.undefined
      ; reset = Data.Bool.undefined
      }
    ;;
  end

  module Output = Data.Bool

  let create () =
    create
      (module struct
        module Input = Input
        module Output = Output

        type t = bool ref [@@deriving sexp_of]

        let t = ref Output.undefined
        let sexp_of_t t = [%message "Flip_flop_with_load_enable_and_reset" ~_:(t : t)]
        let created_at = [%here]
        let output t _ = !t

        let update_state t { Input.input; load_enable; reset } =
          if reset then t := false else if load_enable then t := input
        ;;
      end)
  ;;
end
OCaml

Innovation. Community. Security.