static void Main(string[] args) { state = new State(); state.LoadRom(@"c:\invaders"); Thread emulatorThread = new Thread(new ThreadStart(state.Run)); emulatorThread.Start(); mainWindow = new MainWindow(); Application.Run(mainWindow); }
public Registers(State s) : this() { state = s; state.parityTable = new int[] { 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 }; }
public void pchl(State state) { state.cycleCount += 5; state.memory.pc = state.registers.HL; }
public void cmp(State state) { state.cycleCount += 4; int src = (state.currentOpcode & 0x7); int result = state.registers.A - state.registers.ReadByte(src); state.registers.SetFlags((byte)Flag.SIGN | (byte)Flag.ZERO | (byte)Flag.PARITY | (byte)Flag.ACARRY | (byte)Flag.CARRY, state.registers.A, result); }
public void pop(State state) { state.cycleCount += 10; int dst = (state.currentOpcode >> 4) & 0x03; ushort value = state.stack.Pop(); state.registers.WriteWord(dst, value); }
public void ori(State state) { state.cycleCount += 7; byte value = state.memory.ReadByte(); int result = state.registers.A | value; state.registers.SetFlags((byte)Flag.SIGN | (byte)Flag.ZERO | (byte)Flag.PARITY | (byte)Flag.ACARRY | (byte)Flag.CARRY, state.registers.A, result); state.registers.A = (byte)result; }
public void sbi(State state) { state.cycleCount += 7; int result; byte value = state.memory.ReadByte(); if(state.registers.GetFlag(Flag.CARRY)) { result = state.registers.A - (value - 1); } else { result = state.registers.A - value; } state.registers.SetFlags((byte)Flag.SIGN | (byte)Flag.ZERO | (byte)Flag.PARITY | (byte)Flag.ACARRY | (byte)Flag.CARRY, state.registers.A, result); state.registers.A = (byte) result; }
public void cnc(State state) { if (!state.registers.GetFlag(Flag.CARRY)) { call(state); } else { state.memory.pc += 2; state.cycleCount += 11; } }
public void lhld(State state) { state.cycleCount += 16; ushort address = state.memory.ReadWord(); ushort value = state.memory.ReadWordAt(address); state.registers.HL = value; }
public void output(State state) { state.cycleCount += 10; byte port = state.memory.ReadByte(); switch (port) { case 2: state.port2 = state.registers.A; break; case 3: if (((state.registers.A & (1 << 3)) > 0) && ((state.port3o & (1 << 3)) == 0)) { //state.soundInvaderKilled.Play(); } state.port3o = state.registers.A; break; case 4: state.port4LO = state.port4HI; state.port4HI = state.registers.A; break; case 5: break; case 6: // Watchdog for reset, not implemented. break; default: state.DumpState(); Console.WriteLine("Unhandled OUT {0}", port); Console.ReadLine(); break; } }
public void ei(State state) { state.cycleCount += 4; state.registers.F |= Flag.INTERRUPT; }
public void sta(State state) { state.cycleCount += 13; ushort address = state.memory.ReadWord(); state.memory.WriteByte(address, state.registers.A); }
public void input(State state) { state.cycleCount += 10; byte port = state.memory.ReadByte(); switch (port) { case 1: state.registers.A = (byte)state.inp1; break; case 2: state.registers.A = (byte)state.inp2; break; case 3: state.registers.A = (byte)((((state.port4HI << 8) | state.port4LO) << state.port2) >> 8); break; default: break; } }
public void lda(State state) { state.cycleCount += 13; ushort address = state.memory.ReadWord(); byte value = state.memory.ReadByteAt(address); state.registers.A = value; }
public void adc(State state) { state.cycleCount += 4; int src = (state.currentOpcode & 0x7); byte value = state.registers.ReadByte(src); int result = state.registers.A + value + (state.registers.F & (byte)Flag.CARRY); state.registers.SetFlags((byte)Flag.SIGN | (byte)Flag.ZERO | (byte)Flag.PARITY | (byte)Flag.ACARRY | (byte)Flag.CARRY, value, result); state.registers.A = (byte)result; }
public void rrc(State state) { state.cycleCount += 4; if((state.registers.A & 1) != 0) { state.registers.F |= Flag.CARRY; } else { state.registers.F &= unchecked((byte)~Flag.CARRY); } int result = (state.registers.A >> 1) | ((state.registers.F & Flag.CARRY) << 7 ); state.registers.A = (byte)result; }
public void dcx(State state) { state.cycleCount += 5; int dst = (state.currentOpcode >> 4) & 0x03; int value = state.registers.ReadWord(dst); int result = value - 1; state.registers.WriteWord(dst, (ushort) result); }
public void jc(State state) { state.cycleCount += 10; ushort address = state.memory.ReadWord(); if (state.registers.GetFlag(Flag.CARRY)) { state.memory.pc = address; } }
public void rlc(State state) { state.cycleCount += 4; if (((int)state.registers.A & (1 << 7)) != 0) { state.registers.F |= Flag.CARRY; } else { state.registers.F &= unchecked((byte)~Flag.CARRY); } int result = (state.registers.A << 1) | (state.registers.F & Flag.CARRY); state.registers.A = (byte)result; }
/* This BCD stuff is like black magic to me, implemented by looking at similar emulators. */ public void daa(State state) { state.cycleCount += 4; int top4 = (state.registers.A >> 4) & 0xf; int bot4 = (state.registers.A & 0xf); if ((bot4 > 9) || state.registers.GetFlag(Flag.ACARRY)) { state.registers.SetFlags((byte)Flag.SIGN | (byte)Flag.ZERO | (byte)Flag.PARITY | (byte)Flag.ACARRY | (byte)Flag.CARRY, state.registers.A, state.registers.A + 6); state.registers.A += 6; top4 = (state.registers.A >> 4) & 0xF; bot4 = (state.registers.A & 0xf); } if((top4 > 9) || state.registers.GetFlag(Flag.CARRY)) { top4 += 6; state.registers.SetFlags((byte)Flag.SIGN | (byte)Flag.ZERO | (byte)Flag.PARITY | (byte)Flag.ACARRY | (byte)Flag.CARRY, state.registers.A, (((top4 << 4) | bot4))); state.registers.A = (byte)(((top4 << 4) | bot4)); } }
public void cz(State state) { if (state.registers.GetFlag(Flag.ZERO)) { call(state); } else { state.memory.pc += 2; state.cycleCount += 11; } }
public void rnz(State state) { if (!state.registers.GetFlag(Flag.ZERO)) { state.cycleCount += 1; ret(state); } else { state.cycleCount += 5; } }
public void rar(State state) { state.cycleCount += 4; int temp = state.registers.A; state.registers.A >>= 1; if(state.registers.GetFlag(Flag.CARRY)) { state.registers.A |= 0x80; } if((temp & 1) > 0) { state.registers.F |= Flag.CARRY; } else { state.registers.F &= unchecked((byte)~Flag.CARRY); } }
public void stc(State state) { state.cycleCount += 4; state.registers.F |= Flag.CARRY; }
public void shld(State state) { state.cycleCount += 16; ushort address = state.memory.ReadWord(); state.memory.WriteWord(address, state.registers.HL); }
public void dad(State state) { state.cycleCount += 10; int src = (state.currentOpcode >> 4) & 0x03; int dst = state.registers.ReadWord(2); int value = state.registers.ReadWord(src); int result = dst + value; if (result > 0xffff) { state.registers.F |= Flag.CARRY; } else { state.registers.F &= unchecked((byte)~Flag.CARRY); } state.registers.WriteWord(2, (ushort)result); }
public void cma(State state) { state.cycleCount += 4; state.registers.A = (byte) ~(state.registers.A); }
public void xthl(State state) { state.cycleCount += 18; ushort value = state.stack.Pop(); state.stack.Push(state.registers.HL); state.registers.HL = value; }
public void rc(State state) { if (state.registers.GetFlag(Flag.CARRY)) { state.cycleCount += 1; ret(state); } else { state.cycleCount += 5; } }
public void xchg(State state) { state.cycleCount += 5; ushort hl = state.registers.ReadWord(2); ushort de = state.registers.ReadWord(1); state.registers.WriteWord(2, de); state.registers.WriteWord(1, hl); }