2015-07-16 23:56:02 +00:00
|
|
|
//
|
|
|
|
// Atari2600.hpp
|
2015-07-26 19:25:11 +00:00
|
|
|
// CLK
|
2015-07-16 23:56:02 +00:00
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 14/07/2015.
|
|
|
|
// Copyright © 2015 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef Atari2600_cpp
|
|
|
|
#define Atari2600_cpp
|
|
|
|
|
2016-06-19 22:57:40 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2016-01-05 04:12:47 +00:00
|
|
|
#include "../../Processors/6502/CPU6502.hpp"
|
2016-06-19 22:57:40 +00:00
|
|
|
#include "../../Components/6532/6532.hpp"
|
2016-06-01 01:23:44 +00:00
|
|
|
#include "../CRTMachine.hpp"
|
2016-06-19 22:57:40 +00:00
|
|
|
|
2015-08-19 00:33:24 +00:00
|
|
|
#include "Atari2600Inputs.h"
|
2015-07-16 23:56:02 +00:00
|
|
|
|
|
|
|
namespace Atari2600 {
|
|
|
|
|
2016-06-03 01:22:55 +00:00
|
|
|
const unsigned int number_of_upcoming_events = 6;
|
2016-05-27 18:33:08 +00:00
|
|
|
const unsigned int number_of_recorded_counters = 7;
|
2016-05-17 11:09:18 +00:00
|
|
|
|
2016-06-01 01:23:44 +00:00
|
|
|
class Speaker: public ::Outputs::Filter<Speaker> {
|
|
|
|
public:
|
2016-06-02 23:50:16 +00:00
|
|
|
Speaker();
|
|
|
|
~Speaker();
|
|
|
|
|
2016-06-01 01:23:44 +00:00
|
|
|
void set_volume(int channel, uint8_t volume);
|
|
|
|
void set_divider(int channel, uint8_t divider);
|
|
|
|
void set_control(int channel, uint8_t control);
|
|
|
|
|
|
|
|
void get_samples(unsigned int number_of_samples, int16_t *target);
|
|
|
|
void skip_samples(unsigned int number_of_samples);
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t _volume[2];
|
|
|
|
uint8_t _divider[2];
|
|
|
|
uint8_t _control[2];
|
2016-06-02 23:50:16 +00:00
|
|
|
|
|
|
|
int _poly4_counter[2];
|
|
|
|
int _poly5_counter[2];
|
|
|
|
int _poly9_counter[2];
|
2016-06-01 23:53:16 +00:00
|
|
|
int _output_state[2];
|
2016-06-02 23:50:16 +00:00
|
|
|
|
|
|
|
int _divider_counter[2];
|
|
|
|
|
|
|
|
int _pattern_periods[16];
|
|
|
|
int _patterns[16][512];
|
2016-06-01 01:23:44 +00:00
|
|
|
};
|
|
|
|
|
2016-06-19 22:57:40 +00:00
|
|
|
class PIA: public MOS::MOS6532<PIA> {
|
|
|
|
public:
|
|
|
|
inline uint8_t get_port_input(int port)
|
|
|
|
{
|
|
|
|
return _portValues[port];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void update_port_input(int port, uint8_t mask, bool set)
|
|
|
|
{
|
2016-06-19 23:36:34 +00:00
|
|
|
if(set) _portValues[port] &= ~mask; else _portValues[port] |= mask;
|
2016-06-20 22:57:35 +00:00
|
|
|
set_port_did_change(port);
|
2016-06-19 22:57:40 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 23:36:34 +00:00
|
|
|
PIA() :
|
|
|
|
_portValues{0xff, 0xff}
|
|
|
|
{}
|
|
|
|
|
2016-06-19 22:57:40 +00:00
|
|
|
private:
|
|
|
|
uint8_t _portValues[2];
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-06-01 02:16:20 +00:00
|
|
|
class Machine: public CPU6502::Processor<Machine>, public CRTMachine::Machine {
|
2015-07-16 23:56:02 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
Machine();
|
2015-07-28 01:15:10 +00:00
|
|
|
~Machine();
|
2015-07-16 23:56:02 +00:00
|
|
|
|
|
|
|
void set_rom(size_t length, const uint8_t *data);
|
2015-07-31 22:04:33 +00:00
|
|
|
void switch_region();
|
2015-07-16 23:56:02 +00:00
|
|
|
|
2015-08-19 00:33:24 +00:00
|
|
|
void set_digital_input(Atari2600DigitalInput input, bool state);
|
2016-06-19 23:36:34 +00:00
|
|
|
void set_switch_is_enabled(Atari2600Switch input, bool state);
|
2015-08-19 00:33:24 +00:00
|
|
|
|
2016-06-01 01:23:44 +00:00
|
|
|
// to satisfy CPU6502::Processor
|
|
|
|
unsigned int perform_bus_operation(CPU6502::BusOperation operation, uint16_t address, uint8_t *value);
|
2016-06-01 23:27:04 +00:00
|
|
|
void synchronise();
|
2016-06-01 01:23:44 +00:00
|
|
|
|
|
|
|
// to satisfy CRTMachine::Machine
|
|
|
|
virtual void setup_output(float aspect_ratio);
|
|
|
|
virtual void close_output();
|
|
|
|
virtual Outputs::CRT::CRT *get_crt() { return _crt; }
|
|
|
|
virtual Outputs::Speaker *get_speaker() { return &_speaker; }
|
|
|
|
virtual void run_for_cycles(int number_of_cycles) { CPU6502::Processor<Machine>::run_for_cycles(number_of_cycles); }
|
2016-06-17 00:39:46 +00:00
|
|
|
virtual double get_clock_rate() { return 1194720; }
|
|
|
|
// TODO: different rate for PAL
|
2015-07-22 22:15:18 +00:00
|
|
|
|
2015-07-16 23:56:02 +00:00
|
|
|
private:
|
2016-06-19 22:57:40 +00:00
|
|
|
uint8_t *_rom, *_romPages[4];
|
2015-08-13 12:24:02 +00:00
|
|
|
size_t _rom_size;
|
2015-07-16 23:56:02 +00:00
|
|
|
|
2016-06-19 22:57:40 +00:00
|
|
|
// the RIOT
|
|
|
|
PIA _mos6532;
|
2015-07-16 23:56:02 +00:00
|
|
|
|
2015-07-31 00:52:26 +00:00
|
|
|
// playfield registers
|
2015-08-13 20:32:22 +00:00
|
|
|
uint8_t _playfieldControl;
|
2015-07-27 23:04:03 +00:00
|
|
|
uint8_t _playfieldColour;
|
|
|
|
uint8_t _backgroundColour;
|
2016-05-28 01:51:27 +00:00
|
|
|
uint8_t _playfield[41];
|
2015-07-31 00:52:26 +00:00
|
|
|
|
2016-05-26 01:43:19 +00:00
|
|
|
// ... and derivatives
|
|
|
|
int _ballSize, _missileSize[2];
|
|
|
|
|
2016-05-17 01:54:27 +00:00
|
|
|
// delayed clock events
|
|
|
|
enum OutputState {
|
|
|
|
Sync,
|
|
|
|
Blank,
|
|
|
|
ColourBurst,
|
|
|
|
Pixel
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Event {
|
|
|
|
enum Action {
|
2016-05-18 01:41:32 +00:00
|
|
|
Playfield = 1 << 0,
|
2016-05-30 23:56:36 +00:00
|
|
|
ResetCounter = 1 << 1,
|
2016-05-26 01:12:25 +00:00
|
|
|
|
2016-05-30 23:56:36 +00:00
|
|
|
HMoveSetup = 1 << 2,
|
|
|
|
HMoveCompare = 1 << 3,
|
|
|
|
HMoveDecrement = 1 << 4,
|
2016-05-17 01:54:27 +00:00
|
|
|
};
|
2016-05-17 22:21:49 +00:00
|
|
|
int updates;
|
2016-05-18 01:41:32 +00:00
|
|
|
|
2016-05-17 01:54:27 +00:00
|
|
|
OutputState state;
|
2016-05-25 01:39:57 +00:00
|
|
|
uint8_t playfieldPixel;
|
2016-05-30 23:56:36 +00:00
|
|
|
int counter;
|
2016-05-18 01:41:32 +00:00
|
|
|
|
2016-05-27 18:33:08 +00:00
|
|
|
Event() : updates(0), playfieldPixel(0) {}
|
2016-05-17 11:09:18 +00:00
|
|
|
} _upcomingEvents[number_of_upcoming_events];
|
2016-05-17 01:54:27 +00:00
|
|
|
unsigned int _upcomingEventsPointer;
|
|
|
|
|
2016-05-27 18:33:08 +00:00
|
|
|
// object counters
|
|
|
|
struct ObjectCounter {
|
|
|
|
int count; // the counter value, multiplied by four, counting phase
|
|
|
|
int pixel; // for non-sprite objects, a count of cycles since the last counter reset; for sprite objects a count of pixels so far elapsed
|
|
|
|
int broad_pixel; // for sprite objects, a count of cycles since the last counter reset; otherwise unused
|
|
|
|
|
|
|
|
ObjectCounter() : count(0), pixel(0), broad_pixel(0) {}
|
|
|
|
} _objectCounter[number_of_recorded_counters][5];
|
|
|
|
unsigned int _objectCounterPointer;
|
|
|
|
|
|
|
|
// the latched playfield output
|
2016-05-28 01:51:27 +00:00
|
|
|
uint8_t _playfieldOutput, _nextPlayfieldOutput;
|
2016-05-16 23:55:56 +00:00
|
|
|
|
2015-07-31 00:52:26 +00:00
|
|
|
// player registers
|
|
|
|
uint8_t _playerColour[2];
|
2016-05-26 01:43:19 +00:00
|
|
|
uint8_t _playerReflectionMask[2];
|
2016-05-21 14:26:27 +00:00
|
|
|
uint8_t _playerGraphics[2][2];
|
|
|
|
uint8_t _playerGraphicsSelector[2];
|
2015-08-09 21:47:11 +00:00
|
|
|
bool _playerStart[2];
|
2015-07-31 00:52:26 +00:00
|
|
|
|
2016-05-27 18:33:08 +00:00
|
|
|
// object flags
|
|
|
|
bool _hasSecondCopy[2];
|
|
|
|
bool _hasThirdCopy[2];
|
|
|
|
bool _hasFourthCopy[2];
|
|
|
|
uint8_t _objectMotion[5]; // the value stored to this counter's motion register
|
|
|
|
|
2015-07-31 00:52:26 +00:00
|
|
|
// player + missile registers
|
|
|
|
uint8_t _playerAndMissileSize[2];
|
|
|
|
|
|
|
|
// missile registers
|
2016-05-26 01:43:19 +00:00
|
|
|
uint8_t _missileGraphicsEnable[2];
|
|
|
|
bool _missileGraphicsReset[2];
|
2015-07-31 00:52:26 +00:00
|
|
|
|
|
|
|
// ball registers
|
2016-05-22 18:26:02 +00:00
|
|
|
uint8_t _ballGraphicsEnable[2];
|
|
|
|
uint8_t _ballGraphicsSelector;
|
2015-07-19 20:48:14 +00:00
|
|
|
|
|
|
|
// graphics output
|
2016-05-16 12:01:29 +00:00
|
|
|
unsigned int _horizontalTimer;
|
2015-07-19 20:48:14 +00:00
|
|
|
bool _vSyncEnabled, _vBlankEnabled;
|
2016-05-17 22:21:49 +00:00
|
|
|
|
|
|
|
// horizontal motion control
|
2015-08-13 17:59:23 +00:00
|
|
|
uint8_t _hMoveCounter;
|
|
|
|
uint8_t _hMoveFlags;
|
2016-05-17 22:21:49 +00:00
|
|
|
|
2015-08-19 00:33:24 +00:00
|
|
|
// joystick state
|
2015-08-19 00:58:05 +00:00
|
|
|
uint8_t _tiaInputValue[2];
|
2015-08-19 00:33:24 +00:00
|
|
|
|
2015-08-12 23:31:57 +00:00
|
|
|
// collisions
|
|
|
|
uint8_t _collisions[8];
|
|
|
|
|
2015-08-16 20:08:29 +00:00
|
|
|
void output_pixels(unsigned int count);
|
2016-05-16 23:04:13 +00:00
|
|
|
uint8_t get_output_pixel();
|
2016-05-21 14:18:15 +00:00
|
|
|
void update_timers(int mask);
|
2016-06-01 01:23:44 +00:00
|
|
|
|
2016-06-01 23:27:04 +00:00
|
|
|
// outputs
|
2016-03-09 01:49:07 +00:00
|
|
|
Outputs::CRT::CRT *_crt;
|
2016-06-01 01:23:44 +00:00
|
|
|
Speaker _speaker;
|
2015-07-19 20:48:14 +00:00
|
|
|
|
2016-06-01 23:27:04 +00:00
|
|
|
// speaker backlog accumlation counter
|
|
|
|
unsigned int _cycles_since_speaker_update;
|
|
|
|
void update_audio();
|
|
|
|
|
2015-07-19 20:48:14 +00:00
|
|
|
// latched output state
|
2015-08-16 20:08:29 +00:00
|
|
|
unsigned int _lastOutputStateDuration;
|
2016-05-28 01:51:27 +00:00
|
|
|
OutputState _stateByExtendTime[2][57];
|
|
|
|
OutputState *_stateByTime;
|
2015-07-19 20:48:14 +00:00
|
|
|
OutputState _lastOutputState;
|
2015-07-20 01:21:34 +00:00
|
|
|
uint8_t *_outputBuffer;
|
2016-05-26 01:43:19 +00:00
|
|
|
|
|
|
|
// lookup table for collision reporting
|
2016-05-27 18:33:08 +00:00
|
|
|
uint8_t _reportedCollisions[64][8];
|
2016-05-26 01:43:19 +00:00
|
|
|
void setup_reported_collisions();
|
2015-07-16 23:56:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Atari2600_cpp */
|