blob: 30b03fa07e08c07a8459302d1127844ba99ef04d [file] [log] [blame]
Wenting Zhang7cb1b882022-11-24 18:47:53 -05001`timescale 1ns / 1ps
2//////////////////////////////////////////////////////////////////////////////////
3// Company:
4// Engineer: Wenting Zhang
5//
6// Create Date: 13:13:04 04/13/2018
7// Module Name: serial
8// Project Name: VerilogBoy
9// Description:
10// Dummy serial interface
11// Dependencies:
12//
13// Additional Comments:
14//
15//////////////////////////////////////////////////////////////////////////////////
16module serial(
17 input clk,
18 input rst,
19 input wire [15:0] a,
20 output reg [7:0] dout,
21 input wire [7:0] din,
22 input wire rd,
23 input wire wr,
24 output reg int_serial_req,
25 input wire int_serial_ack
26 );
27
28 reg clk_spi; //8kHz SPI Clock
29
30 /*clk_div #(.WIDTH(10), .DIV(512)) spi_div(
31 .i(clk),
32 .o(clk_spi)
33 );*/
34
35 reg [8:0] counter;
36
37 always @(posedge clk)
38 begin
39 if (rst) begin
40 counter <= 9'h72;
41 clk_spi <= 1'b0;
42 end
43 else begin
44 if (counter == (512 / 2 - 1)) begin
45 clk_spi <= ~clk_spi;
46 counter <= 0;
47 end
48 else
49 counter <= counter + 1'b1;
50 end
51 end
52
53 //reg [7:0] reg_sb;
54 reg reg_sc_start;
55 reg reg_sc_int;
56
57 always @(*) begin
58 dout = 8'hff;
59 if (a == 16'hff01) dout = 8'hff; else
60 if (a == 16'hff02) dout = {reg_sc_start, 6'b111111, reg_sc_int};
61 end
62
63 reg [3:0] count;
64 reg last_clk;
65
66 always @(posedge clk) begin
67 if (rst) begin
68 //reg_sb <= 8'h00;
69 reg_sc_start <= 1'b0;
70 reg_sc_int <= 1'b0;
71 int_serial_req <= 1'b0;
72 count <= 4'd0;
73 last_clk <= 1'b0;
74 end
75 else begin
76 last_clk <= clk_spi;
77 //if (wr && (a == 16'hff01)) reg_sb <= din;
78 if (wr && (a == 16'hff02)) begin
79 reg_sc_start <= din[7];
80 reg_sc_int <= din[0];
81 if (din[7] && din[0]) count <= 4'd8;
82 else count <= 4'd0;
83 end
84 else begin
85 // Dummy serial interface
86 if (count != 4'd0) begin
87 if (!last_clk && clk_spi) begin
88 count <= count - 4'd1;
89 if ((count - 4'd1) == 0) begin
90 int_serial_req <= 1'b1;
91 end
92 end
93 end
94 else begin
95 if ((int_serial_req)&&(int_serial_ack)) begin
96 int_serial_req <= 1'b0;
97 end
98 end
99 end
100 end
101 end
102
103
104endmodule