summaryrefslogtreecommitdiff
path: root/hdl/command_parser.sv
blob: 5be5957e6fa83507be09d7225e4b9a323fe598c6 (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
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
`include "defs.svh"

module command_parser
    #(  TAG = 0
    ) ( input   bit         clock
    ,   input   bit         reset

    ,   output  bit         uart_ready
    ,   input   bit         uart_valid
    ,   input   uart_byte_t uart_data

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

    ,   input   bit             command_ready
    ,   output  bit             command_valid
    ,   output  arb_to_ram_t    command_data

    ,   input   bit loop_ready
    ,   output  bit loop_valid
    ,   output  bit loop_data

    ,   output  bit clear_caches
    );

    bit         input_byte_valid;
    uart_byte_t input_byte;

    bit [`RAM_ADDRESS_BITS:0]   loop_count;

    (* syn_encoding = "one-hot" *) enum int unsigned
        {   READ_COMMAND
        ,   READ_ADDRESS
        ,   READ_DATA
        ,   READ_LOOP_COUNT
        ,   LOOP_MEMORY
        } state;

    always @(posedge clock) begin
        if (reset) begin
            uart_ready = 0;
            command_valid = 0;
            for (int i = 0; i < `RAM_LINE_WORDS; i = i + 1)
                command_data.mask[i] = ~0;
            command_data.tag = TAG;
            loop_valid = 0;
            clear_caches = 0;
            input_byte_valid = 0;
            input_byte = 0;
            loop_count = 0;
            state = state.first;
        end else begin
            if (echo_ready) echo_valid = 0;
            if (command_ready && command_valid) begin
                command_valid = 0;
                ++command_data.address;
            end
            if (uart_ready && uart_valid) begin
                echo_valid = 1;
                echo_data = uart_data;
                input_byte_valid = 1;
                input_byte = uart_data;
            end
            if (loop_ready && loop_valid) begin
                loop_valid = 0;
            end
            clear_caches = 0;

            if (!command_valid && !loop_valid) begin
                case (state)

                READ_COMMAND: if (input_byte_valid) begin
                    case (input_byte)

                    "@": begin
                        command_data.address = 0;
                        loop_valid = 1;
                        loop_data = 0;
                        state = READ_ADDRESS;
                    end

                    "?": begin
                        command_valid = 1;
                        command_data.write = 0;
                        loop_valid = 1;
                        loop_data = 0;
                    end

                    "=": begin
                        command_data.write = 1;
                        for (int i = 0; i < `RAM_LINE_WORDS; i = i + 1)
                            command_data.data[i] = 0;
                        loop_valid = 1;
                        loop_data = 0;
                        state = READ_DATA;
                    end

                    "!": begin
                        command_data.write = 1;
                        for (int i = 0; i < `RAM_LINE_WORDS; i = i + 1)
                            command_data.data[i] = 0;
                        loop_count = 0;
                        state = READ_LOOP_COUNT;
                    end

                    "#": begin
                        command_data.write = 0;
                        loop_count = 0;
                        state = READ_LOOP_COUNT;
                    end

                    "$": begin
                        clear_caches = 1;
                    end

                    endcase
                    input_byte_valid = 0;
                end

                READ_ADDRESS: if (input_byte_valid) begin
                    if (input_byte == ":") begin
                        // ignore
                    end else if (input_byte >= "0" && input_byte <= "9") begin
                        command_data.address = command_data.address << 4;
                        command_data.address[$clog2(`RAM_LINE_WORDS)+:4] = input_byte - "0";
                    end else if (input_byte >= "a" && input_byte <= "f") begin
                        command_data.address = command_data.address << 4;
                        command_data.address[$clog2(`RAM_LINE_WORDS)+:4] = input_byte - "a" + 10;
                    end else if (input_byte >= "A" && input_byte <= "F") begin
                        command_data.address = command_data.address << 4;
                        command_data.address[$clog2(`RAM_LINE_WORDS)+:4] = input_byte - "A" + 10;
                    end else begin
                        state = state.first;
                    end
                    input_byte_valid = 0;
                end

                READ_DATA: if (input_byte_valid) begin
                    automatic bit [$bits(command_data.data)-1:0] flat_data = command_data.data;
                    flat_data = flat_data << 4;
                    if (input_byte == ":") begin
                        // ignore
                    end else if (input_byte >= "0" && input_byte <= "9") begin
                        flat_data[3:0] = input_byte - "0";
                        command_data.data = flat_data;
                    end else if (input_byte >= "a" && input_byte <= "f") begin
                        flat_data[3:0] = input_byte - "a" + 10;
                        command_data.data = flat_data;
                    end else if (input_byte >= "A" && input_byte <= "F") begin
                        flat_data[3:0] = input_byte - "A" + 10;
                        command_data.data = flat_data;
                    end else begin
                        command_valid = 1;
                        state = state.first;
                    end
                    input_byte_valid = 0;
                end

                READ_LOOP_COUNT: if (input_byte_valid) begin
                    if (input_byte == ":") begin
                        // ignore
                    end else if (input_byte >= "0" && input_byte <= "9") begin
                        loop_count = loop_count << 4;
                        loop_count[3:0] = input_byte - "0";
                    end else if (input_byte >= "a" && input_byte <= "f") begin
                        loop_count = loop_count << 4;
                        loop_count[3:0] = input_byte - "a" + 10;
                    end else if (input_byte >= "A" && input_byte <= "F") begin
                        loop_count = loop_count << 4;
                        loop_count[3:0] = input_byte - "A" + 10;
                    end else begin
                        loop_valid = 1;
                        loop_data = 1;
                        state = LOOP_MEMORY;
                    end
                    input_byte_valid = 0;
                end

`ifdef SLOW_LOOPING
                LOOP_MEMORY: if (!echo_valid) begin
                    if (loop_count == 0) begin
                        echo_valid = 1;
                        echo_data = "\n";
                        state = state.first;
                    end else begin
                        echo_valid = 1;
                        echo_data = ".";
                        command_valid = 1;
                        --loop_count;
                    end
                end
`else
                LOOP_MEMORY: begin
                    if (loop_count == 0) begin
                        state = state.first;
                    end else begin
                        command_valid = 1;
                        if (--loop_count == 0) begin
                            state = state.first;
                        end
                    end
                end
`endif

                endcase
            end

            uart_ready = !echo_valid && !input_byte_valid;
        end
    end

endmodule