`include "master_rom.v" `include "i2c.v" `include "uart.v" `ifdef SIM `define UART_DIVIDE 1 `else `define UART_DIVIDE 1 // s/192/3/ for 19200 baud uart `endif module master(input CLKin, output [4:0] led, output uart_tx, input uart_rx, output reg ready_out = 1, input ready_in, output scl, output sda); // wire clk; // wire clk_tmp; //pll pll (.clock_in(CLKin), .clock_out(clk)); reg [20:0] counter = 0; `ifdef SIM wire clk = CLKin; `else reg clk = 0; always @ (posedge CLKin) begin if(counter == 5000) begin counter <= 0; clk <= 1 - clk; end else counter <= counter + 1; end `endif reg [3:0] program_counter = 0; wire [31:0] rom_output; master_rom master_rom (.clk(clk), .addr(program_counter), .data(rom_output)); reg [7:0] i2c_tx_byte; reg i2c_transmit = 0; wire i2c_is_transmitting; i2c_write i2c (.clk(clk), .scl(scl), .sda(sda), .tx_byte(i2c_tx_byte), .transmit(i2c_transmit), .is_transmitting(i2c_is_transmitting)); reg [3:0] i2c_init_step = 0; always @ (posedge clk) begin if(i2c_is_transmitting || i2c_transmit) i2c_transmit <= 0; else begin if(i2c_init_step == 0) begin i2c_tx_byte <= 8'h21; // turn on oscillator i2c_transmit <= 1; i2c_init_step <= 1; end else if(i2c_init_step == 1) begin i2c_tx_byte <= 8'h87; // display on, blink 0.5Hz i2c_transmit <= 1; i2c_init_step <= 2; end else if(i2c_init_step == 2) begin i2c_tx_byte <= 8'hEF; // max brightness i2c_transmit <= 1; i2c_init_step <= 3; end end end `define STATE_SEND 0 `define STATE_WAIT_PROPAGATE 1 `define STATE_WAIT_NEWS 2 `define STATE_PROPAGATE_NEWS 3 `define STATE_WASTE_TIME 4 reg [5:0] state = `STATE_SEND; reg [5:0] uart_ptr = 0; wire received; wire [7:0] rx_byte; reg transmit = 0; reg [7:0] tx_byte = 0; wire is_receiving; wire is_transmitting; // 19200 (actually 300) baud uart 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)); reg [15:0] waste_counter = 0; reg [7:0] saved_news [3:0]; assign led[4] = state != `STATE_WASTE_TIME; assign led[3:0] = i2c_init_step; always @(posedge clk) begin case(state) `STATE_SEND: begin if(transmit) begin transmit <= 0; end else if(uart_ptr == 4) begin program_counter <= program_counter + 1; uart_ptr <= 0; if(rom_output[26:24] == 6) // `OP_ROUTE state <= `STATE_WAIT_NEWS; else state <= `STATE_WAIT_PROPAGATE; end else if(!is_transmitting && ready_in) begin tx_byte <= rom_output[uart_ptr * 8 +: 8]; transmit <= 1; uart_ptr <= uart_ptr + 1; end end `STATE_WAIT_PROPAGATE: begin if(received) begin state <= `STATE_WASTE_TIME; end end `STATE_WASTE_TIME: begin if(waste_counter == 100) begin waste_counter <= 0; state <= `STATE_SEND; end else waste_counter <= waste_counter + 1; end `STATE_WAIT_NEWS: begin /** On a route instruction, we: - receive the instruction back - receive the news - propagate the news - go to `STATE_WASTE_TIME */ if(uart_ptr == 8) begin state <= `STATE_PROPAGATE_NEWS; uart_ptr <= 0; end else if(received) begin if(uart_ptr[2]) /* uart_ptr >= 4 */ saved_news[uart_ptr[1:0]] <= rx_byte; uart_ptr <= uart_ptr + 1; end end // case: `STATE_WAIT_NEWS `STATE_PROPAGATE_NEWS: begin if(uart_ptr == 4) begin state <= `STATE_WASTE_TIME; uart_ptr <= 0; end else if(!is_transmitting && ready_in) begin tx_byte <= saved_news[uart_ptr]; transmit <= 1; uart_ptr <= uart_ptr + 1; end end endcase end endmodule