Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. open HardCaml
  2.  
  3. (* different register types *)
  4. module Seq = struct
  5. open Signal
  6. open Types
  7. open Seq
  8. module type S = sig
  9. include Interface.S
  10. val reg : Api.Comb.t t -> Signal.Types.register
  11. end
  12. module type T = sig
  13. include Seq
  14. val sys : (module S)
  15. end
  16. module Full = struct
  17. type 'a t = {
  18. clock : 'a[@bits 1];
  19. reset : 'a[@bits 1];
  20. clear : 'a[@bits 1];
  21. }[@@deriving hardcaml]
  22. let reg r =
  23. { r_full with
  24. reg_clock = r.clock;
  25. reg_reset = r.reset;
  26. reg_clear = r.clear;
  27. }
  28. end
  29. module Async = struct
  30. type 'a t = {
  31. clock : 'a[@bits 1];
  32. reset : 'a[@bits 1];
  33. }[@@deriving hardcaml]
  34. let reg r =
  35. { r_async with
  36. reg_clock = r.clock;
  37. reg_reset = r.reset;
  38. }
  39. end
  40. module Sync = struct
  41. type 'a t = {
  42. clock : 'a[@bits 1];
  43. clear : 'a[@bits 1];
  44. }[@@deriving hardcaml]
  45. let reg r =
  46. { r_sync with
  47. reg_clock = r.clock;
  48. reg_clear = r.clear;
  49. }
  50. end
  51. module None = struct
  52. type 'a t = {
  53. clock : 'a[@bits 1];
  54. }[@@deriving hardcaml]
  55. let reg r =
  56. { r_none with
  57. reg_clock = r.clock;
  58. }
  59. end
  60. let make reg r =
  61. let module Seq = Make_seq(struct let reg_spec = reg r let ram_spec = r_none end) in
  62. (module Seq : Seq)
  63. end
  64.  
  65. (**********************************************************************)
  66.  
  67. module type Inst_type = sig
  68. val inst_type : [ `flat | `inst | `hier of Circuit.Hierarchy.database ]
  69. end
  70.  
  71. module Make_inst(T : Inst_type)(I : Interface.S)(O : Interface.S) = struct
  72.  
  73. module Inst = Interface.Inst(I)(O)
  74. module Hier = Interface.Hier(I)(O)
  75.  
  76. let f name f i =
  77. match T.inst_type with
  78. | `flat -> f i
  79. | `inst -> Inst.make name i
  80. | `hier(db) -> Hier.make db name f i
  81.  
  82. end
  83.  
  84. module Make_seq_inst(T : Inst_type)(S : Seq.S)(I : Interface.S)(O : Interface.S) = struct
  85.  
  86. module Isys = struct
  87. type 'a t = {
  88. sys : 'a S.t;
  89. i : 'a I.t;
  90. }[@@deriving hardcaml]
  91. end
  92.  
  93. module Inst = Interface.Inst(Isys)(O)
  94. module Hier = Interface.Hier(Isys)(O)
  95.  
  96. let sys = S.(map (fun (n,b) -> Api.Comb.input n b) t)
  97. module Seq = (val (Seq.make S.reg sys))
  98.  
  99. let f name f i =
  100. match T.inst_type with
  101. | `flat -> f i
  102. | `inst -> Inst.make name { Isys.sys = sys; i }
  103. | `hier(db) -> Hier.make db name (fun i -> f i.Isys.i) { Isys.sys = sys; i }
  104.  
  105. end
  106.  
  107. module Inst_type = struct
  108. module Hier() = struct
  109. let db = Circuit.Hierarchy.empty ()
  110. let inst_type = `hier(db)
  111. end
  112. module Flat = struct let inst_type = `flat end
  113. module Inst = struct let inst_type = `inst end
  114. end
  115.  
  116. (**********************************************************************)
  117.  
  118. module Fa = struct
  119.  
  120. module I = struct
  121. type 'a t = {
  122. a : 'a[@bits 1];
  123. b : 'a[@bits 1];
  124. cin : 'a[@bits 1]
  125. }[@@deriving hardcaml]
  126. end
  127.  
  128. module O = struct
  129. type 'a t = {
  130. sum : 'a[@bits 1];
  131. cout : 'a[@bits 1];
  132. }[@@deriving hardcaml]
  133. end
  134.  
  135. let name = "fulladder"
  136.  
  137. open Api.Comb
  138.  
  139. let f i =
  140. let open I in
  141. let sum = i.a ^: i.b ^: i.cin in
  142. let cout = (i.a ^: i.b) |: (i.a ^: i.cin) |: (i.b ^: i.cin) in
  143. { O.sum; cout }
  144.  
  145. end
  146.  
  147. (* combinatorial carry ripple adder *)
  148. module Carry_ripple(T : Inst_type) = struct
  149.  
  150. module I = struct
  151. type 'a t = {
  152. a : 'a[@bits 8];
  153. b : 'a[@bits 8];
  154. }[@@deriving hardcaml]
  155. end
  156.  
  157. module O = struct
  158. type 'a t = {
  159. c : 'a[@bits 9];
  160. }[@@deriving hardcaml]
  161. end
  162.  
  163. let name = "carry_ripple_adder"
  164.  
  165. module Fa_inst = Make_inst(T)(Fa.I)(Fa.O)
  166.  
  167. let f i =
  168. let open I in
  169. let open Api.Comb in
  170. let c, s = List.fold_left2
  171. (fun (c,s) a b ->
  172. let o = Fa_inst.f Fa.name Fa.f { Fa.I.a; b; cin=c} in
  173. o.Fa.O.cout,o.Fa.O.sum::s)
  174. (gnd,[]) (List.rev (bits i.I.a)) (List.rev (bits i.I.b))
  175. in
  176. { O.c = concat (c::s) }
  177.  
  178. end
  179.  
  180. module Reg = struct
  181. module I = struct
  182. type 'a t = {
  183. d : 'a[@bits 9];
  184. }[@@deriving hardcaml]
  185. end
  186.  
  187. module O = struct
  188. type 'a t = {
  189. q : 'a[@bits 9];
  190. }[@@deriving hardcaml]
  191. end
  192.  
  193. let name = "sync_reg"
  194.  
  195. module Make(Seq : Signal.Seq) = struct
  196.  
  197. open Api.Comb
  198.  
  199. let f i =
  200. { O.q = Seq.reg ~e:vdd i.I.d }
  201.  
  202. end
  203.  
  204. end
  205.  
  206. module AdderReg(T : Inst_type) = struct
  207.  
  208. module Cra = Carry_ripple(T)
  209. module Cra_inst = Make_inst(T)(Cra.I)(Cra.O)
  210.  
  211. module I = Cra.I
  212. module O = Reg.O
  213.  
  214. module Reg_inst = Make_seq_inst(T)(Seq.Full)(Reg.I)(Reg.O)
  215.  
  216. let name = "adder_reg"
  217.  
  218. let f i =
  219. (* construct carry ripple adder *)
  220. let o = Cra_inst.f Cra.name Cra.f i in
  221.  
  222. (* register *)
  223. let module Reg' = Reg.Make(Reg_inst.Seq) in
  224. Reg_inst.f Reg.name Reg'.f { Reg.I.d = o.Cra.O.c }
  225.  
  226. end
  227.  
  228.  
  229. module H = Inst_type.Hier()
  230. module A = AdderReg(H)
  231. module Circ = Interface.Circ(A.I)(A.O)
  232. module A_inst = Make_seq_inst(H)(Seq.Full)(A.I)(A.O)
  233. let circ = Circ.make "top" (A_inst.f A.name A.f)
  234. let rtlh() = Rtl.Hierarchy.write H.db "" (fun _ -> Rtl.Verilog.write print_string) circ
  235.  
  236.  
  237. module F = Inst_type.Flat
  238. module Aflat = AdderReg(F)
  239. module Aflat_inst = Make_seq_inst(F)(Seq.Full)(Aflat.I)(Aflat.O)
  240. module Circ = Interface.Circ(Aflat.I)(Aflat.O)
  241. let circ = Circ.make "top" (Aflat_inst.f Aflat.name Aflat.f)
  242. let rtlf() = Rtl.Verilog.write print_string circ
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement