2022-03-18 21:24:12 +00:00
|
|
|
|
//
|
|
|
|
|
// DingusdevPowerPCTests.mm
|
|
|
|
|
// Clock Signal
|
|
|
|
|
//
|
|
|
|
|
// Created by Thomas Harte on 18/03/2022.
|
|
|
|
|
// Copyright 2022 Thomas Harte. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
|
|
#include "../../../InstructionSets/PowerPC/Decoder.hpp"
|
|
|
|
|
|
2022-03-25 00:44:03 +00:00
|
|
|
|
using namespace InstructionSet::PowerPC;
|
2022-03-18 21:24:12 +00:00
|
|
|
|
|
|
|
|
|
@interface DingusdevPowerPCTests : XCTestCase
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation DingusdevPowerPCTests
|
|
|
|
|
|
|
|
|
|
- (void)testDecoding {
|
|
|
|
|
NSData *const testData =
|
|
|
|
|
[NSData dataWithContentsOfURL:
|
|
|
|
|
[[NSBundle bundleForClass:[self class]]
|
|
|
|
|
URLForResource:@"ppcdisasmtest"
|
|
|
|
|
withExtension:@"csv"
|
|
|
|
|
subdirectory:@"dingusdev PowerPC tests"]];
|
|
|
|
|
|
|
|
|
|
NSString *const wholeFile = [[NSString alloc] initWithData:testData encoding:NSUTF8StringEncoding];
|
|
|
|
|
NSArray<NSString *> *const lines = [wholeFile componentsSeparatedByString:@"\n"];
|
|
|
|
|
|
|
|
|
|
InstructionSet::PowerPC::Decoder decoder(InstructionSet::PowerPC::Model::MPC601);
|
|
|
|
|
for(NSString *const line in lines) {
|
|
|
|
|
// Ignore empty lines and comments.
|
|
|
|
|
if([line length] == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if([line characterAtIndex:0] == '#') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSArray<NSString *> *const columns = [line componentsSeparatedByString:@","];
|
|
|
|
|
|
2022-03-25 00:44:03 +00:00
|
|
|
|
// Columns are 1: address; 2: opcode; 3–: specific to the instruction.
|
2022-03-18 21:24:12 +00:00
|
|
|
|
const uint32_t address = uint32_t(std::strtol([columns[0] UTF8String], 0, 16));
|
|
|
|
|
const uint32_t opcode = uint32_t(std::strtol([columns[1] UTF8String], 0, 16));
|
|
|
|
|
NSString *const operation = columns[2];
|
|
|
|
|
const auto instruction = decoder.decode(opcode);
|
|
|
|
|
|
|
|
|
|
switch(instruction.operation) {
|
|
|
|
|
default:
|
2022-03-18 23:55:26 +00:00
|
|
|
|
NSAssert(FALSE, @"Didn't handle %@", line);
|
2022-03-18 21:24:12 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2022-03-25 10:25:06 +00:00
|
|
|
|
case Operation::bclrx:
|
2022-03-25 00:44:03 +00:00
|
|
|
|
case Operation::bcctrx: {
|
|
|
|
|
NSString *baseOperation = nil;
|
2022-03-25 10:25:06 +00:00
|
|
|
|
|
2022-03-25 00:44:03 +00:00
|
|
|
|
switch(instruction.branch_options()) {
|
2022-03-25 10:25:06 +00:00
|
|
|
|
case BranchOptions::Always: baseOperation = @"b"; break;
|
2022-03-25 00:44:03 +00:00
|
|
|
|
case BranchOptions::Clear:
|
|
|
|
|
switch(Condition(instruction.bi() & 3)) {
|
|
|
|
|
default: break;
|
2022-03-25 10:25:06 +00:00
|
|
|
|
case Condition::Negative: baseOperation = @"bge"; break;
|
|
|
|
|
case Condition::Positive: baseOperation = @"ble"; break;
|
|
|
|
|
case Condition::Zero: baseOperation = @"bne"; break;
|
2022-03-25 00:44:03 +00:00
|
|
|
|
case Condition::SummaryOverflow:
|
2022-03-25 10:25:06 +00:00
|
|
|
|
baseOperation = @"bns";
|
2022-03-25 00:44:03 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case BranchOptions::Set:
|
|
|
|
|
switch(Condition(instruction.bi() & 3)) {
|
|
|
|
|
default: break;
|
2022-03-25 10:25:06 +00:00
|
|
|
|
case Condition::Negative: baseOperation = @"blt"; break;
|
|
|
|
|
case Condition::Positive: baseOperation = @"bgt"; break;
|
|
|
|
|
case Condition::Zero: baseOperation = @"beq"; break;
|
2022-03-25 00:44:03 +00:00
|
|
|
|
case Condition::SummaryOverflow:
|
2022-03-25 10:25:06 +00:00
|
|
|
|
baseOperation = @"bso";
|
2022-03-25 00:44:03 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 10:25:06 +00:00
|
|
|
|
if(instruction.operation == Operation::bcctrx) {
|
|
|
|
|
baseOperation = [baseOperation stringByAppendingString:@"ctr"];
|
|
|
|
|
} else {
|
|
|
|
|
baseOperation = [baseOperation stringByAppendingString:@"lr"];
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 00:44:03 +00:00
|
|
|
|
if(!baseOperation) {
|
|
|
|
|
NSAssert(FALSE, @"Didn't handle bi %d for bo %d, %@", instruction.bi() & 3, instruction.bo(), line);
|
|
|
|
|
} else {
|
|
|
|
|
if(instruction.lk()) {
|
|
|
|
|
baseOperation = [baseOperation stringByAppendingString:@"l"];
|
|
|
|
|
}
|
|
|
|
|
if(instruction.branch_prediction_hint()) {
|
|
|
|
|
baseOperation = [baseOperation stringByAppendingString:@"+"];
|
|
|
|
|
}
|
|
|
|
|
XCTAssertEqualObjects(operation, baseOperation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(instruction.bi() & ~3) {
|
|
|
|
|
NSString *const expectedCR = [NSString stringWithFormat:@"cr%d", instruction.bi() >> 2];
|
|
|
|
|
XCTAssertEqualObjects(columns[3], expectedCR);
|
|
|
|
|
}
|
|
|
|
|
} break;
|
|
|
|
|
|
2022-03-18 23:55:26 +00:00
|
|
|
|
case Operation::bcx: {
|
2022-03-25 00:44:03 +00:00
|
|
|
|
switch(instruction.branch_options()) {
|
|
|
|
|
case BranchOptions::Always:
|
|
|
|
|
XCTAssertEqualObjects(operation, @"b");
|
|
|
|
|
break;
|
2022-03-18 23:55:26 +00:00
|
|
|
|
default:
|
|
|
|
|
NSLog(@"No opcode tested for bcx with bo %02x", instruction.bo());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 21:24:12 +00:00
|
|
|
|
const uint32_t destination = uint32_t(std::strtol([columns[3] UTF8String], 0, 16));
|
2022-03-18 23:55:26 +00:00
|
|
|
|
const uint32_t decoded_destination = instruction.bd() + address;
|
|
|
|
|
XCTAssertEqual(decoded_destination, destination);
|
|
|
|
|
} break;
|
2022-03-18 21:24:12 +00:00
|
|
|
|
|
2022-03-18 23:55:26 +00:00
|
|
|
|
case Operation::bx: {
|
2022-03-18 21:24:12 +00:00
|
|
|
|
switch((instruction.aa() ? 2 : 0) | (instruction.lk() ? 1 : 0)) {
|
|
|
|
|
case 0: XCTAssertEqualObjects(operation, @"b"); break;
|
|
|
|
|
case 1: XCTAssertEqualObjects(operation, @"bl"); break;
|
|
|
|
|
case 2: XCTAssertEqualObjects(operation, @"ba"); break;
|
|
|
|
|
case 3: XCTAssertEqualObjects(operation, @"bla"); break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 23:55:26 +00:00
|
|
|
|
const uint32_t destination = uint32_t(std::strtol([columns[3] UTF8String], 0, 16));
|
2022-03-18 21:24:12 +00:00
|
|
|
|
const uint32_t decoded_destination =
|
|
|
|
|
instruction.li() + (instruction.aa() ? 0 : address);
|
|
|
|
|
XCTAssertEqual(decoded_destination, destination);
|
|
|
|
|
} break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|