2019-03-04 14:40:31 +01:00
|
|
|
/*
|
|
|
|
(c) Raphaël Jacquot 2019
|
|
|
|
|
|
|
|
This file is part of hp_saturn.
|
|
|
|
|
|
|
|
hp_saturn is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
hp_saturn is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Foobar. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
`default_nettype none
|
|
|
|
|
|
|
|
module saturn_serial (
|
|
|
|
i_clk,
|
|
|
|
|
|
|
|
i_char_to_send,
|
|
|
|
i_char_valid,
|
|
|
|
|
|
|
|
o_serial_tx,
|
|
|
|
|
|
|
|
o_serial_busy
|
|
|
|
);
|
|
|
|
|
|
|
|
input wire [0:0] i_clk;
|
|
|
|
|
|
|
|
input wire [7:0] i_char_to_send;
|
|
|
|
input wire [0:0] i_char_valid;
|
|
|
|
|
|
|
|
output wire [0:0] o_serial_tx;
|
|
|
|
|
|
|
|
output wire [0:0] o_serial_busy;
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-03-04 17:01:59 +01:00
|
|
|
reg [10:0] clocking_reg;
|
2019-03-04 14:40:31 +01:00
|
|
|
reg [9:0] data_reg;
|
|
|
|
|
|
|
|
`ifdef SIM
|
|
|
|
`define BIT_DELAY_START 13'h0
|
|
|
|
`define BIT_DELAY_TEST 0
|
|
|
|
`else
|
2019-03-04 14:53:48 +01:00
|
|
|
/* 9600 */
|
|
|
|
// `define BIT_DELAY_START 13'h54D
|
|
|
|
//`define BIT_DELAY_TEST 12
|
|
|
|
|
|
|
|
/* 115200 */
|
|
|
|
`define BIT_DELAY_START 13'h27
|
|
|
|
`define BIT_DELAY_TEST 8
|
|
|
|
|
2019-03-04 14:40:31 +01:00
|
|
|
`endif
|
|
|
|
|
|
|
|
reg [12:0] bit_delay;
|
|
|
|
|
|
|
|
initial begin
|
2019-03-04 15:15:11 +01:00
|
|
|
bit_delay = `BIT_DELAY_START;
|
2019-03-04 17:01:59 +01:00
|
|
|
clocking_reg = {11{1'b1}};
|
2019-03-04 15:15:11 +01:00
|
|
|
data_reg = {10{1'b1}};
|
2019-03-04 14:40:31 +01:00
|
|
|
end
|
|
|
|
|
2019-03-04 15:15:11 +01:00
|
|
|
assign o_serial_busy = !clocking_reg[0];
|
|
|
|
assign o_serial_tx = data_reg[0];
|
2019-03-04 14:40:31 +01:00
|
|
|
|
|
|
|
always @(posedge i_clk) begin
|
2019-03-04 15:15:11 +01:00
|
|
|
bit_delay <= bit_delay + 13'd1;
|
2019-03-04 17:01:59 +01:00
|
|
|
|
2019-03-04 14:40:31 +01:00
|
|
|
// $display("%0d", bit_delay);
|
|
|
|
if (i_char_valid && !o_serial_busy) begin
|
2019-03-04 18:29:00 +01:00
|
|
|
// $write("%c", i_char_to_send);
|
2019-03-04 15:44:51 +01:00
|
|
|
// $display("serial storing char %c", i_char_to_send);
|
2019-03-04 17:01:59 +01:00
|
|
|
clocking_reg <= 11'b0;
|
2019-03-04 15:15:11 +01:00
|
|
|
data_reg <= { 1'b1, i_char_to_send, 1'b0 };
|
2019-03-04 14:40:31 +01:00
|
|
|
bit_delay <= `BIT_DELAY_START;
|
|
|
|
end
|
2019-03-04 17:01:59 +01:00
|
|
|
|
|
|
|
if (o_serial_busy && bit_delay[`BIT_DELAY_TEST]) begin
|
|
|
|
// $display("%b %b %b", o_serial_tx, data_reg, clocking_reg);
|
|
|
|
clocking_reg <= { 1'b1, clocking_reg[10:1] };
|
2019-03-04 15:15:11 +01:00
|
|
|
data_reg <= { 1'b1, data_reg[9:1] };
|
2019-03-04 14:40:31 +01:00
|
|
|
bit_delay <= `BIT_DELAY_START;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
endmodule
|