From 065ec618c41ebe88965f280d8bb0f9932b31a5db Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 19 Jun 2016 19:36:34 -0400 Subject: [PATCH] Factored out and templated the 6532, finally taking the opportunity to add a means to control the on-console switches of an Atari 2600. --- Machines/Atari2600/Atari2600.cpp | 12 +++ Machines/Atari2600/Atari2600.hpp | 7 +- Machines/Atari2600/Atari2600Inputs.h | 8 ++ .../Base.lproj/Atari2600Document.xib | 89 +++++++++++++++++++ .../Base.lproj/ElectronDocument.xib | 2 +- .../Documents/Atari2600Document.swift | 22 ++++- .../Machine/Wrappers/CSAtari2600.h | 6 ++ .../Machine/Wrappers/CSAtari2600.mm | 42 +++++++++ 8 files changed, 185 insertions(+), 3 deletions(-) diff --git a/Machines/Atari2600/Atari2600.cpp b/Machines/Atari2600/Atari2600.cpp index 26014eb98..ba56db751 100644 --- a/Machines/Atari2600/Atari2600.cpp +++ b/Machines/Atari2600/Atari2600.cpp @@ -722,6 +722,18 @@ void Machine::set_digital_input(Atari2600DigitalInput input, bool state) } } +void Machine::set_switch_is_enabled(Atari2600Switch input, bool state) +{ + switch(input) { + case Atari2600SwitchReset: _mos6532.update_port_input(1, 0x01, state); break; + case Atari2600SwitchSelect: _mos6532.update_port_input(1, 0x02, state); break; + case Atari2600SwitchColour: _mos6532.update_port_input(1, 0x08, state); break; + case Atari2600SwitchLeftPlayerDifficulty: _mos6532.update_port_input(1, 0x40, state); break; + case Atari2600SwitchRightPlayerDifficulty: _mos6532.update_port_input(1, 0x80, state); break; + } +} + + void Machine::set_rom(size_t length, const uint8_t *data) { _rom_size = 1024; diff --git a/Machines/Atari2600/Atari2600.hpp b/Machines/Atari2600/Atari2600.hpp index a03157e63..baa8dd0b0 100644 --- a/Machines/Atari2600/Atari2600.hpp +++ b/Machines/Atari2600/Atari2600.hpp @@ -59,9 +59,13 @@ class PIA: public MOS::MOS6532 { inline void update_port_input(int port, uint8_t mask, bool set) { - if(set) _portValues[port] |= mask; else _portValues[port] &= ~mask; + if(set) _portValues[port] &= ~mask; else _portValues[port] |= mask; } + PIA() : + _portValues{0xff, 0xff} + {} + private: uint8_t _portValues[2]; @@ -78,6 +82,7 @@ class Machine: public CPU6502::Processor, public CRTMachine::Machine { void switch_region(); void set_digital_input(Atari2600DigitalInput input, bool state); + void set_switch_is_enabled(Atari2600Switch input, bool state); // to satisfy CPU6502::Processor unsigned int perform_bus_operation(CPU6502::BusOperation operation, uint16_t address, uint8_t *value); diff --git a/Machines/Atari2600/Atari2600Inputs.h b/Machines/Atari2600/Atari2600Inputs.h index 780c90ff2..30ec4447c 100644 --- a/Machines/Atari2600/Atari2600Inputs.h +++ b/Machines/Atari2600/Atari2600Inputs.h @@ -27,6 +27,14 @@ typedef enum { Atari2600DigitalInputJoy2Fire, } Atari2600DigitalInput; +typedef enum { + Atari2600SwitchReset, + Atari2600SwitchSelect, + Atari2600SwitchColour, + Atari2600SwitchLeftPlayerDifficulty, + Atari2600SwitchRightPlayerDifficulty +} Atari2600Switch; + #ifdef __cplusplus } #endif diff --git a/OSBindings/Mac/Clock Signal/Base.lproj/Atari2600Document.xib b/OSBindings/Mac/Clock Signal/Base.lproj/Atari2600Document.xib index e1f140fda..7fd9c5d3e 100644 --- a/OSBindings/Mac/Clock Signal/Base.lproj/Atari2600Document.xib +++ b/OSBindings/Mac/Clock Signal/Base.lproj/Atari2600Document.xib @@ -6,7 +6,13 @@ + + + + + + @@ -37,6 +43,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/OSBindings/Mac/Clock Signal/Base.lproj/ElectronDocument.xib b/OSBindings/Mac/Clock Signal/Base.lproj/ElectronDocument.xib index f5c6a8e5a..a74f5d383 100644 --- a/OSBindings/Mac/Clock Signal/Base.lproj/ElectronDocument.xib +++ b/OSBindings/Mac/Clock Signal/Base.lproj/ElectronDocument.xib @@ -1,5 +1,5 @@ - + diff --git a/OSBindings/Mac/Clock Signal/Documents/Atari2600Document.swift b/OSBindings/Mac/Clock Signal/Documents/Atari2600Document.swift index 4cfd1d4da..862f9f3c2 100644 --- a/OSBindings/Mac/Clock Signal/Documents/Atari2600Document.swift +++ b/OSBindings/Mac/Clock Signal/Documents/Atari2600Document.swift @@ -29,7 +29,6 @@ class Atari2600Document: MachineDocument { } // MARK: CSOpenGLViewResponderDelegate - private func inputForKey(event: NSEvent) -> Atari2600DigitalInput? { switch event.keyCode { case 123: return Atari2600DigitalInputJoy1Left @@ -64,4 +63,25 @@ class Atari2600Document: MachineDocument { atari2600.setResetLineEnabled(false) } } + + // MARK: Options + @IBOutlet var resetButton: NSButton! + @IBOutlet var selectButton: NSButton! + @IBOutlet var colourButton: NSButton! + @IBOutlet var leftPlayerDifficultyButton: NSButton! + @IBOutlet var rightPlayerDifficultyButton: NSButton! + + @IBAction func optionDidChange(sender: AnyObject!) { + atari2600.colourButton = colourButton.state == NSOnState + atari2600.leftPlayerDifficultyButton = leftPlayerDifficultyButton.state == NSOnState + atari2600.rightPlayerDifficultyButton = rightPlayerDifficultyButton.state == NSOnState + } + + @IBAction func optionWasPressed(sender: NSButton!) { + if sender == resetButton { + atari2600.pressResetButton() + } else { + atari2600.pressSelectButton() + } + } } \ No newline at end of file diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.h b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.h index 05a0b70c3..4e842c19a 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.h +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.h @@ -15,4 +15,10 @@ - (void)setState:(BOOL)state forDigitalInput:(Atari2600DigitalInput)digitalInput; - (void)setResetLineEnabled:(BOOL)enabled; +@property (nonatomic, assign) BOOL colourButton; +@property (nonatomic, assign) BOOL leftPlayerDifficultyButton; +@property (nonatomic, assign) BOOL rightPlayerDifficultyButton; +- (void)pressResetButton; +- (void)pressSelectButton; + @end diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.mm b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.mm index cec8e85bf..39bb10e8a 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.mm +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.mm @@ -79,4 +79,46 @@ struct CRTDelegate: public Outputs::CRT::Delegate { return &_atari2600; } +#pragma mark - Switches + +- (void)setColourButton:(BOOL)colourButton { + _colourButton = colourButton; + @synchronized(self) { + _atari2600.set_switch_is_enabled(Atari2600SwitchColour, colourButton); + } +} + +- (void)setLeftPlayerDifficultyButton:(BOOL)leftPlayerDifficultyButton { + _leftPlayerDifficultyButton = leftPlayerDifficultyButton; + @synchronized(self) { + _atari2600.set_switch_is_enabled(Atari2600SwitchLeftPlayerDifficulty, leftPlayerDifficultyButton); + } +} + +- (void)setRightPlayerDifficultyButton:(BOOL)rightPlayerDifficultyButton { + _rightPlayerDifficultyButton = rightPlayerDifficultyButton; + @synchronized(self) { + _atari2600.set_switch_is_enabled(Atari2600SwitchRightPlayerDifficulty, rightPlayerDifficultyButton); + } +} + +- (void)toggleSwitch:(Atari2600Switch)toggleSwitch { + @synchronized(self) { + _atari2600.set_switch_is_enabled(toggleSwitch, true); + } + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + @synchronized(self) { + _atari2600.set_switch_is_enabled(toggleSwitch, false); + } + }); +} + +- (void)pressResetButton { + [self toggleSwitch:Atari2600SwitchReset]; +} + +- (void)pressSelectButton { + [self toggleSwitch:Atari2600SwitchSelect]; +} + @end