// File: /workspace/github_c_makefile_star2_O3/calmopyrin[P]tedplay/Tedmem.cpp
// Function: TED::Read(unsigned int)
unsigned char TED::Read(unsigned int addr)
{
	switch ( addr & 0xF000 ) {
		case 0x0000:
			switch ( addr & 0xFFFF ) {
				case 0:
					return prddr;
				case 1:
					{
						unsigned char retval =
							(
							//ReadBus()&
							0xC0)
							//|(tap->ReadCSTIn(CycleCounter)&0x10)
							;
						return (prp&prddr)|(retval&~prddr);
					}
				default:
					return actram[addr&0xFFFF];
			}
			break;
		case 0x1000:
		case 0x2000:
		case 0x3000:
			return actram[addr&0xFFFF];
		case 0x4000:
		case 0x5000:
		case 0x6000:
		case 0x7000:
			return actram[addr&RAMMask];
		case 0x8000:
		case 0x9000:
		case 0xA000:
		case 0xB000:
			return mem_8000_bfff[addr&0x3FFF];
		case 0xC000:
		case 0xD000:
		case 0xE000:
			return mem_c000_ffff[addr&0x3FFF];
		case 0xF000:
			switch ( addr >> 8 ) {
				case 0xFF:
					switch (addr) {
						case 0xFF00 : return timer1&0xFF;
						case 0xFF01 : return timer1>>8;
						case 0xFF02 : return timer2&0xFF;
						case 0xFF03 : return timer2>>8;
						case 0xFF04 : return timer3&0xFF;
						case 0xFF05 : return timer3>>8;
						case 0xFF06 : return Ram[0xFF06];
						case 0xFF07 : return Ram[0xFF07];
						case 0xFF08 :
#if 0
							if (joyemu)
								  return keys->feedjoy(Ram[0xFF08])&(keys->feedkey(Ram[0xFD30]));
							  else
								  return keys->feedkey(Ram[0xFD30]);
#else
							return 0xFF;
#endif
						case 0xFF09 : return Ram[0xFF09]|(0x25);
						case 0xFF0A : return Ram[0xFF0A]|(0xA0);
						case 0xFF0B : return irqline & 0xFF;
						case 0xFF0C : return ((crsrpos>>8)&0x03)|0xFC;
						case 0xFF0D : return crsrpos&0xFF;
						case 0xFF10 : return Ram[0xFF10] | 0x7C;
						case 0xFF12 : return Ram[0xFF12] | 0xC0;
						case 0xFF0E :
						case 0xFF0F :
						case 0xFF11 :
						case 0xFF13 :
							return Ram[addr];
						case 0xFF14 : return Ram[addr]|0x07;	// lowest 3 bits are always high
						case 0xFF15 : return ecol[0]|0x80;	// The highest bit is not used and so always 1
						case 0xFF16 : return ecol[1]|0x80;	// A few games (Rockman) used it...
						case 0xFF17 : return ecol[2]|0x80;
						case 0xFF18 : return ecol[3]|0x80;
						case 0xFF19 : return (framecol&0xFF)|0x80;
						case 0xFF1A : return (CharacterPositionReload>>8)&0xFF;
						case 0xFF1B : return CharacterPositionReload&0xFF;
						case 0xFF1C : return (beamy>>8)|0xFE;
						case 0xFF1D : return beamy&0xFF; /// 1-8. bit of the rasterline counter
						case 0xFF1E : return ((98+beamx)<<1)%228; // raster column
						case 0xFF1F : return 0x80|(crsrphase<<3)|VertSubCount;
						default:
							return mem_c000_ffff[addr&0x3FFF];
					}
					break;
				case 0xFE:
					//return tcbmbus->Read(addr);
					//return 0xFE;
					return protectedPlayerMemory[addr & 0xfff];
				case 0xFD:
					switch (addr>>4) {
						case 0xFD0: // RS232
							return 0xFD;
						case 0xFD1: // User port, PIO & 256 RAM expansion
							//return (tap->IsButtonPressed()<<2)^0xFF;
							return 0xFF;
						case 0xFD2: // Speech hardware
						case 0xFD4: // SID Card
						case 0xFD5:
							if (sidCard) {
								return sidCard->read(addr & 0x1f);
							}
							return 0xFD;
						case 0xFD3:
							return Ram[0xFD30];
					}
					return 0xFD;
				case 0xFC:
					return mem_fc00_fcff[addr&0x3FFF];
				default:
					return mem_c000_ffff[addr&0x3FFF];
			}
	}
	//fprintf(stderr,"Unhandled read %04X\n", addr);
	return 0;
}

