soundstream: cleanups
With an updated toolchain we can eliminate the heap allocations:
- allocate the sample buffer in .bss
- allocate the sample filter buffers on the stack (sized at 4KiB)
- mark the soundstream compartment with CHERIOT_NO_AMBIENT_MALLOC
now that we no longer use the heap
- replace the one use of CHECK with Debug::Assert
Bug: 330741645
Bypass-Presubmit-Reason: no sencha presubmit tests
Change-Id: I9b6df0a6cca5d12547610623ac0638636fd37ae1
diff --git a/sw/device/cheriot/soundstream/soundstream.cc b/sw/device/cheriot/soundstream/soundstream.cc
index 45dd2a9..6d6c06f 100644
--- a/sw/device/cheriot/soundstream/soundstream.cc
+++ b/sw/device/cheriot/soundstream/soundstream.cc
@@ -14,19 +14,6 @@
* limitations under the License.
*/
-// Data structures that hold audio samples are allocated on the heap
-// because they are too big (for now) for .data/.bss. We use calloc
-// for this and need to override the default quota (4K) to satisfy our
-// large'ish requests.
-//#define kSamples (5 * 16000)
-#define kSamples (5000) // NB: reduced sample count for slow renode
-#define kFilterSamples (256)
-#define roundup(a, b) (((a) + (b) - 1) / (b)) * (b)
-#define MALLOC_QUOTA \
- roundup( \
- (kSamples * sizeof(int32_t)) + 2 * (kFilterSamples * sizeof(int16_t)), \
- 4096)
-
#include "soundstream.h"
#include <fail-simulator-on-error.h>
@@ -39,10 +26,12 @@
/// Expose debugging features unconditionally for this compartment.
using Debug = ConditionalDebug<true, "SOUNDSTREAM">;
-#include "compat.h"
-
#define ABS(x) (x > 0 ? x : -x)
+//#define kSamples (5 * 16000)
+#define kSamples (2000) // NB: reduced sample count for slow renode
+#define kFilterSamples (256)
+
void __cheri_compartment("soundstream") entry(void) {
Debug::log("soundstream (Thread {})", thread_id_get());
@@ -54,29 +43,15 @@
i2s_irq_acknowledge_all();
i2s_irq_set_enabled(kI2sIrqRxWatermark, /*enabled=*/true);
- // NB: sample buffers are temporarily allocated from the heap because
- // the toolchain does not support large'ish data structures. Support
- // has been committed upstream but not yet brought in.
-
- // XXX calloc returns 0xffffffff (v:0 0xfffffe00-0xfffffe00 l:0x0 o:0x0 p: -
- // ------ -- ---) on failure so cannot check against NULL
- // int16_t samples_left[kFilterSamples] = {0};
- // int16_t samples_right[kFilterSamples] = {0};
- int16_t* samples_left = (int16_t*)calloc(sizeof(int16_t), kFilterSamples);
- Debug::Assert(__builtin_cheri_length_get(samples_left) > 0,
- "samples_left allocation failed {}", samples_left);
- int16_t* samples_right = (int16_t*)calloc(sizeof(int16_t), kFilterSamples);
- Debug::Assert(__builtin_cheri_length_get(samples_right) > 0,
- "samples_right allocation failed {}", samples_right);
+ // NB: the stack is 4KiB so this uses 1/4 of it
+ int16_t samples_left[kFilterSamples] = {0};
+ int16_t samples_right[kFilterSamples] = {0};
size_t index_left = 0;
size_t index_right = 0;
int32_t total_left = 0;
int32_t total_right = 0;
- // static int32_t samples[kSamples];
- int32_t* samples = (int32_t*)calloc(sizeof(int32_t), kSamples);
- Debug::Assert(__builtin_cheri_length_get(samples) > 0,
- "Sample allocation failed {}", samples);
+ static int32_t samples[kSamples];
memset(samples, 0xa5, sizeof(int32_t) * kSamples);
Debug::log("Setup complete");
@@ -190,7 +165,7 @@
ml_top_wait_for_finish();
ml_top_get_output_header(&header);
- CHECK(header.length == sizeof(result_buffer), "Unexpected ML result size");
+ Debug::Assert(header.length == sizeof(result_buffer), "Unexpected ML result size");
ml_top_get_output_data(&header, result_buffer);
encode((const unsigned char*)result_buffer, sizeof(result_buffer),
diff --git a/sw/device/cheriot/soundstream/xmake.lua b/sw/device/cheriot/soundstream/xmake.lua
index 5ab5325..0872683 100644
--- a/sw/device/cheriot/soundstream/xmake.lua
+++ b/sw/device/cheriot/soundstream/xmake.lua
@@ -68,6 +68,7 @@
add_files("soundstream.cc", "encode.cc")
add_files(path.join(matcha_dir, "hw/top_matcha/sw/autogen/top_matcha.c"));
add_includedirs(matcha_dir, matcha_gen_dir, opentitan_dir)
+ add_defines("CHERIOT_NO_AMBIENT_MALLOC")
-- Firmware image.
firmware("soundstream-firmware")