Created
March 25, 2017 17:00
-
-
Save andrewray/f08c858a804cfd655cf887c46b1d3ea4 to your computer and use it in GitHub Desktop.
Updated version of cyclesim_generators.ml using interfaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#require "delimcc";; | |
#require "hardcaml,hardcaml-waveterm,ppx_deriving_hardcaml";; | |
open Delimcc | |
open HardCaml | |
open Api | |
type ('a, 'b) t = Done | More of 'a * ('b -> ('a, 'b) t) | |
let gen f = | |
(* | |
* Note: the first value to yield gets thrown away as the generator | |
* has not yet started. | |
*) | |
let start _ = | |
let p = Delimcc.new_prompt () in | |
Delimcc.push_prompt p begin fun () -> | |
f (fun x -> Delimcc.shift0 p (fun k -> More (x, k))); Done | |
end in | |
let next = ref start in | |
fun rv -> | |
match !next rv with | |
| More (x, k) -> next := k; Some x | |
| Done -> None | |
(* type of testbench generators. [let foo = `gen (fun yield -> ... )] *) | |
(*type gen = [ `gen of ('a list -> unit) -> unit ] as 'a*) | |
type gen = [ `gen of (gen list -> unit) -> unit ] | |
(* wait n cycles (default 1), then run the generator *) | |
let delay ?(n=1) (`gen f) = `gen begin fun yield -> | |
for i=0 to n-1 do | |
yield []; | |
done; | |
f yield | |
end | |
(* run generators in sequence *) | |
let seq : (gen list -> gen) = fun l -> `gen begin fun yield -> | |
List.iter (fun (`gen f) -> f yield) l | |
end | |
(* run the given generator for n cycles. | |
The generator can 'yield' new generators which will start running | |
in the current cycle. | |
If n is not specified, run until there are no more generators *) | |
let run ?n sim (driver : gen) = | |
let rec runsim' (n,m) drivers = | |
match drivers, n with | |
| _, Some(n) when n = !m -> () | |
| [], None -> () | |
| _ -> | |
if drivers = [] then () | |
else | |
let rec f = function | |
| [] -> Cs.cycle sim; incr m; [] | |
| h::t -> | |
match h () with | |
| Some (x:gen list) -> h :: f (t @ (List.map (fun (`gen x) -> gen x) x)) | |
| None -> f t | |
in | |
runsim' (n,m) (f drivers) | |
in | |
runsim' (n, ref 0) [(function `gen f -> gen f) driver] | |
(********************************************************************) | |
open Comb | |
open Seq | |
module W = HardCamlWaveTerm.Wave.Make(HardCamlWaveTerm.Wave.Bits(B)) | |
module Ws = HardCamlWaveTerm.Sim.Make(B)(W) | |
module Widget = HardCamlWaveTerm.Widget.Make(B)(W) | |
open Printf | |
module I = struct | |
type 'a t = { | |
clock : 'a[@bits 1]; | |
clear : 'a[@bits 1]; | |
enable : 'a[@bits 1]; | |
a : 'a[@bits 8]; | |
b : 'a[@bits 8]; | |
x : 'a[@bits 8]; | |
y : 'a[@bits 8]; | |
}[@@deriving hardcaml] | |
end | |
module O = struct | |
type 'a t = { | |
c : 'a[@bits 8]; | |
d : 'a[@bits 8]; | |
}[@@deriving hardcaml] | |
end | |
let hwfn i = | |
let open I in | |
let module Seq = Signal.Make_seq(struct i | |
let reg_spec = { r_sync with Signal.Types.reg_clock = i.clock; reg_clear = i.clear } | |
let ram_spec = r_none | |
end) in | |
let f a b = Seq.reg ~e:enable (a +: b) in | |
let g a b = Seq.reg ~e:enable (a *: b) in | |
let open I in | |
{ | |
O.c = f i.a i.b; | |
d = g i.x i.y | |
} | |
module G = HardCaml.Interface.Sim(B)(I)(O) | |
let _,sim,i,_,o = G.make "foo" hwfn | |
let sim, waves = Ws.wrap sim | |
(********************************************************************) | |
(********************************************************************) | |
open I | |
open O | |
(* reset, enable and start generators *) | |
let rec start_gen : gen = `gen begin fun yield -> | |
i.clear := B.vdd; | |
yield []; | |
i.clear := B.gnd; | |
i.enable := B.vdd; | |
yield [ | |
delay add_gen; (* run adder inputs *) | |
delay (seq [mul_gen; mul_gen]); (* run multiplier inputs, twice *) | |
delay ~n:2 (show 'a' o.c); (* show adder output *) | |
delay ~n:2 (show 'm' o.d); (* show multiplier output *) | |
] | |
end | |
(* set the adder inputs for 10 cycles *) | |
and add_gen = `gen begin fun yield -> | |
for j=0 to 9 do | |
i.a := B.consti 8 (j+1); | |
i.b := B.consti 8 (j+1); | |
yield []; | |
done | |
end | |
(* set the multiplier inputs for 5 cycles *) | |
and mul_gen = `gen begin fun yield -> | |
for j=0 to 4 do | |
i.x := B.consti 8 (j+1); | |
i.y := B.consti 8 (j+1); | |
yield []; | |
done | |
end | |
(* show outputs *) | |
and show s p = `gen begin fun yield -> | |
while true do | |
printf "%c = %i\n" s (B.to_int !p); | |
yield [] | |
done | |
end | |
(* run testbench *) | |
let test () = | |
let () = run ~n:18 sim start_gen in | |
let waveform = new Widget.waveform () in | |
let waves = W.{ cfg={default with wave_width=2}; waves } in | |
waveform#set_waves waves; | |
Lwt_main.run @@ Widget.run_widget waveform | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment