diff --git a/Machines/Atari2600/Atari2600.cpp b/Machines/Atari2600/Atari2600.cpp index 9260e22e5..6d76cdadb 100644 --- a/Machines/Atari2600/Atari2600.cpp +++ b/Machines/Atari2600/Atari2600.cpp @@ -13,6 +13,8 @@ using namespace Atari2600; namespace { static const unsigned int horizontalTimerPeriod = 228; + static const double NTSC_clock_rate = 1194720; + static const double PAL_clock_rate = 1182298; } Machine::Machine() : @@ -52,6 +54,7 @@ Machine::Machine() : _stateByExtendTime[vbextend][c] = state; } } + set_clock_rate(NTSC_clock_rate); } void Machine::setup_output(float aspect_ratio) @@ -94,8 +97,7 @@ void Machine::switch_region() _is_pal_region = true; _speaker->set_input_rate((float)(get_clock_rate() / 38.0)); - - if(delegate) delegate->machine_did_change_clock_rate(this); + set_clock_rate(PAL_clock_rate); } void Machine::close_output() @@ -109,11 +111,6 @@ Machine::~Machine() close_output(); } -double Machine::get_clock_rate() -{ - return _is_pal_region ? 1182298 : 1194720; -} - void Machine::update_timers(int mask) { unsigned int upcomingPointerPlus4 = (_upcomingEventsPointer + 4)%number_of_upcoming_events; diff --git a/Machines/Atari2600/Atari2600.hpp b/Machines/Atari2600/Atari2600.hpp index 4a1778b14..3b9fa0131 100644 --- a/Machines/Atari2600/Atari2600.hpp +++ b/Machines/Atari2600/Atari2600.hpp @@ -94,7 +94,6 @@ class Machine: public CPU6502::Processor, public CRTMachine::Machine { virtual std::shared_ptr get_crt() { return _crt; } virtual std::shared_ptr get_speaker() { return _speaker; } virtual void run_for_cycles(int number_of_cycles) { CPU6502::Processor::run_for_cycles(number_of_cycles); } - virtual double get_clock_rate(); // TODO: different rate for PAL private: diff --git a/Machines/CRTMachine.hpp b/Machines/CRTMachine.hpp index c193199c4..6e00b482f 100644 --- a/Machines/CRTMachine.hpp +++ b/Machines/CRTMachine.hpp @@ -30,15 +30,26 @@ class Machine { virtual void run_for_cycles(int number_of_cycles) = 0; // TODO: sever the clock-rate stuff. - virtual double get_clock_rate() = 0; + double get_clock_rate() { + return clock_rate_; + } class Delegate { public: virtual void machine_did_change_clock_rate(Machine *machine) = 0; }; - void set_delegate(Delegate *delegate) { this->delegate = delegate; } + void set_delegate(Delegate *delegate) { this->delegate_ = delegate; } protected: - Delegate *delegate; + double clock_rate_; + void set_clock_rate(double clock_rate) { + if(clock_rate_ != clock_rate) { + clock_rate_ = clock_rate; + if(delegate_) delegate_->machine_did_change_clock_rate(this); + } + } + + private: + Delegate *delegate_; }; } diff --git a/Machines/Commodore/Vic-20/Vic20.cpp b/Machines/Commodore/Vic-20/Vic20.cpp index 097d63cfe..555ae488c 100644 --- a/Machines/Commodore/Vic-20/Vic20.cpp +++ b/Machines/Commodore/Vic-20/Vic20.cpp @@ -35,9 +35,34 @@ Machine::Machine() : _tape.set_delegate(this); // establish the memory maps + set_memory_size(MemorySize::Default); + + // set the NTSC clock rate + set_region(NTSC); +// _debugPort.reset(new ::Commodore::Serial::DebugPort); +// _debugPort->set_serial_bus(_serialBus); +// _serialBus->add_port(_debugPort); +} + +void Machine::set_memory_size(MemorySize size) +{ memset(_processorReadMemoryMap, 0, sizeof(_processorReadMemoryMap)); memset(_processorWriteMemoryMap, 0, sizeof(_processorWriteMemoryMap)); + switch(size) + { + default: break; + case ThreeKB: + write_to_map(_processorReadMemoryMap, _expansionRAM, 0x0000, 0x1000); + write_to_map(_processorWriteMemoryMap, _expansionRAM, 0x0000, 0x1000); + break; + case ThirtyTwoKB: + write_to_map(_processorReadMemoryMap, _expansionRAM, 0x0000, 0x8000); + write_to_map(_processorWriteMemoryMap, _expansionRAM, 0x0000, 0x8000); + break; + } + + // install the ROMs and VIC-visible memory write_to_map(_processorReadMemoryMap, _userBASICMemory, 0x0000, sizeof(_userBASICMemory)); write_to_map(_processorReadMemoryMap, _screenMemory, 0x1000, sizeof(_screenMemory)); write_to_map(_processorReadMemoryMap, _colorMemory, 0x9400, sizeof(_colorMemory)); @@ -48,10 +73,6 @@ Machine::Machine() : write_to_map(_processorWriteMemoryMap, _userBASICMemory, 0x0000, sizeof(_userBASICMemory)); write_to_map(_processorWriteMemoryMap, _screenMemory, 0x1000, sizeof(_screenMemory)); write_to_map(_processorWriteMemoryMap, _colorMemory, 0x9400, sizeof(_colorMemory)); - -// _debugPort.reset(new ::Commodore::Serial::DebugPort); -// _debugPort->set_serial_bus(_serialBus); -// _serialBus->add_port(_debugPort); } void Machine::write_to_map(uint8_t **map, uint8_t *area, uint16_t address, uint16_t length) @@ -141,9 +162,26 @@ void Machine::mos6522_did_change_interrupt_status(void *mos6522) #pragma mark - Setup +void Machine::set_region(Commodore::Vic20::Region region) +{ + _region = region; + switch(region) + { + case PAL: + set_clock_rate(1108404); + if(_mos6560) _mos6560->set_output_mode(MOS::MOS6560::OutputMode::PAL); + break; + case NTSC: + set_clock_rate(1022727); + if(_mos6560) _mos6560->set_output_mode(MOS::MOS6560::OutputMode::NTSC); + break; + } +} + void Machine::setup_output(float aspect_ratio) { _mos6560.reset(new Vic6560()); + set_region(_region); memset(_mos6560->_videoMemoryMap, 0, sizeof(_mos6560->_videoMemoryMap)); write_to_map(_mos6560->_videoMemoryMap, _characterROM, 0x0000, sizeof(_characterROM)); @@ -191,9 +229,25 @@ void Machine::add_prg(size_t length, const uint8_t *data) set_typer_for_string("RUN\n"); } - _rom = new uint8_t[length - 2]; - memcpy(_rom, &data[2], length - 2); - write_to_map(_processorReadMemoryMap, _rom, _rom_address, _rom_length); + if(_rom_address == 0xa000) + { + _rom = new uint8_t[length - 2]; + memcpy(_rom, &data[2], length - 2); + write_to_map(_processorReadMemoryMap, _rom, _rom_address, _rom_length); + } + else + { + // TODO: write to virtual media (tape, probably?), load normally. + data += 2; + while(_rom_length) + { + uint8_t *ram = _processorWriteMemoryMap[_rom_address >> 10]; + if(ram) ram[_rom_address & 0x3ff] = *data; + data++; + _rom_length--; + _rom_address++; + } + } } } diff --git a/Machines/Commodore/Vic-20/Vic20.hpp b/Machines/Commodore/Vic-20/Vic20.hpp index 29a1fe3e5..e4fb58386 100644 --- a/Machines/Commodore/Vic-20/Vic20.hpp +++ b/Machines/Commodore/Vic-20/Vic20.hpp @@ -32,6 +32,17 @@ enum ROMSlot { Drive }; +enum MemorySize { + Default, + ThreeKB, + ThirtyTwoKB +}; + +enum Region { + NTSC, + PAL +}; + #define key(line, mask) (((mask) << 3) | (line)) enum Key: uint16_t { @@ -260,6 +271,9 @@ class Machine: _keyboardVIA->set_joystick_state(input, isPressed); } + void set_memory_size(MemorySize size); + void set_region(Region region); + inline void set_use_fast_tape_hack(bool activate) { _use_fast_tape_hack = activate; } inline void set_should_automatically_load_media(bool activate) { _should_automatically_load_media = activate; } @@ -273,7 +287,6 @@ class Machine: virtual std::shared_ptr get_crt() { return _mos6560->get_crt(); } virtual std::shared_ptr get_speaker() { return _mos6560->get_speaker(); } virtual void run_for_cycles(int number_of_cycles) { CPU6502::Processor::run_for_cycles(number_of_cycles); } - virtual double get_clock_rate() { return 1022727; } // TODO: or 1108405 for PAL; see http://www.antimon.org/dl/c64/code/stable.txt // to satisfy MOS::MOS6522::Delegate @@ -291,6 +304,7 @@ class Machine: uint8_t _characterROM[0x1000]; uint8_t _basicROM[0x2000]; uint8_t _kernelROM[0x2000]; + uint8_t _expansionRAM[0x8000]; uint8_t *_rom; uint16_t _rom_address, _rom_length; @@ -305,6 +319,8 @@ class Machine: uint8_t *_processorWriteMemoryMap[64]; void write_to_map(uint8_t **map, uint8_t *area, uint16_t address, uint16_t length); + Region _region; + std::unique_ptr _mos6560; std::shared_ptr _userPortVIA; std::shared_ptr _keyboardVIA; diff --git a/Machines/Electron/Electron.cpp b/Machines/Electron/Electron.cpp index bf07f869d..30c9a0ad4 100644 --- a/Machines/Electron/Electron.cpp +++ b/Machines/Electron/Electron.cpp @@ -55,6 +55,7 @@ Machine::Machine() : memset(_roms[c], 0xff, 16384); _tape.set_delegate(this); + set_clock_rate(2000000); } void Machine::setup_output(float aspect_ratio) diff --git a/Machines/Electron/Electron.hpp b/Machines/Electron/Electron.hpp index d5f593e1a..6ec7b81a2 100644 --- a/Machines/Electron/Electron.hpp +++ b/Machines/Electron/Electron.hpp @@ -162,7 +162,6 @@ class Machine: virtual std::shared_ptr get_crt() { return _crt; } virtual std::shared_ptr get_speaker() { return _speaker; } virtual void run_for_cycles(int number_of_cycles) { CPU6502::Processor::run_for_cycles(number_of_cycles); } - virtual double get_clock_rate() { return 2000000; } // to satisfy Tape::Delegate virtual void tape_did_change_interrupt_status(Tape *tape); diff --git a/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m b/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m index 267ecee96..47507b332 100644 --- a/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m +++ b/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m @@ -174,9 +174,8 @@ static void audioOutputCallback( - (void)enqueueAudioBuffer:(const int16_t *)buffer numberOfSamples:(size_t)lengthInSamples { - while(1) + if([_writeLock tryLockWhenCondition:AudioQueueCanProceed]) { - [_writeLock lockWhenCondition:AudioQueueCanProceed]; if((_audioStreamReadPosition + _streamLength) - _audioStreamWritePosition >= lengthInSamples) { size_t samplesBeforeOverflow = _streamLength - (_audioStreamWritePosition % _streamLength); @@ -193,7 +192,6 @@ static void audioOutputCallback( _audioStreamWritePosition += lengthInSamples; [_writeLock unlockWithCondition:[self writeLockCondition]]; - break; } else { diff --git a/OSBindings/Mac/Clock Signal/Base.lproj/Vic20Document.xib b/OSBindings/Mac/Clock Signal/Base.lproj/Vic20Document.xib index cd45e6cb1..b50cdf757 100644 --- a/OSBindings/Mac/Clock Signal/Base.lproj/Vic20Document.xib +++ b/OSBindings/Mac/Clock Signal/Base.lproj/Vic20Document.xib @@ -6,8 +6,10 @@ + + @@ -44,16 +46,16 @@ - + - + - + + + + + + + + + + + + + + + + + + @@ -87,21 +106,28 @@ + + + + + + + - + diff --git a/OSBindings/Mac/Clock Signal/Documents/Vic20Document.swift b/OSBindings/Mac/Clock Signal/Documents/Vic20Document.swift index 7ad6b8c32..b55a42427 100644 --- a/OSBindings/Mac/Clock Signal/Documents/Vic20Document.swift +++ b/OSBindings/Mac/Clock Signal/Documents/Vic20Document.swift @@ -26,12 +26,6 @@ class Vic20Document: MachineDocument { override init() { super.init() - if let kernel = rom("kernel-ntsc"), basic = rom("basic"), characters = rom("characters-english") { - vic20.setKernelROM(kernel) - vic20.setBASICROM(basic) - vic20.setCharactersROM(characters) - } - if let drive = dataForResource("1540", ofType: "bin", inDirectory: "ROMImages/Commodore1540") { vic20.setDriveROM(drive) } @@ -69,25 +63,121 @@ class Vic20Document: MachineDocument { vic20.setPRG(data) } + // MARK: automatic loading tick box @IBOutlet var loadAutomaticallyButton: NSButton? var autoloadingUserDefaultsKey: String { get { return prefixedUserDefaultsKey("autoload") } } + @IBAction func setShouldLoadAutomatically(sender: NSButton!) { let loadAutomatically = sender.state == NSOnState vic20.shouldLoadAutomatically = loadAutomatically NSUserDefaults.standardUserDefaults().setBool(loadAutomatically, forKey: self.autoloadingUserDefaultsKey) } + + // MARK: country selector + @IBOutlet var countryButton: NSPopUpButton? + var countryUserDefaultsKey: String { + get { return prefixedUserDefaultsKey("country") } + } + + @IBAction func setCountry(sender: NSPopUpButton!) { + NSUserDefaults.standardUserDefaults().setInteger(sender.indexOfSelectedItem, forKey: self.countryUserDefaultsKey) + setCountry(sender.indexOfSelectedItem) + } + + private func setCountry(countryID: Int) { + var charactersROM: String? + var kernelROM: String? + switch countryID { + case 0: // Danish + charactersROM = "characters-danish" + kernelROM = "kernel-danish" + vic20.region = .PAL + case 1: // European + charactersROM = "characters-english" + kernelROM = "kernel-pal" + vic20.region = .PAL + case 2: // Japanese + charactersROM = "characters-japanese" + kernelROM = "kernel-japanese" + vic20.region = .NTSC + case 3: // Swedish + charactersROM = "characters-swedish" + kernelROM = "kernel-swedish" + vic20.region = .PAL + case 4: // US + charactersROM = "characters-english" + kernelROM = "kernel-ntsc" + vic20.region = .NTSC + default: break + } + + if let charactersROM = charactersROM, kernelROM = kernelROM { + if let kernel = rom(kernelROM), basic = rom("basic"), characters = rom(charactersROM) { + vic20.setKernelROM(kernel) + vic20.setBASICROM(basic) + vic20.setCharactersROM(characters) + } + } + } + + // MARK: memory model selector + @IBOutlet var memorySizeButton: NSPopUpButton? + var memorySizeUserDefaultsKey: String { + get { return prefixedUserDefaultsKey("memorySize") } + } + + @IBAction func setMemorySize(sender: NSPopUpButton!) { + var selectedSize: Int? + switch sender.indexOfSelectedItem { + case 0: selectedSize = 5 + case 1: selectedSize = 8 + case 2: selectedSize = 32 + default: break + } + if let selectedSize = selectedSize { + NSUserDefaults.standardUserDefaults().setInteger(selectedSize, forKey: self.memorySizeUserDefaultsKey) + setMemorySize(sender.indexOfSelectedItem) + } + } + private func setMemorySize(sizeIndex: Int) { + switch sizeIndex { + case 2: vic20.memorySize = .Size32Kb + case 1: vic20.memorySize = .Size8Kb + default: vic20.memorySize = .Size5Kb + } + } + + // MARK: option restoration override func establishStoredOptions() { super.establishStoredOptions() let standardUserDefaults = NSUserDefaults.standardUserDefaults() standardUserDefaults.registerDefaults([ - autoloadingUserDefaultsKey: true + self.autoloadingUserDefaultsKey: true, + self.memorySizeUserDefaultsKey: 5, + self.countryUserDefaultsKey: 1 ]) let loadAutomatically = standardUserDefaults.boolForKey(self.autoloadingUserDefaultsKey) vic20.shouldLoadAutomatically = loadAutomatically self.loadAutomaticallyButton?.state = loadAutomatically ? NSOnState : NSOffState + + let memorySize = standardUserDefaults.integerForKey(self.memorySizeUserDefaultsKey) + var indexToSelect: Int? + switch memorySize { + case 32: indexToSelect = 2 + case 8: indexToSelect = 1 + default: indexToSelect = 0 + } + if let indexToSelect = indexToSelect { + self.memorySizeButton?.selectItemAtIndex(indexToSelect) + setMemorySize(indexToSelect) + } + + let country = standardUserDefaults.integerForKey(self.countryUserDefaultsKey) + setCountry(country) + self.countryButton?.selectItemAtIndex(country) } } diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.h b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.h index daaee4c06..c8ade6c34 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.h +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.h @@ -10,6 +10,19 @@ #import "CSKeyboardMachine.h" #import "CSFastLoading.h" +typedef NS_ENUM(NSInteger, CSVic20Region) +{ + CSVic20RegionPAL, + CSVic20RegionNTSC +}; + +typedef NS_ENUM(NSInteger, CSVic20MemorySize) +{ + CSVic20MemorySize5Kb, + CSVic20MemorySize8Kb, + CSVic20MemorySize32Kb, +}; + @interface CSVic20 : CSMachine - (void)setKernelROM:(nonnull NSData *)rom; @@ -24,5 +37,7 @@ @property (nonatomic, assign) BOOL useFastLoadingHack; @property (nonatomic, assign) BOOL shouldLoadAutomatically; +@property (nonatomic, assign) CSVic20Region region; +@property (nonatomic, assign) CSVic20MemorySize memorySize; @end diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.mm b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.mm index 1ad7165f9..5108741a1 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.mm +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSVic20.mm @@ -190,18 +190,38 @@ using namespace Commodore::Vic20; } } +#pragma mark - Public configuration options + - (void)setUseFastLoadingHack:(BOOL)useFastLoadingHack { + _useFastLoadingHack = useFastLoadingHack; @synchronized(self) { - _useFastLoadingHack = useFastLoadingHack; _vic20.set_use_fast_tape_hack(useFastLoadingHack ? true : false); } } - (void)setShouldLoadAutomatically:(BOOL)shouldLoadAutomatically { + _shouldLoadAutomatically = shouldLoadAutomatically; @synchronized(self) { - _shouldLoadAutomatically = shouldLoadAutomatically; _vic20.set_should_automatically_load_media(shouldLoadAutomatically ? true : false); } } +- (void)setRegion:(CSVic20Region)region { + _region = region; + @synchronized(self) { + _vic20.set_region( (region == CSVic20RegionPAL) ? Commodore::Vic20::Region::PAL : Commodore::Vic20::Region::NTSC); + } +} + +- (void)setMemorySize:(CSVic20MemorySize)memorySize { + _memorySize = memorySize; + @synchronized(self) { + switch(memorySize) { + case CSVic20MemorySize5Kb: _vic20.set_memory_size(Commodore::Vic20::Default); break; + case CSVic20MemorySize8Kb: _vic20.set_memory_size(Commodore::Vic20::ThreeKB); break; + case CSVic20MemorySize32Kb: _vic20.set_memory_size(Commodore::Vic20::ThirtyTwoKB); break; + } + } +} + @end diff --git a/ROMImages/Vic20/readme.txt b/ROMImages/Vic20/readme.txt index ce0e24f6b..551904779 100644 --- a/ROMImages/Vic20/readme.txt +++ b/ROMImages/Vic20/readme.txt @@ -3,15 +3,12 @@ ROM files would ordinarily go here; the copyright status of these is uncertain s Expected files: basic.bin -characters-english.bin -kernel-ntsc.bin - -Likely to be desired in the future: - characters-danish.bin +characters-english.bin characters-japanese.bin characters-swedish.bin kernel-danish.bin kernel-japanese.bin +kernel-ntsc.bin kernel-pal.bin kernel-swedish.bin