// Assembly:
movl %esi, %eax
shrl $0xc, %eax
andl $0xf, %eax
leaq 0x6243(%rip), %rcx      # 0x10520
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0xb6010(%rdi), %rax
andl 0x76008(%rdi), %esi
jmp 0xa3ad
movq 0x75ff0(%rdi), %rax
jmp 0xa3a7
movq 0xb6010(%rdi), %rax
movzwl %si, %ecx
movb (%rax,%rcx), %al
retq
movl %esi, %eax
shrl $0x8, %eax
addl $0xffffff04, %eax       # imm = 0xFFFFFF04
cmpl $0x3, %eax
ja 0xa3a0
leaq 0x6238(%rip), %rcx      # 0x10560
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x76000(%rdi), %rax
jmp 0xa3a7
cmpw $0x1, %si
je 0xa34e
movzwl %si, %eax
testl %eax, %eax
jne 0xa35f
movb 0xb6019(%rdi), %al
retq
movb 0xb6018(%rdi), %al
xorb $-0x40, %al
andb 0xb6019(%rdi), %al
xorb $-0x40, %al
retq
movzwl %si, %eax
movq 0xb6010(%rdi), %rcx
movb (%rcx,%rax), %al
retq
andl $0xfff, %esi            # imm = 0xFFF
movb 0xb61b8(%rdi,%rsi), %al
retq
leal -0xff00(%rsi), %eax
cmpl $0x1f, %eax
ja 0xa3a0
leaq 0x61f7(%rip), %rcx      # 0x10584
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl %esi, %eax
movb 0x45fc0(%rdi,%rax), %al
retq
movq 0x75ff8(%rdi), %rax
andl $0x3fff, %esi           # imm = 0x3FFF
movb (%rax,%rsi), %al
retq
movl %esi, %ecx
shrl $0x4, %ecx
movb $-0x3, %al
addl $0xfffff02f, %ecx       # imm = 0xFFFFF02F
cmpl $0x4, %ecx
ja 0xa311
leaq 0x61a2(%rip), %rdx      # 0x10570
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq 0xb71d0(%rdi), %rdi
testq %rdi, %rdi
je 0xa311
andl $0x1f, %esi
movq (%rdi), %rax
movq 0x38(%rax), %rax
jmpq *%rax
movb $-0x1, %al
retq
movb 0x55cf0(%rdi), %al
retq
movb 0x1c09d(%rip), %al      # 0x264a0
retq
movb 0xb6184(%rdi), %al
retq
movb 0x65fc9(%rdi), %al
retq
movb 0x45fa9(%rdi), %al
orb $-0x4, %al
retq
movb 0x55ec9(%rdi), %al
orb $0x25, %al
retq
movb 0x65fc5(%rdi), %al
retq
movb 0xb6138(%rdi), %al
jmp 0xa4e3
movb 0xb6191(%rdi), %al
jmp 0xa4e3
movb 0x65fc8(%rdi), %al
retq
movb 0x55ec6(%rdi), %al
retq
movb 0x65fc4(%rdi), %al
retq
movb 0x65fcc(%rdi), %al
retq
movb 0x45fa8(%rdi), %al
retq
movb 0x65fcd(%rdi), %al
retq
movb 0x55eca(%rdi), %al
orb $-0x60, %al
retq
movb 0x55ec7(%rdi), %al
retq
movl 0x45fac(%rdi), %eax
shll $0x3, %eax
orl 0x1bffe(%rip), %eax     # 0x26488
jmp 0xa4e3
movb 0x55ed4(%rdi), %al
orb $0x7, %al
retq
movb 0xb618e(%rdi), %al
jmp 0xa4e3
movb 0x55ed0(%rdi), %al
orb $0x7c, %al
retq
movl 0x81c(%rdi), %eax
leal 0xc4(,%rax,2), %eax
movl %eax, %ecx
shrl $0x2, %ecx
imulq $0x8fb823f, %rcx, %rcx  # imm = 0x8FB823F
shrq $0x21, %rcx
imull $0xe4, %ecx, %ecx
subl %ecx, %eax
retq
movb 0xb618f(%rdi), %al
jmp 0xa4e3
movb 0x55ed2(%rdi), %al
orb $-0x40, %al
retq
movb 0xb6190(%rdi), %al
orb $-0x80, %al
retq
movb 0x821(%rdi), %al
orb $-0x2, %al
retq
movb 0x820(%rdi), %al
retq
movb 0x1bfa5(%rip), %al      # 0x264a1
retq
nop