gsim 1.1.4

High speed digital logic simulation
Documentation
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
#ifndef GSIM_H
#define GSIM_H

/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */

#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

#if defined(__GNUC__) && (__GNUC__ >= 4)
#define GSIM_MUST_USE __attribute__ ((warn_unused_result))
#elif defined(_MSC_VER) && (_MSC_VER >= 1700)
#define GSIM_MUST_USE _Check_return_
#else
#define GSIM_MUST_USE
#endif

#define INVALID_WIRE_ID      0xFFFFFFFF
#define INVALID_COMPONENT_ID 0xFFFFFFFF

#define GSIM_RESULT_SUCCESS 0

#define GSIM_RESULT_MAX_STEPS_REACHED 1

#define GSIM_RESULT_FALSE 0
#define GSIM_RESULT_TRUE  1

#define GSIM_RESULT_HIGH_Z    0
#define GSIM_RESULT_UNDEFINED 1
#define GSIM_RESULT_LOGIC_0   2
#define GSIM_RESULT_LOGIC_1   3

#define GSIM_RESULT_NULL_POINTER          -0x0000_0001
#define GSIM_RESULT_POINTER_MISALIGNED    -0x0000_0002
#define GSIM_RESULT_INVALID_ARGUMENT      -0x0000_0003
#define GSIM_RESULT_ARGUMENT_OUT_OF_RANGE -0x0000_0004
#define GSIM_RESULT_UTF8_ENCODING         -0x0000_0005
#define GSIM_RESULT_IO                    -0x0000_0006
#define GSIM_RESULT_INVALID_OPERATION     -0x0000_0007

#define GSIM_RESULT_RESOURCE_LIMIT_REACHED  -0x0001_0001
#define GSIM_RESULT_WIRE_WIDTH_MISMATCH     -0x0001_0002
#define GSIM_RESULT_WIRE_WIDTH_INCOMPATIBLE -0x0001_0003
#define GSIM_RESULT_OFFSET_OUT_OF_RANGE     -0x0001_0004
#define GSIM_RESULT_TOO_FEW_INPUTS          -0x0001_0005
#define GSIM_RESULT_INVALID_INPUT_COUNT     -0x0001_0006
#define GSIM_RESULT_INVALID_COMPONENT_TYPE  -0x0001_0007

#define GSIM_RESULT_CONFLICT             -0x0002_0001
#define GSIM_RESULT_INVALID_WIRE_ID      -0x0002_0002
#define GSIM_RESULT_INVALID_COMPONENT_ID -0x0002_0003

#define GSIM_RESULT_MALFORMED_FORMAT -0x0003_0001
#define GSIM_RESULT_UNSUPPORTED      -0x0003_0002

/**
 * An opaque type representing a logic state of up to 255 bits.
 * Use only behind a pointer.
 */
typedef struct LogicState LogicState;

/**
 * An opaque type used to build up a simulation graph.
 * Use only behind a pointer.
 */
typedef struct Builder Builder;

/**
 * An opaque type representing a simulation.
 * Use only behind a pointer.
 */
typedef struct Simulator Simulator;

/**
 * One of the `GSIM_RESULT_*` constants
 */
typedef int32_t GsimResult;

/**
 * A unique identifier for a wire inside a simulation
 */
typedef uint32_t WireId;

/**
 * A unique identifier for a component inside a simulation
 */
typedef uint32_t ComponentId;

typedef struct PortList {
    size_t len;
    const char *const *names;
    const WireId *wires;
} PortList;

typedef struct SimulationErrors {
    size_t conflicts_len;
    const WireId *conflicts;
} SimulationErrors;

