aboutsummaryrefslogtreecommitdiffstats
path: root/src/verilog/PrintHTL.ml
blob: ffa853f990543ab56e9e73ef290769c0483cd937 (plain)
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
(* -*- mode: tuareg -*-
 * Vericert: Verified high-level synthesis.
 * Copyright (C) 2019-2020 Yann Herklotz <yann@yannherklotz.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *)

open Value
open Datatypes
open Camlcoq
open AST
open Clflags
open Printf
open Maps
open AST
open HTL
open PrintAST
open PrintVerilog
open VericertClflags

let pstr pp = fprintf pp "%s"

(* TODO import from PrintVerilog.v *)
let rec intersperse c = function
  | [] -> []
  | [x] -> [x]
  | x :: xs -> x :: c :: intersperse c xs

let register a = sprintf "reg_%d" (P.to_int a)
let registers a = String.concat "" (intersperse ", " (List.map register a))
let vmodule a = sprintf "%s" (extern_atom a)
let instance a = sprintf "instance_%d" (P.to_int a)

let print_instruction pp (pc, i) =
  fprintf pp "%5d:\t%s" pc (pprint_stmnt 0 i)

let print_instantiation pp (pc, HTLinstantiation (mod_name, inst_name, args, dst, fin)) =
  fprintf pp "%5d:\t %s %s(%s) -> %s\n" pc (vmodule mod_name) (instance inst_name) (registers args) (register dst)

let ptree_to_list ptree =
  List.sort
    (fun (pc1, _) (pc2, _) -> compare pc2 pc1)
    (List.rev_map
      (fun (pc, i) -> (P.to_int pc, i))
      (PTree.elements ptree))

let print_module pp id f =
  fprintf pp "%s(%s) {\n" (extern_atom id) (registers f.mod_params);
  let datapath = ptree_to_list f.mod_datapath in
  let controllogic = ptree_to_list f.mod_controllogic in
  let insts = ptree_to_list f.mod_insts in
  if List.length insts > 0 then (
    fprintf pp "  instances {\n";
    List.iter (print_instantiation pp) insts;
    fprintf pp "  }\n\n";
  ) else fprintf pp "  ";
  fprintf pp "datapath {\n";
  List.iter (print_instruction pp) datapath;
  fprintf pp "  }\n\n  controllogic {\n";
  List.iter (print_instruction pp) controllogic;
  fprintf pp "  }\n}\n\n"

let print_globdef pp (id, gd) =
  match gd with
  | Gfun(Internal f) -> print_module pp id f
  | _ -> ()

let print_program pp prog =
  List.iter (print_globdef pp) prog.prog_defs

let destination : string option ref = ref None

let print_if prog =
  match !destination with
  | None -> ()
  | Some f ->
    let oc = open_out f in
    print_program oc prog;
    close_out oc