]>
Commit | Line | Data |
---|---|---|
46a95fd3 | 1 | `include "pll.v" |
3b542afc | 2 | `include "master_rom.v" |
ffba35f8 MG |
3 | `include "i2c.v" |
4 | `include "uart.v" | |
3b542afc MG |
5 | |
6 | `ifdef SIM | |
7 | `define UART_DIVIDE 1 | |
46a95fd3 | 8 | `define I2C_DIVIDE 4 |
3b542afc | 9 | `else |
2542aba7 | 10 | `define UART_DIVIDE 1024 |
46a95fd3 | 11 | `define I2C_DIVIDE 256 |
3b542afc MG |
12 | `endif |
13 | ||
46a95fd3 MG |
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; | |
3b542afc | 16 | |
46a95fd3 | 17 | assign clk = CLKin; |
3b542afc | 18 | |
46a95fd3 | 19 | // ROM |
3b542afc | 20 | |
46a95fd3 MG |
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; | |
3b542afc | 25 | |
46a95fd3 MG |
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) | |
2542aba7 | 39 | dont_send <= 21'b111111111111111111111; |
46a95fd3 MG |
40 | else if(dont_send) |
41 | dont_send <= dont_send - 1; | |
3b542afc MG |
42 | end |
43 | ||
46a95fd3 | 44 | // state |
3b542afc | 45 | |
46a95fd3 MG |
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]; | |
3b542afc | 103 | |
ffba35f8 | 104 | reg [7:0] i2c_tx_byte; |
46a95fd3 | 105 | reg [5:0] more_bytes = 0; |
ffba35f8 MG |
106 | reg i2c_transmit = 0; |
107 | wire i2c_is_transmitting; | |
108 | ||
46a95fd3 | 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)); |
ffba35f8 MG |
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 | |
46a95fd3 | 122 | i2c_tx_byte <= 8'h81; // display on, blink off |
ffba35f8 MG |
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; | |
46a95fd3 MG |
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; | |
ffba35f8 MG |
136 | end |
137 | end | |
138 | end | |
139 | ||
3b542afc | 140 | |
3b542afc MG |
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 | ||
3b542afc MG |
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 | ||
46a95fd3 MG |
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); | |
7f1b6bd9 | 157 | |
7f1b6bd9 | 158 | |
46a95fd3 MG |
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; | |
7f1b6bd9 | 164 | |
3b542afc MG |
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 | |
3b542afc | 171 | uart_ptr <= 0; |
46a95fd3 MG |
172 | if(dont_wait) |
173 | state <= `STATE_WASTE_TIME; | |
174 | else if(rom_op == 6) // `OP_ROUTE | |
7f1b6bd9 MG |
175 | state <= `STATE_WAIT_NEWS; |
176 | else | |
177 | state <= `STATE_WAIT_PROPAGATE; | |
46a95fd3 | 178 | end else if(!is_transmitting && !dont_send) begin |
3b542afc | 179 | tx_byte <= rom_output[uart_ptr * 8 +: 8]; |
46a95fd3 | 180 | sent_byte[uart_ptr] <= rom_output[uart_ptr * 8 +: 8]; |
3b542afc | 181 | transmit <= 1; |
46a95fd3 | 182 | bytes_sent <= bytes_sent + 1; |
3b542afc MG |
183 | uart_ptr <= uart_ptr + 1; |
184 | end | |
185 | end | |
186 | ||
187 | `STATE_WAIT_PROPAGATE: begin | |
188 | if(received) begin | |
7f1b6bd9 | 189 | state <= `STATE_WASTE_TIME; |
3b542afc MG |
190 | end |
191 | end | |
192 | ||
7f1b6bd9 | 193 | `STATE_WASTE_TIME: begin |
46a95fd3 | 194 | if(waste_counter == 50000) begin |
7f1b6bd9 | 195 | waste_counter <= 0; |
46a95fd3 MG |
196 | if(program_counter == 255) |
197 | program_counter <= main; | |
198 | else | |
199 | program_counter <= program_counter + 1; | |
7f1b6bd9 MG |
200 | state <= `STATE_SEND; |
201 | end else | |
202 | waste_counter <= waste_counter + 1; | |
203 | end | |
204 | ||
3b542afc | 205 | `STATE_WAIT_NEWS: begin |
7f1b6bd9 MG |
206 | /** On a route instruction, we: |
207 | - receive the instruction back | |
208 | - receive the news | |
209 | - propagate the news | |
ffba35f8 | 210 | - go to `STATE_WASTE_TIME |
7f1b6bd9 MG |
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 | |
3b542afc | 221 | |
7f1b6bd9 | 222 | `STATE_PROPAGATE_NEWS: begin |
46a95fd3 MG |
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 | |
7f1b6bd9 MG |
229 | state <= `STATE_WASTE_TIME; |
230 | uart_ptr <= 0; | |
46a95fd3 | 231 | end else if(!is_transmitting && !dont_send) begin |
7f1b6bd9 | 232 | tx_byte <= saved_news[uart_ptr]; |
46a95fd3 | 233 | sent_byte[uart_ptr] <= saved_news[uart_ptr]; |
7f1b6bd9 | 234 | transmit <= 1; |
46a95fd3 | 235 | bytes_sent <= bytes_sent + 1; |
7f1b6bd9 MG |
236 | uart_ptr <= uart_ptr + 1; |
237 | end | |
46a95fd3 | 238 | end // case: `STATE_PROPAGATE_NEWS |
3b542afc MG |
239 | endcase |
240 | end | |
241 | ||
242 | endmodule |