1 /*
2  * Copyright (c) 2004-2010 Stephen Williams (steve@icarus.com)
3  *
4  *    This source code is free software; you can redistribute it
5  *    and/or modify it in source code form under the terms of the GNU
6  *    General Public License as published by the Free Software
7  *    Foundation; either version 2 of the License, or (at your option)
8  *    any later version.
9  *
10  *    This program is distributed in the hope that it will be useful,
11  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *    GNU General Public License for more details.
14  *
15  *    You should have received a copy of the GNU General Public License
16  *    along with this program; if not, write to the Free Software
17  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 # include  "compile.h"
21 # include  "vvp_net.h"
22 # include  <cstdlib>
23 # include  <iostream>
24 # include  <cassert>
25 
26 
vvp_fun_concat(unsigned w0,unsigned w1,unsigned w2,unsigned w3)27 vvp_fun_concat::vvp_fun_concat(unsigned w0, unsigned w1,
28 			       unsigned w2, unsigned w3)
29 : val_(w0+w1+w2+w3)
30 {
31       wid_[0] = w0;
32       wid_[1] = w1;
33       wid_[2] = w2;
34       wid_[3] = w3;
35 
36       for (unsigned idx = 0 ;  idx < val_.size() ;  idx += 1)
37 	    val_.set_bit(idx, BIT4_Z);
38 }
39 
~vvp_fun_concat()40 vvp_fun_concat::~vvp_fun_concat()
41 {
42 }
43 
recv_vec4(vvp_net_ptr_t port,const vvp_vector4_t & bit,vvp_context_t)44 void vvp_fun_concat::recv_vec4(vvp_net_ptr_t port, const vvp_vector4_t&bit,
45                                vvp_context_t)
46 {
47       unsigned pdx = port.port();
48 
49       if (bit.size() != wid_[pdx]) {
50 	    cerr << "internal error: port " << pdx
51 		 << " expects wid=" << wid_[pdx]
52 		 << ", got wid=" << bit.size() << endl;
53 	    assert(0);
54       }
55 
56       unsigned off = 0;
57       for (unsigned idx = 0 ;  idx < pdx ;  idx += 1)
58 	    off += wid_[idx];
59 
60       for (unsigned idx = 0 ;  idx < wid_[pdx] ;  idx += 1) {
61 	    val_.set_bit(off+idx, bit.value(idx));
62       }
63 
64       port.ptr()->send_vec4(val_, 0);
65 }
66 
recv_vec4_pv(vvp_net_ptr_t port,const vvp_vector4_t & bit,unsigned base,unsigned wid,unsigned vwid,vvp_context_t)67 void vvp_fun_concat::recv_vec4_pv(vvp_net_ptr_t port, const vvp_vector4_t&bit,
68                                   unsigned base, unsigned wid, unsigned vwid,
69                                   vvp_context_t)
70 {
71       assert(bit.size() == wid);
72 
73       unsigned pdx = port.port();
74 
75       if (vwid != wid_[pdx]) {
76 	    cerr << "internal error: port " << pdx
77 		 << " expects wid=" << wid_[pdx]
78 		 << ", got wid=" << vwid << endl;
79 	    assert(0);
80       }
81 
82       unsigned off = 0;
83       for (unsigned idx = 0 ;  idx < pdx ;  idx += 1)
84 	    off += wid_[idx];
85 
86       unsigned limit = off + wid_[pdx];
87 
88       off += base;
89       for (unsigned idx = 0 ;  idx < wid ;  idx += 1) {
90             if (off+idx >= limit) break;
91 	    val_.set_bit(off+idx, bit.value(idx));
92       }
93 
94       port.ptr()->send_vec4(val_, 0);
95 }
96 
compile_concat(char * label,unsigned w0,unsigned w1,unsigned w2,unsigned w3,unsigned argc,struct symb_s * argv)97 void compile_concat(char*label, unsigned w0, unsigned w1,
98 		    unsigned w2, unsigned w3,
99 		    unsigned argc, struct symb_s*argv)
100 {
101       vvp_fun_concat*fun = new vvp_fun_concat(w0, w1, w2, w3);
102 
103       vvp_net_t*net = new vvp_net_t;
104       net->fun = fun;
105 
106       define_functor_symbol(label, net);
107       free(label);
108 
109       inputs_connect(net, argc, argv);
110       free(argv);
111 }
112 
113 
vvp_fun_concat8(unsigned w0,unsigned w1,unsigned w2,unsigned w3)114 vvp_fun_concat8::vvp_fun_concat8(unsigned w0, unsigned w1,
115 			       unsigned w2, unsigned w3)
116 : val_(w0+w1+w2+w3)
117 {
118       wid_[0] = w0;
119       wid_[1] = w1;
120       wid_[2] = w2;
121       wid_[3] = w3;
122 
123       for (unsigned idx = 0 ;  idx < val_.size() ;  idx += 1)
124 	    val_.set_bit(idx, vvp_scalar_t(BIT4_Z, 0, 0));
125 }
126 
~vvp_fun_concat8()127 vvp_fun_concat8::~vvp_fun_concat8()
128 {
129 }
130 
recv_vec4(vvp_net_ptr_t port,const vvp_vector4_t & bit,vvp_context_t)131 void vvp_fun_concat8::recv_vec4(vvp_net_ptr_t port, const vvp_vector4_t&bit,
132 				vvp_context_t)
133 {
134       vvp_vector8_t bit8 (bit, 6, 6);
135       recv_vec8(port, bit8);
136 }
137 
recv_vec4_pv(vvp_net_ptr_t port,const vvp_vector4_t & bit,unsigned base,unsigned wid,unsigned vwid,vvp_context_t)138 void vvp_fun_concat8::recv_vec4_pv(vvp_net_ptr_t port, const vvp_vector4_t&bit,
139 				   unsigned base, unsigned wid, unsigned vwid,
140 				   vvp_context_t)
141 {
142       vvp_vector8_t bit8 (bit, 6, 6);
143       recv_vec8_pv(port, bit8, base, wid, vwid);
144 }
145 
recv_vec8(vvp_net_ptr_t port,const vvp_vector8_t & bit)146 void vvp_fun_concat8::recv_vec8(vvp_net_ptr_t port, const vvp_vector8_t&bit)
147 {
148       unsigned pdx = port.port();
149 
150       if (bit.size() != wid_[pdx]) {
151 	    cerr << "internal error: port " << pdx
152 		 << " expects wid=" << wid_[pdx]
153 		 << ", got wid=" << bit.size() << endl;
154 	    assert(0);
155       }
156 
157       unsigned off = 0;
158       for (unsigned idx = 0 ;  idx < pdx ;  idx += 1)
159 	    off += wid_[idx];
160 
161       for (unsigned idx = 0 ;  idx < wid_[pdx] ;  idx += 1) {
162 	    val_.set_bit(off+idx, bit.value(idx));
163       }
164 
165       port.ptr()->send_vec8(val_);
166 }
167 
recv_vec8_pv(vvp_net_ptr_t port,const vvp_vector8_t & bit,unsigned base,unsigned wid,unsigned vwid)168 void vvp_fun_concat8::recv_vec8_pv(vvp_net_ptr_t port, const vvp_vector8_t&bit,
169 				   unsigned base, unsigned wid, unsigned vwid)
170 {
171       assert(bit.size() == wid);
172 
173       unsigned pdx = port.port();
174 
175       if (vwid != wid_[pdx]) {
176 	    cerr << "internal error: port " << pdx
177 		 << " expects wid=" << wid_[pdx]
178 		 << ", got wid=" << vwid << endl;
179 	    assert(0);
180       }
181 
182       unsigned off = 0;
183       for (unsigned idx = 0 ;  idx < pdx ;  idx += 1)
184 	    off += wid_[idx];
185 
186       unsigned limit = off + wid_[pdx];
187 
188       off += base;
189       for (unsigned idx = 0 ;  idx < wid ;  idx += 1) {
190             if (off+idx >= limit) break;
191 	    val_.set_bit(off+idx, bit.value(idx));
192       }
193 
194       port.ptr()->send_vec8(val_);
195 }
196 
compile_concat8(char * label,unsigned w0,unsigned w1,unsigned w2,unsigned w3,unsigned argc,struct symb_s * argv)197 void compile_concat8(char*label, unsigned w0, unsigned w1,
198 		     unsigned w2, unsigned w3,
199 		     unsigned argc, struct symb_s*argv)
200 {
201       vvp_fun_concat8*fun = new vvp_fun_concat8(w0, w1, w2, w3);
202 
203       vvp_net_t*net = new vvp_net_t;
204       net->fun = fun;
205 
206       define_functor_symbol(label, net);
207       free(label);
208 
209       inputs_connect(net, argc, argv);
210       free(argv);
211 }
212 
vvp_fun_repeat(unsigned width,unsigned repeat)213 vvp_fun_repeat::vvp_fun_repeat(unsigned width, unsigned repeat)
214 : wid_(width), rep_(repeat)
215 {
216 }
217 
~vvp_fun_repeat()218 vvp_fun_repeat::~vvp_fun_repeat()
219 {
220 }
221 
recv_vec4(vvp_net_ptr_t port,const vvp_vector4_t & bit,vvp_context_t)222 void vvp_fun_repeat::recv_vec4(vvp_net_ptr_t port, const vvp_vector4_t&bit,
223                                vvp_context_t)
224 {
225       assert(bit.size() == wid_/rep_);
226 
227       vvp_vector4_t val (wid_);
228 
229       for (unsigned rdx = 0 ;  rdx < rep_ ;  rdx += 1) {
230 	    unsigned off = rdx * bit.size();
231 
232 	    for (unsigned idx = 0 ; idx < bit.size() ;  idx += 1)
233 		  val.set_bit(off+idx, bit.value(idx));
234 
235       }
236 
237       port.ptr()->send_vec4(val, 0);
238 }
239 
compile_repeat(char * label,long width,long repeat,struct symb_s arg)240 void compile_repeat(char*label, long width, long repeat, struct symb_s arg)
241 {
242       vvp_fun_repeat*fun = new vvp_fun_repeat(width, repeat);
243 
244       vvp_net_t*net = new vvp_net_t;
245       net->fun = fun;
246 
247       define_functor_symbol(label, net);
248       free(label);
249 
250       input_connect(net, 0, arg.text);
251 }
252