summaryrefslogtreecommitdiff
path: root/hdl/result_printer.sv
blob: 4f27d0e464fc606e1dd18d909855047b1f57187b (plain) (blame)
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
`include "defs.svh"

module result_printer
    (   input   bit                 clock
    ,   input   bit                 reset

    ,   output  bit                 result_ready
    ,   input   bit                 result_valid
    ,   input   ram_read_response_t result_data

    ,   input   bit         echo_ready
    ,   output  bit         echo_valid
    ,   output  uart_byte_t echo_data
    );

    bit                 hold_valid;
    ram_read_response_t hold;
    ram_byte_count_t    byte_count;
    ram_word_count_t    word_count;

    (* syn_encoding = "one-hot" *) enum int unsigned
        {   HIGH_NIBBLE
        ,   LOW_NIBBLE
        } state;

    always @(posedge clock) begin
        if (reset) begin
            result_ready = 0;
            echo_valid = 0;
            echo_data = 0;
            hold_valid = 0;
            byte_count = 0;
            word_count = 0;
            state = state.first;
        end else begin
            if (echo_ready) echo_valid = 0;
            if (result_ready && result_valid) begin
                hold_valid = 1;
                hold = result_data;
                byte_count = `RAM_WORD_BYTES;
                word_count = `RAM_LINE_WORDS;
                state = state.first;
            end

            if (hold_valid && !echo_valid) begin
                if (word_count != 0) begin
                    automatic ram_word_t w = hold.data[word_count-1];
                    automatic ram_byte_t b = w[byte_count-1];
                    echo_valid = 1;
                    case (state)
                    HIGH_NIBBLE: echo_data = b[7:4];
                    LOW_NIBBLE: echo_data = b[3:0];
                    endcase
                    if (echo_data < 10)
                        echo_data = echo_data + "0";
                    else
                        echo_data = echo_data + "A" - 10;
                    state = state.next;
                    if (state == state.first) begin
                        byte_count = byte_count - 1;
                        if (byte_count == 0) begin
                            byte_count = `RAM_WORD_BYTES;
                            word_count = word_count - 1;
                        end
                    end
                end else begin
                    echo_valid = 1;
                    echo_data = "\n";
                    hold_valid = 0;
                end
            end

            result_ready = !hold_valid;
        end
    end

endmodule