12 reg [7:0] rom [32767:0];
13 initial $readmemh("rom.hex", rom);
15 wire decode = address[15:13] == 0;
16 always @(posedge clk) begin
17 rdlatch <= rd && decode;
18 odata <= rom[address[10:0]];
20 assign data = rdlatch ? odata : 8'bzzzzzzzz;
30 reg [7:0] addrlatch = 0;
31 reg romno = 0, romnotmp = 0;
32 reg [7:0] brom0 [255:0];
33 reg [7:0] brom1 [255:0];
35 initial $readmemh("fpgaboot.hex", brom0);
36 initial $readmemh("gbboot.hex", brom1);
42 wire decode = address[15:8] == 0;
43 wire [7:0] odata = (romno == 0) ? brom0[addrlatch] : brom1[addrlatch];
44 always @(posedge clk) begin
45 rdlatch <= rd && decode;
46 addrlatch <= address[7:0];
47 if (wr && decode) romnotmp <= data[0];
48 if (rd && address == 16'h0000) romno <= romnotmp; /* Latch when the program restarts. */
50 assign data = rdlatch ? odata : 8'bzzzzzzzz;
59 reg [7:0] ram [127:0];
61 wire decode = (address >= 16'hFF80) && (address <= 16'hFFFE);
64 assign data = rdlatch ? odata : 8'bzzzzzzzz;
68 rdlatch <= rd && decode;
69 if (decode) // This has to go this way. The only way XST knows how to do
70 begin // block ram is chip select, write enable, and always
71 if (wr) // reading. "else if rd" does not cut it ...
72 ram[address[6:0]] <= data;
73 odata <= ram[address[6:0]];
83 output wire cr_nADV, cr_nCE, cr_nOE, cr_nWE, cr_CRE, cr_nLB, cr_nUB, cr_CLK,
84 output wire st_nCE, st_nRST,
85 output wire [22:0] cr_A,
88 parameter ADDR_PROGADDRH = 16'hFF60;
89 parameter ADDR_PROGADDRM = 16'hFF61;
90 parameter ADDR_PROGADDRL = 16'hFF62;
91 parameter ADDR_PROGDATA = 16'hFF63;
92 parameter ADDR_PROGFLASH = 16'hFF65;
93 parameter ADDR_MBC = 16'hFF64;
95 reg rdlatch = 0, wrlatch = 0;
96 reg [15:0] addrlatch = 0;
97 reg [7:0] datalatch = 0;
99 reg [7:0] progaddrh, progaddrm, progaddrl;
103 reg [7:0] mbc_emul = 8'b00000101; // High bit is whether we're poking flash
104 // low 7 bits are the MBC that we are emulating
106 assign cr_nADV = 0; /* Addresses are always valid! :D */
107 assign cr_nCE = ~(addrlatch != ADDR_PROGFLASH); /* The chip is enabled */
108 assign cr_nLB = 0; /* Lower byte is enabled */
109 assign cr_nUB = 0; /* Upper byte is enabled */
110 assign cr_CRE = 0; /* Data writes, not config */
111 assign cr_CLK = 0; /* Clock? I think not! */
113 assign st_nRST = 1; /* Keep the strataflash out of reset. */
114 assign st_nCE = ~(addrlatch == ADDR_PROGFLASH);
116 wire decode = (addrlatch[15:14] == 2'b00) /* extrom */ || (addrlatch[15:13] == 3'b101) /* extram */ || (addrlatch == ADDR_PROGDATA) || (addrlatch == ADDR_PROGFLASH);
118 reg [3:0] rambank = 0;
119 reg [8:0] rombank = 1;
121 assign cr_nOE = decode ? ~rdlatch : 1;
122 assign cr_nWE = (decode && ((addrlatch == ADDR_PROGDATA) || (addrlatch == ADDR_PROGFLASH) || (mbc_emul[6:0] == 0) || (addrlatch[15:13] == 3'b101))) ? ~wrlatch : 1;
124 assign cr_DQ = (~cr_nOE) ? 16'bzzzzzzzzzzzzzzzz : {8'b0, datalatch};
125 assign cr_A = (addrlatch[15:14] == 2'b00) ? /* extrom, home bank */ {9'b0,addrlatch[13:0]} :
126 (addrlatch[15:14] == 2'b01) ? /* extrom, paged bank */ {rombank, addrlatch[13:0]} :
127 (addrlatch[15:13] == 3'b101) ? /* extram */ {1'b1, 5'b0, rambank, addrlatch[12:0]} :
128 ((addrlatch == ADDR_PROGDATA) || (addrlatch == ADDR_PROGFLASH)) ? progaddr :
131 always @(posedge clk) begin
133 ADDR_PROGADDRH: if (wr) progaddrh <= data;
134 ADDR_PROGADDRM: if (wr) progaddrm <= data;
135 ADDR_PROGADDRL: if (wr) progaddrl <= data;
136 ADDR_PROGDATA: if (rd || wr) begin
137 progaddr <= {progaddrh[6:0], progaddrm[7:0], progaddrl[7:0]};
138 {progaddrh[6:0], progaddrm[7:0], progaddrl[7:0]} <= {progaddrh[6:0], progaddrm[7:0], progaddrl[7:0]} + 23'b1;
140 ADDR_PROGFLASH: if (rd || wr) begin
141 progaddr <= {progaddrh[6:0], progaddrm[7:0], progaddrl[7:0]};
142 {progaddrh[6:0], progaddrm[7:0], progaddrl[7:0]} <= {progaddrh[6:0], progaddrm[7:0], progaddrl[7:0]} + 23'b1;
151 if (mbc_emul[6:0] == 5) begin
152 if ((address[15:12] == 4'h2) && wr)
153 rombank <= {rombank[8], data};
154 else if ((address[15:12] == 4'h3) && wr)
155 rombank <= {data[0], rombank[7:0]};
156 else if ((address[15:12] == 4'h4) && wr)
157 rambank <= data[3:0];
162 addrlatch <= address;
166 assign data = (rdlatch && decode) ?
167 (addrlatch == ADDR_PROGADDRH) ? progaddrh :
168 (addrlatch == ADDR_PROGADDRM) ? progaddrm :
169 (addrlatch == ADDR_PROGADDRL) ? progaddrl :
175 input [15:0] address,
180 // synthesis attribute ram_style of ram is block
181 reg [7:0] ram [8191:0];
183 wire decode = (address >= 16'hC000) && (address <= 16'hFDFF); /* This includes echo RAM. */
186 assign data = rdlatch ? odata : 8'bzzzzzzzz;
188 always @(posedge clk)
190 rdlatch <= rd && decode;
191 if (decode) // This has to go this way. The only way XST knows how to do
192 begin // block ram is chip select, write enable, and always
193 if (wr) // reading. "else if rd" does not cut it ...
194 ram[address[12:0]] <= data;
195 odata <= ram[address[12:0]];
201 input [15:0] address,
205 input [7:0] switches,
206 output reg [7:0] ledout = 0);
208 wire decode = address == 16'hFF51;
211 assign data = rdlatch ? odata : 8'bzzzzzzzz;
213 always @(posedge clk)
215 rdlatch <= rd && decode;
218 else if (decode && wr)
224 module Dumpable(input [2:0] r, g, input [1:0] b, input hs, vs, vgaclk);
230 output reg vgaclk = 0,
234 input [7:0] switches,
236 output wire [7:0] leds,
239 output wire [3:0] digits,
240 output wire [7:0] seven,
241 output wire cr_nADV, cr_nCE, cr_nOE, cr_nWE, cr_CRE, cr_nLB, cr_nUB, cr_CLK, st_nCE, st_nRST,
242 output wire [22:0] cr_A,
248 output wire [2:0] r, g,
250 output wire soundl, soundr);
253 always #62 clk <= ~clk;
254 always #100 vgaclk <= ~vgaclk;
256 Dumpable dump(r,g,b,hs,vs,vgaclk);
263 wire [7:0] switches = 8'b0;
264 wire [3:0] buttons = 4'b0;
266 wire xtalb, clk, vgaclk, ethclk;
267 IBUFG iclkbuf(.O(xtalb), .I(xtal));
268 CPUDCM cpudcm (.CLKIN_IN(xtalb), .CLKFX_OUT(clk));
269 pixDCM pixdcm (.CLKIN_IN(xtalb), .CLKFX_OUT(vgaclk));
270 ethDCM ethdcm (.CLKIN_IN(xtalb), .CLKFX_OUT(ethclk));
271 wire [7:0] ps2buttons;
274 wire [15:0] addr [1:0];
275 wire [7:0] data [1:0];
276 wire wr [1:0], rd [1:0];
278 wire irq, tmrirq, lcdcirq, vblankirq, btnirq;
285 .bus0address(addr[0]),
289 .bus1address(addr[1]),
333 wire lcdhs, lcdvs, lcdclk;
334 wire [2:0] lcdr, lcdg;
344 .vblankirq(vblankirq),
370 assign leds = sleds | ps2buttons;
413 (state == 2'b00) ? 4'b0010 :
414 (state == 2'b01) ? 4'b0001 :
415 (state == 2'b10) ? 4'b1000 :
418 UART nouart ( /* no u */
475 .snd_data_r(soundr));