-
Notifications
You must be signed in to change notification settings - Fork 17
/
spram-demo.v
111 lines (96 loc) · 2.31 KB
/
spram-demo.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/** \file
* Demo reading / writing to the single port RAM.
*
* The up5k has 1024 Kb of single ported block RAM.
* This is can't read/write simultaneously, so it is necessary to
* mux the read/write pins.
*
* This is another serial demo, but the FIFO is stored in the
* SPRAM and clocked out slowly to show how much the FIFO can store.
* If you overflow the FIFO, bad things happen.
*/
`include "util.v"
`include "spram.v"
`include "uart.v"
module top(
output led_r,
output led_g,
output led_b,
output serial_txd,
input serial_rxd,
output spi_cs,
output gpio_2
);
assign spi_cs = 1; // it is necessary to turn off the SPI flash chip
wire debug0 = gpio_2;
wire clk_48;
wire reset = 0;
SB_HFOSC u_hfosc (
.CLKHFPU(1'b1),
.CLKHFEN(1'b1),
.CLKHF(clk_48)
);
reg [31:0] counter;
always @(posedge clk_48)
counter <= counter + 1;
// pulse the green LED to know that we're alive
wire pwm_g;
pwm pwm_g_driver(clk_48, 1, pwm_g);
assign led_g = !(counter[25:23] == 0 && pwm_g);
assign led_b = serial_rxd; // idles high
// generate a 3 MHz/12 MHz serial clock from the 48 MHz clock
// this is the 3 Mb/s maximum supported by the FTDI chip
wire clk_1, clk_4;
divide_by_n #(.N(16)) div1(clk_48, reset, clk_1);
divide_by_n #(.N( 4)) div4(clk_48, reset, clk_4);
reg [7:0] uart_txd;
reg uart_txd_strobe;
wire uart_txd_ready;
wire [7:0] uart_rxd;
wire uart_rxd_strobe;
uart_tx txd(
.mclk(clk_48),
.reset(reset),
.baud_x1(clk_1),
.serial(serial_txd),
.ready(uart_txd_ready),
.data(uart_txd),
.data_strobe(uart_txd_strobe)
);
uart_rx rxd(
.mclk(clk_48),
.reset(reset),
.baud_x4(clk_4),
.serial(serial_rxd),
.data(uart_rxd),
.data_strobe(uart_rxd_strobe)
);
assign debug0 = serial_txd;
reg fifo_read_strobe;
wire fifo_available;
fifo_spram buffer(
.clk(clk_48),
.reset(reset),
.write_data(uart_rxd),
.write_strobe(uart_rxd_strobe),
.data_available(fifo_available),
.read_data(uart_txd),
.read_strobe(fifo_read_strobe)
);
always @(posedge clk_48) begin
uart_txd_strobe <= 0;
fifo_read_strobe <= 0;
led_r <= 1;
// single port fifo can't read/write the same cycle
if (fifo_available
&& uart_txd_ready
&& !uart_rxd_strobe
&& !uart_txd_strobe
&& counter[18:0] == 0
) begin
fifo_read_strobe <= 1;
uart_txd_strobe <= 1;
led_r <= 0;
end
end
endmodule