2 `include "master_rom.v"
10 `define UART_DIVIDE 1024
11 `define I2C_DIVIDE 256
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);
21 reg [7:0] program_counter = 0;
22 // go here at end of program
24 wire [31:0] rom_output;
26 master_rom master_rom (.clk(clk), .addr(program_counter), .data(rom_output));
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];
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;
37 always @(posedge clk) begin
39 dont_send <= 21'b111111111111111111111;
41 dont_send <= dont_send - 1;
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
53 reg [5:0] state = `STATE_SEND;
54 reg [5:0] uart_ptr = 0;
56 reg [30:0] waste_counter = 0;
57 reg [7:0] saved_news [3:0];
58 reg [7:0] sent_byte [3:0];
60 reg [7:0] bytes_sent = 0;
63 reg [15:0] leds [3:0];
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
76 wire [7:0] mb_leds [15:0];
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;
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]};
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]};
102 assign mb_in = mb_leds[mb_addr];
104 reg [7:0] i2c_tx_byte;
105 reg [5:0] more_bytes = 0;
106 reg i2c_transmit = 0;
107 wire i2c_is_transmitting;
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));
111 reg [3:0] i2c_init_step = 0;
113 always @ (posedge clk) begin
114 if(i2c_is_transmitting || i2c_transmit)
117 if(i2c_init_step == 0) begin
118 i2c_tx_byte <= 8'h21; // turn on oscillator
121 end else if(i2c_init_step == 1) begin
122 i2c_tx_byte <= 8'h81; // display on, blink off
125 end else if(i2c_init_step == 2) begin
126 i2c_tx_byte <= 8'hEF; // max brightness
129 end else if(i2c_init_step == 3) begin
144 reg [7:0] tx_byte = 0;
146 wire is_transmitting;
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));
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);
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;
165 always @(posedge clk) begin
170 end else if(uart_ptr == 4) begin
173 state <= `STATE_WASTE_TIME;
174 else if(rom_op == 6) // `OP_ROUTE
175 state <= `STATE_WAIT_NEWS;
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];
182 bytes_sent <= bytes_sent + 1;
183 uart_ptr <= uart_ptr + 1;
187 `STATE_WAIT_PROPAGATE: begin
189 state <= `STATE_WASTE_TIME;
193 `STATE_WASTE_TIME: begin
194 if(waste_counter == 50000) begin
196 if(program_counter == 255)
197 program_counter <= main;
199 program_counter <= program_counter + 1;
200 state <= `STATE_SEND;
202 waste_counter <= waste_counter + 1;
205 `STATE_WAIT_NEWS: begin
206 /** On a route instruction, we:
207 - receive the instruction back
210 - go to `STATE_WASTE_TIME
212 if(uart_ptr == 8) begin
213 state <= `STATE_PROPAGATE_NEWS;
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;
220 end // case: `STATE_WAIT_NEWS
222 `STATE_PROPAGATE_NEWS: begin
225 end else if(uart_ptr == 4) begin
227 leds[rom_led - 1] <= (saved_news[1] << 8) + saved_news[0];
229 state <= `STATE_WASTE_TIME;
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];
235 bytes_sent <= bytes_sent + 1;
236 uart_ptr <= uart_ptr + 1;
238 end // case: `STATE_PROPAGATE_NEWS