2021-07-23 01:16:23 +00:00
|
|
|
//
|
|
|
|
// Chipset.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 22/07/2021.
|
|
|
|
// Copyright © 2021 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef Chipset_hpp
|
|
|
|
#define Chipset_hpp
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
|
2021-10-04 13:44:54 +00:00
|
|
|
#include "../../Activity/Source.hpp"
|
|
|
|
#include "../../Components/6526/6526.hpp"
|
2021-07-26 22:44:20 +00:00
|
|
|
#include "../../Outputs/CRT/CRT.hpp"
|
2021-10-04 13:44:54 +00:00
|
|
|
#include "../../Processors/68000/68000.hpp"
|
2021-10-04 23:45:05 +00:00
|
|
|
#include "../../Storage/Disk/Controller/DiskController.hpp"
|
2021-10-04 15:12:13 +00:00
|
|
|
#include "../../Storage/Disk/Drive.hpp"
|
2021-07-23 01:16:23 +00:00
|
|
|
|
|
|
|
#include "Blitter.hpp"
|
2021-09-17 01:17:23 +00:00
|
|
|
#include "Copper.hpp"
|
|
|
|
#include "DMADevice.hpp"
|
2021-10-04 13:44:54 +00:00
|
|
|
#include "MemoryMap.hpp"
|
2021-07-23 01:16:23 +00:00
|
|
|
|
|
|
|
namespace Amiga {
|
|
|
|
|
2021-08-10 00:31:14 +00:00
|
|
|
enum class InterruptFlag: uint16_t {
|
|
|
|
SerialPortTransmit = 1 << 0,
|
|
|
|
DiskBlock = 1 << 1,
|
|
|
|
Software = 1 << 2,
|
|
|
|
IOPortsAndTimers = 1 << 3, // i.e. CIA A.
|
|
|
|
Copper = 1 << 4,
|
|
|
|
VerticalBlank = 1 << 5,
|
|
|
|
Blitter = 1 << 6,
|
|
|
|
AudioChannel0 = 1 << 7,
|
|
|
|
AudioChannel1 = 1 << 8,
|
|
|
|
AudioChannel2 = 1 << 9,
|
|
|
|
AudioChannel3 = 1 << 10,
|
|
|
|
SerialPortReceive = 1 << 11,
|
|
|
|
DiskSyncMatch = 1 << 12,
|
|
|
|
External = 1 << 13, // i.e. CIA B.
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class DMAFlag: uint16_t {
|
|
|
|
AudioChannel0 = 1 << 0,
|
|
|
|
AudioChannel1 = 1 << 1,
|
|
|
|
AudioChannel2 = 1 << 2,
|
|
|
|
AudioChannel3 = 1 << 3,
|
|
|
|
Disk = 1 << 4,
|
|
|
|
Sprites = 1 << 5,
|
|
|
|
Blitter = 1 << 6,
|
|
|
|
Copper = 1 << 7,
|
|
|
|
Bitplane = 1 << 8,
|
|
|
|
AllBelow = 1 << 9,
|
|
|
|
BlitterPriority = 1 << 10,
|
|
|
|
BlitterZero = 1 << 13,
|
|
|
|
BlitterBusy = 1 << 14,
|
|
|
|
};
|
|
|
|
|
2021-07-23 01:16:23 +00:00
|
|
|
class Chipset {
|
|
|
|
public:
|
2021-10-04 15:12:13 +00:00
|
|
|
Chipset(MemoryMap &memory_map, int input_clock_rate);
|
2021-07-23 01:16:23 +00:00
|
|
|
|
2021-07-24 01:24:07 +00:00
|
|
|
struct Changes {
|
2021-07-26 20:40:42 +00:00
|
|
|
int interrupt_level = 0;
|
2021-07-27 00:13:06 +00:00
|
|
|
HalfCycles duration;
|
2021-08-09 01:52:28 +00:00
|
|
|
|
|
|
|
Changes &operator += (const Changes &rhs) {
|
|
|
|
duration += rhs.duration;
|
|
|
|
return *this;
|
|
|
|
}
|
2021-07-24 01:24:07 +00:00
|
|
|
};
|
|
|
|
|
2021-07-27 20:41:18 +00:00
|
|
|
/// Advances the stated amount of time.
|
|
|
|
Changes run_for(HalfCycles);
|
|
|
|
|
|
|
|
/// Advances to the next available CPU slot.
|
|
|
|
Changes run_until_cpu_slot();
|
2021-07-23 01:16:23 +00:00
|
|
|
|
|
|
|
/// Performs the provided microcycle, which the caller guarantees to be a memory access.
|
|
|
|
void perform(const CPU::MC68000::Microcycle &);
|
|
|
|
|
2021-07-28 23:36:30 +00:00
|
|
|
/// Sets the current state of the CIA interrupt lines.
|
|
|
|
void set_cia_interrupts(bool cia_a, bool cia_b);
|
|
|
|
|
2021-07-26 20:40:42 +00:00
|
|
|
/// Provides the chipset's current interrupt level.
|
|
|
|
int get_interrupt_level() {
|
|
|
|
return interrupt_level_;
|
|
|
|
}
|
|
|
|
|
2021-07-26 22:44:20 +00:00
|
|
|
// The standard CRT set.
|
|
|
|
void set_scan_target(Outputs::Display::ScanTarget *scan_target);
|
|
|
|
Outputs::Display::ScanStatus get_scaled_scan_status() const;
|
|
|
|
void set_display_type(Outputs::Display::DisplayType);
|
|
|
|
Outputs::Display::DisplayType get_display_type() const;
|
|
|
|
|
2021-10-04 13:44:54 +00:00
|
|
|
// Activity observation.
|
|
|
|
void set_activity_observer(Activity::Observer *observer) {
|
|
|
|
cia_a_handler_.set_activity_observer(observer);
|
|
|
|
}
|
|
|
|
|
2021-07-23 01:16:23 +00:00
|
|
|
private:
|
2021-09-15 00:51:32 +00:00
|
|
|
friend class DMADeviceBase;
|
2021-08-09 21:35:09 +00:00
|
|
|
|
2021-10-04 13:44:54 +00:00
|
|
|
// MARK: - E Clock follow along.
|
|
|
|
|
|
|
|
HalfCycles cia_divider_;
|
|
|
|
|
2021-07-23 01:16:23 +00:00
|
|
|
// MARK: - Interrupts.
|
|
|
|
|
|
|
|
uint16_t interrupt_enable_ = 0;
|
|
|
|
uint16_t interrupt_requests_ = 0;
|
2021-07-26 20:40:42 +00:00
|
|
|
int interrupt_level_ = 0;
|
2021-07-23 01:16:23 +00:00
|
|
|
|
2021-07-26 20:40:42 +00:00
|
|
|
void update_interrupts();
|
2021-08-10 00:31:14 +00:00
|
|
|
void posit_interrupt(InterruptFlag);
|
2021-07-23 01:16:23 +00:00
|
|
|
|
2021-07-27 20:41:18 +00:00
|
|
|
// MARK: - Scheduler.
|
|
|
|
|
2021-08-09 01:52:28 +00:00
|
|
|
template <bool stop_on_cpu> Changes run(HalfCycles duration = HalfCycles::max());
|
2021-07-28 01:59:27 +00:00
|
|
|
template <bool stop_on_cpu> int advance_slots(int, int);
|
2021-07-27 20:41:18 +00:00
|
|
|
template <int cycle, bool stop_if_cpu> bool perform_cycle();
|
2021-07-28 01:33:07 +00:00
|
|
|
template <int cycle> void output();
|
2021-07-27 20:41:18 +00:00
|
|
|
|
|
|
|
// MARK: - DMA Control, Scheduler and Blitter.
|
2021-07-23 01:16:23 +00:00
|
|
|
|
|
|
|
uint16_t dma_control_ = 0;
|
|
|
|
Blitter blitter_;
|
|
|
|
|
|
|
|
// MARK: - Sprites.
|
|
|
|
|
|
|
|
struct Sprite {
|
|
|
|
void set_pointer(int shift, uint16_t value);
|
|
|
|
void set_start_position(uint16_t value);
|
|
|
|
void set_stop_and_control(uint16_t value);
|
|
|
|
void set_image_data(int slot, uint16_t value);
|
|
|
|
} sprites_[8];
|
2021-07-23 01:45:51 +00:00
|
|
|
|
2021-08-10 23:01:41 +00:00
|
|
|
// MARK: - Raster position and state.
|
2021-07-23 01:45:51 +00:00
|
|
|
|
2021-08-10 23:01:41 +00:00
|
|
|
// Definitions related to PAL/NTSC.
|
2021-07-28 01:33:07 +00:00
|
|
|
int line_length_ = 227;
|
2021-07-26 22:44:20 +00:00
|
|
|
int frame_height_ = 312;
|
2021-07-26 22:51:01 +00:00
|
|
|
int vertical_blank_height_ = 29;
|
2021-07-23 02:00:53 +00:00
|
|
|
|
2021-08-10 23:01:41 +00:00
|
|
|
// Current raster position.
|
|
|
|
int line_cycle_ = 0, y_ = 0;
|
|
|
|
|
|
|
|
// Parameters affecting bitplane collection and output.
|
2021-07-23 02:00:53 +00:00
|
|
|
uint16_t display_window_start_[2] = {0, 0};
|
|
|
|
uint16_t display_window_stop_[2] = {0, 0};
|
|
|
|
uint16_t fetch_window_[2] = {0, 0};
|
2021-07-26 22:44:20 +00:00
|
|
|
|
2021-08-10 23:01:41 +00:00
|
|
|
// Ephemeral bitplane collection state.
|
|
|
|
bool fetch_vertical_ = false, fetch_horizontal_ = false;
|
2021-08-12 00:31:37 +00:00
|
|
|
bool display_horizontal_ = false;
|
2021-08-11 01:28:48 +00:00
|
|
|
bool did_fetch_ = false;
|
2021-08-10 23:01:41 +00:00
|
|
|
|
2021-08-12 00:31:37 +00:00
|
|
|
// Output state.
|
|
|
|
uint16_t border_colour_ = 0;
|
|
|
|
bool is_border_ = true;
|
|
|
|
int zone_duration_ = 0;
|
|
|
|
uint16_t *pixels_ = nullptr;
|
|
|
|
void flush_output();
|
|
|
|
|
2021-09-09 00:57:26 +00:00
|
|
|
struct BitplaneData: public std::array<uint16_t, 6> {
|
2021-09-29 02:12:13 +00:00
|
|
|
BitplaneData &operator <<= (int c) {
|
|
|
|
(*this)[0] <<= c;
|
|
|
|
(*this)[1] <<= c;
|
|
|
|
(*this)[2] <<= c;
|
|
|
|
(*this)[3] <<= c;
|
|
|
|
(*this)[4] <<= c;
|
|
|
|
(*this)[5] <<= c;
|
2021-09-09 00:57:26 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
2021-08-11 01:28:48 +00:00
|
|
|
|
|
|
|
class Bitplanes: public DMADevice<6> {
|
|
|
|
public:
|
|
|
|
using DMADevice::DMADevice;
|
|
|
|
|
2021-09-20 01:55:45 +00:00
|
|
|
bool advance(int cycle);
|
2021-08-11 01:28:48 +00:00
|
|
|
void do_end_of_line();
|
2021-08-11 22:47:35 +00:00
|
|
|
void set_control(uint16_t);
|
2021-08-11 01:28:48 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool is_high_res_ = false;
|
2021-08-12 00:31:37 +00:00
|
|
|
int plane_count_ = 0;
|
2021-08-11 01:28:48 +00:00
|
|
|
|
|
|
|
BitplaneData next;
|
|
|
|
} bitplanes_;
|
|
|
|
|
|
|
|
void post_bitplanes(const BitplaneData &data);
|
2021-08-10 23:01:41 +00:00
|
|
|
|
2021-09-09 00:57:26 +00:00
|
|
|
BitplaneData current_bitplanes_, next_bitplanes_;
|
|
|
|
// std::array<uint8_t, 912> even_playfield_;
|
|
|
|
// std::array<uint8_t, 912> odd_playfield_;
|
2021-08-11 22:47:35 +00:00
|
|
|
int odd_delay_ = 0, even_delay_ = 0;
|
2021-09-20 23:00:52 +00:00
|
|
|
bool is_high_res_ = false;
|
2021-08-11 22:47:35 +00:00
|
|
|
|
2021-07-26 22:44:20 +00:00
|
|
|
// MARK: - Copper.
|
|
|
|
|
2021-09-17 01:17:23 +00:00
|
|
|
Copper copper_;
|
2021-07-30 22:24:27 +00:00
|
|
|
|
|
|
|
// MARK: - Serial port.
|
|
|
|
|
|
|
|
class SerialPort {
|
|
|
|
public:
|
|
|
|
void set_control(uint16_t) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint16_t value = 0, reload = 0;
|
|
|
|
uint16_t shift = 0, receive_shift = 0;
|
|
|
|
uint16_t status;
|
|
|
|
} serial_;
|
2021-07-26 22:44:20 +00:00
|
|
|
|
2021-08-09 21:35:09 +00:00
|
|
|
// MARK: - Disk drives.
|
|
|
|
|
2021-10-04 23:45:05 +00:00
|
|
|
class DiskController: private Storage::Disk::Controller {
|
|
|
|
public:
|
|
|
|
DiskController(Cycles clock_rate);
|
|
|
|
|
2021-10-05 12:12:01 +00:00
|
|
|
void set_mtr_sel_side_dir_step(uint8_t);
|
|
|
|
uint8_t get_rdy_trk0_wpro_chng();
|
2021-10-04 23:45:05 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void process_input_bit(int value) final;
|
|
|
|
void process_index_hole() final;
|
|
|
|
|
2021-10-05 12:12:01 +00:00
|
|
|
// Implement the Amiga's drive ID shift registers
|
|
|
|
// directly in the controller for now.
|
|
|
|
uint32_t drive_ids_[4]{};
|
|
|
|
uint32_t previous_select_ = 0;
|
|
|
|
|
2021-10-04 23:45:05 +00:00
|
|
|
} disk_controller_;
|
2021-10-04 15:12:13 +00:00
|
|
|
|
2021-08-11 01:28:48 +00:00
|
|
|
class DiskDMA: public DMADevice<1> {
|
2021-08-09 21:35:09 +00:00
|
|
|
public:
|
|
|
|
using DMADevice::DMADevice;
|
|
|
|
|
|
|
|
void set_length(uint16_t value) {
|
|
|
|
dma_enable_ = value & 0x8000;
|
|
|
|
write_ = value & 0x4000;
|
|
|
|
length_ = value & 0x3fff;
|
|
|
|
|
|
|
|
if(dma_enable_) {
|
2021-08-11 22:47:35 +00:00
|
|
|
printf("Not yet implemented: disk DMA [%s of %d to %06x]\n", write_ ? "write" : "read", length_, pointer_[0]);
|
2021-08-09 21:35:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:17:01 +00:00
|
|
|
bool advance();
|
2021-08-10 00:31:14 +00:00
|
|
|
|
2021-08-09 21:35:09 +00:00
|
|
|
private:
|
|
|
|
uint16_t length_;
|
|
|
|
bool dma_enable_ = false;
|
|
|
|
bool write_ = false;
|
|
|
|
} disk_;
|
|
|
|
|
2021-07-26 22:44:20 +00:00
|
|
|
// MARK: - Pixel output.
|
|
|
|
|
|
|
|
Outputs::CRT::CRT crt_;
|
2021-07-26 22:59:11 +00:00
|
|
|
uint16_t palette_[32]{};
|
2021-08-11 22:47:35 +00:00
|
|
|
uint16_t swizzled_palette_[32]{};
|
2021-10-04 23:45:05 +00:00
|
|
|
|
|
|
|
// MARK: - CIAs
|
|
|
|
private:
|
|
|
|
class CIAAHandler: public MOS::MOS6526::PortHandler {
|
|
|
|
public:
|
|
|
|
CIAAHandler(MemoryMap &map, DiskController &controller);
|
|
|
|
void set_port_output(MOS::MOS6526::Port port, uint8_t value);
|
|
|
|
uint8_t get_port_input(MOS::MOS6526::Port port);
|
|
|
|
void set_activity_observer(Activity::Observer *observer);
|
|
|
|
|
|
|
|
private:
|
|
|
|
MemoryMap &map_;
|
|
|
|
DiskController &controller_;
|
|
|
|
Activity::Observer *observer_ = nullptr;
|
|
|
|
inline static const std::string led_name = "Power";
|
|
|
|
} cia_a_handler_;
|
|
|
|
|
|
|
|
class CIABHandler: public MOS::MOS6526::PortHandler {
|
|
|
|
public:
|
|
|
|
CIABHandler(DiskController &controller);
|
|
|
|
void set_port_output(MOS::MOS6526::Port port, uint8_t value);
|
|
|
|
uint8_t get_port_input(MOS::MOS6526::Port);
|
|
|
|
|
|
|
|
private:
|
|
|
|
DiskController &controller_;
|
|
|
|
} cia_b_handler_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// CIAs are provided for direct access; it's up to the caller properly
|
|
|
|
// to distinguish relevant accesses.
|
|
|
|
MOS::MOS6526::MOS6526<CIAAHandler, MOS::MOS6526::Personality::P8250> cia_a;
|
|
|
|
MOS::MOS6526::MOS6526<CIABHandler, MOS::MOS6526::Personality::P8250> cia_b;
|
2021-07-23 01:16:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Chipset_hpp */
|