summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/cpu/i8008/i8008.c
blob: 63f4103fe3fa174a86cb55111529f6809dce4960 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*****************************************************************************
 *
 *   i8008.c
 *
 *   Intel 8008 CPU
 *
 *   Initial version by Miodrag Milanovic
 *
 *****************************************************************************/
#include "emu.h"
#include "debugger.h"
#include "i8008.h"

//**************************************************************************
//  MACROS
//**************************************************************************

#define REG_1					((opcode >> 3) & 7)
#define REG_2					(opcode & 7)
#define GET_PC					(m_ADDR[m_pc_pos])

//**************************************************************************
//  GLOBAL VARIABLES
//**************************************************************************

// device type definition
const device_type I8008 = &device_creator<i8008_device>;

//**************************************************************************
//  DEVICE INTERFACE
//**************************************************************************

//-------------------------------------------------
//  i8008_device - constructor
//-------------------------------------------------
i8008_device::i8008_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: cpu_device(mconfig, I8008, "i8008", tag, owner, clock),
	  m_program_config("program", ENDIANNESS_LITTLE, 8, 14),
	  m_io_config("io", ENDIANNESS_LITTLE, 8, 8),
	  m_program(0),
	  m_direct(0)
{
	// set our instruction counter
	m_icountptr = &m_icount;
};

//-------------------------------------------------
//  device_start - start up the device
//-------------------------------------------------

void i8008_device::device_start()
{
	// find address spaces
	m_program = &space(AS_PROGRAM);
	m_direct = &m_program->direct();
	m_io = &space(AS_IO);

	// save state
	save_item(NAME(m_PC));
	save_item(NAME(m_A));
	save_item(NAME(m_B));
	save_item(NAME(m_C));
	save_item(NAME(m_D));
	save_item(NAME(m_E));
	save_item(NAME(m_H));
	save_item(NAME(m_L));
	save_item(NAME(m_CF));
	save_item(NAME(m_SF));
	save_item(NAME(m_ZF));
	save_item(NAME(m_PF));
	save_item(NAME(m_pc_pos));
	save_item(NAME(m_ADDR[0]));
	save_item(NAME(m_ADDR[1]));
	save_item(NAME(m_ADDR[2]));
	save_item(NAME(m_ADDR[3]));
	save_item(NAME(m_ADDR[4]));
	save_item(NAME(m_ADDR[5]));
	save_item(NAME(m_ADDR[6]));
	save_item(NAME(m_ADDR[7]));
	save_item(NAME(m_HALT));
	save_item(NAME(m_irq_state));

	// register our state for the debugger
	state_add(I8008_PC,       "PC",       m_PC.w.l).mask(0x3fff);
	state_add(STATE_GENPC,    "GENPC",    m_PC.w.l).mask(0x3fff).noshow();
	state_add(STATE_GENFLAGS, "GENFLAGS", m_flags).mask(0x0f).callimport().callexport().noshow().formatstr("%4s");
	state_add(I8008_A,        "A",        m_A);
	state_add(I8008_B,        "B",        m_B);
	state_add(I8008_C,        "C",        m_C);
	state_add(I8008_D,        "D",        m_D);
	state_add(I8008_E,        "E",        m_E);
	state_add(I8008_H,        "H",        m_H);
	state_add(I8008_L,        "L",        m_L);

	astring tempstr;
	for (int addrnum = 0; addrnum < 8; addrnum++)
		state_add(I8008_ADDR1 + addrnum, tempstr.format("ADDR%d", addrnum + 1), m_ADDR[addrnum].w.l).mask(0xfff);

	init_tables();
}

void i8008_device::init_tables (void)
{
	int i;
	UINT8 p;
	for (i = 0; i < 256; i++)
	{
		p = 0;
		if (BIT(i,0)) p++;
		if (BIT(i,1)) p++;
		if (BIT(i,2)) p++;
		if (BIT(i,3)) p++;
		if (BIT(i,4)) p++;
		if (BIT(i,5)) p++;
		if (BIT(i,6)) p++;
		if (BIT(i,7)) p++;
		m_PARITY[i] = ((p&1) ? 0 : 1);
	}
}

//-------------------------------------------------
//  device_reset - reset the device
//-------------------------------------------------

void i8008_device::device_reset()
{
	m_CF = m_SF = m_ZF = m_PF = 0;
	m_A = m_B = m_C = m_D = m_E = m_H = m_L = 0;
	m_PC.d = 0;
	m_pc_pos = 0;
	m_HALT = 0;
	m_irq_state = CLEAR_LINE;
	memset(m_ADDR,0,sizeof(m_ADDR));
}

//-------------------------------------------------
//  memory_space_config - return the configuration
//  of the specified address space, or NULL if
//  the space doesn't exist
//-------------------------------------------------

const address_space_config *i8008_device::memory_space_config(address_spacenum spacenum) const
{
	return	(spacenum == AS_PROGRAM) ? &m_program_config :
			(spacenum == AS_IO) ? &m_io_config :
			NULL;
}

//-------------------------------------------------
//  state_import - import state into the device,
//  after it has been set
//-------------------------------------------------

void i8008_device::state_import(const device_state_entry &entry)
{
	switch (entry.index())
	{
		case STATE_GENFLAGS:
			m_CF = (m_flags >> 3) & 1;
			m_ZF = (m_flags >> 2) & 1;
			m_SF = (m_flags >> 1) & 1;
			m_PF = (m_flags >> 0) & 1;
			break;
	}
}

//-------------------------------------------------
//  state_export - export state from the device,
//  to a known location where it can be read
//-------------------------------------------------

void i8008_device::state_export(const device_state_entry &entry)
{
	switch (entry.index())
	{
		case STATE_GENFLAGS:
			m_flags = (m_CF ? 0x08 : 0x00) |
							  (m_ZF ? 0x04 : 0x00) |
							  (m_SF ? 0x02 : 0x00) |
							  (m_PF ? 0x01 : 0x00);
			break;
	}
}

//-------------------------------------------------
//  state_string_export - export state as a string
//  for the debugger
//-------------------------------------------------

void i8008_device::state_string_export(const device_state_entry &entry, astring &string)
{
	switch (entry.index())
	{
		case STATE_GENFLAGS:
			string.printf("%c%c%c%c",
				m_CF ? 'C':'.',
				m_ZF ? 'Z':'.',
				m_SF ? 'S':'.',
				m_PF ? 'P':'.');
			break;
	}
}

//-------------------------------------------------
//  disasm_min_opcode_bytes - return the length
//  of the shortest instruction, in bytes
//-------------------------------------------------

UINT32 i8008_device::disasm_min_opcode_bytes() const
{
	return 1;
}

//-------------------------------------------------
//  disasm_max_opcode_bytes - return the length
//  of the longest instruction, in bytes
//-------------------------------------------------

UINT32 i8008_device::disasm_max_opcode_bytes() const
{
	return 3;
}

//-------------------------------------------------
//  disasm_disassemble - call the disassembly
//  helper function
//-------------------------------------------------

offs_t i8008_device::disasm_disassemble(char *buffer, offs_t pc, const UINT8 *oprom, const UINT8 *opram, UINT32 options)
{
	extern CPU_DISASSEMBLE( i8008 );
	return CPU_DISASSEMBLE_NAME(i8008)(NULL, buffer, pc, oprom, opram, 0);
}

//**************************************************************************
//  EXECUTION
//**************************************************************************

//-------------------------------------------------
//  execute_min_cycles - return minimum number of
//  cycles it takes for one instruction to execute
//-------------------------------------------------

UINT32 i8008_device::execute_min_cycles() const
{
	return 8;
}

//-------------------------------------------------
//  execute_max_cycles - return maximum number of
//  cycles it takes for one instruction to execute
//-------------------------------------------------

UINT32 i8008_device::execute_max_cycles() const
{
	return 16;
}

//-------------------------------------------------
//  execute_set_input - set input and IRQ lines
//-------------------------------------------------

void i8008_device::execute_set_input(int inputnum, int state)
{
	m_irq_state = state;
}

//-------------------------------------------------
//  execute_run - execute until our icount expires
//-------------------------------------------------

void i8008_device::execute_run()
{
	do
	{
		if (m_irq_state != CLEAR_LINE) {
			take_interrupt();
		}
		debugger_instruction_hook(this, m_PC.d);
		execute_one(rop());
	} while (m_icount > 0);
}

inline void i8008_device::illegal(UINT8 opcode)
{
	if ((machine().debug_flags & DEBUG_FLAG_ENABLED) != 0)
	{
		logerror("I8008 illegal instruction %04X $%02X\n", m_PC.w.l, opcode);
	}
}

void i8008_device::take_interrupt()
{
	if (m_HALT) {
		GET_PC.w.l = (GET_PC.w.l + 1) & 0x3fff;
		m_PC = GET_PC;
		m_HALT = 0;
	}
	// For now only support one byte operation to be executed
	execute_one(standard_irq_callback(0));
}

inline void i8008_device::execute_one(int opcode)
{
	UINT16 tmp;

	switch (opcode >> 6)
	{
		case 0x03:	// starting with 11
					if (opcode==0xff) {
						// HLT
						m_icount -= 4;
						GET_PC.w.l = GET_PC.w.l - 1;
						m_PC = GET_PC;
						m_HALT = 1;
					} else {
						// Lrr
						m_icount -= 5;
						if (REG_1==7) m_icount -= 2;
						if (REG_2==7) m_icount -= 3;
						set_reg(REG_1, get_reg(REG_2));
					}
					break;
		case 0x00:	// starting with 00
					switch(opcode & 7) {
						case 0 :	if(((opcode >> 3) & 7)==0) {
							        	// HLT
										m_icount -= 4;
										GET_PC.w.l = GET_PC.w.l - 1;
										m_PC = GET_PC;
										m_HALT = 1;
									} else {
										if(((opcode >> 3) & 7)==7) {
											// ILLEGAL
											m_icount -= 5;
											illegal(opcode);
										} else {
											// INr
											m_icount -= 5;
											tmp = get_reg(REG_1) + 1;
											set_reg(REG_1, tmp & 0xff);
											update_flags(tmp & 0xff);
										}
									}
									break;
						case 1 :	if(((opcode >> 3) & 7)==0) {
							        	// HLT
										m_icount -= 4;
										GET_PC.w.l = GET_PC.w.l - 1;
										m_PC = GET_PC;
										m_HALT = 1;
									} else {
										if(((opcode >> 3) & 7)==7) {
											// ILLEGAL
											m_icount -= 5;
											illegal(opcode);
										} else {
											// DCr
											m_icount -= 5;
											tmp = get_reg(REG_1) - 1;
											set_reg(REG_1, tmp & 0xff);
											update_flags(tmp & 0xff);
										}
									}
									break;
						case 2 :	{
										// All instuction from this group have same timing
										m_icount -= 5;
										switch((opcode >> 3) & 7) {
											case 0 :
												// RLC
												tmp = m_A;
												m_A = (m_A << 1) | BIT(tmp,7);
												m_CF = BIT(tmp,7);
												break;
											case 1 :
												// RRC
												tmp = m_A;
												m_A = (m_A >> 1) | (BIT(tmp,0) ? 0x80 : 0x00);
												m_CF = BIT(tmp,0);
												break;
											case 2 :
												// RAL
												tmp = m_A;
												m_A = (m_A << 1) | m_CF;
												m_CF = BIT(tmp,7);
												break;
											case 3 :
												// RAR
												tmp = m_A;
												m_A = (m_A >> 1) | (m_CF ? 0x80 : 0x00);
												m_CF = BIT(tmp,0);
												break;
											default :
												// ILLEGAL
												illegal(opcode);
												break;
										}
									}
									break;
						case 3 :
									// Rcc
									{
										m_icount -= 3;
										if (do_condition(opcode)==1) {
											m_icount -= 2;
											pop_stack();
											m_PC = GET_PC;
										}
									}
									break;
						case 4 :	{
										m_icount -= 8;
										switch((opcode >> 3) & 7) {
											case 0 :
												// ADI
												tmp = get_reg(0) + arg();
												set_reg(0,tmp & 0xff);
												update_flags(tmp & 0xff);
												m_CF = (tmp >> 8) & 1;
												break;
											case 1 :
												// ACI
												tmp = get_reg(0) + arg() + m_CF;
												set_reg(0,tmp & 0xff);
												update_flags(tmp & 0xff);
												m_CF = (tmp >> 8) & 1;
												break;
											case 2 :
												// SUI
												tmp = get_reg(0) - arg();
												set_reg(0,tmp & 0xff);
												update_flags(tmp & 0xff);
												m_CF = (tmp >> 8) & 1;
												break;
											case 3 :
												// SBI
												tmp = get_reg(0) - arg() - m_CF;
												set_reg(0,tmp & 0xff);
												update_flags(tmp & 0xff);
												m_CF = (tmp >> 8) & 1;
												break;
											case 4 :
												// NDI
												tmp = get_reg(0) & arg();
												set_reg(0,tmp & 0xff);
												update_flags(tmp & 0xff);
												m_CF = 0;
												break;
											case 5 :
												// XRI
												tmp = get_reg(0) ^ arg();
												set_reg(0,tmp & 0xff);
												update_flags(tmp & 0xff);
												m_CF = 0;
												break;
											case 6 :
												// ORI
												tmp = get_reg(0) | arg();
												set_reg(0,tmp & 0xff);
												update_flags(tmp & 0xff);
												m_CF = 0;
												break;
											case 7 :
												// CPI
												tmp = get_reg(0) - arg();
												update_flags(tmp & 0xff);
												m_CF = (tmp >> 8) & 1;
												break;
										}
									}
									break;
						case 5 :	// RST
									m_icount -= 5;
									push_stack();
									GET_PC.w.l = opcode & 0x38;
									m_PC = GET_PC;
									break;
						case 6 :	// LrI
									m_icount -= 8;
									if (REG_1==7) m_icount -= 1; // LMI
									set_reg(REG_1, arg());
									break;
						case 7 :	// RET
									m_icount -= 5;
									pop_stack();
									m_PC = GET_PC;
									break;
					}
					break;

		case 0x01:	// starting with 01
					switch(opcode & 7) {
						case 0 :
							// Jcc
							m_icount -= 9;
							tmp = get_addr();
							if (do_condition(opcode)==1) {
								m_icount -= 2;
								GET_PC.w.l = tmp;
								m_PC = GET_PC;
							}
							break;
						case 2 :
							// Ccc
							m_icount -= 9;
							tmp = get_addr();
							if (do_condition(opcode)==1) {
								m_icount -= 2;
								push_stack();
								GET_PC.w.l = tmp;
								m_PC = GET_PC;
							}
							break;
						case 4 :
							// JMP
							m_icount -= 11;
							GET_PC.w.l = get_addr();
							m_PC = GET_PC;
							break;
						case 6 :
							// CAL
							m_icount -= 11;
							tmp = get_addr();
							push_stack();
							GET_PC.w.l = tmp;
							m_PC = GET_PC;
							break;
						default :
							if (((opcode>>4)&3)==0) {
								// INP
								m_icount -= 8;
								m_A = m_io->read_byte((opcode >> 1) & 0x1f);
							} else {
								// OUT
								m_icount -= 6;
								m_io->write_byte((opcode >> 1) & 0x1f, m_A);
							}
							break;
					}
					break;
		case 0x02:	// starting with 10
					m_icount -= 5;
					if ((opcode & 7)==7) m_icount -= 3; // operations with memory
					switch((opcode >> 3) & 7) {
						case 0 :
							// ADx
							tmp = get_reg(0) + get_reg(opcode & 7);
							set_reg(0,tmp & 0xff);
							update_flags(tmp & 0xff);
							m_CF = (tmp >> 8) & 1;
							break;
						case 1 :
							// ACx
							tmp = get_reg(0) + get_reg(opcode & 7) + m_CF;
							set_reg(0,tmp & 0xff);
							update_flags(tmp & 0xff);
							m_CF = (tmp >> 8) & 1;
							break;
						case 2 :
							// SUx
							tmp = get_reg(0) - get_reg(opcode & 7);
							set_reg(0,tmp & 0xff);
							update_flags(tmp & 0xff);
							m_CF = (tmp >> 8) & 1;
							break;
						case 3 :
							// SBx
							tmp = get_reg(0) - get_reg(opcode & 7) - m_CF;
							set_reg(0,tmp & 0xff);
							update_flags(tmp & 0xff);
							m_CF = (tmp >> 8) & 1;
							break;
						case 4 :
							// NDx
							tmp = get_reg(0) & get_reg(opcode & 7);
							set_reg(0,tmp & 0xff);
							update_flags(tmp & 0xff);
							m_CF = 0;
							break;
						case 5 :
							// XRx
							tmp = get_reg(0) ^ get_reg(opcode & 7);
							set_reg(0,tmp & 0xff);
							update_flags(tmp & 0xff);
							m_CF = 0;
							break;
						case 6 :
							// ORx
							tmp = get_reg(0) | get_reg(opcode & 7);
							set_reg(0,tmp & 0xff);
							update_flags(tmp & 0xff);
							m_CF = 0;
							break;
						case 7 :
							// CPx
							tmp = get_reg(0) - get_reg(opcode & 7);
							update_flags(tmp & 0xff);
							m_CF = (tmp >> 8) & 1;
							break;
					}
					break;
	}
}

