1 /* dv-m68hc11spi.c -- Simulation of the 68HC11 SPI
2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
3 Written by Stephane Carrez (stcarrez@nerim.fr)
4 (From a driver model Contributed by Cygnus Solutions.)
6 This file is part of the program GDB, the GNU debugger.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* This must come before any other includes. */
28 #include "dv-sockser.h"
29 #include "sim-assert.h"
34 m68hc11spi - m68hc11 SPI interface
39 Implements the m68hc11 Synchronous Serial Peripheral Interface
40 described in the m68hc11 user guide (Chapter 8 in pink book).
41 The SPI I/O controller is directly connected to the CPU
42 interrupt. The simulator implements:
46 - Write collision detection
58 Reset port. This port is only used to simulate a reset of the SPI
59 I/O controller. It should be connected to the RESET output of the cpu.
73 static const struct hw_port_descriptor m68hc11spi_ports
[] =
75 { "reset", RESET_PORT
, 0, input_port
, },
83 /* Information about next character to be transmited. */
84 unsigned char tx_char
;
88 unsigned char rx_char
;
89 unsigned char rx_clear_scsr
;
90 unsigned char clk_pin
;
92 /* SPI clock rate (twice the real clock). */
95 /* Periodic SPI event. */
96 struct hw_event
* spi_event
;
101 /* Finish off the partially created hw device. Attach our local
102 callbacks. Wire up our port names etc */
104 static hw_io_read_buffer_method m68hc11spi_io_read_buffer
;
105 static hw_io_write_buffer_method m68hc11spi_io_write_buffer
;
106 static hw_port_event_method m68hc11spi_port_event
;
107 static hw_ioctl_method m68hc11spi_ioctl
;
109 #define M6811_SPI_FIRST_REG (M6811_SPCR)
110 #define M6811_SPI_LAST_REG (M6811_SPDR)
114 attach_m68hc11spi_regs (struct hw
*me
,
115 struct m68hc11spi
*controller
)
117 hw_attach_address (hw_parent (me
), M6811_IO_LEVEL
, io_map
,
119 M6811_SPI_LAST_REG
- M6811_SPI_FIRST_REG
+ 1,
124 m68hc11spi_finish (struct hw
*me
)
126 struct m68hc11spi
*controller
;
128 controller
= HW_ZALLOC (me
, struct m68hc11spi
);
129 set_hw_data (me
, controller
);
130 set_hw_io_read_buffer (me
, m68hc11spi_io_read_buffer
);
131 set_hw_io_write_buffer (me
, m68hc11spi_io_write_buffer
);
132 set_hw_ports (me
, m68hc11spi_ports
);
133 set_hw_port_event (me
, m68hc11spi_port_event
);
135 set_hw_ioctl (me
, m68hc11spi_ioctl
);
137 me
->to_ioctl
= m68hc11spi_ioctl
;
140 /* Attach ourself to our parent bus. */
141 attach_m68hc11spi_regs (me
, controller
);
143 /* Initialize to reset state. */
144 controller
->spi_event
= NULL
;
145 controller
->rx_clear_scsr
= 0;
150 /* An event arrives on an interrupt port */
153 m68hc11spi_port_event (struct hw
*me
,
160 struct m68hc11spi
*controller
;
164 controller
= hw_data (me
);
166 cpu
= STATE_CPU (sd
, 0);
171 HW_TRACE ((me
, "SPI reset"));
173 /* Reset the state of SPI registers. */
174 controller
->rx_clear_scsr
= 0;
175 if (controller
->spi_event
)
177 hw_event_queue_deschedule (me
, controller
->spi_event
);
178 controller
->spi_event
= 0;
182 m68hc11spi_io_write_buffer (me
, &val
, io_map
,
183 (unsigned_word
) M6811_SPCR
, 1);
188 hw_abort (me
, "Event on unknown port %d", my_port
);
194 set_bit_port (struct hw
*me
, sim_cpu
*cpu
, int port
, int mask
, int value
)
199 val
= cpu
->ios
[port
] | mask
;
201 val
= cpu
->ios
[port
] & ~mask
;
203 /* Set the new value and post an event to inform other devices
204 that pin 'port' changed. */
205 m68hc11cpu_set_port (me
, cpu
, port
, val
);
209 /* When a character is sent/received by the SPI, the PD2..PD5 line
210 are driven by the following signals:
213 -----+---------+--------+---/-+-------
215 MISO +---------+--------+---/-+
217 CLK _______/ \____/ \__ CPOL=0, CPHA=0
219 \____/ \___/ CPOL=1, CPHA=0
221 __/ \____/ \___/ CPOL=0, CPHA=1
223 \____/ \____/ \__ CPOL=1, CPHA=1
226 \__________________________//___/
235 #define SPI_START_BYTE 0
236 #define SPI_START_BIT 1
237 #define SPI_MIDDLE_BIT 2
240 m68hc11spi_clock (struct hw
*me
, void *data
)
243 struct m68hc11spi
* controller
;
245 int check_interrupt
= 0;
247 controller
= hw_data (me
);
249 cpu
= STATE_CPU (sd
, 0);
251 /* Cleanup current event. */
252 if (controller
->spi_event
)
254 hw_event_queue_deschedule (me
, controller
->spi_event
);
255 controller
->spi_event
= 0;
258 /* Change a bit of data at each two SPI event. */
259 if (controller
->mode
== SPI_START_BIT
)
261 /* Reflect the bit value on bit 2 of port D. */
262 set_bit_port (me
, cpu
, M6811_PORTD
, (1 << 2),
263 (controller
->tx_char
& (1 << controller
->tx_bit
)));
264 controller
->tx_bit
--;
265 controller
->mode
= SPI_MIDDLE_BIT
;
267 else if (controller
->mode
== SPI_MIDDLE_BIT
)
269 controller
->mode
= SPI_START_BIT
;
272 if (controller
->mode
== SPI_START_BYTE
)
274 /* Start a new SPI transfer. */
276 /* TBD: clear SS output. */
277 controller
->mode
= SPI_START_BIT
;
278 controller
->tx_bit
= 7;
279 set_bit_port (me
, cpu
, M6811_PORTD
, (1 << 4), ~controller
->clk_pin
);
283 /* Change the SPI clock at each event on bit 4 of port D. */
284 controller
->clk_pin
= ~controller
->clk_pin
;
285 set_bit_port (me
, cpu
, M6811_PORTD
, (1 << 4), controller
->clk_pin
);
288 /* Transmit is now complete for this byte. */
289 if (controller
->mode
== SPI_START_BIT
&& controller
->tx_bit
< 0)
291 controller
->rx_clear_scsr
= 0;
292 cpu
->ios
[M6811_SPSR
] |= M6811_SPIF
;
293 if (cpu
->ios
[M6811_SPCR
] & M6811_SPIE
)
298 controller
->spi_event
= hw_event_queue_schedule (me
, controller
->clock
,
304 interrupts_update_pending (&cpu
->cpu_interrupts
);
307 /* Flags of the SPCR register. */
308 io_reg_desc spcr_desc
[] = {
309 { M6811_SPIE
, "SPIE ", "Serial Peripheral Interrupt Enable" },
310 { M6811_SPE
, "SPE ", "Serial Peripheral System Enable" },
311 { M6811_DWOM
, "DWOM ", "Port D Wire-OR mode option" },
312 { M6811_MSTR
, "MSTR ", "Master Mode Select" },
313 { M6811_CPOL
, "CPOL ", "Clock Polarity" },
314 { M6811_CPHA
, "CPHA ", "Clock Phase" },
315 { M6811_SPR1
, "SPR1 ", "SPI Clock Rate Select" },
316 { M6811_SPR0
, "SPR0 ", "SPI Clock Rate Select" },
321 /* Flags of the SPSR register. */
322 io_reg_desc spsr_desc
[] = {
323 { M6811_SPIF
, "SPIF ", "SPI Transfer Complete flag" },
324 { M6811_WCOL
, "WCOL ", "Write Collision" },
325 { M6811_MODF
, "MODF ", "Mode Fault" },
330 m68hc11spi_info (struct hw
*me
)
335 struct m68hc11spi
*controller
;
339 cpu
= STATE_CPU (sd
, 0);
340 controller
= hw_data (me
);
342 sim_io_printf (sd
, "M68HC11 SPI:\n");
344 base
= cpu_get_io_base (cpu
);
346 val
= cpu
->ios
[M6811_SPCR
];
347 print_io_byte (sd
, "SPCR", spcr_desc
, val
, base
+ M6811_SPCR
);
348 sim_io_printf (sd
, "\n");
350 val
= cpu
->ios
[M6811_SPSR
];
351 print_io_byte (sd
, "SPSR", spsr_desc
, val
, base
+ M6811_SPSR
);
352 sim_io_printf (sd
, "\n");
354 if (controller
->spi_event
)
358 sim_io_printf (sd
, " SPI has %d bits to send\n",
359 controller
->tx_bit
+ 1);
360 t
= hw_event_remain_time (me
, controller
->spi_event
);
361 sim_io_printf (sd
, " SPI current bit-cycle finished in %s\n",
362 cycle_to_string (cpu
, t
, PRINT_TIME
| PRINT_CYCLE
));
364 t
+= (controller
->tx_bit
+ 1) * 2 * controller
->clock
;
365 sim_io_printf (sd
, " SPI operation finished in %s\n",
366 cycle_to_string (cpu
, t
, PRINT_TIME
| PRINT_CYCLE
));
371 m68hc11spi_ioctl (struct hw
*me
,
372 hw_ioctl_request request
,
375 m68hc11spi_info (me
);
379 /* generic read/write */
382 m68hc11spi_io_read_buffer (struct hw
*me
,
389 struct m68hc11spi
*controller
;
393 HW_TRACE ((me
, "read 0x%08lx %d", (long) base
, (int) nr_bytes
));
396 cpu
= STATE_CPU (sd
, 0);
397 controller
= hw_data (me
);
402 controller
->rx_clear_scsr
= cpu
->ios
[M6811_SCSR
]
403 & (M6811_SPIF
| M6811_WCOL
| M6811_MODF
);
406 val
= cpu
->ios
[base
];
410 if (controller
->rx_clear_scsr
)
412 cpu
->ios
[M6811_SPSR
] &= ~controller
->rx_clear_scsr
;
413 controller
->rx_clear_scsr
= 0;
414 interrupts_update_pending (&cpu
->cpu_interrupts
);
416 val
= controller
->rx_char
;
422 *((unsigned8
*) dest
) = val
;
427 m68hc11spi_io_write_buffer (struct hw
*me
,
434 struct m68hc11spi
*controller
;
438 HW_TRACE ((me
, "write 0x%08lx %d", (long) base
, (int) nr_bytes
));
441 cpu
= STATE_CPU (sd
, 0);
442 controller
= hw_data (me
);
444 val
= *((const unsigned8
*) source
);
448 cpu
->ios
[M6811_SPCR
] = val
;
450 /* The SPI clock rate is 2, 4, 16, 32 of the internal CPU clock.
451 We have to drive the clock pin and need a 2x faster clock. */
452 switch (val
& (M6811_SPR1
| M6811_SPR0
))
455 controller
->clock
= 1;
459 controller
->clock
= 2;
463 controller
->clock
= 8;
467 controller
->clock
= 16;
471 /* Set the clock pin. */
472 if ((val
& M6811_CPOL
)
473 && (controller
->spi_event
== 0
474 || ((val
& M6811_CPHA
) && controller
->mode
== 1)))
475 controller
->clk_pin
= 1;
477 controller
->clk_pin
= 0;
479 set_bit_port (me
, cpu
, M6811_PORTD
, (1 << 4), controller
->clk_pin
);
482 /* Can't write to SPSR. */
487 if (!(cpu
->ios
[M6811_SPCR
] & M6811_SPE
))
492 if (controller
->rx_clear_scsr
)
494 cpu
->ios
[M6811_SPSR
] &= ~controller
->rx_clear_scsr
;
495 controller
->rx_clear_scsr
= 0;
496 interrupts_update_pending (&cpu
->cpu_interrupts
);
499 /* If transfer is taking place, a write to SPDR
500 generates a collision. */
501 if (controller
->spi_event
)
503 cpu
->ios
[M6811_SPSR
] |= M6811_WCOL
;
507 /* Refuse the write if there was no read of SPSR. */
510 /* Prepare to send a byte. */
511 controller
->tx_char
= val
;
512 controller
->mode
= SPI_START_BYTE
;
514 /* Toggle clock pin internal value when CPHA is 0 so that
515 it will really change in the middle of a bit. */
516 if (!(cpu
->ios
[M6811_SPCR
] & M6811_CPHA
))
517 controller
->clk_pin
= ~controller
->clk_pin
;
519 cpu
->ios
[M6811_SPDR
] = val
;
521 /* Activate transmission. */
522 m68hc11spi_clock (me
, NULL
);
532 const struct hw_descriptor dv_m68hc11spi_descriptor
[] = {
533 { "m68hc11spi", m68hc11spi_finish
},
534 { "m68hc12spi", m68hc11spi_finish
},