1 /* dv-m68hc11sio.c -- Simulation of the 68HC11 serial device.
2 Copyright (C) 1999-2022 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 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 m68hc11sio - m68hc11 serial I/O
39 Implements the m68hc11 serial I/O controller described in the m68hc11
40 user guide. The serial I/O controller is directly connected to the CPU
41 interrupt. The simulator implements:
50 Use dv-sockser TCP-port backend or stdio for backend. Default: stdio.
57 Reset port. This port is only used to simulate a reset of the serial
58 I/O controller. It should be connected to the RESET output of the cpu.
72 static const struct hw_port_descriptor m68hc11sio_ports
[] =
74 { "reset", RESET_PORT
, 0, input_port
, },
79 /* Serial Controller information. */
82 enum {sio_tcp
, sio_stdio
} backend
; /* backend */
84 /* Number of cpu cycles to send a bit on the wire. */
85 unsigned long baud_cycle
;
87 /* Length in bits of characters sent, this includes the
88 start/stop and parity bits. Together with baud_cycle, this
89 is used to find the number of cpu cycles to send/receive a data. */
90 unsigned int data_length
;
92 /* Information about next character to be transmited. */
93 unsigned char tx_has_char
;
94 unsigned char tx_char
;
96 unsigned char rx_char
;
97 unsigned char rx_clear_scsr
;
99 /* Periodic I/O polling. */
100 struct hw_event
* tx_poll_event
;
101 struct hw_event
* rx_poll_event
;
106 /* Finish off the partially created hw device. Attach our local
107 callbacks. Wire up our port names etc. */
109 static hw_io_read_buffer_method m68hc11sio_io_read_buffer
;
110 static hw_io_write_buffer_method m68hc11sio_io_write_buffer
;
111 static hw_port_event_method m68hc11sio_port_event
;
112 static hw_ioctl_method m68hc11sio_ioctl
;
114 #define M6811_SCI_FIRST_REG (M6811_BAUD)
115 #define M6811_SCI_LAST_REG (M6811_SCDR)
119 attach_m68hc11sio_regs (struct hw
*me
,
120 struct m68hc11sio
*controller
)
122 hw_attach_address (hw_parent (me
), M6811_IO_LEVEL
, io_map
,
124 M6811_SCI_LAST_REG
- M6811_SCI_FIRST_REG
+ 1,
127 if (hw_find_property(me
, "backend") != NULL
)
129 const char *value
= hw_find_string_property(me
, "backend");
130 if(! strcmp(value
, "tcp"))
131 controller
->backend
= sio_tcp
;
132 else if(! strcmp(value
, "stdio"))
133 controller
->backend
= sio_stdio
;
135 hw_abort (me
, "illegal value for backend parameter `%s':"
136 "use tcp or stdio", value
);
142 m68hc11sio_finish (struct hw
*me
)
144 struct m68hc11sio
*controller
;
146 controller
= HW_ZALLOC (me
, struct m68hc11sio
);
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
;
325 controller
= hw_data (me
);
327 cpu
= STATE_CPU (sd
, 0);
329 cpu
->ios
[M6811_SCSR
] |= M6811_TDRE
;
330 cpu
->ios
[M6811_SCSR
] |= M6811_TC
;
332 /* Transmitter is enabled and we have something to send. */
333 if ((cpu
->ios
[M6811_SCCR2
] & M6811_TE
) && controller
->tx_has_char
)
335 cpu
->ios
[M6811_SCSR
] &= ~M6811_TDRE
;
336 cpu
->ios
[M6811_SCSR
] &= ~M6811_TC
;
337 controller
->tx_has_char
= 0;
338 switch (controller
->backend
)
341 dv_sockser_write (sd
, controller
->tx_char
);
345 sim_io_write_stdout (sd
, (const char *)&controller
->tx_char
, 1);
346 sim_io_flush_stdout (sd
);
354 if (controller
->tx_poll_event
)
356 hw_event_queue_deschedule (me
, controller
->tx_poll_event
);
357 controller
->tx_poll_event
= 0;
360 if ((cpu
->ios
[M6811_SCCR2
] & M6811_TE
)
361 && ((cpu
->ios
[M6811_SCSR
] & M6811_TC
) == 0))
363 unsigned long clock_cycle
;
365 /* Compute CPU clock cycles to wait for the next character. */
366 clock_cycle
= controller
->data_length
* controller
->baud_cycle
;
368 controller
->tx_poll_event
= hw_event_queue_schedule (me
, clock_cycle
,
373 interrupts_update_pending (&cpu
->cpu_interrupts
);
376 /* Descriptions of the SIO I/O ports. These descriptions are only used to
377 give information of the SIO device under GDB. */
378 io_reg_desc sccr2_desc
[] = {
379 { M6811_TIE
, "TIE ", "Transmit Interrupt Enable" },
380 { M6811_TCIE
, "TCIE ", "Transmit Complete Interrupt Enable" },
381 { M6811_RIE
, "RIE ", "Receive Interrupt Enable" },
382 { M6811_ILIE
, "ILIE ", "Idle Line Interrupt Enable" },
383 { M6811_TE
, "TE ", "Transmit Enable" },
384 { M6811_RE
, "RE ", "Receive Enable" },
385 { M6811_RWU
, "RWU ", "Receiver Wake Up" },
386 { M6811_SBK
, "SBRK ", "Send Break" },
390 io_reg_desc sccr1_desc
[] = {
391 { M6811_R8
, "R8 ", "Receive Data bit 8" },
392 { M6811_T8
, "T8 ", "Transmit Data bit 8" },
393 { M6811_M
, "M ", "SCI Character length (0=8-bits, 1=9-bits)" },
394 { M6811_WAKE
, "WAKE ", "Wake up method select (0=idle, 1=addr mark" },
398 io_reg_desc scsr_desc
[] = {
399 { M6811_TDRE
, "TDRE ", "Transmit Data Register Empty" },
400 { M6811_TC
, "TC ", "Transmit Complete" },
401 { M6811_RDRF
, "RDRF ", "Receive Data Register Full" },
402 { M6811_IDLE
, "IDLE ", "Idle Line Detect" },
403 { M6811_OR
, "OR ", "Overrun Error" },
404 { M6811_NF
, "NF ", "Noise Flag" },
405 { M6811_FE
, "FE ", "Framing Error" },
409 io_reg_desc baud_desc
[] = {
410 { M6811_TCLR
, "TCLR ", "Clear baud rate (test mode)" },
411 { M6811_SCP1
, "SCP1 ", "SCI baud rate prescaler select (SCP1)" },
412 { M6811_SCP0
, "SCP0 ", "SCI baud rate prescaler select (SCP0)" },
413 { M6811_RCKB
, "RCKB ", "Baur Rate Clock Check (test mode)" },
414 { M6811_SCR2
, "SCR2 ", "SCI Baud rate select (SCR2)" },
415 { M6811_SCR1
, "SCR1 ", "SCI Baud rate select (SCR1)" },
416 { M6811_SCR0
, "SCR0 ", "SCI Baud rate select (SCR0)" },
421 m68hc11sio_info (struct hw
*me
)
426 struct m68hc11sio
*controller
;
431 cpu
= STATE_CPU (sd
, 0);
432 controller
= hw_data (me
);
434 sim_io_printf (sd
, "M68HC11 SIO:\n");
436 base
= cpu_get_io_base (cpu
);
438 val
= cpu
->ios
[M6811_BAUD
];
439 print_io_byte (sd
, "BAUD ", baud_desc
, val
, base
+ M6811_BAUD
);
440 sim_io_printf (sd
, " (%ld baud)\n",
441 (cpu
->cpu_frequency
/ 4) / controller
->baud_cycle
);
443 val
= cpu
->ios
[M6811_SCCR1
];
444 print_io_byte (sd
, "SCCR1", sccr1_desc
, val
, base
+ M6811_SCCR1
);
445 sim_io_printf (sd
, " (%d bits) (%dN1)\n",
446 controller
->data_length
, controller
->data_length
- 2);
448 val
= cpu
->ios
[M6811_SCCR2
];
449 print_io_byte (sd
, "SCCR2", sccr2_desc
, val
, base
+ M6811_SCCR2
);
450 sim_io_printf (sd
, "\n");
452 val
= cpu
->ios
[M6811_SCSR
];
453 print_io_byte (sd
, "SCSR ", scsr_desc
, val
, base
+ M6811_SCSR
);
454 sim_io_printf (sd
, "\n");
456 clock_cycle
= controller
->data_length
* controller
->baud_cycle
;
458 if (controller
->tx_poll_event
)
463 t
= hw_event_remain_time (me
, controller
->tx_poll_event
);
464 n
= (clock_cycle
- t
) / controller
->baud_cycle
;
465 n
= controller
->data_length
- n
;
466 sim_io_printf (sd
, " Transmit finished in %s (%d bit%s)\n",
467 cycle_to_string (cpu
, t
, PRINT_TIME
| PRINT_CYCLE
),
468 n
, (n
> 1 ? "s" : ""));
470 if (controller
->rx_poll_event
)
474 t
= hw_event_remain_time (me
, controller
->rx_poll_event
);
475 sim_io_printf (sd
, " Receive finished in %s\n",
476 cycle_to_string (cpu
, t
, PRINT_TIME
| PRINT_CYCLE
));
482 m68hc11sio_ioctl (struct hw
*me
,
483 hw_ioctl_request request
,
486 m68hc11sio_info (me
);
490 /* generic read/write */
493 m68hc11sio_io_read_buffer (struct hw
*me
,
500 struct m68hc11sio
*controller
;
504 HW_TRACE ((me
, "read 0x%08lx %d", (long) base
, (int) nr_bytes
));
507 cpu
= STATE_CPU (sd
, 0);
508 controller
= hw_data (me
);
513 controller
->rx_clear_scsr
= cpu
->ios
[M6811_SCSR
]
514 & (M6811_RDRF
| M6811_IDLE
| M6811_OR
| M6811_NF
| M6811_FE
);
519 val
= cpu
->ios
[base
];
523 if (controller
->rx_clear_scsr
)
525 cpu
->ios
[M6811_SCSR
] &= ~controller
->rx_clear_scsr
;
527 val
= controller
->rx_char
;
533 *((unsigned8
*) dest
) = val
;
538 m68hc11sio_io_write_buffer (struct hw
*me
,
545 struct m68hc11sio
*controller
;
549 HW_TRACE ((me
, "write 0x%08lx %d", (long) base
, (int) nr_bytes
));
552 cpu
= STATE_CPU (sd
, 0);
553 controller
= hw_data (me
);
555 val
= *((const unsigned8
*) source
);
563 cpu
->ios
[M6811_BAUD
] = val
;
564 switch (val
& (M6811_SCP1
|M6811_SCP0
))
566 case M6811_BAUD_DIV_1
:
570 case M6811_BAUD_DIV_3
:
574 case M6811_BAUD_DIV_4
:
579 case M6811_BAUD_DIV_13
:
583 val
&= (M6811_SCR2
|M6811_SCR1
|M6811_SCR0
);
584 divisor
*= (1 << val
);
586 baud
= (cpu
->cpu_frequency
/ 4) / divisor
;
588 HW_TRACE ((me
, "divide rate %ld, baud rate %ld",
591 controller
->baud_cycle
= divisor
;
598 controller
->data_length
= 11;
600 controller
->data_length
= 10;
602 cpu
->ios
[M6811_SCCR1
] = val
;
607 if ((val
& M6811_RE
) == 0)
609 val
&= ~(M6811_RDRF
|M6811_IDLE
|M6811_OR
|M6811_NF
|M6811_NF
);
610 val
|= (cpu
->ios
[M6811_SCCR2
]
611 & (M6811_RDRF
|M6811_IDLE
|M6811_OR
|M6811_NF
|M6811_NF
));
612 cpu
->ios
[M6811_SCCR2
] = val
;
616 /* Activate reception. */
617 if (controller
->rx_poll_event
== 0)
621 /* Compute CPU clock cycles to wait for the next character. */
622 clock_cycle
= controller
->data_length
* controller
->baud_cycle
;
624 controller
->rx_poll_event
= hw_event_queue_schedule (me
, clock_cycle
,
628 cpu
->ios
[M6811_SCCR2
] = val
;
629 interrupts_update_pending (&cpu
->cpu_interrupts
);
637 if (!(cpu
->ios
[M6811_SCSR
] & M6811_TDRE
))
642 controller
->tx_char
= val
;
643 controller
->tx_has_char
= 1;
644 if ((cpu
->ios
[M6811_SCCR2
] & M6811_TE
)
645 && controller
->tx_poll_event
== 0)
647 m68hc11sio_tx_poll (me
, NULL
);
658 const struct hw_descriptor dv_m68hc11sio_descriptor
[] = {
659 { "m68hc11sio", m68hc11sio_finish
},
660 { "m68hc12sio", m68hc11sio_finish
},