|
| 1 | +#include "SB16.h" |
| 2 | +#include "IO.h" |
| 3 | +#include <Kernel/VM/MemoryManager.h> |
| 4 | + |
| 5 | +const u16 DSP_READ = 0x22A; |
| 6 | +const u16 DSP_WRITE = 0x22C; |
| 7 | +const u16 DSP_STATUS = 0x22E; |
| 8 | +const u16 DSP_R_ACK = 0x22F; |
| 9 | + |
| 10 | +/* Write a value to the DSP write register */ |
| 11 | +void SB16::dsp_write(u8 value) |
| 12 | +{ |
| 13 | + while (IO::in8(DSP_WRITE) & 0x80) { |
| 14 | + ; |
| 15 | + } |
| 16 | + IO::out8(DSP_WRITE, value); |
| 17 | +} |
| 18 | + |
| 19 | +/* Reads the value of the DSP read register */ |
| 20 | +u8 SB16::dsp_read() |
| 21 | +{ |
| 22 | + while (!(IO::in8(DSP_STATUS) & 0x80)) { |
| 23 | + ; |
| 24 | + } |
| 25 | + return IO::in8(DSP_READ); |
| 26 | +} |
| 27 | + |
| 28 | +/* Changes the sample rate of sound output */ |
| 29 | +void SB16::set_sample_rate(uint16_t hz) |
| 30 | +{ |
| 31 | + dsp_write(0x41); // output |
| 32 | + dsp_write((u8)(hz >> 8)); |
| 33 | + dsp_write((u8)hz); |
| 34 | + dsp_write(0x42); // input |
| 35 | + dsp_write((u8)(hz >> 8)); |
| 36 | + dsp_write((u8)hz); |
| 37 | +} |
| 38 | + |
| 39 | +static SB16* s_the; |
| 40 | + |
| 41 | +SB16::SB16() |
| 42 | + : IRQHandler(5) |
| 43 | + , CharacterDevice(42, 42) // ### ? |
| 44 | +{ |
| 45 | + s_the = this; |
| 46 | + initialize(); |
| 47 | +} |
| 48 | + |
| 49 | +SB16::~SB16() |
| 50 | +{ |
| 51 | +} |
| 52 | + |
| 53 | +SB16& SB16::the() |
| 54 | +{ |
| 55 | + return *s_the; |
| 56 | +} |
| 57 | + |
| 58 | +void SB16::handle_irq() |
| 59 | +{ |
| 60 | + m_interrupted = true; |
| 61 | + |
| 62 | + // Stop sound output ready for the next block. |
| 63 | + dsp_write(0xd0); |
| 64 | + |
| 65 | + IO::in8(DSP_STATUS); // 8 bit interrupt |
| 66 | + if (m_major_version >= 4) |
| 67 | + IO::in8(DSP_R_ACK); // 16 bit interrupt |
| 68 | +} |
| 69 | + |
| 70 | +void SB16::initialize() |
| 71 | +{ |
| 72 | + IO::out8(0x226, 1); |
| 73 | + IO::delay(); |
| 74 | + IO::out8(0x226, 0); |
| 75 | + |
| 76 | + auto data = dsp_read(); |
| 77 | + if (data != 0xaa) { |
| 78 | + kprintf("SB16: sb not ready"); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + // Get the version info |
| 83 | + dsp_write(0xe1); |
| 84 | + m_major_version = dsp_read(); |
| 85 | + auto vmin = dsp_read(); |
| 86 | + |
| 87 | + kprintf("SB16: found version %d.%d\n", m_major_version, vmin); |
| 88 | + enable_irq(); |
| 89 | +} |
| 90 | + |
| 91 | +bool SB16::can_read(FileDescription&) const |
| 92 | +{ |
| 93 | + return false; |
| 94 | +} |
| 95 | + |
| 96 | +ssize_t SB16::read(FileDescription&, u8*, ssize_t) |
| 97 | +{ |
| 98 | + return 0; |
| 99 | +} |
| 100 | + |
| 101 | +void SB16::dma_start(uint32_t length) |
| 102 | +{ |
| 103 | + const auto addr = m_dma_buffer_page->paddr().get(); |
| 104 | + const u8 channel = 1; |
| 105 | + const u8 mode = 0; |
| 106 | + |
| 107 | + // Disable the DMA channel |
| 108 | + IO::out8(0x0a, 4 + (channel % 4)); |
| 109 | + |
| 110 | + // Clear the byte pointer flip-flop |
| 111 | + IO::out8(0x0c, 0); |
| 112 | + |
| 113 | + // Write the DMA mode for the transfer |
| 114 | + IO::out8(0x0b, channel | mode); |
| 115 | + |
| 116 | + // Write the offset of the buffer |
| 117 | + IO::out8(0x02, (u8)addr); |
| 118 | + IO::out8(0x02, (u8)(addr >> 8)); |
| 119 | + |
| 120 | + // Write the transfer length |
| 121 | + IO::out8(0x03, (u8)(length - 1)); |
| 122 | + IO::out8(0x03, (u8)((length - 1) >> 8)); |
| 123 | + |
| 124 | + // Write the buffer |
| 125 | + IO::out8(0x83, addr >> 16); |
| 126 | + |
| 127 | + // Enable the DMA channel |
| 128 | + IO::out8(0x0a, channel); |
| 129 | +} |
| 130 | + |
| 131 | +void SB16::wait_for_irq() |
| 132 | +{ |
| 133 | + m_interrupted = false; |
| 134 | +#ifdef SB16_DEBUG |
| 135 | + kprintf("SB16: waiting for interrupt...\n"); |
| 136 | +#endif |
| 137 | + // FIXME: Add timeout. |
| 138 | + while (!m_interrupted) { |
| 139 | + // FIXME: Put this process into a Blocked state instead, it's stupid to wake up just to check a flag. |
| 140 | + Scheduler::yield(); |
| 141 | + } |
| 142 | +#ifdef SB16_DEBUG |
| 143 | + kprintf("SB16: got interrupt!\n"); |
| 144 | +#endif |
| 145 | + memory_barrier(); |
| 146 | +} |
| 147 | + |
| 148 | +ssize_t SB16::write(FileDescription&, const u8* data, ssize_t length) |
| 149 | +{ |
| 150 | + if (!m_dma_buffer_page) { |
| 151 | + kprintf("SB16: Allocating page\n"); |
| 152 | + m_dma_buffer_page = MM.allocate_supervisor_physical_page(); |
| 153 | + } |
| 154 | + |
| 155 | + kprintf("SB16: Writing buffer of %d bytes\n", length); |
| 156 | + const int BLOCK_SIZE = 32 * 1024; |
| 157 | + if (length > BLOCK_SIZE) { |
| 158 | + return -ENOSPC; |
| 159 | + } |
| 160 | + const u8 mode = 0; |
| 161 | + |
| 162 | + disable_irq(); |
| 163 | + const int sample_rate = 44100; |
| 164 | + set_sample_rate(sample_rate); |
| 165 | + dma_start(length); |
| 166 | + memcpy(m_dma_buffer_page->paddr().as_ptr(), data, length); |
| 167 | + |
| 168 | + u8 command = 0x06; |
| 169 | + command |= 0xc0; |
| 170 | + |
| 171 | + dsp_write(command); |
| 172 | + dsp_write(mode); |
| 173 | + dsp_write((u8)length); |
| 174 | + dsp_write((u8)(length >> 8)); |
| 175 | + |
| 176 | + enable_irq(); |
| 177 | + wait_for_irq(); |
| 178 | + return length; |
| 179 | +} |
0 commit comments