/**
 * Frees a string that was returned by other functions in the API.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE GsimResult string_free(const char *s);

/**
 * Creates a new `Builder` object.
 * The resulting `Builder` must be freed by calling `simulator_build`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult builder_new(Builder **builder);

/**
 * Writes the simulation graph into a Graphviz DOT file.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE GsimResult builder_write_dot(const Builder *builder, const char *dot_file);

/**
 * Gets the width of a wire.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult builder_get_wire_width(const Builder *builder,
                                  WireId wire,
                                  uint8_t *width);

/**
 * Drives a wire to a certain state without needing a component.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult builder_set_wire_drive(Builder *builder,
                                  WireId wire,
                                  const LogicState *drive);

/**
 * Gets the current drive of a wire.
 * The resulting `LogicState` must be freed by calling `logic_state_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult builder_get_wire_drive(const Builder *builder,
                                  WireId wire,
                                  const LogicState **drive);

/**
 * Gets the width of a register in the simulation.
 * The ID passed to `register` must refer to a register component.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult builder_get_register_width(const Builder *builder,
                                      ComponentId register_,
                                      uint8_t *width);

/**
 * Gets the current state of a register in the simulation.
 * The ID passed to `register` must refer to a register component.
 * The resulting `LogicState` must be freed by calling `logic_state_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult builder_read_register_state(const Builder *builder,
                                       ComponentId register_,
                                       const LogicState **state);

/**
 * Sets the state of a register in the simulation.
 * The ID passed to `register` must refer to a register component.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult builder_write_register_state(Builder *builder,
                                        ComponentId register_,
                                        const LogicState *state);

/**
 * Gets the size and width of a memory block in the simulation.
 * The ID passed to `memory` must refer to a memory component.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult builder_get_memory_metrics(const Builder *builder,
                                      ComponentId memory,
                                      size_t *size,
                                      uint8_t *width);

/**
 * Gets the current state of a memory location in the simulation.
 * The ID passed to `memory` must refer to a memory component.
 * The resulting `LogicState` must be freed by calling `logic_state_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult builder_read_memory_state(const Builder *builder,
                                     ComponentId memory,
                                     size_t addr,
                                     const LogicState **state);

/**
 * Sets the state of a memory location in the simulation.
 * The ID passed to `memory` must refer to a memory component.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult builder_write_memory_state(Builder *builder,
                                      ComponentId memory,
                                      size_t addr,
                                      const LogicState *state);

/**
 * Sets the name of a wire.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE GsimResult builder_set_wire_name(Builder *builder, WireId wire, const char *name);

/**
 * Gets the name of a wire, if one has been assigned.
 * If no name has been assigned to the wire, name will be set to `null`.
 * The resulting string (if any) must be freed by calling `string_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult builder_get_wire_name(const Builder *builder,
                                 WireId wire,
                                 const char **name);

/**
 * Sets the name of a wire.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult builder_set_component_name(Builder *builder,
                                      ComponentId component,
                                      const char *name);

/**
 * Gets the name of a component, if one has been assigned.
 * If no name has been assigned to the component, name will be set to `null`.
 * The resulting string (if any) must be freed by calling `string_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult builder_get_component_name(const Builder *builder,
                                      ComponentId component,
                                      const char **name);

GSIM_MUST_USE GsimResult builder_add_wire(Builder *builder, uint8_t width, WireId *wire);

GSIM_MUST_USE
GsimResult builder_add_and_gate(Builder *builder,
                                const WireId *inputs,
                                size_t input_len,
                                WireId output,
                                ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_or_gate(Builder *builder,
                               const WireId *inputs,
                               size_t input_len,
                               WireId output,
                               ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_xor_gate(Builder *builder,
                                const WireId *inputs,
                                size_t input_len,
                                WireId output,
                                ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_nand_gate(Builder *builder,
                                 const WireId *inputs,
                                 size_t input_len,
                                 WireId output,
                                 ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_nor_gate(Builder *builder,
                                const WireId *inputs,
                                size_t input_len,
                                WireId output,
                                ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_xnor_gate(Builder *builder,
                                 const WireId *inputs,
                                 size_t input_len,
                                 WireId output,
                                 ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_merge(Builder *builder,
                             const WireId *inputs,
                             size_t input_len,
                             WireId output,
                             ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_priority_decoder(Builder *builder,
                                        const WireId *inputs,
                                        size_t input_len,
                                        WireId output,
                                        ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_buffer(Builder *builder,
                              WireId input_a,
                              WireId input_b,
                              WireId output,
                              ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_add(Builder *builder,
                           WireId input_a,
                           WireId input_b,
                           WireId output,
                           ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_sub(Builder *builder,
                           WireId input_a,
                           WireId input_b,
                           WireId output,
                           ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_mul(Builder *builder,
                           WireId input_a,
                           WireId input_b,
                           WireId output,
                           ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_left_shift(Builder *builder,
                                  WireId input_a,
                                  WireId input_b,
                                  WireId output,
                                  ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_logical_right_shift(Builder *builder,
                                           WireId input_a,
                                           WireId input_b,
                                           WireId output,
                                           ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_arithmetic_right_shift(Builder *builder,
                                              WireId input_a,
                                              WireId input_b,
                                              WireId output,
                                              ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_compare_equal(Builder *builder,
                                     WireId input_a,
                                     WireId input_b,
                                     WireId output,
                                     ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_compare_not_equal(Builder *builder,
                                         WireId input_a,
                                         WireId input_b,
                                         WireId output,
                                         ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_compare_less_than(Builder *builder,
                                         WireId input_a,
                                         WireId input_b,
                                         WireId output,
                                         ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_compare_greater_than(Builder *builder,
                                            WireId input_a,
                                            WireId input_b,
                                            WireId output,
                                            ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_compare_less_than_or_equal(Builder *builder,
                                                  WireId input_a,
                                                  WireId input_b,
                                                  WireId output,
                                                  ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_compare_greater_than_or_equal(Builder *builder,
                                                     WireId input_a,
                                                     WireId input_b,
                                                     WireId output,
                                                     ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_compare_less_than_signed(Builder *builder,
                                                WireId input_a,
                                                WireId input_b,
                                                WireId output,
                                                ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_compare_greater_than_signed(Builder *builder,
                                                   WireId input_a,
                                                   WireId input_b,
                                                   WireId output,
                                                   ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_compare_less_than_or_equal_signed(Builder *builder,
                                                         WireId input_a,
                                                         WireId input_b,
                                                         WireId output,
                                                         ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_compare_greater_than_or_equal_signed(Builder *builder,
                                                            WireId input_a,
                                                            WireId input_b,
                                                            WireId output,
                                                            ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_not_gate(Builder *builder,
                                WireId input,
                                WireId output,
                                ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_neg(Builder *builder,
                           WireId input,
                           WireId output,
                           ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_horizontal_and_gate(Builder *builder,
                                           WireId input,
                                           WireId output,
                                           ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_horizontal_or_gate(Builder *builder,
                                          WireId input,
                                          WireId output,
                                          ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_horizontal_xor_gate(Builder *builder,
                                           WireId input,
                                           WireId output,
                                           ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_horizontal_nand_gate(Builder *builder,
                                            WireId input,
                                            WireId output,
                                            ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_horizontal_nor_gate(Builder *builder,
                                           WireId input,
                                           WireId output,
                                           ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_horizontal_xnor_gate(Builder *builder,
                                            WireId input,
                                            WireId output,
                                            ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_zero_extend(Builder *builder,
                                   WireId input,
                                   WireId output,
                                   ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_sign_extend(Builder *builder,
                                   WireId input,
                                   WireId output,
                                   ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_slice(Builder *builder,
                             WireId input,
                             uint8_t offset,
                             WireId output,
                             ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_adder(Builder *builder,
                             WireId input_a,
                             WireId input_b,
                             WireId carry_in,
                             WireId output,
                             WireId carry_out,
                             ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_multiplexer(Builder *builder,
                                   const WireId *inputs,
                                   size_t input_len,
                                   WireId select,
                                   WireId output,
                                   ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_register(Builder *builder,
                                WireId data_in,
                                WireId data_out,
                                WireId enable,
                                WireId clock,
                                uint8_t clock_polarity,
                                ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_ram(Builder *builder,
                           WireId write_addr,
                           WireId data_in,
                           WireId read_addr,
                           WireId data_out,
                           WireId write,
                           WireId clock,
                           uint8_t clock_polarity,
                           ComponentId *component);

GSIM_MUST_USE
GsimResult builder_add_rom(Builder *builder,
                           WireId addr,
                           WireId data,
                           ComponentId *component);

/**
 * Frees all allocations of a `PortList` struct that was returned by other functions in the API.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE GsimResult port_list_free(struct PortList port_list);

/**
 * Imports a module defined by a Yosys netgraph into the circuit.
 * On success, `inputs` and `outputs` will contain a list of the imported modules ports.
 * The resulting `PortList` objects must be freed by calling `port_list_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 *
 * Valid netgraphs can be generated with this command:
 * `yosys -p "read_verilog <VERILOG-FILE>; synth -top <TOP-MODULE> -flatten -noalumacc -nordff -run begin:fine; hierarchy -check; check; write_json <OUTPUT-FILE>`
 */
GSIM_MUST_USE
GsimResult builder_import_yosys_module(Builder *builder,
                                       const char *json_file,
                                       struct PortList *inputs,
                                       struct PortList *outputs);

/**
 * Creates a `LogicState` with all bits set to the high impedance state.
 * The returned `LogicState` must be freed by calling `logic_state_free`.
 */
GSIM_MUST_USE const LogicState *logic_state_high_z(void);

/**
 * Creates a `LogicState` with all bits set to an undefined state.
 * The returned `LogicState` must be freed by calling `logic_state_free`.
 */
GSIM_MUST_USE const LogicState *logic_state_undefined(void);

/**
 * Creates a `LogicState` with all bits set to the logic low state.
 * The returned `LogicState` must be freed by calling `logic_state_free`.
 */
GSIM_MUST_USE const LogicState *logic_state_logic_0(void);

/**
 * Creates a `LogicState` with all bits set to the logic high state.
 * The returned `LogicState` must be freed by calling `logic_state_free`.
 */
GSIM_MUST_USE const LogicState *logic_state_logic_1(void);

/**
 * Creates a `LogicState` representing the given integer. High bits are set to 0.
 * The returned `LogicState` must be freed by calling `logic_state_free`.
 */
GSIM_MUST_USE const LogicState *logic_state_from_int(uint32_t value);

