/*
 * $Id$
 */

#include <stdio.h>
#include "ucode.h"

extern unsigned long cycles;
extern unsigned int md;
extern unsigned int dispatch_memory[2048];
extern int u_pc;
extern int spc_stack[32];
extern int spc_stack_ptr;

int dispatch_test;

void
lashup_poll(void)
{
	int i;
	char line[64];

	printf("lashup> "); fflush(stdout);
	fgets(line, sizeof(line), stdin);
	
	switch (line[0]) {
	case 'd':
		printf("dispatch test\n");
		for (i = 0; i < 2048; i++)
			dispatch_memory[i] = i;
		dispatch_test = 1;
		if (line[1] == 'n')
			dispatch_test = 2;
		break;
	case 'q':
		exit(1);
	}
}

void
lashup_start(int *pp0_pc, ucw_t *pp0, int *pp1_pc, ucw_t *pp1, int *pu_pc, int *ptrace)
{
	unsigned long long ir;

	if (dispatch_test) {
		int n_bit, p_bit, r_bit, n_inst;
		int daddr, dvalue;
		*ptrace = 1;

		printf("\ncycles %lu, pc0 %o, pc1 %o, u_pc %o\n", cycles, *pp0_pc, *pp1_pc, *pu_pc);
		md = -(cycles + 1);
		md = -1;

		/*
		 * NPR
		 * 000 %Jump-Branch-Xct-Next
		 * 001 %Jump-Return-Xct-Next
		 * 010 %Jump-Call-Xct-Next
		 * 011 %Jump-Nop
		 * 100 %Jump-Branch
		 * 101 %Jump-Return
		 * 110 %Jump-Call
		 * 111 %Jump-Skip
		 */

		ir = 0;
		daddr = -1;
		r_bit = p_bit = n_bit = 0;
		n_inst = 0;
		if (dispatch_test > 1)
			n_inst = 1;

		switch (cycles) {
		case 0: ir = (010000LL << 32) | (0124000LL << 16) | 0; break;
		case 1: if (*pp1_pc != 0) printf("new pc %o, should be %o\n", *pp1_pc, 0); break;

		case 3: ir = (010000LL << 32) | (0124000LL << 16) | 040; break;
		case 4: if (*pp1_pc != 1) printf("new pc %o, should be %o\n", *pp1_pc, 1); break;

		case 6: ir = (010000LL << 32) | (0124000LL << 16) | 0100; break;
		case 7: if (*pp1_pc != 3) printf("new pc %o, should be %o\n", *pp1_pc, 3); break;

		case 9:
			ir = (010000LL << 32) | (0124000LL << 16) | 0;
			p_bit = 1; daddr = 0;
			break;
		case 10: printf("spc[%o] = %o\n", spc_stack_ptr, spc_stack[spc_stack_ptr]); break;

		case 12:
			ir = (010000LL << 32) | (0124000LL << 16) | 0;
			r_bit = 1; daddr = 0;
			break;
		case 13: printf("spc[%o] = %o\n", spc_stack_ptr, spc_stack[spc_stack_ptr]); break;

		case 15:
			ir = (010000LL << 32) | (0124000LL << 16) | 0;
			n_bit = 1; daddr = 0;
			break;
		case 16: printf("spc[%o] = %o\n", spc_stack_ptr, spc_stack[spc_stack_ptr]); break;

		case 18:
			ir = (010000LL << 32) | (0124000LL << 16) | 0;
			p_bit = 1; r_bit = 1; daddr = 0;
			break;
		case 19: printf("spc[%o] = %o\n", spc_stack_ptr, spc_stack[spc_stack_ptr]); break;

		case 21:
			ir = (010000LL << 32) | (0124000LL << 16) | 0;
			n_bit = 1; r_bit = 1; daddr = 0;
			break;
		case 22: printf("spc[%o] = %o\n", spc_stack_ptr, spc_stack[spc_stack_ptr]); break;

		case 24:
			ir = (010000LL << 32) | (0124000LL << 16) | 0;
			n_bit = 1; p_bit = 1; daddr = 0;
			break;
		case 25: printf("spc[%o] = %o\n", spc_stack_ptr, spc_stack[spc_stack_ptr]); break;

		case 27:
			ir = (010000LL << 32) | (0124000LL << 16) | 0;
			n_bit = 1; p_bit = 1; r_bit = 1; daddr = 0;
			break;
		case 28: printf("spc[%o] = %o\n", spc_stack_ptr, spc_stack[spc_stack_ptr]); break;
		}

		if (daddr >= 0) {
			dvalue =
				(n_bit ? (1 << 14) : 0) |
				(p_bit ? (1 << 15) : 0) |
				(r_bit ? (1 << 16) : 0);
			dispatch_memory[daddr] = dvalue;
		}

		if (ir) {
			if (n_inst) ir |= 1 << 25;
			if (r_bit) {
				*pp0_pc = 0200;
				*pp1_pc = 0201; 
				*pu_pc = 0202; 
			} else {
				*pp0_pc = 0100;
				*pp1_pc = 0101; 
				*pu_pc = 0102; 
			}
			*ptrace = 1;

		} else {
			/* nop */
			ir = 00000000000010000;
			*ptrace = 0;
		}

		*pp0 = ir;
		*pp1 = 00000000000010000;
	}
}

