From 454505e5f18fc9491fc099919ff7909f3abc8c8f Mon Sep 17 00:00:00 2001 From: zeromus Date: Sun, 27 Feb 2011 11:40:08 +0000 Subject: [PATCH] [NES] add p1 controls. theyre hardcoded right now and need to go through the normal controls config system. but i think nes will be a more amazing exercise of the input configuration system than the other consoles...? --- .../Consoles/Nintendo/NES/NES.cs | 95 ++++++++++++++++++- BizHawk.MultiClient/MainForm.cs | 10 +- 2 files changed, 99 insertions(+), 6 deletions(-) diff --git a/BizHawk.Emulation/Consoles/Nintendo/NES/NES.cs b/BizHawk.Emulation/Consoles/Nintendo/NES/NES.cs index d53630208f..f8fe77f160 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/NES/NES.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/NES/NES.cs @@ -84,6 +84,8 @@ namespace BizHawk.Emulation.Consoles.Nintendo RomInfo romInfo; byte[] ram; + IPortDevice[] ports; + //user configuration int[,] palette; //TBD!! @@ -94,6 +96,16 @@ namespace BizHawk.Emulation.Consoles.Nintendo public byte ReadReg(int addr) { + switch (addr) + { + case 0x4016: + case 0x4017: + return read_joyport(addr); + default: + //Console.WriteLine("read register: {0:x4}", addr); + break; + + } return 0xFF; } @@ -104,13 +116,31 @@ namespace BizHawk.Emulation.Consoles.Nintendo void WriteReg(int addr, byte val) { - //Console.WriteLine("wrote register: {0:x4} = {1:x2}", addr,val); switch (addr) { case 0x4014: Exec_OAMDma(val); break; + case 0x4016: + ports[0].Write(val & 1); + ports[1].Write(val & 1); + break; + default: + //Console.WriteLine("wrote register: {0:x4} = {1:x2}", addr, val); + break; } } + byte read_joyport(int addr) + { + //read joystick port + //many todos here + if (addr == 0x4016) + { + byte ret = ports[0].Read(); + return ret; + } + else return 0; + } + void Exec_OAMDma(byte val) { ushort addr = (ushort)(val << 8); @@ -195,7 +225,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo new ControllerDefinition { Name = "NES Controls", - BoolButtons = { "A","B","SELECT","START","LEFT","UP","DOWN","RIGHT", "RESET" } + BoolButtons = { "A","B","Select","Start","Left","Up","Down","Right", "Reset" } }; public ControllerDefinition ControllerDefinition { get { return NESController; } } @@ -219,6 +249,64 @@ namespace BizHawk.Emulation.Consoles.Nintendo cpu.Execute(cycles); } + interface IPortDevice + { + void Write(int value); + byte Read(); + void Update(); + } + + //static INPUTC GPC = { ReadGP, 0, StrobeGP, UpdateGP, 0, 0, LogGP, LoadGP }; + class JoypadPortDevice : NullPortDevice + { + int state; + NES nes; + public JoypadPortDevice(NES nes) + { + this.nes = nes; + } + void Strobe() + { + value = 0; + foreach (string str in new string[] { "Right", "Left", "Down", "Up", "Start", "Select", "B", "A" }) + { + value <<= 1; + value |= nes.Controller.IsPressed(str) ? 1 : 0; + } + } + public override void Write(int value) + { + if (state == 1 && value == 0) + Strobe(); + state = value; + } + public override byte Read() + { + int ret = value&1; + value >>= 1; + return (byte)ret; + } + public override void Update() + { + + } + int value; + } + + class NullPortDevice : IPortDevice + { + public virtual void Write(int value) + { + } + public virtual byte Read() + { + return 0xFF; + } + public virtual void Update() + { + } + } + public void HardReset() { cpu = new MOS6502(); @@ -226,6 +314,9 @@ namespace BizHawk.Emulation.Consoles.Nintendo cpu.WriteMemory = WriteMemory; ppu = new PPU(this); ram = new byte[0x800]; + ports = new IPortDevice[2]; + ports[0] = new JoypadPortDevice(this); + ports[1] = new NullPortDevice(); //fceux uses this technique, which presumably tricks some games into thinking the memory is randomized for (int i = 0; i < 0x800; i++) diff --git a/BizHawk.MultiClient/MainForm.cs b/BizHawk.MultiClient/MainForm.cs index 71259f5894..c439615390 100644 --- a/BizHawk.MultiClient/MainForm.cs +++ b/BizHawk.MultiClient/MainForm.cs @@ -320,10 +320,12 @@ namespace BizHawk.MultiClient var NESControls = new Controller(NES.NESController); NESControls.BindMulti("B", "Z"); NESControls.BindMulti("A", "X"); - NESControls.BindMulti("UP", "UpArrow"); - NESControls.BindMulti("DOWN", "DownArrow"); - NESControls.BindMulti("LEFT", "LeftArrow"); - NESControls.BindMulti("RIGHT", "RightArrow"); + NESControls.BindMulti("Up", "UpArrow"); + NESControls.BindMulti("Down", "DownArrow"); + NESControls.BindMulti("Left", "LeftArrow"); + NESControls.BindMulti("Right", "RightArrow"); + NESControls.BindMulti("Select", "E"); + NESControls.BindMulti("Start", "R"); Global.NESControls = NESControls; }