// File: /workspace/github_c_makefile_star2_O2/calmopyrin[P]tedplay/Tedmem.cpp
// Function: TED::ted_process(short*, unsigned int)
void TED::ted_process(short *buffer, unsigned int count)
{
    do {
        switch(++beamx) {

            default:
                break;

            case 2:
                if (VertSubActive)
                	VertSubCount = (VertSubCount+1)&7;
                break;

            case 3:
				if (AttribFetch && beamy==0) {
					VertSubCount = 7;
				}
				break;

             case 4:
                 if (AttribFetch) {
					BadLine |= ((vshift)&7) == (beamy&7);
					if ( BadLine ) {
						ClockingState = THALT1;
					} else
                 		ClockingState = TSS;
                 	if (beamy==203) {
						AttribFetch = false;
						if (!(BadLine & 2)) ClockingState = TSS;
					}
				}
                break;

            case 8:
				HBlanking = false;
				break;

            case 10:
				if (VertSubActive)
					CharacterPosition = CharacterPositionReload;
                break;

            case 16:
				if (ScreenOn) {
					SideBorderFlipFlop = true;
					memset( scrptr, mcol[0], hshift);
					if (nrwscr)
						CharacterWindow = true;
					x = 0;
				}
				if (BadLine & 1)
					DoDMA(tmpClrbuf, 0);
				if (BadLine & 2)
					DoDMA(chrbuf, (BadLine & 1) ? 0 : 0x400);
                break;

			case 18:
				if (ScreenOn && !nrwscr) {
					CharacterWindow = true;
				}
				break;

			case 89:
				if (/*VertSubActive &&*/ VertSubCount == 6)
					CharacterCount = (CharacterCount + 40)&0x3FF;
				break;

            case 90:
    			if ( VertSubActive && VertSubCount == 7 )
					CharacterPositionReload = (CharacterPosition + 40)&0x3FF;
				break;

            case 91:
		    	ClockingState = TRFSH;
		    	break;

			case 94:
				if (ScreenOn && !nrwscr)
  					SideBorderFlipFlop = CharacterWindow = false;
				break;

  			case 96:
				if (ScreenOn && nrwscr)
  					SideBorderFlipFlop = CharacterWindow = false;
				// FIXME this breaks on FF1E writes
				if (BadLine & 1) {
					// swap DMA fetch pointers for colour DMA...
					unsigned char *tmpbuf = clrbuf;
					clrbuf = tmpClrbuf;
					tmpClrbuf = tmpbuf;
				}
  			    break;

			case 111:
				if ((BadLine&1)) {
    			    BadLine = 2;
					VertSubActive = true;
				} else if (BadLine&2) {// in the second bad line, we're finished...
   					BadLine &= ~2;
				}
				break;

            case 102:
		    	ClockingState = fastmode ? TDS : TSS;
		    	break;

		    case 104:
				HBlanking = true;
				break;

		    case 107: // HSYNC start
				break;

			case 256:
			case 128:
				beamx = 15;
				break;
			case 114: // HSYNC end
    			beamx=0;
    			// the beam reached a new line
    			TVScanLineCounter += 1;
    			if ( TVScanLineCounter >= 340 ) {
					TVScanLineCounter = 0;
					scrptr=screen;
//					render_ok=true;
				}
    			switch (++beamy) {

    			    case 4:
    			        if (!fltscr) ScreenOn = AttribFetch;
    			        break;

    			    case 8:
						if (fltscr) ScreenOn = AttribFetch;
    			        break;

					case 200:
						if (fltscr) ScreenOn = false;
						break;

    			    case 204:
						if (!fltscr) ScreenOn = false;
						break;

    			    case 205:
						CharacterCount = 0;
					    // cursor phase counter in TED register $1F
						VertSubActive = false;
						if ((++crsrphase&0x0F) == 0x0F)
    			        	crsrblinkon ^= 1;
    			        break;

					case 251:
						VBlanking = true;
						break;

    			    case 261: // Vertical retrace
						// frame ready...
						//render_ok=true;
						// reset screen pointer ("TV" electron beam)
						scrptr=screen;
						TVScanLineCounter = 0;
						break;

					case 271:
						VBlanking = false;
						break;

					case 512:
					case 312:
					    beamy = 0;
					    CharacterPosition = CharacterPositionReload = 0;
					    AttribFetch = (Ram[0xFF06]&0x10) != 0;
				}
				// is there raster interrupt?
				if (beamy == irqline)
					Ram[0xFF09] |= Ram[0xFF0A]&0x02 ? 0x82 : 0x02;
		}

		if (beamx&1) {	// perform these only in every second cycle
			if (t2on && !((timer2--)&0xFFFF)) {// Timer2 permitted
				timer2=0xFFFF;
				Ram[0xFF09] |= Ram[0xFF0A]&0x10 ? 0x90 : 0x10; // interrupt
			}
			if (t3on && !((timer3--)&0xFFFF)) {// Timer3 permitted
				timer3=0xFFFF;
				Ram[0xFF09] |= Ram[0xFF0A]&0x40 ? 0xC0 : 0x40; // interrupt
			}
			if (!CharacterWindow && !HBlanking && !VBlanking) {
				// we are on the border area, so use the frame color
				*((int*)(scrptr+4)) = framecol;
			}
			if (scrptr != endptr)
				scrptr+=8;
		} else {
			if (t1on && !timer1--) { // Timer1 permitted decreased and zero
				timer1=(t1start-1)&0xFFFF;
				Ram[0xFF09] |= Ram[0xFF0A]&0x08 ? 0x88 : 0x08; // interrupt
			}

			if (!(HBlanking |VBlanking)) {
				if (SideBorderFlipFlop) { // drawing the visible part of the screen
					// call the relevant rendering function
					//switch (scrattr) {
					//	case 0:
					//		hi_text();
					//		break;
					//	case REVERSE :
					//		rv_text();
					//		break;
					//	case MULTICOLOR|REVERSE :
					//	    mc_text_rvs();
					//	    break;
					//	case MULTICOLOR :
					//		mc_text();
					//		break;
					//	case EXTCOLOR|REVERSE :
					//	case EXTCOLOR :
					//		ec_text();
					//		break;
					//	case GRAPHMODE|REVERSE :
					//	case GRAPHMODE :
					//		hi_bitmap();
					//		break;
					//	case EXTCOLOR|MULTICOLOR :
					//	case GRAPHMODE|EXTCOLOR :
					//	case GRAPHMODE|MULTICOLOR|EXTCOLOR :
					//	case REVERSE|MULTICOLOR|EXTCOLOR :
					//	case GRAPHMODE|MULTICOLOR|EXTCOLOR|REVERSE :
					//		mcec();
					//		break;
					//	case GRAPHMODE|MULTICOLOR :
					//	case GRAPHMODE|MULTICOLOR|REVERSE :
					//		mc_bitmap();
					//		break;
					//	default:
				 //   		illegalbank();
				 //   		break;
					//}
					x = (x + 1) & 0x3F;
				}
				if (!CharacterWindow) {
					// we are on the border area, so use the frame color
					*((int*)scrptr) = framecol;
				}
			}

			CycleCounter++;
			if (!(CycleCounter & 0x03) && buffer && int(count) > 0) {
				static unsigned int remainder = 0;
				unsigned int samples = (playbackSpeed + remainder) / 4;
				if (samples) {
					renderSound(samples, buffer);
					if (sidCard) {
						short imBuf[128];
						sidCard->calcSamples(imBuf, samples);
						unsigned int i = samples - 1;
						do {
							buffer[i] += (short)(int(imBuf[i]) * int(masterVolume) / 1000);
						}while (i--);
					}
					storeToBuffer(buffer, samples);
				}
				remainder = (playbackSpeed + remainder) % 4;
				count -= samples;
				buffer += samples;
			}
		}
		if (aligned_write) {
			*aw_addr_ptr = aw_value;
			aligned_write = false;
		}
		switch (ClockingState|(beamx&1)) {
        	case TRFSH|1:
 	    	case TSS|1:
 	    	case TDS|1:
       		case TDS:
     	    	cpuptr->process();
     	    	break;
      		case THALT1|1:
  	    	case THALT2|1:
        	case THALT3|1:
            	cpuptr->stopcycle();
            	ClockingState<<=1;
            	break;
		}

	} while (int(count) > 0 && buffer);

	render_ok = false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r14
leaq 0x824(%rdi), %rax
movq %rax, 0x8(%rsp)
pushq $0x47
popq %r12
movl 0x81c(%r14), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x81c(%r14)
leal -0x1(%rax), %ecx
cmpl $0x10, %ecx
jbe 0xaea4
leal -0x58(%rax), %ecx
cmpl $0xf, %ecx
ja 0xaed7
leaq 0x4803(%rip), %rdx      # 0xf678
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
cmpl $0x6, 0x1a613(%rip)     # 0x25498
jne 0xb373
movl 0x1a633(%rip), %eax     # 0x254c4
addl $0x28, %eax
andl $0x3ff, %eax            # imm = 0x3FF
movl %eax, 0x1a625(%rip)     # 0x254c4
jmp 0xb373
leaq 0x4789(%rip), %rdx      # 0xf634
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
cmpb $0x1, 0x1a5ed(%rip)     # 0x254a8
jne 0xb373
movl 0x1a5d1(%rip), %eax     # 0x25498
incl %eax
andl $0x7, %eax
movl %eax, 0x1a5c6(%rip)     # 0x25498
jmp 0xb373
cmpl $0x6e, %eax
je 0xb15e
cmpl $0x71, %eax
je 0xb182
cmpl $0x7f, %eax
je 0xaef9
cmpl $0xff, %eax
jne 0xb373
movl $0xf, 0x81c(%r14)
jmp 0xb373
movb $0x0, 0x1a5c9(%rip)     # 0x254d9
jmp 0xb373
cmpb $0x1, 0x1a5a0(%rip)     # 0x254bc
jne 0xb373
movl 0x820(%r14), %ecx
movl 0xb6020(%r14), %eax
xorl %ecx, %eax
xorl %edx, %edx
testb $0x7, %al
sete %dl
movl 0x1a579(%rip), %eax     # 0x254b8
orl %eax, %edx
movl %edx, 0x1a571(%rip)     # 0x254b8
pushq $0x10
popq %rdx
pushq $0x2
popq %rsi
cmovel %esi, %edx
movl %edx, 0x1a56a(%rip)     # 0x254c0
cmpl $0xcb, %ecx
jne 0xb373
movb $0x0, 0x1a553(%rip)     # 0x254bc
testb $0x2, %al
jne 0xb373
movl $0x2, 0x1a545(%rip)     # 0x254c0
jmp 0xb373
cmpb $0x1, 0x1a535(%rip)     # 0x254bc
jne 0xb373
cmpl $0x0, 0x820(%r14)
jne 0xb373
movl $0x7, 0x1a4f3(%rip)     # 0x25498
jmp 0xb373
cmpb $0x1, 0x1a4f7(%rip)     # 0x254a8
jne 0xb373
movl 0x1a4f3(%rip), %eax     # 0x254b0
movl %eax, 0x1a4e9(%rip)     # 0x254ac
jmp 0xb373
cmpb $0x1, 0x1a50b(%rip)     # 0x254da
jne 0xb00b
movb $0x1, 0x1a4dc(%rip)     # 0x254b4
movq 0xb6160(%r14), %rdi
movl 0xb601c(%r14), %edx
movzbl 0xb618a(%r14), %esi
callq 0x3130
cmpl $0x0, 0xb6024(%r14)
je 0xb004
movb $0x1, 0x1a4d7(%rip)     # 0x254db
andl $0x0, 0x1a4d1(%rip)     # 0x254dc
movl 0x1a4a7(%rip), %edx     # 0x254b8
testb $0x1, %dl
je 0xb02d
movq 0xb6100(%r14), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0xb6ca
movl 0x1a48b(%rip), %edx     # 0x254b8
testb $0x2, %dl
je 0xb373
movq 0xb60f0(%r14), %rsi
notl %edx
andl $0x1, %edx
shll $0xa, %edx
movq %r14, %rdi
callq 0xb6ca
jmp 0xb373
cmpb $0x1, 0x1a481(%rip)     # 0x254da
jne 0xb373
cmpl $0x0, 0xb6024(%r14)
jne 0xb373
movb $0x1, 0x1a467(%rip)     # 0x254db
jmp 0xb373
cmpb $0x1, 0x1a45a(%rip)     # 0x254da
jne 0xb373
cmpl $0x0, 0xb6024(%r14)
jne 0xb373
xorl %eax, %eax
movb %al, 0x1a43f(%rip)      # 0x254db
movb %al, 0x1a412(%rip)      # 0x254b4
jmp 0xb373
movl $0x8, 0x1a40f(%rip)     # 0x254c0
jmp 0xb373
cmpb $0x1, 0x1a3eb(%rip)     # 0x254a8
jne 0xb373
cmpl $0x7, 0x1a3ce(%rip)     # 0x25498
jne 0xb373
movl 0x1a3d6(%rip), %eax     # 0x254ac
addl $0x28, %eax
andl $0x3ff, %eax            # imm = 0x3FF
movl %eax, 0x1a3cc(%rip)     # 0x254b0
jmp 0xb373
cmpb $0x1, 0x1a3ea(%rip)     # 0x254da
jne 0xb10a
cmpl $0x0, 0xb6024(%r14)
je 0xb10a
xorl %eax, %eax
movb %al, 0x1a3d7(%rip)      # 0x254db
movb %al, 0x1a3aa(%rip)      # 0x254b4
testb $0x1, 0x1a3a7(%rip)     # 0x254b8
je 0xb373
movdqu 0xb60f8(%r14), %xmm0
pshufd $0x4e, %xmm0, %xmm0     # xmm0 = xmm0[2,3,0,1]
movdqu %xmm0, 0xb60f8(%r14)
jmp 0xb373
xorl %eax, %eax
cmpl $0x0, 0xb6180(%r14)
setne %al
leal 0x2(,%rax,2), %eax
movl %eax, 0x1a373(%rip)     # 0x254c0
jmp 0xb373
movb $0x1, 0x1a380(%rip)     # 0x254d9
jmp 0xb373
movl 0x1a354(%rip), %eax     # 0x254b8
testb $0x1, %al
jne 0xb255
testb $0x2, %al
je 0xb373
andl $-0x4, %eax
movl %eax, 0x1a33b(%rip)     # 0x254b8
jmp 0xb373
andl $0x0, 0x81c(%r14)
movl 0x1a350(%rip), %eax     # 0x254e0
incl %eax
movl %eax, 0x1a348(%rip)     # 0x254e0
cmpl $0x154, %eax            # imm = 0x154
jb 0xb1b2
andl $0x0, 0x1a33a(%rip)     # 0x254e0
movq 0x8(%rsp), %rax
movq %rax, 0xb6160(%r14)
movl 0x820(%r14), %ecx
leal 0x1(%rcx), %eax
movl %eax, 0x820(%r14)
cmpl $0x1ff, %ecx            # imm = 0x1FF
je 0xb26b
cmpl $0x7, %ecx
je 0xb2b7
cmpl $0xc7, %ecx
je 0xb33a
cmpl $0xcb, %ecx
je 0xb2e7
cmpl $0xcc, %ecx
je 0xb306
cmpl $0xfa, %ecx
je 0xb2f8
cmpl $0x104, %ecx            # imm = 0x104
je 0xb29a
cmpl $0x10e, %ecx            # imm = 0x10E
je 0xb2d9
cmpl $0x137, %ecx            # imm = 0x137
je 0xb26b
cmpl $0x3, %ecx
jne 0xb350
cmpl $0x0, 0xb6028(%r14)
pushq $0x4
popq %rax
jne 0xb350
movb 0x1a274(%rip), %al      # 0x254bc
movb %al, 0x1a28c(%rip)      # 0x254da
pushq $0x4
jmp 0xb2d6
movl $0x2, 0x1a259(%rip)     # 0x254b8
movb $0x1, 0x1a242(%rip)     # 0x254a8
jmp 0xb373
andl $0x0, 0x820(%r14)
andl $0x0, 0x1a236(%rip)     # 0x254b0
andl $0x0, 0x1a22b(%rip)     # 0x254ac
movb 0x55ec6(%r14), %al
shrb $0x4, %al
andb $0x1, %al
movb %al, 0x1a229(%rip)      # 0x254bc
xorl %eax, %eax
jmp 0xb350
movq 0x8(%rsp), %rax
movq %rax, 0xb6160(%r14)
andl $0x0, 0x1a233(%rip)     # 0x254e0
movl $0x105, %eax            # imm = 0x105
jmp 0xb350
cmpl $0x0, 0xb6028(%r14)
pushq $0x8
popq %rax
je 0xb350
movb 0x1a1ee(%rip), %al      # 0x254bc
movb %al, 0x1a206(%rip)      # 0x254da
pushq $0x8
popq %rax
jmp 0xb350
movb $0x0, 0x1a204(%rip)     # 0x254e4
movl $0x10f, %eax            # imm = 0x10F
jmp 0xb350
movl $0xcc, %eax
cmpl $0x0, 0xb6028(%r14)
jne 0xb350
jmp 0xb349
movb $0x1, 0x1a1e5(%rip)     # 0x254e4
movl $0xfb, %eax
jmp 0xb350
andl $0x0, 0x1a1b7(%rip)     # 0x254c4
movb $0x0, 0x1a194(%rip)     # 0x254a8
movl 0x45fac(%r14), %ecx
incl %ecx
movl %ecx, 0x45fac(%r14)
notl %ecx
movl $0xcd, %eax
testb $0xf, %cl
jne 0xb350
xorb $0x1, 0x45fb0(%r14)
jmp 0xb350
movl $0xc8, %eax
cmpl $0x0, 0xb6028(%r14)
je 0xb350
movb $0x0, 0x1a18a(%rip)     # 0x254da
cmpl 0xb6184(%r14), %eax
jne 0xb373
testb $0x2, 0x55eca(%r14)
movl $0x82, %eax
pushq $0x2
popq %rcx
cmovel %ecx, %eax
orb %al, 0x55ec9(%r14)
testb $0x1, 0x81c(%r14)
jne 0xb4d3
cmpb $0x1, 0x65fc0(%r14)
jne 0xb3c2
subl $0x1, 0x65fc4(%r14)
jae 0xb3c2
movl 0x65fd0(%r14), %eax
decl %eax
movzwl %ax, %eax
movl %eax, 0x65fc4(%r14)
testb $0x8, 0x55eca(%r14)
movl $0x88, %eax
pushq $0x8
popq %rcx
cmovel %ecx, %eax
orb %al, 0x55ec9(%r14)
cmpb $0x0, 0x1a110(%rip)     # 0x254d9
jne 0xb407
testb $0x1, 0x1a112(%rip)     # 0x254e4
jne 0xb407
cmpb $0x1, 0x1a0d9(%rip)     # 0x254b4
jne 0xb3ee
movl 0x1a0f9(%rip), %eax     # 0x254dc
incl %eax
andl $0x3f, %eax
movl %eax, 0x1a0ee(%rip)     # 0x254dc
cmpb $0x0, 0x1a0e6(%rip)     # 0x254db
jne 0xb407
movl 0xb6138(%r14), %eax
movq 0xb6160(%r14), %rcx
movl %eax, (%rcx)
movq 0x1a07a(%rip), %rax     # 0x25488
incq %rax
movq %rax, 0x1a070(%rip)     # 0x25488
testb $0x3, %al
jne 0xb5a4
testq %r15, %r15
je 0xb5a4
testl %ebx, %ebx
jle 0xb5a4
movl 0x1a0b1(%rip), %eax     # 0x254e8
addl 0xb71b8(%r14), %eax
movl %eax, %r13d
shrl $0x2, %r13d
movl %r13d, %ebp
cmpl $0x4, %eax
jb 0xb4be
movq %r14, %rdi
movl %r13d, %esi
movq %r15, %rdx
callq 0xbac4
movq 0xb71d0(%r14), %rdi
testq %rdi, %rdi
je 0xb4a3
movq (%rdi), %rax
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
callq *0x48(%rax)
leaq 0x1a074(%rip), %rax     # 0x254f0
movl (%rax), %ecx
leal -0x1(%r13), %esi
movl %r13d, %edi
movl $0x3e8, %r8d            # imm = 0x3E8
movswl 0x10(%rsp,%rsi,2), %eax
imull %ecx, %eax
cltd
idivl %r8d
addw %ax, (%r15,%rsi,2)
decq %rsi
decl %edi
jne 0xb48b
movq %r14, %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0xba34
movl 0x1a031(%rip), %eax     # 0x254e8
addl 0xb71b8(%r14), %eax
andl $0x3, %eax
movl %eax, 0x1a021(%rip)     # 0x254e8
subl %r13d, %ebx
leaq (%r15,%rbp,2), %r15
jmp 0xb5a4
cmpb $0x1, 0x65fc1(%r14)
jne 0xb518
movl 0x65fc8(%r14), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x65fc8(%r14)
testw %ax, %ax
jne 0xb518
movl $0xffff, 0x65fc8(%r14) # imm = 0xFFFF
testb $0x10, 0x55eca(%r14)
movl $0x90, %eax
pushq $0x10
popq %rcx
cmovel %ecx, %eax
orb %al, 0x55ec9(%r14)
cmpb $0x1, 0x65fc2(%r14)
jne 0xb55d
movl 0x65fcc(%r14), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x65fcc(%r14)
testw %ax, %ax
jne 0xb55d
movl $0xffff, 0x65fcc(%r14) # imm = 0xFFFF
testb $0x40, 0x55eca(%r14)
movl $0xc0, %eax
pushq $0x40
popq %rcx
cmovel %ecx, %eax
orb %al, 0x55ec9(%r14)
cmpb $0x0, 0x19f77(%rip)     # 0x254db
jne 0xb589
cmpb $0x0, 0x19f6c(%rip)     # 0x254d9
jne 0xb589
cmpb $0x0, 0x19f6e(%rip)     # 0x254e4
jne 0xb589
movl 0xb6138(%r14), %eax
movq 0xb6160(%r14), %rcx
movl %eax, 0x4(%rcx)
movq 0xb6160(%r14), %rax
cmpq 0xb6168(%r14), %rax
je 0xb5a4
addq $0x8, %rax
movq %rax, 0xb6160(%r14)
cmpb $0x1, 0x19f21(%rip)     # 0x254cc
jne 0xb5c3
movb 0x19f25(%rip), %al      # 0x254d8
movq 0x19f16(%rip), %rcx     # 0x254d0
movb %al, (%rcx)
movb $0x0, 0x19f09(%rip)     # 0x254cc
movl 0x81c(%r14), %eax
andl $0x1, %eax
orl 0x19eed(%rip), %eax     # 0x254c0
addl $-0x3, %eax
cmpl $0x3e, %eax
ja 0xb611
btq %rax, %r12
jae 0xb5ef
movq 0x45fb8(%r14), %rdi
callq 0x435a
jmp 0xb611
movabsq $0x4000000040004000, %rcx # imm = 0x4000000040004000
btq %rax, %rcx
jae 0xb611
movq 0x45fb8(%r14), %rdi
callq 0x725c
shll 0x19eaf(%rip)           # 0x254c0
testl %ebx, %ebx
jle 0xb61e
testq %r15, %r15
jne 0xae4d
movb $0x0, 0x45fa4(%r14)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq