Debian Codegraph @

examples/emcu-blinky.v

raw
1 `default_nettype none
2
3 /*
4 * The LED on the board should blink.
5 * Attention: flashing is performed with firmware as follows:
6 * openFPGALoader -f -b tangnano4k --mcufw=emcu-firmware/blinky.bin emcu-blinky-tangnano4k.fs
7 *
8 * or
9 * openFPGALoader -f -b tangnano4k --mcufw=emcu-firmware/uart.bin emcu-blinky-tangnano4k.fs
10 *
11 */
12
13 module top (
14 input wire clk,
15 inout wire mode_led,
16 input wire RXD,
17 output wire TXD,
18 input wire rst_i);
19
20 // dummy sinks
21 wire dummy_uart1_txd;
22 wire dummy_uart0_baudtick;
23 wire dummy_uart1_baudtick;
24 wire dummy_intmonitor;
25 wire dummy_targexp0_hsel;
26 wire [31:0] dummy_targexp0_haddr;
27 wire [1:0] dummy_targexp0_htrans;
28 wire dummy_targexp0_hwrite;
29 wire [2:0] dummy_targexp0_hsize;
30 wire [2:0] dummy_targexp0_hburst;
31 wire [3:0] dummy_targexp0_hprot;
32 wire [1:0] dummy_targexp0_memattr;
33 wire dummy_targexp0_exreq;
34 wire [3:0] dummy_targexp0_hmaster;
35 wire [31:0] dummy_targexp0_hwdata;
36 wire dummy_targexp0_hmastlock;
37 wire dummy_targexp0_hauser;
38 wire [3:0] dummy_targexp0_hwuser;
39 wire [31:0] dummy_initexp0_hrdata;
40 wire dummy_initexp0_hready;
41 wire dummy_initexp0_hresp;
42 wire dummy_initexp0_exresp;
43 wire [2:0] dummy_initexp0_hruser;
44 wire [3:0] dummy_apbtargexp2_pstrb;
45 wire [2:0] dummy_apbtargexp2_pprot;
46 wire dummy_apbtargexp2_psel;
47 wire dummy_apbtargexp2_penable;
48 wire [11:0] dummy_apbtargexp2_paddr;
49 wire dummy_apbtargexp2_pwrite;
50 wire [31:0] dummy_apbtargexp2_pwdata;
51 wire dummy_daptdo;
52 wire dummy_dapjtagnsw;
53 wire [3:0] dummy_tpiutracedata;
54 wire dummy_targexp0_hreadymux;
55 wire dummy_dapntdoen;
56
57 // ROM 32k
58 wire [12:0] rom_addr;
59 wire [15:0] dummy_rom_addr; // flash is addressed by 4 bytes each, and there are also unused high bits of the address
60 wire targflash0_hsel;
61 wire [1:0] targflash0_htrans;
62 wire [2:0] dummy_targflash0_hsize;
63 wire [2:0] dummy_targflash0_hburst;
64 wire dummy_targflash0_hreadymux;
65 wire [31:0] rom_out;
66 wire targflash0_readyout;
67
68 // SRAM 8k
69 wire [10:0] sram0_addr;
70 wire [1:0] dummy_sram0_addr; // high address bits
71 wire sram0_cs;
72 wire [3:0] sram0_wren; // byte write mask
73 wire [31:0] sram0_wdata;
74 wire [31:0] sram0_rdata; // all 32bit
75 wire [23:0] dummy_sram0_rdata_0; // for unused bits
76 wire [23:0] dummy_sram0_rdata_1;
77 wire [23:0] dummy_sram0_rdata_2;
78 wire [23:0] dummy_sram0_rdata_3;
79
80 wire mtx_hreset_n;
81
82 // The processor reset inputs are not explicitly brought out - Global Set Reset is used.
83 GSR gsr (
84 .GSRI(rst_i)
85 );
86
87 wire GND = 1'b0;
88 wire VCC = 1'b1;
89
90 // GPIO inout ports, 16 total but we need only one
91 wire [15:0] gpio_out;
92 wire [15:0] gpio_oen; // active low
93 wire [15:0] gpio_in;
94
95 IOBUF led(
96 .O(gpio_in[0]),
97 .IO(mode_led),
98 .I(gpio_out[0]),
99 .OEN(~gpio_oen[0])
100 );
101
102 EMCU cpu (
103 .FCLK(clk),
104 .PORESETN(GND), // doesn't matter
105 .SYSRESETN(GND), // doesn't matter
106 .RTCSRCCLK(GND), // this is normal port but we haven't RTC in this example
107 .MTXHRESETN(mtx_hreset_n),
108
109 .IOEXPOUTPUTO(gpio_out),
110 .IOEXPOUTPUTENO(gpio_oen),
111 .IOEXPINPUTI(gpio_in),
112
113 .UART0RXDI(RXD),
114 .UART1RXDI(GND),
115 .UART0TXDO(TXD),
116 .UART1TXDO(dummy_uart1_txd),
117 .UART0BAUDTICK(dummy_uart0_baudtick),
118 .UART1BAUDTICK(dummy_uart1_baudtick),
119
120 .INTMONITOR(dummy_intmonitor),
121
122 .SRAM0ADDR({dummy_sram0_addr, sram0_addr}),
123 .SRAM0CS(sram0_cs),
124 .SRAM0WREN(sram0_wren),
125 .SRAM0WDATA(sram0_wdata),
126 .SRAM0RDATA(sram0_rdata),
127
128 .TARGFLASH0HSEL(targflash0_hsel),
129 .TARGFLASH0HADDR({dummy_rom_addr[15:2], rom_addr, dummy_rom_addr[1:0]}),
130 .TARGFLASH0HTRANS(targflash0_htrans),
131 .TARGFLASH0HSIZE(dummy_targflash0_hsize),
132 .TARGFLASH0HBURST(dummy_targflash0_hburst),
133 .TARGFLASH0HREADYMUX(dummy_targflash0_hreadymux),
134 .TARGFLASH0HRDATA(rom_out),
135 .TARGFLASH0HRUSER({GND,GND,GND}),
136 .TARGFLASH0HRESP(GND),
137 .TARGFLASH0EXRESP(GND),
138 .TARGFLASH0HREADYOUT(targflash0_readyout),
139
140 .TARGEXP0HSEL(dummy_targexp0_hsel),
141 .TARGEXP0HADDR(dummy_targexp0_haddr),
142 .TARGEXP0HTRANS(dummy_targexp0_htrans),
143 .TARGEXP0HWRITE(dummy_targexp0_hwrite),
144 .TARGEXP0HSIZE(dummy_targexp0_hsize),
145 .TARGEXP0HBURST(dummy_targexp0_hburst[2:0]),
146 .TARGEXP0HPROT(dummy_targexp0_hprot[3:0]),
147 .TARGEXP0MEMATTR(dummy_targexp0_memattr),
148 .TARGEXP0EXREQ(dummy_targexp0_exreq),
149 .TARGEXP0HMASTER(dummy_targexp0_hmaster),
150 .TARGEXP0HWDATA(dummy_targexp0_hwdata),
151 .TARGEXP0HMASTLOCK(dummy_targexp0_hmastlock),
152 .TARGEXP0HREADYMUX(dummy_targexp0_hreadymux),
153 .TARGEXP0HAUSER(dummy_targexp0_hauser),
154 .TARGEXP0HWUSER(dummy_targexp0_hwuser),
155 .INITEXP0HRDATA(dummy_initexp0_hrdata),
156 .INITEXP0HREADY(dummy_initexp0_hready),
157 .INITEXP0HRESP(dummy_initexp0_hresp),
158 .INITEXP0EXRESP(dummy_initexp0_exresp),
159 .INITEXP0HRUSER(dummy_initexp0_hruser),
160 .APBTARGEXP2PSTRB(dummy_apbtargexp2_pstrb),
161 .APBTARGEXP2PPROT(dummy_apbtargexp2_pprot),
162 .APBTARGEXP2PSEL(dummy_apbtargexp2_psel),
163 .APBTARGEXP2PENABLE(dummy_apbtargexp2_penable),
164 .APBTARGEXP2PADDR(dummy_apbtargexp2_paddr),
165 .APBTARGEXP2PWRITE(dummy_apbtargexp2_pwrite),
166 .APBTARGEXP2PWDATA(dummy_apbtargexp2_pwdata),
167 .DAPTDO(dummy_daptdo),
168 .DAPJTAGNSW(dummy_dapjtagnsw),
169 .DAPNTDOEN(dummy_dapntdoen),
170 .TPIUTRACEDATA(dummy_tpiutracedata),
171 .TARGEXP0HRDATA({32{GND}}),
172 .TARGEXP0HREADYOUT(GND),
173 .TARGEXP0HRESP(VCC), // XXX
174 .TARGEXP0EXRESP(GND),
175 .TARGEXP0HRUSER({GND,GND,GND}),
176 .INITEXP0HSEL(GND),
177 .INITEXP0HADDR({32{GND}}),
178 .INITEXP0HTRANS({GND,GND}),
179 .INITEXP0HWRITE(GND),
180 .INITEXP0HSIZE({GND,GND,GND}),
181 .INITEXP0HBURST({GND,GND,GND}),
182 .INITEXP0HPROT({GND,GND,GND,GND}),
183 .INITEXP0MEMATTR({GND,GND}),
184 .INITEXP0EXREQ(GND),
185 .INITEXP0HMASTER({GND,GND,GND,GND}),
186 .INITEXP0HWDATA({32{GND}}),
187 .INITEXP0HMASTLOCK(GND),
188 .INITEXP0HAUSER(GND),
189 .INITEXP0HWUSER({GND,GND,GND,GND}),
190 .APBTARGEXP2PRDATA({32{GND}}),
191 .APBTARGEXP2PREADY(GND),
192 .APBTARGEXP2PSLVERR(GND),
193 .DAPSWDITMS(GND),
194 .FLASHERR(GND),
195 .FLASHINT(GND)
196 );
197
198 // ROM
199 // To understand what the entrances and outputs of this primitive, I followed the description of the AMBA protocol.
200 // https://developer.arm.com/documentation/ihi0011/a/ or search for IHI0011a.pdf
201 // It was from there and by the names of the ports of the primitive that it
202 // became clear that RAM is connected directly without tricks, but for Flash
203 // it would be necessary to implement a whole client of this AHB Bus.
204 // My implementation is naive and bad, but it works. And by the way, I did
205 // not bother with another APB bus because for my purposes there was enough
206 // UART and GPIO :)
207 //
208 localparam ROM_IDLE = 2'b00;
209 localparam ROM_READ = 2'b01;
210 localparam ROM_OKEY = 2'b10;
211 reg [1:0] rom_state;
212 reg rom_sel;
213 reg [12:0] flash_in_addr;
214
215 // AHB slave
216 always @(posedge clk) begin
217 if (!(rst_i & mtx_hreset_n)) begin
218 rom_state <= ROM_IDLE;
219 rom_sel <= 1'b0;
220 end else begin
221 case (rom_state)
222 ROM_IDLE: begin
223 if (targflash0_hsel & targflash0_htrans[1]) begin // NONSEQ/SEQ transfer
224 rom_state <= ROM_READ;
225 flash_in_addr <= rom_addr;
226 rom_sel <= 1'b1;
227 end
228 end
229 ROM_READ: begin
230 if (targflash0_hsel & targflash0_htrans[1]) begin // NONSEQ/SEQ transfer
231 rom_state <= ROM_OKEY;
232 rom_sel <= 1'b0;
233 end else begin
234 rom_state <= ROM_IDLE;
235 rom_sel <= 1'b0;
236 end
237 end
238 ROM_OKEY: begin
239 if (targflash0_hsel & targflash0_htrans[1]) begin // NONSEQ/SEQ transfer
240 rom_state <= ROM_IDLE;
241 rom_sel <= 1'b0;
242 end
243 end
244 endcase
245 end
246 end
247 assign targflash0_readyout = rom_state == ROM_IDLE;
248
249 FLASH256K rom(
250 .DOUT(rom_out[31:0]),
251 .XADR(flash_in_addr[12:6]),
252 .YADR(flash_in_addr[5:0]),
253 .XE(rst_i),
254 .YE(rst_i),
255 .SE(rom_sel),
256 .ERASE(GND),
257 .PROG(GND),
258 .NVSTR(GND),
259 .DIN({32{GND}})
260 );
261
262 // RAM 4 block of 2K
263 // Inferring will probably also work, but with primitives it is somehow easier for me.
264 SDPB ram_0 (
265 .DO({dummy_sram0_rdata_0, sram0_rdata[7:0]}),
266 .DI({{24{GND}}, sram0_wdata[7:0]}),
267 .BLKSELA({GND, GND, sram0_cs}),
268 .BLKSELB({GND, GND, sram0_cs}),
269 .ADA({sram0_addr, GND,GND,GND}),
270 .ADB({sram0_addr, GND,GND,GND}),
271 .CLKA(clk),
272 .CLKB(clk),
273 .CEA(sram0_wren[0]),
274 .CEB(!sram0_wren[0]),
275 .OCE(VCC),
276 .RESETA(GND),
277 .RESETB(!rst_i)
278 );
279 defparam ram_0.BIT_WIDTH_0=8;
280 defparam ram_0.BIT_WIDTH_1=8;
281 defparam ram_0.BLK_SEL_0=3'b001;
282 defparam ram_0.BLK_SEL_1=3'b001;
283 defparam ram_0.READ_MODE=1'b0;
284 defparam ram_0.RESET_MODE="SYNC";
285
286 SDPB ram_1 (
287 .DO({dummy_sram0_rdata_1, sram0_rdata[15:8]}),
288 .DI({{24{GND}}, sram0_wdata[15:8]}),
289 .BLKSELA({GND, GND, sram0_cs}),
290 .BLKSELB({GND, GND, sram0_cs}),
291 .ADA({sram0_addr, GND,GND,GND}),
292 .ADB({sram0_addr, GND,GND,GND}),
293 .CLKA(clk),
294 .CLKB(clk),
295 .CEA(sram0_wren[1]),
296 .CEB(!sram0_wren[1]),
297 .OCE(VCC),
298 .RESETA(GND),
299 .RESETB(!rst_i)
300 );
301 defparam ram_1.BIT_WIDTH_0=8;
302 defparam ram_1.BIT_WIDTH_1=8;
303 defparam ram_1.BLK_SEL_0=3'b001;
304 defparam ram_1.BLK_SEL_1=3'b001;
305 defparam ram_1.READ_MODE=1'b0;
306 defparam ram_1.RESET_MODE="SYNC";
307
308 SDPB ram_2 (
309 .DO({dummy_sram0_rdata_2, sram0_rdata[23:16]}),
310 .DI({{24{GND}}, sram0_wdata[23:16]}),
311 .BLKSELA({GND, GND, sram0_cs}),
312 .BLKSELB({GND, GND, sram0_cs}),
313 .ADA({sram0_addr, GND,GND,GND}),
314 .ADB({sram0_addr, GND,GND,GND}),
315 .CLKA(clk),
316 .CLKB(clk),
317 .CEA(sram0_wren[2]),
318 .CEB(!sram0_wren[2]),
319 .OCE(VCC),
320 .RESETA(GND),
321 .RESETB(!rst_i)
322 );
323 defparam ram_2.BIT_WIDTH_0=8;
324 defparam ram_2.BIT_WIDTH_1=8;
325 defparam ram_2.BLK_SEL_0=3'b001;
326 defparam ram_2.BLK_SEL_1=3'b001;
327 defparam ram_2.READ_MODE=1'b0;
328 defparam ram_2.RESET_MODE="SYNC";
329
330 SDPB ram_3 (
331 .DO({dummy_sram0_rdata_3, sram0_rdata[31:24]}),
332 .DI({{24{GND}}, sram0_wdata[31:24]}),
333 .BLKSELA({GND, GND, sram0_cs}),
334 .BLKSELB({GND, GND, sram0_cs}),
335 .ADA({sram0_addr, GND,GND,GND}),
336 .ADB({sram0_addr, GND,GND,GND}),
337 .CLKA(clk),
338 .CLKB(clk),
339 .CEA(sram0_wren[3]),
340 .CEB(!sram0_wren[3]),
341 .OCE(VCC),
342 .RESETA(GND),
343 .RESETB(!rst_i)
344 );
345 defparam ram_3.BIT_WIDTH_0=8;
346 defparam ram_3.BIT_WIDTH_1=8;
347 defparam ram_3.BLK_SEL_0=3'b001;
348 defparam ram_3.BLK_SEL_1=3'b001;
349 defparam ram_3.READ_MODE=1'b0;
350 defparam ram_3.RESET_MODE="SYNC";
351
352 endmodule