/**
 * Creates a `LogicState` representing the given integer. Integer words are given in little endian order, high bits are set to 0.
 * Will fail if `word_len` is not between 1 and 8 inclusive.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 * The returned `LogicState` must be freed by calling `logic_state_free`.
 */
GSIM_MUST_USE
GsimResult logic_state_from_big_int(const uint32_t *value,
                                    size_t word_len,
                                    const LogicState **state);

/**
 * Parses a `LogicState` from a string representation. High bits are set to high impedance.
 * Will fail if the string is longer than 255 characters, shorter than one character, or contains characters other than `'z'`, `'Z'`, `'x'`, `'X'`, `'0'` and `'1'`.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 * The resulting `LogicState` must be freed by calling `logic_state_free`, only if the operation succeeded.
 */
GSIM_MUST_USE
GsimResult logic_state_parse(const char *s,
                             const LogicState **state);

/**
 * Clones a `LogicState` into a new allocation.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 * The cloned `LogicState` must be freed separately by calling `logic_state_free`, only if the operation succeeded.
 */
GSIM_MUST_USE
GsimResult logic_state_clone(const LogicState *state,
                             const LogicState **clone);

/**
 * Attempts to convert the first `width` bits of a `LogicState` to an integer.
 * `width`  must be between 1 and 32 inclusive. Will fail if any of the bits are either in the `Z` or `X` state.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult logic_state_to_int(const LogicState *state,
                              uint8_t width,
                              uint32_t *value);

/**
 * Attempts to convert the first `width` bits of a `LogicState` to an integer. Integer words are returned in little endian order.
 * `value` must contain at least `width / 32` words rounded up. Will fail if any of the bits are either in the `Z` or `X` state.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult logic_state_to_big_int(const LogicState *state,
                                  uint8_t width,
                                  uint32_t *value);

/**
 * Gets the state of a single bit in a `LogicState`.
 * On success, returns one of the following values: `GSIM_RESULT_HIGH_Z`, `GSIM_RESULT_UNDEFINED`, `GSIM_RESULT_LOGIC_0`, `GSIM_RESULT_LOGIC_1`
 */
GSIM_MUST_USE
GsimResult logic_state_get_bit_state(const LogicState *state,
                                     uint8_t bit_index);

/**
 * Prints the string representation of a `LogicState` into a buffer.
 * The buffer must be big enough to hold at least `width` bytes and will not be null terminated by this function.
 * Since the buffer is owned by the caller, it must not be freed by `string_free`.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult logic_state_print(const LogicState *state,
                             uint8_t width,
                             char *buffer);

/**
 * Checks the first `width` bits of two `LogicState` objects for equality.
 * On success, returns one of the following values: `GSIM_RESULT_FALSE`, `GSIM_RESULT_TRUE`
 */
GSIM_MUST_USE GsimResult logic_state_eq(const LogicState *a, const LogicState *b, uint8_t width);

