1 // license:BSD-3-Clause
2 // copyright-holders:Nathan Woods
3 /*** m6809: Portable 6809 emulator ******************************************
4 
5     Copyright John Butler
6 
7     References:
8 
9         6809 Simulator V09, By L.C. Benschop, Eindhoven The Netherlands.
10 
11         m6809: Portable 6809 emulator, DS (6809 code in MAME, derived from
12             the 6809 Simulator V09)
13 
14         6809 Microcomputer Programming & Interfacing with Experiments"
15             by Andrew C. Staugaard, Jr.; Howard W. Sams & Co., Inc.
16 
17     System dependencies:    uint16_t must be 16 bit unsigned int
18                             uint8_t must be 8 bit unsigned int
19                             uint32_t must be more than 16 bits
20                             arrays up to 65536 bytes must be supported
21                             machine must be twos complement
22 
23     History:
24 
25 July 2016 ErikGav:
26     Unify with 6309 pairs and quads (A+B=D, E+F=W, D+W=Q)
27 
28 March 2013 NPW:
29     Rewrite of 6809/6309/Konami CPU; attempted to make cycle exact.
30 
31 991026 HJB:
32     Fixed missing calls to cpu_changepc() for the TFR and EXG ocpodes.
33     Replaced m6809_slapstic checks by a macro (CHANGE_PC). ESB still
34     needs the tweaks.
35 
36 991024 HJB:
37     Tried to improve speed: Using bit7 of cycles1/2 as flag for multi
38     byte opcodes is gone, those opcodes now call fetch_effective_address().
39     Got rid of the slow/fast flags for stack (S and U) memory accesses.
40     Minor changes to use 32 bit values as arguments to memory functions
41     and added defines for that purpose (e.g. X = 16bit XD = 32bit).
42 
43 990312 HJB:
44     Added bugfixes according to Aaron's findings.
45     Reset only sets CC_II and CC_IF, DP to zero and PC from reset vector.
46 990311 HJB:
47     Added _info functions. Now uses static m6808_Regs struct instead
48     of single statics. Changed the 16 bit registers to use the generic
49     PAIR union. Registers defined using macros. Split the core into
50     four execution loops for M6802, M6803, M6808 and HD63701.
51     TST, TSTA and TSTB opcodes reset carry flag.
52     Modified the read/write stack handlers to push LSB first then MSB
53     and pull MSB first then LSB.
54 
55 990228 HJB:
56     Changed the interrupt handling again. Now interrupts are taken
57     either right at the moment the lines are asserted or whenever
58     an interrupt is enabled and the corresponding line is still
59     asserted. That way the pending_interrupts checks are not
60     needed anymore. However, the CWAI and SYNC flags still need
61     some flags, so I changed the name to 'int_state'.
62     This core also has the code for the old interrupt system removed.
63 
64 990225 HJB:
65     Cleaned up the code here and there, added some comments.
66     Slightly changed the SAR opcodes (similiar to other CPU cores).
67     Added symbolic names for the flag bits.
68     Changed the way CWAI/Interrupt() handle CPU state saving.
69     A new flag M6809_STATE in pending_interrupts is used to determine
70     if a state save is needed on interrupt entry or already done by CWAI.
71     Added M6809_IRQ_LINE and M6809_FIRQ_LINE defines to m6809.h
72     Moved the internal interrupt_pending flags from m6809.h to m6809.c
73     Changed CWAI cycles2[0x3c] to be 2 (plus all or at least 19 if
74     CWAI actually pushes the entire state).
75     Implemented undocumented TFR/EXG for undefined source and mixed 8/16
76     bit transfers (they should transfer/exchange the constant $ff).
77     Removed unused jmp/jsr _slap functions from 6809ops.c,
78     m6809_slapstick check moved into the opcode functions.
79 
80 ******************************************************************************
81 
82     M6809 cycle timings are relative to a four-phase clock cycle defined by
83     the Q and E signals on pins 35 and 34. The Q clock must lead the E clock
84     by approximately half a cycle. On the MC6809E, Q and E are inputs, and
85     one 74LS74 wired as a two-stage Johnson counter is almost sufficient to
86     generate both (though E requires voltage levels above TTL). On the
87     MC6809, however, Q and E are output from an internal clock generator
88     which can be driven by a crystal oscillator connected to pins 38 and 39.
89     (The MC6809E reuses the same numbered pins for the unrelated TSC and LIC
90     functions.) The frequencies of Q and E on the MC6809 are that of the XTAL
91     divided by 4. MAME's emulation formerly assigned this internal clock
92     divider to the MC6809E and not to the MC6809; the confusion resulting
93     from this error is in the process of being straightened out.
94 
95     Maximum clock ratings:
96 
97                         Q & E           EXTAL
98         MC6809(E)       1.0 MHz         4.0 MHz
99         MC68A09(E)      1.5 MHz         6.0 MHz
100         MC68B09(E)      2.0 MHz         8.0 MHz
101         HD63B09(E)      2.0 MHz         8.0 MHz
102         HD63C09(E)      3.0 MHz         12.0 MHz
103 
104 *****************************************************************************/
105 
106 #include "emu.h"
107 #include "debugger.h"
108 #include "m6809.h"
109 #include "m6809inl.h"
110 #include "6x09dasm.h"
111 
112 
113 //**************************************************************************
114 //  PARAMETERS
115 //**************************************************************************
116 
117 #define LOG_INTERRUPTS  0
118 
119 // turn off 'unreferenced label' errors
120 #if defined(__GNUC__)
121 #pragma GCC diagnostic ignored "-Wunused-label"
122 #endif
123 #ifdef _MSC_VER
124 #pragma warning( disable : 4102 )
125 #endif
126 
127 //**************************************************************************
128 //  DEVICE INTERFACE
129 //**************************************************************************
130 
131 DEFINE_DEVICE_TYPE(MC6809, mc6809_device, "mc6809", "Motorola MC6809")
132 DEFINE_DEVICE_TYPE(MC6809E, mc6809e_device, "mc6809e", "Motorola MC6809E")
133 DEFINE_DEVICE_TYPE(M6809, m6809_device, "m6809", "MC6809 (legacy)")
134 
135 
136 //-------------------------------------------------
137 //  m6809_base_device - constructor
138 //-------------------------------------------------
139 
m6809_base_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock,const device_type type,int divider)140 m6809_base_device::m6809_base_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock, const device_type type, int divider)
141 	: cpu_device(mconfig, type, tag, owner, clock),
142 	m_lic_func(*this),
143 	m_program_config("program", ENDIANNESS_BIG, 8, 16),
144 	m_sprogram_config("decrypted_opcodes", ENDIANNESS_BIG, 8, 16),
145 	m_clock_divider(divider)
146 {
147 	m_mintf = nullptr;
148 }
149 
150 //-------------------------------------------------
151 //  device_start - device-specific startup
152 //-------------------------------------------------
153 
device_start()154 void m6809_base_device::device_start()
155 {
156 	if (!m_mintf)
157 		m_mintf = std::make_unique<mi_default>();
158 
159 	space(AS_PROGRAM).cache(m_mintf->cprogram);
160 	space(AS_PROGRAM).specific(m_mintf->program);
161 	space(has_space(AS_OPCODES) ? AS_OPCODES : AS_PROGRAM).cache(m_mintf->csprogram);
162 
163 	m_lic_func.resolve_safe();
164 
165 	// register our state for the debugger
166 	state_add(STATE_GENPCBASE, "CURPC",     m_ppc.w).callimport().noshow();
167 	state_add(STATE_GENFLAGS,  "CURFLAGS",  m_cc).formatstr("%8s").noshow();
168 	state_add(M6809_PC,        "PC",        m_pc.w).callimport().mask(0xffff);
169 	state_add(M6809_S,         "S",         m_s.w).mask(0xffff);
170 	state_add(M6809_CC,        "CC",        m_cc).mask(0xff);
171 	state_add(M6809_DP,        "DP",        m_dp).mask(0xff);
172 
173 	if (is_6809())
174 	{
175 		state_add(M6809_A,     "A",         m_q.r.a).mask(0xff);
176 		state_add(M6809_B,     "B",         m_q.r.b).mask(0xff);
177 		state_add(M6809_D,     "D",         m_q.r.d).mask(0xffff);
178 		state_add(M6809_X,     "X",         m_x.w).mask(0xffff);
179 		state_add(M6809_Y,     "Y",         m_y.w).mask(0xffff);
180 		state_add(M6809_U,     "U",         m_u.w).mask(0xffff);
181 	}
182 
183 	// initialize variables
184 	m_cc = 0;
185 	m_pc.w = 0;
186 	m_s.w = 0;
187 	m_u.w = 0;
188 	m_q.q = 0;
189 	m_x.w = 0;
190 	m_y.w = 0;
191 	m_dp = 0;
192 	m_reg = 0;
193 	m_reg8 = nullptr;
194 	m_reg16 = nullptr;
195 
196 	// setup regtable
197 	save_item(NAME(m_pc.w));
198 	save_item(NAME(m_ppc.w));
199 	save_item(NAME(m_q.q));
200 	save_item(NAME(m_dp));
201 	save_item(NAME(m_u.w));
202 	save_item(NAME(m_s.w));
203 	save_item(NAME(m_x.w));
204 	save_item(NAME(m_y.w));
205 	save_item(NAME(m_cc));
206 	save_item(NAME(m_temp.w));
207 	save_item(NAME(m_opcode));
208 	save_item(NAME(m_nmi_asserted));
209 	save_item(NAME(m_nmi_line));
210 	save_item(NAME(m_firq_line));
211 	save_item(NAME(m_irq_line));
212 	save_item(NAME(m_lds_encountered));
213 	save_item(NAME(m_state));
214 	save_item(NAME(m_ea.w));
215 	save_item(NAME(m_addressing_mode));
216 	save_item(NAME(m_reg));
217 	save_item(NAME(m_cond));
218 
219 	// set our instruction counter
220 	set_icountptr(m_icount);
221 	m_icount = 0;
222 }
223 
224 
225 
226 //-------------------------------------------------
227 //  device_reset - device-specific reset
228 //-------------------------------------------------
229 
device_reset()230 void m6809_base_device::device_reset()
231 {
232 	m_nmi_line = false;
233 	m_nmi_asserted = false;
234 	m_firq_line = false;
235 	m_irq_line = false;
236 	m_lds_encountered = false;
237 
238 	m_dp = 0x00;        // reset direct page register
239 
240 	m_cc |= CC_I;       // IRQ disabled
241 	m_cc |= CC_F;       // FIRQ disabled
242 
243 	m_pc.b.h = space(AS_PROGRAM).read_byte(VECTOR_RESET_FFFE + 0);
244 	m_pc.b.l = space(AS_PROGRAM).read_byte(VECTOR_RESET_FFFE + 1);
245 
246 	// reset sub-instruction state
247 	reset_state();
248 }
249 
250 
251 
252 //-------------------------------------------------
253 //  device_pre_save - device-specific pre-save
254 //-------------------------------------------------
255 
device_pre_save()256 void m6809_base_device::device_pre_save()
257 {
258 	if (m_reg8 == &m_q.r.a)
259 		m_reg = M6809_A;
260 	else if (m_reg8 == &m_q.r.b)
261 		m_reg = M6809_B;
262 	else if (m_reg16 == &m_q.p.d)
263 		m_reg = M6809_D;
264 	else if (m_reg16 == &m_x)
265 		m_reg = M6809_X;
266 	else if (m_reg16 == &m_y)
267 		m_reg = M6809_Y;
268 	else if (m_reg16 == &m_u)
269 		m_reg = M6809_U;
270 	else if (m_reg16 == &m_s)
271 		m_reg = M6809_S;
272 	else
273 		m_reg = 0;
274 }
275 
276 
277 //-------------------------------------------------
278 //  device_post_load - device-specific post-load
279 //-------------------------------------------------
280 
device_post_load()281 void m6809_base_device::device_post_load()
282 {
283 	m_reg8 = nullptr;
284 	m_reg16 = nullptr;
285 
286 	switch(m_reg)
287 	{
288 		case M6809_A:
289 			set_regop8(m_q.r.a);
290 			break;
291 		case M6809_B:
292 			set_regop8(m_q.r.b);
293 			break;
294 		case M6809_D:
295 			set_regop16(m_q.p.d);
296 			break;
297 		case M6809_X:
298 			set_regop16(m_x);
299 			break;
300 		case M6809_Y:
301 			set_regop16(m_y);
302 			break;
303 		case M6809_U:
304 			set_regop16(m_u);
305 			break;
306 		case M6809_S:
307 			set_regop16(m_s);
308 			break;
309 	}
310 }
311 
312 
313 //-------------------------------------------------
314 //  memory_space_config - return the configuration
315 //  of the specified address space, or nullptr if
316 //  the space doesn't exist
317 //-------------------------------------------------
318 
memory_space_config() const319 device_memory_interface::space_config_vector m6809_base_device::memory_space_config() const
320 {
321 	if(has_configured_map(AS_OPCODES))
322 		return space_config_vector {
323 			std::make_pair(AS_PROGRAM, &m_program_config),
324 			std::make_pair(AS_OPCODES, &m_sprogram_config)
325 		};
326 	else
327 		return space_config_vector {
328 			std::make_pair(AS_PROGRAM, &m_program_config)
329 		};
330 }
331 
332 
333 //-------------------------------------------------
334 //  state_import - import state into the device,
335 //  after it has been set
336 //-------------------------------------------------
337 
state_import(const device_state_entry & entry)338 void m6809_base_device::state_import(const device_state_entry &entry)
339 {
340 	switch (entry.index())
341 	{
342 	case M6809_PC:
343 		m_ppc.w = m_pc.w;
344 		break;
345 
346 	case STATE_GENPCBASE:
347 		m_pc.w = m_ppc.w;
348 		break;
349 	}
350 }
351 
352 //-------------------------------------------------
353 //  state_string_export - export state as a string
354 //  for the debugger
355 //-------------------------------------------------
356 
state_string_export(const device_state_entry & entry,std::string & str) const357 void m6809_base_device::state_string_export(const device_state_entry &entry, std::string &str) const
358 {
359 	switch (entry.index())
360 	{
361 		case STATE_GENFLAGS:
362 			str = string_format("%c%c%c%c%c%c%c%c",
363 				(m_cc & 0x80) ? 'E' : '.',
364 				(m_cc & 0x40) ? 'F' : '.',
365 				(m_cc & 0x20) ? 'H' : '.',
366 				(m_cc & 0x10) ? 'I' : '.',
367 				(m_cc & 0x08) ? 'N' : '.',
368 				(m_cc & 0x04) ? 'Z' : '.',
369 				(m_cc & 0x02) ? 'V' : '.',
370 				(m_cc & 0x01) ? 'C' : '.');
371 			break;
372 	}
373 }
374 
375 
376 //-------------------------------------------------
377 //  disassemble - call the disassembly
378 //  helper function
379 //-------------------------------------------------
380 
create_disassembler()381 std::unique_ptr<util::disasm_interface> m6809_base_device::create_disassembler()
382 {
383 	return std::make_unique<m6809_disassembler>();
384 }
385 
386 
387 //**************************************************************************
388 //  CORE EXECUTION LOOP
389 //**************************************************************************
390 
391 //-------------------------------------------------
392 //  execute_clocks_to_cycles - convert the raw
393 //  clock into cycles per second
394 //-------------------------------------------------
395 
execute_clocks_to_cycles(uint64_t clocks) const396 uint64_t m6809_base_device::execute_clocks_to_cycles(uint64_t clocks) const noexcept
397 {
398 	return (clocks + m_clock_divider - 1) / m_clock_divider;
399 }
400 
401 
402 //-------------------------------------------------
403 //  execute_cycles_to_clocks - convert a cycle
404 //  count back to raw clocks
405 //-------------------------------------------------
406 
execute_cycles_to_clocks(uint64_t cycles) const407 uint64_t m6809_base_device::execute_cycles_to_clocks(uint64_t cycles) const noexcept
408 {
409 	return cycles * m_clock_divider;
410 }
411 
412 
413 //-------------------------------------------------
414 //  execute_min_cycles - return minimum number of
415 //  cycles it takes for one instruction to execute
416 //-------------------------------------------------
417 
execute_min_cycles() const418 uint32_t m6809_base_device::execute_min_cycles() const noexcept
419 {
420 	return 1;
421 }
422 
423 
424 //-------------------------------------------------
425 //  execute_max_cycles - return maximum number of
426 //  cycles it takes for one instruction to execute
427 //-------------------------------------------------
428 
execute_max_cycles() const429 uint32_t m6809_base_device::execute_max_cycles() const noexcept
430 {
431 	return 19;
432 }
433 
434 
435 //-------------------------------------------------
436 //  execute_input_lines - return the number of
437 //  input/interrupt lines
438 //-------------------------------------------------
439 
execute_input_lines() const440 uint32_t m6809_base_device::execute_input_lines() const noexcept
441 {
442 	return 3;
443 }
444 
445 
446 //-------------------------------------------------
447 //  execute_set_input - act on a changed input/
448 //  interrupt line
449 //-------------------------------------------------
450 
execute_set_input(int inputnum,int state)451 void m6809_base_device::execute_set_input(int inputnum, int state)
452 {
453 	if (LOG_INTERRUPTS)
454 		logerror("%s: inputnum=%s state=%d totalcycles=%d\n", machine().describe_context(), inputnum_string(inputnum), state, (int) attotime_to_clocks(machine().time()));
455 
456 	switch(inputnum)
457 	{
458 		case INPUT_LINE_NMI:
459 			// NMI is edge triggered
460 			m_nmi_asserted = m_nmi_asserted || ((state != CLEAR_LINE) && !m_nmi_line && m_lds_encountered);
461 			m_nmi_line = (state != CLEAR_LINE);
462 			break;
463 
464 		case M6809_FIRQ_LINE:
465 			// FIRQ is line triggered
466 			m_firq_line = (state != CLEAR_LINE);
467 			break;
468 
469 		case M6809_IRQ_LINE:
470 			// IRQ is line triggered
471 			m_irq_line = (state != CLEAR_LINE);
472 			break;
473 	}
474 }
475 
476 
477 //-------------------------------------------------
478 //  inputnum_string
479 //-------------------------------------------------
480 
inputnum_string(int inputnum)481 const char *m6809_base_device::inputnum_string(int inputnum)
482 {
483 	switch(inputnum)
484 	{
485 		case INPUT_LINE_NMI:    return "NMI";
486 		case M6809_FIRQ_LINE:   return "FIRQ";
487 		case M6809_IRQ_LINE:    return "IRQ";
488 		default:                return "???";
489 	}
490 }
491 
492 
493 //-------------------------------------------------
494 //  read_exgtfr_register
495 //-------------------------------------------------
496 
read_exgtfr_register(uint8_t reg)497 m6809_base_device::exgtfr_register m6809_base_device::read_exgtfr_register(uint8_t reg)
498 {
499 	exgtfr_register result;
500 	result.byte_value = 0xFF;
501 	result.word_value = 0x00FF;
502 
503 	switch(reg & 0x0F)
504 	{
505 		case  0: result.word_value = m_q.r.d;   break;  // D
506 		case  1: result.word_value = m_x.w;     break;  // X
507 		case  2: result.word_value = m_y.w;     break;  // Y
508 		case  3: result.word_value = m_u.w;     break;  // U
509 		case  4: result.word_value = m_s.w;     break;  // S
510 		case  5: result.word_value = m_pc.w;    break;  // PC
511 		case  8: result.byte_value = m_q.r.a;   break;  // A
512 		case  9: result.byte_value = m_q.r.b;   break;  // B
513 		case 10: result.byte_value = m_cc;      break;  // CC
514 		case 11: result.byte_value = m_dp;      break;  // DP
515 	}
516 	return result;
517 }
518 
519 
520 //-------------------------------------------------
521 //  write_exgtfr_register
522 //-------------------------------------------------
523 
write_exgtfr_register(uint8_t reg,m6809_base_device::exgtfr_register value)524 void m6809_base_device::write_exgtfr_register(uint8_t reg, m6809_base_device::exgtfr_register value)
525 {
526 	switch(reg & 0x0F)
527 	{
528 		case  0: m_q.r.d = value.word_value;    break;  // D
529 		case  1: m_x.w   = value.word_value;    break;  // X
530 		case  2: m_y.w   = value.word_value;    break;  // Y
531 		case  3: m_u.w   = value.word_value;    break;  // U
532 		case  4: m_s.w   = value.word_value;    break;  // S
533 		case  5: m_pc.w  = value.word_value;    break;  // PC
534 		case  8: m_q.r.a = value.byte_value;    break;  // A
535 		case  9: m_q.r.b = value.byte_value;    break;  // B
536 		case 10: m_cc    = value.byte_value;    break;  // CC
537 		case 11: m_dp    = value.byte_value;    break;  // DP
538 	}
539 }
540 
541 
542 //-------------------------------------------------
543 //  log_illegal - used to log hits to illegal
544 //  instructions (except for HD6309 which traps)
545 //-------------------------------------------------
546 
log_illegal()547 void m6809_base_device::log_illegal()
548 {
549 	logerror("%s: illegal opcode at %04x\n", machine().describe_context(), (unsigned) m_pc.w);
550 }
551 
552 
553 //-------------------------------------------------
554 //  execute_one - try to execute a single instruction
555 //-------------------------------------------------
556 
execute_one()557 void m6809_base_device::execute_one()
558 {
559 	switch(pop_state())
560 	{
561 #include "cpu/m6809/m6809.hxx"
562 	}
563 }
564 
565 
566 //-------------------------------------------------
567 //  execute_run - execute a timeslice's worth of
568 //  opcodes
569 //-------------------------------------------------
570 
execute_run()571 void m6809_base_device::execute_run()
572 {
573 	do
574 	{
575 		execute_one();
576 	} while(m_icount > 0);
577 }
578 
579 
read(uint16_t adr)580 uint8_t m6809_base_device::mi_default::read(uint16_t adr)
581 {
582 	return program.read_byte(adr);
583 }
584 
read_opcode(uint16_t adr)585 uint8_t m6809_base_device::mi_default::read_opcode(uint16_t adr)
586 {
587 	return csprogram.read_byte(adr);
588 }
589 
read_opcode_arg(uint16_t adr)590 uint8_t m6809_base_device::mi_default::read_opcode_arg(uint16_t adr)
591 {
592 	return cprogram.read_byte(adr);
593 }
594 
595 
write(uint16_t adr,uint8_t val)596 void m6809_base_device::mi_default::write(uint16_t adr, uint8_t val)
597 {
598 	program.write_byte(adr, val);
599 }
600 
601 
602 
603 //-------------------------------------------------
604 //  mc6809_device
605 //-------------------------------------------------
606 
mc6809_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)607 mc6809_device::mc6809_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
608 	: m6809_base_device(mconfig, tag, owner, clock, MC6809, 4)
609 {
610 }
611 
612 
613 
614 //-------------------------------------------------
615 //  mc6809e_device
616 //-------------------------------------------------
617 
mc6809e_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)618 mc6809e_device::mc6809e_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
619 	: m6809_base_device(mconfig, tag, owner, clock, MC6809E, 1)
620 {
621 }
622 
623 
624 
625 //-------------------------------------------------
626 //  m6809_device
627 //-------------------------------------------------
628 
m6809_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)629 m6809_device::m6809_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
630 	: m6809_base_device(mconfig, tag, owner, clock, M6809, 1)
631 {
632 }
633