Skip to content

Commit 82c1258

Browse files
author
Federico Berti
committed
add CpuConfig per instance
1 parent 9ea7f86 commit 82c1258

9 files changed

Lines changed: 88 additions & 52 deletions

File tree

src/main/java/m68k/cpu/Cpu.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,8 @@ default int readMemoryWordSigned(int addr)
151151
Instruction getInstructionFor(int opcode);
152152
DisassembledOperand disassembleSrcEA(int address, int mode, int reg, Size sz);
153153
DisassembledOperand disassembleDstEA(int address, int mode, int reg, Size sz);
154+
155+
default CpuConfig getConfig(){
156+
return CpuConfig.DEFAULT_CONFIG;
157+
}
154158
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package m68k.cpu;
2+
3+
import java.util.StringJoiner;
4+
5+
/**
6+
* Federico Berti
7+
* <p>
8+
* Copyright 2025
9+
*/
10+
public class CpuConfig {
11+
public static final CpuConfig DEFAULT_CONFIG = new CpuConfig(false, false, false);
12+
public final boolean emulateBrokenTasWrite;
13+
public final boolean accurateDivTiming;
14+
15+
//TODO not working
16+
public final boolean enablePrefetch;
17+
18+
public CpuConfig(boolean brokenTas, boolean divTiming, boolean enablePrefetch){
19+
emulateBrokenTasWrite = brokenTas;
20+
accurateDivTiming = divTiming;
21+
this.enablePrefetch = enablePrefetch;
22+
}
23+
24+
/**
25+
* true -> hardware where write-back to *memory* doesn't work
26+
*/
27+
public boolean emulateBrokenTasWrite(){
28+
return emulateBrokenTasWrite;
29+
}
30+
31+
public boolean accurateDivTiming() {
32+
return accurateDivTiming;
33+
}
34+
35+
public boolean enablePrefetch() {
36+
return enablePrefetch;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return new StringJoiner(", ", CpuConfig.class.getSimpleName() + "[", "]")
42+
.add("emulateBrokenTasWrite=" + emulateBrokenTasWrite)
43+
.add("accurateDivTiming=" + accurateDivTiming)
44+
.add("enablePrefetch=" + enablePrefetch)
45+
.toString();
46+
}
47+
}

src/main/java/m68k/cpu/MC68000.java

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package m68k.cpu;
22

33
import m68k.cpu.instructions.*;
4+
import m68k.util.LogHelper;
5+
import org.slf4j.Logger;
46

