2019-06-29 01:58:38 +00:00
|
|
|
//
|
|
|
|
// TestRunner68000.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 28/06/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2019-06-29 01:58:38 +00:00
|
|
|
|
|
|
|
#include <array>
|
2022-05-26 11:52:14 +00:00
|
|
|
#include <functional>
|
2022-05-25 14:55:03 +00:00
|
|
|
#include <vector>
|
2019-06-29 01:58:38 +00:00
|
|
|
|
2023-05-10 22:13:01 +00:00
|
|
|
#include "../../../Processors/68000/68000.hpp"
|
2019-06-29 01:58:38 +00:00
|
|
|
|
2022-05-25 14:55:03 +00:00
|
|
|
using namespace InstructionSet::M68k;
|
2019-06-29 01:58:38 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Provides a 68000 with 64kb of RAM in its low address space;
|
|
|
|
/RESET will put the supervisor stack pointer at 0xFFFF and
|
|
|
|
begin execution at 0x0400.
|
|
|
|
*/
|
2023-05-10 22:13:01 +00:00
|
|
|
class RAM68000: public CPU::MC68000::BusHandler {
|
2019-06-29 01:58:38 +00:00
|
|
|
public:
|
2022-05-26 00:22:38 +00:00
|
|
|
RAM68000() : m68000_(*this) {}
|
2019-06-29 01:58:38 +00:00
|
|
|
|
2021-08-07 21:51:00 +00:00
|
|
|
uint32_t initial_pc() const {
|
|
|
|
return 0x1000;
|
|
|
|
}
|
|
|
|
|
2022-05-26 00:22:38 +00:00
|
|
|
void set_program(
|
|
|
|
const std::vector<uint16_t> &program,
|
2022-05-26 11:52:14 +00:00
|
|
|
uint32_t stack_pointer = 0x206
|
|
|
|
) {
|
2019-06-29 01:58:38 +00:00
|
|
|
memcpy(&ram_[0x1000 >> 1], program.data(), program.size() * sizeof(uint16_t));
|
|
|
|
|
2022-05-26 00:22:38 +00:00
|
|
|
// Ensure the condition codes start unset and set the initial program counter
|
2022-05-26 11:52:14 +00:00
|
|
|
// and supervisor stack pointer, as well as starting in supervisor mode.
|
2022-05-26 13:17:37 +00:00
|
|
|
auto registers = m68000_.get_state().registers;
|
2022-05-26 23:37:30 +00:00
|
|
|
registers.status = 0x2700;
|
2022-05-26 13:17:37 +00:00
|
|
|
registers.program_counter = initial_pc();
|
|
|
|
registers.supervisor_stack_pointer = stack_pointer;
|
|
|
|
m68000_.decode_from_state(registers);
|
2022-05-26 11:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_registers(std::function<void(InstructionSet::M68k::RegisterSet &)> func) {
|
|
|
|
auto state = m68000_.get_state();
|
|
|
|
func(state.registers);
|
|
|
|
m68000_.set_state(state);
|
2019-06-29 01:58:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 19:10:29 +00:00
|
|
|
void will_perform(uint32_t, uint16_t) {
|
2019-06-29 01:58:38 +00:00
|
|
|
--instructions_remaining_;
|
2022-05-26 13:17:37 +00:00
|
|
|
if(instructions_remaining_ < 0) {
|
2022-05-26 00:22:38 +00:00
|
|
|
throw StopException();
|
2022-05-25 20:32:26 +00:00
|
|
|
}
|
2019-06-29 01:58:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void run_for_instructions(int count) {
|
2022-05-26 13:17:37 +00:00
|
|
|
duration_ = HalfCycles(0);
|
2022-05-26 00:22:38 +00:00
|
|
|
instructions_remaining_ = count;
|
|
|
|
if(!instructions_remaining_) return;
|
2019-06-29 01:58:38 +00:00
|
|
|
|
2022-05-26 00:22:38 +00:00
|
|
|
try {
|
|
|
|
while(true) {
|
|
|
|
run_for(HalfCycles(2000));
|
|
|
|
}
|
|
|
|
} catch (const StopException &) {}
|
2019-06-29 01:58:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void run_for(HalfCycles cycles) {
|
|
|
|
m68000_.run_for(cycles);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t *ram_at(uint32_t address) {
|
|
|
|
return &ram_[(address >> 1) % ram_.size()];
|
|
|
|
}
|
|
|
|
|
2023-12-22 04:08:18 +00:00
|
|
|
template <typename Microcycle> HalfCycles perform_bus_operation(const Microcycle &cycle, int) {
|
2019-06-29 01:58:38 +00:00
|
|
|
const uint32_t word_address = cycle.word_address();
|
2022-05-26 13:17:37 +00:00
|
|
|
duration_ += cycle.length;
|
2019-06-29 01:58:38 +00:00
|
|
|
|
|
|
|
if(cycle.data_select_active()) {
|
2024-01-03 18:21:39 +00:00
|
|
|
if(cycle.operation & CPU::MC68000::Operation::InterruptAcknowledge) {
|
2022-05-25 14:55:03 +00:00
|
|
|
cycle.value->b = 10;
|
2019-06-29 01:58:38 +00:00
|
|
|
} else {
|
2024-01-03 18:21:39 +00:00
|
|
|
switch(cycle.operation & (CPU::MC68000::Operation::SelectWord | CPU::MC68000::Operation::SelectByte | CPU::MC68000::Operation::Read)) {
|
2019-06-29 01:58:38 +00:00
|
|
|
default: break;
|
|
|
|
|
2024-01-03 18:21:39 +00:00
|
|
|
case CPU::MC68000::Operation::SelectWord | CPU::MC68000::Operation::Read:
|
2022-05-25 14:55:03 +00:00
|
|
|
cycle.value->w = ram_[word_address % ram_.size()];
|
2019-06-29 01:58:38 +00:00
|
|
|
break;
|
2024-01-03 18:21:39 +00:00
|
|
|
case CPU::MC68000::Operation::SelectByte | CPU::MC68000::Operation::Read:
|
2022-05-25 14:55:03 +00:00
|
|
|
cycle.value->b = ram_[word_address % ram_.size()] >> cycle.byte_shift();
|
2019-06-29 01:58:38 +00:00
|
|
|
break;
|
2024-01-03 18:21:39 +00:00
|
|
|
case CPU::MC68000::Operation::SelectWord:
|
2022-05-25 14:55:03 +00:00
|
|
|
ram_[word_address % ram_.size()] = cycle.value->w;
|
2019-06-29 01:58:38 +00:00
|
|
|
break;
|
2024-01-03 18:21:39 +00:00
|
|
|
case CPU::MC68000::Operation::SelectByte:
|
2019-06-29 01:58:38 +00:00
|
|
|
ram_[word_address % ram_.size()] = uint16_t(
|
2022-05-25 14:55:03 +00:00
|
|
|
(cycle.value->b << cycle.byte_shift()) |
|
2019-06-29 01:58:38 +00:00
|
|
|
(ram_[word_address % ram_.size()] & cycle.untouched_byte_mask())
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return HalfCycles(0);
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:13:01 +00:00
|
|
|
CPU::MC68000::State get_processor_state() {
|
2022-05-26 00:22:38 +00:00
|
|
|
return m68000_.get_state();
|
2019-06-29 01:58:38 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 14:55:03 +00:00
|
|
|
auto &processor() {
|
2019-06-29 01:58:38 +00:00
|
|
|
return m68000_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_cycle_count() {
|
2019-10-30 02:36:29 +00:00
|
|
|
return int(duration_.as_integral()) >> 1;
|
2019-06-29 01:58:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 03:35:28 +00:00
|
|
|
void reset_cycle_count() {
|
|
|
|
duration_ = HalfCycles(0);
|
|
|
|
}
|
|
|
|
|
2019-06-29 01:58:38 +00:00
|
|
|
private:
|
2022-05-26 00:22:38 +00:00
|
|
|
struct StopException {};
|
|
|
|
|
2023-05-10 22:13:01 +00:00
|
|
|
CPU::MC68000::Processor<RAM68000, true, true, true> m68000_;
|
2019-07-01 01:43:30 +00:00
|
|
|
std::array<uint16_t, 256*1024> ram_{};
|
2019-06-29 01:58:38 +00:00
|
|
|
int instructions_remaining_;
|
|
|
|
HalfCycles duration_;
|
|
|
|
};
|