Skip to content

Commit

Permalink
cellMic: add nullptr check for data in cell_mic_read
Browse files Browse the repository at this point in the history
Also rename S to Size for readability
  • Loading branch information
Megamouse committed Mar 26, 2024
1 parent e05239f commit 1f1f71b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
9 changes: 5 additions & 4 deletions rpcs3/Emu/Cell/Modules/cellMic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1144,8 +1144,6 @@ error_code cellMicRemoveNotifyEventQueue(u64 key)

error_code cell_mic_read(s32 dev_num, vm::ptr<void> data, s32 max_bytes, /*CellMicSignalType*/u32 type)
{
// TODO: CELL_MICIN_ERROR_PARAM

auto& mic_thr = g_fxo->get<mic_thread>();
const std::lock_guard lock(mic_thr.mutex);
if (!mic_thr.init)
Expand All @@ -1159,16 +1157,19 @@ error_code cell_mic_read(s32 dev_num, vm::ptr<void> data, s32 max_bytes, /*CellM
if (!device.is_opened() || !(device.get_signal_types() & type))
return CELL_MICIN_ERROR_NOT_OPEN;

if (!data)
return not_an_error(0);

switch (type)
{
case CELLMIC_SIGTYPE_DSP: return not_an_error(device.read_dsp(vm::_ptr<u8>(data.addr()), max_bytes));
case CELLMIC_SIGTYPE_AUX: return CELL_OK; // TODO
case CELLMIC_SIGTYPE_AUX: return not_an_error(0); // TODO
case CELLMIC_SIGTYPE_RAW: return not_an_error(device.read_raw(vm::_ptr<u8>(data.addr()), max_bytes));
default:
fmt::throw_exception("Invalid CELLMIC_SIGTYPE %d", type);
}

return CELL_OK;
return not_an_error(0);
}

error_code cellMicReadRaw(s32 dev_num, vm::ptr<void> data, s32 max_bytes)
Expand Down
45 changes: 23 additions & 22 deletions rpcs3/Emu/Cell/Modules/cellMic.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@ struct CellMicInputStream

struct CellMicInputDefinition
{
// TODO: Data types
volatile u32 uiDevId;
CellMicInputStream data;
be_t<u32> uiDevId;
CellMicInputStream data;
CellMicInputFormatI aux_format;
CellMicInputFormatI raw_format;
CellMicInputFormatI sig_format;
Expand All @@ -182,13 +181,13 @@ struct CellMicStatus
// --- End of cell definitions ---


template <usz S>
template <usz Size>
class simple_ringbuf
{
public:
simple_ringbuf()
{
m_container.resize(S);
m_container.resize(Size);
}

bool has_data() const
Expand All @@ -200,19 +199,19 @@ class simple_ringbuf
{
ensure(buf);

u32 to_read = size > m_used ? m_used : size;
const u32 to_read = size > m_used ? m_used : size;
if (!to_read)
return 0;

u8* data = m_container.data();
u32 new_tail = m_tail + to_read;
u8* data = m_container.data();
const u32 new_tail = m_tail + to_read;

if (new_tail >= S)
if (new_tail >= Size)
{
u32 first_chunk_size = S - m_tail;
const u32 first_chunk_size = Size - m_tail;
std::memcpy(buf, data + m_tail, first_chunk_size);
std::memcpy(buf + first_chunk_size, data, to_read - first_chunk_size);
m_tail = (new_tail - S);
m_tail = (new_tail - Size);
}
else
{
Expand All @@ -227,30 +226,32 @@ class simple_ringbuf

void write_bytes(const u8* buf, const u32 size)
{
ensure(size <= S);
ensure(size <= Size);

if (u32 over_size = m_used + size; over_size > S)
const u32 over_size = m_used + size;

if (over_size > Size)
{
m_tail += (over_size - S);
if (m_tail > S)
m_tail -= S;
m_tail += (over_size - Size);
if (m_tail > Size)
m_tail -= Size;

m_used = S;
m_used = Size;
}
else
{
m_used = over_size;
}

u8* data = m_container.data();
u32 new_head = m_head + size;
u8* data = m_container.data();
const u32 new_head = m_head + size;

if (new_head >= S)
if (new_head >= Size)
{
u32 first_chunk_size = S - m_head;
const u32 first_chunk_size = Size - m_head;
std::memcpy(data + m_head, buf, first_chunk_size);
std::memcpy(data, buf + first_chunk_size, size - first_chunk_size);
m_head = (new_head - S);
m_head = (new_head - Size);
}
else
{
Expand Down

0 comments on commit 1f1f71b

Please sign in to comment.