1 /* dv-m68hc11sio.c -- Simulation of the 68HC11 serial device.
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3 Written by Stephane Carrez (stcarrez@worldnet.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 2 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, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "dv-sockser.h"
28 #include "sim-assert.h"
33 m68hc11sio - m68hc11 serial I/O
38 Implements the m68hc11 serial I/O controller described in the m68hc11
39 user guide. The serial I/O controller is directly connected to the CPU
40 interrupt. The simulator implements:
49 Use dv-sockser TCP-port backend or stdio for backend. Default: stdio.
56 Reset port. This port is only used to simulate a reset of the serial
57 I/O controller. It should be connected to the RESET output of the cpu.
71 static const struct hw_port_descriptor m68hc11sio_ports
[] =
73 { "reset", RESET_PORT
, 0, input_port
, },
78 /* Serial Controller information. */
81 enum {sio_tcp
, sio_stdio
} backend
; /* backend */
83 /* Number of cpu cycles to send a bit on the wire. */
84 unsigned long baud_cycle
;
86 /* Length in bits of characters sent, this includes the
87 start/stop and parity bits. Together with baud_cycle, this
88 is used to find the number of cpu cycles to send/receive a data. */
89 unsigned int data_length
;
91 /* Information about next character to be transmited. */
92 unsigned char tx_has_char
;
93 unsigned char tx_char
;
95 unsigned char rx_char
;
96 unsigned char rx_clear_scsr
;
98 /* Periodic I/O polling. */
99 struct hw_event
* tx_poll_event
;
100 struct hw_event
* rx_poll_event
;
105 /* Finish off the partially created hw device. Attach our local
106 callbacks. Wire up our port names etc. */
108 static hw_io_read_buffer_method m68hc11sio_io_read_buffer
;
109 static hw_io_write_buffer_method m68hc11sio_io_write_buffer
;
110 static hw_port_event_method m68hc11sio_port_event
;
111 static hw_ioctl_method m68hc11sio_ioctl
;
113 #define M6811_SCI_FIRST_REG (M6811_BAUD)
114 #define M6811_SCI_LAST_REG (M6811_SCDR)
118 attach_m68hc11sio_regs (struct hw
*me
,
119 struct m68hc11sio
*controller
)
121 hw_attach_address (hw_parent (me
), 0, io_map
,
123 M6811_SCI_LAST_REG
- M6811_SCI_FIRST_REG
+ 1,
126 if (hw_find_property(me
, "backend") != NULL
)
128 const char *value
= hw_find_string_property(me
, "backend");
129 if(! strcmp(value
, "tcp"))
130 controller
->backend
= sio_tcp
;
131 else if(! strcmp(value
, "stdio"))
132 controller
->backend
= sio_stdio
;
134 hw_abort (me
, "illegal value for backend parameter `%s':"
135 "use tcp or stdio", value
);
141 m68hc11sio_finish (struct hw
*me
)
143 struct m68hc11sio
*controller
;
145 controller
= HW_ZALLOC (me
, struct m68hc11sio
);
146 me
->overlap_mode_hw
= 1;
147 set_hw_data (me
, controller
);
148 set_hw_io_read_buffer (me
, m68hc11sio_io_read_buffer
);
149 set_hw_io_write_buffer (me
, m68hc11sio_io_write_buffer
);
150 set_hw_ports (me
, m68hc11sio_ports
);
151 set_hw_port_event (me
, m68hc11sio_port_event
);
153 set_hw_ioctl (me
, m68hc11sio_ioctl
);
155 me
->to_ioctl
= m68hc11sio_ioctl
;
158 /* Preset defaults. */
159 controller
->backend
= sio_stdio
;
161 /* Attach ourself to our parent bus. */
162 attach_m68hc11sio_regs (me
, controller
);
164 /* Initialize to reset state. */
165 controller
->tx_poll_event
= NULL
;
166 controller
->rx_poll_event
= NULL
;
167 controller
->tx_char
= 0;
168 controller
->tx_has_char
= 0;
169 controller
->rx_clear_scsr
= 0;
170 controller
->rx_char
= 0;
175 /* An event arrives on an interrupt port. */
178 m68hc11sio_port_event (struct hw
*me
,
185 struct m68hc11sio
*controller
;
189 controller
= hw_data (me
);
191 cpu
= STATE_CPU (sd
, 0);
196 HW_TRACE ((me
, "SCI reset"));
198 /* Reset the state of SCI registers. */
200 m68hc11sio_io_write_buffer (me
, &val
, io_map
,
201 (unsigned_word
) M6811_BAUD
, 1);
202 m68hc11sio_io_write_buffer (me
, &val
, io_map
,
203 (unsigned_word
) M6811_SCCR1
, 1);
204 m68hc11sio_io_write_buffer (me
, &val
, io_map
,
205 (unsigned_word
) M6811_SCCR2
, 1);
207 cpu
->ios
[M6811_SCSR
] = M6811_TC
| M6811_TDRE
;
208 controller
->rx_char
= 0;
209 controller
->tx_char
= 0;
210 controller
->tx_has_char
= 0;
211 controller
->rx_clear_scsr
= 0;
212 if (controller
->rx_poll_event
)
214 hw_event_queue_deschedule (me
, controller
->rx_poll_event
);
215 controller
->rx_poll_event
= 0;
217 if (controller
->tx_poll_event
)
219 hw_event_queue_deschedule (me
, controller
->tx_poll_event
);
220 controller
->tx_poll_event
= 0;
223 /* In bootstrap mode, initialize the SCI to 1200 bauds to
224 simulate some initial setup by the internal rom. */
225 if (((cpu
->ios
[M6811_HPRIO
]) & (M6811_SMOD
| M6811_MDA
)) == M6811_SMOD
)
227 unsigned char val
= 0x33;
229 m68hc11sio_io_write_buffer (me
, &val
, io_map
,
230 (unsigned_word
) M6811_BAUD
, 1);
232 m68hc11sio_io_write_buffer (me
, &val
, io_map
,
233 (unsigned_word
) M6811_SCCR2
, 1);
239 hw_abort (me
, "Event on unknown port %d", my_port
);
246 m68hc11sio_rx_poll (struct hw
*me
, void *data
)
249 struct m68hc11sio
*controller
;
253 int check_interrupt
= 0;
255 controller
= hw_data (me
);
257 cpu
= STATE_CPU (sd
, 0);
258 switch (controller
->backend
)
261 cnt
= dv_sockser_read (sd
);
270 cnt
= sim_io_poll_read (sd
, 0 /* stdin */, &cc
, 1);
280 /* Raise the overrun flag if the previous character was not read. */
281 if (cpu
->ios
[M6811_SCSR
] & M6811_RDRF
)
282 cpu
->ios
[M6811_SCSR
] |= M6811_OR
;
284 cpu
->ios
[M6811_SCSR
] |= M6811_RDRF
;
285 controller
->rx_char
= cc
;
286 controller
->rx_clear_scsr
= 0;
291 /* handle idle line detect here. */
295 if (controller
->rx_poll_event
)
297 hw_event_queue_deschedule (me
, controller
->rx_poll_event
);
298 controller
->rx_poll_event
= 0;
301 if (cpu
->ios
[M6811_SCCR2
] & M6811_RE
)
303 unsigned long clock_cycle
;
305 /* Compute CPU clock cycles to wait for the next character. */
306 clock_cycle
= controller
->data_length
* controller
->baud_cycle
;
308 controller
->rx_poll_event
= hw_event_queue_schedule (me
, clock_cycle
,
314 interrupts_update_pending (&cpu
->cpu_interrupts
);
319 m68hc11sio_tx_poll (struct hw
*me
, void *data
)
322 struct m68hc11sio
*controller
;
324 int check_interrupt
= 0;
326 controller
= hw_data (me
);
328 cpu
= STATE_CPU (sd
, 0);
330 cpu
->ios
[M6811_SCSR
] |= M6811_TDRE
;
331 cpu
->ios
[M6811_SCSR
] |= M6811_TC
;
333 /* Transmitter is enabled and we have something to sent. */
334 if ((cpu
->ios
[M6811_SCCR2
] & M6811_TE
) && controller
->tx_has_char
)
336 cpu
->ios
[M6811_SCSR
] &= ~M6811_TDRE
;
337 cpu
->ios
[M6811_SCSR
] &= ~M6811_TC
;
338 controller
->tx_has_char
= 0;
340 switch (controller
->backend
)
343 dv_sockser_write (sd
, controller
->tx_char
);
347 sim_io_write_stdout (sd
, &controller
->tx_char
, 1);
348 sim_io_flush_stdout (sd
);
356 if (controller
->tx_poll_event
)
358 hw_event_queue_deschedule (me
, controller
->tx_poll_event
);
359 controller
->tx_poll_event
= 0;
362 if ((cpu
->ios
[M6811_SCCR2
] & M6811_TE
)
363 && ((cpu
->ios
[M6811_SCSR
] & M6811_TC
) == 0))
365 unsigned long clock_cycle
;
367 /* Compute CPU clock cycles to wait for the next character. */
368 clock_cycle
= controller
->data_length
* controller
->baud_cycle
;
370 controller
->tx_poll_event
= hw_event_queue_schedule (me
, clock_cycle
,
376 interrupts_update_pending (&cpu
->cpu_interrupts
);
379 /* Descriptions of the SIO I/O ports. These descriptions are only used to
380 give information of the SIO device under GDB. */
381 io_reg_desc sccr2_desc
[] = {
382 { M6811_TIE
, "TIE ", "Transmit Interrupt Enable" },
383 { M6811_TCIE
, "TCIE ", "Transmit Complete Interrupt Enable" },
384 { M6811_RIE
, "RIE ", "Receive Interrupt Enable" },
385 { M6811_ILIE
, "ILIE ", "Idle Line Interrupt Enable" },
386 { M6811_TE
, "TE ", "Transmit Enable" },
387 { M6811_RE
, "RE ", "Receive Enable" },
388 { M6811_RWU
, "RWU ", "Receiver Wake Up" },
389 { M6811_SBK
, "SBRK ", "Send Break" },
393 io_reg_desc sccr1_desc
[] = {
394 { M6811_R8
, "R8 ", "Receive Data bit 8" },
395 { M6811_T8
, "T8 ", "Transmit Data bit 8" },
396 { M6811_M
, "M ", "SCI Character length (0=8-bits, 1=9-bits)" },
397 { M6811_WAKE
, "WAKE ", "Wake up method select (0=idle, 1=addr mark" },
401 io_reg_desc scsr_desc
[] = {
402 { M6811_TDRE
, "TDRE ", "Transmit Data Register Empty" },
403 { M6811_TC
, "TC ", "Transmit Complete" },
404 { M6811_RDRF
, "RDRF ", "Receive Data Register Full" },
405 { M6811_IDLE
, "IDLE ", "Idle Line Detect" },
406 { M6811_OR
, "OR ", "Overrun Error" },
407 { M6811_NF
, "NF ", "Noise Flag" },
408 { M6811_FE
, "FE ", "Framing Error" },
412 io_reg_desc baud_desc
[] = {
413 { M6811_TCLR
, "TCLR ", "Clear baud rate (test mode)" },
414 { M6811_SCP1
, "SCP1 ", "SCI baud rate prescaler select (SCP1)" },
415 { M6811_SCP0
, "SCP0 ", "SCI baud rate prescaler select (SCP0)" },
416 { M6811_RCKB
, "RCKB ", "Baur Rate Clock Check (test mode)" },
417 { M6811_SCR2
, "SCR2 ", "SCI Baud rate select (SCR2)" },
418 { M6811_SCR1
, "SCR1 ", "SCI Baud rate select (SCR1)" },
419 { M6811_SCR0
, "SCR0 ", "SCI Baud rate select (SCR0)" },
424 m68hc11sio_info (struct hw
*me
)
429 struct m68hc11sio
*controller
;
434 cpu
= STATE_CPU (sd
, 0);
435 controller
= hw_data (me
);
437 sim_io_printf (sd
, "M68HC11 SIO:\n");
439 base
= cpu_get_io_base (cpu
);
441 val
= cpu
->ios
[M6811_BAUD
];
442 print_io_byte (sd
, "BAUD ", baud_desc
, val
, base
+ M6811_BAUD
);
443 sim_io_printf (sd
, " (%ld baud)\n",
444 (cpu
->cpu_frequency
/ 4) / controller
->baud_cycle
);
446 val
= cpu
->ios
[M6811_SCCR1
];
447 print_io_byte (sd
, "SCCR1", sccr1_desc
, val
, base
+ M6811_SCCR1
);
448 sim_io_printf (sd
, " (%d bits) (%dN1)\n",
449 controller
->data_length
, controller
->data_length
- 2);
451 val
= cpu
->ios
[M6811_SCCR2
];
452 print_io_byte (sd
, "SCCR2", sccr2_desc
, val
, base
+ M6811_SCCR2
);
453 sim_io_printf (sd
, "\n");
455 val
= cpu
->ios
[M6811_SCSR
];
456 print_io_byte (sd
, "SCSR ", scsr_desc
, val
, base
+ M6811_SCSR
);
457 sim_io_printf (sd
, "\n");
459 clock_cycle
= controller
->data_length
* controller
->baud_cycle
;
461 if (controller
->tx_poll_event
)
466 t
= hw_event_remain_time (me
, controller
->tx_poll_event
);
467 n
= (clock_cycle
- t
) / controller
->baud_cycle
;
468 n
= controller
->data_length
- n
;
469 sim_io_printf (sd
, " Transmit finished in %ld cycles (%d bit%s)\n",
470 (long) t
, n
, (n
> 1 ? "s" : ""));
472 if (controller
->rx_poll_event
)
476 t
= hw_event_remain_time (me
, controller
->rx_poll_event
);
477 sim_io_printf (sd
, " Receive finished in %ld cycles\n",
484 m68hc11sio_ioctl (struct hw
*me
,
485 hw_ioctl_request request
,
488 m68hc11sio_info (me
);
492 /* generic read/write */
495 m68hc11sio_io_read_buffer (struct hw
*me
,
502 struct m68hc11sio
*controller
;
506 HW_TRACE ((me
, "read 0x%08lx %d", (long) base
, (int) nr_bytes
));
509 cpu
= STATE_CPU (sd
, 0);
510 controller
= hw_data (me
);
515 controller
->rx_clear_scsr
= cpu
->ios
[M6811_SCSR
]
516 & (M6811_RDRF
| M6811_IDLE
| M6811_OR
| M6811_NF
| M6811_FE
);
521 val
= cpu
->ios
[base
];
525 if (controller
->rx_clear_scsr
)
527 cpu
->ios
[M6811_SCSR
] &= ~controller
->rx_clear_scsr
;
529 val
= controller
->rx_char
;
535 *((unsigned8
*) dest
) = val
;
540 m68hc11sio_io_write_buffer (struct hw
*me
,
547 struct m68hc11sio
*controller
;
551 HW_TRACE ((me
, "write 0x%08lx %d", (long) base
, (int) nr_bytes
));
554 cpu
= STATE_CPU (sd
, 0);
555 controller
= hw_data (me
);
557 val
= *((const unsigned8
*) source
);
565 cpu
->ios
[M6811_BAUD
] = val
;
566 switch (val
& (M6811_SCP1
|M6811_SCP0
))
568 case M6811_BAUD_DIV_1
:
572 case M6811_BAUD_DIV_3
:
576 case M6811_BAUD_DIV_4
:
581 case M6811_BAUD_DIV_13
:
585 val
&= (M6811_SCR2
|M6811_SCR1
|M6811_SCR0
);
586 divisor
*= (1 << val
);
588 baud
= (cpu
->cpu_frequency
/ 4) / divisor
;
590 HW_TRACE ((me
, "divide rate %ld, baud rate %ld",
593 controller
->baud_cycle
= divisor
;
600 controller
->data_length
= 11;
602 controller
->data_length
= 10;
604 cpu
->ios
[M6811_SCCR1
] = val
;
609 if ((val
& M6811_RE
) == 0)
611 val
&= ~(M6811_RDRF
|M6811_IDLE
|M6811_OR
|M6811_NF
|M6811_NF
);
612 val
|= (cpu
->ios
[M6811_SCCR2
]
613 & (M6811_RDRF
|M6811_IDLE
|M6811_OR
|M6811_NF
|M6811_NF
));
614 cpu
->ios
[M6811_SCCR2
] = val
;
618 /* Activate reception. */
619 if (controller
->rx_poll_event
== 0)
623 /* Compute CPU clock cycles to wait for the next character. */
624 clock_cycle
= controller
->data_length
* controller
->baud_cycle
;
626 controller
->rx_poll_event
= hw_event_queue_schedule (me
, clock_cycle
,
630 cpu
->ios
[M6811_SCCR2
] = val
;
631 interrupts_update_pending (&cpu
->cpu_interrupts
);
639 if (!(cpu
->ios
[M6811_SCSR
] & M6811_TDRE
))
644 controller
->tx_char
= val
;
645 controller
->tx_has_char
= 1;
646 if ((cpu
->ios
[M6811_SCCR2
] & M6811_TE
)
647 && controller
->tx_poll_event
== 0)
649 m68hc11sio_tx_poll (me
, NULL
);
660 const struct hw_descriptor dv_m68hc11sio_descriptor
[] = {
661 { "m68hc11sio", m68hc11sio_finish
, },