summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/cpu/t11/t11.c
blob: c7219d1a1dbbd6bd2905bd5d0ac3bc99b01ae552 (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
/*** t11: Portable DEC T-11 emulator ******************************************

    Copyright Aaron Giles

    System dependencies:    long must be at least 32 bits
                            word must be 16 bit unsigned int
                            byte must be 8 bit unsigned int
                            long must be more than 16 bits
                            arrays up to 65536 bytes must be supported
                            machine must be twos complement

*****************************************************************************/

#include "debugger.h"
#include "t11.h"


/*************************************
 *
 *  Internal state representation
 *
 *************************************/

typedef struct _t11_state t11_state;
struct _t11_state
{
	PAIR				ppc;	/* previous program counter */
    PAIR				reg[8];
    PAIR				psw;
    UINT16				initial_pc;
    UINT8				wait_state;
    UINT8				irq_state;
    int					icount;
	cpu_irq_callback 	irq_callback;
	const device_config *device;
	const address_space *program;
};



/*************************************
 *
 *  Macro shortcuts
 *
 *************************************/

/* registers of various sizes */
#define REGD(x) reg[x].d
#define REGW(x) reg[x].w.l
#define REGB(x) reg[x].b.l

/* PC, SP, and PSW definitions */
#define SP 		REGW(6)
#define PC 		REGW(7)
#define SPD 	REGD(6)
#define PCD 	REGD(7)
#define PSW 	psw.b.l



/*************************************
 *
 *  Low-level memory operations
 *
 *************************************/

INLINE int ROPCODE(t11_state *cpustate)
{
	int val = memory_decrypted_read_word(cpustate->program, cpustate->PC);
	cpustate->PC += 2;
	return val;
}


INLINE int RBYTE(t11_state *cpustate, int addr)
{
	return T11_RDMEM(cpustate, addr);
}


INLINE void WBYTE(t11_state *cpustate, int addr, int data)
{
	T11_WRMEM(cpustate, addr, data);
}


INLINE int RWORD(t11_state *cpustate, int addr)
{
	return T11_RDMEM_WORD(cpustate, addr & 0xfffe);
}


INLINE void WWORD(t11_state *cpustate, int addr, int data)
{
	T11_WRMEM_WORD(cpustate, addr & 0xfffe, data);
}



/*************************************
 *
 *  Low-level stack operations
 *
 *************************************/

INLINE void PUSH(t11_state *cpustate, int val)
{
	cpustate->SP -= 2;
	WWORD(cpustate, cpustate->SPD, val);
}


INLINE int POP(t11_state *cpustate)
{
	int result = RWORD(cpustate, cpustate->SPD);
	cpustate->SP += 2;
	return result;
}



/*************************************
 *
 *  Flag definitions and operations
 *
 *************************************/

/* flag definitions */
#define CFLAG 1
#define VFLAG 2
#define ZFLAG 4
#define NFLAG 8

/* extracts flags */
#define GET_C (cpustate->PSW & CFLAG)
#define GET_V (cpustate->PSW & VFLAG)
#define GET_Z (cpustate->PSW & ZFLAG)
#define GET_N (cpustate->PSW & NFLAG)

/* clears flags */
#define CLR_C (cpustate->PSW &= ~CFLAG)
#define CLR_V (cpustate->PSW &= ~VFLAG)
#define CLR_Z (cpustate->PSW &= ~ZFLAG)
#define CLR_N (cpustate->PSW &= ~NFLAG)

/* sets flags */
#define SET_C (cpustate->PSW |= CFLAG)
#define SET_V (cpustate->PSW |= VFLAG)
#define SET_Z (cpustate->PSW |= ZFLAG)
#define SET_N (cpustate->PSW |= NFLAG)



/*************************************
 *
 *  Interrupt handling
 *
 *************************************/

struct irq_table_entry
{
	UINT8	priority;
	UINT8	vector;
};

static const struct irq_table_entry irq_table[] =
{
	{ 0<<5, 0x00 },
	{ 4<<5, 0x38 },
	{ 4<<5, 0x34 },
	{ 4<<5, 0x30 },
	{ 5<<5, 0x5c },
	{ 5<<5, 0x58 },
	{ 5<<5, 0x54 },
	{ 5<<5, 0x50 },
	{ 6<<5, 0x4c },
	{ 6<<5, 0x48 },
	{ 6<<5, 0x44 },
	{ 6<<5, 0x40 },
	{ 7<<5, 0x6c },
	{ 7<<5, 0x68 },
	{ 7<<5, 0x64 },
	{ 7<<5, 0x60 }
};

static void t11_check_irqs(t11_state *cpustate)
{
	const struct irq_table_entry *irq = &irq_table[cpustate->irq_state & 15];
	int priority = cpustate->PSW & 0xe0;

	/* compare the priority of the interrupt to the PSW */
	if (irq->priority > priority)
	{
		int vector = irq->vector;
		int new_pc, new_psw;

		/* call the callback; if we don't get -1 back, use the return value as our vector */
		if (cpustate->irq_callback != NULL)
		{
			int new_vector = (*cpustate->irq_callback)(cpustate->device, cpustate->irq_state & 15);
			if (new_vector != -1)
				vector = new_vector;
		}

		/* fetch the new PC and PSW from that vector */
		assert((vector & 3) == 0);
		new_pc = RWORD(cpustate, vector);
		new_psw = RWORD(cpustate, vector + 2);

		/* push the old state, set the new one */
		PUSH(cpustate, cpustate->PSW);
		PUSH(cpustate, cpustate->PC);
		cpustate->PCD = new_pc;
		cpustate->PSW = new_psw;
		t11_check_irqs(cpustate);

		/* count cycles and clear the WAIT flag */
		cpustate->icount -= 114;
		cpustate->wait_state = 0;
	}
}



/*************************************
 *
 *  Core opcodes
 *
 *************************************/

/* includes the static function prototypes and the master opcode table */
#include "t11table.c"

/* includes the actual opcode implementations */
#include "t11ops.c"



/*************************************
 *
 *  Fetch current context into buffer
 *
 *************************************/

static CPU_GET_CONTEXT( t11 ) { }



/*************************************
 *
 *  Retrieve context from buffer
 *
 *************************************/

static CPU_SET_CONTEXT( t11 ) { }



/*************************************
 *
 *  Low-level initialization/cleanup
 *
 *************************************/

static CPU_INIT( t11 )
{
	static const UINT16 initial_pc[] =
	{
		0xc000, 0x8000, 0x4000, 0x2000,
		0x1000, 0x0000, 0xf600, 0xf400
	};
	const struct t11_setup *setup = device->static_config;
	t11_state *cpustate = device->token;

	cpustate->initial_pc = initial_pc[setup->mode >> 13];
	cpustate->irq_callback = irqcallback;
	cpustate->device = device;
	cpustate->program = memory_find_address_space(device, ADDRESS_SPACE_PROGRAM);

	state_save_register_device_item(device, 0, cpustate->ppc.w.l);
	state_save_register_device_item(device, 0, cpustate->reg[0].w.l);
	state_save_register_device_item(device, 0, cpustate->reg[1].w.l);
	state_save_register_device_item(device, 0, cpustate->reg[2].w.l);
	state_save_register_device_item(device, 0, cpustate->reg[3].w.l);
	state_save_register_device_item(device, 0, cpustate->reg[4].w.l);
	state_save_register_device_item(device, 0, cpustate->reg[5].w.l);
	state_save_register_device_item(device, 0, cpustate->reg[6].w.l);
	state_save_register_device_item(device, 0, cpustate->reg[7].w.l);
	state_save_register_device_item(device, 0, cpustate->psw.w.l);
	state_save_register_device_item(device, 0, cpustate->initial_pc);
	state_save_register_device_item(device, 0, cpustate->wait_state);
	state_save_register_device_item(device, 0, cpustate->irq_state);
}



/*************************************
 *
 *  CPU reset
 *
 *************************************/

static CPU_RESET( t11 )
{
	t11_state *cpustate = device->token;

	/* initial SP is 376 octal, or 0xfe */
	cpustate->SP = 0x00fe;

	/* initial PC comes from the setup word */
	cpustate->PC = cpustate->initial_pc;

	/* PSW starts off at highest priority */
	cpustate->PSW = 0xe0;

	/* initialize the IRQ state */
	cpustate->irq_state = 0;

	/* reset the remaining state */
	cpustate->REGD(0) = 0;
	cpustate->REGD(1) = 0;
	cpustate->REGD(2) = 0;
	cpustate->REGD(3) = 0;
	cpustate->REGD(4) = 0;
	cpustate->REGD(5) = 0;
	cpustate->ppc.d = 0;
	cpustate->wait_state = 0;
}



/*************************************
 *
 *  Interrupt handling
 *
 *************************************/

static void set_irq_line(t11_state *cpustate, int irqline, int state)
{
	/* set the appropriate bit */
	if (state == CLEAR_LINE)
		cpustate->irq_state &= ~(1 << irqline);
	else
		cpustate->irq_state |= 1 << irqline;
}



/*************************************
 *
 *  Core execution
 *
 *************************************/

static CPU_EXECUTE( t11 )
{
	t11_state *cpustate = device->token;

	cpustate->icount = cycles;
	t11_check_irqs(cpustate);

	if (cpustate->wait_state)
	{
		cpustate->icount = 0;
		goto getout;
	}

	do
	{
		UINT16 op;
		
		cpustate->ppc = cpustate->reg[7];	/* copy PC to previous PC */

		debugger_instruction_hook(device, cpustate->PCD);

		op = ROPCODE(cpustate);
		(*opcode_table[op >> 3])(cpustate, op);

	} while (cpustate->icount > 0);

getout:

	return cycles - cpustate->icount;
}



/**************************************************************************
 * Generic set_info
 **************************************************************************/

static CPU_SET_INFO( t11 )
{
	t11_state *cpustate = device->token;

	switch (state)
	{
		/* --- the following bits of info are set as 64-bit signed integers --- */
		case CPUINFO_INT_INPUT_STATE + T11_IRQ0:		set_irq_line(cpustate, T11_IRQ0, info->i);		break;
		case CPUINFO_INT_INPUT_STATE + T11_IRQ1:		set_irq_line(cpustate, T11_IRQ1, info->i);		break;
		case CPUINFO_INT_INPUT_STATE + T11_IRQ2:		set_irq_line(cpustate, T11_IRQ2, info->i);		break;
		case CPUINFO_INT_INPUT_STATE + T11_IRQ3:		set_irq_line(cpustate, T11_IRQ3, info->i);		break;

		case CPUINFO_INT_PC:
		case CPUINFO_INT_REGISTER + T11_PC:				cpustate->PC = info->i; 				 		break;
		case CPUINFO_INT_SP:
		case CPUINFO_INT_REGISTER + T11_SP:				cpustate->SP = info->i;							break;
		case CPUINFO_INT_REGISTER + T11_PSW:			cpustate->PSW = info->i;						break;
		case CPUINFO_INT_REGISTER + T11_R0:				cpustate->REGW(0) = info->i;					break;
		case CPUINFO_INT_REGISTER + T11_R1:				cpustate->REGW(1) = info->i;					break;
		case CPUINFO_INT_REGISTER + T11_R2:				cpustate->REGW(2) = info->i;					break;
		case CPUINFO_INT_REGISTER + T11_R3:				cpustate->REGW(3) = info->i;					break;
		case CPUINFO_INT_REGISTER + T11_R4:				cpustate->REGW(4) = info->i;					break;
		case CPUINFO_INT_REGISTER + T11_R5:				cpustate->REGW(5) = info->i;					break;
	}
}



/**************************************************************************
 * Generic get_info
 **************************************************************************/

CPU_GET_INFO( t11 )
{
	t11_state *cpustate = (device != NULL) ? device->token : NULL;
	
	switch (state)
	{
		/* --- the following bits of info are returned as 64-bit signed integers --- */
		case CPUINFO_INT_CONTEXT_SIZE:					info->i = sizeof(t11_state);			break;
		case CPUINFO_INT_INPUT_LINES:					info->i = 4;							break;
		case CPUINFO_INT_DEFAULT_IRQ_VECTOR:			info->i = -1;							break;
		case CPUINFO_INT_ENDIANNESS:					info->i = ENDIANNESS_LITTLE;					break;
		case CPUINFO_INT_CLOCK_MULTIPLIER:				info->i = 1;							break;
		case CPUINFO_INT_CLOCK_DIVIDER:					info->i = 1;							break;
		case CPUINFO_INT_MIN_INSTRUCTION_BYTES:			info->i = 2;							break;
		case CPUINFO_INT_MAX_INSTRUCTION_BYTES:			info->i = 6;							break;
		case CPUINFO_INT_MIN_CYCLES:					info->i = 12;							break;
		case CPUINFO_INT_MAX_CYCLES:					info->i = 110;							break;

		case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_PROGRAM:	info->i = 16;					break;
		case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_PROGRAM: info->i = 16;					break;
		case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_PROGRAM: info->i = 0;					break;
		case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_DATA:	info->i = 0;					break;
		case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_DATA: 	info->i = 0;					break;
		case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_DATA: 	info->i = 0;					break;
		case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_IO:		info->i = 0;					break;
		case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_IO: 		info->i = 0;					break;
		case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_IO: 		info->i = 0;					break;

		case CPUINFO_INT_INPUT_STATE + T11_IRQ0:		info->i = (cpustate->irq_state & 1) ? ASSERT_LINE : CLEAR_LINE; break;
		case CPUINFO_INT_INPUT_STATE + T11_IRQ1:		info->i = (cpustate->irq_state & 2) ? ASSERT_LINE : CLEAR_LINE; break;
		case CPUINFO_INT_INPUT_STATE + T11_IRQ2:		info->i = (cpustate->irq_state & 4) ? ASSERT_LINE : CLEAR_LINE; break;
		case CPUINFO_INT_INPUT_STATE + T11_IRQ3:		info->i = (cpustate->irq_state & 8) ? ASSERT_LINE : CLEAR_LINE; break;

		case CPUINFO_INT_PREVIOUSPC:					info->i = cpustate->ppc.w.l;			break;

		case CPUINFO_INT_PC:
		case CPUINFO_INT_REGISTER + T11_PC:				info->i = cpustate->PCD;				break;
		case CPUINFO_INT_SP:
		case CPUINFO_INT_REGISTER + T11_SP:				info->i = cpustate->SPD;				break;
		case CPUINFO_INT_REGISTER + T11_PSW:			info->i = cpustate->PSW;				break;
		case CPUINFO_INT_REGISTER + T11_R0:				info->i = cpustate->REGD(0);			break;
		case CPUINFO_INT_REGISTER + T11_R1:				info->i = cpustate->REGD(1);			break;
		case CPUINFO_INT_REGISTER + T11_R2:				info->i = cpustate->REGD(2);			break;
		case CPUINFO_INT_REGISTER + T11_R3:				info->i = cpustate->REGD(3);			break;
		case CPUINFO_INT_REGISTER + T11_R4:				info->i = cpustate->REGD(4);			break;
		case CPUINFO_INT_REGISTER + T11_R5:				info->i = cpustate->REGD(5);			break;

		/* --- the following bits of info are returned as pointers to data or functions --- */
		case CPUINFO_PTR_SET_INFO:						info->setinfo = CPU_SET_INFO_NAME(t11);			break;
		case CPUINFO_PTR_GET_CONTEXT:					info->getcontext = CPU_GET_CONTEXT_NAME(t11);	break;
		case CPUINFO_PTR_SET_CONTEXT:					info->setcontext = CPU_SET_CONTEXT_NAME(t11);	break;
		case CPUINFO_PTR_INIT:							info->init = CPU_INIT_NAME(t11);				break;
		case CPUINFO_PTR_RESET:							info->reset = CPU_RESET_NAME(t11);				break;
		case CPUINFO_PTR_EXECUTE:						info->execute = CPU_EXECUTE_NAME(t11);			break;
		case CPUINFO_PTR_DISASSEMBLE:					info->disassemble = CPU_DISASSEMBLE_NAME(t11);	break;
		case CPUINFO_PTR_INSTRUCTION_COUNTER:			info->icount = &cpustate->icount;				break;

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case CPUINFO_STR_NAME:							strcpy(info->s, "T11");					break;
		case CPUINFO_STR_CORE_FAMILY:					strcpy(info->s, "DEC T-11");			break;
		case CPUINFO_STR_CORE_VERSION:					strcpy(info->s, "1.0");					break;
		case CPUINFO_STR_CORE_FILE:						strcpy(info->s, __FILE__);				break;
		case CPUINFO_STR_CORE_CREDITS:					strcpy(info->s, "Copyright Aaron Giles"); break;

		case CPUINFO_STR_FLAGS:
			sprintf(info->s, "%c%c%c%c%c%c%c%c",
				cpustate->psw.b.l & 0x80 ? '?':'.',
				cpustate->psw.b.l & 0x40 ? 'I':'.',
				cpustate->psw.b.l & 0x20 ? 'I':'.',
				cpustate->psw.b.l & 0x10 ? 'T':'.',
				cpustate->psw.b.l & 0x08 ? 'N':'.',
				cpustate->psw.b.l & 0x04 ? 'Z':'.',
				cpustate->psw.b.l & 0x02 ? 'V':'.',
				cpustate->psw.b.l & 0x01 ? 'C':'.');
			break;

		case CPUINFO_STR_REGISTER + T11_PC:				sprintf(info->s, "PC:%04X", cpustate->reg[7].w.l); break;
		case CPUINFO_STR_REGISTER + T11_SP:				sprintf(info->s, "SP:%04X", cpustate->reg[6].w.l); break;
		case CPUINFO_STR_REGISTER + T11_PSW:			sprintf(info->s, "PSW:%02X", cpustate->psw.b.l);   break;
		case CPUINFO_STR_REGISTER + T11_R0:				sprintf(info->s, "R0:%04X", cpustate->reg[0].w.l); break;
		case CPUINFO_STR_REGISTER + T11_R1:				sprintf(info->s, "R1:%04X", cpustate->reg[1].w.l); break;
		case CPUINFO_STR_REGISTER + T11_R2:				sprintf(info->s, "R2:%04X", cpustate->reg[2].w.l); break;
		case CPUINFO_STR_REGISTER + T11_R3:				sprintf(info->s, "R3:%04X", cpustate->reg[3].w.l); break;
		case CPUINFO_STR_REGISTER + T11_R4:				sprintf(info->s, "R4:%04X", cpustate->reg[4].w.l); break;
		case CPUINFO_STR_REGISTER + T11_R5:				sprintf(info->s, "R5:%04X", cpustate->reg[5].w.l); break;
	}
}