Skip to content

Commit 3480d36

Browse files
authored
CPU Refactoring (#119)
* Refactor CPU functions and bump dependencies. * Finish refactor and fix various io calls. * Add new tests for ByteRegisterInstructions * Refactor multiple components and add new unit tests * Bump snakeyaml dependency.
1 parent b56b742 commit 3480d36

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2897
-2034
lines changed

.github/workflows/gradle.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ jobs:
66
steps:
77
- name: Checkout
88
uses: actions/checkout@v3
9-
- name: Setup JDK 11
9+
- name: Setup JDK 17
1010
uses: actions/setup-java@v3
1111
with:
12-
java-version: '11'
12+
java-version: '17'
1313
distribution: 'adopt'
1414
- name: Grant execute permission for gradlew
1515
run: chmod +x gradlew

build.gradle

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ plugins {
88
id 'java'
99
id 'jacoco'
1010
id 'application'
11-
id 'com.github.johnrengelman.shadow' version '8.1.1'
11+
id 'com.gradleup.shadow' version '8.3.5'
12+
id 'org.sonarqube' version '6.0.1.5171'
1213
}
1314

14-
group 'ca.craigthomas'
15-
version '1.0'
15+
group = 'ca.craigthomas'
16+
version = '1.0'
1617

17-
mainClassName = 'ca.craigthomas.yacoco3e.runner.Runner'
18+
java {
19+
sourceCompatibility = 1.8
20+
}
1821

19-
sourceCompatibility = 1.8
22+
application {
23+
mainClass.set("ca.craigthomas.yacoco3e.runner.Runner")
24+
}
2025

2126
repositories {
2227
mavenCentral()
@@ -26,7 +31,7 @@ dependencies {
2631
implementation 'commons-io:commons-io:2.18.0'
2732
implementation 'org.apache.commons:commons-lang3:3.17.0'
2833
implementation 'com.beust:jcommander:1.82'
29-
implementation 'org.yaml:snakeyaml:2.3'
34+
implementation 'org.yaml:snakeyaml:2.4'
3035
testImplementation 'junit:junit:4.13.2'
3136
testImplementation 'org.mockito:mockito-core:5.15.2'
3237
}

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

src/main/java/ca/craigthomas/yacoco3e/common/IO.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/*
2-
* Copyright (C) 2017-2019 Craig Thomas
2+
* Copyright (C) 2017-2025 Craig Thomas
33
* This project uses an MIT style license - see LICENSE for details.
44
*/
55
package ca.craigthomas.yacoco3e.common;
66

7-
import ca.craigthomas.yacoco3e.components.Memory;
87
import org.apache.commons.io.IOUtils;
98

109
import java.io.*;
Lines changed: 42 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022 Craig Thomas
2+
* Copyright (C) 2022-2025 Craig Thomas
33
* This project uses an MIT style license - see LICENSE for details.
44
*/
55
package ca.craigthomas.yacoco3e.components;
@@ -8,7 +8,6 @@
88

99
import java.util.function.Function;
1010

11-
import static ca.craigthomas.yacoco3e.datatypes.AddressingMode.IMMEDIATE;
1211
import static ca.craigthomas.yacoco3e.datatypes.RegisterSet.*;
1312

1413
/**
@@ -19,185 +18,152 @@
1918
*/
2019
public class BranchInstruction extends Instruction
2120
{
22-
protected Function<InstructionBundle, Boolean> operation;
21+
protected Function<IOController, Boolean> operation;
2322

2423
public BranchInstruction(int opcode,
2524
int ticks,
2625
String mnemonic,
27-
Function<InstructionBundle, Boolean> operation
26+
Function<IOController, Boolean> operation
2827
) {
2928
this.opcodeValue = opcode;
3029
this.mnemonic = mnemonic;
3130
this.ticks = ticks;
3231
this.operation = operation;
33-
this.addressingMode = IMMEDIATE;
34-
this.immediateByte = true;
32+
this.addressingMode = AddressingMode.IMMEDIATE;
33+
this.isByteSized = true;
34+
this.isValidInstruction = true;
35+
this.addressRead = new UnsignedWord();
3536
}
3637

37-
public int call(MemoryResult memoryResult, IOController io) {
38-
if (operation.apply(new InstructionBundle(memoryResult, io)).equals(true)) {
39-
UnsignedByte offset = memoryResult.value.getHigh();
40-
io.regs.pc.add(offset.isNegative() ? offset.getSignedShort() : offset.getShort());
38+
public int call(IOController io) {
39+
if (operation.apply(io).equals(true)) {
40+
io.regs.pc.add(byteRead.isNegative() ? byteRead.getSignedShort() : byteRead.getShort());
4141
}
4242
return ticks;
4343
}
4444

4545
/**
4646
* Branches to the specified address, pushing the value of the PC onto the S
4747
* stack before branching.
48-
*
49-
* @param bundle the InstructionBundle that contains information to process
5048
*/
51-
public static Boolean branchToSubroutine(InstructionBundle bundle) {
52-
bundle.io.pushStack(Register.S, bundle.io.regs.pc);
49+
public static Boolean branchToSubroutine(IOController io) {
50+
io.pushStack(Register.S, io.regs.pc);
5351
return true;
5452
}
5553

5654
/**
5755
* Branch always will always return true when called.
58-
*
59-
* @param bundle the InstructionBundle with the information to process.
6056
*/
61-
public static Boolean branchAlways(InstructionBundle bundle) {
57+
public static Boolean branchAlways(IOController io) {
6258
return true;
6359
}
6460

6561
/**
6662
* Branch never will always return false when called.
67-
*
68-
* @param bundle the InstructionBundle with the information to process.
6963
*/
70-
public static Boolean branchNever(InstructionBundle bundle) {
64+
public static Boolean branchNever(IOController io) {
7165
return false;
7266
}
7367

7468
/**
7569
* Branch on high will return true if carry is not set, and zero is not set.
76-
*
77-
* @param bundle the InstructionBundle with the information to process.
7870
*/
79-
public static Boolean branchOnHigh(InstructionBundle bundle) {
80-
return !bundle.io.regs.cc.isMasked(CC_C) && !bundle.io.regs.cc.isMasked(CC_Z);
71+
public static Boolean branchOnHigh(IOController io) {
72+
return !io.regs.cc.isMasked(CC_C) && !io.regs.cc.isMasked(CC_Z);
8173
}
8274

8375
/**
8476
* Branch on lower will return true if carry is set or zero is set.
85-
*
86-
* @param bundle the InstructionBundle with the information to process.
8777
*/
88-
public static Boolean branchOnLower(InstructionBundle bundle) {
89-
return bundle.io.regs.cc.isMasked(CC_C) || bundle.io.regs.cc.isMasked(CC_Z);
78+
public static Boolean branchOnLower(IOController io) {
79+
return io.regs.cc.isMasked(CC_C) || io.regs.cc.isMasked(CC_Z);
9080
}
9181

9282
/**
9383
* Branch on carry clear will return true if carry is not set.
94-
*
95-
* @param bundle the InstructionBundle with the information to process.
9684
*/
97-
public static Boolean branchOnCarryClear(InstructionBundle bundle) {
98-
return !bundle.io.regs.cc.isMasked(CC_C);
85+
public static Boolean branchOnCarryClear(IOController io) {
86+
return !io.regs.cc.isMasked(CC_C);
9987
}
10088

10189
/**
10290
* Branch on carry set will return true if carry is set.
103-
*
104-
* @param bundle the InstructionBundle with the information to process.
10591
*/
106-
public static Boolean branchOnCarrySet(InstructionBundle bundle) {
107-
return bundle.io.regs.cc.isMasked(CC_C);
92+
public static Boolean branchOnCarrySet(IOController io) {
93+
return io.regs.cc.isMasked(CC_C);
10894
}
10995

11096
/**
11197
* Branch on not equal will return true if the zero flag is set.
112-
*
113-
* @param bundle the InstructionBundle with the information to process.
11498
*/
115-
public static Boolean branchOnNotEqual(InstructionBundle bundle) {
116-
return !bundle.io.regs.cc.isMasked(CC_Z);
99+
public static Boolean branchOnNotEqual(IOController io) {
100+
return !io.regs.cc.isMasked(CC_Z);
117101
}
118102

119103
/**
120104
* Branch on equal will return true if the zero flag is set.
121-
*
122-
* @param bundle the InstructionBundle with the information to process.
123105
*/
124-
public static Boolean branchOnEqual(InstructionBundle bundle) {
125-
return bundle.io.regs.cc.isMasked(CC_Z);
106+
public static Boolean branchOnEqual(IOController io) {
107+
return io.regs.cc.isMasked(CC_Z);
126108
}
127109

128110
/**
129111
* Branch on overflow clear will return true if the overflow flag is clear.
130-
*
131-
* @param bundle the InstructionBundle with the information to process.
132112
*/
133-
public static Boolean branchOnOverflowClear(InstructionBundle bundle) {
134-
return !bundle.io.regs.cc.isMasked(CC_V);
113+
public static Boolean branchOnOverflowClear(IOController io) {
114+
return !io.regs.cc.isMasked(CC_V);
135115
}
136116

137117
/**
138118
* Branch on overflow set will return true if the overflow flag is set.
139-
*
140-
* @param bundle the InstructionBundle with the information to process.
141119
*/
142-
public static Boolean branchOnOverflowSet(InstructionBundle bundle) {
143-
return bundle.io.regs.cc.isMasked(CC_V);
120+
public static Boolean branchOnOverflowSet(IOController io) {
121+
return io.regs.cc.isMasked(CC_V);
144122
}
145123

146124
/**
147125
* Branch on plus will return true if the negative flag is clear.
148-
*
149-
* @param bundle the InstructionBundle with the information to process.
150126
*/
151-
public static Boolean branchOnPlus(InstructionBundle bundle) {
152-
return !bundle.io.regs.cc.isMasked(CC_N);
127+
public static Boolean branchOnPlus(IOController io) {
128+
return !io.regs.cc.isMasked(CC_N);
153129
}
154130

155131
/**
156132
* Branch on minus will return true if the negative flag is set.
157-
*
158-
* @param bundle the InstructionBundle with the information to process.
159133
*/
160-
public static Boolean branchOnMinus(InstructionBundle bundle) {
161-
return bundle.io.regs.cc.isMasked(CC_N);
134+
public static Boolean branchOnMinus(IOController io) {
135+
return io.regs.cc.isMasked(CC_N);
162136
}
163137

164138
/**
165139
* Branch on greater than equal to zero will return true if the negative and overflow
166140
* flags are both set or both clear.
167-
*
168-
* @param bundle the InstructionBundle with the information to process.
169141
*/
170-
public static Boolean branchOnGreaterThanEqualZero(InstructionBundle bundle) {
171-
return bundle.io.regs.cc.isMasked(CC_N) == bundle.io.regs.cc.isMasked(CC_V);
142+
public static Boolean branchOnGreaterThanEqualZero(IOController io) {
143+
return io.regs.cc.isMasked(CC_N) == io.regs.cc.isMasked(CC_V);
172144
}
173145

174146
/**
175147
* Branch on less than zero will return true if the negative and overflow are not equal.
176-
*
177-
* @param bundle the InstructionBundle with the information to process.
178148
*/
179-
public static Boolean branchOnLessThanZero(InstructionBundle bundle) {
180-
return bundle.io.regs.cc.isMasked(CC_N) != bundle.io.regs.cc.isMasked(CC_V);
149+
public static Boolean branchOnLessThanZero(IOController io) {
150+
return io.regs.cc.isMasked(CC_N) != io.regs.cc.isMasked(CC_V);
181151
}
182152

183153
/**
184154
* Branch on greater than zero will return true if the zero flag is clear,
185155
* and if negative and overflow are both set or both clear.
186-
*
187-
* @param bundle the InstructionBundle with the information to process.
188156
*/
189-
public static Boolean branchOnGreaterThanZero(InstructionBundle bundle) {
190-
return !bundle.io.regs.cc.isMasked(CC_Z) && (bundle.io.regs.cc.isMasked(CC_N) == bundle.io.regs.cc.isMasked(CC_V));
157+
public static Boolean branchOnGreaterThanZero(IOController io) {
158+
return !io.regs.cc.isMasked(CC_Z) && (io.regs.cc.isMasked(CC_N) == io.regs.cc.isMasked(CC_V));
191159
}
192160

193161
/**
194162
* Branch on less than equal to zero will return true if the zero flag is set,
195163
* or if overflow and negative are not equal.
196-
*
197-
* @param bundle the InstructionBundle with the information to process.
198164
*/
199-
public static Boolean branchOnLessThanEqualZero(InstructionBundle bundle) {
200-
return bundle.io.regs.cc.isMasked(CC_Z) || (bundle.io.regs.cc.isMasked(CC_N) != bundle.io.regs.cc.isMasked(CC_V));
165+
public static Boolean branchOnLessThanEqualZero(IOController io) {
166+
return io.regs.cc.isMasked(CC_Z) || (io.regs.cc.isMasked(CC_N) != io.regs.cc.isMasked(CC_V));
201167
}
202168

203169
}

0 commit comments

Comments
 (0)