HDLRuby 2.10.5 → 2.11.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/HDLRuby.gemspec +1 -0
  3. data/README.md +8 -4
  4. data/Rakefile +8 -0
  5. data/{lib/HDLRuby/sim/Makefile → ext/hruby_sim/Makefile_csim} +0 -0
  6. data/ext/hruby_sim/extconf.rb +13 -0
  7. data/ext/hruby_sim/hruby_rcsim_build.c +1190 -0
  8. data/{lib/HDLRuby/sim → ext/hruby_sim}/hruby_sim.h +255 -16
  9. data/{lib/HDLRuby/sim → ext/hruby_sim}/hruby_sim_calc.c +311 -182
  10. data/{lib/HDLRuby/sim → ext/hruby_sim}/hruby_sim_core.c +36 -18
  11. data/{lib/HDLRuby/sim → ext/hruby_sim}/hruby_sim_list.c +0 -0
  12. data/{lib/HDLRuby/sim → ext/hruby_sim}/hruby_sim_stack_calc.c +4 -1
  13. data/{lib/HDLRuby/sim → ext/hruby_sim}/hruby_sim_stack_calc.c.sav +0 -0
  14. data/ext/hruby_sim/hruby_sim_tree_calc.c +375 -0
  15. data/{lib/HDLRuby/sim → ext/hruby_sim}/hruby_sim_vcd.c +5 -5
  16. data/{lib/HDLRuby/sim → ext/hruby_sim}/hruby_sim_vizualize.c +2 -2
  17. data/{lib/HDLRuby/sim → ext/hruby_sim}/hruby_value_pool.c +8 -1
  18. data/lib/HDLRuby/hdr_samples/bstr_bench.rb +2 -0
  19. data/lib/HDLRuby/hdr_samples/case_bench.rb +2 -2
  20. data/lib/HDLRuby/hdr_samples/counter_bench.rb +0 -1
  21. data/lib/HDLRuby/hdr_samples/counter_dff_bench.rb +46 -0
  22. data/lib/HDLRuby/hdr_samples/dff_bench.rb +1 -1
  23. data/lib/HDLRuby/hdr_samples/print_bench.rb +62 -0
  24. data/lib/HDLRuby/hdr_samples/rom.rb +5 -3
  25. data/lib/HDLRuby/hdr_samples/simple_counter_bench.rb +43 -0
  26. data/lib/HDLRuby/hdrcc.rb +54 -8
  27. data/lib/HDLRuby/hruby_bstr.rb +1175 -917
  28. data/lib/HDLRuby/hruby_high.rb +200 -90
  29. data/lib/HDLRuby/hruby_high_fullname.rb +82 -0
  30. data/lib/HDLRuby/hruby_low.rb +41 -23
  31. data/lib/HDLRuby/hruby_low2c.rb +7 -0
  32. data/lib/HDLRuby/hruby_rcsim.rb +978 -0
  33. data/lib/HDLRuby/hruby_rsim.rb +1134 -0
  34. data/lib/HDLRuby/hruby_rsim_vcd.rb +322 -0
  35. data/lib/HDLRuby/hruby_values.rb +362 -18
  36. data/lib/HDLRuby/hruby_verilog.rb +21 -3
  37. data/lib/HDLRuby/version.rb +1 -1
  38. metadata +24 -13
