1--  API to build a netlist.
2--  Copyright (C) 2017 Tristan Gingold
3--
4--  This file is part of GHDL.
5--
6--  This program is free software; you can redistribute it and/or modify
7--  it under the terms of the GNU General Public License as published by
8--  the Free Software Foundation; either version 2 of the License, or
9--  (at your option) any later version.
10--
11--  This program is distributed in the hope that it will be useful,
12--  but WITHOUT ANY WARRANTY; without even the implied warranty of
13--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14--  GNU General Public License for more details.
15--
16--  You should have received a copy of the GNU General Public License
17--  along with this program; if not, write to the Free Software
18--  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19--  MA 02110-1301, USA.
20
21with Ada.Unchecked_Deallocation;
22with Netlists.Gates; use Netlists.Gates;
23
24package Netlists.Builders is
25   type Context is private;
26   type Context_Acc is access Context;
27
28   type Uns32_Arr is array (Natural range <>) of Uns32;
29   type Uns32_Arr_Acc is access Uns32_Arr;
30   procedure Unchecked_Deallocate is new Ada.Unchecked_Deallocation
31     (Uns32_Arr, Uns32_Arr_Acc);
32
33   function New_Internal_Name (Ctxt : Context_Acc; Prefix : Sname := No_Sname)
34                              return Sname;
35
36   --  Create a builder for Design.  Must be called once.
37   function Build_Builders (Design : Module) return Context_Acc;
38
39   function Get_Design (Ctxt : Context_Acc) return Module;
40
41   --  Set the parent for the new instances.
42   procedure Set_Parent (Ctxt : Context_Acc; Parent : Module);
43
44   function Build_Dyadic (Ctxt : Context_Acc;
45                          Id : Dyadic_Module_Id;
46                          L, R : Net) return Net;
47
48   function Build_Shift_Rotate (Ctxt : Context_Acc;
49                                Id : Shift_Rotate_Module_Id;
50                                L, R : Net) return Net;
51
52   function Build_Monadic (Ctxt : Context_Acc;
53                           Id : Monadic_Module_Id;
54                           Op : Net) return Net;
55
56   function Build_Compare (Ctxt : Context_Acc;
57                           Id : Compare_Module_Id;
58                           L, R : Net) return Net;
59
60   function Build_Reduce (Ctxt : Context_Acc;
61                          Id : Reduce_Module_Id;
62                          Op : Net) return Net;
63
64   function Build_Const_X (Ctxt : Context_Acc;
65                           W : Width) return Net;
66   function Build_Const_Z (Ctxt : Context_Acc;
67                           W : Width) return Net;
68
69   function Build_Const_UB32 (Ctxt : Context_Acc;
70                              Val : Uns32;
71                              W : Width) return Net;
72   function Build_Const_UL32 (Ctxt : Context_Acc;
73                              Val : Uns32;
74                              Xz : Uns32;
75                              W : Width) return Net;
76
77   function Build_Const_SB32 (Ctxt : Context_Acc;
78                              Val : Int32;
79                              W : Width) return Net;
80
81   --  Large constants.
82   --  Bit means only 0 or 1.
83   --  Log means 0/1/Z/X.  Parameters 2N are aval, 2N+1 are bval.
84   function Build_Const_Bit (Ctxt : Context_Acc;
85                             W : Width) return Instance;
86   function Build_Const_Log (Ctxt : Context_Acc;
87                             W : Width) return Instance;
88
89   function Build_Posedge (Ctxt : Context_Acc; Src : Net) return Net;
90   function Build_Negedge (Ctxt : Context_Acc; Src : Net) return Net;
91
92   function Build_Mux2 (Ctxt : Context_Acc;
93                        Sel : Net;
94                        I0, I1 : Net) return Net;
95   function Build_Mux4 (Ctxt : Context_Acc;
96                        Sel : Net;
97                        I0, I1, I2, I3 : Net) return Net;
98
99   --  The width of SEL gives the number of inputs (+ 2).
100   --  The width of DEF (default value) gives the width of the output.
101   function Build_Pmux (Ctxt : Context_Acc;
102                        Sel : Net;
103                        Def : Net) return Net;
104
105   --  Build: I0 & I1 [ & I2 [ & I3 ]]
106   function Build_Concat2 (Ctxt : Context_Acc; I0, I1 : Net) return Net;
107   function Build_Concat3 (Ctxt : Context_Acc; I0, I1, I2 : Net) return Net;
108   function Build_Concat4 (Ctxt : Context_Acc; I0, I1, I2, I3 : Net)
109                          return Net;
110   --  NBR_INPUTS is the number of inputs ports, W is the width of the
111   --  output.
112   function Build_Concatn (Ctxt : Context_Acc; W : Width; Nbr_Inputs : Uns32)
113                          return Net;
114
115   function Build_Trunc
116     (Ctxt : Context_Acc; Id : Module_Id; I : Net; W : Width) return Net;
117   function Build_Extend
118     (Ctxt : Context_Acc; Id : Module_Id; I : Net; W : Width) return Net;
119
120   function Build_Extract
121     (Ctxt : Context_Acc; I : Net; Off, W : Width) return Net;
122
123   function Build_Extract_Bit
124     (Ctxt : Context_Acc; I : Net; Off : Width) return Net;
125   function Build_Dyn_Extract
126     (Ctxt : Context_Acc; Mem : Net; Idx : Net; Off : Uns32; W : Width)
127     return Net;
128
129   function Build_Dyn_Insert
130     (Ctxt : Context_Acc; Mem : Net; V : Net; Idx : Net; Off : Uns32)
131     return Net;
132   function Build_Dyn_Insert_En
133     (Ctxt : Context_Acc; Mem : Net; V : Net; Idx : Net; En : Net; Off : Uns32)
134     return Net;
135
136   function Build_Memidx
137     (Ctxt : Context_Acc;
138      I : Net; Step : Uns32; Max : Uns32; W : Width) return Net;
139   function Build_Addidx (Ctxt : Context_Acc; L, R : Net) return Net;
140
141   function Build_Memory (Ctxt : Context_Acc; W : Width) return Instance;
142   function Build_Memory_Init (Ctxt : Context_Acc; W : Width; Init : Net)
143                              return Instance;
144   function Build_Mem_Rd
145     (Ctxt : Context_Acc; Pport : Net; Addr : Net; Data_W : Width)
146     return Instance;
147   function Build_Mem_Rd_Sync (Ctxt : Context_Acc;
148                               Pport : Net;
149                               Addr : Net;
150                               Clk : Net;
151                               En : Net;
152                               Data_W : Width) return Instance;
153   function Build_Mem_Wr_Sync (Ctxt : Context_Acc;
154                               Pport : Net;
155                               Addr : Net;
156                               Clk : Net;
157                               En : Net;
158                               Data : Net) return Instance;
159
160   function Build_Mem_Multiport (Ctxt : Context_Acc; I0, I1 : Net) return Net;
161
162   function Build_Output (Ctxt : Context_Acc; W : Width) return Net;
163   function Build_Ioutput (Ctxt : Context_Acc; Init : Net) return Net;
164   function Build_Inout (Ctxt : Context_Acc; W : Width) return Instance;
165   function Build_Iinout (Ctxt : Context_Acc; W : Width) return Instance;
166   function Build_Signal (Ctxt : Context_Acc; Name : Sname; W : Width)
167                         return Net;
168   function Build_Isignal (Ctxt : Context_Acc; Name : Sname; Init : Net)
169                          return Net;
170   function Build_Port (Ctxt : Context_Acc; N : Net) return Net;
171   function Build_Enable (Ctxt : Context_Acc) return Net;
172
173   function Build_Assert (Ctxt : Context_Acc; Name : Sname; Cond : Net)
174                         return Instance;
175   function Build_Assume (Ctxt : Context_Acc; Name : Sname; Cond : Net)
176                         return Instance;
177   function Build_Cover (Ctxt : Context_Acc; Name : Sname; Cond : Net)
178                        return Instance;
179   function Build_Assert_Cover (Ctxt : Context_Acc; Name : Sname; Cond : Net)
180                         return Instance;
181
182   function Build_Formal_Input
183     (Ctxt : Context_Acc; Id : Formal_Module_Id; W : Width) return Net;
184
185   --  A simple flip-flop.
186   function Build_Dff (Ctxt : Context_Acc;
187                       Clk : Net;
188                       D : Net) return Net;
189   --  A flip-flop with an initial value (only for fpga)
190   --  The width is derived from INIT and D can be No_Net.
191   function Build_Idff (Ctxt : Context_Acc;
192                        Clk : Net;
193                        D : Net;
194                        Init : Net) return Net;
195   function Build_Adff (Ctxt : Context_Acc;
196                        Clk : Net;
197                        D : Net; Rst : Net; Rst_Val : Net) return Net;
198   function Build_Iadff (Ctxt : Context_Acc;
199                        Clk : Net;
200                        D : Net; Rst : Net; Rst_Val : Net;
201                                            Init : Net) return Net;
202
203   function Build_Mdff (Ctxt : Context_Acc;
204                        Clk : Net;
205                        D : Net;
206                        Els : Net) return Net;
207   function Build_Midff (Ctxt : Context_Acc;
208                         Clk : Net;
209                         D : Net;
210                         Els : Net;
211                         Init : Net) return Net;
212
213   function Build_Tri (Ctxt : Context_Acc; En : Net; D : Net) return Net;
214
215   function Build_Resolver (Ctxt : Context_Acc; L, R : Net) return Net;
216
217   function Build_Nop (Ctxt : Context_Acc; I : Net) return Net;
218private
219   type Module_Arr is array (Module_Id range <>) of Module;
220
221   type Context is record
222      Design : Module;
223      Parent : Module;
224      Num : Uns32;
225      M_Dyadic : Module_Arr (Dyadic_Module_Id);
226      M_Shift_Rotate : Module_Arr (Shift_Rotate_Module_Id);
227      M_Monadic : Module_Arr (Monadic_Module_Id);
228      M_Compare : Module_Arr (Compare_Module_Id);
229      M_Concat : Module_Arr (Concat_Module_Id);
230      M_Concatn : Module;
231      M_Const_UB32 : Module;
232      M_Const_SB32 : Module;
233      M_Const_UL32 : Module;
234      M_Const_X : Module;
235      M_Const_Z : Module;
236      M_Const_Bit : Module;
237      M_Const_Log : Module;
238      M_Posedge : Module;
239      M_Negedge : Module;
240      M_Mux2 : Module;
241      M_Mux4 : Module;
242      M_Pmux : Module;
243      M_Nop : Module;
244      M_Output : Module;
245      M_Ioutput : Module;
246      M_Signal : Module;
247      M_Isignal : Module;
248      M_Port : Module;
249      M_Inout : Module;
250      M_Iinout : Module;
251      M_Enable : Module;
252      M_Dff : Module;
253      M_Idff : Module;
254      M_Adff : Module;
255      M_Iadff : Module;
256      M_Mdff : Module;
257      M_Midff : Module;
258      M_Tri : Module;
259      M_Resolver : Module;
260      M_Truncate : Module_Arr (Truncate_Module_Id);
261      M_Extend : Module_Arr (Extend_Module_Id);
262      M_Reduce : Module_Arr (Reduce_Module_Id);
263      M_Extract : Module;
264      M_Dyn_Extract : Module;
265      M_Dyn_Insert : Module;
266      M_Dyn_Insert_En : Module;
267      M_Memidx : Module;
268      M_Addidx : Module;
269      M_Memory : Module;
270      M_Memory_Init : Module;
271      M_Mem_Rd : Module;
272      M_Mem_Rd_Sync : Module;
273      M_Mem_Wr_Sync : Module;
274      M_Mem_Multiport : Module;
275      M_Assert : Module;
276      M_Assume : Module;
277      M_Cover : Module;
278      M_Assert_Cover : Module;
279      M_Formal_Input : Module_Arr (Formal_Module_Id);
280   end record;
281end Netlists.Builders;
282