Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(DMA): implement DMA sniffing (partially) #91

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 63 additions & 16 deletions src/peripherals/dma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ const EN = 1 << 0;
const CHn_CTRL_TRIG_WRITE_MASK = 0xffffff;
const CHn_CTRL_TRIG_WC_MASK = READ_ERROR | WRITE_ERROR;

// SNIFF_CTRL bits
const OUT_INV = 1 << 11;
const OUT_REV = 1 << 10;
const SNIFF_BSWAP = 1 << 9;
const CALC_MASK = 0xf;
const CALC_SHIFT = 5;
const DMACH_MASK = 0xf;
const DMACH_SHIFT = 1;

export class RPDMAChannel {
private ctrl = 0;
private readAddr = 0;
Expand Down Expand Up @@ -158,36 +167,53 @@ export class RPDMAChannel {
}

transfer8 = () => {
const { rp2040 } = this;
rp2040.writeUint8(this.writeAddr, rp2040.readUint8(this.readAddr));
const { rp2040, ctrl, dma } = this;
const output = rp2040.readUint8(this.readAddr);
if (ctrl & SNIFF_EN) {
dma.sniffTransfer(this, output);
}
rp2040.writeUint8(this.writeAddr, output);
};

transfer16 = () => {
const { rp2040 } = this;
rp2040.writeUint16(this.writeAddr, rp2040.readUint16(this.readAddr));
const { rp2040, ctrl, dma } = this;
const output = rp2040.readUint16(this.readAddr);
if (ctrl & SNIFF_EN) {
dma.sniffTransfer(this, output);
}
rp2040.writeUint16(this.writeAddr, output);
};

transferSwap16 = () => {
const { rp2040 } = this;
const { rp2040, ctrl, dma } = this;
const input = rp2040.readUint16(this.readAddr);
rp2040.writeUint16(this.writeAddr, ((input & 0xff) << 8) | (input >> 8));
const output = ((input & 0xff) << 8) | (input >> 8);
if (ctrl & SNIFF_EN) {
dma.sniffTransfer(this, output);
}
rp2040.writeUint16(this.writeAddr, output);
};

transfer32 = () => {
const { rp2040 } = this;
rp2040.writeUint32(this.writeAddr, rp2040.readUint32(this.readAddr));
const { rp2040, ctrl, dma } = this;
const output = rp2040.readUint32(this.readAddr);
if (ctrl & SNIFF_EN) {
dma.sniffTransfer(this, output);
}
rp2040.writeUint32(this.writeAddr, output);
};

transferSwap32 = () => {
const { rp2040 } = this;
const { rp2040, ctrl, dma } = this;
const input = rp2040.readUint32(this.readAddr);
rp2040.writeUint32(
this.writeAddr,
((input & 0x000000ff) << 24) |
((input & 0x0000ff00) << 8) |
((input & 0x00ff0000) >> 8) |
((input >> 24) & 0xff)
);
const output = ((input & 0x000000ff) << 24) |
((input & 0x0000ff00) << 8) |
((input & 0x00ff0000) >> 8) |
((input >> 24) & 0xff);
if (ctrl & SNIFF_EN) {
dma.sniffTransfer(this, output);
}
rp2040.writeUint32(this.writeAddr, output);
};

transfer = () => {
Expand Down Expand Up @@ -390,6 +416,8 @@ export class RPDMA extends BasePeripheral implements Peripheral {
private timer1 = 0;
private timer2 = 0;
private timer3 = 0;
private sniffCtrl = 0;
private sniffData = 0;

readonly dreq: boolean[] = Array(DREQChannel.DREQ_MAX);

Expand Down Expand Up @@ -429,6 +457,10 @@ export class RPDMA extends BasePeripheral implements Peripheral {
return this.intForce1;
case INTS1:
return this.intStatus1;
case SNIFF_CTRL:
return this.sniffCtrl;
case SNIFF_DATA:
return this.sniffData;
case N_CHANNELS:
return this.channels.length;
}
Expand Down Expand Up @@ -483,6 +515,12 @@ export class RPDMA extends BasePeripheral implements Peripheral {
}
}
return;
case SNIFF_CTRL:
this.sniffCtrl = value;
return;
case SNIFF_DATA:
this.sniffData = value;
return;
case CHAN_ABORT:
for (const chan of this.channels) {
if (value & (1 << chan.index)) {
Expand Down Expand Up @@ -549,4 +587,13 @@ export class RPDMA extends BasePeripheral implements Peripheral {
this.rp2040.setInterrupt(IRQ.DMA_IRQ0, !!this.intStatus0);
this.rp2040.setInterrupt(IRQ.DMA_IRQ1, !!this.intStatus1);
}

sniffTransfer(channel: RPDMAChannel, datum: number) {
const sniffChannel = (this.sniffCtrl >> DMACH_SHIFT) & DMACH_MASK;
if ((this.sniffCtrl & EN) && (channel.index === sniffChannel)) {
// TODO: Actually obey settings in sniffCtrl...
// currently assuming OUT_INV = OUT_REV = SNIFF_BSWAP = 0; CALC = 0xf
this.sniffData += datum;
urish marked this conversation as resolved.
Show resolved Hide resolved
}
}
}