1 /* dv-m68hc11eepr.c -- Simulation of the 68HC11 Internal EEPROM.
2 Copyright (C) 1999, 2000, 2001, 2002, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Written by Stephane Carrez (stcarrez@nerim.fr)
5 (From a driver model Contributed by Cygnus Solutions.)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "sim-assert.h"
26 #include "sim-events.h"
36 m68hc11eepr - m68hc11 EEPROM
41 Implements the 68HC11 eeprom device described in the m68hc11
42 user guide (Chapter 4 in the pink book).
49 Base of eeprom and its length.
53 Path of the EEPROM file. The default is 'm6811.eeprom'.
64 /* static functions */
75 static const struct hw_port_descriptor m68hc11eepr_ports
[] =
77 { "reset", RESET_PORT
, 0, input_port
, },
83 /* The timer/counter register internal state. Note that we store
84 state using the control register images, in host endian order. */
88 address_word base_address
; /* control register base */
93 /* Current state of the eeprom programing:
94 - eeprom_wmode indicates whether the EEPROM address and byte have
96 - eeprom_waddr indicates the EEPROM address that was latched
97 and eeprom_wbyte is the byte that was latched.
98 - eeprom_wcycle indicates the CPU absolute cycle type when
99 the high voltage was applied (successfully) on the EEPROM.
101 These data members are setup only when we detect good EEPROM programing
102 conditions (see Motorola EEPROM Programming and PPROG register usage).
103 When the high voltage is switched off, we look at the CPU absolute
104 cycle time to see if the EEPROM command must succeeds or not.
105 The EEPROM content is updated and saved only at that time.
106 (EEPROM command is: byte zero bits program, byte erase, row erase
109 The CONFIG register is programmed in the same way. It is physically
110 located at the end of the EEPROM (eeprom size + 1). It is not mapped
111 in memory but it's saved in the EEPROM file. */
112 unsigned long eeprom_wcycle
;
119 /* Minimum time in CPU cycles for programming the EEPROM. */
120 unsigned long eeprom_min_cycles
;
122 const char* file_name
;
127 /* Finish off the partially created hw device. Attach our local
128 callbacks. Wire up our port names etc. */
130 static hw_io_read_buffer_method m68hc11eepr_io_read_buffer
;
131 static hw_io_write_buffer_method m68hc11eepr_io_write_buffer
;
132 static hw_ioctl_method m68hc11eepr_ioctl
;
134 /* Read or write the memory bank content from/to a file.
135 Returns 0 if the operation succeeded and -1 if it failed. */
137 m6811eepr_memory_rw (struct m68hc11eepr
*controller
, int mode
)
139 const char *name
= controller
->file_name
;
143 size
= controller
->size
;
144 fd
= open (name
, mode
, 0644);
147 if (mode
== O_RDONLY
)
149 memset (controller
->eeprom
, 0xFF, size
);
150 /* Default value for CONFIG register (0xFF should be ok):
151 controller->eeprom[size - 1] = M6811_NOSEC | M6811_NOCOP
152 | M6811_ROMON | M6811_EEON; */
158 if (mode
== O_RDONLY
)
160 if (read (fd
, controller
->eeprom
, size
) != size
)
168 if (write (fd
, controller
->eeprom
, size
) != size
)
183 attach_m68hc11eepr_regs (struct hw
*me
,
184 struct m68hc11eepr
*controller
)
186 unsigned_word attach_address
;
188 unsigned attach_size
;
189 reg_property_spec reg
;
191 if (hw_find_property (me
, "reg") == NULL
)
192 hw_abort (me
, "Missing \"reg\" property");
194 if (!hw_find_reg_array_property (me
, "reg", 0, ®
))
195 hw_abort (me
, "\"reg\" property must contain one addr/size entry");
197 hw_unit_address_to_attach_address (hw_parent (me
),
202 hw_unit_size_to_attach_size (hw_parent (me
),
206 /* Attach the two IO registers that control the EEPROM.
207 The EEPROM is only attached at reset time because it may
208 be enabled/disabled by the EEON bit in the CONFIG register. */
209 hw_attach_address (hw_parent (me
), M6811_IO_LEVEL
,
210 io_map
, M6811_PPROG
, 1, me
);
211 hw_attach_address (hw_parent (me
), M6811_IO_LEVEL
,
212 io_map
, M6811_CONFIG
, 1, me
);
214 if (hw_find_property (me
, "file") == NULL
)
215 controller
->file_name
= "m6811.eeprom";
217 controller
->file_name
= hw_find_string_property (me
, "file");
219 controller
->attach_space
= attach_space
;
220 controller
->base_address
= attach_address
;
221 controller
->eeprom
= (char*) hw_malloc (me
, attach_size
+ 1);
222 controller
->eeprom_min_cycles
= 10000;
223 controller
->size
= attach_size
+ 1;
224 controller
->mapped
= 0;
226 m6811eepr_memory_rw (controller
, O_RDONLY
);
230 /* An event arrives on an interrupt port. */
233 m68hc11eepr_port_event (struct hw
*me
,
240 struct m68hc11eepr
*controller
;
243 controller
= hw_data (me
);
245 cpu
= STATE_CPU (sd
, 0);
250 HW_TRACE ((me
, "EEPROM reset"));
252 /* Re-read the EEPROM from the file. This gives the chance
253 to users to erase this file before doing a reset and have
254 a fresh EEPROM taken into account. */
255 m6811eepr_memory_rw (controller
, O_RDONLY
);
257 /* Reset the state of EEPROM programmer. The CONFIG register
258 is also initialized from the EEPROM/file content. */
259 cpu
->ios
[M6811_PPROG
] = 0;
260 if (cpu
->cpu_use_local_config
)
261 cpu
->ios
[M6811_CONFIG
] = cpu
->cpu_config
;
263 cpu
->ios
[M6811_CONFIG
] = controller
->eeprom
[controller
->size
-1];
264 controller
->eeprom_wmode
= 0;
265 controller
->eeprom_waddr
= 0;
266 controller
->eeprom_wbyte
= 0;
268 /* Attach or detach to the bus depending on the EEPROM enable bit.
269 The EEPROM CONFIG register is still enabled and can be programmed
270 for a next configuration (taken into account only after a reset,
271 see Motorola spec). */
272 if (!(cpu
->ios
[M6811_CONFIG
] & M6811_EEON
))
274 if (controller
->mapped
)
275 hw_detach_address (hw_parent (me
), M6811_EEPROM_LEVEL
,
276 controller
->attach_space
,
277 controller
->base_address
,
278 controller
->size
- 1,
280 controller
->mapped
= 0;
284 if (!controller
->mapped
)
285 hw_attach_address (hw_parent (me
), M6811_EEPROM_LEVEL
,
286 controller
->attach_space
,
287 controller
->base_address
,
288 controller
->size
- 1,
290 controller
->mapped
= 1;
296 hw_abort (me
, "Event on unknown port %d", my_port
);
303 m68hc11eepr_finish (struct hw
*me
)
305 struct m68hc11eepr
*controller
;
307 controller
= HW_ZALLOC (me
, struct m68hc11eepr
);
308 set_hw_data (me
, controller
);
309 set_hw_io_read_buffer (me
, m68hc11eepr_io_read_buffer
);
310 set_hw_io_write_buffer (me
, m68hc11eepr_io_write_buffer
);
311 set_hw_ports (me
, m68hc11eepr_ports
);
312 set_hw_port_event (me
, m68hc11eepr_port_event
);
314 set_hw_ioctl (me
, m68hc11eepr_ioctl
);
316 me
->to_ioctl
= m68hc11eepr_ioctl
;
319 attach_m68hc11eepr_regs (me
, controller
);
324 static io_reg_desc pprog_desc
[] = {
325 { M6811_BYTE
, "BYTE ", "Byte Program Mode" },
326 { M6811_ROW
, "ROW ", "Row Program Mode" },
327 { M6811_ERASE
, "ERASE ", "Erase Mode" },
328 { M6811_EELAT
, "EELAT ", "EEProm Latch Control" },
329 { M6811_EEPGM
, "EEPGM ", "EEProm Programming Voltable Enable" },
332 extern io_reg_desc config_desc
[];
335 /* Describe the state of the EEPROM device. */
337 m68hc11eepr_info (struct hw
*me
)
342 struct m68hc11eepr
*controller
;
346 cpu
= STATE_CPU (sd
, 0);
347 controller
= hw_data (me
);
348 base
= cpu_get_io_base (cpu
);
350 sim_io_printf (sd
, "M68HC11 EEprom:\n");
352 val
= cpu
->ios
[M6811_PPROG
];
353 print_io_byte (sd
, "PPROG ", pprog_desc
, val
, base
+ M6811_PPROG
);
354 sim_io_printf (sd
, "\n");
356 val
= cpu
->ios
[M6811_CONFIG
];
357 print_io_byte (sd
, "CONFIG ", config_desc
, val
, base
+ M6811_CONFIG
);
358 sim_io_printf (sd
, "\n");
360 val
= controller
->eeprom
[controller
->size
- 1];
361 print_io_byte (sd
, "(*NEXT*) ", config_desc
, val
, base
+ M6811_CONFIG
);
362 sim_io_printf (sd
, "\n");
364 /* Describe internal state of EEPROM. */
365 if (controller
->eeprom_wmode
)
367 if (controller
->eeprom_waddr
== controller
->size
- 1)
368 sim_io_printf (sd
, " Programming CONFIG register ");
370 sim_io_printf (sd
, " Programming: 0x%04x ",
371 controller
->eeprom_waddr
+ controller
->base_address
);
373 sim_io_printf (sd
, "with 0x%02x\n",
374 controller
->eeprom_wbyte
);
377 sim_io_printf (sd
, " EEProm file: %s\n",
378 controller
->file_name
);
382 m68hc11eepr_ioctl (struct hw
*me
,
383 hw_ioctl_request request
,
386 m68hc11eepr_info (me
);
390 /* generic read/write */
393 m68hc11eepr_io_read_buffer (struct hw
*me
,
400 struct m68hc11eepr
*controller
;
403 HW_TRACE ((me
, "read 0x%08lx %d", (long) base
, (int) nr_bytes
));
406 controller
= hw_data (me
);
407 cpu
= STATE_CPU (sd
, 0);
413 while (nr_bytes
!= 0)
419 *((uint8
*) dest
) = cpu
->ios
[base
];
423 hw_abort (me
, "reading wrong register 0x%04x", base
);
425 dest
= (uint8
*) (dest
) + 1;
433 /* In theory, we can't read the EEPROM when it's being programmed. */
434 if ((cpu
->ios
[M6811_PPROG
] & M6811_EELAT
) != 0
435 && cpu_is_running (cpu
))
437 sim_memory_error (cpu
, SIM_SIGBUS
, base
,
438 "EEprom not configured for reading");
441 base
= base
- controller
->base_address
;
442 memcpy (dest
, &controller
->eeprom
[base
], nr_bytes
);
448 m68hc11eepr_io_write_buffer (struct hw
*me
,
455 struct m68hc11eepr
*controller
;
459 HW_TRACE ((me
, "write 0x%08lx %d", (long) base
, (int) nr_bytes
));
462 controller
= hw_data (me
);
463 cpu
= STATE_CPU (sd
, 0);
465 /* Programming several bytes at a time is not possible. */
466 if (space
!= io_map
&& nr_bytes
!= 1)
468 sim_memory_error (cpu
, SIM_SIGBUS
, base
,
469 "EEprom write error (only 1 byte can be programmed)");
474 hw_abort (me
, "Cannot write more than 1 byte to EEPROM device at a time");
476 val
= *((const uint8
*) source
);
478 /* Write to the EEPROM control register. */
479 if (space
== io_map
&& base
== M6811_PPROG
)
484 addr
= base
+ cpu_get_io_base (cpu
);
486 /* Setting EELAT and EEPGM at the same time is an error.
487 Clearing them both is ok. */
488 wrong_bits
= (cpu
->ios
[M6811_PPROG
] ^ val
) & val
;
489 wrong_bits
&= (M6811_EELAT
| M6811_EEPGM
);
491 if (wrong_bits
== (M6811_EEPGM
|M6811_EELAT
))
493 sim_memory_error (cpu
, SIM_SIGBUS
, addr
,
494 "Wrong eeprom programing value");
498 if ((val
& M6811_EELAT
) == 0)
502 if ((val
& M6811_EEPGM
) && !(cpu
->ios
[M6811_PPROG
] & M6811_EELAT
))
504 sim_memory_error (cpu
, SIM_SIGBUS
, addr
,
505 "EEProm high voltage applied after EELAT");
507 if ((val
& M6811_EEPGM
) && controller
->eeprom_wmode
== 0)
509 sim_memory_error (cpu
, SIM_SIGSEGV
, addr
,
510 "EEProm high voltage applied without address");
512 if (val
& M6811_EEPGM
)
514 controller
->eeprom_wcycle
= cpu_current_cycle (cpu
);
516 else if (cpu
->ios
[M6811_PPROG
] & M6811_PPROG
)
519 unsigned long t
= cpu_current_cycle (cpu
);
521 t
-= controller
->eeprom_wcycle
;
522 if (t
< controller
->eeprom_min_cycles
)
524 sim_memory_error (cpu
, SIM_SIGILL
, addr
,
525 "EEprom programmed only for %lu cycles",
529 /* Program the byte by clearing some bits. */
530 if (!(cpu
->ios
[M6811_PPROG
] & M6811_ERASE
))
532 controller
->eeprom
[controller
->eeprom_waddr
]
533 &= controller
->eeprom_wbyte
;
536 /* Erase a byte, row or the complete eeprom. Erased value is 0xFF.
537 Ignore row or complete eeprom erase when we are programming the
538 CONFIG register (last EEPROM byte). */
539 else if ((cpu
->ios
[M6811_PPROG
] & M6811_BYTE
)
540 || controller
->eeprom_waddr
== controller
->size
- 1)
542 controller
->eeprom
[controller
->eeprom_waddr
] = 0xff;
544 else if (cpu
->ios
[M6811_BYTE
] & M6811_ROW
)
548 /* Size of EEPROM (-1 because the last byte is the
550 max_size
= controller
->size
;
551 controller
->eeprom_waddr
&= 0xFFF0;
553 && controller
->eeprom_waddr
< max_size
; i
++)
555 controller
->eeprom
[controller
->eeprom_waddr
] = 0xff;
556 controller
->eeprom_waddr
++;
563 max_size
= controller
->size
;
564 for (i
= 0; i
< max_size
; i
++)
566 controller
->eeprom
[i
] = 0xff;
570 /* Save the eeprom in a file. We have to save after each
571 change because the simulator can be stopped or crash... */
572 if (m6811eepr_memory_rw (controller
, O_WRONLY
| O_CREAT
) != 0)
574 sim_memory_error (cpu
, SIM_SIGABRT
, addr
,
575 "EEPROM programing failed: errno=%d", errno
);
577 controller
->eeprom_wmode
= 0;
579 cpu
->ios
[M6811_PPROG
] = val
;
583 /* The CONFIG IO register is mapped at end of EEPROM.
585 if (space
== io_map
&& base
== M6811_CONFIG
)
587 base
= controller
->size
- 1;
591 base
= base
- controller
->base_address
;
594 /* Writing the memory is allowed for the Debugger or simulator
595 (cpu not running). */
596 if (cpu_is_running (cpu
))
598 if ((cpu
->ios
[M6811_PPROG
] & M6811_EELAT
) == 0)
600 sim_memory_error (cpu
, SIM_SIGSEGV
, base
,
601 "EEprom not configured for writing");
604 if (controller
->eeprom_wmode
!= 0)
606 sim_memory_error (cpu
, SIM_SIGSEGV
, base
,
607 "EEprom write error");
610 controller
->eeprom_wmode
= 1;
611 controller
->eeprom_waddr
= base
;
612 controller
->eeprom_wbyte
= val
;
616 controller
->eeprom
[base
] = val
;
617 m6811eepr_memory_rw (controller
, O_WRONLY
);
623 const struct hw_descriptor dv_m68hc11eepr_descriptor
[] = {
624 { "m68hc11eepr", m68hc11eepr_finish
},
625 { "m68hc12eepr", m68hc11eepr_finish
},