2019-03-09 05:00:23 +00:00
|
|
|
//
|
|
|
|
// 68000Storage.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 08/03/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef MC68000Storage_h
|
|
|
|
#define MC68000Storage_h
|
|
|
|
|
|
|
|
class ProcessorStorage {
|
|
|
|
public:
|
|
|
|
ProcessorStorage();
|
|
|
|
|
|
|
|
protected:
|
2019-03-10 21:27:34 +00:00
|
|
|
RegisterPair32 data_[8];
|
2019-03-14 01:08:13 +00:00
|
|
|
RegisterPair32 address_[8];
|
2019-03-10 21:27:34 +00:00
|
|
|
RegisterPair32 program_counter_;
|
|
|
|
|
2019-03-14 01:08:13 +00:00
|
|
|
RegisterPair32 stack_pointers_[2]; // [0] = user stack pointer; [1] = supervisor; the values from here
|
|
|
|
// are copied into/out of address_[7] upon mode switches.
|
|
|
|
|
2019-03-20 01:33:52 +00:00
|
|
|
RegisterPair32 prefetch_queue_; // Each word will go into the low part of the word, then proceed upward.
|
2019-03-10 21:42:13 +00:00
|
|
|
bool dtack_ = true;
|
2019-03-09 05:00:23 +00:00
|
|
|
|
2019-03-10 21:42:13 +00:00
|
|
|
// Various status bits.
|
|
|
|
int is_supervisor_;
|
2019-03-24 22:20:54 +00:00
|
|
|
int interrupt_level_;
|
|
|
|
uint_fast32_t zero_result_; // The zero flag is set if this value is zero.
|
2019-03-16 21:54:58 +00:00
|
|
|
uint_fast32_t carry_flag_; // The carry flag is set if this value is non-zero.
|
|
|
|
uint_fast32_t extend_flag_; // The extend flag is set if this value is non-zero.
|
|
|
|
uint_fast32_t overflow_flag_; // The overflow flag is set if this value is non-zero.
|
|
|
|
uint_fast32_t negative_flag_; // The negative flag is set if this value is non-zero.
|
2019-03-24 22:20:54 +00:00
|
|
|
uint_fast32_t trace_flag_; // The trace flag is set if this value is non-zero.
|
2019-03-09 05:00:23 +00:00
|
|
|
|
2019-03-19 02:51:32 +00:00
|
|
|
// Generic sources and targets for memory operations;
|
|
|
|
// by convention: [0] = source, [1] = destination.
|
2019-03-27 02:07:28 +00:00
|
|
|
RegisterPair32 effective_address_[2];
|
2019-03-14 01:08:13 +00:00
|
|
|
RegisterPair32 bus_data_[2];
|
2019-03-10 21:27:34 +00:00
|
|
|
|
2019-03-22 02:30:41 +00:00
|
|
|
HalfCycles half_cycles_left_to_run_;
|
|
|
|
|
2019-03-13 02:46:31 +00:00
|
|
|
enum class Operation {
|
2019-03-30 03:40:54 +00:00
|
|
|
None,
|
2019-03-13 02:46:31 +00:00
|
|
|
ABCD, SBCD,
|
|
|
|
ADD, AND, EOR, OR, SUB,
|
2019-03-16 21:54:58 +00:00
|
|
|
|
2019-03-30 03:13:41 +00:00
|
|
|
MOVEb, MOVEw, MOVEl, MOVEq,
|
2019-03-24 22:20:54 +00:00
|
|
|
MOVEAw, MOVEAl,
|
|
|
|
|
2019-03-25 03:05:57 +00:00
|
|
|
MOVEtoSR, MOVEfromSR,
|
|
|
|
|
2019-03-26 02:54:49 +00:00
|
|
|
CMPb, CMPw, CMPl,
|
|
|
|
|
2019-03-27 02:07:28 +00:00
|
|
|
BRA, Bcc,
|
2019-03-13 02:46:31 +00:00
|
|
|
};
|
|
|
|
|
2019-03-09 05:00:23 +00:00
|
|
|
/*!
|
2019-03-13 02:46:31 +00:00
|
|
|
Bus steps are sequences of things to communicate to the bus.
|
2019-03-17 02:36:09 +00:00
|
|
|
Standard behaviour is: (i) perform microcycle; (ii) perform action.
|
2019-03-09 05:00:23 +00:00
|
|
|
*/
|
2019-03-13 02:46:31 +00:00
|
|
|
struct BusStep {
|
2019-03-09 05:00:23 +00:00
|
|
|
Microcycle microcycle;
|
|
|
|
enum class Action {
|
2019-03-10 21:27:34 +00:00
|
|
|
None,
|
|
|
|
|
2019-03-19 02:51:32 +00:00
|
|
|
/// Performs effective_address_[0] += 2.
|
|
|
|
IncrementEffectiveAddress0,
|
|
|
|
|
|
|
|
/// Performs effective_address_[1] += 2.
|
|
|
|
IncrementEffectiveAddress1,
|
2019-03-10 21:27:34 +00:00
|
|
|
|
|
|
|
/// Performs program_counter_ += 2.
|
|
|
|
IncrementProgramCounter,
|
|
|
|
|
|
|
|
/// Copies prefetch_queue_[1] to prefetch_queue_[0].
|
|
|
|
AdvancePrefetch,
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Terminates an atomic program; if nothing else is pending, schedules the next instruction.
|
|
|
|
This action is special in that it usurps any included microcycle. So any Step with this
|
|
|
|
as its action acts as an end-of-list sentinel.
|
|
|
|
*/
|
|
|
|
ScheduleNextProgram
|
|
|
|
|
|
|
|
} action = Action::None;
|
2019-03-17 01:47:46 +00:00
|
|
|
|
2019-03-17 02:36:09 +00:00
|
|
|
inline bool operator ==(const BusStep &rhs) const {
|
2019-03-17 01:47:46 +00:00
|
|
|
if(action != rhs.action) return false;
|
|
|
|
return microcycle == rhs.microcycle;
|
|
|
|
}
|
2019-03-17 02:36:09 +00:00
|
|
|
|
|
|
|
inline bool is_terminal() const {
|
|
|
|
return action == Action::ScheduleNextProgram;
|
|
|
|
}
|
2019-03-09 05:00:23 +00:00
|
|
|
};
|
|
|
|
|
2019-03-13 02:46:31 +00:00
|
|
|
/*!
|
|
|
|
A micro-op is: (i) an action to take; and (ii) a sequence of bus operations
|
|
|
|
to perform after taking the action.
|
|
|
|
|
2019-03-17 02:36:09 +00:00
|
|
|
NOTE: this therefore has the opposite order of behaviour compared to a BusStep,
|
|
|
|
the action occurs BEFORE the bus operations, not after.
|
|
|
|
|
|
|
|
A nullptr bus_program terminates a sequence of micro operations; the is_terminal
|
|
|
|
test should be used to query for that. The action on the final operation will
|
|
|
|
be performed.
|
2019-03-13 02:46:31 +00:00
|
|
|
*/
|
|
|
|
struct MicroOp {
|
2019-03-19 02:51:32 +00:00
|
|
|
enum class Action: int {
|
2019-03-13 02:46:31 +00:00
|
|
|
None,
|
2019-03-22 02:30:41 +00:00
|
|
|
|
|
|
|
/// Does whatever this instruction says is the main operation.
|
2019-03-14 01:08:13 +00:00
|
|
|
PerformOperation,
|
|
|
|
|
2019-03-22 02:30:41 +00:00
|
|
|
/// Sets the flags as if a MOVE operation had occurred, by inspecting *source.
|
|
|
|
SetMoveFlagsb,
|
|
|
|
SetMoveFlagsw,
|
|
|
|
SetMoveFlagsl,
|
|
|
|
|
2019-03-19 02:51:32 +00:00
|
|
|
/*
|
|
|
|
All of the below will honour the source and destination masks
|
|
|
|
in deciding where to apply their actions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// Subtracts 1.
|
|
|
|
Decrement1,
|
|
|
|
/// Subtracts 2.
|
|
|
|
Decrement2,
|
|
|
|
/// Subtracts 4.
|
|
|
|
Decrement4,
|
|
|
|
|
|
|
|
/// Adds 1.
|
|
|
|
Increment1,
|
|
|
|
/// Adds 2.
|
|
|
|
Increment2,
|
|
|
|
/// Adds 4.
|
|
|
|
Increment4,
|
|
|
|
|
2019-03-30 03:13:41 +00:00
|
|
|
/// Copies the source and/or destination to effective_address_.
|
|
|
|
CopyToEffectiveAddress,
|
2019-03-24 01:03:52 +00:00
|
|
|
|
2019-03-23 01:43:51 +00:00
|
|
|
/// Peeking into the end of the prefetch queue, calculates the proper target of (d16,An) addressing.
|
2019-03-19 02:51:32 +00:00
|
|
|
CalcD16An,
|
|
|
|
|
2019-03-23 01:43:51 +00:00
|
|
|
/// Peeking into the end of the prefetch queue, calculates the proper target of (d8,An,Xn) addressing.
|
2019-03-19 02:51:32 +00:00
|
|
|
CalcD8AnXn,
|
|
|
|
|
|
|
|
/// Peeking into the prefetch queue, calculates the proper target of (d16,PC) addressing,
|
|
|
|
/// adjusting as though it had been performed after the proper PC fetches. The source
|
|
|
|
/// and destination mask flags affect only the destination of the result.
|
|
|
|
CalcD16PC,
|
|
|
|
|
|
|
|
/// Peeking into the prefetch queue, calculates the proper target of (d8,An,Xn) addressing,
|
|
|
|
/// adjusting as though it had been performed after the proper PC fetches. The source
|
|
|
|
/// and destination mask flags affect only the destination of the result.
|
|
|
|
CalcD8PCXn,
|
|
|
|
|
|
|
|
/// Sets the high word according to the MSB of the low word.
|
|
|
|
SignExtendWord,
|
|
|
|
|
|
|
|
/// Sets the high three bytes according to the MSB of the low byte.
|
|
|
|
SignExtendByte,
|
2019-03-19 15:53:37 +00:00
|
|
|
|
2019-03-23 01:43:51 +00:00
|
|
|
/// From the next word in the prefetch queue assembles a 0-padded 32-bit long word in either or
|
|
|
|
/// both of effective_address_[0] and effective_address_[1].
|
2019-03-25 03:05:57 +00:00
|
|
|
AssembleWordAddressFromPrefetch,
|
|
|
|
|
|
|
|
/// From the next word in the prefetch queue assembles a 0-padded 32-bit long word in either or
|
|
|
|
/// both of bus_data_[0] and bus_data_[1].
|
|
|
|
AssembleWordDataFromPrefetch,
|
2019-03-23 01:43:51 +00:00
|
|
|
|
|
|
|
/// Copies the next two prefetch words into one of the effective_address_.
|
2019-03-25 03:05:57 +00:00
|
|
|
AssembleLongWordAddressFromPrefetch,
|
|
|
|
|
|
|
|
/// Copies the next two prefetch words into one of the bus_data_.
|
|
|
|
AssembleLongWordDataFromPrefetch
|
2019-03-19 02:51:32 +00:00
|
|
|
};
|
|
|
|
static const int SourceMask = 1 << 30;
|
|
|
|
static const int DestinationMask = 1 << 29;
|
|
|
|
int action = int(Action::None);
|
2019-03-17 18:34:16 +00:00
|
|
|
|
2019-03-13 02:46:31 +00:00
|
|
|
BusStep *bus_program = nullptr;
|
2019-03-15 01:22:02 +00:00
|
|
|
|
|
|
|
MicroOp() {}
|
2019-03-19 02:51:32 +00:00
|
|
|
MicroOp(int action) : action(action) {}
|
|
|
|
MicroOp(int action, BusStep *bus_program) : action(action), bus_program(bus_program) {}
|
|
|
|
|
|
|
|
MicroOp(Action action) : MicroOp(int(action)) {}
|
|
|
|
MicroOp(Action action, BusStep *bus_program) : MicroOp(int(action), bus_program) {}
|
2019-03-17 01:47:46 +00:00
|
|
|
|
|
|
|
inline bool is_terminal() const {
|
|
|
|
return bus_program == nullptr;
|
|
|
|
}
|
2019-03-13 02:46:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
A program represents the implementation of a particular opcode, as a sequence
|
|
|
|
of micro-ops and, separately, the operation to perform plus whatever other
|
|
|
|
fields the operation requires.
|
|
|
|
*/
|
|
|
|
struct Program {
|
|
|
|
MicroOp *micro_operations = nullptr;
|
2019-03-14 23:32:15 +00:00
|
|
|
RegisterPair32 *source = nullptr;
|
|
|
|
RegisterPair32 *destination = nullptr;
|
2019-03-14 01:08:13 +00:00
|
|
|
Operation operation;
|
2019-03-24 22:20:54 +00:00
|
|
|
bool requires_supervisor = false;
|
2019-03-25 03:05:57 +00:00
|
|
|
|
|
|
|
void set_source(ProcessorStorage &storage, int mode, int reg) {
|
|
|
|
switch(mode) {
|
|
|
|
case 0: source = &storage.data_[reg]; break;
|
|
|
|
case 1: source = &storage.address_[reg]; break;
|
|
|
|
default: source = &storage.bus_data_[0]; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_destination(ProcessorStorage &storage, int mode, int reg) {
|
|
|
|
switch(mode) {
|
|
|
|
case 0: destination = &storage.data_[reg]; break;
|
|
|
|
case 1: destination = &storage.address_[reg]; break;
|
|
|
|
default: destination = &storage.bus_data_[1]; break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-13 02:46:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Storage for all the sequences of bus steps and micro-ops used throughout
|
|
|
|
// the 68000.
|
|
|
|
std::vector<BusStep> all_bus_steps_;
|
|
|
|
std::vector<MicroOp> all_micro_ops_;
|
2019-03-09 05:00:23 +00:00
|
|
|
|
2019-03-13 02:46:31 +00:00
|
|
|
// A lookup table from instructions to implementations.
|
|
|
|
Program instructions[65536];
|
|
|
|
|
2019-03-26 02:54:49 +00:00
|
|
|
// Special steps for exception handlers.
|
|
|
|
BusStep *reset_bus_steps_;
|
|
|
|
|
|
|
|
// Special micro-op sequences for conditionals.
|
|
|
|
BusStep *branch_taken_bus_steps_;
|
|
|
|
BusStep *branch_byte_not_taken_bus_steps_;
|
|
|
|
BusStep *branch_word_not_taken_bus_steps_;
|
2019-03-13 02:46:31 +00:00
|
|
|
|
|
|
|
// Current bus step pointer, and outer program pointer.
|
|
|
|
Program *active_program_ = nullptr;
|
|
|
|
MicroOp *active_micro_op_ = nullptr;
|
|
|
|
BusStep *active_step_ = nullptr;
|
2019-03-10 21:27:34 +00:00
|
|
|
|
2019-03-18 01:57:00 +00:00
|
|
|
/// Copies address_[7] to the proper stack pointer based on current mode.
|
|
|
|
void write_back_stack_pointer();
|
|
|
|
|
|
|
|
/// Sets or clears the supervisor flag, ensuring the stack pointer is properly updated.
|
|
|
|
void set_is_supervisor(bool);
|
|
|
|
|
2019-03-09 05:00:23 +00:00
|
|
|
private:
|
2019-03-16 23:41:07 +00:00
|
|
|
friend class ProcessorStorageConstructor;
|
2019-03-09 05:00:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* MC68000Storage_h */
|