57
/*
68
// M68k - Java Amiga MachineCore
@@ -28,44 +30,31 @@
2830
*/
2931
public class MC68000 extends CpuCore implements InstructionSet
3032
{
31-
32-
public static final boolean ENABLE_PREFETCH = Boolean.valueOf(System.getProperty("68k.enable.prefetch", "false"));
33-
34-
static {
35-
initProperties();
36-
}
37-
38-
private static void initProperties() {
39-
TAS.EMULATE_BROKEN_TAS = Boolean.valueOf(System.getProperty("68k.broken.tas", "false"));
40-
if(TAS.EMULATE_BROKEN_TAS){
41-
System.out.println("Emulating broken TAS instruction");
42-
}
43-
DIVU.ACCURATE_DIV_TIMING = Boolean.valueOf(System.getProperty("68k.accurate.div.timing", "false"));
44-
if(DIVU.ACCURATE_DIV_TIMING){
45-
System.out.println("Using accurate DIVU/S timing");
46-
}
47-
if(ENABLE_PREFETCH){
48-
System.out.println("Enable prefetch");
49-
}
50-
}
51-
33+
private final static Logger LOG = LogHelper.getLogger(MC68000.class.getSimpleName());
5234
protected final Instruction[] i_table;
5335
protected final Instruction unknown;
5436
protected int loaded_ops;
5537

56-
public MC68000()
57-
{
38+
protected final CpuConfig cpuConfig;
39+
40+
public MC68000() {
41+
this(CpuConfig.DEFAULT_CONFIG);
42+
}
43+
44+
public MC68000(CpuConfig c){
45+
cpuConfig = c;
5846
i_table = new Instruction[NUM_OPCODES];
5947
unknown = new UNKNOWN(this);
6048
for(int i = 0; i < NUM_OPCODES; i++)
6149
i_table[i] = unknown;
6250
loaded_ops = 0;
6351
loadInstructionSet();
52+
LOG.info("Using CpuConfig: {}", cpuConfig);
6453
}
6554

6655
@Override
6756
public int execute() {
68-
if(ENABLE_PREFETCH){
57+
if(cpuConfig.enablePrefetch){
6958
return executePrefetch();
7059
} else {
7160
return executeNoPrefetch();
@@ -95,7 +84,7 @@ private int executeNoPrefetch() {
9584

9685
@Override
9786
public int getPrefetchWord(){
98-
return ENABLE_PREFETCH ? ir : readMemoryWord(reg_pc);
87+
return cpuConfig.enablePrefetch ? ir : readMemoryWord(reg_pc);
9988
}
10089

10190
protected void loadInstructionSet()
@@ -212,4 +201,9 @@ public Instruction getInstructionAt(int address)
212201
int opcode = readMemoryWord(address);
213202
return getInstructionFor(opcode);
214203
}
204+
205+
@Override
206+
public CpuConfig getConfig() {
207+
return cpuConfig;
208+
}
215209
}

src/main/java/m68k/cpu/instructions/DIVS.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333
public class DIVS implements InstructionHandler
3434
{
3535
protected final Cpu cpu;
36+
protected final boolean accurateDivTiming;
3637

3738
public DIVS(Cpu cpu)
3839
{
3940
this.cpu = cpu;
41+
accurateDivTiming = cpu.getConfig().accurateDivTiming;
4042
}
4143

4244
public void register(InstructionSet is)
@@ -120,7 +122,7 @@ protected final int divs(int opcode)
120122
cpu.clrFlags((Cpu.V_FLAG | Cpu.C_FLAG));
121123
}
122124
//worst case but less than 10% difference between best and worst cases
123-
time = DIVU.ACCURATE_DIV_TIMING ? getDivs68kCycles(d, s) : 158;
125+
time = accurateDivTiming ? getDivs68kCycles(d, s) : 158;
124126
}
125127

126128
return time + OperandTiming.getOperandTiming(op, Size.Word);

src/main/java/m68k/cpu/instructions/DIVU.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030

3131
public class DIVU implements InstructionHandler
3232
{
33-
public static boolean ACCURATE_DIV_TIMING;
34-
3533
protected final Cpu cpu;
34+
protected final boolean accurateDivTiming;
3635

3736
public DIVU(Cpu cpu)
3837
{
3938
this.cpu = cpu;
39+
accurateDivTiming = cpu.getConfig().accurateDivTiming;
4040
}
4141

4242
public void register(InstructionSet is)
@@ -126,7 +126,7 @@ protected final int divu(int opcode)
126126
cpu.clrFlags((Cpu.V_FLAG | Cpu.C_FLAG));
127127
}
128128
//worst case but less than 10% difference between best and worst cases
129-
time = ACCURATE_DIV_TIMING ? getDivu68kCycles(dl, s) : 140;
129+
time = accurateDivTiming ? getDivu68kCycles(dl, s) : 140;
130130
}
131131

132132
return time + OperandTiming.getOperandTiming(op, Size.Word);

src/main/java/m68k/cpu/instructions/TAS.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
//
3030
*/
3131
public class TAS implements InstructionHandler {
32-
//EMULATE_BROKEN_TAS -> hardware where write-back to *memory* doesn't work
33-
public static boolean EMULATE_BROKEN_TAS;
3432

3533
protected final Cpu cpu;
34+
protected final boolean emulateBrokenTasWrite;
3635

3736
public TAS(Cpu cpu) {
3837
this.cpu = cpu;
38+
emulateBrokenTasWrite = cpu.getConfig().emulateBrokenTasWrite;
3939
}
4040

4141
public final void register(InstructionSet is)
@@ -90,7 +90,7 @@ protected synchronized final int tas(int opcode)
9090
}
9191
cpu.clrFlags(Cpu.C_FLAG | Cpu.V_FLAG);
9292

93-
boolean writeBack = !EMULATE_BROKEN_TAS || (EMULATE_BROKEN_TAS && op.isRegisterMode());
93+
boolean writeBack = !emulateBrokenTasWrite || op.isRegisterMode();
9494
if (writeBack) {
9595
op.setByte(v | 0x80);
9696
}

src/main/java/m68k/util/MC68000Helper.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import m68k.cpu.DisassembledInstruction;
2424
import m68k.cpu.Instruction;
2525
import m68k.cpu.MC68000;
26-
import m68k.cpu.instructions.TAS;
2726
import org.slf4j.Logger;
2827

2928
import java.util.Arrays;
@@ -35,19 +34,14 @@ public class MC68000Helper {
3534
private final static Logger LOG = LogHelper.getLogger(MC68000Helper.class.getSimpleName());
3635

3736
public static final boolean STOP_ON_EXCEPTION;
38-
public static final boolean GENESIS_TAS_BROKEN;
3937
public static final boolean M68K_DEBUG;
4038
public final static int OVERCLOCK_FACTOR;
4139

4240
static {
4341
STOP_ON_EXCEPTION =
4442
Boolean.parseBoolean(System.getProperty("68k.stop.on.exception", "false"));
45-
GENESIS_TAS_BROKEN = Boolean.parseBoolean(System.getProperty("68k.broken.tas", "true"));
4643
M68K_DEBUG = Boolean.parseBoolean(System.getProperty("68k.debug", "false"));
4744
OVERCLOCK_FACTOR = Integer.parseInt(System.getProperty("68k.overclock.factor", "0"));
48-
if (GENESIS_TAS_BROKEN != TAS.EMULATE_BROKEN_TAS) {
49-
LOG.info("Overriding 68k TAS broken setting: {}", GENESIS_TAS_BROKEN);
50-
}
5145
if (M68K_DEBUG) {
5246
LOG.info("68k debug mode: true");
5347
}

src/test/java/m68k/cpu/instructions/TasTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package m68k.cpu.instructions;
22

33
import m68k.cpu.Cpu;
4+
import m68k.cpu.CpuConfig;
45
import m68k.cpu.MC68000;
56
import m68k.memory.AddressSpace;
67
import m68k.memory.MemorySpace;
@@ -19,12 +20,17 @@ public class TasTest {
1920

2021
AddressSpace bus;
2122
Cpu cpu;
23+
static CpuConfig tasBroken = new CpuConfig(true, false, false);
2224

2325
@BeforeEach public void setUp() {
26+
setUp(CpuConfig.DEFAULT_CONFIG);
27+
}
28+
29+
private void setUp(CpuConfig cpuConfig) {
2430
//create 1kb of memory for the cpu
2531
bus = new MemorySpace(1);
2632

27-
cpu = new MC68000();
33+
cpu = new MC68000(cpuConfig);
2834
cpu.setAddressSpace(bus);
2935
cpu.reset();
3036
cpu.setAddrRegisterLong(7, 0x200);
@@ -34,7 +40,6 @@ public class TasTest {
3440
// 0100 1010 1100 0000
3541
@Test
3642
public void testTasRegOk() {
37-
TAS.EMULATE_BROKEN_TAS = false;
3843
bus.writeWord(4, 0x4AC0); //TAS D0
3944
cpu.setPC(4);
4045
cpu.setDataRegisterLong(0, 0);
@@ -45,7 +50,7 @@ public void testTasRegOk() {
4550
}
4651

4752
@Test public void testTasRegBroken() {
48-
TAS.EMULATE_BROKEN_TAS = true;
53+
setUp(tasBroken);
4954
bus.writeWord(4, 0x4AC0); //TAS D0
5055
cpu.setPC(4);
5156
cpu.setDataRegisterLong(0, 0);
@@ -56,7 +61,6 @@ public void testTasRegOk() {
5661
}
5762

5863
@Test public void testTasMemOk() {
59-
TAS.EMULATE_BROKEN_TAS = false;
6064
int val = 0x20;
6165
int memAddr = 100;
6266
bus.writeWord(4, 0x4AD0); //TAS (A0)
@@ -71,7 +75,7 @@ public void testTasRegOk() {
7175
}
7276

7377
@Test public void testTasMemBroken() {
74-
TAS.EMULATE_BROKEN_TAS = true;
78+
setUp(tasBroken);
7579
bus.writeWord(4, 0x4AD0); //TAS (A0)
7680
bus.writeByte(100, 0x20);
7781
cpu.setPC(4);

src/test/java/miggy/cpu/instructions/TASTest.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
package miggy.cpu.instructions;
22

3-
import m68k.cpu.instructions.TAS;
43
import miggy.BasicSetup;
54
import miggy.SystemModel;
65
import miggy.SystemModel.CpuFlag;
7-
import org.junit.jupiter.api.BeforeEach;
86
import org.junit.jupiter.api.Test;
97

108
import static m68k.util.TestCpuUtil.*;
119

1210
// $Revision: 21 $
1311
public class TASTest extends BasicSetup {
1412

15-
@Override
16-
@BeforeEach
17-
protected void setUp() {
18-
super.setUp();
19-
TAS.EMULATE_BROKEN_TAS = false;
20-
}
21-
2213
@Test public void testSet() {
2314
setInstructionAtPC(0x4ac0); //tas d0
2415
SystemModel.CPU.setDataRegister(0, 0x87654381);

0 commit comments

Comments
 (0)