/***************************************************************************
    INLINE FUNCTIONS
***************************************************************************/

inline void i8008_device::push_stack()
{
	m_pc_pos = (m_pc_pos + 1) & 7;
}

inline void i8008_device::pop_stack()
{
	m_ADDR[m_pc_pos].d = 0;
	m_pc_pos = (m_pc_pos - 1) & 7;
}

inline UINT8 i8008_device::rop()
{
	UINT8 retVal = m_direct->read_decrypted_byte(GET_PC.w.l);
	GET_PC.w.l = (GET_PC.w.l + 1) & 0x3fff;
	m_PC = GET_PC;
	return retVal;
}

inline UINT8 i8008_device::get_reg(UINT8 reg)
{
	UINT8 retVal;
	switch(reg) {
		case 0 : retVal = m_A; break;
		case 1 : retVal = m_B; break;
		case 2 : retVal = m_C; break;
		case 3 : retVal = m_D; break;
		case 4 : retVal = m_E; break;
		case 5 : retVal = m_H; break;
		case 6 : retVal = m_L; break;
		default: retVal = m_program->read_byte((m_H << 8) + m_L); break;
	}
	return retVal;
}

inline void i8008_device::set_reg(UINT8 reg, UINT8 val)
{
	switch(reg) {
		case 0 : m_A = val; break;
		case 1 : m_B = val; break;
		case 2 : m_C = val; break;
		case 3 : m_D = val; break;
		case 4 : m_E = val; break;
		case 5 : m_H = val; break;
		case 6 : m_L = val; break;
		default: m_program->write_byte((m_H << 8) + m_L, val); break;
	}
}

inline UINT8 i8008_device::arg()
{
	UINT8 retVal = m_direct->read_raw_byte(GET_PC.w.l);
	GET_PC.w.l = (GET_PC.w.l + 1) & 0x3fff;
	m_PC = GET_PC;
	return retVal;
}

inline void i8008_device::update_flags(UINT8 val)
{
	m_ZF = (val == 0) ? 1 : 0;
	m_SF = (val & 0x80) ? 1 : 0;
	m_PF = m_PARITY[val];
}

inline UINT8 i8008_device::do_condition(UINT8 val)
{
	UINT8 v = (val >> 5) & 1;
	UINT8 cond = 0;
	switch((val>> 3) & 0x03) {
		case 0 :
				if (m_CF==v) cond = 1;
				break;
		case 1 :
				if (m_ZF==v) cond = 1;
				break;
		case 2 :
				if (m_SF==v) cond = 1;
				break;
		case 3 :
				if (m_PF==v) cond = 1;
				break;
	}
	return cond;
}

inline UINT16 i8008_device::get_addr()
{
	UINT8 lo = arg();
	UINT8 hi = arg();
	return ((hi & 0x3f) << 8) + lo;
}