2020-10-17 14:28:04 +01:00
|
|
|
use super::arm7tdmi::memory::{MemoryAccess, MemoryInterface};
|
2020-01-31 10:41:13 +00:00
|
|
|
use super::cartridge::BackupMedia;
|
2020-09-27 13:44:17 +01:00
|
|
|
use super::interrupt::{self, Interrupt, InterruptConnect, SharedInterruptFlags};
|
2019-12-21 18:19:43 +00:00
|
|
|
use super::iodev::consts::{REG_FIFO_A, REG_FIFO_B};
|
2020-10-18 16:32:44 +01:00
|
|
|
use super::sched::{EventType, Scheduler, SchedulerConnect, SharedScheduler};
|
2019-11-08 23:43:43 +00:00
|
|
|
use super::sysbus::SysBus;
|
|
|
|
|
|
|
|
use num::FromPrimitive;
|
2020-01-16 18:06:22 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-11-08 23:43:43 +00:00
|
|
|
|
2020-01-16 18:06:22 +00:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
2019-11-08 23:43:43 +00:00
|
|
|
pub struct DmaChannel {
|
|
|
|
id: usize,
|
|
|
|
|
|
|
|
pub src: u32,
|
|
|
|
pub dst: u32,
|
|
|
|
pub wc: u32,
|
|
|
|
pub ctrl: DmaChannelCtrl,
|
|
|
|
|
2019-11-16 16:16:13 +00:00
|
|
|
// These are "latched" when the dma is enabled.
|
|
|
|
internal: DmaInternalRegs,
|
|
|
|
|
2019-11-08 23:43:43 +00:00
|
|
|
running: bool,
|
2019-12-20 17:18:16 +00:00
|
|
|
fifo_mode: bool,
|
2019-11-08 23:43:43 +00:00
|
|
|
irq: Interrupt,
|
2020-05-25 23:46:05 +01:00
|
|
|
interrupt_flags: SharedInterruptFlags,
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 18:06:22 +00:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
|
2019-11-16 16:16:13 +00:00
|
|
|
struct DmaInternalRegs {
|
|
|
|
src_addr: u32,
|
|
|
|
dst_addr: u32,
|
|
|
|
count: u32,
|
|
|
|
}
|
|
|
|
|
2019-11-08 23:43:43 +00:00
|
|
|
impl DmaChannel {
|
2020-05-25 23:46:05 +01:00
|
|
|
pub fn new(id: usize, interrupt_flags: SharedInterruptFlags) -> DmaChannel {
|
2019-11-08 23:43:43 +00:00
|
|
|
if id > 3 {
|
|
|
|
panic!("invalid dma id {}", id);
|
|
|
|
}
|
|
|
|
DmaChannel {
|
|
|
|
id: id,
|
|
|
|
irq: Interrupt::from_usize(id + 8).unwrap(),
|
|
|
|
running: false,
|
|
|
|
src: 0,
|
|
|
|
dst: 0,
|
|
|
|
wc: 0,
|
|
|
|
ctrl: DmaChannelCtrl(0),
|
2020-10-10 18:57:00 +01:00
|
|
|
|
2019-12-20 17:18:16 +00:00
|
|
|
fifo_mode: false,
|
2019-11-16 16:16:13 +00:00
|
|
|
internal: Default::default(),
|
2020-05-25 23:46:05 +01:00
|
|
|
interrupt_flags,
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_running(&self) -> bool {
|
|
|
|
self.running
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_src_low(&mut self, low: u16) {
|
|
|
|
let src = self.src;
|
|
|
|
self.src = (src & 0xffff0000) | (low as u32);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_src_high(&mut self, high: u16) {
|
|
|
|
let src = self.src;
|
2020-05-31 23:26:38 +01:00
|
|
|
let high = (high & 0xfff) as u32;
|
2019-11-08 23:43:43 +00:00
|
|
|
self.src = (src & 0xffff) | (high << 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_dst_low(&mut self, low: u16) {
|
|
|
|
let dst = self.dst;
|
|
|
|
self.dst = (dst & 0xffff0000) | (low as u32);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_dst_high(&mut self, high: u16) {
|
|
|
|
let dst = self.dst;
|
2020-05-31 23:26:38 +01:00
|
|
|
let high = (high & 0xfff) as u32;
|
2019-11-08 23:43:43 +00:00
|
|
|
self.dst = (dst & 0xffff) | (high << 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_word_count(&mut self, value: u16) {
|
|
|
|
self.wc = value as u32;
|
|
|
|
}
|
|
|
|
|
2021-06-08 23:33:12 +01:00
|
|
|
pub fn write_dma_ctrl(&mut self, value: u16, #[cfg(feature = "debugger")] trace: bool) -> bool {
|
2019-11-08 23:43:43 +00:00
|
|
|
let ctrl = DmaChannelCtrl(value);
|
2019-12-20 17:18:16 +00:00
|
|
|
let timing = ctrl.timing();
|
2019-11-08 23:43:43 +00:00
|
|
|
let mut start_immediately = false;
|
2019-11-16 16:16:13 +00:00
|
|
|
if ctrl.is_enabled() && !self.ctrl.is_enabled() {
|
2021-06-08 23:33:12 +01:00
|
|
|
#[cfg(feature = "debugger")]
|
|
|
|
{
|
|
|
|
if trace {
|
|
|
|
trace!(
|
|
|
|
"DMA{} enabled! timing={} src={:#x} dst={:#x} cnt={}",
|
|
|
|
self.id,
|
|
|
|
timing,
|
|
|
|
self.src,
|
|
|
|
self.dst,
|
|
|
|
self.wc
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 23:43:43 +00:00
|
|
|
self.running = true;
|
2019-12-20 17:18:16 +00:00
|
|
|
start_immediately = timing == 0;
|
2019-11-16 16:16:13 +00:00
|
|
|
self.internal.src_addr = self.src;
|
|
|
|
self.internal.dst_addr = self.dst;
|
|
|
|
self.internal.count = self.wc;
|
2019-12-21 18:19:43 +00:00
|
|
|
self.fifo_mode = timing == 3
|
|
|
|
&& ctrl.repeat()
|
|
|
|
&& (self.id == 1 || self.id == 2)
|
|
|
|
&& (self.dst == REG_FIFO_A || self.dst == REG_FIFO_B);
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
2019-12-27 10:37:32 +00:00
|
|
|
if !ctrl.is_enabled() {
|
|
|
|
self.running = false;
|
|
|
|
}
|
2019-11-08 23:43:43 +00:00
|
|
|
self.ctrl = ctrl;
|
|
|
|
return start_immediately;
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:46:05 +01:00
|
|
|
fn xfer(&mut self, sb: &mut SysBus) {
|
2019-11-08 23:43:43 +00:00
|
|
|
let word_size = if self.ctrl.is_32bit() { 4 } else { 2 };
|
2019-11-16 16:16:13 +00:00
|
|
|
let count = match self.internal.count {
|
|
|
|
0 => match self.id {
|
|
|
|
3 => 0x1_0000,
|
|
|
|
_ => 0x0_4000,
|
|
|
|
},
|
|
|
|
_ => self.internal.count,
|
|
|
|
};
|
2020-01-31 16:24:42 +00:00
|
|
|
|
|
|
|
if self.id == 3 && word_size == 2 {
|
|
|
|
if let BackupMedia::Eeprom(eeprom) = &mut sb.cartridge.backup {
|
|
|
|
eeprom.on_dma3_transfer(
|
|
|
|
self.internal.src_addr,
|
|
|
|
self.internal.dst_addr,
|
|
|
|
count as usize,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 17:18:16 +00:00
|
|
|
let fifo_mode = self.fifo_mode;
|
|
|
|
|
2020-10-18 16:33:37 +01:00
|
|
|
let src_adj = match self.ctrl.src_adj() {
|
|
|
|
/* Increment */ 0 => word_size,
|
|
|
|
/* Decrement */ 1 => 0 - word_size,
|
|
|
|
/* Fixed */ 2 => 0,
|
|
|
|
_ => panic!("forbidden DMA source address adjustment"),
|
|
|
|
};
|
|
|
|
let dst_adj = match self.ctrl.dst_adj() {
|
|
|
|
/* Increment[+Reload] */ 0 | 3 => word_size,
|
|
|
|
/* Decrement */ 1 => 0 - word_size,
|
|
|
|
/* Fixed */ 2 => 0,
|
|
|
|
_ => panic!("forbidden DMA dest address adjustment"),
|
|
|
|
};
|
|
|
|
|
2020-10-17 14:28:04 +01:00
|
|
|
let mut access = MemoryAccess::NonSeq;
|
2019-12-20 17:18:16 +00:00
|
|
|
if fifo_mode {
|
2019-12-21 18:19:43 +00:00
|
|
|
for _ in 0..4 {
|
2020-10-17 14:28:04 +01:00
|
|
|
let v = sb.load_32(self.internal.src_addr & !3, access);
|
|
|
|
sb.store_32(self.internal.dst_addr & !3, v, access);
|
|
|
|
access = MemoryAccess::Seq;
|
2019-12-21 18:19:43 +00:00
|
|
|
self.internal.src_addr += 4;
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
2019-12-20 17:18:16 +00:00
|
|
|
} else if word_size == 4 {
|
|
|
|
for _ in 0..count {
|
2020-10-17 14:28:04 +01:00
|
|
|
let w = sb.load_32(self.internal.src_addr & !3, access);
|
|
|
|
sb.store_32(self.internal.dst_addr & !3, w, access);
|
|
|
|
access = MemoryAccess::Seq;
|
2020-10-18 16:33:37 +01:00
|
|
|
self.internal.src_addr += src_adj;
|
|
|
|
self.internal.dst_addr += dst_adj;
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
2019-12-20 17:18:16 +00:00
|
|
|
} else {
|
|
|
|
for _ in 0..count {
|
2020-10-17 14:28:04 +01:00
|
|
|
let hw = sb.load_16(self.internal.src_addr & !1, access);
|
|
|
|
sb.store_16(self.internal.dst_addr & !1, hw, access);
|
|
|
|
access = MemoryAccess::Seq;
|
2020-10-18 16:33:37 +01:00
|
|
|
self.internal.src_addr += src_adj;
|
|
|
|
self.internal.dst_addr += dst_adj;
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.ctrl.is_triggering_irq() {
|
2020-05-25 23:46:05 +01:00
|
|
|
interrupt::signal_irq(&self.interrupt_flags, self.irq);
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
|
|
|
if self.ctrl.repeat() {
|
|
|
|
/* reload */
|
|
|
|
if 3 == self.ctrl.dst_adj() {
|
2019-11-16 16:16:13 +00:00
|
|
|
self.internal.dst_addr = self.dst;
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.running = false;
|
|
|
|
self.ctrl.set_enabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 18:06:22 +00:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
2019-11-08 23:43:43 +00:00
|
|
|
pub struct DmaController {
|
|
|
|
pub channels: [DmaChannel; 4],
|
2020-01-16 18:06:22 +00:00
|
|
|
pending_set: u8,
|
2020-10-10 18:57:00 +01:00
|
|
|
#[serde(skip)]
|
|
|
|
#[serde(default = "Scheduler::new_shared")]
|
|
|
|
scheduler: SharedScheduler,
|
2021-06-08 23:33:12 +01:00
|
|
|
#[cfg(feature = "debugger")]
|
|
|
|
pub trace: bool,
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 13:44:17 +01:00
|
|
|
impl InterruptConnect for DmaController {
|
|
|
|
fn connect_irq(&mut self, interrupt_flags: SharedInterruptFlags) {
|
|
|
|
for channel in &mut self.channels {
|
|
|
|
channel.interrupt_flags = interrupt_flags.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 16:32:44 +01:00
|
|
|
impl SchedulerConnect for DmaController {
|
|
|
|
fn connect_scheduler(&mut self, scheduler: SharedScheduler) {
|
|
|
|
self.scheduler = scheduler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 23:43:43 +00:00
|
|
|
impl DmaController {
|
2020-10-10 18:57:00 +01:00
|
|
|
pub fn new(interrupt_flags: SharedInterruptFlags, scheduler: SharedScheduler) -> DmaController {
|
2019-11-08 23:43:43 +00:00
|
|
|
DmaController {
|
|
|
|
channels: [
|
2020-05-25 23:46:05 +01:00
|
|
|
DmaChannel::new(0, interrupt_flags.clone()),
|
|
|
|
DmaChannel::new(1, interrupt_flags.clone()),
|
|
|
|
DmaChannel::new(2, interrupt_flags.clone()),
|
|
|
|
DmaChannel::new(3, interrupt_flags.clone()),
|
2019-11-08 23:43:43 +00:00
|
|
|
],
|
2020-01-16 18:06:22 +00:00
|
|
|
pending_set: 0,
|
2020-10-10 18:57:00 +01:00
|
|
|
scheduler: scheduler,
|
2021-06-08 23:33:12 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "debugger")]
|
|
|
|
trace: false,
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:58:32 +00:00
|
|
|
pub fn is_active(&self) -> bool {
|
2020-01-16 18:06:22 +00:00
|
|
|
self.pending_set != 0
|
2019-12-27 10:37:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 23:46:05 +01:00
|
|
|
pub fn perform_work(&mut self, sb: &mut SysBus) {
|
2020-01-16 18:06:22 +00:00
|
|
|
for id in 0..4 {
|
|
|
|
if self.pending_set & (1 << id) != 0 {
|
2020-05-25 23:46:05 +01:00
|
|
|
self.channels[id].xfer(sb);
|
2020-01-16 18:06:22 +00:00
|
|
|
}
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
2020-01-16 18:06:22 +00:00
|
|
|
self.pending_set = 0;
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_16(&mut self, channel_id: usize, ofs: u32, value: u16) {
|
|
|
|
match ofs {
|
|
|
|
0 => self.channels[channel_id].write_src_low(value),
|
|
|
|
2 => self.channels[channel_id].write_src_high(value),
|
|
|
|
4 => self.channels[channel_id].write_dst_low(value),
|
|
|
|
6 => self.channels[channel_id].write_dst_high(value),
|
|
|
|
8 => self.channels[channel_id].write_word_count(value),
|
|
|
|
10 => {
|
2021-06-08 23:33:12 +01:00
|
|
|
#[cfg(feature = "debugger")]
|
|
|
|
let start_immediately = self.channels[channel_id].write_dma_ctrl(value, self.trace);
|
|
|
|
#[cfg(not(feature = "debugger"))]
|
|
|
|
let start_immediately = self.channels[channel_id].write_dma_ctrl(value);
|
|
|
|
if start_immediately {
|
2020-10-10 18:57:00 +01:00
|
|
|
// DMA actually starts after 3 cycles
|
|
|
|
self.scheduler
|
2020-10-17 14:28:04 +01:00
|
|
|
.push(EventType::DmaActivateChannel(channel_id), 3);
|
2019-12-27 10:37:32 +00:00
|
|
|
} else {
|
2020-10-10 18:57:00 +01:00
|
|
|
self.deactivate_channel(channel_id);
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-31 19:18:28 +00:00
|
|
|
_ => panic!("Invalid dma offset {:x}", ofs),
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:22:30 +01:00
|
|
|
pub fn notify_from_gpu(&mut self, timing: u16) {
|
2019-11-08 23:43:43 +00:00
|
|
|
for i in 0..4 {
|
2020-05-20 19:22:30 +01:00
|
|
|
if self.channels[i].ctrl.is_enabled() && self.channels[i].ctrl.timing() == timing {
|
2020-01-16 18:06:22 +00:00
|
|
|
self.pending_set |= 1 << i;
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 17:18:16 +00:00
|
|
|
|
|
|
|
pub fn notify_sound_fifo(&mut self, fifo_addr: u32) {
|
|
|
|
for i in 1..=2 {
|
|
|
|
if self.channels[i].ctrl.is_enabled()
|
2019-12-27 10:37:32 +00:00
|
|
|
&& self.channels[i].running
|
2019-12-20 17:18:16 +00:00
|
|
|
&& self.channels[i].ctrl.timing() == 3
|
|
|
|
&& self.channels[i].dst == fifo_addr
|
|
|
|
{
|
2020-01-16 18:06:22 +00:00
|
|
|
self.pending_set |= 1 << i;
|
2019-12-20 17:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-10 18:57:00 +01:00
|
|
|
|
|
|
|
pub fn activate_channel(&mut self, channel_id: usize) {
|
|
|
|
self.pending_set |= 1 << channel_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deactivate_channel(&mut self, channel_id: usize) {
|
|
|
|
self.pending_set &= !(1 << channel_id);
|
|
|
|
}
|
2019-11-08 23:43:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 19:22:30 +01:00
|
|
|
pub const TIMING_VBLANK: u16 = 1;
|
|
|
|
pub const TIMING_HBLANK: u16 = 2;
|
|
|
|
|
|
|
|
pub trait DmaNotifer {
|
|
|
|
fn notify(&mut self, timing: u16);
|
|
|
|
}
|
|
|
|
|
2019-11-08 23:43:43 +00:00
|
|
|
bitfield! {
|
2020-01-16 18:06:22 +00:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Default)]
|
2019-11-08 23:43:43 +00:00
|
|
|
pub struct DmaChannelCtrl(u16);
|
|
|
|
impl Debug;
|
|
|
|
u16;
|
|
|
|
dst_adj, _ : 6, 5;
|
|
|
|
src_adj, _ : 8, 7;
|
|
|
|
repeat, _ : 9;
|
|
|
|
is_32bit, _: 10;
|
|
|
|
timing, _: 13, 12;
|
|
|
|
is_triggering_irq, _: 14;
|
|
|
|
is_enabled, set_enabled: 15;
|
|
|
|
}
|