HDLRuby 2.3.4 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,9 +13,9 @@ module HDLRuby
13
13
  @@absoluteCounter = -1 # The absolute name counter.
14
14
 
15
15
  # Generates an absolute uniq name.
16
- def self.uniq_name
16
+ def self.uniq_name(base = "")
17
17
  @@absoluteCounter += 1
18
- name = ":#{@@absoluteCounter}"
18
+ name = base.to_s + ":#{@@absoluteCounter}"
19
19
  if Symbol.all_symbols.find {|symbol| symbol.to_s == name } then
20
20
  # The symbol exists, try again.
21
21
  return self.uniq_name
@@ -170,6 +170,12 @@ extern Value mod_value(Value src0, Value src1, Value dst);
170
170
  * @return dst */
171
171
  extern Value not_value(Value src, Value dst);
172
172
 
173
+ /** Compute the or of the bits a a value.
174
+ * @param src the source value
175
+ * @param dst the destination value
176
+ * @return dst */
177
+ extern Value reduce_or_value(Value src, Value dst);
178
+
173
179
  /** Computes the AND of two values.
174
180
  * @param src0 the first source value of the and
175
181
  * @param src1 the second source value of the and
@@ -185,43 +191,43 @@ extern Value and_value(Value src0, Value src1, Value dst);
185
191
  extern Value or_value(Value src0, Value src1, Value dst);
186
192
 
187
193
  /** Computes the XOR of two values.
188
- * @param src0 the first source value of the or
189
- * @param src1 the second source value of the or
194
+ * @param src0 the first source value of the xor
195
+ * @param src1 the second source value of the xor
190
196
  * @param dst the destination value
191
197
  * @return dst */
192
198
  extern Value xor_value(Value src0, Value src1, Value dst);
193
199
 
194
200
  /** Computes the left shift of two general values.
195
- * @param src0 the first source value of the addition
196
- * @param src1 the second source value of the addition
201
+ * @param src0 the first source value of the shift
202
+ * @param src1 the second source value of the shift
197
203
  * @param dst the destination
198
204
  * @return dst */
199
205
  Value shift_left_value(Value src0, Value src1, Value dst);
200
206
 
201
207
  /** Computes the right shift of two general values.
202
- * @param src0 the first source value of the addition
203
- * @param src1 the second source value of the addition
208
+ * @param src0 the first source value of the shift
209
+ * @param src1 the second source value of the shift
204
210
  * @param dst the destination
205
211
  * @return dst */
206
212
  Value shift_right_value(Value src0, Value src1, Value dst);
207
213
 
208
214
  /** Computes the equal (NXOR) of two values.
209
- * @param src0 the first source value of the addition
210
- * @param src1 the second source value of the addition
215
+ * @param src0 the first source value of the comparison
216
+ * @param src1 the second source value of the comparison
211
217
  * @param dst the destination value
212
218
  * @return dst */
213
219
  extern Value equal_value(Value src0, Value src1, Value dst);
214
220
 
215
221
  /** Computes the lesser comparision of two values.
216
- * @param src0 the first source value of the addition
217
- * @param src1 the second source value of the addition
222
+ * @param src0 the first source value of the comparison
223
+ * @param src1 the second source value of the comparison
218
224
  * @param dst the destination value
219
225
  * @return dst */
220
226
  extern Value lesser_value(Value src0, Value src1, Value dst);
221
227
 
222
228
  /** Computes the greater comparision of two values.
223
- * @param src0 the first source value of the addition
224
- * @param src1 the second source value of the addition
229
+ * @param src0 the first source value of the comparison
230
+ * @param src1 the second source value of the comparison
225
231
  * @param dst the destination value
226
232
  * @return dst */
227
233
  extern Value greater_value(Value src0, Value src1, Value dst);
@@ -555,39 +561,71 @@ extern unsigned long long make_delay(int value, Unit unit);
555
561
 
556
562
 
557
563
 
564
+ /* Iterate over all the signals.
565
+ * @param func function to applie on each signal. */
566
+ extern void each_all_signal(void (*func)(SignalI));
558
567
 
559
568
  /* Interface to the visualization engine. */
560
569
 
561
- /** Prints the time.
562
- * @param time the time to show. */
563
- extern void print_time(unsigned long long time);
564
-
565
- /** Prints the time and goes to the next line.
566
- * @param time the time to show. */
567
- extern void println_time(unsigned long long time);
568
-
569
- /** Prints the name of an object.
570
- * @param object the object to print the name. */
571
- extern void print_name(Object object);
572
-
573
- /** Prints a value.
574
- * @param value the value to print */
575
- extern void print_value(Value value);
576
-
577
- /** Prints a signal.
578
- * @param signal the signal to show */
579
- extern void print_signal(SignalI signal);
580
-
581
- /** Prints a signal and goes to the next line.
582
- * @param signal the signal to show */
583
- extern void println_signal(SignalI signal);
584
-
570
+ typedef struct {
571
+ void (*print_time)(unsigned long long);
572
+ void (*print_name)(Object);
573
+ void (*print_value)(Value);
574
+ void (*print_signal)(SignalI);
575
+ } PrinterS;
576
+
577
+ PrinterS printer;
578
+
579
+ /** Initializes the visualization printer engine.
580
+ * @param print_time the time printer
581
+ * @param print_name the name printer
582
+ * @param print_value the value printer
583
+ * @param print_signal the signal state printer. */
584
+ void init_visualizer(void (*print_time)(unsigned long long),
585
+ void (*print_name)(Object),
586
+ void (*print_value)(Value),
587
+ void (*print_signal)(SignalI));
588
+
589
+ // /** Prints the time.
590
+ // * @param time the time to show. */
591
+ // extern void print_time(unsigned long long time);
592
+ //
593
+ // // /** Prints the time and goes to the next line.
594
+ // // * @param time the time to show. */
595
+ // // extern void println_time(unsigned long long time);
596
+ //
597
+ // /** Prints the name of an object.
598
+ // * @param object the object to print the name. */
599
+ // extern void print_name(Object object);
600
+ //
601
+ // /** Prints a value.
602
+ // * @param value the value to print */
603
+ // extern void print_value(Value value);
604
+ //
605
+ // /** Prints a signal.
606
+ // * @param signal the signal to show */
607
+ // extern void print_signal(SignalI signal);
608
+ //
609
+ // // /** Prints a signal and goes to the next line.
610
+ // // * @param signal the signal to show */
611
+ // // extern void println_signal(SignalI signal);
612
+
613
+ /** Sets up the default vizualization engine.
614
+ * @param name the name of the vizualization. */
615
+ extern void init_default_visualizer(char* name);
616
+
617
+ /** Sets up the vcd vizualization engine.
618
+ * @param name the name of the vizualization. */
619
+ extern void init_vcd_visualizer(char* name);
585
620
 
586
621
  /* The interface to the simulator core. */
587
622
 
588
623
  /** The simulation core function.
624
+ * @param name the name of the simulation.
625
+ * @param init_vizualizer the vizualizer engine initializer.
589
626
  * @param limit the time limit in fs. */
590
- extern void hruby_sim_core(unsigned long long limit);
627
+ extern void hruby_sim_core(char* name, void (*init_vizualizer)(char*),
628
+ unsigned long long limit);
591
629
 
592
630
 
593
631
 
@@ -714,6 +714,45 @@ static Value not_value_bitstring(Value src, Value dst) {
714
714
  return dst;
715
715
  }
716
716
 
717
+ /** Compute the or of the bits a bitstring value.
718
+ * @param src the source value
719
+ * @param dst the destination value
720
+ * @return dst */
721
+ Value reduce_or_value_bitstring(Value src, Value dst) {
722
+ /* Compute the width of the result in bits. */
723
+ unsigned long long width = type_width(src->type);
724
+
725
+ /* Update the destination capacity if required. */
726
+ resize_value(dst,width);
727
+ /* Set the type and size of the destination from the type of the source.*/
728
+ dst->type = src->type;
729
+ dst->numeric = 0;
730
+
731
+ /* Get access to the source and destination data. */
732
+ char* src_data = src->data_str;
733
+ char* dst_data = dst->data_str;
734
+
735
+ /* Performs the reduce or. */
736
+ unsigned long long count;
737
+ char res;
738
+ for(count = 0; count < width; ++count) {
739
+ /* Performs the reduce or. */
740
+ char d = src_data[count] - '0'; /* Get and convert to bit. */
741
+ if ((d == (d&1)) && (res != 'x'-'0')) { /* d is defined. */
742
+ res |= d;
743
+ } else {
744
+ /* res is undefined. */
745
+ res = 'x' - '0';
746
+ }
747
+ /* Apart for the first bit, there are only 0, still we are in
748
+ * the loop, set it. */
749
+ dst_data[count] = '0';
750
+ }
751
+ dst_data[0] = res + '0';
752
+ /* Return the destination. */
753
+ return dst;
754
+ }
755
+
717
756
 
718
757
  /** Computes the and of two bitstring values.
719
758
  * @param src0 the first source value of the and
@@ -1575,6 +1614,21 @@ static Value not_value_numeric(Value src, Value dst) {
1575
1614
  }
1576
1615
 
1577
1616
 
1617
+ /** Compute the or of the bits a numeric value.
1618
+ * @param src the source value
1619
+ * @param dst the destination value
1620
+ * @return dst */
1621
+ Value reduce_or_value_numeric(Value src, Value dst) {
1622
+ /* Sets state of the destination using the first source. */
1623
+ dst->type = src->type;
1624
+ dst->numeric = 1;
1625
+
1626
+ /* Perform the reduce or. */
1627
+ dst->data_int = fix_numeric_type(dst->type, src->data_int != 0);
1628
+ return dst;
1629
+ }
1630
+
1631
+
1578
1632
  /** Computes the AND of two numeric values.
1579
1633
  * @param src0 the first source value of the addition
1580
1634
  * @param src1 the second source value of the addition
@@ -2093,6 +2147,21 @@ Value not_value(Value src, Value dst) {
2093
2147
  }
2094
2148
 
2095
2149
 
2150
+ /** Compute the or of the bits a value.
2151
+ * @param src the source value
2152
+ * @param dst the destination value
2153
+ * @return dst */
2154
+ Value reduce_or_value(Value src, Value dst) {
2155
+ if (src->numeric) {
2156
+ /* The source is numeric. */
2157
+ return reduce_or_value_numeric(src,dst);
2158
+ } else {
2159
+ /* The source cannot be numeric, compute bitsitrings. */
2160
+ return reduce_or_value_bitstring(src,dst);
2161
+ }
2162
+ }
2163
+
2164
+
2096
2165
  /** Computes the AND of two general values.
2097
2166
  * @param src0 the first source value of the addition
2098
2167
  * @param src1 the second source value of the addition
@@ -118,9 +118,10 @@ void hruby_sim_update_signals() {
118
118
  /* Is there a change? */
119
119
  if (same_content_value(sig->c_value,sig->f_value)) continue;
120
120
  /* Yes, process the signal. */
121
- println_signal(sig);
122
- // printf("c_value="); print_value(sig->c_value);
123
- // printf("\nf_value="); print_value(sig->f_value); printf("\n");
121
+ // println_signal(sig);
122
+ printer.print_signal(sig);
123
+ // printf("c_value="); printer.print_value(sig->c_value);
124
+ // printf("\nf_value="); printer.print_value(sig->f_value); printf("\n");
124
125
  // printf("Touched signal: %p (%s)\n",sig,sig->name);
125
126
  /* Update the current value of the signal. */
126
127
  copy_value(sig->f_value,sig->c_value);
@@ -185,7 +186,8 @@ void hruby_sim_update_signals() {
185
186
  SignalI sig = e->data;
186
187
  delete_element(e);
187
188
  /* Yes, process the signal. */
188
- println_signal(sig);
189
+ // println_signal(sig);
190
+ printer.print_signal(sig);
189
191
  /* Update the current value of the signal. */
190
192
  /* Mark the corresponding code as activated. */
191
193
  /* Any edge activation. */
@@ -284,7 +286,8 @@ void hruby_sim_advance_time() {
284
286
  }
285
287
  /* Sets the new activation time. */
286
288
  hruby_sim_time = next_time;
287
- println_time(hruby_sim_time);
289
+ // println_time(hruby_sim_time);
290
+ printer.print_time(hruby_sim_time);
288
291
  }
289
292
 
290
293
 
@@ -393,9 +396,18 @@ void hruby_sim_end_timed_behaviors() {
393
396
 
394
397
 
395
398
 
399
+ // /** The simulation core function.
400
+ // * @param limit the time limit in fs. */
401
+ // void hruby_sim_core(unsigned long long limit) {
396
402
  /** The simulation core function.
403
+ * @param name the name of the simulation.
404
+ * @param vizualizer the vizualizer engine initializer.
397
405
  * @param limit the time limit in fs. */
398
- void hruby_sim_core(unsigned long long limit) {
406
+ void hruby_sim_core(char* name, void (*init_vizualizer)(char*),
407
+ unsigned long long limit) {
408
+ /* Initilize the vizualizer. */
409
+ init_vizualizer(name);
410
+
399
411
  /* Initialize the time to 0. */
400
412
  hruby_sim_time = 0;
401
413
 
@@ -597,3 +609,14 @@ unsigned long long make_delay(int value, Unit unit) {
597
609
  return -1;
598
610
  }
599
611
 
612
+
613
+
614
+
615
+ /* Iterate over all the signals.
616
+ * @param func function to applie on each signal. */
617
+ void each_all_signal(void (*func)(SignalI)) {
618
+ int i;
619
+ for(i = 0; i<num_all_signals; ++i) {
620
+ func(all_signals[i]);
621
+ }
622
+ }
@@ -0,0 +1,215 @@
1
+ #include <stdio.h>
2
+ #include <string.h>
3
+ #include <time.h>
4
+ #include <stdarg.h>
5
+ #include "hruby_sim.h"
6
+
7
+
8
+ /**
9
+ * The HDLRuby simulation vcd format genertion engine, to be used with C code
10
+ * generated by hruby_low2c.
11
+ **/
12
+
13
+ /* Global variables storing the configuration of the vcd generation. */
14
+
15
+ /* The target file. */
16
+ static FILE* vcd_file;
17
+
18
+ /* The time scale unit in the ps. */
19
+ static unsigned long long vcd_timeunit = 1;
20
+
21
+ /* Accessing target file. */
22
+
23
+ /** Prints to the vcd file.
24
+ * @param fmt the format for handling the variadic arguments. */
25
+ static int vcd_print(char* fmt, ...) {
26
+ int ret;
27
+
28
+ /* Declare a va_list type variable */
29
+ va_list myargs;
30
+
31
+ /* Initialise the va_list variable with the ... after fmt */
32
+ va_start(myargs, fmt);
33
+
34
+ /* Forward the '...' to vprintf */
35
+ ret = vfprintf(vcd_file, fmt, myargs);
36
+
37
+ /* Clean up the va_list */
38
+ va_end(myargs);
39
+
40
+ return ret;
41
+ }
42
+
43
+
44
+ /* Utility functions for printing in vcd format. */
45
+
46
+ /* Replace all the occurences of a character with another.
47
+ * CREDIT: Superlokkus */
48
+ static char* replace_char(char* str, char find, char replace){
49
+ char *current_pos = strchr(str,find);
50
+ while (current_pos){
51
+ *current_pos = replace;
52
+ current_pos = strchr(current_pos+1,find);
53
+ }
54
+ return str;
55
+ }
56
+
57
+
58
+ /* Low-end print functions. */
59
+
60
+ /** Prints the time.
61
+ * @param time the time to show (given in ps). */
62
+ static void vcd_print_time(unsigned long long time) {
63
+ vcd_print("#%llu\n",time/vcd_timeunit);
64
+ }
65
+
66
+
67
+ /** Prints the name of an object.
68
+ * @param object the object to print the name. */
69
+ static void vcd_print_name(Object object) {
70
+ /* Recurse on the owner if any. */
71
+ // printf("owner=%p\n",object->owner);
72
+ if (object->owner != NULL) {
73
+ vcd_print_name(object->owner);
74
+ vcd_print("$");
75
+ }
76
+ /* Depending on the kind of object. */
77
+ switch(object->kind) {
78
+ case SYSTEMT:
79
+ case SIGNALI:
80
+ case SCOPE:
81
+ case SYSTEMI:
82
+ /* Print the name if name. */
83
+ /* Trick: SystemT, SignalI, Scope and SystemI have the
84
+ * field name at the same place. */
85
+ if (((SystemI)object)->name != NULL) {
86
+ char name[256];
87
+ strncpy(name,((SystemI)object)->name,256);
88
+ replace_char(name,':','$');
89
+ vcd_print("%s",name);
90
+ }
91
+ default: /* Nothing to do */
92
+ break;
93
+ }
94
+ }
95
+
96
+ /** Prints a value.
97
+ * @param value the value to print */
98
+ static void vcd_print_value(Value value) {
99
+ vcd_print("b");
100
+ if (value->numeric) {
101
+ unsigned long long width = type_width(value->type);
102
+ unsigned long long mask = 1ULL << (width-1);
103
+ for(; mask > 0; mask >>= 1) {
104
+ vcd_print("%d",(value->data_int & mask) != 0);
105
+ }
106
+ } else {
107
+ /* Display a bitstring value. */
108
+ int i;
109
+ int width = type_width(value->type);
110
+ char* data = value->data_str;
111
+ if (value->capacity == 0) {
112
+ /* The value is empty, therefore undefined. */
113
+ for(i=width; i>0; --i) {
114
+ vcd_print("u");
115
+ }
116
+ }
117
+ else {
118
+ /* The value is not empty. */
119
+ for(i=width; i>0; --i) {
120
+ vcd_print("%c",data[i-1]);
121
+ }
122
+ }
123
+ }
124
+ }
125
+
126
+ /** Prints a signal declaration.
127
+ * @param signal the signal to declare */
128
+ static void vcd_print_var(SignalI signal) {
129
+ vcd_print("$var wire %d ",type_width(signal->type));
130
+ vcd_print_name((Object)signal);
131
+ vcd_print(" ");
132
+ vcd_print_name((Object)signal);
133
+ vcd_print(" $end\n");
134
+ }
135
+
136
+
137
+ /** Prints a signal.
138
+ * @param signal the signal to show */
139
+ static void vcd_print_signal(SignalI signal) {
140
+ vcd_print_value(signal->f_value);
141
+ vcd_print(" ");
142
+ vcd_print_name((Object)signal);
143
+ vcd_print("\n");
144
+ }
145
+
146
+
147
+
148
+
149
+ /* high-end print functions. */
150
+
151
+ /** Prints the header of the vcd file. */
152
+ static void vcd_print_header() {
153
+ /* The date section. */
154
+ vcd_print("$date\n");
155
+ {
156
+ char text[100];
157
+ time_t now = time(NULL);
158
+ struct tm *t = localtime(&now);
159
+ strftime(text, sizeof(text), "%d %m %Y %H:%M", t);
160
+ vcd_print(" %s\n", text);
161
+ }
162
+ vcd_print("$end\n");
163
+
164
+ /* The version section. */
165
+ vcd_print("$version\n");
166
+ vcd_print(" Generated from HDLRuby simulator\n");
167
+ vcd_print("$end");
168
+
169
+ /* The comment section. */
170
+ vcd_print("$comment\n");
171
+ vcd_print(" Is it an easter egg?\n");
172
+ vcd_print("$end\n");
173
+
174
+ /* The time scale section: for now 1ps only. */
175
+ vcd_print("$timescale 1ps $end\n");
176
+
177
+ /* The scope section: nothing specific. */
178
+ vcd_print("$scope module logic $end\n");
179
+
180
+ /* The variables declaration. */
181
+ each_all_signal(&vcd_print_var);
182
+
183
+ /* Ends the declarations. */
184
+ vcd_print("$upscope $end\n");
185
+ vcd_print("$enddefinitions $end\n");
186
+
187
+ /* Display the initializations. */
188
+ vcd_print("$dumpvars\n");
189
+ // Maybe nothing to do.
190
+ // each_all_init_signal(&vcd_print_signal);
191
+ vcd_print("$end\n");
192
+ }
193
+
194
+
195
+ /* The configuration and initialization of the vcd vizualizer. */
196
+
197
+
198
+ /** Sets up the vcd vizualization engine.
199
+ * @param name the name of the vizualization. */
200
+ extern void init_vcd_visualizer(char* name) {
201
+ /* Open the resulting file with name: <name>.vcd */
202
+ char filename[256];
203
+ strncpy(filename,name,256);
204
+ strncat(filename,".vcd",256);
205
+ vcd_file = fopen(filename,"w");
206
+
207
+ /* Initialize the vizualizer printer engine. */
208
+ init_visualizer(&vcd_print_time,
209
+ &vcd_print_name,
210
+ &vcd_print_value,
211
+ &vcd_print_signal);
212
+
213
+ /* Prints the header of the vcd file. */
214
+ vcd_print_header();
215
+ }