/**
 * Frees a `LogicState` that was returned by other functions in the API.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE GsimResult logic_state_free(LogicState *state);

/**
 * Creates a new `Simulator` object from a `Builder`.
 * If the operation succeeded, the specified `Builder` will be freed and be set to `null`.
 * The resulting `Simulator` must be freed by calling `simulator_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_build(Builder **builder,
                           struct Simulator **simulator);

/**
 * Creates a new `Simulator` object from a `Builder`, with VCD tracing enabled.
 * If the operation succeeded, the specified `Builder` will be freed and be set to `null`.
 * The `Builder` may be freed even if the operation failed. In this case it will also be set to `null`.
 * The resulting `Simulator` must be freed by calling `simulator_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_build_with_trace(Builder **builder,
                                      const char *trace_file,
                                      const struct Simulator **simulator);

/**
 * Writes the simulation graph into a Graphviz DOT file.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_write_dot(const struct Simulator *simulator,
                               const char *dot_file,
                               uint8_t show_states);

/**
 * Gets the width of a wire.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_get_wire_width(const struct Simulator *simulator,
                                    WireId wire,
                                    uint8_t *width);

/**
 * Drives a wire to a certain state without needing a component.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_set_wire_drive(struct Simulator *simulator,
                                    WireId wire,
                                    const LogicState *drive);

/**
 * Gets the current drive of a wire.
 * The resulting `LogicState` must be freed by calling `logic_state_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_get_wire_drive(const struct Simulator *simulator,
                                    WireId wire,
                                    const LogicState **drive);

/**
 * Gets the current state of a wire.
 * The resulting `LogicState` must be freed by calling `logic_state_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_get_wire_state(const struct Simulator *simulator,
                                    WireId wire,
                                    const LogicState **state);

/**
 * Gets the width of a register in the simulation.
 * The ID passed to `register` must refer to a register component.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_get_register_width(const struct Simulator *simulator,
                                        ComponentId register_,
                                        uint8_t *width);

/**
 * Gets the current state of a register in the simulation.
 * The ID passed to `register` must refer to a register component.
 * The resulting `LogicState` must be freed by calling `logic_state_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_read_register_state(const struct Simulator *simulator,
                                         ComponentId register_,
                                         const LogicState **state);

/**
 * Sets the state of a register in the simulation.
 * The ID passed to `register` must refer to a register component.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_write_register_state(struct Simulator *simulator,
                                          ComponentId register_,
                                          const LogicState *state);

/**
 * Gets the size and width of a memory block in the simulation.
 * The ID passed to `memory` must refer to a memory component.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_get_memory_metrics(const struct Simulator *simulator,
                                        ComponentId memory,
                                        size_t *size,
                                        uint8_t *width);

/**
 * Gets the current state of a memory location in the simulation.
 * The ID passed to `memory` must refer to a memory component.
 * The resulting `LogicState` must be freed by calling `logic_state_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_read_memory_state(const struct Simulator *simulator,
                                       ComponentId memory,
                                       size_t addr,
                                       const LogicState **state);

/**
 * Sets the state of a memory location in the simulation.
 * The ID passed to `memory` must refer to a memory component.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_write_memory_state(struct Simulator *simulator,
                                        ComponentId memory,
                                        size_t addr,
                                        const LogicState *state);

/**
 * Gets the name of a wire, if one has been assigned.
 * If no name has been assigned to the wire, name will be set to `null`.
 * The resulting string (if any) must be freed by calling `string_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_get_wire_name(struct Simulator *simulator,
                                   WireId wire,
                                   const char **name);

/**
 * Gets the name of a component, if one has been assigned.
 * If no name has been assigned to the component, name will be set to `null`.
 * The resulting string (if any) must be freed by calling `string_free`, only if the operation succeeded.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_get_component_name(struct Simulator *simulator,
                                        ComponentId component,
                                        const char **name);

/**
 * Resets the simulation.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE GsimResult simulator_reset(struct Simulator *simulator);

/**
 * Frees all allocations of a `SimulationErrors` struct that was returned by other functions in the API.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulation_errors_free(struct SimulationErrors error);

/**
 * Runs the simulation until it settles, but at most for `max_steps` steps.
 * On success, returns one of the following values:
 * - `GSIM_RESULT_SUCCESS`: the simulation settled within `max_steps` steps
 * - `GSIM_RESULT_MAX_STEPS_REACHED`: the simulation did not settle within `max_steps` steps
 *
 * If a `Conflict` failure is reported, `errors` will contain additional information about which wires had a driver conflict.
 * In this case, `errors` must later be freed by calling `simulation_errors_free`.
 */
GSIM_MUST_USE
GsimResult simulator_run_sim(struct Simulator *simulator,
                             uint64_t max_steps,
                             struct SimulationErrors *errors);

/**
 * Writes the current state of the simulation into the simulators associated VCD file at the specified time in nanoseconds.
 * Calling this function with a `Simulator` that was not constructed by `simulator_build_with_trace` is not illegal, but will have no effect.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE
GsimResult simulator_trace(struct Simulator *simulator,
                           uint64_t time);

/**
 * Frees a `Simulator` object.
 * Returns `GSIM_RESULT_SUCCESS` on success.
 */
GSIM_MUST_USE GsimResult simulator_free(struct Simulator *simulator);

#endif /* GSIM_H */