-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtop.v
More file actions
181 lines (156 loc) · 4.98 KB
/
top.v
File metadata and controls
181 lines (156 loc) · 4.98 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
module nanoV_top (
input clk12MHz,
input rstn,
input spi_miso,
output reg spi_select,
output spi_clk_out,
output reg spi_mosi,
input uart_rxd,
output uart_txd,
output uart_rts,
input button1,
input button2,
input button3,
output out0,
output out1,
output out2,
output out3,
output out4,
output out5,
output out6,
output out7,
output led1,
output led2,
output led3,
output led4,
output led5,
output led6,
output led7,
output led8,
output lcol1,
output lcol2,
output lcol3,
output lcol4,
output spkp,
output spkm);
wire cpu_clk;
assign cpu_clk = clk12MHz;
reg buffered_spi_in;
wire spi_data_out, spi_select_out, spi_clk_enable;
wire [31:0] data_in;
wire [31:0] addr_out;
wire [31:0] data_out;
wire is_data, is_addr, is_data_in;
nanoV_cpu nano(
cpu_clk,
rstn,
buffered_spi_in,
spi_select_out,
spi_data_out,
spi_clk_enable,
data_in,
addr_out,
data_out,
is_data,
is_addr,
is_data_in);
localparam PERI_NONE = 0;
localparam PERI_LEDS = 1;
localparam PERI_GPIO_OUT = 2;
localparam PERI_GPIO_IN = 3;
localparam PERI_UART = 4;
localparam PERI_UART_STATUS = 5;
localparam PERI_MUSIC = 6;
reg [2:0] connect_peripheral;
always @(posedge cpu_clk) begin
if (!rstn) begin
connect_peripheral <= PERI_NONE;
end
else if (is_addr) begin
if (addr_out == 32'h10000000) connect_peripheral <= PERI_GPIO_OUT;
else if (addr_out == 32'h10000004) connect_peripheral <= PERI_GPIO_IN;
else if (addr_out == 32'h10000008) connect_peripheral <= PERI_LEDS;
else if (addr_out == 32'h10000010) connect_peripheral <= PERI_UART;
else if (addr_out == 32'h10000014) connect_peripheral <= PERI_UART_STATUS;
else if (addr_out == 32'h10000020) connect_peripheral <= PERI_MUSIC;
else connect_peripheral <= PERI_NONE;
end
end
reg [7:0] output_data;
reg [31:0] led_data;
reg [7:0] midi_note;
always @(posedge cpu_clk) begin
if (!rstn) begin
led_data <= 0;
output_data <= 0;
end else if (is_data) begin
if (connect_peripheral == PERI_LEDS) led_data <= data_out;
else if (connect_peripheral == PERI_GPIO_OUT) output_data <= data_out[7:0];
else if (connect_peripheral == PERI_MUSIC) midi_note <= data_out[7:0];
end
end
assign { out7, out6, out5, out4, out3, out2, out1, out0 } = output_data;
wire uart_tx_busy;
wire uart_rx_valid;
wire [7:0] uart_rx_data;
assign data_in[31:8] = 0;
assign data_in[7:0] = connect_peripheral == PERI_GPIO_OUT ? output_data :
connect_peripheral == PERI_GPIO_IN ? {5'b0, button3, button2, button1} :
connect_peripheral == PERI_UART ? uart_rx_data :
connect_peripheral == PERI_UART_STATUS ? {6'b0, uart_rx_valid, uart_tx_busy} :
connect_peripheral == PERI_MUSIC ? midi_note : 0;
wire uart_tx_start = is_data && connect_peripheral == PERI_UART;
wire [7:0] uart_tx_data = data_out[7:0];
uart_tx #(.CLK_HZ(12_000_000), .BIT_RATE(93_750)) i_uart_tx(
.clk(cpu_clk),
.resetn(rstn),
.uart_txd(uart_txd),
.uart_tx_en(uart_tx_start),
.uart_tx_data(uart_tx_data),
.uart_tx_busy(uart_tx_busy)
);
uart_rx #(.CLK_HZ(12_000_000), .BIT_RATE(93_750)) i_uart_rx(
.clk(cpu_clk),
.resetn(rstn),
.uart_rxd(uart_rxd),
.uart_rts(uart_rts),
.uart_rx_read(connect_peripheral == PERI_UART && is_data_in),
.uart_rx_valid(uart_rx_valid),
.uart_rx_data(uart_rx_data)
);
// TODO: Probably need to use SB_IO directly for reading/writing with good timing
always @(negedge cpu_clk) begin
buffered_spi_in <= spi_miso;
end
always @(posedge cpu_clk) begin
if (!rstn)
spi_select <= 1;
else
spi_select <= spi_select_out;
spi_mosi <= spi_data_out;
end
assign spi_clk_out = !cpu_clk && spi_clk_enable;
// map the output of ledscan to the port pins
wire [7:0] leds_out;
wire [3:0] lcol;
assign { led8, led7, led6, led5, led4, led3, led2, led1 } = leds_out[7:0];
assign { lcol4, lcol3, lcol2, lcol1 } = lcol[3:0];
LedScan scan (
.clk12MHz(clk12MHz),
.leds1(led_data[31:24]),
.leds2(led_data[23:16]),
.leds3(led_data[15:8]),
.leds4(led_data[7:0]),
.leds(leds_out),
.lcol(lcol)
);
wire spk_out;
Music music (
.clk12MHz(clk12MHz),
.rstn(rstn),
.midi_note(midi_note),
.spk_out(spk_out)
);
assign spkp = spk_out;
assign spkm = !spk_out;
endmodule