Skip to content

Instantly share code, notes, and snippets.

@andrewray
Created February 6, 2017 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewray/44f5f62d172df8cde5b056e23e91e961 to your computer and use it in GitHub Desktop.
Save andrewray/44f5f62d172df8cde5b056e23e91e961 to your computer and use it in GitHub Desktop.
Experimental generators for simulation based on Delimcc
#require "delimcc";;
#require "hardcaml";;
open Delimcc
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
open HardCaml
open Api
open Comb
open Seq
open Printf
let f a b = reg r_sync enable (a +: b)
let g a b = reg r_sync enable (a *: b)
let a = input "a" 8
let b = input "b" 8
let x = input "x" 8
let y = input "y" 8
let c = output "c" (f a b)
let d = output "d" (g x y)
let circ = Circuit.make "foo" [c;d]
let sim = Cyclesim.make circ
let enable = Cs.in_port sim "enable"
let a = Cs.in_port sim "a"
let b = Cs.in_port sim "b"
let x = Cs.in_port sim "x"
let y = Cs.in_port sim "y"
let c = Cs.out_port sim "c"
let d = Cs.out_port sim "d"
let enable_driver = gen begin fun cycle ->
enable := B.vdd;
cycle ();
end
let add_driver = gen begin fun cycle ->
cycle ();
for i=1 to 10 do
a := B.consti 8 i;
b := B.consti 8 i;
cycle ();
printf "a = %i\n" (B.to_int !c);
done
end
let mul_driver = gen begin fun cycle ->
cycle ();
for i=1 to 5 do
x := B.consti 8 i;
y := B.consti 8 i;
cycle ();
printf "m = %i\n" (B.to_int !d);
done
end
let rec runsim drivers =
if drivers = [] then ()
else
let rec f = function
| [] -> Cs.cycle sim; []
| h::t ->
match h () with
| Some () -> h :: f t
| None -> f t
in
runsim (f drivers)
let () = runsim [ enable_driver; add_driver; mul_driver ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment