]> git.ozlabs.org Git - minimigmac.git/blob - fpga/cpu_intf.v
Initial commit
[minimigmac.git] / fpga / cpu_intf.v
1 `timescale 1ns / 100ps
2
3 /*
4  * m68k CPU to internal bus interface
5  * 
6  * Devices & Memory bus protocol:
7  *
8  * We implement a two phase bus protocol to the outside world
9  * 
10  * - Address phase (phase 0) corresponds to CPU S4 & S5
11  *
12  * - Data phase (phase 1) corrsponds to CPU S6 & S7
13  * 
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.
18  * 
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.
23  * 
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.
28  * 
29  *  Signal      Phase 0 clk             Phase 1 clk             Phase 1
30  * 
31  *  bus_cs_xxx  yes                     yes                     no (*)
32  *  bus_we      yes                     yes                     yes
33  *  bus_ube/lbe no (**)                 yes                     no (**)
34  *  bus_addr    yes                     yes                     yes
35  *  bus_wdata   yes                     yes                     no (***)
36  *  bus_phase   0                       1                       0
37  * 
38  *  from device:
39  * 
40  *  bus_ack     no                      yes                     no (****)
41  *  bus_rdata   no                      yes                     n/a
42  * 
43  * (*) CS is lowered during phase 1 so that the next clock edge doesn't
44  *     get mistaken for a new phase 0 cycle.
45  * 
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
55  *      for now I believe.
56  * 
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.
63  * 
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.
71  * 
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.
78  */
79
80 /* Backbus interface registers
81  *
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.
86  * 
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
90  * strobe
91  */
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
108
109 module cpu_intf(input                   sysclk,
110                 input                   reset,
111
112                 /* m68k CPU pins */
113                 inout [15:0]            cpu_data,
114                 input [23:1]            cpu_addr,
115                 input                   _cpu_as,
116                 input                   _cpu_uds,
117                 input                   _cpu_lds,
118                 input                   cpu_r_w,
119                 output                  _cpu_dtack,
120                 /*inout                 _cpu_reset,*/
121
122                 /* Backbus interface */
123                 input [5:0]             bb_addr,
124                 input [7:0]             bb_wdata,
125                 output[7:0]             bb_rdata,
126                 input                   bb_strobe,
127                 input                   bb_wr,
128                 
129                 /* Common device bus interface */
130                 output [23:1]           bus_addr,
131                 output [15:0]           bus_wdata,
132                 output                  bus_ube,
133                 output                  bus_lbe,
134                 output                  bus_we,
135                 output                  bus_phase,
136
137                 /* Per-device CS, ACK and data input */
138                 output                  bus_cs_ram,
139                 output                  bus_cs_rom,
140                 input                   bus_ack_mem,
141                 input [15:0]            bus_rdata_mem, /* RAM and ROM */
142                 output                  bus_cs_scsi,
143                 input                   bus_ack_scsi,
144                 input [7:0]             bus_rdata_scsi,
145                 output                  bus_cs_scc,
146                 input                   bus_ack_scc,
147                 input [7:0]             bus_rdata_scc,
148                 output                  bus_cs_via,
149                 input                   bus_ack_via,
150                 input [7:0]             bus_rdata_via,
151                 output                  bus_cs_iwm,
152                 input                   bus_ack_iwm,
153                 input [7:0]             bus_rdata_iwm,
154
155                 /* Global control signals */
156                 input                   rom_ovl /* from VIA */
157 );
158         
159         /* For autovector simulation since we have no _avec pin */
160         wire            cs_ivec;
161
162         /* No target for a bus request */
163         wire            cs_nack;        
164
165         /* Bus request signal, activates chip selects */
166         wire            bus_req;
167
168         /* Cumulative ack result from devices */
169         wire            bus_ack;        
170
171         /* Address data and control latches (for spy and SPI ops) */
172         reg [23:1]      addr_latch;
173         reg [15:0]      data_latch;
174         reg             _uds_latch;
175         reg             _lds_latch;
176         reg             r_w_latch;
177
178         /* SPI control register */
179         reg [7:0]       spi_ctrl;       
180
181         /* Data bus mux */
182         wire [15:0]     bus_rdata;      /* Demuxed from devices */
183
184         /* Breakpoint */
185         reg [23:1]      addr_bkpt;
186         wire            addr_bphit;
187         
188         /* Signals from the SPI interface */
189         wire            spi_wreg;       
190         wire            spi_reg_addr0;
191         wire            spi_reg_addr1;
192         wire            spi_reg_addr2;
193         wire            spi_reg_data0;
194         wire            spi_reg_data1;
195         wire            spi_reg_misc;
196         wire            spi_reg_ctrl;
197         wire            spi_reg_stat;
198         wire            spi_reg_bkpt0;
199         wire            spi_reg_bkpt1;
200         wire            spi_reg_bkpt2;
201         
202         /* State machine */
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 */
210
211         /* Note on individual state bits::
212             - 7 : CPU cycle
213             - 6 : SPI cycle
214             - 5 : unused
215             - 4 : unused
216             - 3 : stopped
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
220          
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
225          */
226         reg [7:0]       state;
227         
228         /* Instanciate address decoder */       
229         addr_decode decode0(.addr(bus_addr),
230                             .req(bus_req),
231                             .rom_ovl(rom_ovl),
232                             .cs_ram(bus_cs_ram),
233                             .cs_rom(bus_cs_rom),
234                             .cs_scsi(bus_cs_scsi),
235                             .cs_scc(bus_cs_scc),
236                             .cs_iwm(bus_cs_iwm),
237                             .cs_via(bus_cs_via),
238                             .cs_ivec(cs_ivec),
239                             .cs_nack(cs_nack));
240
241         /* Generate bus address. Avoid propagating z state from the
242          * CPU to the FPGA by gating with _cpu_as
243          */
244         assign bus_addr = (!state[7]) ? addr_latch :
245                           _cpu_as ? 22'h3fffff : cpu_addr;
246
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
250          */
251         assign cpu_data = (cpu_r_w & state[1]) ? data_latch : 16'bz;
252
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);
258
259         /* Address decoder's enable signal which controls CS signals */
260         assign bus_req = state[2] & (state[6] | ~_cpu_as);
261
262         /* Bus phase output straight off the state machine */
263         assign bus_phase = state[0];
264
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] } :
272 `ifdef __IVERILOG__
273                            16'hbeef;
274 `else   
275                            16'b1111_1111_1111_1111;
276 `endif  
277
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
281          */
282         assign bus_ack = bus_ack_mem |
283                          bus_ack_scsi |
284                          bus_ack_scc |
285                          bus_ack_via |
286                          bus_ack_iwm |
287                          cs_ivec | cs_nack;
288 `ifdef __IVERILOG__
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]);
293                 end
294         end
295 `endif
296
297
298         /* Breakpoint check */
299         assign addr_bphit = (cpu_addr == addr_bkpt) &
300                             spi_ctrl[`CPUINTF_CTRL_BKEN_BIT];
301                             
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.
308          */
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
313                 if (reset) begin
314                         state <= bi_state_stop;
315                 end else
316                   case(state)
317                           bi_state_idle: begin
318                                   /* SPI takeover */
319                                   if (spi_ctrl[`CPUINTF_CTRL_STOP_BIT])
320                                           state <=  bi_state_stop;
321
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
326                                    */
327                                   else if (!_cpu_as) begin
328                                           if (addr_bphit)
329                                             state <= bi_state_stop;
330                                           else
331                                             state <= bi_state_cpu0;
332                                   end                     
333
334                                   /* If the CPU is idle and we are stepping,
335                                    * we stop again
336                                    */
337                                   else if (spi_ctrl[`CPUINTF_CTRL_STEP_BIT])
338                                     state <= bi_state_stop;
339                           end
340                           bi_state_cpu0: begin
341                                   /* We got an ack from the device, move to
342                                    * phase 1
343                                    */
344                                   if (bus_ack)
345                                     state <= bi_state_cpu1;
346
347                                   /* No ack, another try at SPI takeover */
348                                   else if (spi_ctrl[`CPUINTF_CTRL_STOP_BIT])
349                                     state <=  bi_state_stop;
350                           end
351                           bi_state_cpu1: begin
352                                   /* If stepping, stop now */
353                                   if (spi_ctrl[`CPUINTF_CTRL_STEP_BIT])
354                                     state <=  bi_state_stop;
355
356                                   /* Back to idle */
357                                   else
358                                     state <= bi_state_idle;
359                           end
360                           bi_state_spip: begin
361                                   state <= bi_state_spi0;
362                           end
363                           bi_state_spi0: begin
364                                   if (bus_ack)
365                                     state <= bi_state_spi1;
366                           end
367                           bi_state_spi1: begin
368                                   state <= bi_state_stop;
369                           end
370                           bi_state_stop: begin
371                                   /* Restart CPU */
372                                   if (spi_ctrl[`CPUINTF_CTRL_RUN_BIT])
373                                     state <= bi_state_idle;
374
375                                   /* Start a simulated cycle */
376                                   else if (spi_ctrl[`CPUINTF_CTRL_CYCLE_BIT])
377                                     state <= bi_state_spip;
378                           end
379                   endcase
380         end
381
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;
395
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
399          */
400
401         /* Address latch */
402         always@(posedge sysclk or posedge reset) begin
403                 if (reset) begin
404                         addr_latch <= 0;
405                 end else begin
406                         /* Latch the CPU address for any CPU cycle in phase 0 */
407                         if (state[7]) begin
408                                 if (state[0])
409                                   addr_latch <= cpu_addr;
410                         end else if (spi_wreg) begin
411                                 /* Or fill from SPI */
412                                 if (spi_reg_addr0)
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];
418                         end                     
419                 end
420         end
421
422         /* Data latch. The CPU always feed from this, we latch the data
423          * from the device at phase 1 clock edge
424          */
425         always@(posedge sysclk or posedge reset) begin
426                 if (reset) begin
427                         data_latch <= 0;
428                 end else begin
429                         /* Latch the bus data in phase 1 clock. Don't
430                          * bother with ack, we'll latch a fresh value
431                          * later if not set
432                          */
433                         if (state[0]) begin
434                                 if (bus_we)
435                                   data_latch <= bus_wdata;
436                                 else
437                                   data_latch <= bus_rdata;
438                         /* Else handle write from SPI */
439                         end else if (spi_wreg) begin
440                                 if (spi_reg_data0)
441                                   data_latch[15:8] <= bb_wdata;
442                                 else if (spi_reg_data1)
443                                   data_latch[7:0] <= bb_wdata;
444                         end                     
445                 end
446         end
447
448         /* Misc latches (uds/lds/rw) */
449         always@(posedge sysclk or posedge reset) begin
450                 if (reset) begin
451                         _uds_latch <= 0;                        
452                         _lds_latch <= 0;
453                         r_w_latch <= 0;                 
454                 end else begin
455                         /* Latch the CPU misc for any CPU cycle in phase 0 */
456                         if (state[7]) begin
457                                 if (state[0]) begin
458                                         _uds_latch <= _cpu_uds;
459                                         _lds_latch <= _cpu_lds;
460                                         r_w_latch <= cpu_r_w;
461                                 end                             
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];
468                                 end
469                         end
470                 end
471         end
472
473         /* SPI control register */
474         always@(posedge sysclk or posedge reset) begin
475                 if (reset) begin
476                         spi_ctrl <= 0;
477                 end else begin
478                         /* Stop bit cleared whenever we are stopped */
479                         if (state[3])
480                           spi_ctrl[`CPUINTF_CTRL_STOP_BIT] <= 0;
481                         /* Run bit cleared if we are in any CPU cycle */
482                         if (state[7])
483                           spi_ctrl[`CPUINTF_CTRL_RUN_BIT] <= 0;
484                         /* Cycle bit cleared when we are in an SPI cycle */
485                         if (state[6])                    
486                           spi_ctrl[`CPUINTF_CTRL_CYCLE_BIT] <= 0;
487
488                         /* Now write logic from SPI. All bits are OR except
489                          * for step and bken that is sticky
490                          */
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];
494                         end
495                 end             
496         end     
497
498         /* Breakpoint register */
499         always@(posedge sysclk or posedge reset) begin
500                 if (reset) begin
501                         addr_bkpt <= 0;
502                 end else begin
503                         if (spi_wreg) begin
504                                 /* Or fill from SPI */
505                                 if (spi_reg_bkpt0)
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];
511                         end                     
512                 end
513         end
514         
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,
522                                             r_w_latch } :
523                           spi_reg_ctrl  ? spi_ctrl :
524                           spi_reg_stat  ? state :
525                           0;
526 endmodule