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