2017-05-17 02:05:42 +00:00
|
|
|
//
|
|
|
|
// TestMachineZ80.m
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 16/05/2017.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-05-17 02:05:42 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "TestMachineZ80.h"
|
|
|
|
#include "Z80AllRAM.hpp"
|
2017-06-04 01:22:16 +00:00
|
|
|
#import "TestMachine+ForSubclassEyesOnly.h"
|
2017-05-17 02:05:42 +00:00
|
|
|
|
2017-05-20 01:20:28 +00:00
|
|
|
@interface CSTestMachineZ80 ()
|
2017-06-22 00:38:08 +00:00
|
|
|
- (void)testMachineDidPerformBusOperation:(CPU::Z80::PartialMachineCycle::Operation)operation
|
2017-06-19 02:03:13 +00:00
|
|
|
address:(uint16_t)address
|
|
|
|
value:(uint8_t)value
|
2017-07-28 00:17:13 +00:00
|
|
|
timeStamp:(HalfCycles)time_stamp;
|
2017-05-20 01:20:28 +00:00
|
|
|
@end
|
|
|
|
|
2017-05-22 23:49:38 +00:00
|
|
|
#pragma mark - C++ delegate handlers
|
2017-05-20 01:20:28 +00:00
|
|
|
|
2017-05-22 23:49:38 +00:00
|
|
|
class BusOperationHandler: public CPU::Z80::AllRAMProcessor::MemoryAccessDelegate {
|
|
|
|
public:
|
|
|
|
BusOperationHandler(CSTestMachineZ80 *targetMachine) : target_(targetMachine) {}
|
|
|
|
|
2017-07-28 00:17:13 +00:00
|
|
|
void z80_all_ram_processor_did_perform_bus_operation(CPU::Z80::AllRAMProcessor &processor, CPU::Z80::PartialMachineCycle::Operation operation, uint16_t address, uint8_t value, HalfCycles time_stamp) {
|
2017-06-22 00:32:08 +00:00
|
|
|
[target_ testMachineDidPerformBusOperation:operation address:address value:value timeStamp:time_stamp];
|
2017-05-22 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CSTestMachineZ80 *target_;
|
|
|
|
};
|
|
|
|
|
2017-05-20 01:20:28 +00:00
|
|
|
#pragma mark - Register enum map
|
|
|
|
|
2017-05-17 02:05:42 +00:00
|
|
|
static CPU::Z80::Register registerForRegister(CSTestMachineZ80Register reg) {
|
|
|
|
switch (reg) {
|
2017-05-22 23:14:46 +00:00
|
|
|
case CSTestMachineZ80RegisterAF: return CPU::Z80::Register::AF;
|
|
|
|
case CSTestMachineZ80RegisterA: return CPU::Z80::Register::A;
|
|
|
|
case CSTestMachineZ80RegisterF: return CPU::Z80::Register::Flags;
|
|
|
|
case CSTestMachineZ80RegisterBC: return CPU::Z80::Register::BC;
|
|
|
|
case CSTestMachineZ80RegisterB: return CPU::Z80::Register::B;
|
2017-05-20 01:53:39 +00:00
|
|
|
case CSTestMachineZ80RegisterC: return CPU::Z80::Register::C;
|
|
|
|
case CSTestMachineZ80RegisterDE: return CPU::Z80::Register::DE;
|
2017-05-22 23:14:46 +00:00
|
|
|
case CSTestMachineZ80RegisterD: return CPU::Z80::Register::D;
|
|
|
|
case CSTestMachineZ80RegisterE: return CPU::Z80::Register::E;
|
|
|
|
case CSTestMachineZ80RegisterHL: return CPU::Z80::Register::HL;
|
|
|
|
case CSTestMachineZ80RegisterH: return CPU::Z80::Register::H;
|
|
|
|
case CSTestMachineZ80RegisterL: return CPU::Z80::Register::L;
|
|
|
|
|
|
|
|
case CSTestMachineZ80RegisterAFDash: return CPU::Z80::Register::AFDash;
|
|
|
|
case CSTestMachineZ80RegisterBCDash: return CPU::Z80::Register::BCDash;
|
|
|
|
case CSTestMachineZ80RegisterDEDash: return CPU::Z80::Register::DEDash;
|
|
|
|
case CSTestMachineZ80RegisterHLDash: return CPU::Z80::Register::HLDash;
|
|
|
|
|
|
|
|
case CSTestMachineZ80RegisterIX: return CPU::Z80::Register::IX;
|
|
|
|
case CSTestMachineZ80RegisterIY: return CPU::Z80::Register::IY;
|
|
|
|
|
|
|
|
case CSTestMachineZ80RegisterI: return CPU::Z80::Register::I;
|
|
|
|
case CSTestMachineZ80RegisterR: return CPU::Z80::Register::R;
|
|
|
|
|
|
|
|
case CSTestMachineZ80RegisterIFF1: return CPU::Z80::Register::IFF1;
|
|
|
|
case CSTestMachineZ80RegisterIFF2: return CPU::Z80::Register::IFF2;
|
|
|
|
case CSTestMachineZ80RegisterIM: return CPU::Z80::Register::IM;
|
|
|
|
|
|
|
|
case CSTestMachineZ80RegisterProgramCounter: return CPU::Z80::Register::ProgramCounter;
|
|
|
|
case CSTestMachineZ80RegisterStackPointer: return CPU::Z80::Register::StackPointer;
|
2017-07-22 02:52:25 +00:00
|
|
|
|
|
|
|
case CSTestMachineZ80RegisterMemPtr: return CPU::Z80::Register::MemPtr;
|
2017-05-17 02:05:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 23:49:38 +00:00
|
|
|
#pragma mark - Capture class
|
|
|
|
|
2017-05-29 19:57:27 +00:00
|
|
|
@interface CSTestMachineZ80BusOperationCapture()
|
|
|
|
@property(nonatomic, assign) CSTestMachineZ80BusOperationCaptureOperation operation;
|
|
|
|
@property(nonatomic, assign) uint16_t address;
|
|
|
|
@property(nonatomic, assign) uint8_t value;
|
|
|
|
@property(nonatomic, assign) int timeStamp;
|
|
|
|
@end
|
|
|
|
|
2017-05-22 23:49:38 +00:00
|
|
|
@implementation CSTestMachineZ80BusOperationCapture
|
2017-05-23 01:50:34 +00:00
|
|
|
|
|
|
|
- (NSString *)description {
|
2017-05-29 21:13:24 +00:00
|
|
|
NSString *opName = @"";
|
|
|
|
switch(self.operation) {
|
2017-06-17 22:20:30 +00:00
|
|
|
case CSTestMachineZ80BusOperationCaptureOperationReadOpcode: opName = @"ro"; break;
|
|
|
|
case CSTestMachineZ80BusOperationCaptureOperationRead: opName = @"r"; break;
|
|
|
|
case CSTestMachineZ80BusOperationCaptureOperationWrite: opName = @"w"; break;
|
|
|
|
case CSTestMachineZ80BusOperationCaptureOperationPortRead: opName = @"i"; break;
|
|
|
|
case CSTestMachineZ80BusOperationCaptureOperationPortWrite: opName = @"o"; break;
|
|
|
|
case CSTestMachineZ80BusOperationCaptureOperationInternalOperation: opName = @"iop"; break;
|
2017-05-29 21:13:24 +00:00
|
|
|
}
|
|
|
|
return [NSString stringWithFormat:@"%@ %04x %02x [%d]", opName, self.address, self.value, self.timeStamp];
|
2017-05-23 01:50:34 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 23:49:38 +00:00
|
|
|
@end
|
|
|
|
|
2017-05-20 01:20:28 +00:00
|
|
|
#pragma mark - Test class
|
|
|
|
|
2017-05-17 02:19:40 +00:00
|
|
|
@implementation CSTestMachineZ80 {
|
2017-05-31 02:41:23 +00:00
|
|
|
CPU::Z80::AllRAMProcessor *_processor;
|
2017-05-22 23:49:38 +00:00
|
|
|
BusOperationHandler *_busOperationHandler;
|
|
|
|
|
|
|
|
NSMutableArray<CSTestMachineZ80BusOperationCapture *> *_busOperationCaptures;
|
2017-05-23 01:50:34 +00:00
|
|
|
int _timeSeekingReadOpcode;
|
2017-05-20 01:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Lifecycle
|
|
|
|
|
|
|
|
- (instancetype)init {
|
|
|
|
if(self = [super init]) {
|
2017-05-31 02:41:23 +00:00
|
|
|
_processor = CPU::Z80::AllRAMProcessor::Processor();
|
2017-06-02 02:33:05 +00:00
|
|
|
_processor->reset_power_on();
|
2017-05-22 23:49:38 +00:00
|
|
|
_busOperationHandler = new BusOperationHandler(self);
|
2017-05-29 19:57:27 +00:00
|
|
|
_busOperationCaptures = [[NSMutableArray alloc] init];
|
2017-05-20 01:20:28 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
2017-05-22 23:49:38 +00:00
|
|
|
delete _busOperationHandler;
|
2017-05-17 02:05:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Accessors
|
|
|
|
|
|
|
|
- (void)setData:(NSData *)data atAddress:(uint16_t)startAddress {
|
2017-05-31 02:41:23 +00:00
|
|
|
_processor->set_data_at_address(startAddress, data.length, (const uint8_t *)data.bytes);
|
2017-05-17 02:05:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)runForNumberOfCycles:(int)cycles {
|
2017-07-24 02:21:39 +00:00
|
|
|
_processor->run_for(Cycles(cycles));
|
2017-05-17 02:05:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setValue:(uint16_t)value forRegister:(CSTestMachineZ80Register)reg {
|
2017-05-31 02:41:23 +00:00
|
|
|
_processor->set_value_of_register(registerForRegister(reg), value);
|
2017-05-17 02:05:42 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 01:53:39 +00:00
|
|
|
- (void)setValue:(uint8_t)value atAddress:(uint16_t)address {
|
2017-05-31 02:41:23 +00:00
|
|
|
_processor->set_data_at_address(address, 1, &value);
|
2017-05-20 01:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (uint8_t)valueAtAddress:(uint16_t)address {
|
|
|
|
uint8_t value;
|
2017-05-31 02:41:23 +00:00
|
|
|
_processor->get_data_at_address(address, 1, &value);
|
2017-05-20 01:53:39 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-05-17 02:05:42 +00:00
|
|
|
- (uint16_t)valueForRegister:(CSTestMachineZ80Register)reg {
|
2017-05-31 02:41:23 +00:00
|
|
|
return _processor->get_value_of_register(registerForRegister(reg));
|
2017-05-17 02:05:42 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 15:54:27 +00:00
|
|
|
- (BOOL)isHalted {
|
2017-05-31 02:41:23 +00:00
|
|
|
return _processor->get_halt_line() ? YES : NO;
|
2017-05-29 15:54:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 00:17:13 +00:00
|
|
|
- (int)completedHalfCycles {
|
|
|
|
return _processor->get_timestamp().as_int();
|
2017-06-13 02:22:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 21:41:45 +00:00
|
|
|
- (void)setNmiLine:(BOOL)nmiLine {
|
|
|
|
_nmiLine = nmiLine;
|
|
|
|
_processor->set_non_maskable_interrupt_line(nmiLine ? true : false);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setIrqLine:(BOOL)irqLine {
|
|
|
|
_irqLine = irqLine;
|
|
|
|
_processor->set_interrupt_line(irqLine ? true : false);
|
|
|
|
}
|
|
|
|
|
2017-06-23 01:09:26 +00:00
|
|
|
- (void)setWaitLine:(BOOL)waitLine {
|
|
|
|
_waitLine = waitLine;
|
|
|
|
_processor->set_wait_line(waitLine ? true : false);
|
|
|
|
}
|
|
|
|
|
2017-06-04 01:22:16 +00:00
|
|
|
- (CPU::AllRAMProcessor *)processor {
|
|
|
|
return _processor;
|
|
|
|
}
|
|
|
|
|
2017-05-22 23:49:38 +00:00
|
|
|
#pragma mark - Bus operation accumulation
|
|
|
|
|
2017-05-30 15:59:07 +00:00
|
|
|
- (void)setCaptureBusActivity:(BOOL)captureBusActivity {
|
|
|
|
_captureBusActivity = captureBusActivity;
|
2017-05-31 02:41:23 +00:00
|
|
|
_processor->set_memory_access_delegate(captureBusActivity ? _busOperationHandler : nullptr);
|
2017-05-30 15:59:07 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 00:17:13 +00:00
|
|
|
- (void)testMachineDidPerformBusOperation:(CPU::Z80::PartialMachineCycle::Operation)operation address:(uint16_t)address value:(uint8_t)value timeStamp:(HalfCycles)timeStamp {
|
2017-05-22 23:49:38 +00:00
|
|
|
if(self.captureBusActivity) {
|
2017-05-29 19:57:27 +00:00
|
|
|
CSTestMachineZ80BusOperationCapture *capture = [[CSTestMachineZ80BusOperationCapture alloc] init];
|
2017-06-22 00:32:08 +00:00
|
|
|
switch(operation) {
|
2017-06-22 00:38:08 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Write:
|
2017-06-22 00:32:08 +00:00
|
|
|
capture.operation = CSTestMachineZ80BusOperationCaptureOperationWrite;
|
|
|
|
break;
|
|
|
|
|
2017-06-22 00:38:08 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Read:
|
2017-06-22 00:32:08 +00:00
|
|
|
capture.operation = CSTestMachineZ80BusOperationCaptureOperationRead;
|
|
|
|
break;
|
|
|
|
|
2017-06-22 00:38:08 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Refresh:
|
2017-06-22 00:32:08 +00:00
|
|
|
capture.operation = CSTestMachineZ80BusOperationCaptureOperationReadOpcode;
|
|
|
|
break;
|
|
|
|
|
2017-06-22 00:38:08 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Input:
|
2017-06-22 00:32:08 +00:00
|
|
|
capture.operation = CSTestMachineZ80BusOperationCaptureOperationPortRead;
|
|
|
|
break;
|
|
|
|
|
2017-06-22 00:38:08 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Output:
|
2017-06-22 00:32:08 +00:00
|
|
|
capture.operation = CSTestMachineZ80BusOperationCaptureOperationPortWrite;
|
|
|
|
break;
|
|
|
|
|
2017-06-22 00:38:08 +00:00
|
|
|
case CPU::Z80::PartialMachineCycle::Internal:
|
2017-06-22 00:32:08 +00:00
|
|
|
capture.operation = CSTestMachineZ80BusOperationCaptureOperationInternalOperation;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: return;
|
2017-05-22 23:49:38 +00:00
|
|
|
}
|
2017-06-22 00:32:08 +00:00
|
|
|
capture.address = address;
|
|
|
|
capture.value = value;
|
2017-07-28 00:17:13 +00:00
|
|
|
capture.timeStamp = timeStamp.as_int();
|
2017-06-22 00:32:08 +00:00
|
|
|
|
|
|
|
[_busOperationCaptures addObject:capture];
|
2017-05-22 23:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray<CSTestMachineZ80BusOperationCapture *> *)busOperationCaptures {
|
|
|
|
return [_busOperationCaptures copy];
|
|
|
|
}
|
|
|
|
|
2017-05-17 02:05:42 +00:00
|
|
|
@end
|