]>
Commit | Line | Data |
---|---|---|
1 | `include "pll.v" | |
2 | `include "master_rom.v" | |
3 | `include "i2c.v" | |
4 | `include "uart.v" | |
5 | ||
6 | `ifdef SIM | |
7 | `define UART_DIVIDE 1 | |
8 | `define I2C_DIVIDE 4 | |
9 | `else | |
10 | `define UART_DIVIDE 1024 | |
11 | `define I2C_DIVIDE 256 | |
12 | `endif | |
13 | ||
14 | module master(input CLKin, output [4:0] led, output uart_tx, input uart_rx, output reg busy_out = 0, input busy_in, output scl, output sda); | |
15 | wire clk; | |
16 | ||
17 | assign clk = CLKin; | |
18 | ||
19 | // ROM | |
20 | ||
21 | reg [7:0] program_counter = 0; | |
22 | // go here at end of program | |
23 | reg [7:0] main = 1; | |
24 | wire [31:0] rom_output; | |
25 | ||
26 | master_rom master_rom (.clk(clk), .addr(program_counter), .data(rom_output)); | |
27 | ||
28 | wire [2:0] rom_op = rom_output[26:24]; | |
29 | wire [2:0] rom_led = rom_output[14:12]; | |
30 | wire [3:0] rom_chip_select = rom_output[31:28]; | |
31 | ||
32 | // if the 4th board won't execute this instruction, | |
33 | // then we won't receive propagation or news | |
34 | wire dont_wait = !rom_chip_select[3]; | |
35 | reg [25:0] dont_send = 23'b11111111111111111111111; | |
36 | ||
37 | always @(posedge clk) begin | |
38 | if(busy_in) | |
39 | dont_send <= 21'b111111111111111111111; | |
40 | else if(dont_send) | |
41 | dont_send <= dont_send - 1; | |
42 | end | |
43 | ||
44 | // state | |
45 | ||
46 | `define STATE_SEND 0 | |
47 | `define STATE_WAIT_PROPAGATE 1 | |
48 | `define STATE_WAIT_NEWS 2 | |
49 | `define STATE_PROPAGATE_NEWS 3 | |
50 | `define STATE_SET_LEDS 4 | |
51 | `define STATE_WASTE_TIME 5 | |
52 | ||
53 | reg [5:0] state = `STATE_SEND; | |
54 | reg [5:0] uart_ptr = 0; | |
55 | ||
56 | reg [30:0] waste_counter = 0; | |
57 | reg [7:0] saved_news [3:0]; | |
58 | reg [7:0] sent_byte [3:0]; | |
59 | ||
60 | reg [7:0] bytes_sent = 0; | |
61 | ||
62 | // i2c | |
63 | reg [15:0] leds [3:0]; | |
64 | ||
65 | initial begin | |
66 | leds[0] <= 16'hF00F; | |
67 | leds[1] <= 16'h0000; | |
68 | leds[2] <= 16'h0000; | |
69 | leds[3] <= 16'hFFFF; | |
70 | end | |
71 | ||
72 | /* even rows are green, odd rows are red: | |
73 | * mb_leds[2 * k] is the kth row of green leds | |
74 | * mb_leds[2 * k + 1] is the kth row of red leds | |
75 | */ | |
76 | wire [7:0] mb_leds [15:0]; | |
77 | ||
78 | // all red leds are off | |
79 | assign mb_leds[1] = program_counter; | |
80 | assign mb_leds[3] = 0; | |
81 | assign mb_leds[5] = state; | |
82 | assign mb_leds[7] = uart_ptr; | |
83 | assign mb_leds[9] = 0; | |
84 | assign mb_leds[11] = 0; | |
85 | assign mb_leds[13] = 0; | |
86 | assign mb_leds[15] = 0; | |
87 | ||
88 | // green leds, first half | |
89 | assign mb_leds[0] = {leds[1][3:0], leds[0][3:0]}; | |
90 | assign mb_leds[2] = {leds[1][7:4], leds[0][7:4]}; | |
91 | assign mb_leds[4] = {leds[1][11:8], leds[0][11:8]}; | |
92 | assign mb_leds[6] = {leds[1][15:12], leds[0][15:12]}; | |
93 | ||
94 | // green leds, second half | |
95 | assign mb_leds[8] = {leds[3][3:0], leds[2][3:0]}; | |
96 | assign mb_leds[10] = {leds[3][7:4], leds[2][7:4]}; | |
97 | assign mb_leds[12] = {leds[3][11:8], leds[2][11:8]}; | |
98 | assign mb_leds[14] = {leds[3][15:12], leds[2][15:12]}; | |
99 | ||
100 | wire [7:0] mb_in; | |
101 | wire [5:0] mb_addr; | |
102 | assign mb_in = mb_leds[mb_addr]; | |
103 | ||
104 | reg [7:0] i2c_tx_byte; | |
105 | reg [5:0] more_bytes = 0; | |
106 | reg i2c_transmit = 0; | |
107 | wire i2c_is_transmitting; | |
108 | ||
109 | i2c_write #(.CLOCK_DIVIDE(`I2C_DIVIDE)) i2c (.clk(clk), .scl(scl), .sda(sda), .tx_byte(i2c_tx_byte), .transmit(i2c_transmit), .is_transmitting(i2c_is_transmitting), .more_bytes(more_bytes), .mb_in(mb_in), .mb_addr(mb_addr)); | |
110 | ||
111 | reg [3:0] i2c_init_step = 0; | |
112 | ||
113 | always @ (posedge clk) begin | |
114 | if(i2c_is_transmitting || i2c_transmit) | |
115 | i2c_transmit <= 0; | |
116 | else begin | |
117 | if(i2c_init_step == 0) begin | |
118 | i2c_tx_byte <= 8'h21; // turn on oscillator | |
119 | i2c_transmit <= 1; | |
120 | i2c_init_step <= 1; | |
121 | end else if(i2c_init_step == 1) begin | |
122 | i2c_tx_byte <= 8'h81; // display on, blink off | |
123 | i2c_transmit <= 1; | |
124 | i2c_init_step <= 2; | |
125 | end else if(i2c_init_step == 2) begin | |
126 | i2c_tx_byte <= 8'hEF; // max brightness | |
127 | i2c_transmit <= 1; | |
128 | i2c_init_step <= 3; | |
129 | end else if(i2c_init_step == 3) begin | |
130 | i2c_tx_byte <= 0; | |
131 | more_bytes <= 16; | |
132 | i2c_transmit <= 1; | |
133 | i2c_init_step <= 4; | |
134 | end else begin | |
135 | i2c_transmit <= 1; | |
136 | end | |
137 | end | |
138 | end | |
139 | ||
140 | ||
141 | wire received; | |
142 | wire [7:0] rx_byte; | |
143 | reg transmit = 0; | |
144 | reg [7:0] tx_byte = 0; | |
145 | wire is_receiving; | |
146 | wire is_transmitting; | |
147 | ||
148 | uart #(.CLOCK_DIVIDE(`UART_DIVIDE)) uart (.clk(clk), .rx(uart_rx), .tx(uart_tx), .received(received), .transmit(transmit), .tx_byte(tx_byte), .rx_byte(rx_byte), .is_receiving(is_receiving), .is_transmitting(is_transmitting)); | |
149 | ||
150 | assign led[4] = state != `STATE_WASTE_TIME; | |
151 | // assign led[2:0] = rom_op == 6 ? rom_led : 0; | |
152 | // assign led[3] = refresh_leds || i2c_is_transmitting; | |
153 | // assign led[0] = (leds[0] == 16'h000f); | |
154 | // assign led[1] = (leds[0] == 16'h00f0); | |
155 | // assign led[2] = (leds[0] == 16'h0111); | |
156 | // assign led[3] = (leds[0] == 16'hf00f); | |
157 | ||
158 | ||
159 | //assign led[3:0] = saved_news[0][3:0]; | |
160 | //assign led[3:0] = program_counter[3:0]; | |
161 | assign led[0] = dont_send; | |
162 | assign led[1] = dont_wait; | |
163 | assign led[2] = is_transmitting; | |
164 | ||
165 | always @(posedge clk) begin | |
166 | case(state) | |
167 | `STATE_SEND: begin | |
168 | if(transmit) begin | |
169 | transmit <= 0; | |
170 | end else if(uart_ptr == 4) begin | |
171 | uart_ptr <= 0; | |
172 | if(dont_wait) | |
173 | state <= `STATE_WASTE_TIME; | |
174 | else if(rom_op == 6) // `OP_ROUTE | |
175 | state <= `STATE_WAIT_NEWS; | |
176 | else | |
177 | state <= `STATE_WAIT_PROPAGATE; | |
178 | end else if(!is_transmitting && !dont_send) begin | |
179 | tx_byte <= rom_output[uart_ptr * 8 +: 8]; | |
180 | sent_byte[uart_ptr] <= rom_output[uart_ptr * 8 +: 8]; | |
181 | transmit <= 1; | |
182 | bytes_sent <= bytes_sent + 1; | |
183 | uart_ptr <= uart_ptr + 1; | |
184 | end | |
185 | end | |
186 | ||
187 | `STATE_WAIT_PROPAGATE: begin | |
188 | if(received) begin | |
189 | state <= `STATE_WASTE_TIME; | |
190 | end | |
191 | end | |
192 | ||
193 | `STATE_WASTE_TIME: begin | |
194 | if(waste_counter == 50000) begin | |
195 | waste_counter <= 0; | |
196 | if(program_counter == 255) | |
197 | program_counter <= main; | |
198 | else | |
199 | program_counter <= program_counter + 1; | |
200 | state <= `STATE_SEND; | |
201 | end else | |
202 | waste_counter <= waste_counter + 1; | |
203 | end | |
204 | ||
205 | `STATE_WAIT_NEWS: begin | |
206 | /** On a route instruction, we: | |
207 | - receive the instruction back | |
208 | - receive the news | |
209 | - propagate the news | |
210 | - go to `STATE_WASTE_TIME | |
211 | */ | |
212 | if(uart_ptr == 8) begin | |
213 | state <= `STATE_PROPAGATE_NEWS; | |
214 | uart_ptr <= 0; | |
215 | end else if(received) begin | |
216 | if(uart_ptr[2]) /* uart_ptr >= 4 */ | |
217 | saved_news[uart_ptr[1:0]] <= rx_byte; | |
218 | uart_ptr <= uart_ptr + 1; | |
219 | end | |
220 | end // case: `STATE_WAIT_NEWS | |
221 | ||
222 | `STATE_PROPAGATE_NEWS: begin | |
223 | if(transmit) begin | |
224 | transmit <= 0; | |
225 | end else if(uart_ptr == 4) begin | |
226 | if(rom_led) begin | |
227 | leds[rom_led - 1] <= (saved_news[1] << 8) + saved_news[0]; | |
228 | end | |
229 | state <= `STATE_WASTE_TIME; | |
230 | uart_ptr <= 0; | |
231 | end else if(!is_transmitting && !dont_send) begin | |
232 | tx_byte <= saved_news[uart_ptr]; | |
233 | sent_byte[uart_ptr] <= saved_news[uart_ptr]; | |
234 | transmit <= 1; | |
235 | bytes_sent <= bytes_sent + 1; | |
236 | uart_ptr <= uart_ptr + 1; | |
237 | end | |
238 | end // case: `STATE_PROPAGATE_NEWS | |
239 | endcase | |
240 | end | |
241 | ||
242 | endmodule |