The library can capture and print waveforms from Hardcaml simulations Printing results as waveforms We can make a waveform with It takes as an argument a simulator and returns a waveform and modified simulator that captures the input and output port values The waveform can be displayed with Capturing in expect tests Waveforms can be captured as expect test output are a form of automated testing where the expected output of a function or component is directly embedded in the test file and automatically compared against actual results With waveforms we get presented with a diff as we develop or change code Alongside good editor integration using expect tests can be a surprisingly convenient way to iterate on a hardware implementation Configuration options The function takes optional arguments which control the rendering of the waveform first cycle to display width and height of the waveform The height is inferred if not specified scale at which the waveform is shown negative values allowed configuration of the signals to show left or right align the name in the signal pane width of signal pane configure what signals to show and in what format Display rules The signals are shown in the order of the first matching A display rule matches one or more signal names matches the given name matches any name in the given list matches the signal name against a regular expression defined using the OCaml RE library The way the value is shown is configured by a Standard formatting includes and is like binary but renders a single bit value like the clear signal in the previous example will render as a bit if the width is 1 or X which may be Hex or Int for example otherwise takes a list of strings and looks it up based on the runtime value of the signal passes the runtime value to a function which returns the string to display is like index but looks up the the runtime value in a map Saving a waveform Hardcaml waveforms can be saved to file using It takes a waveform and file name and saves it Note that it also compresses the waveform data using which must be available will load the waveform The hardcaml waveterm library includes an executable which runs an interactive waveform viewer on saved waveform files hardcaml_waveterm Waveform create Waveform print # let test let sim Simulator create create in let waves sim Waveform create sim in testbench sim waves val test unit > Waveform t <fun> # let waves test val waves Waveform t <abstr> # Waveform print waves SignalsWaves clock clear incr dout 00 01 02 00 unit let%expect_test counter let waves test Waveform print waves %expect | SignalsWaves clock clear incr dout 00 01 02 00 | expect tests Waveform print start_cycle display_width display_height wave_width display_rules signals_alignment signals_width display_rules # Waveform print display_height 10 display_rules Display_rule port_name_is dout wave_format Unsigned_int port_name_matches Re Posix compile re cl * wave_format Bit waves SignalsWaves dout 0 1 2 0 clear clock unit Display_rule t port_name_is port_name_is_one_of port_name_matches wave_format Binary Hex Unsigned_int Int Bit Bit_or X Index Custom Map Serialize marshall gzip Serialize unmarshall
VCDs are a common hardware design file format for storing waveforms They can be used with standard waveform viewers like Gtkwave To generate a VCD call the function It takes an to write to and a simulator and returns a modified simulator to use during simulation The VCD will be generated as the simulation runs The being written to may need to be flushed This VCD file can subsequently be opened with a GUI based waveform viewer tool such as Surfer or GTKWave image Vcd wrap Out_channel Out_channel let test let sim Simulator create create in let filename tmp waves vcd in let oc Out_channel open_text filename in let sim Vcd wrap oc sim in testbench sim * Closing the out channel will ensure the file is flushed to disk * Out_channel close oc Stdio print_endline Saved waves to ^ filename let%expect_test vcd generation test %expect | Saved waves to tmp waves vcd |