diff --git a/c_common/front_end_common_lib/include/buffered_eieio_defs.h b/c_common/front_end_common_lib/include/buffered_eieio_defs.h index 7357c83f7f..7532b9ac7d 100644 --- a/c_common/front_end_common_lib/include/buffered_eieio_defs.h +++ b/c_common/front_end_common_lib/include/buffered_eieio_defs.h @@ -3,7 +3,6 @@ //! The different command message IDs typedef enum eieio_command_messages { - // Database handshake with visualiser DATABASE_CONFIRMATION = 1, @@ -42,9 +41,9 @@ typedef enum buffered_operations { } buffered_operations; //! pointer to an EIEIO message -typedef uint16_t* eieio_msg_t; +typedef uint16_t *eieio_msg_t; // The maximum sequence number -#define MAX_SEQUENCE_NO 0xFF +#define MAX_SEQUENCE_NO 0xFF #endif diff --git a/c_common/models/reverse_iptag_multicast_source/eieio_structures.h b/c_common/models/reverse_iptag_multicast_source/eieio_structures.h new file mode 100644 index 0000000000..9aa6d1cd6a --- /dev/null +++ b/c_common/models/reverse_iptag_multicast_source/eieio_structures.h @@ -0,0 +1,99 @@ +#ifndef _EIEIO_STRUCTURES_H +#define _EIEIO_STRUCTURES_H + +//! The EIEIO message types + +typedef enum eieio_data_message_types { + KEY_16_BIT = 0, + KEY_PAYLOAD_16_BIT = 1, + KEY_32_BIT = 2, + KEY_PAYLOAD_32_BIT = 3 +} eieio_data_message_types; + +// The meaning of the bits in eieio_data_message_types values +#define EIEIO_PKT_HAS_PAYLOAD 0 +#define EIEIO_PKT_32BIT 1 + +//! The EIEIO prefix types +typedef enum eieio_prefix_types { + PREFIX_TYPE_LOWER_HALF_WORD, + PREFIX_TYPE_UPPER_HALF_WORD +} eieio_prefix_types; + +//! The parameter positions +typedef enum read_in_parameters { + APPLY_PREFIX, + PREFIX, + PREFIX_TYPE, + CHECK_KEYS, + HAS_KEY, + KEY_SPACE, + MASK, + BUFFER_REGION_SIZE, + SPACE_BEFORE_DATA_REQUEST, + RETURN_TAG_ID, + RETURN_TAG_DEST, + BUFFERED_IN_SDP_PORT +} read_in_parameters; + +//! The memory regions +typedef enum memory_regions { + SYSTEM, + CONFIGURATION, + RECORDING_REGION, + BUFFER_REGION, + PROVENANCE_REGION, +} memory_regions; + +//! The provenance data items +typedef enum provenance_items { + N_RECEIVED_PACKETS, + N_SENT_PACKETS, + INCORRECT_KEYS, + INCORRECT_PACKETS, + LATE_PACKETS +} provenance_items; + +//! The number of regions that can be recorded +#define NUMBER_OF_REGIONS_TO_RECORD 1 +#define SPIKE_HISTORY_CHANNEL 0 + +//! the minimum space required for a buffer to work +#define MIN_BUFFER_SPACE 10 + +//! the amount of ticks to wait between requests +#define TICKS_BETWEEN_REQUESTS 25 + +//! the maximum size of a packet +#define MAX_PACKET_SIZE 280 + +typedef struct __attribute__((packed)) { + uint32_t length; + uint32_t time; + uint8_t data[MAX_PACKET_SIZE]; +} recorded_packet_t; + +typedef struct __attribute__((packed)) { + uint16_t event; + uint16_t payload; +} event16_t; + +typedef struct __attribute__((packed)) { + uint16_t eieio_header_command; + uint16_t chip_id; + uint8_t processor; + uint8_t pad1; + uint8_t region; + uint8_t sequence; + uint32_t space_available; +} req_packet_sdp_t; + +// Bits in the EIEIO message header +#define PKT_COUNT 0 // 8 bits +#define PKT_TYPE 10 // 2 bits +#define HAS_TIMESTAMP 12 // 1 bit +#define PREFIX_APPLY 13 // 1 bit +#define PKT_MODE 14 // 1 bit +#define WANTS_PREFIX 15 // 1 bit + +#endif //_EIEIO_STRUCTURES_H diff --git a/c_common/models/reverse_iptag_multicast_source/reverse_iptag_multicast_source.c b/c_common/models/reverse_iptag_multicast_source/reverse_iptag_multicast_source.c index 30e7b2b432..c026719a52 100644 --- a/c_common/models/reverse_iptag_multicast_source/reverse_iptag_multicast_source.c +++ b/c_common/models/reverse_iptag_multicast_source/reverse_iptag_multicast_source.c @@ -15,82 +15,16 @@ extern void spin1_wfi(); #error APPLICATION_NAME_HASH must be defined #endif -//! The EIEIO message types +#include "eieio_structures.h" //! \brief human readable versions of the different priorities and usages. typedef enum callback_priorities { - SDP_CALLBACK = 1, TIMER = 2, DMA = 0 -}callback_priorities; - -typedef enum eieio_data_message_types { - KEY_16_BIT, KEY_PAYLOAD_16_BIT, KEY_32_BIT, KEY_PAYLOAD_32_bIT -} eieio_data_message_types; - -//! The EIEIO prefix types -typedef enum eieio_prefix_types { - PREFIX_TYPE_LOWER_HALF_WORD, PREFIX_TYPE_UPPER_HALF_WORD -} eieio_prefix_types; - -//! The parameter positions -typedef enum read_in_parameters{ - APPLY_PREFIX, PREFIX, PREFIX_TYPE, CHECK_KEYS, HAS_KEY, KEY_SPACE, MASK, - BUFFER_REGION_SIZE, SPACE_BEFORE_DATA_REQUEST, RETURN_TAG_ID, - RETURN_TAG_DEST, BUFFERED_IN_SDP_PORT -} read_in_parameters; - -//! The memory regions -typedef enum memory_regions{ - SYSTEM, - CONFIGURATION, - RECORDING_REGION, - BUFFER_REGION, - PROVENANCE_REGION, -} memory_regions; - -//! The provenance data items -typedef enum provenance_items { - N_RECEIVED_PACKETS, - N_SENT_PACKETS, - INCORRECT_KEYS, - INCORRECT_PACKETS, - LATE_PACKETS -} provenance_items; - -//! The number of regions that can be recorded -#define NUMBER_OF_REGIONS_TO_RECORD 1 -#define SPIKE_HISTORY_CHANNEL 0 - -//! the minimum space required for a buffer to work -#define MIN_BUFFER_SPACE 10 - -//! the amount of ticks to wait between requests -#define TICKS_BETWEEN_REQUESTS 25 - -//! the maximum size of a packet -#define MAX_PACKET_SIZE 280 - -#pragma pack(1) - -typedef struct { - uint32_t length; - uint32_t time; - uint8_t data[MAX_PACKET_SIZE]; -} recorded_packet_t; - -typedef struct { - uint16_t event; - uint16_t payload; -} event16_t; - -typedef struct { - uint16_t eieio_header_command; - uint16_t chip_id; - uint8_t processor; - uint8_t pad1; - uint8_t region; - uint8_t sequence; - uint32_t space_available; -} req_packet_sdp_t; + SDP_CALLBACK = 1, + TIMER = 2, + DMA = 0 +} callback_priorities; + +//--------------------------------------------------------------------------- // Globals static uint32_t time; @@ -140,8 +74,49 @@ static bool stopped = false; static bool recording_in_progress = false; static recorded_packet_t *recorded_packet; +//--------------------------------------------------------------------------- + +#define UINT(width, value) ((uint##width##_t) (value)) + +//! Select a particular bit from a value, as a boolean +static inline bool test_bit( + uint16_t value, uint16_t bit_index) +{ + assert(bit_index <= 15); + return (bool) ((value >> bit_index) & 0x1); +} + + +//! Select a range of bits from a value, as a byte +static inline uint8_t pick_bits( + uint16_t value, uint16_t low_bit_index, uint16_t bit_count) +{ + assert(bit_count <= 8); + assert(low_bit_index <= 15); + uint16_t selector = (1 << bit_count) - 1; + return UINT(8, (value >> low_bit_index) & selector); +} + +//! Combine a pair of shorts (in an array) into a 32-bit number +static inline uint32_t pack_shorts( + uint16_t *buffer) +{ + return (UINT(32, buffer[1]) << 16) | UINT(32, buffer[0]); +} + +//! How to subtract one pointer from another. +static inline uint32_t space( + uint8_t *from, uint8_t *to) +{ + assert(from <= to); + return UINT(32, to) - UINT(32, from); +} + +//--------------------------------------------------------------------------- + static inline uint16_t calculate_eieio_packet_command_size( - eieio_msg_t eieio_msg_ptr) { + eieio_msg_t eieio_msg_ptr) +{ uint16_t data_hdr_value = eieio_msg_ptr[0]; uint16_t command_number = data_hdr_value & ~0xC000; @@ -151,74 +126,79 @@ static inline uint16_t calculate_eieio_packet_command_size( case EVENT_STOP_COMMANDS: case STOP_SENDING_REQUESTS: case START_SENDING_REQUESTS: - return 2; + return 2; case SPINNAKER_REQUEST_BUFFERS: - return 12; + return 12; case HOST_SEND_SEQUENCED_DATA: - - // does not include the EIEIO packet payload - return 4; + // does not include the EIEIO packet payload + return 4; case SPINNAKER_REQUEST_READ_DATA: - return 16; + return 16; case HOST_DATA_READ: - return 8; + return 8; default: - return 0; + return 0; } return 0; } static inline uint16_t calculate_eieio_packet_event_size( - eieio_msg_t eieio_msg_ptr) { + eieio_msg_t eieio_msg_ptr) +{ uint16_t data_hdr_value = eieio_msg_ptr[0]; - uint8_t pkt_type = (uint8_t)(data_hdr_value >> 10 & 0x3); - bool pkt_apply_prefix = (bool)(data_hdr_value >> 15); - bool pkt_payload_prefix_apply = (bool)(data_hdr_value >> 13 & 0x1); - uint8_t event_count = data_hdr_value & 0xFF; + uint8_t pkt_type = pick_bits(data_hdr_value, PKT_TYPE, 2); + bool pkt_apply_prefix = test_bit(data_hdr_value, WANTS_PREFIX); + bool pkt_payload_prefix_apply = test_bit(data_hdr_value, PREFIX_APPLY); + uint8_t event_count = pick_bits(data_hdr_value, PKT_COUNT, 8); uint16_t event_size, total_size; uint16_t header_size = 2; switch (pkt_type) { case KEY_16_BIT: - event_size = 2; - break; + event_size = 2; + break; case KEY_PAYLOAD_16_BIT: case KEY_32_BIT: - event_size = 4; - break; - case KEY_PAYLOAD_32_bIT: - event_size = 8; - break; + event_size = 4; + break; + case KEY_PAYLOAD_32_BIT: + event_size = 8; + break; } if (pkt_apply_prefix) { - header_size += 2; + header_size += 2; } if (pkt_payload_prefix_apply) { - if (pkt_type == 0 || pkt_type == 1) { - header_size += 2; - } else { - header_size += 4; - } + if (test_bit(!pkt_type, EIEIO_PKT_32BIT)) { + header_size += 2; + } else { + header_size += 4; + } } total_size = event_count * event_size + header_size; return total_size; } -static inline uint16_t calculate_eieio_packet_size(eieio_msg_t eieio_msg_ptr) { +static inline uint16_t calculate_eieio_packet_size( + eieio_msg_t eieio_msg_ptr) +{ uint16_t data_hdr_value = eieio_msg_ptr[0]; - uint8_t pkt_type = (data_hdr_value >> 14) & 0x03; + uint8_t pkt_type = pick_bits(data_hdr_value, 14, 2); - if (pkt_type == 0x01) { - return calculate_eieio_packet_command_size(eieio_msg_ptr); + if (pkt_type == KEY_PAYLOAD_16_BIT) { + return calculate_eieio_packet_command_size(eieio_msg_ptr); } else { - return calculate_eieio_packet_event_size(eieio_msg_ptr); + return calculate_eieio_packet_event_size(eieio_msg_ptr); } } +//--------------------------------------------------------------------------- + static inline void print_packet_bytes( - eieio_msg_t eieio_msg_ptr, uint16_t length) { + eieio_msg_t eieio_msg_ptr, uint16_t length) +{ use(eieio_msg_ptr); use(length); #if LOG_LEVEL >= LOG_DEBUG @@ -227,16 +207,18 @@ static inline void print_packet_bytes( log_debug("packet of %d bytes:", length); for (int i = 0; i < length; i++) { - if ((i & 7) == 0) { - io_printf(IO_BUF, "\n"); - } - io_printf(IO_BUF, "%02x", ptr[i]); + if ((i & 7) == 0) { + io_printf(IO_BUF, "\n"); + } + io_printf(IO_BUF, "%02x", ptr[i]); } io_printf(IO_BUF, "\n"); #endif } -static inline void print_packet(eieio_msg_t eieio_msg_ptr) { +static inline void print_packet( + eieio_msg_t eieio_msg_ptr) +{ use(eieio_msg_ptr); #if LOG_LEVEL >= LOG_DEBUG uint32_t len = calculate_eieio_packet_size(eieio_msg_ptr); @@ -245,7 +227,8 @@ static inline void print_packet(eieio_msg_t eieio_msg_ptr) { } static inline void signal_software_error( - eieio_msg_t eieio_msg_ptr, uint16_t length) { + eieio_msg_t eieio_msg_ptr, uint16_t length) +{ use(eieio_msg_ptr); use(length); #if LOG_LEVEL >= LOG_DEBUG @@ -254,183 +237,191 @@ static inline void signal_software_error( #endif } -static inline uint32_t get_sdram_buffer_space_available() { +//--------------------------------------------------------------------------- + +static inline uint32_t get_sdram_buffer_space_available(void) +{ if (read_pointer < write_pointer) { - uint32_t final_space = - (uint32_t) end_of_buffer_region - (uint32_t) write_pointer; - uint32_t initial_space = - (uint32_t) read_pointer - (uint32_t) buffer_region; - return final_space + initial_space; + uint32_t final_space = space(write_pointer, end_of_buffer_region); + uint32_t initial_space = space(buffer_region, read_pointer); + return final_space + initial_space; } else if (write_pointer < read_pointer) { - return (uint32_t) read_pointer - (uint32_t) write_pointer; + return space(write_pointer, read_pointer); } else if (last_buffer_operation == BUFFER_OPERATION_WRITE) { - - // If pointers are equal, buffer is full if last operation is write - return 0; + // If pointers are equal, buffer is full if last operation is write + return 0; } else { - - // If pointers are equal, buffer is empty if last operation is read - return buffer_region_size; + // If pointers are equal, buffer is empty if last operation is read + return buffer_region_size; } } -static inline bool is_eieio_packet_in_buffer(void) { - +static inline bool is_eieio_packet_in_buffer(void) +{ // If there is no buffering being done, there are no packets if (buffer_region_size == 0) { - return false; + return false; } // There are packets as long as the buffer is not empty; the buffer is // empty if the pointers are equal and the last operation was read - return !((write_pointer == read_pointer) && - (last_buffer_operation == BUFFER_OPERATION_READ)); + return (write_pointer != read_pointer) + || (last_buffer_operation == BUFFER_OPERATION_WRITE); } -static inline uint32_t extract_time_from_eieio_msg(eieio_msg_t eieio_msg_ptr) { +static inline uint32_t extract_time_from_eieio_msg( + eieio_msg_t eieio_msg_ptr) +{ uint16_t data_hdr_value = eieio_msg_ptr[0]; - bool pkt_has_timestamp = (bool) ((data_hdr_value >> 12) & 0x1); - bool pkt_apply_prefix = (bool) ((data_hdr_value >> 15) & 0x1); - bool pkt_mode = (bool) ((data_hdr_value >> 14) & 0x1); + bool pkt_has_timestamp = test_bit(data_hdr_value, HAS_TIMESTAMP); + bool pkt_apply_prefix = test_bit(data_hdr_value, WANTS_PREFIX); + bool pkt_mode = test_bit(data_hdr_value, PKT_MODE); // If the packet is actually a command packet, return the current time if (!pkt_apply_prefix && pkt_mode) { - return time; + return time; } - // If the packet indicates that payloads are timestamps - if (pkt_has_timestamp) { - bool pkt_payload_prefix_apply = (bool) ((data_hdr_value >> 13) & 0x1); - uint8_t pkt_type = (uint8_t) ((data_hdr_value >> 10) & 0x3); - uint32_t payload_time = 0; - bool got_payload_time = false; - uint16_t *event_ptr = &eieio_msg_ptr[1]; - - // If there is a payload prefix - if (pkt_payload_prefix_apply) { - - // If there is a key prefix, the payload prefix is after that - if (pkt_apply_prefix) { - event_ptr += 1; - } - - if (pkt_type & 0x2) { - - // 32 bit packet - payload_time = (event_ptr[1] << 16) | event_ptr[0]; - event_ptr += 2; - } else { - - // 16 bit packet - payload_time = event_ptr[0]; - event_ptr += 1; - } - got_payload_time = true; - } - - // If the packets have a payload - if (pkt_type & 0x1) { - if (pkt_type & 0x2) { + // If this is not a timed packet, return the current time + if (!pkt_has_timestamp) { + return time; + } - // 32 bit packet - payload_time |= (event_ptr[1] << 16) | event_ptr[0]; - } else { + // The packet has indicated that payloads are timestamps + bool pkt_payload_prefix_apply = test_bit(data_hdr_value, 13); + uint8_t pkt_type = pick_bits(data_hdr_value, PKT_TYPE, 2); + uint32_t payload_time = 0; + bool got_payload_time = false; + uint16_t *event_ptr = &eieio_msg_ptr[1]; - // 16 bit packet - payload_time |= event_ptr[0]; - } - got_payload_time = true; - } + // If there is a payload prefix + if (pkt_payload_prefix_apply) { + // If there is a key prefix, the payload prefix is after that + if (pkt_apply_prefix) { + event_ptr += 1; + } + + if (test_bit(pkt_type, EIEIO_PKT_32BIT)) { + // 32 bit packet + payload_time = pack_shorts(event_ptr); + event_ptr += 2; + } else { + // 16 bit packet + payload_time = event_ptr[0]; + event_ptr += 1; + } + got_payload_time = true; + } - // If no actual time was found, return the current time - if (!got_payload_time) { - return time; - } - return payload_time; + // If the packets have a payload + if (test_bit(pkt_type, EIEIO_PKT_HAS_PAYLOAD)) { + if (test_bit(pkt_type, EIEIO_PKT_32BIT)) { + // 32 bit packet + payload_time |= pack_shorts(event_ptr); + } else { + // 16 bit packet + payload_time |= event_ptr[0]; + } + got_payload_time = true; } - // This is not a timed packet, return the current time - return time; + // If no actual time was found, return the current time + if (!got_payload_time) { + return time; + } + return payload_time; } +//--------------------------------------------------------------------------- + static inline bool add_eieio_packet_to_sdram( - eieio_msg_t eieio_msg_ptr, uint32_t length) { + eieio_msg_t eieio_msg_ptr, uint32_t length) +{ uint8_t *msg_ptr = (uint8_t *) eieio_msg_ptr; log_debug("read_pointer = 0x%.8x, write_pointer= = 0x%.8x," - "last_buffer_operation == read = %d, packet length = %d", - read_pointer, write_pointer, - last_buffer_operation == BUFFER_OPERATION_READ, length); + "last_buffer_operation == read = %d, packet length = %d", + read_pointer, write_pointer, + last_buffer_operation == BUFFER_OPERATION_READ, length); if ((read_pointer < write_pointer) || - (read_pointer == write_pointer && - last_buffer_operation == BUFFER_OPERATION_READ)) { - uint32_t final_space = - (uint32_t) end_of_buffer_region - (uint32_t) write_pointer; - - if (final_space >= length) { - log_debug("Packet fits in final space of %d", final_space); - - spin1_memcpy(write_pointer, msg_ptr, length); - write_pointer += length; - last_buffer_operation = BUFFER_OPERATION_WRITE; - if (write_pointer >= end_of_buffer_region) { - write_pointer = buffer_region; - } - return true; - } else { - - uint32_t total_space = - final_space + - ((uint32_t) read_pointer - (uint32_t) buffer_region); - if (total_space < length) { - log_debug("Not enough space (%d bytes)", total_space); - return false; - } - - log_debug( - "Copying first %d bytes to final space of %d", final_space); - spin1_memcpy(write_pointer, msg_ptr, final_space); - write_pointer = buffer_region; - msg_ptr += final_space; - - uint32_t final_len = length - final_space; - log_debug("Copying remaining %d bytes", final_len); - spin1_memcpy(write_pointer, msg_ptr, final_len); - write_pointer += final_len; - last_buffer_operation = BUFFER_OPERATION_WRITE; - if (write_pointer == end_of_buffer_region) { - write_pointer = buffer_region; - } - return true; - } + (read_pointer == write_pointer && + last_buffer_operation == BUFFER_OPERATION_READ)) { + uint32_t final_space = space(write_pointer, end_of_buffer_region); + + if (final_space >= length) { + log_debug("Packet fits in final space of %d", final_space); + goto doSingleCopy; + } + + uint32_t total_space = final_space + space(buffer_region, read_pointer); + if (total_space < length) { + log_debug("Not enough space (%d bytes)", total_space); + return false; + } + + // Needs two copies + log_debug("Copying first %d bytes to final space of %d", + final_space); + spin1_memcpy(write_pointer, msg_ptr, final_space); + write_pointer = buffer_region; + msg_ptr += final_space; + + uint32_t final_len = length - final_space; + log_debug("Copying remaining %d bytes", final_len); + spin1_memcpy(write_pointer, msg_ptr, final_len); + write_pointer += final_len; } else if (write_pointer < read_pointer) { - uint32_t middle_space = - (uint32_t) read_pointer - (uint32_t) write_pointer; - - if (middle_space < length) { - log_debug("Not enough space in middle (%d bytes)", middle_space); - return false; - } else { - log_debug("Packet fits in middle space of %d", middle_space); - spin1_memcpy(write_pointer, msg_ptr, length); - write_pointer += length; - last_buffer_operation = BUFFER_OPERATION_WRITE; - if (write_pointer == end_of_buffer_region) { - write_pointer = buffer_region; - } - return true; - } + uint32_t middle_space = space(write_pointer, read_pointer); + + if (middle_space < length) { + log_debug("Not enough space in middle (%d bytes)", middle_space); + return false; + } + log_debug("Packet fits in middle space of %d", middle_space); + doSingleCopy: + spin1_memcpy(write_pointer, msg_ptr, length); + write_pointer += length; + } else { + log_debug("Buffer already full"); + return false; } - log_debug("Buffer already full"); - return false; + last_buffer_operation = BUFFER_OPERATION_WRITE; + if (write_pointer == end_of_buffer_region) { + write_pointer = buffer_region; + } + return true; } -static inline void process_16_bit_packets( - void* event_pointer, bool pkt_prefix_upper, uint32_t pkt_count, - uint32_t pkt_key_prefix, uint32_t pkt_payload_prefix, - bool pkt_has_payload, bool pkt_payload_is_timestamp) { +//--------------------------------------------------------------------------- + +static inline bool check_key( + uint32_t key) +{ + return !check || (key & mask) == key_space; +} +static inline void send_packet( + uint32_t key) +{ + while (!spin1_send_mc_packet(key, 0, NO_PAYLOAD)) { + spin1_delay_us(1); + } +} + +static inline void send_packet_payload( + uint32_t key, uint32_t payload) +{ + while (!spin1_send_mc_packet(key, payload, WITH_PAYLOAD)) { + spin1_delay_us(1); + } +} + +static inline void process_16_bit_packets( + void* event_pointer, bool pkt_prefix_upper, uint32_t pkt_count, + uint32_t pkt_key_prefix, uint32_t pkt_payload_prefix, + bool pkt_has_payload, bool pkt_payload_is_timestamp) +{ log_debug("process_16_bit_packets"); log_debug("event_pointer: %08x", (uint32_t) event_pointer); log_debug("count: %d", pkt_count); @@ -441,54 +432,49 @@ static inline void process_16_bit_packets( uint16_t *next_event = (uint16_t *) event_pointer; for (uint32_t i = 0; i < pkt_count; i++) { - uint32_t key = (uint32_t) next_event[0]; - log_debug("Packet key = %d", key); - next_event += 1; - uint32_t payload = 0; - if (pkt_has_payload) { - payload = (uint32_t) next_event[0]; - next_event += 1; - } - - if (!pkt_prefix_upper) { - key <<= 16; - } - key |= pkt_key_prefix; - payload |= pkt_payload_prefix; - - log_debug( - "check before send packet: check=%d, key=0x%08x, mask=0x%08x," - " key_space=%d: %d", check, key, mask, key_space, - (!check) || (check && ((key & mask) == key_space))); - - if (has_key) { - if (!check || (check && ((key & mask) == key_space))) { - n_send_packets += 1; - if (pkt_has_payload && !pkt_payload_is_timestamp) { - log_debug( - "mc packet 16-bit key=%d, payload=%d", key, payload); - while (!spin1_send_mc_packet(key, payload, WITH_PAYLOAD)) { - spin1_delay_us(1); - } - } else { - log_debug( - "mc packet 16-bit key=%d", key); - while (!spin1_send_mc_packet(key, 0, NO_PAYLOAD)) { - spin1_delay_us(1); - } - } - } else { - incorrect_keys++; - } - } + uint32_t key = (uint32_t) next_event[0]; + log_debug("Packet key = %d", key); + next_event++; + uint32_t payload = 0; + if (pkt_has_payload) { + payload = (uint32_t) next_event[0]; + next_event++; + } + + if (!pkt_prefix_upper) { + key <<= 16; + } + key |= pkt_key_prefix; + payload |= pkt_payload_prefix; + + log_debug("check before send packet: check=%d, key=0x%08x, " + "mask=0x%08x, key_space=%d: %d", + check, key, mask, key_space, check_key(key)); + + if (has_key) { + if (!check_key(key)) { + incorrect_keys++; + continue; + } + + n_send_packets += 1; + if (pkt_has_payload && !pkt_payload_is_timestamp) { + log_debug("mc packet 16-bit key=%d, payload=%d", + key, payload); + send_packet_payload(key, payload); + } else { + log_debug("mc packet 16-bit key=%d", key); + send_packet(key); + } + } } } static inline void process_32_bit_packets( - void* event_pointer, uint32_t pkt_count, - uint32_t pkt_key_prefix, uint32_t pkt_payload_prefix, - bool pkt_has_payload, bool pkt_payload_is_timestamp) { - + void* event_pointer, uint32_t pkt_count, uint32_t pkt_key_prefix, + uint32_t pkt_payload_prefix, bool pkt_has_payload, + bool pkt_payload_is_timestamp) +{ log_debug("process_32_bit_packets"); log_debug("event_pointer: %08x", (uint32_t) event_pointer); log_debug("count: %d", pkt_count); @@ -498,49 +484,46 @@ static inline void process_32_bit_packets( uint16_t *next_event = (uint16_t *) event_pointer; for (uint32_t i = 0; i < pkt_count; i++) { - uint32_t key = (next_event[1] << 16) | next_event[0]; - log_debug("Packet key = 0x%08x", key); - next_event += 2; - uint32_t payload = 0; - if (pkt_has_payload) { - payload = (next_event[1] << 16) | next_event[0]; - next_event += 2; - } - - key |= pkt_key_prefix; - payload |= pkt_payload_prefix; - - log_debug("check before send packet: %d", - (!check) || (check && ((key & mask) == key_space))); - - if (has_key) { - if (!check || (check && ((key & mask) == key_space))) { - n_send_packets += 1; - if (pkt_has_payload && !pkt_payload_is_timestamp) { - log_debug( - "mc packet 32-bit key=0x%08x , payload=0x%08x", - key, payload); - while (!spin1_send_mc_packet(key, payload, WITH_PAYLOAD)) { - spin1_delay_us(1); - } - } else { - log_debug("mc packet 32-bit key=0x%08x", key); - while (!spin1_send_mc_packet(key, 0, NO_PAYLOAD)) { - spin1_delay_us(1); - } - } - } else { - incorrect_keys++; - } - } + uint32_t key = pack_shorts(next_event); + log_debug("Packet key = 0x%08x", key); + next_event += 2; + uint32_t payload = 0; + if (pkt_has_payload) { + payload = pack_shorts(next_event); + next_event += 2; + } + + key |= pkt_key_prefix; + payload |= pkt_payload_prefix; + + log_debug("check before send packet: %d", check_key(key)); + + if (has_key) { + if (!check_key(key)) { + incorrect_keys++; + continue; + } + + n_send_packets++; + if (pkt_has_payload && !pkt_payload_is_timestamp) { + log_debug("mc packet 32-bit key=0x%08x , payload=0x%08x", + key, payload); + send_packet_payload(key, payload); + } else { + log_debug("mc packet 32-bit key=0x%08x", key); + send_packet(key); + } + } } } -void recording_done_callback() { +void recording_done_callback(void) +{ recording_in_progress = false; } -static inline void record_packet(eieio_msg_t eieio_msg_ptr, uint32_t length) { +static inline void record_packet( + eieio_msg_t eieio_msg_ptr, uint32_t length) { if (recording_flags > 0) { while (recording_in_progress) { spin1_wfi(); @@ -566,8 +549,11 @@ static inline void record_packet(eieio_msg_t eieio_msg_ptr, uint32_t length) { } } +//--------------------------------------------------------------------------- + static inline bool eieio_data_parse_packet( - eieio_msg_t eieio_msg_ptr, uint32_t length) { + eieio_msg_t eieio_msg_ptr, uint32_t length) +{ log_debug("eieio_data_process_data_packet"); print_packet_bytes(eieio_msg_ptr, length); @@ -575,9 +561,8 @@ static inline bool eieio_data_parse_packet( void *event_pointer = (void *) &eieio_msg_ptr[1]; if (data_hdr_value == 0) { - - // Count is 0, so no data - return true; + // Count is 0, so no data + return true; } log_debug("===================================="); @@ -585,16 +570,16 @@ static inline bool eieio_data_parse_packet( log_debug("event_pointer: %08x", (uint32_t) event_pointer); print_packet(eieio_msg_ptr); - bool pkt_apply_prefix = (bool) ((data_hdr_value >> 15) & 0x1); - bool pkt_prefix_upper = (bool) ((data_hdr_value >> 14) & 0x1); - bool pkt_payload_apply_prefix = (bool) ((data_hdr_value >> 13) & 0x1); - uint8_t pkt_type = (uint8_t) ((data_hdr_value >> 10) & 0x3); - uint8_t pkt_count = (uint8_t) (data_hdr_value & 0xFF); - bool pkt_has_payload = (bool) (pkt_type & 0x1); + bool pkt_apply_prefix = test_bit(data_hdr_value, WANTS_PREFIX); + bool pkt_prefix_upper = test_bit(data_hdr_value, PKT_MODE); + bool pkt_payload_apply_prefix = test_bit(data_hdr_value, PREFIX_APPLY); + uint8_t pkt_type = pick_bits(data_hdr_value, PKT_TYPE, 2); + uint8_t pkt_count = pick_bits(data_hdr_value, PKT_COUNT, 8); + bool pkt_has_payload = test_bit(pkt_type, EIEIO_PKT_HAS_PAYLOAD); uint32_t pkt_key_prefix = 0; uint32_t pkt_payload_prefix = 0; - bool pkt_payload_is_timestamp = (bool)((data_hdr_value >> 12) & 0x1); + bool pkt_payload_is_timestamp = test_bit(data_hdr_value, HAS_TIMESTAMP); log_debug("data_hdr_value: %04x", data_hdr_value); log_debug("pkt_apply_prefix: %d", pkt_apply_prefix); @@ -605,63 +590,52 @@ static inline bool eieio_data_parse_packet( log_debug("pkt_count: %d", pkt_count); log_debug("payload_on: %d", pkt_has_payload); - uint16_t *hdr_pointer = (uint16_t *) event_pointer; + uint16_t *hdr_pointer = event_pointer; if (pkt_apply_prefix) { - - // Key prefix in the packet - pkt_key_prefix = (uint32_t) hdr_pointer[0]; - hdr_pointer += 1; - - // If the prefix is in the upper part, shift the prefix - if (pkt_prefix_upper) { - pkt_key_prefix <<= 16; - } + // Key prefix in the packet + pkt_key_prefix = (uint32_t) hdr_pointer[0]; + hdr_pointer += 1; + + // If the prefix is in the upper part, shift the prefix + if (pkt_prefix_upper) { + pkt_key_prefix <<= 16; + } } else if (!pkt_apply_prefix && apply_prefix) { - - // If there isn't a key prefix, but the config applies a prefix, - // apply the prefix depending on the key_left_shift - pkt_key_prefix = prefix; - if (prefix_type == PREFIX_TYPE_UPPER_HALF_WORD) { - pkt_prefix_upper = true; - } else { - pkt_prefix_upper = false; - } + // If there isn't a key prefix, but the config applies a prefix, + // apply the prefix depending on the key_left_shift + pkt_key_prefix = prefix; + pkt_prefix_upper = (prefix_type == PREFIX_TYPE_UPPER_HALF_WORD); } if (pkt_payload_apply_prefix) { - - if (!(pkt_type & 0x2)) { - - // If there is a payload prefix and the payload is 16-bit - pkt_payload_prefix = (uint32_t) hdr_pointer[0]; - hdr_pointer += 1; - } else { - - // If there is a payload prefix and the payload is 32-bit - pkt_payload_prefix = - (((uint32_t) hdr_pointer[1] << 16) | - (uint32_t) hdr_pointer[0]); - hdr_pointer += 2; - } + if (!test_bit(pkt_type, EIEIO_PKT_32BIT)) { + // If there is a payload prefix and the payload is 16-bit + pkt_payload_prefix = (uint32_t) hdr_pointer[0]; + hdr_pointer += 1; + } else { + // If there is a payload prefix and the payload is 32-bit + pkt_payload_prefix = pack_shorts(hdr_pointer); + hdr_pointer += 2; + } } // Take the event pointer to start at the header pointer - event_pointer = (void *) hdr_pointer; + event_pointer = hdr_pointer; // If the packet has a payload that is a timestamp, but the timestamp // is not the current time, buffer it if (pkt_has_payload && pkt_payload_is_timestamp && - pkt_payload_prefix != time) { - if (pkt_payload_prefix > time) { - add_eieio_packet_to_sdram(eieio_msg_ptr, length); - return true; - } - late_packets += 1; - return false; + pkt_payload_prefix != time) { + if (pkt_payload_prefix > time) { + add_eieio_packet_to_sdram(eieio_msg_ptr, length); + return true; + } + late_packets++; + return false; } - if (pkt_type <= 1) { + if (!test_bit(pkt_type, EIEIO_PKT_32BIT)) { process_16_bit_packets( event_pointer, pkt_prefix_upper, pkt_count, pkt_key_prefix, pkt_payload_prefix, pkt_has_payload, pkt_payload_is_timestamp); @@ -676,8 +650,11 @@ static inline bool eieio_data_parse_packet( } } +//--------------------------------------------------------------------------- + static inline void eieio_command_parse_stop_requests( - eieio_msg_t eieio_msg_ptr, uint16_t length) { + eieio_msg_t eieio_msg_ptr, uint16_t length) +{ use(eieio_msg_ptr); use(length); log_debug("Stopping packet requests - parse_stop_packet_reqs"); @@ -686,7 +663,8 @@ static inline void eieio_command_parse_stop_requests( } static inline void eieio_command_parse_start_requests( - eieio_msg_t eieio_msg_ptr, uint16_t length) { + eieio_msg_t eieio_msg_ptr, uint16_t length) +{ use(eieio_msg_ptr); use(length); log_debug("Starting packet requests - parse_start_packet_reqs"); @@ -694,95 +672,98 @@ static inline void eieio_command_parse_start_requests( } static inline void eieio_command_parse_sequenced_data( - eieio_msg_t eieio_msg_ptr, uint16_t length) { + eieio_msg_t eieio_msg_ptr, uint16_t length) +{ uint16_t sequence_value_region_id = eieio_msg_ptr[1]; - uint16_t region_id = sequence_value_region_id & 0xFF; - uint16_t sequence_value = (sequence_value_region_id >> 8) & 0xFF; + uint16_t region_id = pick_bits(sequence_value_region_id, 0, 8); + uint16_t sequence_value = pick_bits(sequence_value_region_id, 8, 8); uint8_t next_expected_sequence_no = - (pkt_last_sequence_seen + 1) & MAX_SEQUENCE_NO; + (pkt_last_sequence_seen + 1) & MAX_SEQUENCE_NO; eieio_msg_t eieio_content_pkt = &eieio_msg_ptr[2]; if (region_id != BUFFER_REGION) { - log_debug("received sequenced eieio packet with invalid region id:" - " %d.", region_id); - signal_software_error(eieio_msg_ptr, length); - incorrect_packets++; + log_debug("received sequenced eieio packet with invalid region id:" + " %d.", region_id); + signal_software_error(eieio_msg_ptr, length); + incorrect_packets++; } log_debug("Received packet sequence number: %d", sequence_value); if (sequence_value == next_expected_sequence_no) { - - // parse_event_pkt returns false in case there is an error and the - // packet is dropped (i.e. as it was never received) - log_debug("add_eieio_packet_to_sdram"); - bool ret_value = add_eieio_packet_to_sdram(eieio_content_pkt, - length - 4); - log_debug("add_eieio_packet_to_sdram return value: %d", ret_value); - - if (ret_value) { - pkt_last_sequence_seen = sequence_value; - log_debug("Updating last sequence seen to %d", - pkt_last_sequence_seen); - } else { - log_debug("unable to buffer sequenced data packet."); - signal_software_error(eieio_msg_ptr, length); - incorrect_packets++; - } + // parse_event_pkt returns false in case there is an error and the + // packet is dropped (i.e. as it was never received) + log_debug("add_eieio_packet_to_sdram"); + bool ret_value = add_eieio_packet_to_sdram( + eieio_content_pkt, length - 4); + log_debug("add_eieio_packet_to_sdram return value: %d", ret_value); + + if (ret_value) { + pkt_last_sequence_seen = sequence_value; + log_debug("Updating last sequence seen to %d", + pkt_last_sequence_seen); + } else { + log_debug("unable to buffer sequenced data packet."); + signal_software_error(eieio_msg_ptr, length); + incorrect_packets++; + } } } -static inline bool eieio_commmand_parse_packet(eieio_msg_t eieio_msg_ptr, - uint16_t length) { +static inline bool eieio_commmand_parse_packet( + eieio_msg_t eieio_msg_ptr, uint16_t length) +{ uint16_t data_hdr_value = eieio_msg_ptr[0]; uint16_t pkt_command = data_hdr_value & (~0xC000); switch (pkt_command) { case HOST_SEND_SEQUENCED_DATA: - log_debug("command: HOST_SEND_SEQUENCED_DATA"); - eieio_command_parse_sequenced_data(eieio_msg_ptr, length); - break; + log_debug("command: HOST_SEND_SEQUENCED_DATA"); + eieio_command_parse_sequenced_data(eieio_msg_ptr, length); + return true; case STOP_SENDING_REQUESTS: - log_debug("command: STOP_SENDING_REQUESTS"); - eieio_command_parse_stop_requests(eieio_msg_ptr, length); - break; + log_debug("command: STOP_SENDING_REQUESTS"); + eieio_command_parse_stop_requests(eieio_msg_ptr, length); + return true; case START_SENDING_REQUESTS: - log_debug("command: START_SENDING_REQUESTS"); - eieio_command_parse_start_requests(eieio_msg_ptr, length); - break; + log_debug("command: START_SENDING_REQUESTS"); + eieio_command_parse_start_requests(eieio_msg_ptr, length); + return true; case EVENT_STOP_COMMANDS: - log_debug("command: EVENT_STOP"); - stopped = true; - write_pointer = read_pointer; - break; + log_debug("command: EVENT_STOP"); + stopped = true; + write_pointer = read_pointer; + return true; default: - return false; - break; + return false; } - return true; } -static inline bool packet_handler_selector(eieio_msg_t eieio_msg_ptr, - uint16_t length) { +static inline bool packet_handler_selector( + eieio_msg_t eieio_msg_ptr, uint16_t length) +{ log_debug("packet_handler_selector"); uint16_t data_hdr_value = eieio_msg_ptr[0]; - uint8_t pkt_type = (data_hdr_value >> 14) & 0x03; + uint8_t pkt_type = pick_bits(data_hdr_value, 14, 2); - if (pkt_type == 0x01) { - log_debug("parsing a command packet"); - return eieio_commmand_parse_packet(eieio_msg_ptr, length); + if (pkt_type == KEY_PAYLOAD_16_BIT) { + log_debug("parsing a command packet"); + return eieio_commmand_parse_packet(eieio_msg_ptr, length); } else { - log_debug("parsing an event packet"); - return eieio_data_parse_packet(eieio_msg_ptr, length); + log_debug("parsing an event packet"); + return eieio_data_parse_packet(eieio_msg_ptr, length); } } -void fetch_and_process_packet() { +//--------------------------------------------------------------------------- + +void fetch_and_process_packet(void) +{ uint32_t last_len = 2; log_debug("in fetch_and_process_packet"); @@ -791,105 +772,106 @@ void fetch_and_process_packet() { // If we are not buffering, there is nothing to do log_debug("buffer size is %d", buffer_region_size); if (buffer_region_size == 0) { - return; + return; } log_debug("dealing with SDRAM is set to %d", msg_from_sdram_in_use); - log_debug( - "has_eieio_packet_in_buffer set to %d", - is_eieio_packet_in_buffer()); - while ((!msg_from_sdram_in_use) && is_eieio_packet_in_buffer() && - last_len > 0) { - - // If there is padding, move on 2 bytes - uint16_t next_header = (uint16_t) *read_pointer; - if (next_header == 0x4002) { - read_pointer += 2; - if (read_pointer >= end_of_buffer_region) { - read_pointer = buffer_region; - } - } else { - uint8_t *src_ptr = (uint8_t *) read_pointer; - uint8_t *dst_ptr = (uint8_t *) msg_from_sdram; - uint32_t len = calculate_eieio_packet_size( - (eieio_msg_t) read_pointer); - - last_len = len; - if (len > MAX_PACKET_SIZE) { - log_error("Packet from SDRAM of %u bytes is too big!", len); - rt_error(RTE_SWERR); - } - uint32_t final_space = (end_of_buffer_region - read_pointer); - - log_debug("packet with length %d, from address: %08x", len, - read_pointer); - - if (len > final_space) { - - // If the packet is split, get the bits - log_debug("split packet"); - log_debug("1 - reading packet to %08x from %08x length: %d", - (uint32_t) dst_ptr, (uint32_t) src_ptr, final_space); - spin1_memcpy(dst_ptr, src_ptr, final_space); - - uint32_t remaining_len = len - final_space; - dst_ptr += final_space; - src_ptr = buffer_region; - log_debug("2 - reading packet to %08x from %08x length: %d", - (uint32_t) dst_ptr, (uint32_t) src_ptr, - remaining_len); - - spin1_memcpy(dst_ptr, src_ptr, remaining_len); - read_pointer = buffer_region + remaining_len; - } else { - - // If the packet is whole, get the packet - log_debug("full packet"); - log_debug("1 - reading packet to %08x from %08x length: %d", - (uint32_t) dst_ptr, (uint32_t) src_ptr, len); - - spin1_memcpy(dst_ptr, src_ptr, len); - read_pointer += len; - if (read_pointer >= end_of_buffer_region) { - read_pointer = buffer_region; - } - } - - last_buffer_operation = BUFFER_OPERATION_READ; - - print_packet_bytes(msg_from_sdram, len); - next_buffer_time = extract_time_from_eieio_msg(msg_from_sdram); - log_debug("packet time: %d, current time: %d", - next_buffer_time, time); - - if (next_buffer_time <= time) { - packet_handler_selector(msg_from_sdram, len); - } else { - msg_from_sdram_in_use = true; - msg_from_sdram_length = len; - } - } + log_debug("has_eieio_packet_in_buffer set to %d", + is_eieio_packet_in_buffer()); + while (!msg_from_sdram_in_use && is_eieio_packet_in_buffer() + && last_len > 0) { + // If there is padding, move on 2 bytes + uint16_t next_header = (uint16_t) *read_pointer; + if (next_header == 0x4002) { + read_pointer += 2; + if (read_pointer >= end_of_buffer_region) { + read_pointer = buffer_region; + } + continue; + } + + uint8_t *src_ptr = read_pointer; + uint8_t *dst_ptr = (uint8_t *) msg_from_sdram; + uint32_t len = calculate_eieio_packet_size((eieio_msg_t) read_pointer); + + last_len = len; + if (len > MAX_PACKET_SIZE) { + log_error("Packet from SDRAM of %u bytes is too big!", len); + rt_error(RTE_SWERR); + } + uint32_t final_space = space(read_pointer, end_of_buffer_region); + + log_debug("packet with length %d, from address: %08x", len, + read_pointer); + + if (len > final_space) { + // If the packet is split, get the bits + log_debug("split packet"); + log_debug("1 - reading packet to %08x from %08x length: %d", + (uint32_t) dst_ptr, (uint32_t) src_ptr, final_space); + + spin1_memcpy(dst_ptr, src_ptr, final_space); + uint32_t remaining_len = len - final_space; + dst_ptr += final_space; + src_ptr = buffer_region; + log_debug("2 - reading packet to %08x from %08x length: %d", + (uint32_t) dst_ptr, (uint32_t) src_ptr, remaining_len); + + spin1_memcpy(dst_ptr, src_ptr, remaining_len); + read_pointer = buffer_region + remaining_len; + } else { + // If the packet is whole, get the packet + log_debug("full packet"); + log_debug("1 - reading packet to %08x from %08x length: %d", + (uint32_t) dst_ptr, (uint32_t) src_ptr, len); + + spin1_memcpy(dst_ptr, src_ptr, len); + read_pointer += len; + if (read_pointer >= end_of_buffer_region) { + read_pointer = buffer_region; + } + } + + last_buffer_operation = BUFFER_OPERATION_READ; + + print_packet_bytes(msg_from_sdram, len); + next_buffer_time = extract_time_from_eieio_msg(msg_from_sdram); + log_debug("packet time: %d, current time: %d", + next_buffer_time, time); + + if (next_buffer_time <= time) { + packet_handler_selector(msg_from_sdram, len); + } else { + msg_from_sdram_in_use = true; + msg_from_sdram_length = len; + } } } -void send_buffer_request_pkt(void) { +//--------------------------------------------------------------------------- + +void send_buffer_request_pkt(void) +{ uint32_t space = get_sdram_buffer_space_available(); if ((space >= space_before_data_request) && - ((space != last_space) || (space == buffer_region_size))) { - log_debug("sending request packet with space: %d and seq_no: %d at %u", - space, pkt_last_sequence_seen, time); - - last_space = space; - req_ptr->sequence |= pkt_last_sequence_seen; - req_ptr->space_available = space; - spin1_send_sdp_msg(&req, 1); - req_ptr->sequence &= 0; - req_ptr->space_available = 0; + ((space != last_space) || (space == buffer_region_size))) { + log_debug("sending request packet with space: %d and seq_no: %d at %u", + space, pkt_last_sequence_seen, time); + + last_space = space; + req_ptr->sequence |= pkt_last_sequence_seen; + req_ptr->space_available = space; + spin1_send_sdp_msg(&req, 1); + req_ptr->sequence = 0; + req_ptr->space_available = 0; } } -bool read_parameters(address_t region_address) { +//--------------------------------------------------------------------------- +bool read_parameters( + address_t region_address) +{ // Get the configuration data apply_prefix = region_address[APPLY_PREFIX]; prefix = region_address[PREFIX]; @@ -907,7 +889,7 @@ bool read_parameters(address_t region_address) { // There is no point in sending requests until there is space for // at least one packet if (space_before_data_request < MIN_BUFFER_SPACE) { - space_before_data_request = MIN_BUFFER_SPACE; + space_before_data_request = MIN_BUFFER_SPACE; } // Set the initial values @@ -919,11 +901,9 @@ bool read_parameters(address_t region_address) { send_packet_reqs = true; last_request_tick = 0; - if (buffer_region_size != 0) { - last_buffer_operation = BUFFER_OPERATION_WRITE; - } else { - last_buffer_operation = BUFFER_OPERATION_READ; - } + last_buffer_operation = (buffer_region_size != 0 + ? BUFFER_OPERATION_WRITE + : BUFFER_OPERATION_READ); // allocate a buffer size of the maximum SDP payload size msg_from_sdram = (eieio_msg_t) spin1_malloc(MAX_PACKET_SIZE); @@ -957,7 +937,11 @@ bool read_parameters(address_t region_address) { return true; } -bool setup_buffer_region(address_t region_address) { +//--------------------------------------------------------------------------- + +bool setup_buffer_region( + address_t region_address) +{ buffer_region = (uint8_t *) region_address; read_pointer = buffer_region; write_pointer = buffer_region; @@ -970,12 +954,15 @@ bool setup_buffer_region(address_t region_address) { return true; } +//--------------------------------------------------------------------------- + //! \brief Initialises the recording parts of the model //! \return True if recording initialisation is successful, false otherwise -static bool initialise_recording(){ +static bool initialise_recording(void) +{ address_t address = data_specification_get_data_address(); address_t recording_region = data_specification_get_region( - RECORDING_REGION, address); + RECORDING_REGION, address); log_info("Recording starts at 0x%08x", recording_region); @@ -984,7 +971,11 @@ static bool initialise_recording(){ return success; } -static void provenance_callback(address_t address) { +//--------------------------------------------------------------------------- + +static void provenance_callback( + address_t address) +{ address[N_RECEIVED_PACKETS] = n_received_packets; address[N_SENT_PACKETS] = n_send_packets; address[INCORRECT_KEYS] = incorrect_keys; @@ -992,54 +983,57 @@ static void provenance_callback(address_t address) { address[LATE_PACKETS] = late_packets; } -bool initialise(uint32_t *timer_period) { +//--------------------------------------------------------------------------- +bool initialise( + uint32_t *timer_period) +{ // Get the address this core's DTCM data starts at from SRAM address_t address = data_specification_get_data_address(); // Read the header if (!data_specification_read_header(address)) { - return false; + return false; } // Get the timing details and set up the simulation interface if (!simulation_initialise( - data_specification_get_region(SYSTEM, address), - APPLICATION_NAME_HASH, timer_period, &simulation_ticks, - &infinite_run, SDP_CALLBACK, DMA)) { - return false; + data_specification_get_region(SYSTEM, address), + APPLICATION_NAME_HASH, timer_period, &simulation_ticks, + &infinite_run, SDP_CALLBACK, DMA)) { + return false; } simulation_set_provenance_function( - provenance_callback, - data_specification_get_region(PROVENANCE_REGION, address)); + provenance_callback, + data_specification_get_region(PROVENANCE_REGION, address)); // Read the parameters if (!read_parameters( - data_specification_get_region(CONFIGURATION, address))) { - return false; + data_specification_get_region(CONFIGURATION, address))) { + return false; } // set up recording data structures if (!initialise_recording()) { - return false; + return false; } // Read the buffer region - if (buffer_region_size > 0) { - if (!setup_buffer_region(data_specification_get_region( - BUFFER_REGION, address))) { - return false; - } + if (buffer_region_size > 0 && !setup_buffer_region( + data_specification_get_region(BUFFER_REGION, address))) { + return false; } return true; } -void resume_callback() { +//--------------------------------------------------------------------------- +void resume_callback(void) +{ address_t address = data_specification_get_data_address(); setup_buffer_region(data_specification_get_region( - BUFFER_REGION, address)); + BUFFER_REGION, address)); // set the code to start sending packet requests again send_packet_reqs = true; @@ -1054,66 +1048,73 @@ void resume_callback() { stopped = false; } -void timer_callback(uint unused0, uint unused1) { +//--------------------------------------------------------------------------- + +void timer_callback( + uint unused0, uint unused1) +{ use(unused0); use(unused1); time++; log_debug("timer_callback, final time: %d, current time: %d," - "next packet buffer time: %d", simulation_ticks, time, - next_buffer_time); + "next packet buffer time: %d", simulation_ticks, time, + next_buffer_time); if (stopped || ((infinite_run != TRUE) && (time >= simulation_ticks))) { - // Wait for recording to finish while (recording_in_progress) { spin1_wfi(); } - // Enter pause and resume state to avoid another tick - simulation_handle_pause_resume(resume_callback); + // Enter pause and resume state to avoid another tick + simulation_handle_pause_resume(resume_callback); - // close recording channels - if (recording_flags > 0) { - recording_finalise(); - } + // close recording channels + if (recording_flags > 0) { + recording_finalise(); + } - log_debug("Last time of stop notification request: %d", - last_stop_notification_request); + log_debug("Last time of stop notification request: %d", + last_stop_notification_request); - // Subtract 1 from the time so this tick gets done again on the next - // run - time = simulation_ticks - 1; + // Subtract 1 from the time so this tick gets done again on the next + // run + time = simulation_ticks - 1; - return; + return; } if (send_packet_reqs && - ((time - last_request_tick) >= TICKS_BETWEEN_REQUESTS)) { - send_buffer_request_pkt(); - last_request_tick = time; + (time - last_request_tick >= TICKS_BETWEEN_REQUESTS)) { + send_buffer_request_pkt(); + last_request_tick = time; } if (!msg_from_sdram_in_use) { - fetch_and_process_packet(); + fetch_and_process_packet(); } else if (next_buffer_time < time) { - late_packets += 1; - fetch_and_process_packet(); + late_packets += 1; + fetch_and_process_packet(); } else if (next_buffer_time == time) { - eieio_data_parse_packet(msg_from_sdram, msg_from_sdram_length); - fetch_and_process_packet(); + eieio_data_parse_packet(msg_from_sdram, msg_from_sdram_length); + fetch_and_process_packet(); } if (recording_flags > 0) { - recording_do_timestep_update(time); + recording_do_timestep_update(time); } } -void sdp_packet_callback(uint mailbox, uint port) { +//--------------------------------------------------------------------------- + +void sdp_packet_callback( + uint mailbox, uint port) +{ use(port); sdp_msg_t *msg = (sdp_msg_t *) mailbox; uint16_t length = msg->length; - eieio_msg_t eieio_msg_ptr = (eieio_msg_t) &(msg->cmd_rc); + eieio_msg_t eieio_msg_ptr = (eieio_msg_t) &msg->cmd_rc; n_received_packets += 1; @@ -1123,14 +1124,16 @@ void sdp_packet_callback(uint mailbox, uint port) { spin1_msg_free(msg); } -// Entry point -void c_main(void) { +//--------------------------------------------------------------------------- +// Entry point +void c_main(void) +{ // Configure system uint32_t timer_period = 0; if (!initialise(&timer_period)) { - rt_error(RTE_SWERR); - return; + rt_error(RTE_SWERR); + return; } // Set timer_callback