@@ -0,0 +1,1190 @@
1
+ #ifdef RCSIM
2
+
3
+ #include <stdio.h>
4
+ #include <stdarg.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+ #include <limits.h>
8
+
9
+ #include <ruby.h>
10
+ #include "extconf.h"
11
+
12
+ #include "hruby_sim.h"
13
+
14
+ /**
15
+ * The C-Ruby hybrid HDLRuby simulation builder.
16
+ **/
17
+
18
+
19
+ /*#### Creating the VALUE wrapper of C simulation objects. */
20
+
21
+ // #define rcsim_wrapper(TYPE) \
22
+ // static const rb_data_type_t TYPE ## _ruby = { \
23
+ // #TYPE ,\
24
+ // {0, free_dbm, memsize_dbm,},\
25
+ // 0, 0,\
26
+ // RUBY_TYPED_FREE_IMMEDIATELY, }
27
+ //
28
+ // #define rcsim_to_value(TYPE, POINTER, VALUE) \
29
+ // (VALUE) = TypedData_Make_Struct(klass, TYPE , &( TYPE ## _ruby ), (POINTER) )
30
+ //
31
+ // #define value_to_rcsim(VALUE, TYPE, POINTER) \
32
+ // TypedData_Get_Struct((VALUE), TYPE , &( TYPE ## _ruby ), (POINTER) );
33
+ //
34
+ // rcsim_wrapper(Type);
35
+ // rcsim_wrapper(SystemT);
36
+ // rcsim_wrapper(Scope);
37
+ // rcsim_wrapper(Behavior);
38
+ // rcsim_wrapper(Event);
39
+ // rcsim_wrapper(SignalI);
40
+ // rcsim_wrapper(SystemI);
41
+ // rcsim_wrapper(Statement);
42
+ // rcsim_wrapper(Transmit);
43
+ // rcsim_wrapper(Print);
44
+ // rcsim_wrapper(TimeWait);
45
+ // rcsim_wrapper(TimeTerminate);
46
+ // rcsim_wrapper(HIf);
47
+ // rcsim_wrapper(HCase);
48
+ // rcsim_wrapper(Block);
49
+ // rcsim_wrapper(Value);
50
+ // rcsim_wrapper(Expression);
51
+ // rcsim_wrapper(Cast);
52
+ // rcsim_wrapper(Unary);
53
+ // rcsim_wrapper(Binary);
54
+ // rcsim_wrapper(Select);
55
+ // rcsim_wrapper(Concat);
56
+ // rcsim_wrapper(Reference);
57
+ // rcsim_wrapper(RefConcat);
58
+ // rcsim_wrapper(RefIndex);
59
+ // rcsim_wrapper(RefRange);
60
+
61
+ static VALUE RCSimPointer;
62
+
63
+ #define rcsim_to_value(TYPE,POINTER,VALUE) \
64
+ (VALUE) = Data_Wrap_Struct(RCSimPointer, 0, 0, (POINTER))
65
+ // (VALUE) = Data_Wrap_Struct(RCSimPointer, 0, free, (POINTER))
66
+ // (VALUE) = ULL2NUM((unsigned long long)(POINTER))
67
+
68
+ #define value_to_rcsim(TYPE,VALUE,POINTER) \
69
+ Data_Get_Struct((VALUE),TYPE,(POINTER))
70
+ // (POINTER) = (TYPE*)NUM2ULL((VALUE))
71
+
72
+ /*#### Generates the list of ID coressponding to the HDLRuby symbols. ####*/
73
+
74
+ static ID id_ANYEDGE;
75
+ static ID id_POSEDGE;
76
+ static ID id_NEGEDGE;
77
+ static ID id_PAR;
78
+ static ID id_SEQ;
79
+ // static ID id_NOT;
80
+ // static ID id_NEG;
81
+ // static ID id_ADD;
82
+ // static ID id_SUB;
83
+ // static ID id_MUL;
84
+ // static ID id_DIV;
85
+ // static ID id_MOD;
86
+ // static ID id_POW;
87
+ // static ID id_AND;
88
+ // static ID id_OR ;
89
+ // static ID id_XOR;
90
+ // static ID id_SHL;
91
+ // static ID id_SHR;
92
+ // static ID id_EQ;
93
+ // static ID id_NE;
94
+ // static ID id_LT;
95
+ // static ID id_LE;
96
+ // static ID id_GT;
97
+ // static ID id_GE;
98
+
99
+ void make_sym_IDs() {
100
+ id_ANYEDGE = rb_intern("anyedge");
101
+ id_POSEDGE = rb_intern("posedge");
102
+ id_NEGEDGE = rb_intern("negedge");
103
+ id_PAR = rb_intern("par");
104
+ id_SEQ = rb_intern("seq");
105
+ // id_NOT = rb_intern("~");
106
+ // id_NEG = rb_intern("-@");
107
+ // id_ADD = rb_intern("+");
108
+ // id_SUB = rb_intern("-");
109
+ // id_MUL = rb_intern("*");
110
+ // id_DIV = rb_intern("/");
111
+ // id_MOD = rb_intern("%");
112
+ // id_POW = rb_intern("**");
113
+ // id_AND = rb_intern("&");
114
+ // id_OR = rb_intern("|");
115
+ // id_XOR = rb_intern("^");
116
+ // id_SHL = rb_intern("<<");
117
+ // id_SHR = rb_intern(">>");
118
+ // id_EQ = rb_intern("==");
119
+ // id_NE = rb_intern("!=");
120
+ // id_LT = rb_intern("<");
121
+ // id_LE = rb_intern("<=");
122
+ // id_GT = rb_intern(">");
123
+ // id_GE = rb_intern(">=");
124
+ }
125
+
126
+ /** Converts a symbol to a char value.
127
+ * NOTE: only works for one or two ASCII characters symbols. */
128
+ static unsigned char sym_to_char(VALUE sym) {
129
+ const char* sym_ptr = rb_id2name(SYM2ID(sym));
130
+ // printf("sym_ptr=%s char=%i\n",sym_ptr,(unsigned char)(sym_ptr[0]+sym_ptr[1]*2));
131
+ return (unsigned char)(sym_ptr[0]+sym_ptr[1]*2);
132
+ }
133
+
134
+
135
+ /*#### Getting the C simulation type objects. ####*/
136
+
137
+ /* Get the bit type. */
138
+ VALUE rcsim_get_type_bit(VALUE mod) {
139
+ VALUE res;
140
+ rcsim_to_value(TypeS,get_type_bit(),res);
141
+ return res;
142
+ }
143
+
144
+ /* Get the signed type. */
145
+ VALUE rcsim_get_type_signed(VALUE mod) {
146
+ VALUE res;
147
+ rcsim_to_value(TypeS,get_type_signed(),res);
148
+ return res;
149
+ }
150
+
151
+ /* Get a vector type. */
152
+ VALUE rcsim_get_type_vector(VALUE mod, VALUE baseV, VALUE numV) {
153
+ /* Get the base type. */
154
+ Type base;
155
+ value_to_rcsim(TypeS,baseV,base);
156
+ /* Get the number of elements. */
157
+ unsigned long long num = NUM2LL(numV);
158
+ /* Get the type. */
159
+ Type type = get_type_vector(base,num);
160
+ /* Return it as a Ruby VALUE. */
161
+ VALUE res;
162
+ rcsim_to_value(TypeS,type,res);
163
+ return res;
164
+ }
165
+
166
+
167
+ /*#### Creating the C simulation objects. ####*/
168
+
169
+ /* Creating a systemT C object. */
170
+ VALUE rcsim_make_systemT(VALUE mod, VALUE name) {
171
+ /* Allocates the systemT. */
172
+ SystemT systemT = (SystemT)malloc(sizeof(SystemTS));
173
+ /* Set it up. */
174
+ systemT->kind = SYSTEMT;
175
+ systemT->owner = NULL;
176
+ systemT->name = strdup(StringValueCStr(name));
177
+ systemT->num_inputs = 0;
178
+ systemT->inputs = NULL;
179
+ systemT->num_outputs = 0;
180
+ systemT->outputs = NULL;
181
+ systemT->num_inouts = 0;
182
+ systemT->inouts = NULL;
183
+ systemT->scope = NULL;
184
+ // printf("Created systemT with kind=%d and name=%s\n",systemT->kind,systemT->name);
185
+ /* Returns the C systemT embedded into a ruby VALUE. */
186
+ VALUE res;
187
+ rcsim_to_value(SystemTS,systemT,res);
188
+ return res;
189
+ }
190
+
191
+
192
+ /* Creating a scope C object. */
193
+ VALUE rcsim_make_scope(VALUE mod, VALUE name) {
194
+ /* Allocates the scope. */
195
+ Scope scope = (Scope)malloc(sizeof(ScopeS));
196
+ /* Set it up. */
197
+ scope->kind = SCOPE;
198
+ scope->owner = NULL;
199
+ scope->name = strdup(StringValueCStr(name));
200
+ scope->num_systemIs = 0;
201
+ scope->systemIs = NULL;
202
+ scope->num_inners = 0;
203
+ scope->inners = NULL;
204
+ scope->num_scopes = 0;
205
+ scope->scopes = NULL;
206
+ scope->num_behaviors = 0;
207
+ scope->behaviors = NULL;
208
+ scope->num_codes = 0;
209
+ scope->codes = NULL;
210
+ /* Returns the C scope embedded into a ruby VALUE. */
211
+ VALUE res;
212
+ rcsim_to_value(ScopeS,scope,res);
213
+ return res;
214
+ }
215
+
216
+
217
+ /* Creating a behavior C object. */
218
+ VALUE rcsim_make_behavior(VALUE mod, VALUE timed) {
219
+ /* Allocates the behavior. */
220
+ Behavior behavior = (Behavior)malloc(sizeof(BehaviorS));
221
+ // printf("new behavior=%p\n",behavior);
222
+ /* Set it up. */
223
+ behavior->kind = BEHAVIOR;
224
+ behavior->owner = NULL;
225
+ behavior->num_events = 0;
226
+ behavior->events = NULL;
227
+ behavior->block = NULL;
228
+ behavior->enabled = 0;
229
+ behavior->activated = 0;
230
+ if (TYPE(timed) == T_TRUE) {
231
+ /* The behavior is timed, set it up and register it. */
232
+ behavior->timed = 1;
233
+ register_timed_behavior(behavior);
234
+ } else {
235
+ /* The behavior is not timed. */
236
+ behavior->timed = 0;
237
+ }
238
+ behavior->active_time = 0;
239
+ behavior->thread = NULL;
240
+ /* Returns the C behavior embedded into a ruby VALUE. */
241
+ VALUE res;
242
+ rcsim_to_value(BehaviorS,behavior,res);
243
+ return res;
244
+ }
245
+
246
+
247
+ /* Creating an event C object. */
248
+ VALUE rcsim_make_event(VALUE mod, VALUE typeV, VALUE sigV) {
249
+ /* Allocates the event. */
250
+ Event event = (Event)malloc(sizeof(EventS));
251
+ /* Set it up. */
252
+ event->kind = EVENT;
253
+ event->owner = NULL;
254
+ /* Its type. */
255
+ ID id_edge = SYM2ID(typeV);
256
+ if (id_edge == id_POSEDGE) { event->edge = POSEDGE; }
257
+ else if (id_edge == id_NEGEDGE) { event->edge = NEGEDGE; }
258
+ else if (id_edge == id_ANYEDGE) { event->edge = ANYEDGE; }
259
+ else { perror("Invalid edge type."); }
260
+ /* Its signal. */
261
+ value_to_rcsim(SignalIS,sigV,event->signal);
262
+ /* Returns the C event embedded into a ruby VALUE. */
263
+ VALUE res;
264
+ rcsim_to_value(EventS,event,res);
265
+ return res;
266
+ }
267
+
268
+
269
+ /* Creating a signal C object. */
270
+ VALUE rcsim_make_signal(VALUE mod, VALUE name, VALUE type) {
271
+ /* Allocates the signal. */
272
+ SignalI signal = (SignalI)malloc(sizeof(SignalIS));
273
+ /* Set it up. */
274
+ signal->kind = SIGNALI;
275
+ signal->owner = NULL;
276
+ signal->name = strdup(StringValueCStr(name));
277
+ // printf("Creating signal named=%s\n",signal->name);
278
+ value_to_rcsim(TypeS,type,signal->type);
279
+ // printf("type width=%llu\n",type_width(signal->type));
280
+ signal->c_value = make_value(signal->type,0);
281
+ signal->c_value->signal = signal;
282
+ // printf("c_value=%p type=%p\n",signal->c_value,signal->c_value->type);
283
+ // printf("c_value type width=%llu\n",type_width(signal->c_value->type));
284
+ signal->f_value = make_value(signal->type,0);
285
+ signal->f_value->signal = signal;
286
+ signal->fading = 1; /* Initially the signal can be overwritten by anything.*/
287
+ signal->num_any = 0;
288
+ signal->any = NULL;
289
+ signal->num_pos = 0;
290
+ signal->pos = NULL;
291
+ signal->num_neg = 0;
292
+ signal->neg = NULL;
293
+ /* Register the signal. */
294
+ register_signal(signal);
295
+ /* Returns the C signal embedded into a ruby VALUE. */
296
+ VALUE res;
297
+ rcsim_to_value(SignalIS,signal,res);
298
+ return res;
299
+ }
300
+
301
+
302
+ /* Creating a system instance C object. */
303
+ VALUE rcsim_make_systemI(VALUE mod, VALUE name, VALUE systemT) {
304
+ /* Allocates the system instance. */
305
+ SystemI systemI = (SystemI)malloc(sizeof(SystemIS));
306
+ /* Set it up. */
307
+ systemI->kind = SYSTEMI;
308
+ systemI->owner = NULL;
309
+ systemI->name = strdup(StringValueCStr(name));
310
+ // /* Name is made empty since redundant with Eigen system. */
311
+ // systemI->name = "";
312
+ value_to_rcsim(SystemTS,systemT,systemI->system);
313
+ systemI->num_systems = 1;
314
+ systemI->systems = (SystemT*)malloc(sizeof(SystemT));
315
+ systemI->systems[0] = systemI->system;
316
+ /* Configure the systemI to execute the default systemT. */
317
+ configure(systemI,0);
318
+ /* Returns the C system instance embedded into a ruby VALUE. */
319
+ VALUE res;
320
+ rcsim_to_value(SystemIS,systemI,res);
321
+ return res;
322
+ }
323
+
324
+
325
+ /* Creating a transmit C object. */
326
+ VALUE rcsim_make_transmit(VALUE mod, VALUE left, VALUE right) {
327
+ /* Allocates the transmit. */
328
+ Transmit transmit = (Transmit)malloc(sizeof(TransmitS));
329
+ /* Set it up. */
330
+ transmit->kind = TRANSMIT;
331
+ value_to_rcsim(ReferenceS,left,transmit->left);
332
+ value_to_rcsim(ExpressionS,right,transmit->right);
333
+ /* Returns the C transmit embedded into a ruby VALUE. */
334
+ VALUE res;
335
+ rcsim_to_value(TransmitS,transmit,res);
336
+ return res;
337
+ }
338
+
339
+
340
+ /* Creating a print C object. */
341
+ VALUE rcsim_make_print(VALUE mod) {
342
+ /* Allocates the print. */
343
+ Print print = (Print)malloc(sizeof(PrintS));
344
+ /* Set it up. */
345
+ print->kind = PRINT;
346
+ print->num_args = 0;
347
+ print->args = NULL;
348
+ /* Returns the C print embedded into a ruby VALUE. */
349
+ VALUE res;
350
+ rcsim_to_value(PrintS,print,res);
351
+ return res;
352
+ }
353
+
354
+
355
+ /* Creating a time wait C object. */
356
+ VALUE rcsim_make_timeWait(VALUE mod, VALUE unitV, VALUE delayV) {
357
+ /* Allocates the time wait. */
358
+ TimeWait timeWait = (TimeWait)malloc(sizeof(TimeWaitS));
359
+ /* Set it up. */
360
+ timeWait->kind = TIME_WAIT;
361
+ /* Compute the delay. */
362
+ unsigned long long delay;
363
+ delay = NUM2LL(delayV);
364
+ /* Adjust the delay depending on the unit. */
365
+ const char* unit = rb_id2name(SYM2ID(unitV));
366
+ switch(unit[0]) {
367
+ case 'p': /* Ok as is. */ break;
368
+ case 'n': delay *= 1000; break;
369
+ case 'u': delay *= 1000000; break;
370
+ case 'm': delay *= 1000000000; break;
371
+ case 's': delay *= 1000000000000; break;
372
+ default:
373
+ perror("Invalid delay unit.");
374
+ }
375
+ timeWait->delay = delay;
376
+ /* Returns the C time wait embedded into a ruby VALUE. */
377
+ VALUE res;
378
+ rcsim_to_value(TimeWaitS,timeWait,res);
379
+ return res;
380
+ }
381
+
382
+
383
+ /* Creating a time terminate C object. */
384
+ VALUE rcsim_make_timeTerminate(VALUE mod) {
385
+ /* Allocates the time terminate. */
386
+ TimeTerminate timeTerminate = (TimeTerminate)malloc(sizeof(TimeTerminateS));
387
+ /* Set it up. */
388
+ timeTerminate->kind = TIME_TERMINATE;
389
+ /* Returns the C time terminate embedded into a ruby VALUE. */
390
+ VALUE res;
391
+ rcsim_to_value(TimeTerminateS,timeTerminate,res);
392
+ return res;
393
+ }
394
+
395
+
396
+ /* Creating a hardware if C object. */
397
+ VALUE rcsim_make_hif(VALUE mod, VALUE condition, VALUE yes, VALUE no) {
398
+ /* Allocates the hardware if. */
399
+ HIf hif = (HIf)malloc(sizeof(HIfS));
400
+ /* Set it up. */
401
+ hif->kind = HIF;
402
+ value_to_rcsim(ExpressionS,condition,hif->condition);
403
+ value_to_rcsim(StatementS,yes,hif->yes);
404
+ if (TYPE(no) == T_NIL)
405
+ hif->no = NULL;
406
+ else
407
+ value_to_rcsim(StatementS,no,hif->no);
408
+ hif->num_noifs = 0;
409
+ hif->noconds = NULL;
410
+ hif->nostmnts = NULL;
411
+ /* Returns the C hardware if embedded into a ruby VALUE. */
412
+ VALUE res;
413
+ rcsim_to_value(HIfS,hif,res);
414
+ return res;
415
+ }
416
+
417
+
418
+ /* Creating a hardware case C object. */
419
+ VALUE rcsim_make_hcase(VALUE mod, VALUE value, VALUE defolt) {
420
+ /* Allocates the hardware case. */
421
+ HCase hcase = (HCase)malloc(sizeof(HCaseS));
422
+ /* Set it up. */
423
+ hcase->kind = HCASE;
424
+ value_to_rcsim(ExpressionS,value,hcase->value);
425
+ hcase->num_whens = 0;
426
+ hcase->matches = NULL;
427
+ hcase->stmnts = NULL;
428
+ if (TYPE(defolt) == T_NIL)
429
+ hcase->defolt = NULL;
430
+ else
431
+ value_to_rcsim(StatementS,defolt,hcase->defolt);
432
+ /* Returns the C hardware case embedded into a ruby VALUE. */
433
+ VALUE res;
434
+ rcsim_to_value(HCaseS,hcase,res);
435
+ return res;
436
+ }
437
+
438
+
439
+ /* Creating a block C object. */
440
+ VALUE rcsim_make_block(VALUE mod, VALUE mode) {
441
+ /* Allocates the block. */
442
+ Block block = (Block)malloc(sizeof(BlockS));
443
+ /* Set it up. */
444
+ block->kind = BLOCK;
445
+ block->owner = NULL;
446
+ block->name = NULL;
447
+ block->num_inners = 0;
448
+ block->inners = NULL;
449
+ block->num_stmnts = 0;
450
+ block->stmnts = NULL;
451
+ block->mode = SYM2ID(mode) == id_PAR ? PAR : SEQ;
452
+ /* Returns the C block embedded into a ruby VALUE. */
453
+ VALUE res;
454
+ rcsim_to_value(BlockS,block,res);
455
+ return res;
456
+ }
457
+
458
+
459
+ /* Creating a numeric value C object. */
460
+ VALUE rcsim_make_value_numeric(VALUE mod, VALUE typeV, VALUE contentV) {
461
+ // /* Allocates the value. */
462
+ // Value value = get_value();
463
+ // /* Sets its type. */
464
+ // value_to_rcsim(TypeS,typeV,value->type);
465
+ /* Get the type. */
466
+ Type type;
467
+ value_to_rcsim(TypeS,typeV,type);
468
+ /* Create the value. */
469
+ Value value = make_value(type,0);
470
+ /* Set it to numeric. */
471
+ value->numeric = 1;
472
+ value->capacity = 0;
473
+ value->data_str = NULL;
474
+ value->data_int = NUM2LL(contentV);
475
+ /* Returns the C value embedded into a ruby VALUE. */
476
+ VALUE res;
477
+ rcsim_to_value(ValueS,value,res);
478
+ return res;
479
+ }
480
+
481
+
482
+ /* Creating a bitstring value C object. */
483
+ VALUE rcsim_make_value_bitstring(VALUE mod, VALUE typeV, VALUE contentV) {
484
+ // /* Allocates the value. */
485
+ // Value value = get_value();
486
+ // /* Sets its type. */
487
+ // value_to_rcsim(TypeS,type,value->type);
488
+ /* Get the type. */
489
+ Type type;
490
+ value_to_rcsim(TypeS,typeV,type);
491
+ /* Create the value. */
492
+ Value value = make_value(type,0);
493
+ // printf("Created from bitstring value=%p with type=%p\n",value,value->type);
494
+ // printf("and width=%llu\n",type_width(value->type));
495
+ /* Set it to bitstring. */
496
+ value->numeric = 0;
497
+ /* Generate the string of the content. */
498
+ char* str = StringValueCStr(contentV);
499
+ value->capacity = strlen(str)+1;
500
+ value->data_str = calloc(sizeof(char),value->capacity);
501
+ strcpy(value->data_str,str);
502
+ /* Returns the C value embedded into a ruby VALUE. */
503
+ VALUE res;
504
+ rcsim_to_value(ValueS,value,res);
505
+ return res;
506
+ }
507
+
508
+
509
+ /* Creating a cast C object. */
510
+ VALUE rcsim_make_cast(VALUE mod, VALUE type, VALUE child) {
511
+ /* Allocates the cast. */
512
+ Cast cast = (Cast)malloc(sizeof(CastS));
513
+ /* Set it up. */
514
+ cast->kind = CAST;
515
+ value_to_rcsim(TypeS,type,cast->type);
516
+ value_to_rcsim(ExpressionS,child,cast->child);
517
+ /* Returns the C cast embedded into a ruby VALUE. */
518
+ VALUE res;
519
+ rcsim_to_value(CastS,cast,res);
520
+ return res;
521
+ }
522
+
523
+ /* Creating a unary value C object. */
524
+ VALUE rcsim_make_unary(VALUE mod, VALUE type, VALUE operator, VALUE child) {
525
+ /* Allocates the unary. */
526
+ Unary unary= (Unary)malloc(sizeof(UnaryS));
527
+ /* Set it up. */
528
+ unary->kind = UNARY;
529
+ value_to_rcsim(TypeS,type,unary->type);
530
+ switch(sym_to_char(operator)) {
531
+ case (unsigned char)'~': unary->oper = not_value; break;
532
+ case (unsigned char)('-'+'@'*2): unary->oper = neg_value; break;
533
+ default: perror("Invalid operator for unary.");
534
+ }
535
+ value_to_rcsim(ExpressionS,child,unary->child);
536
+ /* Returns the C unary embedded into a ruby VALUE. */
537
+ VALUE res;
538
+ rcsim_to_value(UnaryS,unary,res);
539
+ return res;
540
+ }
541
+
542
+ /* Creating a binary value C object. */
543
+ VALUE rcsim_make_binary(VALUE mod, VALUE type, VALUE operator, VALUE left, VALUE right) {
544
+ /* Allocates the binary. */
545
+ Binary binary = (Binary)malloc(sizeof(BinaryS));
546
+ /* Set it up. */
547
+ binary->kind = BINARY;
548
+ value_to_rcsim(TypeS,type,binary->type);
549
+ switch(sym_to_char(operator)) {
550
+ case (unsigned char)'+': binary->oper = add_value; break;
551
+ case (unsigned char)'-': binary->oper = sub_value; break;
552
+ case (unsigned char)'*': binary->oper = mul_value; break;
553
+ case (unsigned char)'/': binary->oper = div_value; break;
554
+ case (unsigned char)'%': binary->oper = mod_value; break;
555
+ case (unsigned char)'&': binary->oper = and_value; break;
556
+ case (unsigned char)'|': binary->oper = or_value; break;
557
+ case (unsigned char)'^': binary->oper = xor_value; break;
558
+ case (unsigned char)('<'+'<'*2): binary->oper = shift_left_value; break;
559
+ case (unsigned char)('>'+'>'*2): binary->oper = shift_right_value; break;
560
+ case (unsigned char)('='+'='*2): binary->oper = equal_value_c; break;
561
+ case (unsigned char)('!'+'='*2): binary->oper = not_equal_value_c; break;
562
+ case (unsigned char)'<': binary->oper = lesser_value; break;
563
+ case (unsigned char)('<'+'='*2): binary->oper = lesser_equal_value; break;
564
+ case (unsigned char)'>': binary->oper = greater_value; break;
565
+ case (unsigned char)('>'+'='*2): binary->oper = greater_equal_value; break;
566
+ default: perror("Invalid operator for binary.");
567
+ }
568
+ value_to_rcsim(ExpressionS,left,binary->left);
569
+ value_to_rcsim(ExpressionS,right,binary->right);
570
+ /* Returns the C binary embedded into a ruby VALUE. */
571
+ VALUE res;
572
+ rcsim_to_value(BinaryS,binary,res);
573
+ return res;
574
+ }
575
+
576
+ /* Creating a select C object. */
577
+ VALUE rcsim_make_select(VALUE mod, VALUE type, VALUE sel) {
578
+ /* Allocates the select. */
579
+ Select select = (Select)malloc(sizeof(SelectS));
580
+ /* Set it up. */
581
+ select->kind = SELECT;
582
+ value_to_rcsim(TypeS,type,select->type);
583
+ value_to_rcsim(ExpressionS,sel,select->select);
584
+ select->num_choices = 0;
585
+ select->choices = NULL;
586
+ /* Returns the C select embedded into a ruby VALUE. */
587
+ VALUE res;
588
+ rcsim_to_value(SelectS,select,res);
589
+ return res;
590
+ }
591
+
592
+ /* Creating a concat C object. */
593
+ VALUE rcsim_make_concat(VALUE mod, VALUE type, VALUE dirV) {
594
+ /* Allocates the concat. */
595
+ Concat concat = (Concat)malloc(sizeof(ConcatS));
596
+ /* Set it up. */
597
+ concat->kind = CONCAT;
598
+ value_to_rcsim(TypeS,type,concat->type);
599
+ concat->num_exprs = 0;
600
+ concat->exprs = NULL;
601
+ concat->dir = rb_id2name(SYM2ID(dirV))[0]=='l' ? 1 : 0;
602
+ /* Returns the C concat embedded into a ruby VALUE. */
603
+ VALUE res;
604
+ rcsim_to_value(ConcatS,concat,res);
605
+ return res;
606
+ }
607
+
608
+ /* Creating a ref concat C object. */
609
+ VALUE rcsim_make_refConcat(VALUE mod, VALUE type, VALUE dirV) {
610
+ /* Allocates the ref concat. */
611
+ RefConcat refConcat = (RefConcat)malloc(sizeof(RefConcatS));
612
+ /* Set it up. */
613
+ refConcat->kind = REF_CONCAT;
614
+ value_to_rcsim(TypeS,type,refConcat->type);
615
+ refConcat->num_refs = 0;
616
+ refConcat->refs = NULL;
617
+ refConcat->dir = rb_id2name(SYM2ID(dirV))[0]=='l' ? 0 : 1;
618
+ /* Returns the C ref concat embedded into a ruby VALUE. */
619
+ VALUE res;
620
+ rcsim_to_value(RefConcatS,refConcat,res);
621
+ return res;
622
+ }
623
+
624
+ /* Creating a ref index C object. */
625
+ VALUE rcsim_make_refIndex(VALUE mod, VALUE type, VALUE index, VALUE ref) {
626
+ /* Allocates the ref index. */
627
+ RefIndex refIndex = (RefIndex)malloc(sizeof(RefIndexS));
628
+ /* Set it up. */
629
+ refIndex->kind = REF_INDEX;
630
+ value_to_rcsim(TypeS,type,refIndex->type);
631
+ value_to_rcsim(ExpressionS,index,refIndex->index);
632
+ value_to_rcsim(ReferenceS,ref,refIndex->ref);
633
+ /* Returns the C ref index embedded into a ruby VALUE. */
634
+ VALUE res;
635
+ rcsim_to_value(RefIndexS,refIndex,res);
636
+ return res;
637
+ }
638
+
639
+ /* Creating a ref range C object. */
640
+ VALUE rcsim_make_refRange(VALUE mod, VALUE type, VALUE first, VALUE last, VALUE ref) {
641
+ /* Allocates the ref range. */
642
+ RefRangeE refRange = (RefRangeE)malloc(sizeof(RefRangeES));
643
+ /* Set it up. */
644
+ refRange->kind = REF_RANGE;
645
+ value_to_rcsim(TypeS,type,refRange->type);
646
+ value_to_rcsim(ExpressionS,first,refRange->first);
647
+ value_to_rcsim(ExpressionS,last,refRange->last);
648
+ value_to_rcsim(ReferenceS,ref,refRange->ref);
649
+ /* Returns the C ref range embedded into a ruby VALUE. */
650
+ VALUE res;
651
+ rcsim_to_value(RefRangeES,refRange,res);
652
+ return res;
653
+ }
654
+
655
+
656
+ /* Creating a character string C object. */
657
+ VALUE rcsim_make_stringE(VALUE mod, VALUE strV) {
658
+ /* Allocates the string. */
659
+ StringE stringE = (StringE)malloc(sizeof(StringES));
660
+ /* Set it up. */
661
+ stringE->kind = STRINGE;
662
+ stringE->str = strdup(StringValueCStr(strV));
663
+ /* Returns the C character string embedded into a ruby VALUE. */
664
+ VALUE res;
665
+ rcsim_to_value(StringES,stringE,res);
666
+ return res;
667
+ }
668
+
669
+ /*#### Adding elements to C simulation objects. ####*/
670
+
671
+ /* Adds inputs to a C systemT. */
672
+ VALUE rcsim_add_systemT_inputs(VALUE mod, VALUE systemTV, VALUE sigVs) {
673
+ /* Get the C systemT from the Ruby value. */
674
+ SystemT systemT;
675
+ value_to_rcsim(SystemTS,systemTV,systemT);
676
+ // printf("Adding to systemT with kind=%d and name=%s\n",systemT->kind, systemT->name);
677
+ /* Prepare the size for the inputs. */
678
+ long num = RARRAY_LEN(sigVs);
679
+ long old_num = systemT->num_inputs;
680
+ systemT->num_inputs += num;
681
+ systemT->inputs=realloc(systemT->inputs,sizeof(SignalI)*systemT->num_inputs);
682
+ // printf("size=%d num=%i\n",malloc_size(systemT->inputs),systemT->num_inputs);
683
+ // printf("required size=%lu\n",sizeof(SignalI)*systemT->num_inputs);
684
+ /* Get and add the signals from the Ruby value. */
685
+ for(int i=0; i< num; ++i) {
686
+ SignalI sig;
687
+ value_to_rcsim(SignalIS,rb_ary_entry(sigVs,i),sig);
688
+ // printf("old_num+i=%ld\n",old_num+i);
689
+ systemT->inputs[old_num + i] = sig;
690
+ }
691
+ return systemTV;
692
+ }
693
+
694
+ /* Adds outputs to a C systemT. */
695
+ VALUE rcsim_add_systemT_outputs(VALUE mod, VALUE systemTV, VALUE sigVs) {
696
+ /* Get the C systemT from the Ruby value. */
697
+ SystemT systemT;
698
+ value_to_rcsim(SystemTS,systemTV,systemT);
699
+ /* Prepare the size for the outputs. */
700
+ long num = RARRAY_LEN(sigVs);
701
+ long old_num = systemT->num_outputs;
702
+ systemT->num_outputs += num;
703
+ systemT->outputs =realloc(systemT->outputs,sizeof(SignalI)*systemT->num_outputs);
704
+ /* Get and add the signals from the Ruby value. */
705
+ for(int i=0; i< num; ++i) {
706
+ SignalI sig;
707
+ value_to_rcsim(SignalIS,rb_ary_entry(sigVs,i),sig);
708
+ systemT->outputs[old_num + i] = sig;
709
+ }
710
+ return systemTV;
711
+ }
712
+
713
+ /* Adds inouts to a C systemT. */
714
+ VALUE rcsim_add_systemT_inouts(VALUE mod, VALUE systemTV, VALUE sigVs) {
715
+ /* Get the C systemT from the Ruby value. */
716
+ SystemT systemT;
717
+ value_to_rcsim(SystemTS,systemTV,systemT);
718
+ /* Prepare the size for the inouts. */
719
+ long num = RARRAY_LEN(sigVs);
720
+ long old_num = systemT->num_inouts;
721
+ systemT->num_inouts += num;
722
+ systemT->inouts =realloc(systemT->inouts,sizeof(SignalI)*systemT->num_inouts);
723
+ /* Get and add the signals from the Ruby value. */
724
+ for(int i=0; i< num; ++i) {
725
+ SignalI sig;
726
+ value_to_rcsim(SignalIS,rb_ary_entry(sigVs,i),sig);
727
+ systemT->inouts[old_num + i] = sig;
728
+ }
729
+ return systemTV;
730
+ }
731
+
732
+ /* Adds inners to a C scope. */
733
+ VALUE rcsim_add_scope_inners(VALUE mod, VALUE scopeV, VALUE sigVs) {
734
+ /* Get the C scope from the Ruby value. */
735
+ Scope scope;
736
+ value_to_rcsim(ScopeS,scopeV,scope);
737
+ /* Prepare the size for the inners. */
738
+ long num = RARRAY_LEN(sigVs);
739
+ long old_num = scope->num_inners;
740
+ scope->num_inners += num;
741
+ scope->inners = realloc(scope->inners,sizeof(SignalI)*scope->num_inners);
742
+ /* Get and add the signals from the Ruby value. */
743
+ for(int i=0; i< num; ++i) {
744
+ SignalI sig;
745
+ value_to_rcsim(SignalIS,rb_ary_entry(sigVs,i),sig);
746
+ scope->inners[old_num + i] = sig;
747
+ }
748
+ return scopeV;
749
+ }
750
+
751
+ /* Adds behaviors to a C scope. */
752
+ VALUE rcsim_add_scope_behaviors(VALUE mod, VALUE scopeV, VALUE behVs) {
753
+ /* Get the C scope from the Ruby value. */
754
+ Scope scope;
755
+ value_to_rcsim(ScopeS,scopeV,scope);
756
+ /* Prepare the size for the behaviors. */
757
+ long num = RARRAY_LEN(behVs);
758
+ long old_num = scope->num_behaviors;
759
+ scope->num_behaviors += num;
760
+ scope->behaviors = realloc(scope->behaviors,
761
+ sizeof(Behavior)*scope->num_behaviors);
762
+ /* Get and add the behaviors from the Ruby value. */
763
+ for(int i=0; i< num; ++i) {
764
+ Behavior beh;
765
+ value_to_rcsim(BehaviorS,rb_ary_entry(behVs,i),beh);
766
+ scope->behaviors[old_num + i] = beh;
767
+ }
768
+ return scopeV;
769
+ }
770
+
771
+ /* Adds system instances to a C scope. */
772
+ VALUE rcsim_add_scope_systemIs(VALUE mod, VALUE scopeV, VALUE sysVs) {
773
+ /* Get the C scope from the Ruby value. */
774
+ Scope scope;
775
+ value_to_rcsim(ScopeS,scopeV,scope);
776
+ /* Prepare the size for the system instances. */
777
+ long num = RARRAY_LEN(sysVs);
778
+ long old_num = scope->num_systemIs;
779
+ scope->num_systemIs += num;
780
+ scope->systemIs = realloc(scope->systemIs,
781
+ sizeof(SystemI)*scope->num_systemIs);
782
+ /* Get and add the system instances from the Ruby value. */
783
+ for(int i=0; i< num; ++i) {
784
+ SystemI sys;
785
+ value_to_rcsim(SystemIS,rb_ary_entry(sysVs,i),sys);
786
+ scope->systemIs[old_num + i] = sys;
787
+ }
788
+ return scopeV;
789
+ }
790
+
791
+ /* Adds sub scopes to a C scope. */
792
+ VALUE rcsim_add_scope_scopes(VALUE mod, VALUE scopeV, VALUE scpVs) {
793
+ /* Get the C scope from the Ruby value. */
794
+ Scope scope;
795
+ value_to_rcsim(ScopeS,scopeV,scope);
796
+ /* Prepare the size for the sub scopes. */
797
+ long num = RARRAY_LEN(scpVs);
798
+ long old_num = scope->num_scopes;
799
+ scope->num_scopes += num;
800
+ scope->scopes = realloc(scope->scopes,
801
+ sizeof(Scope)*scope->num_scopes);
802
+ /* Get and add the sub scopes from the Ruby value. */
803
+ for(int i=0; i< num; ++i) {
804
+ Scope scp;
805
+ value_to_rcsim(ScopeS,rb_ary_entry(scpVs,i),scp);
806
+ scope->scopes[old_num + i] = scp;
807
+ }
808
+ return scopeV;
809
+ }
810
+
811
+ /* Adds events to a C behavior. */
812
+ VALUE rcsim_add_behavior_events(VALUE mod, VALUE behaviorV, VALUE eventVs) {
813
+ /* Get the C behavior from the Ruby value. */
814
+ Behavior behavior;
815
+ value_to_rcsim(BehaviorS,behaviorV,behavior);
816
+ /* Prepare the size for the events. */
817
+ long num = RARRAY_LEN(eventVs);
818
+ long old_num = behavior->num_events;
819
+ behavior->num_events += num;
820
+ behavior->events = realloc(behavior->events,
821
+ sizeof(Event)*behavior->num_events);
822
+ /* Get and add the events from the Ruby value. */
823
+ for(int i=0; i< num; ++i) {
824
+ Event event;
825
+ value_to_rcsim(EventS,rb_ary_entry(eventVs,i),event);
826
+ behavior->events[old_num + i] = event;
827
+ /* Update the signal of the event to say it activates the behavior. */
828
+ SignalI sig = event->signal;
829
+ switch(event->edge) {
830
+ case ANYEDGE:
831
+ sig->num_any++;
832
+ sig->any = realloc(sig->any,sizeof(Object)*sig->num_any);
833
+ sig->any[sig->num_any-1] = (Object)behavior;
834
+ break;
835
+ case POSEDGE:
836
+ sig->num_pos++;
837
+ sig->pos = realloc(sig->pos,sizeof(Object)*sig->num_pos);
838
+ sig->pos[sig->num_pos-1] = (Object)behavior;
839
+ break;
840
+ case NEGEDGE:
841
+ sig->num_neg++;
842
+ sig->neg = realloc(sig->neg,sizeof(Object)*sig->num_neg);
843
+ sig->neg[sig->num_neg-1] = (Object)behavior;
844
+ break;
845
+ default:
846
+ perror("Invalid value for an edge.");
847
+ }
848
+ }
849
+ return behaviorV;
850
+ }
851
+
852
+ /* Adds alternate system types to a C system instance. */
853
+ VALUE rcsim_add_systemI_systemTs(VALUE mod, VALUE systemIV, VALUE sysVs) {
854
+ /* Get the C systemI from the Ruby value. */
855
+ SystemI systemI;
856
+ value_to_rcsim(SystemIS,systemIV,systemI);
857
+ /* Prepare the size for the alternate system types. */
858
+ long num = RARRAY_LEN(sysVs);
859
+ long old_num = systemI->num_systems;
860
+ systemI->num_systems += num;
861
+ systemI->systems=realloc(systemI->systems,sizeof(SystemT)*systemI->num_systems);
862
+ /* Get and add the alternate system types from the Ruby value. */
863
+ for(int i=0; i< num; ++i) {
864
+ SystemT sys;
865
+ value_to_rcsim(SystemTS,rb_ary_entry(sysVs,i),sys);
866
+ systemI->systems[old_num + i] = sys;
867
+ }
868
+ return systemIV;
869
+ }
870
+
871
+ /* Adds arguments to a C print. */
872
+ VALUE rcsim_add_print_args(VALUE mod, VALUE printV, VALUE argVs) {
873
+ /* Get the C print from the Ruby value. */
874
+ Print print;
875
+ value_to_rcsim(PrintS,printV,print);
876
+ /* Prepare the size for the arguments. */
877
+ long num = RARRAY_LEN(argVs);
878
+ long old_num = print->num_args;
879
+ print->num_args += num;
880
+ print->args = realloc(print->args,
881
+ sizeof(Expression)*print->num_args);
882
+ /* Get and add the arguments from the Ruby value. */
883
+ for(int i=0; i< num; ++i) {
884
+ Expression arg;
885
+ value_to_rcsim(ExpressionS,rb_ary_entry(argVs,i),arg);
886
+ print->args[old_num + i] = arg;
887
+ }
888
+ return printV;
889
+ }
890
+
891
+ /* Adds noifs to a C hardware if. */
892
+ VALUE rcsim_add_hif_noifs(VALUE mod, VALUE hifV, VALUE condVs, VALUE stmntVs) {
893
+ /* Get the C hardware if from the Ruby value. */
894
+ HIf hif;
895
+ value_to_rcsim(HIfS,hifV,hif);
896
+ /* Prepare the size for the noifs. */
897
+ long num = RARRAY_LEN(condVs);
898
+ long old_num = hif->num_noifs;
899
+ hif->num_noifs += num;
900
+ hif->noconds = realloc(hif->noconds, sizeof(Expression)*hif->num_noifs);
901
+ hif->nostmnts = realloc(hif->nostmnts, sizeof(Statement)*hif->num_noifs);
902
+ /* Get and add the noifs from the Ruby value. */
903
+ for(int i=0; i< num; ++i) {
904
+ Expression cond;
905
+ Statement stmnt;
906
+ value_to_rcsim(ExpressionS,rb_ary_entry(condVs,i),cond);
907
+ hif->noconds[old_num + i] = cond;
908
+ value_to_rcsim(StatementS,rb_ary_entry(stmntVs,i),stmnt);
909
+ hif->nostmnts[old_num + i] = stmnt;
910
+ }
911
+ return hifV;
912
+ }
913
+
914
+ /* Adds whens to a C hardware case. */
915
+ VALUE rcsim_add_hcase_whens(VALUE mod, VALUE hcaseV, VALUE matchVs, VALUE stmntVs) {
916
+ /* Get the C hardware case from the Ruby value. */
917
+ HCase hcase;
918
+ value_to_rcsim(HCaseS,hcaseV,hcase);
919
+ /* Prepare the size for the noifs. */
920
+ long num = RARRAY_LEN(matchVs);
921
+ long old_num = hcase->num_whens;
922
+ hcase->num_whens += num;
923
+ hcase->matches = realloc(hcase->matches,
924
+ sizeof(Expression)*hcase->num_whens);
925
+ hcase->stmnts = realloc(hcase->stmnts,
926
+ sizeof(Statement)*hcase->num_whens);
927
+ /* Get and add the whens from the Ruby value. */
928
+ for(int i=0; i< num; ++i) {
929
+ Expression match;
930
+ Statement stmnt;
931
+ value_to_rcsim(ExpressionS,rb_ary_entry(matchVs,i),match);
932
+ hcase->matches[old_num + i] = match;
933
+ value_to_rcsim(StatementS,rb_ary_entry(stmntVs,i),stmnt);
934
+ hcase->stmnts[old_num + i] = stmnt;
935
+ }
936
+ return hcaseV;
937
+ }
938
+
939
+ /* Adds inners to a C block. */
940
+ VALUE rcsim_add_block_inners(VALUE mod, VALUE blockV, VALUE sigVs) {
941
+ /* Get the C block from the Ruby value. */
942
+ Block block;
943
+ value_to_rcsim(BlockS,blockV,block);
944
+ /* Prepare the size for the inners. */
945
+ long num = RARRAY_LEN(sigVs);
946
+ long old_num = block->num_inners;
947
+ block->num_inners += num;
948
+ block->inners = realloc(block->inners,sizeof(SignalI)*block->num_inners);
949
+ /* Get and add the signals from the Ruby value. */
950
+ for(int i=0; i< num; ++i) {
951
+ SignalI sig;
952
+ value_to_rcsim(SignalIS,rb_ary_entry(sigVs,i),sig);
953
+ block->inners[old_num + i] = sig;
954
+ }
955
+ return blockV;
956
+ }
957
+
958
+ /* Adds statements to a C block. */
959
+ VALUE rcsim_add_block_statements(VALUE mod, VALUE blockV, VALUE stmntVs) {
960
+ /* Get the C block from the Ruby value. */
961
+ Block block;
962
+ value_to_rcsim(BlockS,blockV,block);
963
+ /* Prepare the size for the statements. */
964
+ long num = RARRAY_LEN(stmntVs);
965
+ long old_num = block->num_stmnts;
966
+ block->num_stmnts += num;
967
+ block->stmnts = realloc(block->stmnts,sizeof(Statement)*block->num_stmnts);
968
+ /* Get and add the statements from the Ruby value. */
969
+ for(int i=0; i< num; ++i) {
970
+ Statement stmnt;
971
+ value_to_rcsim(StatementS,rb_ary_entry(stmntVs,i),stmnt);
972
+ block->stmnts[old_num + i] = stmnt;
973
+ }
974
+ return blockV;
975
+ }
976
+
977
+ /* Adds choices to a C select. */
978
+ VALUE rcsim_add_select_choices(VALUE mod, VALUE selectV, VALUE choiceVs) {
979
+ /* Get the C select from the Ruby value. */
980
+ Select select;
981
+ value_to_rcsim(SelectS,selectV,select);
982
+ /* Prepare the size for the choices. */
983
+ long num = RARRAY_LEN(choiceVs);
984
+ long old_num = select->num_choices;
985
+ select->num_choices += num;
986
+ select->choices = realloc(select->choices,sizeof(Expression)*select->num_choices);
987
+ /* Get and add the choices from the Ruby value. */
988
+ for(int i=0; i< num; ++i) {
989
+ Expression choice;
990
+ value_to_rcsim(ExpressionS,rb_ary_entry(choiceVs,i),choice);
991
+ select->choices[old_num + i] = choice;
992
+ }
993
+ return selectV;
994
+ }
995
+
996
+ /* Adds expressions to a C concat. */
997
+ VALUE rcsim_add_concat_expressions(VALUE mod, VALUE concatV, VALUE exprVs) {
998
+ /* Get the C concat from the Ruby value. */
999
+ Concat concat;
1000
+ value_to_rcsim(ConcatS,concatV,concat);
1001
+ /* Prepare the size for the expressions. */
1002
+ long num = RARRAY_LEN(exprVs);
1003
+ long old_num = concat->num_exprs;
1004
+ // printf("add_concat_expressions with num=%li old_num=%li\n",num,old_num);
1005
+ concat->num_exprs += num;
1006
+ concat->exprs = realloc(concat->exprs,sizeof(Expression)*concat->num_exprs);
1007
+ /* Get and add the expressions from the Ruby value. */
1008
+ for(int i=0; i< num; ++i) {
1009
+ Expression expr;
1010
+ value_to_rcsim(ExpressionS,rb_ary_entry(exprVs,i),expr);
1011
+ // printf("Adding expression with type width=%llu\n",type_width(expr->type));
1012
+ concat->exprs[old_num + i] = expr;
1013
+ }
1014
+ return concatV;
1015
+ }
1016
+
1017
+ /* Adds references to a C ref concat. */
1018
+ VALUE rcsim_add_refConcat_refs(VALUE mod, VALUE refConcatV, VALUE refVs) {
1019
+ /* Get the C refConcat from the Ruby value. */
1020
+ RefConcat refConcat;
1021
+ value_to_rcsim(RefConcatS,refConcatV,refConcat);
1022
+ /* Prepare the size for the references. */
1023
+ long num = RARRAY_LEN(refVs);
1024
+ long old_num = refConcat->num_refs;
1025
+ refConcat->num_refs += num;
1026
+ refConcat->refs = realloc(refConcat->refs,sizeof(Reference)*refConcat->num_refs);
1027
+ /* Get and add the references from the Ruby value. */
1028
+ for(int i=0; i< num; ++i) {
1029
+ Reference ref;
1030
+ value_to_rcsim(ReferenceS,rb_ary_entry(refVs,i),ref);
1031
+ refConcat->refs[old_num + i] = ref;
1032
+ }
1033
+ return refConcatV;
1034
+ }
1035
+
1036
+
1037
+ /*#### Modifying C simulation objects. ####*/
1038
+
1039
+ /** Sets the owner for a C simulation object. */
1040
+ VALUE rcsim_set_owner(VALUE mod, VALUE objV, VALUE ownerV) {
1041
+ /* Get the C object from the Ruby value. */
1042
+ Object obj;
1043
+ value_to_rcsim(ObjectS,objV,obj);
1044
+ /* Get the C owner from the Ruby value. */
1045
+ Object owner;
1046
+ value_to_rcsim(ObjectS,ownerV,owner);
1047
+ /* Set the owner. */
1048
+ obj->owner = owner;
1049
+ return objV;
1050
+ }
1051
+
1052
+ /** Sets the scope for a C system type. */
1053
+ VALUE rcsim_set_systemT_scope(VALUE mod, VALUE systemTV, VALUE scopeV) {
1054
+ /* Get the C system type from the Ruby value. */
1055
+ SystemT systemT;
1056
+ value_to_rcsim(SystemTS,systemTV,systemT);
1057
+ /* Get the C scope from the Ruby value. */
1058
+ Scope scope;
1059
+ value_to_rcsim(ScopeS,scopeV,scope);
1060
+ /* Set the scope. */
1061
+ systemT->scope = scope;
1062
+ return systemTV;
1063
+ }
1064
+
1065
+ /** Sets the block for a C behavior. */
1066
+ VALUE rcsim_set_behavior_block(VALUE mod, VALUE behaviorV, VALUE blockV) {
1067
+ /* Get the C behavior from the Ruby value. */
1068
+ Behavior behavior;
1069
+ value_to_rcsim(BehaviorS,behaviorV,behavior);
1070
+ /* Get the C block from the Ruby value. */
1071
+ Block block;
1072
+ value_to_rcsim(BlockS,blockV,block);
1073
+ /* Set the block. */
1074
+ behavior->block = block;
1075
+ return behaviorV;
1076
+ }
1077
+
1078
+ /** Sets the value for a C signal. */
1079
+ VALUE rcsim_set_signal_value(VALUE mod, VALUE signalV, VALUE exprV) {
1080
+ /* Get the C signal from the Ruby value. */
1081
+ SignalI signal;
1082
+ value_to_rcsim(SignalIS,signalV,signal);
1083
+ // printf("rc_sim_set_signal_value for signal=%s\n",signal->name);
1084
+ /* Get the C expression from the Ruby value. */
1085
+ Expression expr;
1086
+ value_to_rcsim(ExpressionS,exprV,expr);
1087
+ /* Compute the value from it. */
1088
+ // Value value = calc_expression(expr);
1089
+ Value value = get_value();
1090
+ value = calc_expression(expr,value);
1091
+ /* Copies the value. */
1092
+ signal->f_value = copy_value(value,signal->f_value);
1093
+ free_value();
1094
+ return signalV;
1095
+ }
1096
+
1097
+
1098
+ /** Starts the C-Ruby hybrid simulation.
1099
+ * @param systemTV the top system type.
1100
+ * @param name the name of the simulation.
1101
+ * @param vcd tells if the vcd generation is used or not. */
1102
+ VALUE rcsim_main(VALUE mod, VALUE systemTV, VALUE name, VALUE vcd) {
1103
+ /* Get the C system type from the Ruby value. */
1104
+ SystemT systemT;
1105
+ value_to_rcsim(SystemTS,systemTV,systemT);
1106
+ /* Set it as the top of the simulator. */
1107
+ top_system = systemT;
1108
+ /* Enable it. */
1109
+ set_enable_system(systemT,1);
1110
+ /* Starts the simulation. */
1111
+ if (TYPE(vcd) == T_TRUE)
1112
+ hruby_sim_core(StringValueCStr(name),init_vcd_visualizer,-1);
1113
+ else
1114
+ hruby_sim_core(StringValueCStr(name),init_default_visualizer,-1);
1115
+ return systemTV;
1116
+ }
1117
+
1118
+
1119
+
1120
+
1121
+ /** The initialization of the C-part of the C-Ruby hybrid HDLRuby simulator. */
1122
+ void Init_hruby_sim() {
1123
+ /* Generate the ID of the symbols used in the simulator. */
1124
+ make_sym_IDs();
1125
+ /* Create the module for C-Ruby interface. */
1126
+ VALUE mod = rb_define_module("RCSimCinterface");
1127
+
1128
+ /* Create the class that wraps C pointers. */
1129
+ RCSimPointer = rb_define_class("RCSimPointer",rb_cObject);
1130
+
1131
+ /* Add the interface methods. */
1132
+ /* Getting the C simulation type objects. */
1133
+ rb_define_singleton_method(mod,"rcsim_get_type_bit",rcsim_get_type_bit,0);
1134
+ rb_define_singleton_method(mod,"rcsim_get_type_signed",rcsim_get_type_signed,0);
1135
+ rb_define_singleton_method(mod,"rcsim_get_type_vector",rcsim_get_type_vector,2);
1136
+ /* Creating the C simulation objects. */
1137
+ rb_define_singleton_method(mod,"rcsim_make_systemT",rcsim_make_systemT,1);
1138
+ rb_define_singleton_method(mod,"rcsim_make_scope",rcsim_make_scope,1);
1139
+ rb_define_singleton_method(mod,"rcsim_make_behavior",rcsim_make_behavior,1);
1140
+ rb_define_singleton_method(mod,"rcsim_make_event",rcsim_make_event,2);
1141
+ rb_define_singleton_method(mod,"rcsim_make_signal",rcsim_make_signal,2);
1142
+ rb_define_singleton_method(mod,"rcsim_make_systemI",rcsim_make_systemI,2);
1143
+ rb_define_singleton_method(mod,"rcsim_make_transmit",rcsim_make_transmit,2);
1144
+ rb_define_singleton_method(mod,"rcsim_make_print",rcsim_make_print,0);
1145
+ rb_define_singleton_method(mod,"rcsim_make_timeWait",rcsim_make_timeWait,2);
1146
+ rb_define_singleton_method(mod,"rcsim_make_timeTerminate",rcsim_make_timeTerminate,0);
1147
+ rb_define_singleton_method(mod,"rcsim_make_hif",rcsim_make_hif,3);
1148
+ rb_define_singleton_method(mod,"rcsim_make_hcase",rcsim_make_hcase,2);
1149
+ rb_define_singleton_method(mod,"rcsim_make_block",rcsim_make_block,1);
1150
+ rb_define_singleton_method(mod,"rcsim_make_value_numeric",rcsim_make_value_numeric,2);
1151
+ rb_define_singleton_method(mod,"rcsim_make_value_bitstring",rcsim_make_value_bitstring,2);
1152
+ rb_define_singleton_method(mod,"rcsim_make_cast",rcsim_make_cast,2);
1153
+ rb_define_singleton_method(mod,"rcsim_make_unary",rcsim_make_unary,3);
1154
+ rb_define_singleton_method(mod,"rcsim_make_binary",rcsim_make_binary,4);
1155
+ rb_define_singleton_method(mod,"rcsim_make_select",rcsim_make_select,2);
1156
+ rb_define_singleton_method(mod,"rcsim_make_concat",rcsim_make_concat,2);
1157
+ rb_define_singleton_method(mod,"rcsim_make_refConcat",rcsim_make_refConcat,1);
1158
+ rb_define_singleton_method(mod,"rcsim_make_refIndex",rcsim_make_refIndex,3);
1159
+ rb_define_singleton_method(mod,"rcsim_make_refRange",rcsim_make_refRange,4);
1160
+ rb_define_singleton_method(mod,"rcsim_make_stringE",rcsim_make_stringE,1);
1161
+ /* Adding elements to C simulation objects. */
1162
+ rb_define_singleton_method(mod,"rcsim_add_systemT_inputs",rcsim_add_systemT_inputs,2);
1163
+ rb_define_singleton_method(mod,"rcsim_add_systemT_outputs",rcsim_add_systemT_outputs,2);
1164
+ rb_define_singleton_method(mod,"rcsim_add_systemT_inouts",rcsim_add_systemT_inouts,2);
1165
+ rb_define_singleton_method(mod,"rcsim_add_scope_inners",rcsim_add_scope_inners,2);
1166
+ rb_define_singleton_method(mod,"rcsim_add_scope_behaviors",rcsim_add_scope_behaviors,2);
1167
+ rb_define_singleton_method(mod,"rcsim_add_scope_systemIs",rcsim_add_scope_systemIs,2);
1168
+ rb_define_singleton_method(mod,"rcsim_add_scope_scopes",rcsim_add_scope_scopes,2);
1169
+ rb_define_singleton_method(mod,"rcsim_add_behavior_events",rcsim_add_behavior_events,2);
1170
+ rb_define_singleton_method(mod,"rcsim_add_systemI_systemTs",rcsim_add_systemI_systemTs,2);
1171
+ rb_define_singleton_method(mod,"rcsim_add_print_args",rcsim_add_print_args,2);
1172
+ rb_define_singleton_method(mod,"rcsim_add_hif_noifs",rcsim_add_hif_noifs,3);
1173
+ rb_define_singleton_method(mod,"rcsim_add_hcase_whens",rcsim_add_hcase_whens,3);
1174
+ rb_define_singleton_method(mod,"rcsim_add_block_inners",rcsim_add_block_inners,2);
1175
+ rb_define_singleton_method(mod,"rcsim_add_block_statements",rcsim_add_block_statements,2);
1176
+ rb_define_singleton_method(mod,"rcsim_add_select_choices",rcsim_add_select_choices,2);
1177
+ rb_define_singleton_method(mod,"rcsim_add_concat_expressions",rcsim_add_concat_expressions,2);
1178
+ rb_define_singleton_method(mod,"rcsim_add_refConcat_refs",rcsim_add_refConcat_refs,2);
1179
+ /* Modifying C simulation objects. */
1180
+ rb_define_singleton_method(mod,"rcsim_set_owner",rcsim_set_owner,2);
1181
+ rb_define_singleton_method(mod,"rcsim_set_systemT_scope",rcsim_set_systemT_scope,2);
1182
+ rb_define_singleton_method(mod,"rcsim_set_behavior_block",rcsim_set_behavior_block,2);
1183
+ rb_define_singleton_method(mod,"rcsim_set_signal_value",rcsim_set_signal_value,2);
1184
+ /* Starting the simulation. */
1185
+ rb_define_singleton_method(mod,"rcsim_main",rcsim_main,3);
1186
+
1187
+ }
1188
+
1189
+
1190
+ #endif