Skip to content

Commit dbd7689

Browse files
author
Federico Berti
committed
mcd: cdc refactor
1 parent 48831bd commit dbd7689

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

src/main/java/mcd/cdc/CdcImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void write(MegaCdDict.RegSpecMcd regSpec, int address, int value, Size si
8686
case MCD_CDC_MODE -> {
8787
int resWord = memoryContext.handleRegWrite(SUB_M68K, regSpec, address, value, size);
8888
cdcContext.address = resWord & NUM_CDC_REG_MASK;
89-
transfer.destination = (resWord >> 8) & 7;
89+
transfer.destination = CdcTransferDestination.vals[(resWord >> 8) & 7];
9090
transfer.completed = (resWord >> 15) & 1;
9191
transfer.ready = (resWord >> 14) & 1;
9292
}
@@ -121,7 +121,7 @@ public int read(MegaCdDict.RegSpecMcd regSpec, int address, Size size) {
121121
int res2 = readBuffer(memoryContext.getRegBuffer(BufferUtil.CpuDeviceAccess.SUB_M68K, regSpec),
122122
address & MDC_SUB_GATE_REGS_MASK, size);
123123
//according to mcd-ver, reading DSR,EDT resets them to 0 (only for DMA transfers?)
124-
if (transfer.destination != 2 && transfer.destination != 3) {
124+
if (transfer.destination.isDma()) {
125125
transfer.completed = 0;
126126
transfer.ready = 0;
127127
updateCdcMode4();
@@ -396,7 +396,7 @@ public void step(int cycles) {
396396
}
397397

398398
private void updateCdcMode4() {
399-
int val = cdcContext.address | (transfer.destination << 8) |
399+
int val = cdcContext.address | (transfer.destination.ordinal() << 8) |
400400
(transfer.ready << 14) | (transfer.completed << 15);
401401
//TODO improve, cannot use the regWriteHandlers as bus writes cannot modify EDT,DSR
402402
writeBufferRaw(memoryContext.getRegBuffer(SUB_M68K, MCD_CDC_MODE),

src/main/java/mcd/cdc/CdcModel.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,29 @@ interface CdcTransferAction {
8686
void complete();
8787
}
8888

89+
enum CdcTransferDestination {
90+
NONE_0, NONE_1, MAIN_READ_2, SUB_READ_3, DMA_PCM_4, DMA_PROGRAM_5, NONE_6, DMA_SUB_WRAM_7;
91+
92+
public static CdcTransferDestination[] vals = CdcTransferDestination.values();
93+
private boolean dmaDestination;
94+
private boolean valid;
95+
96+
CdcTransferDestination() {
97+
dmaDestination = name().startsWith("DMA");
98+
valid = !name().startsWith("NONE");
99+
}
100+
101+
public boolean isDma() {
102+
return dmaDestination;
103+
}
104+
105+
public boolean isValid() {
106+
return valid;
107+
}
108+
}
109+
89110
class CdcTransfer {
90-
public int destination; //n3
111+
public CdcTransferDestination destination; //n3
91112
public int address; //n19
92113

93114
public int source; //n16

src/main/java/mcd/cdc/CdcTransferHelper.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ public void start() {
4545
if (t.enable == 0) return;
4646
t.active = 1;
4747
t.busy = 1;
48+
assert t.destination.isValid() : t.destination;
4849
//DSR is set when destination is mainCpuRead or subCpuRead
49-
t.ready = (t.destination == 2 || t.destination == 3) ? 1 : 0;
50+
t.ready = (t.destination.isDma()) ? 0 : 1;
5051
t.completed = 0;
5152
CdcModel.CdcContext ctx = cdc.getContext();
5253
ctx.irq.transfer.pending = 0;
@@ -69,13 +70,9 @@ public void stop() {
6970

7071
@Override
7172
public int read() {
72-
if (t.destination != 2 && t.destination != 3) {
73+
if (t.destination.isDma()) {
7374
if (t.ready == 0) return 0xFFFF;
7475
}
75-
return readInternal();
76-
}
77-
78-
private int readInternal() {
7976
int data = ram.getShort(t.source);
8077
if (verbose) LOG.info("CDC,RAM_R,ram[{}]={}", th(t.source), th(data));
8178
t.source = (t.source + 2) & 0x3FFF;
@@ -93,23 +90,23 @@ private int readInternal() {
9390

9491
@Override
9592
public void dma() {
96-
if (t.active == 0) {
93+
if (t.active == 0 || !t.destination.isDma()) {
9794
return;
9895
}
99-
if (t.destination != 4 && t.destination != 5 && t.destination != 7) return;
96+
assert t.destination.isValid();
10097
int data = ram.getShort(t.source);
10198
switch (t.destination) {
102-
case 7 -> { //WRAM
99+
case DMA_SUB_WRAM_7 -> { //WRAM
103100
int baseAddr = memoryContext.wramSetup.mode == MegaCdMemoryContext.WordRamMode._1M
104101
? START_MCD_SUB_WORD_RAM_1M : START_MCD_SUB_WORD_RAM_2M;
105102
memoryContext.writeWordRamWord(SUB_M68K, baseAddr | t.address, data);
106103
if (verbose) LOG.info("CDC,DMA_WRAM,wram[{}]={}", th(baseAddr | t.address), th(data));
107104
}
108-
case 5 -> { //PRG-RAM
105+
case DMA_PROGRAM_5 -> { //PRG-RAM
109106
memoryContext.writeProgRam(t.address, data, Size.WORD);
110107
//mcd.write(1, 1, 0x000000 | (n19) address & ~1, data);
111108
}
112-
case 4 -> {
109+
case DMA_PCM_4 -> {
113110
assert t.length > 0;
114111
//PCM DMA requires two 8-bit writes per transfer
115112
writePcm(t.address, data >> 8);

src/main/java/mcd/cdd/CdBiosHelper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ public static String getCdBiosEntryPointIfAny(int pc) {
263263
}
264264

265265
public static void logCdPcInfo(int pc, MC68000 cpu) {
266+
if (!enabled) {
267+
return;
268+
}
266269
if (pc >= LOW_ENTRY && pc <= HIGH_ENTRY) {
267270
String res = CdBiosHelper.getCdBiosEntryPointIfAny(pc);
268271
if (res != CdBiosHelper.NO_ENTRY_POINT) {

0 commit comments

Comments
 (0)