From 787ffee1323823e9e0856a5494f3edc233136590 Mon Sep 17 00:00:00 2001 From: Ed Date: Fri, 26 Apr 2024 13:26:01 +0100 Subject: [PATCH 01/13] Failing MIDI tx test --- tests/test_midi_tx.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_midi_tx.py b/tests/test_midi_tx.py index 1932a587..c9247d38 100644 --- a/tests/test_midi_tx.py +++ b/tests/test_midi_tx.py @@ -23,14 +23,14 @@ def test_tx(capfd, config, build_midi): copy_tree(build_midi, tmpdirname) xe = str(Path(tmpdirname) / f"{config}/test_midi_{config}.xe") - # midi_commands = [[0x90, 0x91, 0x90],# Invalid and should be discarded - # [0x90, 60, 81], # Note on - # [0x80, 60, 81]] # Note off - - midi_commands = [ + midi_commands = [[0x90, 0x91, 0x90],# Invalid and should be discarded [0x90, 60, 81], # Note on [0x80, 60, 81]] # Note off + # midi_commands = [ + # [0x90, 60, 81], # Note on + # [0x80, 60, 81]] # Note off + # midi_command_expected = midi_commands[1:] # should skip invalid first message # Make a 1D list from the 2D list From 867da0d9c9cf0764dd3b128cd678388562215752 Mon Sep 17 00:00:00 2001 From: Ed Date: Fri, 26 Apr 2024 15:07:35 +0100 Subject: [PATCH 02/13] Remove unused (and untested) byte API from queue --- lib_xua/src/midi/queue.h | 8 -------- lib_xua/src/midi/queue.xc | 2 -- 2 files changed, 10 deletions(-) diff --git a/lib_xua/src/midi/queue.h b/lib_xua/src/midi/queue.h index 3e51b328..e62853e9 100644 --- a/lib_xua/src/midi/queue.h +++ b/lib_xua/src/midi/queue.h @@ -47,16 +47,8 @@ inline unsigned queue_pop_word(queue_t &q, unsigned array[]) { return array[q.rdptr++ & q.mask]; } -inline void queue_push_byte(queue_t &q, unsigned char array[], unsigned data) -{ - assert(!queue_is_full(q)); - array[q.wrptr++ & q.mask] = data; } -inline unsigned queue_pop_byte(queue_t &q, unsigned char array[]) { - assert(!queue_is_empty(q)); - return array[q.rdptr++ & q.mask]; -} inline unsigned queue_items(const queue_t &q) { return q.wrptr - q.rdptr; diff --git a/lib_xua/src/midi/queue.xc b/lib_xua/src/midi/queue.xc index 6cab55f7..b88ce469 100644 --- a/lib_xua/src/midi/queue.xc +++ b/lib_xua/src/midi/queue.xc @@ -9,7 +9,5 @@ extern inline int queue_is_empty(const queue_t &q); extern inline int queue_is_full(const queue_t &q); extern inline void queue_push_word(queue_t &q, unsigned array[], unsigned data); extern inline unsigned queue_pop_word(queue_t &q, unsigned array[]); -extern inline void queue_push_byte(queue_t &q, unsigned char array[], unsigned data); -extern inline unsigned queue_pop_byte(queue_t &q, unsigned char array[]); extern inline unsigned queue_space(const queue_t &q); extern inline unsigned queue_items(const queue_t &q); From 19a18c2db56ca3ade73acc8bcced9a8fde44df14 Mon Sep 17 00:00:00 2001 From: Ed Date: Fri, 26 Apr 2024 15:09:21 +0100 Subject: [PATCH 03/13] Remove dangerous runtime asserts in case of mal-formed MIDI message --- lib_xua/src/midi/queue.h | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/lib_xua/src/midi/queue.h b/lib_xua/src/midi/queue.h index e62853e9..01287ddb 100644 --- a/lib_xua/src/midi/queue.h +++ b/lib_xua/src/midi/queue.h @@ -5,6 +5,10 @@ #define assert(x) asm("ecallf %0"::"r"(x)); +#ifndef MIDI_ENABLE_ASSERTS +#define MIDI_ENABLE_ASSERTS 0 +#endif + typedef struct queue_t { /// Read index. unsigned rdptr; @@ -21,7 +25,7 @@ inline int is_power_of_2(unsigned x) { } inline void queue_init(queue_t &q, unsigned size) { - assert(is_power_of_2(size)); + assert(is_power_of_2(size)); // Keep this enabled as will be discovered duirng dev time q.rdptr = 0; q.wrptr = 0; q.size = size; @@ -38,15 +42,28 @@ inline int queue_is_full(const queue_t &q) { inline void queue_push_word(queue_t &q, unsigned array[], unsigned data) { - assert(!queue_is_full(q)); - array[q.wrptr++ & q.mask] = data; + + if(queue_is_full(q)) { + if(MIDI_ENABLE_ASSERTS){ + assert(0); + } else { + // Drop message + } + } else { + array[q.wrptr++ & q.mask] = data; + } } inline unsigned queue_pop_word(queue_t &q, unsigned array[]) { - assert(!queue_is_empty(q)); - return array[q.rdptr++ & q.mask]; -} - + if(queue_is_empty(q)){ + if(MIDI_ENABLE_ASSERTS){ + assert(0); + } else { + return 0x00000000; // midi_out_parse will return a size of 0 for this message/event + } + } else { + return array[q.rdptr++ & q.mask]; + } } From 8627ef97443edce7678c966933c376e95203efed Mon Sep 17 00:00:00 2001 From: Ed Date: Fri, 26 Apr 2024 16:12:07 +0100 Subject: [PATCH 04/13] Minor tidy --- lib_xua/src/midi/midioutparse.h | 5 ++++- lib_xua/src/midi/queue.h | 13 ++++++++----- lib_xua/src/midi/queue.xc | 2 +- tests/test_midi_tx.py | 5 ----- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib_xua/src/midi/midioutparse.h b/lib_xua/src/midi/midioutparse.h index d1dc5fa6..356d4190 100644 --- a/lib_xua/src/midi/midioutparse.h +++ b/lib_xua/src/midi/midioutparse.h @@ -1,8 +1,11 @@ -// Copyright 2011-2021 XMOS LIMITED. +// Copyright 2011-2024 XMOS LIMITED. // This Software is subject to the terms of the XMOS Public Licence: Version 1. #ifndef MIDIOUTPARSE_XH #define MIDIOUTPARSE_XH +#warning MAYBE A SYSEX START AND FINISH IS SAFEST FOR NULL? +#define MIDI_OUT_NULL_MESSAGE 0x00000000 // midi_out_parse will return a size of 0 for this invalid message/event + {unsigned, unsigned, unsigned, unsigned} midi_out_parse(unsigned event); #endif diff --git a/lib_xua/src/midi/queue.h b/lib_xua/src/midi/queue.h index 01287ddb..6576a070 100644 --- a/lib_xua/src/midi/queue.h +++ b/lib_xua/src/midi/queue.h @@ -3,6 +3,8 @@ #ifndef QUEUE_H_ #define QUEUE_H_ +#include "midioutparse.h" + #define assert(x) asm("ecallf %0"::"r"(x)); #ifndef MIDI_ENABLE_ASSERTS @@ -48,10 +50,11 @@ inline void queue_push_word(queue_t &q, unsigned array[], unsigned data) assert(0); } else { // Drop message + return; } - } else { - array[q.wrptr++ & q.mask] = data; } + + array[q.wrptr++ & q.mask] = data; } inline unsigned queue_pop_word(queue_t &q, unsigned array[]) { @@ -59,11 +62,11 @@ inline unsigned queue_pop_word(queue_t &q, unsigned array[]) { if(MIDI_ENABLE_ASSERTS){ assert(0); } else { - return 0x00000000; // midi_out_parse will return a size of 0 for this message/event + return MIDI_OUT_NULL_MESSAGE; } - } else { - return array[q.rdptr++ & q.mask]; } + + return array[q.rdptr++ & q.mask]; } diff --git a/lib_xua/src/midi/queue.xc b/lib_xua/src/midi/queue.xc index b88ce469..d79e7d68 100644 --- a/lib_xua/src/midi/queue.xc +++ b/lib_xua/src/midi/queue.xc @@ -1,4 +1,4 @@ -// Copyright 2013-2021 XMOS LIMITED. +// Copyright 2013-2024 XMOS LIMITED. // This Software is subject to the terms of the XMOS Public Licence: Version 1. #include "queue.h" diff --git a/tests/test_midi_tx.py b/tests/test_midi_tx.py index c9247d38..5b6cfd03 100644 --- a/tests/test_midi_tx.py +++ b/tests/test_midi_tx.py @@ -27,11 +27,6 @@ def test_tx(capfd, config, build_midi): [0x90, 60, 81], # Note on [0x80, 60, 81]] # Note off - # midi_commands = [ - # [0x90, 60, 81], # Note on - # [0x80, 60, 81]] # Note off - - # midi_command_expected = midi_commands[1:] # should skip invalid first message # Make a 1D list from the 2D list midi_command_expected = [[item for row in midi_commands for item in row]] From d57559764f6474d7b225fee6bcd17de2bf549600 Mon Sep 17 00:00:00 2001 From: Ed Date: Mon, 29 Apr 2024 10:17:43 +0100 Subject: [PATCH 05/13] Remove byte API from UT helper --- tests/xua_unit_tests/src/xua_unit_test_helper.xc | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/xua_unit_tests/src/xua_unit_test_helper.xc b/tests/xua_unit_tests/src/xua_unit_test_helper.xc index cb7c321b..bc9bb4b8 100644 --- a/tests/xua_unit_tests/src/xua_unit_test_helper.xc +++ b/tests/xua_unit_tests/src/xua_unit_test_helper.xc @@ -89,18 +89,6 @@ unsigned queue_pop_word_c_wrapper(queue_t *q, unsigned array[]){ } } -void queue_push_byte_c_wrapper(queue_t *q, unsigned char array[], unsigned data){ - unsafe{ - queue_push_byte(*q, array, data); - } -} - -unsigned queue_pop_byte_c_wrapper(queue_t *q, unsigned char array[]){ - unsafe{ - return queue_pop_byte(*q, array); - } -} - unsigned queue_items_c_wrapper(const queue_t *q){ unsafe{ return queue_items(*q); From d429068bcfe70dfc0eed94c4ce720024db89dc69 Mon Sep 17 00:00:00 2001 From: Ed Date: Mon, 29 Apr 2024 10:31:13 +0100 Subject: [PATCH 06/13] Guard midiparseout for XC --- lib_xua/src/midi/midioutparse.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib_xua/src/midi/midioutparse.h b/lib_xua/src/midi/midioutparse.h index 356d4190..9219f60b 100644 --- a/lib_xua/src/midi/midioutparse.h +++ b/lib_xua/src/midi/midioutparse.h @@ -6,6 +6,8 @@ #warning MAYBE A SYSEX START AND FINISH IS SAFEST FOR NULL? #define MIDI_OUT_NULL_MESSAGE 0x00000000 // midi_out_parse will return a size of 0 for this invalid message/event +#ifdef __XC__ {unsigned, unsigned, unsigned, unsigned} midi_out_parse(unsigned event); +#endif #endif From 28a530685f58c51ead2983b41daed6bce1eb4aa3 Mon Sep 17 00:00:00 2001 From: Ed Date: Mon, 29 Apr 2024 12:15:17 +0100 Subject: [PATCH 07/13] Fix TX test --- lib_xua/src/midi/midioutparse.h | 5 +++-- tests/test_midi/src/app_midi_simple.xc | 5 ++++- tests/test_midi_tx.py | 10 +++++----- tests/uart_tx_checker.py | 4 +++- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib_xua/src/midi/midioutparse.h b/lib_xua/src/midi/midioutparse.h index 9219f60b..1723b8f1 100644 --- a/lib_xua/src/midi/midioutparse.h +++ b/lib_xua/src/midi/midioutparse.h @@ -3,10 +3,11 @@ #ifndef MIDIOUTPARSE_XH #define MIDIOUTPARSE_XH -#warning MAYBE A SYSEX START AND FINISH IS SAFEST FOR NULL? -#define MIDI_OUT_NULL_MESSAGE 0x00000000 // midi_out_parse will return a size of 0 for this invalid message/event +// If for any reason we pop a message when not needed (should never happen) this will cause midiparse out to send a size of 0 (drops packet) +#define MIDI_OUT_NULL_MESSAGE 0x00000000 #ifdef __XC__ +// Takes a MIDI packet and decomoses it into up to 3 data bytes followed by a byte count. {unsigned, unsigned, unsigned, unsigned} midi_out_parse(unsigned event); #endif diff --git a/tests/test_midi/src/app_midi_simple.xc b/tests/test_midi/src/app_midi_simple.xc index 064dcb89..94e71079 100644 --- a/tests/test_midi/src/app_midi_simple.xc +++ b/tests/test_midi/src/app_midi_simple.xc @@ -134,7 +134,9 @@ void test(chanend c_midi){ } else { unsigned midi_data[3] = {0}; unsigned byte_count = 0; + // Even though this is an Rx from MIDI we use midi_out_parse so we can decode the packet {midi_data[0], midi_data[1], midi_data[2], byte_count} = midi_out_parse(byterev(rx_packet)); + dprintf("Got packet from MIDI: 0x%8x\n", rx_packet); // Note this needs to always print for capfd in pytest to pick it up printf("dut_midi_rx: %u %u %u\n", midi_data[0], midi_data[1], midi_data[2]); rx_cmd_count++; @@ -144,9 +146,10 @@ void test(chanend c_midi){ case tx_cmd_count < num_to_tx => tmr when timerafter(t_tx) :> int _: unsigned midi[] = {commands[tx_cmd_count][0], commands[tx_cmd_count][1], commands[tx_cmd_count][2]}; + // Even though this is a Tx to MIDI we use midi_in_parse_helper to form the packet from bytes unsigned tx_packet = midi_in_parse_helper(midi, m_state); outuint(c_midi, byterev(tx_packet)); - dprintf("Sent packet to midi: %u %u %u\n", commands[tx_cmd_count][0], commands[tx_cmd_count][1], commands[tx_cmd_count][2]); + dprintf("Sent packet to midi: %u %u %u (0x%8x)\n", commands[tx_cmd_count][0], commands[tx_cmd_count][1], commands[tx_cmd_count][2], tx_packet); t_tx += tx_interval; tx_end += max_tx_time; break; diff --git a/tests/test_midi_tx.py b/tests/test_midi_tx.py index 5b6cfd03..9d7ed3ae 100644 --- a/tests/test_midi_tx.py +++ b/tests/test_midi_tx.py @@ -27,9 +27,8 @@ def test_tx(capfd, config, build_midi): [0x90, 60, 81], # Note on [0x80, 60, 81]] # Note off - # midi_command_expected = midi_commands[1:] # should skip invalid first message - # Make a 1D list from the 2D list - midi_command_expected = [[item for row in midi_commands for item in row]] + # Make a 1D list from the 2D list [1:] because first is invalid and we expect to skip it + midi_command_expected = [[item for row in midi_commands[1:] for item in row]] create_midi_tx_file(midi_commands) create_midi_rx_file() @@ -42,14 +41,14 @@ def test_tx(capfd, config, build_midi): bpb = 8 parity = 0 stop = 1 - length_of_test = sum(len(cmd) for cmd in midi_commands) + length_of_test = sum(len(cmd) for cmd in midi_command_expected) simthreads = [ UARTTxChecker(tx_port, parity, baud, length_of_test, stop, bpb, debug=False) ] - simargs = ["--max-cycles", str(MAX_CYCLES), "-o", "trace.txt"] + simargs = ["--max-cycles", str(MAX_CYCLES), "--trace-to", "trace.txt"] #This is just for local debug so we can capture the traces if needed. It slows xsim down so not needed # simargs.extend(["--vcd-tracing", "-tile tile[1] -ports -o trace.vcd"]) @@ -60,6 +59,7 @@ def test_tx(capfd, config, build_midi): timeout=120, simargs=simargs, ) + capture = capfd.readouterr().out result = tester.run(capture.split("\n")) diff --git a/tests/uart_tx_checker.py b/tests/uart_tx_checker.py index e0476c76..c6ef7869 100644 --- a/tests/uart_tx_checker.py +++ b/tests/uart_tx_checker.py @@ -94,7 +94,9 @@ class UARTTxChecker(px.SimThread): if self.debug: print("tx starts high: %s" % ("True" if initial_port_val else "False")) for x in range(length): - packet.append(chr(self.read_byte(xsi, parity))) + byte = self.read_byte(xsi, parity) + if self.debug: print(f"Checker got byte: {byte}") + packet.append(chr(byte)) return packet def read_byte(self, xsi, parity): From ebf4d0e3879530eb21774691099357aa842a2c2c Mon Sep 17 00:00:00 2001 From: Ed Date: Mon, 29 Apr 2024 12:16:25 +0100 Subject: [PATCH 08/13] Add test for zero packet on egress to avoid invalid FIFO pop --- lib_xua/src/midi/usb_midi.xc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib_xua/src/midi/usb_midi.xc b/lib_xua/src/midi/usb_midi.xc index a7a97cac..437a0e12 100644 --- a/lib_xua/src/midi/usb_midi.xc +++ b/lib_xua/src/midi/usb_midi.xc @@ -13,7 +13,7 @@ #include "iap_user.h" #include "coprocessor_user.h" #endif -//#define MIDI_LOOPBACK 1 + int icount = 0; static unsigned makeSymbol(unsigned data) { @@ -89,7 +89,7 @@ void usb_midi( struct midi_in_parse_state mips; - // the symbol fifo (to go out of uart) + // the symbol fifo (to go out of uart). queue_t symbol_fifo; unsigned symbol_fifo_arr[USB_MIDI_DEVICE_OUT_FIFO_SIZE]; // Used for 32bit USB MIDI events @@ -264,7 +264,7 @@ void usb_midi( } break; #endif - + // Received as packet from USB case !authenticating => midi_get_ack_or_data(c_midi, is_ack, datum): if (is_ack) @@ -281,6 +281,7 @@ void usb_midi( } } else + // A midi packet from the host { unsigned midi[3]; unsigned size; @@ -327,7 +328,7 @@ void usb_midi( midi_from_host_overflow = 1; } // Drop through to the isTX guarded case - if (!isTX) + if (!isTX && size > 0) // do not start tx'ing if this packet has no size { t :> txT; // Should be enough to trigger the other case isTX = 1; From 509cd4fc2ebee5abcee5242969f9e4815f33215b Mon Sep 17 00:00:00 2001 From: Ed Date: Mon, 29 Apr 2024 12:24:09 +0100 Subject: [PATCH 09/13] Copyright --- lib_xua/src/midi/usb_midi.xc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_xua/src/midi/usb_midi.xc b/lib_xua/src/midi/usb_midi.xc index 437a0e12..7f876a42 100644 --- a/lib_xua/src/midi/usb_midi.xc +++ b/lib_xua/src/midi/usb_midi.xc @@ -1,4 +1,4 @@ -// Copyright 2011-2022 XMOS LIMITED. +// Copyright 2011-2024 XMOS LIMITED. // This Software is subject to the terms of the XMOS Public Licence: Version 1. #include #include From 3f00a9ae103ba0a8d530f4658e10ec22409be4ec Mon Sep 17 00:00:00 2001 From: Ed Date: Mon, 29 Apr 2024 13:33:22 +0100 Subject: [PATCH 10/13] Correct a comment --- lib_xua/src/midi/usb_midi.xc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_xua/src/midi/usb_midi.xc b/lib_xua/src/midi/usb_midi.xc index 7f876a42..a81b235f 100644 --- a/lib_xua/src/midi/usb_midi.xc +++ b/lib_xua/src/midi/usb_midi.xc @@ -91,7 +91,7 @@ void usb_midi( // the symbol fifo (to go out of uart). queue_t symbol_fifo; - unsigned symbol_fifo_arr[USB_MIDI_DEVICE_OUT_FIFO_SIZE]; // Used for 32bit USB MIDI events + unsigned symbol_fifo_arr[USB_MIDI_DEVICE_OUT_FIFO_SIZE]; // Used for outgoing UART symbols (which include the start and stop bit) unsigned rxPT, txPT; int midi_from_host_overflow = 0; From d8cc579518d93d20974c0f55b42f202d4b3a7ecb Mon Sep 17 00:00:00 2001 From: Ed Date: Mon, 29 Apr 2024 15:05:26 +0100 Subject: [PATCH 11/13] Extend queue unit test for under/overflow --- .../src/test_midi_queue/test_midi_queue.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/xua_unit_tests/src/test_midi_queue/test_midi_queue.c b/tests/xua_unit_tests/src/test_midi_queue/test_midi_queue.c index ae48988c..f67fed44 100644 --- a/tests/xua_unit_tests/src/test_midi_queue/test_midi_queue.c +++ b/tests/xua_unit_tests/src/test_midi_queue/test_midi_queue.c @@ -2,6 +2,7 @@ // This Software is subject to the terms of the XMOS Public Licence: Version 1. #include #include +#include #include "xua_unit_tests.h" #include "../../../lib_xua/src/midi/queue.h" @@ -25,6 +26,8 @@ unsigned rndm = RANDOM_SEED; void test_midi_queue_init(void) { queue_t symbol_fifo; unsigned symbol_fifo_storage[USB_MIDI_DEVICE_OUT_FIFO_SIZE]; + memset(symbol_fifo_storage, USB_MIDI_DEVICE_OUT_FIFO_SIZE * sizeof(unsigned), 0xdb); // Non zero + queue_init_c_wrapper(&symbol_fifo, ARRAY_SIZE(symbol_fifo_storage)); int empty = queue_is_empty_c_wrapper(&symbol_fifo); @@ -38,6 +41,13 @@ void test_midi_queue_init(void) { unsigned space = queue_space_c_wrapper(&symbol_fifo); TEST_ASSERT_EQUAL_UINT32(USB_MIDI_DEVICE_OUT_FIFO_SIZE, space); + + // Pop empty queue + unsigned entry = queue_pop_word_c_wrapper(&symbol_fifo, symbol_fifo_storage); + TEST_ASSERT_EQUAL_UINT32(MIDI_OUT_NULL_MESSAGE, entry); + + space = queue_space_c_wrapper(&symbol_fifo); + TEST_ASSERT_EQUAL_UINT32(USB_MIDI_DEVICE_OUT_FIFO_SIZE, space); } void test_midi_queue_full(void) { @@ -46,7 +56,7 @@ void test_midi_queue_full(void) { queue_init_c_wrapper(&symbol_fifo, ARRAY_SIZE(symbol_fifo_storage)); for(unsigned i = 0; i < USB_MIDI_DEVICE_OUT_FIFO_SIZE; i++){ - queue_push_word_c_wrapper(&symbol_fifo, symbol_fifo_storage, 0); + queue_push_word_c_wrapper(&symbol_fifo, symbol_fifo_storage, 1111); } int empty = queue_is_empty_c_wrapper(&symbol_fifo); @@ -60,6 +70,12 @@ void test_midi_queue_full(void) { unsigned space = queue_space_c_wrapper(&symbol_fifo); TEST_ASSERT_EQUAL_UINT32(0, space); + + // We want no exception here and this to be ignored + queue_push_word_c_wrapper(&symbol_fifo, symbol_fifo_storage, 2222); + + unsigned entry = queue_pop_word_c_wrapper(&symbol_fifo, symbol_fifo_storage); + TEST_ASSERT_EQUAL_UINT32(1111, entry); } void test_midi_queue_push_pop(void) { From 6c49d5fe36844affba9d00ade637bb129d4fcdaa Mon Sep 17 00:00:00 2001 From: Ed Date: Tue, 30 Apr 2024 08:21:46 +0100 Subject: [PATCH 12/13] Ue xassert and global assert flag --- lib_xua/src/midi/queue.h | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/lib_xua/src/midi/queue.h b/lib_xua/src/midi/queue.h index 6576a070..053e6013 100644 --- a/lib_xua/src/midi/queue.h +++ b/lib_xua/src/midi/queue.h @@ -4,12 +4,8 @@ #define QUEUE_H_ #include "midioutparse.h" +#include "xassert.h" -#define assert(x) asm("ecallf %0"::"r"(x)); - -#ifndef MIDI_ENABLE_ASSERTS -#define MIDI_ENABLE_ASSERTS 0 -#endif typedef struct queue_t { /// Read index. @@ -27,7 +23,7 @@ inline int is_power_of_2(unsigned x) { } inline void queue_init(queue_t &q, unsigned size) { - assert(is_power_of_2(size)); // Keep this enabled as will be discovered duirng dev time + xassert(is_power_of_2(size) && "MIDI FIFO size must be a power of 2"); // Keep this enabled as will be discovered duirng dev time q.rdptr = 0; q.wrptr = 0; q.size = size; @@ -46,12 +42,9 @@ inline void queue_push_word(queue_t &q, unsigned array[], unsigned data) { if(queue_is_full(q)) { - if(MIDI_ENABLE_ASSERTS){ - assert(0); - } else { - // Drop message - return; - } + xassert(0 && "Unexpected push to MIDI queue when full"); + // Silently drop message if asserts not enabled + return; } array[q.wrptr++ & q.mask] = data; @@ -59,11 +52,9 @@ inline void queue_push_word(queue_t &q, unsigned array[], unsigned data) inline unsigned queue_pop_word(queue_t &q, unsigned array[]) { if(queue_is_empty(q)){ - if(MIDI_ENABLE_ASSERTS){ - assert(0); - } else { - return MIDI_OUT_NULL_MESSAGE; - } + xassert(0 && "Unexpected pop from MIDI queue when empty"); + // Return NULL messaqe if asserts not enabled + return MIDI_OUT_NULL_MESSAGE; } return array[q.rdptr++ & q.mask]; From 415bd8605d93f2dbc77be78bf2abde3d5ba1a750 Mon Sep 17 00:00:00 2001 From: Ed Date: Tue, 30 Apr 2024 09:58:04 +0100 Subject: [PATCH 13/13] Disable assert in uint test as we intentionally exercise this condition --- tests/xua_unit_tests/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/xua_unit_tests/CMakeLists.txt b/tests/xua_unit_tests/CMakeLists.txt index 8316cf96..194ae1ee 100644 --- a/tests/xua_unit_tests/CMakeLists.txt +++ b/tests/xua_unit_tests/CMakeLists.txt @@ -69,6 +69,7 @@ foreach(TESTFILE ${TEST_SOURCES}) -DUNITY_INCLUDE_DOUBLE -DXUD_CORE_CLOCK=600 -DXUD_SERIES_SUPPORT=4 + -DXASSERT_ENABLE_ASSERTIONS=0 ) # For HID tests only enable HID