Debian Codegraph @

examples/latch.v

raw
1 `default_nettype none
2
3 // Hardware-in-the-loop latch test
4 //
5 // Exercises transparent latches with runtime-generated data and verifies
6 // hold behavior. Each test:
7 // 1. Loads a counter-derived value with gate open (transparent)
8 // 2. Closes gate, then changes input to complement
9 // 3. Compares: held value must equal the transparent value
10 //
11 // Output per tick:
12 // T0:PASS (or T0:FAIL)
13 // T1:PASS
14 // T2:PASS
15 // T3:PASS
16
17 module top (
18 input clk,
19 output TXD
20 );
21
22 // Auto-reset on power-up
23 reg [3:0] por_cnt = 0;
24 wire por_done = por_cnt[3];
25 always @(posedge clk)
26 if (!por_done) por_cnt <= por_cnt + 1;
27
28 wire rst = !por_done;
29
30 // ---- UART transmitter ----
31 wire uart_ready;
32 reg [7:0] uart_data;
33 reg uart_valid;
34
35 corescore_emitter_uart #(
36 .clk_freq_hz(`CPU_FREQ * 1000000),
37 .baud_rate(115200)
38 ) uart (
39 .i_clk(clk),
40 .i_rst(rst),
41 .i_data(uart_data),
42 .i_valid(uart_valid),
43 .o_ready(uart_ready),
44 .o_uart_tx(TXD)
45 );
46
47 // ---- Data source (runtime counter, prevents constant folding) ----
48 reg [7:0] data_cnt;
49 always @(posedge clk)
50 data_cnt <= data_cnt + 8'd1;
51
52 // ---- Latch under test ----
53 reg [7:0] latch_in;
54 reg gate;
55 reg [7:0] latch_q;
56
57 always @*
58 if (gate) latch_q = latch_in;
59
60 // ---- Test results ----
61 reg [3:0] test_pass; // 1 bit per test
62 reg [7:0] transparent_val;
63
64 // ---- State machine ----
65 localparam S_LOAD = 0; // Load counter value, open gate
66 localparam S_CAPTURE_T = 1; // Capture transparent value, close gate
67 localparam S_HOLD = 2; // Change input while gate is closed
68 localparam S_COMPARE = 3; // Compare held value with transparent
69 localparam S_NEXT = 4; // Next test or start sending
70 localparam S_SEND = 5; // Send character
71 localparam S_SEND_WAIT = 6; // Wait for UART
72
73 reg [2:0] state;
74 reg [1:0] test_idx;
75
76 // ---- Message generation ----
77 // Per test: "Tn:PASS\n" or "Tn:FAIL\n" = 8 chars
78 // After tests: "--- tick ---\n" = 13 chars
79 // Total = 4*8 + 13 = 45 chars
80 localparam MSG_LEN = 45;
81
82 reg [5:0] msg_idx;
83 reg [7:0] msg_char;
84
85 always @* begin
86 msg_char = 8'h00;
87 if (msg_idx < 32) begin
88 case (msg_idx % 8)
89 0: msg_char = "T";
90 1: msg_char = 8'h30 + {6'd0, msg_idx[4:3]}; // '0'-'3'
91 2: msg_char = ":";
92 3: msg_char = test_pass[msg_idx[4:3]] ? "P" : "F";
93 4: msg_char = test_pass[msg_idx[4:3]] ? "A" : "A";
94 5: msg_char = test_pass[msg_idx[4:3]] ? "S" : "I";
95 6: msg_char = test_pass[msg_idx[4:3]] ? "S" : "L";
96 7: msg_char = 8'h0a;
97 default: msg_char = 8'h00;
98 endcase
99 end else begin
100 case (msg_idx - 32)
101 0: msg_char = "-";
102 1: msg_char = "-";
103 2: msg_char = "-";
104 3: msg_char = " ";
105 4: msg_char = "t";
106 5: msg_char = "i";
107 6: msg_char = "c";
108 7: msg_char = "k";
109 8: msg_char = " ";
110 9: msg_char = "-";
111 10: msg_char = "-";
112 11: msg_char = "-";
113 12: msg_char = 8'h0a;
114 default: msg_char = 8'h00;
115 endcase
116 end
117 end
118
119 // ---- Main logic ----
120 always @(posedge clk) begin
121 if (rst) begin
122 state <= S_LOAD;
123 test_idx <= 0;
124 msg_idx <= 0;
125 uart_valid <= 1'b0;
126 gate <= 1'b0;
127 end else case (state)
128 S_LOAD: begin
129 // Load current counter value with gate open
130 latch_in <= data_cnt;
131 gate <= 1'b1;
132 state <= S_CAPTURE_T;
133 end
134
135 S_CAPTURE_T: begin
136 // Capture transparent readback, close gate
137 transparent_val <= latch_q;
138 gate <= 1'b0;
139 state <= S_HOLD;
140 end
141
142 S_HOLD: begin
143 // Now change input — gate is already closed
144 latch_in <= ~data_cnt;
145 state <= S_COMPARE;
146 end
147
148 S_COMPARE: begin
149 // Held value should still match transparent value
150 test_pass[test_idx] <= (latch_q == transparent_val);
151 state <= S_NEXT;
152 end
153
154 S_NEXT: begin
155 if (test_idx == 2'd3) begin
156 test_idx <= 0;
157 msg_idx <= 0;
158 state <= S_SEND;
159 end else begin
160 test_idx <= test_idx + 1;
161 state <= S_LOAD;
162 end
163 end
164
165 S_SEND: begin
166 if (uart_ready) begin
167 uart_data <= msg_char;
168 uart_valid <= 1'b1;
169 state <= S_SEND_WAIT;
170 end
171 end
172
173 S_SEND_WAIT: begin
174 uart_valid <= 1'b0;
175 if (msg_idx == MSG_LEN - 1) begin
176 state <= S_LOAD;
177 end else begin
178 msg_idx <= msg_idx + 1;
179 state <= S_SEND;
180 end
181 end
182 endcase
183 end
184
185 endmodule
186
187 `include "emitter_uart.v"