Example #1
0
        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);
        }
Example #2
0
 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
     };
 }
Example #3
0
        public void pchl(State state)
        {
            state.cycleCount += 5;

            state.memory.pc = state.registers.HL;
        }
Example #4
0
        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);
        }
Example #5
0
        public void pop(State state)
        {
            state.cycleCount += 10;

            int dst = (state.currentOpcode >> 4) & 0x03;
            ushort value = state.stack.Pop();

            state.registers.WriteWord(dst, value);
        }
Example #6
0
        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;
        }
Example #7
0
        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;
        }
Example #8
0
 public void cnc(State state)
 {
     if (!state.registers.GetFlag(Flag.CARRY))
     {
         call(state);
     }
     else
     {
         state.memory.pc += 2;
         state.cycleCount += 11;
     }
 }
Example #9
0
        public void lhld(State state)
        {
            state.cycleCount += 16;

            ushort address = state.memory.ReadWord();
            ushort value = state.memory.ReadWordAt(address);

            state.registers.HL = value;
        }
Example #10
0
        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;

            }
        }
Example #11
0
 public void ei(State state)
 {
     state.cycleCount += 4;
     state.registers.F |= Flag.INTERRUPT;
 }
Example #12
0
        public void sta(State state)
        {
            state.cycleCount += 13;
            ushort address = state.memory.ReadWord();

            state.memory.WriteByte(address, state.registers.A);
        }
Example #13
0
        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;
            }
        }
Example #14
0
        public void lda(State state)
        {
            state.cycleCount += 13;
            ushort address = state.memory.ReadWord();
            byte value = state.memory.ReadByteAt(address);

            state.registers.A = value;
        }
Example #15
0
        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;
        }
Example #16
0
        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;
        }
Example #17
0
        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);
        }
Example #18
0
        public void jc(State state)
        {
            state.cycleCount += 10;

            ushort address = state.memory.ReadWord();

            if (state.registers.GetFlag(Flag.CARRY))
            {
                state.memory.pc = address;
            }
        }
Example #19
0
        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;
        }
Example #20
0
        /*
        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));
            }
        }
Example #21
0
 public void cz(State state)
 {
     if (state.registers.GetFlag(Flag.ZERO))
     {
         call(state);
     }
     else
     {
         state.memory.pc += 2;
         state.cycleCount += 11;
     }
 }
Example #22
0
 public void rnz(State state)
 {
     if (!state.registers.GetFlag(Flag.ZERO))
     {
         state.cycleCount += 1;
         ret(state);
     }
     else
     {
         state.cycleCount += 5;
     }
 }
Example #23
0
        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);
            }
        }   
Example #24
0
 public void stc(State state)
 {
     state.cycleCount += 4;
     state.registers.F |= Flag.CARRY;
 }
Example #25
0
        public void shld(State state)
        {
            state.cycleCount += 16;

            ushort address = state.memory.ReadWord();
            state.memory.WriteWord(address, state.registers.HL);
        }
Example #26
0
        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);
        }
Example #27
0
        public void cma(State state)
        {
            state.cycleCount += 4;

            state.registers.A = (byte) ~(state.registers.A);
        }
Example #28
0
        public void xthl(State state)
        {
            state.cycleCount += 18;

            ushort value = state.stack.Pop();
            state.stack.Push(state.registers.HL);
            state.registers.HL = value;
        }
Example #29
0
 public void rc(State state)
 {
     if (state.registers.GetFlag(Flag.CARRY))
     {
         state.cycleCount += 1;
         ret(state);
     }
     else
     {
         state.cycleCount += 5;
     }
 }
Example #30
0
        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);
        }