4 * m68k CPU to internal bus interface
6 * Devices & Memory bus protocol:
8 * We implement a two phase bus protocol to the outside world
10 * - Address phase (phase 0) corresponds to CPU S4 & S5
12 * - Data phase (phase 1) corrsponds to CPU S6 & S7
14 * Devices shall typically act on the clock edge at the begining of each
15 * phase, tho phase 0 can generally be ignored for simple devices. Phase 0
16 * shall not cause any action to be committed, but the phase 0 edge is
17 * when we sample the "ack" signal which allows transition to phase 1.
19 * Thus phase 0 is only here to allow devices to delay/hold the bus, which
20 * is used by SCSI to make blind transfer reliable and our memory interface
21 * because I'm an idiot and didn't properly interleave CPU and video. I will
22 * probably use it from the SWIM interface too when transfering raw data.
24 * Here are the signal that are valid at phase 0 and phase 1 clock
25 * edges. I also noted whether those signals remain valid in the clock
26 * cycle following phase 1 clock edge (ie, during phase 1 / S6/S7) since
27 * some devices such as memory rely on these.
29 * Signal Phase 0 clk Phase 1 clk Phase 1
31 * bus_cs_xxx yes yes no (*)
33 * bus_ube/lbe no (**) yes no (**)
34 * bus_addr yes yes yes
35 * bus_wdata yes yes no (***)
40 * bus_ack no yes no (****)
41 * bus_rdata no yes n/a
43 * (*) CS is lowered during phase 1 so that the next clock edge doesn't
44 * get mistaken for a new phase 0 cycle.
46 * (**) For a CPU initiated cycle, bus_ube/lbe are directly sourced from
47 * the m68k's _UDS and _LDS lines, which for a write are only asserted
48 * in S4, so right -after- phase 0 clock edge, and are released in S7
49 * right after the negative edge following phase 1 clock edge. The
50 * memory interface routes those signal directly to the SRAM, the
51 * timing should be just right (with something like 15ns margin), but
52 * we might want to establish constraints in the FPGA design to ensure
53 * that. Alternatively, we could latch them on the clock negative edge
54 * and maintain them all the way during phase 1 but that isnt necessary
57 * (***) For a CPU initiate cycle, bus_wdata is sources directly from the
58 * m68k's data bus, which becomes invalid in S7, after the negedge
59 * of the clock. There's a 15ns valid time between UDS/LDS going
60 * up and data becoming invalid on a 16Mhz CPU, but here too we
61 * might want to add constraints to the synthesis tool to guarantee
62 * we stay within the margin.
64 * (****) The CPU DTACK is set from bus_ack and sampled asynchronously
65 * during phase 0 on the negative edge of the clock (end of S4),
66 * and by our CPU interface logic on phase 1 clk to validate
67 * actually going into phase 1 (else we remain in phase 0).
68 * Thus dumb devices can just wire CS to ACK and devices that
69 * want to hold the bus can do so by delaying ACK keeping the
70 * interface in phase 0.
72 * WARNING: While in phase 0, the CPU interface might decide to switch to
73 * another master (the SPI interface is the only one for now),
74 * so devices shouldn't commit/latch anything with the assumption
75 * that a subsequent phase 0 will provide the same cs/address/...
76 * signals. However, if a device does ack, then going into phase 1
77 * on the next cycle is guaranteed.
80 /* Backbus interface registers
82 * WARNING: Writing to the ADDR, DATA and MISC latches should only be
83 * done while the bus is in stopped state. If an attempt is made to
84 * write a latch that the CPU tries to update in the same cycle, the CPU
85 * wins and the SPI write is lost.
87 * Similarily, reading from those latches may result in inconsistent
88 * values if done while the CPU interface is runnig as the SPI
89 * interface will latch the data at some "random" time after the
92 `define CPUINTF_REG_ADDR0 0
93 `define CPUINTF_REG_ADDR1 1
94 `define CPUINTF_REG_ADDR2 2
95 `define CPUINTF_REG_DATA0 3
96 `define CPUINTF_REG_DATA1 4
97 `define CPUINTF_REG_MISC 5 /* bit 2: _UDS, bit 1: _LDS, bit 0: R_W */
98 `define CPUINTF_REG_CTRL 6
99 `define CPUINTF_CTRL_STOP_BIT 0 /* Stop CPU */
100 `define CPUINTF_CTRL_RUN_BIT 1 /* Run CPU */
101 `define CPUINTF_CTRL_CYCLE_BIT 2 /* Issue an SPI generated bus cycle */
102 `define CPUINTF_CTRL_BKEN_BIT 6 /* Sticky, set with RUN to enable bkpt */
103 `define CPUINTF_CTRL_STEP_BIT 7 /* Sticky, set with RUN to single step */
104 `define CPUINTF_REG_STAT 7 /* bus state machine */
105 `define CPUINTF_REG_BKPT0 8
106 `define CPUINTF_REG_BKPT1 9
107 `define CPUINTF_REG_BKPT2 10
109 module cpu_intf(input sysclk,
113 inout [15:0] cpu_data,
114 input [23:1] cpu_addr,
120 /*inout _cpu_reset,*/
122 /* Backbus interface */
124 input [7:0] bb_wdata,
125 output[7:0] bb_rdata,
129 /* Common device bus interface */
130 output [23:1] bus_addr,
131 output [15:0] bus_wdata,
137 /* Per-device CS, ACK and data input */
141 input [15:0] bus_rdata_mem, /* RAM and ROM */
144 input [7:0] bus_rdata_scsi,
147 input [7:0] bus_rdata_scc,
150 input [7:0] bus_rdata_via,
153 input [7:0] bus_rdata_iwm,
155 /* Global control signals */
156 input rom_ovl /* from VIA */
159 /* For autovector simulation since we have no _avec pin */
162 /* No target for a bus request */
165 /* Bus request signal, activates chip selects */
168 /* Cumulative ack result from devices */
171 /* Address data and control latches (for spy and SPI ops) */
172 reg [23:1] addr_latch;
173 reg [15:0] data_latch;
178 /* SPI control register */
182 wire [15:0] bus_rdata; /* Demuxed from devices */
185 reg [23:1] addr_bkpt;
188 /* Signals from the SPI interface */
203 localparam bi_state_idle = 8'h84; /* idle, addr & r_w from CPU */
204 localparam bi_state_cpu0 = 8'h85; /* cpu S4,S5 */
205 localparam bi_state_cpu1 = 8'h82; /* cpu S6,S7 */
206 localparam bi_state_spip = 8'h44; /* spi control, prep addr & r_w */
207 localparam bi_state_spi0 = 8'h45; /* spi control, pseudo S4,S5 */
208 localparam bi_state_spi1 = 8'h42; /* spi control, psuedo S6,S7 */
209 localparam bi_state_stop = 8'h48; /* spi control, stopped */
211 /* Note on individual state bits::
217 - 2 : CS set based on _AS (or 1 for SPI)
218 - 1 : allow cpu data input and force dtack
219 - 0 : bus phase at next clock
221 You may notice that we drop CS on CPU phase S6,S7. We must do
222 that for SPI as we must not have more than 2 phases with CS set
223 in one cycle, but we could leave bit 2 set for the CPU in that
224 stage as _AS is supposed to go down... doesn't matter much tho
228 /* Instanciate address decoder */
229 addr_decode decode0(.addr(bus_addr),
234 .cs_scsi(bus_cs_scsi),
241 /* Generate bus address. Avoid propagating z state from the
242 * CPU to the FPGA by gating with _cpu_as
244 assign bus_addr = (!state[7]) ? addr_latch :
245 _cpu_as ? 22'h3fffff : cpu_addr;
247 /* Data bus to CPU is hi-z unless CPU has r_w up -and- we are
248 * in phase 1. Should keep us safe from shorts. We always feed
249 * the CPU from the data latch
251 assign cpu_data = (cpu_r_w & state[1]) ? data_latch : 16'bz;
253 /* Data bus and control signals to devices */
254 assign bus_wdata = state[7] ? cpu_data : data_latch;
255 assign bus_ube = ~(state[7] ? _cpu_uds : _uds_latch);
256 assign bus_lbe = ~(state[7] ? _cpu_lds : _lds_latch);
257 assign bus_we = ~(state[7] ? cpu_r_w : r_w_latch);
259 /* Address decoder's enable signal which controls CS signals */
260 assign bus_req = state[2] & (state[6] | ~_cpu_as);
262 /* Bus phase output straight off the state machine */
263 assign bus_phase = state[0];
265 /* Data from devices demux (and generate autovector value) */
266 assign bus_rdata = (bus_cs_ram | bus_cs_rom) ? bus_rdata_mem :
267 bus_cs_scsi ? { bus_rdata_scsi, 8'h0 } :
268 bus_cs_scc ? { bus_rdata_scc, 8'h0 } :
269 bus_cs_via ? { bus_rdata_via, 8'h0 } :
270 bus_cs_iwm ? { 8'h0, bus_rdata_iwm } :
271 cs_ivec ? { 11'b0, 2'b11, bus_addr[3:1] } :
275 16'b1111_1111_1111_1111;
278 /* We OR all acks, devices shall not set ack if their CS isn't set
279 * additionally, we ack for ivec and nack signals from the decoder
280 * as those are handled locally
282 assign bus_ack = bus_ack_mem |
289 /* Debug stuff, sim only */
290 always@(posedge sysclk or posedge reset) begin
291 if (cs_nack && bus_we && bus_phase) begin
292 $write("%c", bus_wdata[15:8]);
298 /* Breakpoint check */
299 assign addr_bphit = (cpu_addr == addr_bkpt) &
300 spi_ctrl[`CPUINTF_CTRL_BKEN_BIT];
302 /* Generate _DTACK for CPU cycles only. Note that the doc is
303 * unlcear as to whether we must keep it asserted until
304 * _AS goes back up or whether we can take it off as soon as
305 * state S5 or or later is reached.
306 * Better safe than sorry, we keep it down all the way for now
307 * at least until I have more than my crude sim to test with.
309 assign _cpu_dtack = _cpu_as | ~state[7] | /* CPU cycle and AS asserted */
310 (~state[1] & ~(state[0] & bus_ack));
311 /* Bus state machine */
312 always@(posedge sysclk or posedge reset) begin
314 state <= bi_state_stop;
319 if (spi_ctrl[`CPUINTF_CTRL_STOP_BIT])
320 state <= bi_state_stop;
322 /* If the CPU is trying to start a cycle,
323 * move up. CS will already have been generated
324 * asynchronously on the bus interface. We also
325 * detect breakpoints there
327 else if (!_cpu_as) begin
329 state <= bi_state_stop;
331 state <= bi_state_cpu0;
334 /* If the CPU is idle and we are stepping,
337 else if (spi_ctrl[`CPUINTF_CTRL_STEP_BIT])
338 state <= bi_state_stop;
341 /* We got an ack from the device, move to
345 state <= bi_state_cpu1;
347 /* No ack, another try at SPI takeover */
348 else if (spi_ctrl[`CPUINTF_CTRL_STOP_BIT])
349 state <= bi_state_stop;
352 /* If stepping, stop now */
353 if (spi_ctrl[`CPUINTF_CTRL_STEP_BIT])
354 state <= bi_state_stop;
358 state <= bi_state_idle;
361 state <= bi_state_spi0;
365 state <= bi_state_spi1;
368 state <= bi_state_stop;
372 if (spi_ctrl[`CPUINTF_CTRL_RUN_BIT])
373 state <= bi_state_idle;
375 /* Start a simulated cycle */
376 else if (spi_ctrl[`CPUINTF_CTRL_CYCLE_BIT])
377 state <= bi_state_spip;
382 /* Some SPI backbus interface combo decode logic */
383 assign spi_wreg = bb_strobe && bb_wr;
384 assign spi_reg_addr0 = bb_addr[3:0] == `CPUINTF_REG_ADDR0;
385 assign spi_reg_addr1 = bb_addr[3:0] == `CPUINTF_REG_ADDR1;
386 assign spi_reg_addr2 = bb_addr[3:0] == `CPUINTF_REG_ADDR2;
387 assign spi_reg_data0 = bb_addr[3:0] == `CPUINTF_REG_DATA0;
388 assign spi_reg_data1 = bb_addr[3:0] == `CPUINTF_REG_DATA1;
389 assign spi_reg_misc = bb_addr[3:0] == `CPUINTF_REG_MISC;
390 assign spi_reg_ctrl = bb_addr[3:0] == `CPUINTF_REG_CTRL;
391 assign spi_reg_stat = bb_addr[3:0] == `CPUINTF_REG_STAT;
392 assign spi_reg_bkpt0 = bb_addr[3:0] == `CPUINTF_REG_BKPT0;
393 assign spi_reg_bkpt1 = bb_addr[3:0] == `CPUINTF_REG_BKPT1;
394 assign spi_reg_bkpt2 = bb_addr[3:0] == `CPUINTF_REG_BKPT2;
396 /* Latches used by SPI interface to latch address/data
397 * when generating cycles. When under CPU control, they
398 * latch the last CPU cycle for single step mode
402 always@(posedge sysclk or posedge reset) begin
406 /* Latch the CPU address for any CPU cycle in phase 0 */
409 addr_latch <= cpu_addr;
410 end else if (spi_wreg) begin
411 /* Or fill from SPI */
413 addr_latch[23:16] <= bb_wdata;
414 else if (spi_reg_addr1)
415 addr_latch[15:8] <= bb_wdata;
416 else if (spi_reg_addr2)
417 addr_latch[7:1] <= bb_wdata[7:1];
422 /* Data latch. The CPU always feed from this, we latch the data
423 * from the device at phase 1 clock edge
425 always@(posedge sysclk or posedge reset) begin
429 /* Latch the bus data in phase 1 clock. Don't
430 * bother with ack, we'll latch a fresh value
435 data_latch <= bus_wdata;
437 data_latch <= bus_rdata;
438 /* Else handle write from SPI */
439 end else if (spi_wreg) begin
441 data_latch[15:8] <= bb_wdata;
442 else if (spi_reg_data1)
443 data_latch[7:0] <= bb_wdata;
448 /* Misc latches (uds/lds/rw) */
449 always@(posedge sysclk or posedge reset) begin
455 /* Latch the CPU misc for any CPU cycle in phase 0 */
458 _uds_latch <= _cpu_uds;
459 _lds_latch <= _cpu_lds;
460 r_w_latch <= cpu_r_w;
462 end else if (spi_wreg) begin
463 /* Or fill from SPI */
464 if (spi_reg_misc) begin
465 _uds_latch <= bb_wdata[2];
466 _lds_latch <= bb_wdata[1];
467 r_w_latch <= bb_wdata[0];
473 /* SPI control register */
474 always@(posedge sysclk or posedge reset) begin
478 /* Stop bit cleared whenever we are stopped */
480 spi_ctrl[`CPUINTF_CTRL_STOP_BIT] <= 0;
481 /* Run bit cleared if we are in any CPU cycle */
483 spi_ctrl[`CPUINTF_CTRL_RUN_BIT] <= 0;
484 /* Cycle bit cleared when we are in an SPI cycle */
486 spi_ctrl[`CPUINTF_CTRL_CYCLE_BIT] <= 0;
488 /* Now write logic from SPI. All bits are OR except
489 * for step and bken that is sticky
491 if (spi_wreg && spi_reg_ctrl) begin
492 spi_ctrl[2:0] <= spi_ctrl[2:0] | bb_wdata[2:0];
493 spi_ctrl[7:6] <= bb_wdata[7:6];
498 /* Breakpoint register */
499 always@(posedge sysclk or posedge reset) begin
504 /* Or fill from SPI */
506 addr_bkpt[23:16] <= bb_wdata;
507 else if (spi_reg_bkpt1)
508 addr_bkpt[15:8] <= bb_wdata;
509 else if (spi_reg_bkpt2)
510 addr_bkpt[7:1] <= bb_wdata[7:1];
515 /* SPI backbus register read logic */
516 assign bb_rdata = spi_reg_addr0 ? addr_latch[23:16] :
517 spi_reg_addr1 ? addr_latch[15:8] :
518 spi_reg_addr2 ? { addr_latch[7:1], 1'b0 } :
519 spi_reg_data0 ? data_latch[15:8] :
520 spi_reg_data1 ? data_latch[7:0] :
521 spi_reg_misc ? { 5'b0, _uds_latch, _lds_latch,
523 spi_reg_ctrl ? spi_ctrl :
524 spi_reg_stat ? state :